5 // (C) 2014 Underground Software
7 // JLH = James Hammons <jlhamm@acm.org>
10 // --- ---------- ------------------------------------------------------------
11 // JLH 08/19/2014 Created this file
15 #include <SDL2/SDL_image.h>
22 SDL_Texture * tile1 = 0;
27 bool InitTileHandler(void)
29 SDL_Surface * surface = IMG_Load("res/tilemap1.png");
33 printf("TILE: IMG_Load: %s\n", IMG_GetError());
37 tile1 = SDL_CreateTextureFromSurface(sdlRenderer, surface);
38 SDL_FreeSurface(surface);
44 SDL_Texture * CreateMapTexture(void)
46 Map * map = (Map *)maps[0];
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);
52 RenderMap(map, texture);
58 void RenderMap(Map * map, SDL_Texture * texture)
61 if ((map == 0) || (texture == 0))
64 if (SDL_SetRenderTarget(sdlRenderer, texture) < 0)
66 WriteLog("TILE: Could not set Render Target to map texture... (%s)\n", SDL_GetError());
70 for(int y=0; y<map->height; y++)
72 for(int x=0; x<map->width; x++)
74 unsigned char tile = map->tiles[(y * map->width) + x];
76 dst.x = x * 16, dst.y = y * 16, dst.w = dst.h = 16;
82 src.x = 16, src.y = 0;
84 src.x = 32, src.y = 0;
86 src.x = 48, src.y = 0;
88 src.x = 64, src.y = 0;
90 src.x = 80, src.y = 0;
92 src.x = 96, src.y = 0;
94 src.x = 112, src.y = 0;
96 SDL_RenderCopy(sdlRenderer, tile1, &src, &dst);
100 SDL_SetRenderTarget(sdlRenderer, 0);
102 // Set up global room state from tile map
103 memcpy(roomState, map->tiles, 16 * 11);
104 roomWidth = map->width;
105 roomHeight = map->height;
111 // Get tiles the player is standing on (1x1 to 2x2)
113 void GetTiles(int xpos, int ypos, uint8_t * tiles)
117 int rx = xpos % 16, ry = ypos % 16;
118 int gx = xpos / 16, gy = ypos / 16;
120 tiles[0] = roomState[gx + 0 + ((gy + 0) * roomWidth)];
123 tiles[1] = roomState[gx + 1 + ((gy + 0) * roomWidth)];
126 tiles[2] = roomState[gx + 0 + ((gy + 1) * roomWidth)];
128 if ((rx != 0) && (ry != 0))
129 tiles[3] = roomState[gx + 1 + ((gy + 1) * roomWidth)];
134 // Get tiles next to the direction the player is facing (1 or 2)
136 void GetTiles2(int xpos, int ypos, Direction facing, uint8_t * tiles)
138 tiles[0] = tiles[1] = 0;
140 int rx = xpos % 16, ry = ypos % 16;
141 int gx = xpos / 16, gy = ypos / 16;
143 int dx = 0, dy = 0, extraX = (rx != 0 ? 1 : 0), extraY = (ry != 0 ? 1 : 0);
147 else if (facing == Right)
149 else if (facing == Up)
151 else if (facing == Down)
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))))
161 tiles[0] = roomState[(gx + dx) + ((gy + dy) * roomWidth)];
164 tiles[1] = roomState[(gx + 1) + ((gy + dy) * roomWidth)];
167 tiles[1] = roomState[(gx + dx) + ((gy + 1) * roomWidth)];