-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlearning.cpp
134 lines (101 loc) · 2.63 KB
/
learning.cpp
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
//Using SDL and standard IO
#include <SDL.h>
#include <stdio.h>
#include "engine/player.h"
//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
//Starts up SDL and creates window
bool init();
//Loads media
bool loadMedia();
//Frees media and shuts down SDL
void close();
//The window we'll be renderning to
SDL_Window* window = NULL;
//The surface contained by the window
SDL_Surface* screenSurface = NULL;
//The image we will load and show on the screen
SDL_Surface* helloWorld = NULL;
int main( int argc, char* args[] )
{
Player player1;
//Start up SDL and create window
if( !init() ) {
printf( "Failed to initialize!\n" );
} else {
//Load media
if( !loadMedia() ) {
printf( "Failed to load media!\n" );
} else {
printf( "loaded media!\n" );
}
}
//Mail loop flag
bool quit = false;
//Event handler
while( !quit ) {
//Handle events on queue
//Apply the image
SDL_BlitSurface( helloWorld, NULL, screenSurface, NULL );
//Update the surface
SDL_UpdateWindowSurface( window );
if (player1.getInput() == "quit") {
quit = true;
}
}
//Destroy window
SDL_DestroyWindow( window );
//Quit SDL subsystems
SDL_Quit();
return 0;
}
bool init()
{
//Initialization flag
bool success = true;
//Initialize SDL
if ( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
success = false;
} else {
//Create Window
window = SDL_CreateWindow( "Learning", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
if (window == NULL ) {
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
success = false;
} else {
//Get window surface
screenSurface = SDL_GetWindowSurface( window );
//Fill the surface white
SDL_FillRect( screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0xFF, 0xFF, 0xFF ) );
//UPdate the surface
SDL_UpdateWindowSurface( window );
//Wait two seconds
}
}
return success;
}
bool loadMedia()
{
//Loading success flag
bool success = true;
//Load splash image
helloWorld = SDL_LoadBMP( "assets/hello_world.bmp" );
if( helloWorld == NULL ) {
printf( "Unable to load image %s! SDL Error: %s\n", "assets/hello_world.bmp", SDL_GetError() );
success = false;
}
return success;
}
void close()
{
//Deallocate surface
SDL_FreeSurface( helloWorld );
helloWorld = NULL;
//Destroy Window
SDL_DestroyWindow( window );
window = NULL;
//Quite SDL subsystems
SDL_Quit();
}