]> Shamusworld >> Repos - legend/blob - src/tile.cpp
Initial commit for the Legend of A... project!
[legend] / src / tile.cpp
1 //
2 // Tile handler
3 //
4 // by James Hammons
5 // (C) 2014 Underground Software
6 //
7 // JLH = James Hammons <jlhamm@acm.org>
8 //
9 // WHO  WHEN        WHAT
10 // ---  ----------  ------------------------------------------------------------
11 // JLH  08/19/2014  Created this file
12 //
13
14 #include "tile.h"
15 #include <SDL2/SDL_image.h>
16 #include "data.h"
17 #include "log.h"
18 #include "video.h"
19
20
21 // Global vars
22 SDL_Texture * tile1 = 0;
23 int mapCornerX = 0;
24 int mapCornerY = 0;
25
26
27 bool InitTileHandler(void)
28 {
29         SDL_Surface * surface = IMG_Load("res/tilemap1.png");
30
31         if (!surface)
32         {
33                 printf("TILE: IMG_Load: %s\n", IMG_GetError());
34                 return false;
35         }
36
37         tile1 = SDL_CreateTextureFromSurface(sdlRenderer, surface);
38         SDL_FreeSurface(surface);
39
40         return true;
41 }
42
43
44 SDL_Texture * CreateMapTexture(void)
45 {
46         Map * map = (Map *)maps[0];
47
48         SDL_Texture * texture = SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_ARGB8888,
49 //              SDL_TEXTUREACCESS_STATIC, map->width * 16, map->height * 16);
50                 SDL_TEXTUREACCESS_TARGET, map->width * 16, map->height * 16);
51
52         RenderMap(map, texture);
53
54         return texture;
55 }
56
57
58 void RenderMap(Map * map, SDL_Texture * texture)
59 {
60         // Sanity check...
61         if ((map == 0) || (texture == 0))
62                 return;
63
64         if (SDL_SetRenderTarget(sdlRenderer, texture) < 0)
65         {
66                 WriteLog("TILE: Could not set Render Target to map texture... (%s)\n", SDL_GetError());
67                 return;
68         }
69
70         for(int y=0; y<map->height; y++)
71         {
72                 for(int x=0; x<map->width; x++)
73                 {
74                         unsigned char tile = map->tiles[(y * map->width) + x];
75                         SDL_Rect src, dst;
76                         dst.x = x * 16, dst.y = y * 16, dst.w = dst.h = 16;
77                         src.w = src.h = 16;
78
79                         if (tile == '0')
80                                 src.x = 0, src.y = 0;
81                         else if (tile == '1')
82                                 src.x = 16, src.y = 0;
83                         else if (tile == '2')
84                                 src.x = 32, src.y = 0;
85                         else if (tile == '3')
86                                 src.x = 48, src.y = 0;
87                         else if (tile == '4')
88                                 src.x = 64, src.y = 0;
89                         else if (tile == '5')
90                                 src.x = 80, src.y = 0;
91                         else if (tile == '6')
92                                 src.x = 96, src.y = 0;
93                         else if (tile == '7')
94                                 src.x = 112, src.y = 0;
95
96                         SDL_RenderCopy(sdlRenderer, tile1, &src, &dst);
97                 }
98         }
99
100         SDL_SetRenderTarget(sdlRenderer, 0);
101
102         // Set up global room state from tile map
103         memcpy(roomState, map->tiles, 16 * 11);
104         roomWidth = map->width;
105         roomHeight = map->height;
106         currentMap = map;
107 }
108
109
110 //
111 // Get tiles the player is standing on (1x1 to 2x2)
112 //
113 void GetTiles(int xpos, int ypos, uint8_t * tiles)
114 {
115         memset(tiles, 0, 4);
116
117         int rx = xpos % 16, ry = ypos % 16;
118         int gx = xpos / 16, gy = ypos / 16;
119
120         tiles[0] = roomState[gx + 0 + ((gy + 0) * roomWidth)];
121
122         if (rx != 0)
123                 tiles[1] = roomState[gx + 1 + ((gy + 0) * roomWidth)];
124
125         if (ry != 0)
126                 tiles[2] = roomState[gx + 0 + ((gy + 1) * roomWidth)];
127
128         if ((rx != 0) && (ry != 0))
129                 tiles[3] = roomState[gx + 1 + ((gy + 1) * roomWidth)];
130 }
131
132
133 //
134 // Get tiles next to the direction the player is facing (1 or 2)
135 //
136 void GetTiles2(int xpos, int ypos, Direction facing, uint8_t * tiles)
137 {
138         tiles[0] = tiles[1] = 0;
139
140         int rx = xpos % 16, ry = ypos % 16;
141         int gx = xpos / 16, gy = ypos / 16;
142
143         int dx = 0, dy = 0, extraX = (rx != 0 ? 1 : 0), extraY = (ry != 0 ? 1 : 0);
144
145         if (facing == Left)
146                 dx = -1;
147         else if (facing == Right)
148                 dx = +1 + extraX;
149         else if (facing == Up)
150                 dy = -1;
151         else if (facing == Down)
152                 dy = +1 + extraY;
153
154         // Edge conditions...
155         if (((facing == Left) && (gx == 0))
156                 || ((facing == Right) && (gx == (roomWidth - 1)))
157                 || ((facing == Up) && (gy == 0))
158                 || ((facing == Down) && (gy == (roomHeight - 1))))
159                 return;
160
161         tiles[0] = roomState[(gx + dx) + ((gy + dy) * roomWidth)];
162
163         if (extraX && dy)
164                 tiles[1] = roomState[(gx + 1) + ((gy + dy) * roomWidth)];
165
166         if (extraY && dx)
167                 tiles[1] = roomState[(gx + dx) + ((gy + 1) * roomWidth)];
168 }
169
170