-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
155 lines (142 loc) · 3.29 KB
/
main.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
#include "gwindow.h"
Uint32 callback( Uint32 interval, void* param );
Uint32 callbackPistol( Uint32 interval, void* param );
Uint32 callbackBazooka( Uint32 interval, void* param );
Uint32 callbackSniper( Uint32 interval, void* param );
GWindow* mainWindow;
SDL_TimerID timerGame;
SDL_TimerID timerPistol;
SDL_TimerID timerBazooka;
SDL_TimerID timerSniper;
bool move_up = false;
bool move_down = false;
bool move_right = false;
bool move_left = false;
bool pistolOn = false;
bool sniperrifleOn = false;
bool bazookaOn = false;
bool PlayerShootOn = false;
int PlayerDstX = 0;
int PlayerDstY = 0;
const Uint8 * keystate = SDL_GetKeyboardState( NULL );
void handleEvent(SDL_Event *e);
int main( int argc, char* args[] )
{
mainWindow=new GWindow();
if(!mainWindow->init())
{
std::cout<<"Failed to initialize!\n";
}
else
{
if(!mainWindow->loadMedia())
{
std::cout<<"Failed to load media!\n";
}
else
{
timerGame = SDL_AddTimer(60, callback, NULL);
timerPistol = SDL_AddTimer(500, callbackPistol, NULL);
timerBazooka = SDL_AddTimer(1500, callbackBazooka, NULL);
timerSniper = SDL_AddTimer(2000, callbackSniper, NULL);
bool quit=false;
SDL_Event e;
while (!quit)
{
while ((SDL_PollEvent(&e)) != 0)
{
if (e.type == SDL_QUIT)
{
quit = true;
}
handleEvent(&e);
}
}
}
}
SDL_RemoveTimer(timerGame);
delete mainWindow;
return 0;
}
Uint32 callback(Uint32 interval, void *param)
{
if( keystate[ SDL_SCANCODE_D ] )
{
move_right = true;
}
else{
move_right = false;
}
if( keystate[ SDL_SCANCODE_A ] )
{
move_left = true;
}
else{
move_left = false;
}
if( keystate[ SDL_SCANCODE_W ] )
{
move_up = true;
}
else{
move_up = false;
}
if( keystate[ SDL_SCANCODE_S ] )
{
move_down = true;
}
else{
move_down = false;
}
if ( keystate[ SDL_SCANCODE_1 ] )
{
pistolOn=true;
bazookaOn=false;
sniperrifleOn=false;
}
if ( keystate[ SDL_SCANCODE_2 ] )
{
pistolOn=false;
bazookaOn=true;
sniperrifleOn=false;
}
if ( keystate[ SDL_SCANCODE_3 ] )
{
pistolOn=false;
bazookaOn=false;
sniperrifleOn=true;
}
mainWindow->timerUpdate();
return interval;
}
Uint32 callbackPistol(Uint32 interval, void *param)
{
mainWindow->getGamePtr()->PistolShoot();
return interval;
}
Uint32 callbackBazooka(Uint32 interval, void *param)
{
mainWindow->getGamePtr()->BazookaShoot();
return interval;
}
Uint32 callbackSniper(Uint32 interval, void *param)
{
mainWindow->getGamePtr()->SniperShoot();
return interval;
}
void handleEvent(SDL_Event *e)
{
if(e->type == SDL_MOUSEMOTION)
{
SDL_GetMouseState( &PlayerDstX, &PlayerDstY );
}
if(e->type == SDL_MOUSEBUTTONDOWN)
{
PlayerShootOn=true;
SDL_GetMouseState( &PlayerDstX, &PlayerDstY );
}
if(e->type == SDL_MOUSEBUTTONUP)
{
PlayerShootOn=false;
}
}