diff options
| author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2015-02-17 13:28:50 -0500 |
|---|---|---|
| committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2015-02-17 13:28:50 -0500 |
| commit | 6b99c7aee539e35b8e67520f36adeca9007641cb (patch) | |
| tree | 7e265900e63512889109048047ee6a1b103c4339 /src/map.cpp | |
| parent | 783b308990e7c4ef0837a102a138778f73e4d2b7 (diff) | |
| download | therapy-6b99c7aee539e35b8e67520f36adeca9007641cb.tar.gz therapy-6b99c7aee539e35b8e67520f36adeca9007641cb.tar.bz2 therapy-6b99c7aee539e35b8e67520f36adeca9007641cb.zip | |
Refactored map loader and added a second map
Also tweaked the font for apostrophe, p, and q
Diffstat (limited to 'src/map.cpp')
| -rw-r--r-- | src/map.cpp | 257 |
1 files changed, 30 insertions, 227 deletions
| diff --git a/src/map.cpp b/src/map.cpp index 0dd3e04..cb1ce72 100644 --- a/src/map.cpp +++ b/src/map.cpp | |||
| @@ -1,252 +1,55 @@ | |||
| 1 | #include "map.h" | 1 | #include "map.h" |
| 2 | #include "mapview.h" | ||
| 2 | 3 | ||
| 3 | struct platform_t { | 4 | Map::Map(char* filename) |
| 4 | int x; | ||
| 5 | int y; | ||
| 6 | int w; | ||
| 7 | int h; | ||
| 8 | bool enter_from_left; | ||
| 9 | bool enter_from_right; | ||
| 10 | bool enter_from_top; | ||
| 11 | bool enter_from_bottom; | ||
| 12 | int r; | ||
| 13 | int g; | ||
| 14 | int b; | ||
| 15 | int a; | ||
| 16 | }; | ||
| 17 | |||
| 18 | Map::Map() | ||
| 19 | { | 5 | { |
| 20 | add_collision(-6, 0, GAME_WIDTH, left, 1); | 6 | FILE* f = fopen(filename, "r"); |
| 21 | add_collision(GAME_WIDTH+6, 0, GAME_WIDTH, right, 1); | 7 | m_mapdata = (char*) malloc(MAP_WIDTH*(MAP_HEIGHT-1)*sizeof(char)); |
| 22 | |||
| 23 | FILE* f = fopen("../maps/bigmap.txt", "r"); | ||
| 24 | char* mapbuf = (char*) malloc(MAP_WIDTH*(MAP_HEIGHT-1)*sizeof(char)); | ||
| 25 | 8 | ||
| 26 | for (int i=0; i<MAP_HEIGHT-1; i++) | 9 | for (int i=0; i<MAP_HEIGHT-1; i++) |
| 27 | { | 10 | { |
| 28 | fread(mapbuf + i*MAP_WIDTH, sizeof(char), MAP_WIDTH, f); | 11 | fread(m_mapdata + i*MAP_WIDTH, sizeof(char), MAP_WIDTH, f); |
| 29 | fgetc(f); | 12 | fgetc(f); |
| 30 | } | 13 | } |
| 31 | 14 | ||
| 32 | fclose(f); | 15 | m_title = (char*) calloc(41, sizeof(char)); |
| 33 | 16 | fgets(m_title, 41, f); | |
| 34 | //Texture* tiles = loadTextureFromBMP("../res/tiles.bmp"); | ||
| 35 | bg = createTexture(GAME_WIDTH, GAME_HEIGHT); | ||
| 36 | fillTexture(bg, NULL, 0, 0, 0); | ||
| 37 | |||
| 38 | for (int i=0; i<MAP_WIDTH*(MAP_HEIGHT-1); i++) | ||
| 39 | { | ||
| 40 | int x = i % MAP_WIDTH; | ||
| 41 | int y = i / MAP_WIDTH; | ||
| 42 | Rectangle dst(x*TILE_WIDTH, y*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT); | ||
| 43 | //Rectangle src; | ||
| 44 | |||
| 45 | switch (mapbuf[i]) | ||
| 46 | { | ||
| 47 | case ' ': break; | ||
| 48 | case 'X': fillTexture(bg, &dst, 255, 85, 85); break; | ||
| 49 | case 'P': fillTexture(bg, &dst, 85, 255, 255); break; | ||
| 50 | } | ||
| 51 | |||
| 52 | //blitTexture(tiles, bg, &src, &dst); | ||
| 53 | |||
| 54 | if (mapbuf[i] == 'X') | ||
| 55 | { | ||
| 56 | if ((x != 0) && (mapbuf[i-1] != 'X')) | ||
| 57 | { | ||
| 58 | add_collision(x*TILE_WIDTH, y*TILE_HEIGHT, (y+1)*TILE_HEIGHT, right, 0); | ||
| 59 | } | ||
| 60 | |||
| 61 | if ((x != 15) && (mapbuf[i+1] != 'X')) | ||
| 62 | { | ||
| 63 | add_collision((x+1)*TILE_WIDTH, y*TILE_HEIGHT, (y+1)*TILE_HEIGHT, left, 0); | ||
| 64 | } | ||
| 65 | |||
| 66 | if ((y != 0) && (mapbuf[i-MAP_WIDTH] != 'X')) | ||
| 67 | { | ||
| 68 | add_collision(y*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, down, 0); | ||
| 69 | } | ||
| 70 | |||
| 71 | if ((y != 15) && (mapbuf[i+MAP_WIDTH] != 'X')) | ||
| 72 | { | ||
| 73 | add_collision((y+1)*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, up, 0); | ||
| 74 | } | ||
| 75 | } else if (mapbuf[i] == 'P') | ||
| 76 | { | ||
| 77 | add_collision(y*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, down, 0); | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | Texture* font = loadTextureFromBMP("../res/font.bmp"); | ||
| 82 | const char* map_name = "Everything Is Embarassing"; | ||
| 83 | int start_x = (40/2) - (strlen(map_name)/2); | ||
| 84 | for (int i=0; i<strlen(map_name); i++) | ||
| 85 | { | ||
| 86 | Rectangle srcRect(map_name[i] % 16 * 8, map_name[i] / 16 * 8, 8, 8); | ||
| 87 | Rectangle dstRect((start_x + i)*8, 24*8, 8, 8); | ||
| 88 | blitTexture(font, bg, &srcRect, &dstRect); | ||
| 89 | } | ||
| 90 | 17 | ||
| 91 | destroyTexture(font); | 18 | fclose(f); |
| 92 | //destroyTexture(tiles); | ||
| 93 | } | 19 | } |
| 94 | 20 | ||
| 95 | Map::~Map() | 21 | Map::~Map() |
| 96 | { | 22 | { |
| 97 | destroyTexture(bg); | 23 | free(m_mapdata); |
| 24 | free(m_title); | ||
| 98 | } | 25 | } |
| 99 | 26 | ||
| 100 | void Map::add_collision(int axis, int lower, int upper, direction_t dir, int type) | 27 | const char* Map::mapdata() |
| 101 | { | 28 | { |
| 102 | //printf("added collision\n"); | 29 | return m_mapdata; |
| 103 | list<collision_t>::iterator it; | ||
| 104 | |||
| 105 | switch (dir) | ||
| 106 | { | ||
| 107 | case up: | ||
| 108 | it = up_collisions.begin(); | ||
| 109 | for (; it!=up_collisions.end(); it++) | ||
| 110 | { | ||
| 111 | if (it->axis < axis) break; | ||
| 112 | } | ||
| 113 | |||
| 114 | up_collisions.insert(it, {axis, lower, upper, type}); | ||
| 115 | |||
| 116 | break; | ||
| 117 | case down: | ||
| 118 | it = down_collisions.begin(); | ||
| 119 | for (; it!=down_collisions.end(); it++) | ||
| 120 | { | ||
| 121 | if (it->axis > axis) break; | ||
| 122 | } | ||
| 123 | |||
| 124 | down_collisions.insert(it, {axis, lower, upper, type}); | ||
| 125 | |||
| 126 | break; | ||
| 127 | case left: | ||
| 128 | it = left_collisions.begin(); | ||
| 129 | for (; it!=left_collisions.end(); it++) | ||
| 130 | { | ||
| 131 | if (it->axis < axis) break; | ||
| 132 | } | ||
| 133 | |||
| 134 | left_collisions.insert(it, {axis, lower, upper, type}); | ||
| 135 | |||
| 136 | break; | ||
| 137 | case right: | ||
| 138 | it = right_collisions.begin(); | ||
| 139 | for (; it!=right_collisions.end(); it++) | ||
| 140 | { | ||
| 141 | if (it->axis > axis) break; | ||
| 142 | } | ||
| 143 | |||
| 144 | right_collisions.insert(it, {axis, lower, upper, type}); | ||
| 145 | |||
| 146 | break; | ||
| 147 | } | ||
| 148 | } | 30 | } |
| 149 | 31 | ||
| 150 | void Map::check_collisions(mob_t* mob, int x_next, int y_next) | 32 | const char* Map::title() |
| 151 | { | 33 | { |
| 152 | if (x_next < mob->x) | 34 | return m_title; |
| 153 | { | 35 | } |
| 154 | for (list<collision_t>::iterator it=left_collisions.begin(); it!=left_collisions.end(); it++) | ||
| 155 | { | ||
| 156 | if (it->axis > mob->x) continue; | ||
| 157 | if (it->axis < x_next) break; | ||
| 158 | |||
| 159 | if ((mob->y+mob->h > it->lower) && (mob->y < it->upper)) | ||
| 160 | { | ||
| 161 | // We have a collision! | ||
| 162 | if (it->type == 0) | ||
| 163 | { | ||
| 164 | x_next = it->axis; | ||
| 165 | mob->x_vel = 0; | ||
| 166 | } else if (it->type == 1) | ||
| 167 | { | ||
| 168 | x_next = GAME_WIDTH-mob->w/2; | ||
| 169 | } | ||
| 170 | |||
| 171 | break; | ||
| 172 | } | ||
| 173 | } | ||
| 174 | } else if (x_next > mob->x) | ||
| 175 | { | ||
| 176 | for (list<collision_t>::iterator it=right_collisions.begin(); it!=right_collisions.end(); it++) | ||
| 177 | { | ||
| 178 | if (it->axis < mob->x+mob->w) continue; | ||
| 179 | if (it->axis > x_next+mob->w) break; | ||
| 180 | |||
| 181 | if ((mob->y+mob->h > it->lower) && (mob->y < it->upper)) | ||
| 182 | { | ||
| 183 | // We have a collision! | ||
| 184 | if (it->type == 0) | ||
| 185 | { | ||
| 186 | x_next = it->axis - mob->w; | ||
| 187 | mob->x_vel = 0; | ||
| 188 | } else if (it->type == 1) | ||
| 189 | { | ||
| 190 | x_next = -mob->w/2; | ||
| 191 | } | ||
| 192 | |||
| 193 | break; | ||
| 194 | } | ||
| 195 | } | ||
| 196 | } | ||
| 197 | |||
| 198 | mob->x = x_next; | ||
| 199 | |||
| 200 | if (y_next < mob->y) | ||
| 201 | { | ||
| 202 | for (list<collision_t>::iterator it=up_collisions.begin(); it!=up_collisions.end(); it++) | ||
| 203 | { | ||
| 204 | if (it->axis > mob->y) continue; | ||
| 205 | if (it->axis < y_next) break; | ||
| 206 | |||
| 207 | if ((mob->x+mob->w > it->lower) && (mob->x < it->upper)) | ||
| 208 | { | ||
| 209 | // We have a collision! | ||
| 210 | if (it->type == 0) | ||
| 211 | { | ||
| 212 | y_next = it->axis; | ||
| 213 | mob->y_vel = 0; | ||
| 214 | } else if (it->type == 1) | ||
| 215 | { | ||
| 216 | y_next = GAME_HEIGHT-mob->h/2-1; | ||
| 217 | } | ||
| 218 | |||
| 219 | break; | ||
| 220 | } | ||
| 221 | } | ||
| 222 | } else if (y_next > mob->y) | ||
| 223 | { | ||
| 224 | for (list<collision_t>::iterator it=down_collisions.begin(); it!=down_collisions.end(); it++) | ||
| 225 | { | ||
| 226 | if (it->axis < mob->y+mob->h) continue; | ||
| 227 | if (it->axis > y_next+mob->h) break; | ||
| 228 | 36 | ||
| 229 | if ((mob->x+mob->w > it->lower) && (mob->x < it->upper)) | 37 | Map* Map::getLeftMap() |
| 230 | { | 38 | { |
| 231 | // We have a collision! | 39 | return m_leftMap; |
| 232 | if (it->type == 0) | 40 | } |
| 233 | { | 41 | |
| 234 | y_next = it->axis - mob->h; | 42 | Map* Map::getRightMap() |
| 235 | mob->y_vel = 0; | 43 | { |
| 236 | } else if (it->type == 1) | 44 | return m_rightMap; |
| 237 | { | 45 | } |
| 238 | y_next = 1 - mob->h/2; | 46 | |
| 239 | } | 47 | void Map::setLeftMap(Map* m) |
| 240 | 48 | { | |
| 241 | break; | 49 | m_leftMap = m; |
| 242 | } | ||
| 243 | } | ||
| 244 | } | ||
| 245 | |||
| 246 | mob->y = y_next; | ||
| 247 | } | 50 | } |
| 248 | 51 | ||
| 249 | void Map::render(Texture* buffer) | 52 | void Map::setRightMap(Map* m) |
| 250 | { | 53 | { |
| 251 | blitTexture(bg, buffer, NULL, NULL); | 54 | m_rightMap = m; |
| 252 | } | 55 | } |
