-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
164 lines (131 loc) · 3.39 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
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <time.h>
#include "bird.h"
#include "flappy.h"
#include "pipe.h"
#include "ui.h"
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
uint64_t lose_time = 0;
int score = 0;
static void game_init(void);
static void tick(double delta);
static void draw(void);
int main()
{
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "SDL_Init SDL_INIT_VIDEO failed! SDL error: %s\n",
SDL_GetError());
die(1);
}
window = SDL_CreateWindow("Flappy SDL", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH,
SCREEN_HEIGHT, 0);
if (window == NULL) {
fprintf(stderr, "Creating window failed! SDL error: %s\n",
SDL_GetError());
die(1);
}
int img_flags = IMG_INIT_PNG;
if (!(IMG_Init(img_flags) & img_flags)) {
fprintf(stderr, "IMG_Init failed with flags %08x! SDL error: %s\n",
img_flags, SDL_GetError());
die(1);
}
if (TTF_Init() == -1) {
fprintf(stderr, "TTF_Init failed! SDL error: %s\n", TTF_GetError());
die(1);
}
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == NULL) {
fprintf(stderr, "Could not create renderer! SDL error: %s\n",
SDL_GetError());
die(1);
}
SDL_SetRenderDrawColor(renderer, 0xfb, 0x71, 0xff, 0xff);
game_init();
uint64_t now = SDL_GetPerformanceCounter();
uint64_t last;
SDL_Event e;
while (1) {
while (SDL_PollEvent(&e) != 0) {
if (e.type == SDL_QUIT) {
die(0);
} else if (e.type == SDL_KEYDOWN) {
if (e.key.keysym.sym == SDLK_q) {
die(0);
}
if (e.key.repeat == 0) {
ui_keydown(e.key.keysym.sym);
}
}
}
last = now;
now = SDL_GetPerformanceCounter();
double delta = (double) ((now - last)
/ (double) SDL_GetPerformanceFrequency());
tick(delta);
SDL_RenderClear(renderer);
draw();
SDL_RenderPresent(renderer);
}
die(0);
}
SDL_Texture *load_texture(const char *path)
{
SDL_Surface *tmp = IMG_Load(path);
if (tmp == NULL) {
fprintf(stderr, "Failed to load image %s! SDL error: %s\n", path,
IMG_GetError());
die(1);
}
SDL_Texture *tex = SDL_CreateTextureFromSurface(renderer, tmp);
SDL_FreeSurface(tmp);
if (tex == NULL) {
fprintf(stderr, "Failed to create texture %s! SDL error: %s\n", path,
SDL_GetError());
die(1);
}
return tex;
}
static void game_init(void)
{
srand(time(NULL));
bird_init();
pipe_init();
ui_init();
}
static void tick(double delta)
{
ui_tick(delta);
}
static void draw(void)
{
ui_draw();
}
void game_lose(void)
{
bird_kill();
ui_state = ui_gameover;
lose_time = SDL_GetTicks64();
}
void die(int status)
{
bird_destroy();
pipe_destroy();
ui_destroy();
if (renderer != NULL) {
SDL_DestroyRenderer(renderer);
}
if (window != NULL) {
SDL_DestroyWindow(window);
}
IMG_Quit();
SDL_Quit();
exit(status);
}