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