-
Notifications
You must be signed in to change notification settings - Fork 0
/
SafeguardMain.cpp
273 lines (221 loc) · 6 KB
/
SafeguardMain.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
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
191
192
193
//Using SDL and standard io
#include <SDL.h>
#include <stdio.h>
//starts SDL, creates window
bool init();
//loads media
bool loadMedia();
//frees media, shut down SDL
void close();//FPS thread and its spinlock (for fps counter variable)int fpsworker(void* data);
SDL_SpinLock gDataLock = 0;
//Event handlerSDL_Event e;
//window we're rendering to
SDL_Window* gWindow = NULL;
//surface contained by the window
SDL_Surface* gScreenSurface = NULL;
//The image we will load/show on the screen
SDL_Surface* gYWing = NULL;
//screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;//main loop end flagbool quit;//for fps counterint framesThisSecond; //this is displayed by main functionint countedFrames; //constantly increments, set to 0 by fpsworker once per second
//Keeps track of time between steps
LTimer stepTimer;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
gWindow = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if(gWindow == NULL)
{
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
success = false;
}
else
{
//get window surface
gScreenSurface = SDL_GetWindowSurface(gWindow);
}
}
return success;
}
bool loadMedia()
{
//loading success flag
bool success = true;
//load image
gYWing = SDL_LoadBMP()
}
void close()
{
//Free loaded images
gSplashTexture.free();
//Destroy window
SDL_DestroyRenderer( gRenderer );
SDL_DestroyWindow( gWindow );
gWindow = NULL;
gRenderer = NULL;
//Quit SDL subsystems
IMG_Quit();
SDL_Quit();
}int fpsworker( void* data )
{
//Work continuously SDL_AtomicLock( &gDataLock ); bool fpsquit = quit; SDL_AtomicUnlock( &gDataLock ); while(!fpsquit)
{
SDL_AtomicLock( &gDataLock ); framesThisSecond = countedFrames; countedFrames = 0; SDL_AtomicUnlock( &gDataLock );
//Wait one second
SDL_Delay(1000);
}
return 0;
}
void processInput(SDL_Event e)
{ while(SDL_PollEvent(&e) != 0) { if(e.type == SDL_QUIT) { SDL_AtomicLock( &gDataLock ); quit = true; SDL_AtomicUnlock( &gDataLock ); } } const Uint8* currentKeyStates = SDL_GetKeyboardState(NULL); if(currentKeyStates[SDL_SCANCODE_UP]) { //do something }
}
void update()
{
//Calculate time step
float timeStep = stepTimer.getTicks()/1000.f;
//pseudo:
//update any world stuff (background objects, etc)
//NPC AI
//for each npc
//npc.strategize
//for each object in the world
//object.framenumber++
//object.move (x += xv, y += yv)
//handle collisions
//Move for time step
stepTimer.start(); //Calculate FPS timeText.str(""); SDL_AtomicLock( &gDataLock ); timeText << "FPS: " << framesThisSecond; SDL_AtomicUnlock( &gDataLock ); if(!gFPSTextTexture.loadFromRenderedText(timeText.str().c_str(), fpsColor)) { printf("Unable to render FPS texture.\n"); }
}
void render()
{ //Clear screen SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF); SDL_RenderClear(gRenderer);
//draw background
//draw background objects
//draw bullets
//draw npcs
//draw player
//draw asteroids/explosions/effects //draw FPS number (framesThisSecond) gFPSTextTexture.render((SCREEN_WIDTH - gFPSTextTexture.getWidth())/2, (SCREEN_HEIGHT - gFPSTextTexture.getHeight())/2);
//flip SDL_RenderPresent(gRenderer); //Increment frame counter SDL_AtomicLock( &gDataLock ); countedFrames++; SDL_AtomicUnlock( &gDataLock );
}
void delay()
{
//delay such that game runs at 60fps
}
int main( int argc, char* args[] )
{
//The window being rendered to
SDL_Window* window = NULL;
//THe surface contained by the window
SDL_Surface* screenSurface = NULL;
//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
}
else
{
//Create window
window = SDL_CreateWindow( "SDL Tutorial", 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() );
}
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 2 seconds
SDL_Delay(2000);
}
} quit = false; countedFrames = 0; framesThisSecond = 0; //Start fps counting thread SDL_Thread* fpsThread = SDL_CreateThread(fpsworker, "FPSThread", NULL); //FPS text std::stringstream timeText; SDL_Color fpsColor = {0,0,0,255};
#MAIN GAME LOOP SDL_AtomicLock( &gDataLock ); bool mainquit = quit; SDL_AtomicUnlock( &gDataLock );
while(!mainquit)
{
//Get/process input from user
processInput();
//Make all input-independent updates (game world, physics, AI, etc)
update();
//Draw world + objects
render();
//Delay (goal: 120 fps with vsync)
delay(); }
SDL_WaitThread(fpsThread, NULL);
//Destroy window
SDL_DestroyWindow( window );
//Quit SDL subsystems
SDL_Quit();
return 0;
}