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