| 
1 |  | -#include <SDL3/SDL.h>  | 
2 |  | -#include <SDL3/SDL_main.h>  | 
3 |  | -#include <SDL3_image/SDL_image.h>  | 
4 |  | -#include <stdbool.h>  | 
5 |  | -#include <stdio.h>  | 
6 |  | -#include <stdlib.h>  | 
7 |  | -#include <time.h>  | 
8 |  | - | 
9 |  | -#define SDL_FLAGS SDL_INIT_VIDEO  | 
10 |  | - | 
11 |  | -#define WINDOW_TITLE "Changing Colors"  | 
12 |  | -#define WINDOW_WIDTH 800  | 
13 |  | -#define WINDOW_HEIGHT 600  | 
14 |  | - | 
15 |  | -struct Game {  | 
16 |  | - SDL_Window *window;  | 
17 |  | - SDL_Renderer *renderer;  | 
18 |  | - bool is_running;  | 
19 |  | - SDL_Event event;  | 
20 |  | - SDL_Texture *background;  | 
21 |  | -};  | 
22 |  | - | 
23 |  | -bool game_load_media(struct Game *g);  | 
24 |  | -bool game_init_sdl(struct Game *g);  | 
25 |  | -bool game_new(struct Game *g);  | 
26 |  | -void game_free(struct Game *g);  | 
27 |  | -void game_render_color(struct Game *g);  | 
28 |  | -void game_events(struct Game *g);  | 
29 |  | -void game_draw(const struct Game *g);  | 
30 |  | -void game_run(struct Game *g);  | 
31 |  | - | 
32 |  | -bool game_init_sdl(struct Game *g) {  | 
33 |  | - if (!SDL_Init(SDL_FLAGS)) {  | 
34 |  | - fprintf(stderr, "Error initializing SDL: %s\n", SDL_GetError());  | 
35 |  | - return false;  | 
36 |  | - }  | 
37 |  | - | 
38 |  | - g->window = SDL_CreateWindow(WINDOW_TITLE, WINDOW_WIDTH, WINDOW_HEIGHT, 0);  | 
39 |  | - if (!g->window) {  | 
40 |  | - fprintf(stderr, "Error creating window: %s\n", SDL_GetError());  | 
41 |  | - return false;  | 
42 |  | - }  | 
43 |  | - | 
44 |  | - g->renderer = SDL_CreateRenderer(g->window, NULL);  | 
45 |  | - if (!g->renderer) {  | 
46 |  | - fprintf(stderr, "Error creating renderer: %s\n", SDL_GetError());  | 
47 |  | - return false;  | 
48 |  | - }  | 
49 |  | - | 
50 |  | - return true;  | 
51 |  | -}  | 
52 |  | - | 
53 |  | -bool game_load_media(struct Game *g) {  | 
54 |  | - g->background = IMG_LoadTexture(g->renderer, "images/background.png");  | 
55 |  | - if (!g->background) {  | 
56 |  | - fprintf(stderr, "Error creating Texture: %s\n", SDL_GetError());  | 
57 |  | - return false;  | 
58 |  | - }  | 
59 |  | - | 
60 |  | - return true;  | 
61 |  | -}  | 
62 |  | - | 
63 |  | -bool game_new(struct Game *g) {  | 
64 |  | - g->is_running = true;  | 
65 |  | - | 
66 |  | - if (!game_init_sdl(g)) {  | 
67 |  | - return false;  | 
68 |  | - }  | 
69 |  | - | 
70 |  | - if (!game_load_media(g)) {  | 
71 |  | - return false;  | 
72 |  | - }  | 
73 |  | - | 
74 |  | - srand((unsigned)time(NULL));  | 
75 |  | - | 
76 |  | - return true;  | 
77 |  | -}  | 
78 |  | - | 
79 |  | -void game_free(struct Game *g) {  | 
80 |  | - if (g->background) {  | 
81 |  | - SDL_DestroyTexture(g->background);  | 
82 |  | - g->background = NULL;  | 
83 |  | - }  | 
84 |  | - | 
85 |  | - if (g->renderer) {  | 
86 |  | - SDL_DestroyRenderer(g->renderer);  | 
87 |  | - g->renderer = NULL;  | 
88 |  | - }  | 
89 |  | - | 
90 |  | - if (g->window) {  | 
91 |  | - SDL_DestroyWindow(g->window);  | 
92 |  | - g->window = NULL;  | 
93 |  | - }  | 
94 |  | - | 
95 |  | - SDL_Quit();  | 
96 |  | - | 
97 |  | - printf("all clean!\n");  | 
98 |  | -}  | 
99 |  | - | 
100 |  | -void game_render_color(struct Game *g) {  | 
101 |  | - SDL_SetRenderDrawColor(g->renderer, (Uint8)rand() % 256,  | 
102 |  | - (Uint8)rand() % 256, (Uint8)rand() % 256, 255);  | 
103 |  | -}  | 
104 |  | - | 
105 |  | -void game_events(struct Game *g) {  | 
106 |  | - while (SDL_PollEvent(&g->event)) {  | 
107 |  | - switch (g->event.type) {  | 
108 |  | - case SDL_EVENT_QUIT:  | 
109 |  | - g->is_running = false;  | 
110 |  | - break;  | 
111 |  | - case SDL_EVENT_KEY_DOWN:  | 
112 |  | - switch (g->event.key.scancode) {  | 
113 |  | - case SDL_SCANCODE_ESCAPE:  | 
114 |  | - g->is_running = false;  | 
115 |  | - break;  | 
116 |  | - case SDL_SCANCODE_SPACE:  | 
117 |  | - game_render_color(g);  | 
118 |  | - break;  | 
119 |  | - default:  | 
120 |  | - break;  | 
121 |  | - }  | 
122 |  | - break;  | 
123 |  | - default:  | 
124 |  | - break;  | 
125 |  | - }  | 
126 |  | - }  | 
127 |  | -}  | 
128 |  | - | 
129 |  | -void game_draw(const struct Game *g) {  | 
130 |  | - SDL_RenderClear(g->renderer);  | 
131 |  | - | 
132 |  | - SDL_RenderTexture(g->renderer, g->background, NULL, NULL);  | 
133 |  | - | 
134 |  | - SDL_RenderPresent(g->renderer);  | 
135 |  | -}  | 
136 |  | - | 
137 |  | -void game_run(struct Game *g) {  | 
138 |  | - while (g->is_running) {  | 
139 |  | - | 
140 |  | - game_events(g);  | 
141 |  | - | 
142 |  | - game_draw(g);  | 
143 |  | - | 
144 |  | - SDL_Delay(16);  | 
145 |  | - }  | 
146 |  | -}  | 
 | 1 | +#include "game.h"  | 
147 | 2 | 
 
  | 
148 | 3 | int main(void) {  | 
149 | 4 |  bool exit_status = EXIT_FAILURE;  | 
150 | 5 | 
 
  | 
151 |  | - struct Game game = {0};  | 
 | 6 | + struct Game *game = NULL;  | 
152 | 7 | 
 
  | 
153 | 8 |  if (game_new(&game)) {  | 
154 |  | - | 
155 |  | - game_run(&game);  | 
 | 9 | + game_run(game);  | 
156 | 10 | 
 
  | 
157 | 11 |  exit_status = EXIT_SUCCESS;  | 
158 | 12 |  }  | 
 | 
0 commit comments