-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
347 lines (326 loc) · 14.2 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/* *************Sprite Sheet: Overview***************
* - Load sprite sheet png as SDL texture tex_PI
* (tex_PI has all frames)
* - Animate by moving the frame rectangle around the spritesheet texture
* - Copy rectangular section of texture to the renderer
* - Renderer rectangle sets the size and location on the screen
*
* Example:
* SDL_RenderCopy(ren, tex_PI, &sprite_PI->frame, &sprite_PI->render);
*
* Rects:
* sprite_PI->frame : SDL_Rect identify one frame on the sprite sheet
* sprite_PI->render : SDL_Rect defining size and location of rendered
* frame on screen
* *******************************/
/* *************Sprite Sheet: Select Frames***************
* - Every frame has size sprite_PI->size x sprite_PI->size
* - Example: rect selects the first frame (x=0, y=0)
*
* SDL_Rect sprite_PI->frame = {.x=0, .y=0, .w=sprite_PI->size, .h=sprite_PI->size};
*
* - Define the size and location of the sprite frame on the screen.
* - Example: rect centers the sprite on the screen and renders it to scale
*
* SDL_Rect sprite_PI->render = { .x=wI.w-sprite_PI->size}/2,
* .y=wI.h-sprite_PI->size}/2,
* .w=sprite_PI->size,
* .h=sprite_PI->size }
*
* *******************************/
/* *************TODO***************
* ~1. Figure out how to export from Pixaki with transparent background.~
* ~2. Load all sprite sheets.~
* ~3. Add keyboard control to move sprite.~
* 1. Put the animations together under one Character, then index array to select animation
* 2. Debug overlay take input, e.g., set sprite scale by typing in debug overlay
* *******************************/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <SDL.h>
#include <SDL_ttf.h>
#include <SDL_image.h>
#include "text.h"
#include "window_info.h"
#include "bgnd.h"
#include "print.h"
#include "anim.h"
#include "font.h"
#include "sprite.h"
void shutdown(TTF_Font *debug_font,
SDL_Renderer *ren,
SDL_Window *win,
SDL_Texture *bgnd_tex,
SDL_Texture *tex_PI,
SDL_Texture *tex_PW)
{
IMG_Quit();
SDL_DestroyTexture(bgnd_tex);
SDL_DestroyTexture(tex_PI);
SDL_DestroyTexture(tex_PW);
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
TTF_CloseFont(debug_font);
TTF_Quit();
SDL_Quit();
}
typedef struct
{
int x;
int y;
} Character;
void center_char_on_screen(Character *character, int size, WindowInfo wI)
{ // Center sprite on the screen
character->x=(wI.w-size)/2;
character->y=(wI.h-size)/2;
}
int main(int argc, char *argv[])
{
for(int i=0; i<argc; i++) puts(argv[i]);
// Setup
SDL_Init(SDL_INIT_VIDEO); // Init SDL
// Setup debug overlay font
TTF_Font *debug_font; // Debug overlay font
{ // Setup font
if( font_init() < 0 ) // Init SDL_ttf
{
shutdown(NULL, NULL, NULL, NULL, NULL, NULL);
return EXIT_FAILURE;
}
if( font_load(&debug_font, "fonts/ProggyClean.ttf", 16) < 0 ) // Load font
{
shutdown(NULL, NULL, NULL, NULL, NULL, NULL);
return EXIT_FAILURE;
}
}
// Create a Window and a Renderer
WindowInfo wI; WindowInfo_setup(&wI, argc, argv); // Size and locate window
SDL_Window *win = SDL_CreateWindow(argv[0], wI.x, wI.y, wI.w, wI.h, wI.flags);
SDL_Renderer *ren = SDL_CreateRenderer(win, -1, 0);
if( SDL_SetRenderDrawBlendMode(ren, SDL_BLENDMODE_BLEND) < 0 ) // Draw with alpha
{
puts("Cannot draw with alpha channel");
shutdown(debug_font, ren, win, NULL, NULL, NULL); return EXIT_FAILURE;
}
// Load the spritesheets
IMG_Init(IMG_INIT_PNG); // Spritesheet is a PNG
Sprite PenguinIdle = {.path = "art/penguin-huff.png"};
Sprite PenguinWalk = {.path = "art/penguin-waddle.png"};
Sprite *sprite_PI = &PenguinIdle; // _PI : Penguin Idle
if( sprite_load_info(sprite_PI) < 0 )
{
shutdown(debug_font, ren, win, NULL, NULL, NULL);
return EXIT_FAILURE;
}
Sprite *sprite_PW = &PenguinWalk; // _PW : Penguin Walk
if( sprite_load_info(sprite_PW) < 0 )
{
shutdown(debug_font, ren, win, NULL, NULL, NULL);
return EXIT_FAILURE;
}
sprite_PW->ticks_per_frame = 8;
Character char_penguin = {.x=0, .y=0};
center_char_on_screen(&char_penguin, sprite_PI->scale*sprite_PI->size, wI);
SDL_Texture *tex_PI; // One texture for entire sprite sheet
{ // Load Texture directly from spritesheet file
tex_PI = IMG_LoadTexture(ren, sprite_PI->path);
if( tex_PI == NULL )
{
printf("Failed to load \"%s\": %s", sprite_PI->path, IMG_GetError());
SDL_DestroyWindow(win); SDL_DestroyRenderer(ren);
TTF_CloseFont(debug_font); TTF_Quit(); SDL_Quit();
return EXIT_FAILURE;
}
}
SDL_Texture *tex_PW; // One texture for entire sprite sheet
{ // Load Texture directly from spritesheet file
tex_PW = IMG_LoadTexture(ren, sprite_PW->path);
if( tex_PW == NULL )
{
printf("Failed to load \"%s\": %s", sprite_PW->path, IMG_GetError());
SDL_DestroyWindow(win); SDL_DestroyRenderer(ren);
TTF_CloseFont(debug_font); TTF_Quit(); SDL_Quit();
return EXIT_FAILURE;
}
}
// Create a background texture with a sky-colored gradient
SDL_Texture *bgnd_tex;
bgnd_gradient(&bgnd_tex, ren, wI);
// Game state
bool quit = false;
bool show_debug = true;
bool walk_animation = false;
int walk_direction = 1;
int ticks = 0; // Count SDL ticks
// Debug input
#define DEBUG_INPUT_LEN 20
char debug_input_buffer[DEBUG_INPUT_LEN];
char *debug_input_buffer_end = debug_input_buffer + DEBUG_INPUT_LEN*sizeof(char);
char *debug_input = debug_input_buffer; *debug_input = '\0';
TextBox tb; // Debug overlay text box
char text_buffer[1024]; // Max 1024 characters
{ // Set up the text box
tb.margin = 5; // Margin relative to window
tb.fg = (SDL_Color){255,255,255,255}; // Text color: white
tb.fg_rect=(SDL_Rect){0}; // Init size to 0
tb.fg_rect.x = tb.margin; // Left edge of text
tb.fg_rect.y = tb.margin; // Top edge of text
tb.bg = (SDL_Color){0,0,0,127}; // Bgnd: Black 50% opacity
tb.bg_rect=(SDL_Rect){0}; // Init bgnd size
tb.bg_rect.w = wI.w; // Bgnd is full window width
tb.text = text_buffer; // Point at text buffer
}
while( quit == false )
{
// Penguin position
sprite_PW->render.x = char_penguin.x;
sprite_PI->render.x = char_penguin.x;
sprite_PW->render.y = char_penguin.y;
sprite_PI->render.y = char_penguin.y;
// UI
SDL_Keymod kmod = SDL_GetModState(); // kmod : OR'd modifiers
{ // Filtered
SDL_PumpEvents(); // Update event queue
const Uint8 *k = SDL_GetKeyboardState(NULL); // Get all keys
if( k[SDL_SCANCODE_ESCAPE] ) quit = true; // Esc to quit
if(0)
{ // Up/Down to zoom in/out
if( k[SDL_SCANCODE_UP] )
{
sprite_PI->scale++;
if( sprite_PI->scale>32 ) sprite_PI->scale=32;
sprite_PI->render.x=(wI.w-sprite_PI->scale*sprite_PI->size)/2;
sprite_PI->render.y=(wI.h-sprite_PI->scale*sprite_PI->size)/2;
sprite_PI->render.w=sprite_PI->scale*sprite_PI->size;
sprite_PI->render.h=sprite_PI->scale*sprite_PI->size;
}
if( k[SDL_SCANCODE_DOWN] )
{
sprite_PI->scale--;
if( sprite_PI->scale<1 ) sprite_PI->scale=1;
sprite_PI->render.x=(wI.w-sprite_PI->scale*sprite_PI->size)/2;
sprite_PI->render.y=(wI.h-sprite_PI->scale*sprite_PI->size)/2;
sprite_PI->render.w=sprite_PI->scale*sprite_PI->size;
sprite_PI->render.h=sprite_PI->scale*sprite_PI->size;
}
}
}
{ // Polled
SDL_Event e;
while( SDL_PollEvent(&e) )
{
if( e.type == SDL_KEYDOWN )
{
switch(e.key.keysym.sym)
{
case SDLK_TAB:
show_debug = show_debug ? false : true;
break;
case SDLK_RIGHT:
walk_animation = true;
walk_direction = 1;
if( kmod & (KMOD_LSHIFT|KMOD_RSHIFT) )
{ // DEBUG
anim_next_frame(&sprite_PI->framenum, sprite_PI->framecnt);
anim_load_frame(&sprite_PI->frame, sprite_PI->size, sprite_PI->framenum);
}
break;
case SDLK_LEFT:
walk_animation = true;
walk_direction = -1;
if( kmod & (KMOD_LSHIFT|KMOD_RSHIFT) )
{ // DEBUG
anim_prev_frame(&sprite_PI->framenum, sprite_PI->framecnt);
anim_load_frame(&sprite_PI->frame, sprite_PI->size, sprite_PI->framenum);
}
break;
default: break;
}
}
if( e.type == SDL_KEYUP )
{
switch(e.key.keysym.sym)
{
case SDLK_RIGHT:
case SDLK_LEFT:
if( (kmod & (KMOD_LSHIFT|KMOD_RSHIFT)) == 0 )
{
walk_animation = false;
}
break;
}
}
if( e.type == SDL_TEXTINPUT )
{
// Copy text
const char *c = e.text.text;
while( (*c!='\0') && (debug_input < debug_input_buffer_end ) )
{ *debug_input++ = *c++; } *debug_input = '\0';
}
}
}
{ // Animate
Sprite *sprite = walk_animation ? sprite_PW : sprite_PI;
if( ticks < sprite->ticks_per_frame ) ticks++;
else
{
anim_next_frame(&sprite->framenum, sprite->framecnt);
anim_load_frame(&sprite->frame, sprite->size, sprite->framenum);
ticks = 0;
}
if( walk_animation ) char_penguin.x += 1*sprite->scale*walk_direction;
}
// Render
{ // Paint over old video frame with a beautiful background gradient
SDL_RenderCopy(ren, bgnd_tex, NULL, NULL);
}
{ // Draw the sprite
/* SDL_RenderCopy(ren, tex_PI, NULL, NULL); // Draw entire spritesheet */
Sprite *sprite = (walk_animation) ? sprite_PW : sprite_PI;
SDL_Texture *tex = (walk_animation) ? tex_PW : tex_PI;
SDL_RendererFlip flip = (walk_direction==1) ? SDL_FLIP_NONE : SDL_FLIP_HORIZONTAL;
/* SDL_RenderCopy(ren, tex, &sprite->frame, &sprite->render); // Draw one frame */
SDL_RenderCopyEx(ren, tex, &sprite->frame, &sprite->render, 0, NULL, flip); // Draw one frame
}
if(show_debug)
{ // Debug overlay
Sprite *sprite = (walk_animation) ? sprite_PW : sprite_PI;
{ // Put text in the text box
char *d = tb.text; // d : see macro "print"
print("Spritesheet: "); print(sprite->path);
print(" | ");
print("Sprite size: "); printint(4, sprite->size); print("x"); printint(4, sprite->size);
print(" | ");
print("Animation frame: "); printint(3, sprite->framenum); print(" / "); printint(3, sprite->framecnt);
print(" | ");
print("Ticks per frame: "); printint(3, sprite->ticks_per_frame);
print(" | ");
print("Animation: "); if(walk_animation){ print("waddle");} else print("huff");
print(" | ");
print("Window size: "); printint(5, wI.w); print("x"); printint(5, wI.h); print(" (wxh)");
print("\nInput: "); print(debug_input_buffer);
SDL_Surface *surf = TTF_RenderText_Blended_Wrapped(debug_font, tb.text, tb.fg,
wI.w-tb.margin); // Wrap text here
tb.tex = SDL_CreateTextureFromSurface(ren, surf);
SDL_FreeSurface(surf);
SDL_QueryTexture(tb.tex, NULL, NULL, &tb.fg_rect.w, &tb.fg_rect.h);
}
{ // Draw text
tb.bg_rect.h = tb.fg_rect.h + 2*tb.margin;
SDL_SetRenderDrawColor(ren, tb.bg.r, tb.bg.g, tb.bg.b, tb.bg.a);
// Render bgnd
SDL_RenderFillRect(ren, &tb.bg_rect);
// Render text
SDL_RenderCopy(ren, tb.tex, NULL, &tb.fg_rect);
SDL_DestroyTexture(tb.tex);
}
}
{ // Present to screen
SDL_RenderPresent(ren);
SDL_Delay(10);
}
}
shutdown(debug_font, ren, win, bgnd_tex, tex_PI, tex_PW);
return EXIT_SUCCESS;
}