1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
#include "stdafx.h"
Map::Map(void)
{
height = SCREEN_HEIGHT/TILE_HEIGHT;
width = SCREEN_WIDTH/TILE_WIDTH;
sx = 0;
sy = 0;
//Passability
tileset[0] = 0;
tileset[1] = 0;
tileset[2] = 0;
tileset[3] = 0;
tileset[4] = 0;
tileset[5] = 0;
tileset[6] = 1;
tileset[7] = 1;
tileset[8] = 1;
tileset[9] = 1;
tileset[10] = 1;
tileset[11] = 1;
tileset[12] = 1;
tileset[13] = 1;
tileset[14] = 1;
//Get the Sheet
sheet = IMG_Load("./sprites/tilesheet.png");
// sheet = SDL_DisplayFormat(raw_sprite);
// SDL_FreeSurface(raw_sprite);
Uint32 colorkey = SDL_MapRGB(sheet->format, 255, 0, 255);
SDL_SetColorKey(sheet, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorkey);
std::ifstream in("./map0.bin", std::ios::in | std::ios::binary);
int length;
//get the size
in.seekg (0, std::ios::end);
length = in.tellg();
in.seekg (0, std::ios::beg);
in.read((char *) &tiles, length);
// see how many bytes have been read
std::cout << in.gcount() << " bytes read\n";
in.close();
}
void Map::drawmap(SDL_Surface *viewport){
int x1, x2, y1, y2, tx, ty;
tx = sx/TILE_WIDTH;
ty = sy/TILE_HEIGHT;
x1 = (sx%TILE_WIDTH) * -1;
x2 = x1 + SCREEN_WIDTH + (x1 == 0 ? 0 : TILE_WIDTH);
y1 = (sy%TILE_HEIGHT) * -1;
y2 = y1 + SCREEN_HEIGHT + (y1 == 0 ? 0 : TILE_WIDTH);
for(int y = y1; y<y2; y+=TILE_HEIGHT){
tx = sx/TILE_WIDTH;
for(int x = x1; x<x2; x+=TILE_WIDTH){
this->draw_tile(viewport, tiles[ty][tx], x, y);
tx++;
}
ty++;
}
}
void Map::draw_tile(SDL_Surface *viewport, int type, int x, int y){
if(type > 14 || type < 0){
type = 0;
}
int x_tile, y_tile;
x_tile = floor(type / 3) * TILE_HEIGHT;
y_tile = (type % 3) * TILE_WIDTH;
/*Draws a rectangular surface from sx,sy of swxsh dimensions to dx, dy*/
Gfx::drawsurface(y_tile, x_tile, TILE_WIDTH, TILE_HEIGHT, this->sheet,
x, y, viewport);
// switch (type) {
//
//
//
// case 0:
// case 1:
// case 2:
// /*boxRGBA(viewport,
// x, y,
// x+25, y+25,
// 255, 255, 255, 255);*/
// break;
// case 3:
// case 4:
// case 5:
// boxRGBA(viewport,
// x, y,
// x+TILE_WIDTH, y+TILE_HEIGHT,
// 238, 0, 139, 255);
// break;
// case 6:
// case 7:
// case 8:
// boxRGBA(viewport,
// x, y,
// x+TILE_WIDTH, y+TILE_HEIGHT,
// 34, 34, 34, 255);
// break;
// }
}
int Map::get_passability(int x, int y){
return tileset[tiles[(sy+y)/TILE_HEIGHT][(sx+x)/TILE_WIDTH]];
}
int Map::get_tile(int x, int y){
return tiles[(sy+y)/TILE_HEIGHT][(sx+x)/TILE_WIDTH];
}
int Map::get_sx(){
return sx;
}
int Map::get_sy(){
return sy;
}
void Map::set_sx(int x){
sx = x;
}
void Map::set_sy(int y){
sy = y;
}
Map::~Map(void){
}
|