-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathState.h
More file actions
105 lines (86 loc) · 2.76 KB
/
State.h
File metadata and controls
105 lines (86 loc) · 2.76 KB
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
/*******************************************************************************
CommanderTux
Penguin In Space
Released under the GNU Public License
2005 by Andr� Schnabel (thefrogs@web.de)
*******************************************************************************/
// State.h
#ifndef STATE_H
#define STATE_H
#include <SDL.h>
#include "Framework.h"
// Input stuff
struct SReleased
{
bool key_return, key_escape;
bool key_left, key_right;
bool key_up, key_down;
bool key_s, key_p; // for the editor
bool key_2, key_3, key_4;
bool key_space;
SReleased()
{
key_return = key_escape = false;
key_left = key_right = false;
key_up = key_down = false;
key_s = key_p = false;
key_2 = key_3 = key_4 = false;
key_space = false;
}
};
// TODO: time based motion
// prevent the game from running to fast on better machines
#define MAX_FPS 25
// Every state in the game has to derive from this class
class CState
{
public:
// Constructor
CState()
{ m_max_fps = MAX_FPS; }
// Contains to fundamental lines for input processing
// Every derived class should call this
virtual void Input()
{
SDL_PumpEvents();
m_keys = SDL_GetKeyboardState( nullptr );
if( m_keys[SDL_SCANCODE_F3] )
g_framework->MakeScreenshot("../screenshots/screenshot.bmp");
}
virtual void Update() = 0;
virtual void Draw() = 0;
// The main loop // shouldn't be modified by any derived class
bool Loop() // returns false if quit
{
// So we never use an incomplete initialized framework
if( g_framework->GetLoaded()->everything )
{
Uint32 old_time;
m_done = false;
while( !m_done )
{
old_time = g_framework->GetTicks();
// Event handling... only interesting in windowed mode
while( SDL_PollEvent( &m_event ) )
{
if( m_event.type == SDL_QUIT )
return false;
}
Input();
Update();
Draw();
SDL_RenderPresent( g_framework->GetRenderer() );
while( (g_framework->GetTicks() - old_time) < 1000 / m_max_fps );
}
return true;
}
return false;
}
bool GetDone() { return m_done; }
protected:
const Uint8 *m_keys;
bool m_done;
SDL_Event m_event;
Uint32 m_max_fps;
};
#endif