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>
20 SDL_Texture * sprite1 = 0;
21 SDL_Texture * wizard1 = 0;
22 SDL_Texture * underlay = 0;
23 SDL_Texture * underlay2 = 0;
26 bool InitSpriteHandler(void)
28 SDL_Surface * surface = IMG_Load("res/spritemap1.png");
32 printf("SPRITE: IMG_Load: %s\n", IMG_GetError());
36 sprite1 = SDL_CreateTextureFromSurface(sdlRenderer, surface);
37 SDL_FreeSurface(surface);
39 surface = IMG_Load("res/g2-wizard.png");
43 printf("SPRITE: IMG_Load: %s\n", IMG_GetError());
47 wizard1 = SDL_CreateTextureFromSurface(sdlRenderer, surface);
48 SDL_FreeSurface(surface);
50 uint32_t ulPixels[32 * 32];
52 for(uint32_t i=0; i<32*32; i++)
53 ulPixels[i] = 0xFF00FF7F;
55 surface = SDL_CreateRGBSurfaceFrom(ulPixels, 32, 32, 32, 32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
59 printf("SPRITE: CreateRGBSurfaceFrom: %s\n", IMG_GetError());
63 underlay = SDL_CreateTextureFromSurface(sdlRenderer, surface);
64 SDL_FreeSurface(surface);
66 for(uint32_t i=0; i<32*32; i++)
67 ulPixels[i] = 0xFF7F007F;
69 surface = SDL_CreateRGBSurfaceFrom(ulPixels, 32, 32, 32, 32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
73 printf("SPRITE: CreateRGBSurfaceFrom: %s\n", IMG_GetError());
77 underlay2 = SDL_CreateTextureFromSurface(sdlRenderer, surface);
78 SDL_FreeSurface(surface);
84 void DrawPlayerSprite(Direction facing, int xpos, int ypos)
89 dst.x = xpos, dst.y = ypos, dst.w = dst.h = 16;
95 else if (facing == Down)
97 src.x = 16, src.y = 0;
99 else if (facing == Right)
101 src.x = 32, src.y = 0;
103 else if (facing == Left)
105 src.x = 48, src.y = 0;
108 SDL_RenderCopy(sdlRenderer, sprite1, &src, &dst);
117 dst.x = 100, dst.y = 100, dst.w = dst.h = 24;
119 SDL_RenderCopy(sdlRenderer, wizard1, &src, &dst);
124 void DrawPlayerSprite2(int xpos, int ypos, Direction facing, int frame)
126 // Adjust x/y coords for oversize sprite
127 xpos -= 4, ypos -= 8;
129 int frameX[3] = { 48, 0, 24 };
130 int frameY[3] = { 0, 24, 24 };
133 dst.x = xpos, dst.y = ypos, dst.w = dst.h = 24;
137 src.x = frameX[frame], src.y = (6 * 24) + frameY[frame];
139 else if (facing == Down)
141 src.x = frameX[frame], src.y = (2 * 24) + frameY[frame];
143 else if (facing == Right)
145 src.x = frameX[frame], src.y = (8 * 24) + frameY[frame];
147 else if (facing == Left)
149 src.x = frameX[frame], src.y = (4 * 24) + frameY[frame];
152 SDL_RenderCopy(sdlRenderer, wizard1, &src, &dst);
156 void DrawUnderlay(int xpos, int ypos, Direction facing)
159 int rx = xpos % 16, ry = ypos % 16;
160 int gx = xpos / 16, gy = ypos / 16;
162 src.x = 0, src.y = 0;
163 dest.x = gx * 16, dest.y = gy * 16;
165 src.w = (rx == 0 ? 16 : 32);
166 src.h = (ry == 0 ? 16 : 32);
167 dest.w = src.w, dest.h = src.h;
169 SDL_RenderCopy(sdlRenderer, underlay, &src, &dest);
176 else if (facing == Down)
181 else if (facing == Left)
186 else if (facing == Right)
192 SDL_RenderCopy(sdlRenderer, underlay2, &src, &dest);