| line# | code(run#) |
|---|---|
| 1 | #include "tiles.h" |
| 2 | #include "coordinates.h" |
| 3 | |
| 4 | #include <SDL.h> |
| 5 | #include <SDL_image.h> |
| 6 | #include <SDL_ttf.h> |
| 7 | #include <stdbool.h> |
| 8 | |
| 9 | #include "debugmalloc.h" |
| 10 | |
| 11 | // Translate tile type to coordinates of the tile |
| 12 | // Returns SDL_rect |
| 13 | static SDL_Rect toSourceRect(Tile tile)(14566) |
| 14 | { |
| 15 | SDL_Rect src; |
| 16 | src.w = 64;(14566) |
| 17 | src.h = 64;(14566) |
| 18 | |
| 19 | switch (tile)(14566) |
| 20 | { |
| 21 | case greenFloor:(6110) |
| 22 | src.x = 10;(6110) |
| 23 | src.y = 6;(6110) |
| 24 | break;(6110) |
| 25 | case greyFloor:(4653) |
| 26 | src.x = 11;(4653) |
| 27 | src.y = 6;(4653) |
| 28 | break;(4653) |
| 29 | case brownFloor:(870) |
| 30 | src.x = 12;(870) |
| 31 | src.y = 6;(870) |
| 32 | break;(870) |
| 33 | case wall:(1388) |
| 34 | src.x = 7;(1388) |
| 35 | src.y = 6;(1388) |
| 36 | break;(1388) |
| 37 | case player:(16) |
| 38 | src.x = 0;(16) |
| 39 | src.y = 4;(16) |
| 40 | break;(16) |
| 41 | case crate:(15) |
| 42 | src.x = 3;(15) |
| 43 | src.y = 0;(15) |
| 44 | break;(15) |
| 45 | case crateOnTarget:(1) |
| 46 | src.x = 3;(1) |
| 47 | src.y = 1;(1) |
| 48 | break;(1) |
| 49 | case target:(85) |
| 50 | src.x = 12;(85) |
| 51 | src.y = 3;(85) |
| 52 | break;(85) |
| 53 | case up:(16) |
| 54 | src.x = 2;(16) |
| 55 | src.y = 0;(16) |
| 56 | break;(16) |
| 57 | case down:(16) |
| 58 | src.x = 2;(16) |
| 59 | src.y = 1;(16) |
| 60 | break;(16) |
| 61 | case left:(16) |
| 62 | src.x = 1;(16) |
| 63 | src.y = 0;(16) |
| 64 | break;(16) |
| 65 | case right:(18) |
| 66 | src.x = 1;(18) |
| 67 | src.y = 1;(18) |
| 68 | break;(18) |
| 69 | case home:(2) |
| 70 | src.x = 2;(2) |
| 71 | src.y = 2;(2) |
| 72 | break;(2) |
| 73 | case save:(2) |
| 74 | src.x = 4;(2) |
| 75 | src.y = 0;(2) |
| 76 | break;(2) |
| 77 | case add:(0) |
| 78 | src.x = 1;(0) |
| 79 | src.y = 2;(0) |
| 80 | break;(0) |
| 81 | case blank:(1050) |
| 82 | src.x = 5;(1050) |
| 83 | src.y = 2;(1050) |
| 84 | break;(1050) |
| 85 | case blankR:(87) |
| 86 | src.x = 4;(87) |
| 87 | src.y = 1;(87) |
| 88 | break;(87) |
| 89 | case blankL:(87) |
| 90 | src.x = 5;(87) |
| 91 | src.y = 1;(87) |
| 92 | break;(87) |
| 93 | case blankLR:(132) |
| 94 | src.x = 4;(132) |
| 95 | src.y = 2;(132) |
| 96 | break;(132) |
| 97 | case retry:(2) |
| 98 | src.x = 5;(2) |
| 99 | src.y = 0;(2) |
| 100 | break;(2) |
| 101 | case selection:(0) |
| 102 | src.x = 0;(0) |
| 103 | src.y = 3;(0) |
| 104 | break;(0) |
| 105 | case delete:(0) |
| 106 | src.x = 6;(0) |
| 107 | src.y = 2;(0) |
| 108 | break;(0) |
| 109 | default:(0) |
| 110 | src.x = 0;(0) |
| 111 | src.y = 0;(0) |
| 112 | break;(0) |
| 113 | } |
| 114 | |
| 115 | src.x *= 64;(14566) |
| 116 | src.y *= 64;(14566) |
| 117 | |
| 118 | return src;(14566) |
| 119 | } |
| 120 | |
| 121 | // Render tile to renderer. Coordinates map to whole blocks |
| 122 | // Texture must include proper tiles file |
| 123 | void renderTile(SDL_Renderer *renderer, SDL_Texture *tiles, Tile tile, int x, int y)(14566) |
| 124 | { |
| 125 | SDL_Rect src = toSourceRect(tile);(14566) |
| 126 | SDL_Rect dst = {64 * x, 64 * y, 64, 64};(14566) |
| 127 | |
| 128 | SDL_RenderCopy(renderer, tiles, &src, &dst);(14566) |
| 129 | }(14566) |
| 130 | |
| 131 | // Render tile to renderer. Coordinates map to whole blocks |
| 132 | // Texture must include proper tiles file |
| 133 | void renderTileC(SDL_Renderer *renderer, SDL_Texture *tiles, Tile tile, Coordinates pos)(1036) |
| 134 | { |
| 135 | renderTile(renderer, tiles, tile, pos.x, pos.y);(1036) |
| 136 | }(1036) |
| 137 | |
| 138 | // Render tiles to renderer. Both coordinates are inclusive |
| 139 | // Works same as renderTile |
| 140 | void renderTiles(SDL_Renderer *renderer, SDL_Texture *tiles, Tile tile, int x1, int y1, int x2, int y2)(274) |
| 141 | { |
| 142 | for (int x = x1; x <= x2; x++)(2711) |
| 143 | { |
| 144 | for (int y = y1; y <= y2; y++)(15520) |
| 145 | { |
| 146 | renderTile(renderer, tiles, tile, x, y);(13083) |
| 147 | } |
| 148 | } |
| 149 | }(274) |
| 150 | |
| 151 | // Render tiles to renderer. Both coordinates are inclusive |
| 152 | // Works same as renderTile |
| 153 | void renderTilesC(SDL_Renderer *renderer, SDL_Texture *tiles, Tile tile, Coordinates pos1, Coordinates pos2)(0) |
| 154 | { |
| 155 | renderTiles(renderer, tiles, tile, pos1.x, pos1.y, pos2.x, pos2.y);(0) |
| 156 | }(0) |
| 157 | |
| 158 | // Render text to renderer. Coordinates map to whole blocks |
| 159 | // CenteredX: center font in X direction (false: left) |
| 160 | // CenteredY: center font in Y direction (false: top) |
| 161 | void renderFont(SDL_Renderer *renderer, TTF_Font *font, SDL_Color color, char *text, int x, int y, bool centeredX, bool centeredY)(1246) |
| 162 | { |
| 163 | if (text == NULL)(1246) |
| 164 | return;(1) |
| 165 | if (*text == '\0')(1246) |
| 166 | return;(1) |
| 167 | x *= 64;(1245) |
| 168 | y *= 64;(1245) |
| 169 | x += 32;(1245) |
| 170 | y += 32;(1245) |
| 171 | |
| 172 | SDL_Surface *textSurface; |
| 173 | SDL_Texture *textTexture; |
| 174 | SDL_Rect destination; |
| 175 | |
| 176 | textSurface = TTF_RenderUTF8_Solid(font, text, color);(1245) |
| 177 | textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);(1245) |
| 178 | destination.x = centeredX ? x - textSurface->w / 2 : x;(1245) |
| 179 | destination.y = centeredY ? y - textSurface->h / 2 : y;(1245) |
| 180 | destination.w = textSurface->w;(1245) |
| 181 | destination.h = textSurface->h;(1245) |
| 182 | SDL_RenderCopy(renderer, textTexture, NULL, &destination);(1245) |
| 183 | SDL_FreeSurface(textSurface);(1245) |
| 184 | SDL_DestroyTexture(textTexture);(1245) |
| 185 | } |
| 186 | |
| 187 | // Render text to renderer. Coordinates map to whole blocks |
| 188 | // CenteredX: center font in X direction (false: left) |
| 189 | // CenteredY: center font in Y direction (false: top) |
| 190 | void renderFontC(SDL_Renderer *renderer, TTF_Font *font, SDL_Color color, char *text, Coordinates pos, bool centeredX, bool centeredY)(1008) |
| 191 | { |
| 192 | renderFont(renderer, font, color, text, pos.x, pos.y, centeredX, centeredY);(1008) |
| 193 | }(1008) |
| 194 | |
| 195 | // Checks if click coordinates match tile coordinates |
| 196 | // Tile coordinates map to whole blocks, click coordinates are in pixels |
| 197 | bool clickTile(int tileX, int tileY, int clickX, int clickY)(0) |
| 198 | { |
| 199 | tileX *= 64;(0) |
| 200 | tileY *= 64;(0) |
| 201 | if ((0) |
| 202 | tileX < clickX && tileX + 64 > clickX && // x direction(0) |
| 203 | tileY < clickY && tileY + 64 > clickY) // y direction(0) |
| 204 | { |
| 205 | return true;(0) |
| 206 | } |
| 207 | return false;(0) |
| 208 | } |
| 209 | |
| 210 | // Check if tiles in an area were clicked |
| 211 | // tileX1 <= tileX2, tileY1 <= tileY2 |
| 212 | bool clickTiles(int tileX1, int tileY1, int tileX2, int tileY2, int clickX, int clickY)(0) |
| 213 | { |
| 214 | tileX1 *= 64;(0) |
| 215 | tileY1 *= 64;(0) |
| 216 | tileX2 *= 64;(0) |
| 217 | tileY2 *= 64;(0) |
| 218 | if ((0) |
| 219 | tileX1 < clickX && tileX2 + 64 > clickX && // x direction(0) |
| 220 | tileY1 < clickY && tileY2 + 64 > clickY) // y direction(0) |
| 221 | { |
| 222 | return true;(0) |
| 223 | } |
| 224 | return false;(0) |
| 225 | } |