]> Shamusworld >> Repos - legend/blob - src/sprite.cpp
Initial commit for the Legend of A... project!
[legend] / src / sprite.cpp
1 //
2 // Sprite 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 "sprite.h"
15 #include <SDL2/SDL_image.h>
16 #include "video.h"
17
18
19 // Global vars
20 SDL_Texture * sprite1 = 0;
21 SDL_Texture * wizard1 = 0;
22 SDL_Texture * underlay = 0;
23 SDL_Texture * underlay2 = 0;
24
25
26 bool InitSpriteHandler(void)
27 {
28         SDL_Surface * surface = IMG_Load("res/spritemap1.png");
29
30         if (!surface)
31         {
32                 printf("SPRITE: IMG_Load: %s\n", IMG_GetError());
33                 return false;
34         }
35
36         sprite1 = SDL_CreateTextureFromSurface(sdlRenderer, surface);
37         SDL_FreeSurface(surface);
38
39         surface = IMG_Load("res/g2-wizard.png");
40
41         if (!surface)
42         {
43                 printf("SPRITE: IMG_Load: %s\n", IMG_GetError());
44                 return false;
45         }
46
47         wizard1 = SDL_CreateTextureFromSurface(sdlRenderer, surface);
48         SDL_FreeSurface(surface);
49
50         uint32_t ulPixels[32 * 32];
51
52         for(uint32_t i=0; i<32*32; i++)
53                 ulPixels[i] = 0xFF00FF7F;
54
55         surface = SDL_CreateRGBSurfaceFrom(ulPixels, 32, 32, 32, 32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
56
57         if (!surface)
58         {
59                 printf("SPRITE: CreateRGBSurfaceFrom: %s\n", IMG_GetError());
60                 return false;
61         }
62
63         underlay = SDL_CreateTextureFromSurface(sdlRenderer, surface);
64         SDL_FreeSurface(surface);
65
66         for(uint32_t i=0; i<32*32; i++)
67                 ulPixels[i] = 0xFF7F007F;
68
69         surface = SDL_CreateRGBSurfaceFrom(ulPixels, 32, 32, 32, 32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
70
71         if (!surface)
72         {
73                 printf("SPRITE: CreateRGBSurfaceFrom: %s\n", IMG_GetError());
74                 return false;
75         }
76
77         underlay2 = SDL_CreateTextureFromSurface(sdlRenderer, surface);
78         SDL_FreeSurface(surface);
79
80         return true;
81 }
82
83
84 void DrawPlayerSprite(Direction facing, int xpos, int ypos)
85 {
86         SDL_Rect src, dst;
87 #if 1
88         src.w = src.h = 16;
89         dst.x = xpos, dst.y = ypos, dst.w = dst.h = 16;
90
91         if (facing == Up)
92         {
93                 src.x = 0, src.y = 0;
94         }
95         else if (facing == Down)
96         {
97                 src.x = 16, src.y = 0;
98         }
99         else if (facing == Right)
100         {
101                 src.x = 32, src.y = 0;
102         }
103         else if (facing == Left)
104         {
105                 src.x = 48, src.y = 0;
106         }
107
108         SDL_RenderCopy(sdlRenderer, sprite1, &src, &dst);
109 #else
110
111
112 #endif
113
114 #if 0
115         src.w = src.h = 24;
116         src.x = src.y = 0;
117         dst.x = 100, dst.y = 100, dst.w = dst.h = 24;
118
119         SDL_RenderCopy(sdlRenderer, wizard1, &src, &dst);
120 #endif
121 }
122
123
124 void DrawPlayerSprite2(int xpos, int ypos, Direction facing, int frame)
125 {
126         // Adjust x/y coords for oversize sprite
127         xpos -= 4, ypos -= 8;
128
129         int frameX[3] = { 48, 0, 24 };
130         int frameY[3] = { 0, 24, 24 };
131         SDL_Rect src, dst;
132         src.w = src.h = 24;
133         dst.x = xpos, dst.y = ypos, dst.w = dst.h = 24;
134
135         if (facing == Up)
136         {
137                 src.x = frameX[frame], src.y = (6 * 24) + frameY[frame];
138         }
139         else if (facing == Down)
140         {
141                 src.x = frameX[frame], src.y = (2 * 24) + frameY[frame];
142         }
143         else if (facing == Right)
144         {
145                 src.x = frameX[frame], src.y = (8 * 24) + frameY[frame];
146         }
147         else if (facing == Left)
148         {
149                 src.x = frameX[frame], src.y = (4 * 24) + frameY[frame];
150         }
151
152         SDL_RenderCopy(sdlRenderer, wizard1, &src, &dst);
153 }
154
155
156 void DrawUnderlay(int xpos, int ypos, Direction facing)
157 {
158         SDL_Rect src, dest;
159         int rx = xpos % 16, ry = ypos % 16;
160         int gx = xpos / 16, gy = ypos / 16;
161
162         src.x = 0, src.y = 0;
163         dest.x = gx * 16, dest.y = gy * 16;
164
165         src.w = (rx == 0 ? 16 : 32);
166         src.h = (ry == 0 ? 16 : 32);
167         dest.w = src.w, dest.h = src.h;
168
169         SDL_RenderCopy(sdlRenderer, underlay, &src, &dest);
170
171         if (facing == Up)
172         {
173                 src.h = dest.h = 16;
174                 dest.y -= 16;
175         }
176         else if (facing == Down)
177         {
178                 dest.y += src.h;
179                 src.h = dest.h = 16;
180         }
181         else if (facing == Left)
182         {
183                 src.w = dest.w = 16;
184                 dest.x -= 16;
185         }
186         else if (facing == Right)
187         {
188                 dest.x += src.w;
189                 src.w = dest.w = 16;
190         }
191
192         SDL_RenderCopy(sdlRenderer, underlay2, &src, &dest);
193 }
194