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