-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
111 lines (84 loc) · 2.14 KB
/
test.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
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
//function declarations
bool init();
bool makeWindow();
void end();
void loop();
SDL_Window* gWindow = NULL;
SDL_Surface* gScreenSurface = NULL;
SDL_Surface* gHelloWorld = NULL;
bool init() {
bool success = SDL_Init(SDL_INIT_VIDEO);
if( !success ) {
SDL_Log("Initialization failed! Cuz: %s\n", SDL_GetError());
}
return success;
}
bool makeWindow() {
const int kScreenWidth = 640;
const int kScreenHeight = 480;
gWindow = SDL_CreateWindow("SDL3 Tutorial: Hello SDL3!", kScreenWidth, kScreenHeight, 0);
if( gWindow == NULL) {
SDL_Log("Window creation failed: %s\n", SDL_GetError());
return false;
}
//get draw surface
gScreenSurface = SDL_GetWindowSurface( gWindow );
return true;
}
bool loadMedia() {
char* imagePath = "hello.bmp";
gHelloWorld = SDL_LoadBMP( imagePath );
if( gHelloWorld == NULL ) {
SDL_Log( "Unable to load image %s! Cuz: %s\n", imagePath, SDL_GetError() );
return false;
}
return true;
}
void end() {
//Destroy surface
SDL_DestroySurface( gHelloWorld );
gHelloWorld = NULL;
//Destroy window
SDL_DestroyWindow( gWindow );
gWindow = NULL;
gScreenSurface = NULL;
SDL_Quit();
}
void loop() {
//Main game loop
bool quit = false;
SDL_Event e;
SDL_zero( e ); //ensure that we're dealing with zeroed out memory
while( !quit ) {
//start by processing events
while( SDL_PollEvent( &e ) ) {
if( e.type == SDL_EVENT_QUIT ) {
quit = true;
}
}
//then, perform updates
SDL_FillSurfaceRect( gScreenSurface,
NULL,
SDL_MapSurfaceRGB( gScreenSurface, 0xFF, 0xFF, 0xFF) );
SDL_BlitSurface( gHelloWorld, NULL, gScreenSurface, NULL );
//update the surface
SDL_UpdateWindowSurface( gWindow );
}
}
int main(int argc, char **argv) {
bool success = true;
//initialize
if( !init() )
return -1;
//create window
if( !makeWindow() )
return -2;
//Now, load media
if ( !loadMedia() )
return -4;
loop();
end();
return 0;
}