-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06-Moving-Text.c
190 lines (162 loc) · 5.09 KB
/
06-Moving-Text.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
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_ttf.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define WINDOW_TITLE "06 Moving Text"
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
#define IMAGE_FLAGS IMG_INIT_PNG
#define TEXT_SIZE 80
struct Game {
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Texture *background;
TTF_Font *text_font;
SDL_Color text_color;
SDL_Rect text_rect;
SDL_Texture *text_image;
int text_xvel;
int text_yvel;
};
void game_cleanup(struct Game *game, int exit_status);
bool load_media(struct Game *game);
bool sdl_initialize(struct Game *game);
void text_update(struct Game *game);
int main() {
struct Game game = {
.window = NULL,
.renderer = NULL,
.background = NULL,
.text_font = NULL,
.text_color = {255, 255, 255, 255},
.text_rect = {100, 0, 0, 0},
.text_image = NULL,
.text_xvel = 3,
.text_yvel = 3,
};
if (sdl_initialize(&game)) {
game_cleanup(&game, EXIT_FAILURE);
}
if (load_media(&game)) {
game_cleanup(&game, EXIT_FAILURE);
}
while (true) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
game_cleanup(&game, EXIT_SUCCESS);
break;
case SDL_KEYDOWN:
switch (event.key.keysym.scancode) {
case SDL_SCANCODE_ESCAPE:
game_cleanup(&game, EXIT_SUCCESS);
break;
case SDL_SCANCODE_SPACE:
SDL_SetRenderDrawColor(game.renderer, rand() % 256,
rand() % 256, rand() % 256, 255);
break;
default:
break;
}
default:
break;
}
}
text_update(&game);
SDL_RenderClear(game.renderer);
SDL_RenderCopy(game.renderer, game.background, NULL, NULL);
SDL_RenderCopy(game.renderer, game.text_image, NULL, &game.text_rect);
SDL_RenderPresent(game.renderer);
SDL_Delay(16);
}
game_cleanup(&game, EXIT_SUCCESS);
return 0;
}
void game_cleanup(struct Game *game, int exit_status) {
SDL_DestroyTexture(game->text_image);
TTF_CloseFont(game->text_font);
SDL_DestroyTexture(game->background);
SDL_DestroyRenderer(game->renderer);
SDL_DestroyWindow(game->window);
TTF_Quit();
IMG_Quit();
SDL_Quit();
exit(exit_status);
}
bool sdl_initialize(struct Game *game) {
if (SDL_Init(SDL_INIT_EVERYTHING)) {
fprintf(stderr, "Error initializing SDL: %s\n", SDL_GetError());
return true;
}
int img_init = IMG_Init(IMAGE_FLAGS);
if ((img_init & IMAGE_FLAGS) != IMAGE_FLAGS) {
fprintf(stderr, "Error initializing SDL_image: %s\n", IMG_GetError());
return true;
}
if (TTF_Init()) {
fprintf(stderr, "Error initializing SDL_ttf: %s\n", IMG_GetError());
return true;
}
game->window = SDL_CreateWindow(WINDOW_TITLE, SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
SCREEN_HEIGHT, 0);
if (!game->window) {
fprintf(stderr, "Error creating window: %s\n", SDL_GetError());
return true;
}
game->renderer = SDL_CreateRenderer(game->window, -1, 0);
if (!game->renderer) {
fprintf(stderr, "Error creating renderer: %s\n", SDL_GetError());
return true;
}
srand((unsigned)time(NULL));
return false;
}
bool load_media(struct Game *game) {
game->background = IMG_LoadTexture(game->renderer, "images/background.png");
if (!game->background) {
fprintf(stderr, "Error creating Texture: %s\n", IMG_GetError());
return true;
}
game->text_font = TTF_OpenFont("fonts/freesansbold.ttf", TEXT_SIZE);
if (!game->text_font) {
fprintf(stderr, "Error creating Font: %s\n", TTF_GetError());
return true;
}
SDL_Surface *surface =
TTF_RenderText_Blended(game->text_font, "SDL", game->text_color);
if (!surface) {
fprintf(stderr, "Error creating Surface: %s\n", SDL_GetError());
return true;
}
game->text_rect.w = surface->w;
game->text_rect.h = surface->h;
game->text_image = SDL_CreateTextureFromSurface(game->renderer, surface);
SDL_FreeSurface(surface);
if (!game->text_image) {
fprintf(stderr, "Error creating Texture: %s\n", SDL_GetError());
return true;
}
return false;
}
void text_update(struct Game *game) {
game->text_rect.x += game->text_xvel;
game->text_rect.y += game->text_yvel;
if (game->text_rect.x + game->text_rect.w > SCREEN_WIDTH) {
game->text_xvel = -3;
}
if (game->text_rect.x < 0) {
game->text_xvel = 3;
}
if (game->text_rect.y + game->text_rect.h > SCREEN_HEIGHT) {
game->text_yvel = -3;
}
if (game->text_rect.y < 0) {
game->text_yvel = 3;
}
}