summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Scheibenpflug <zorchenhimer@gmail.com>2015-03-18 00:47:50 (GMT)
committerNick Scheibenpflug <zorchenhimer@gmail.com>2015-03-18 00:47:50 (GMT)
commitc0579f33df4edcf09546f40b5dbfa198e8a2a259 (patch)
treed7f021683683c94c2215d0d743da2cccb36775b0
parent0f0572b8df93aa68c9cb8ae8a88fcc1ef3925939 (diff)
downloadspace-zap-c0579f33df4edcf09546f40b5dbfa198e8a2a259.zip
space-zap-c0579f33df4edcf09546f40b5dbfa198e8a2a259.tar.gz
Use InputHandler for handling input.
-rw-r--r--InputHandler.cpp27
-rw-r--r--main.cpp16
-rw-r--r--main.h1
-rw-r--r--space-zap.vcxproj1
-rw-r--r--space-zap.vcxproj.filters3
5 files changed, 31 insertions, 17 deletions
diff --git a/InputHandler.cpp b/InputHandler.cpp
index ca3c57a..5b9b2d7 100644
--- a/InputHandler.cpp
+++ b/InputHandler.cpp
@@ -8,17 +8,6 @@ InputHandler::~InputHandler() {
8 this->HandleVector.clear(); 8 this->HandleVector.clear();
9} 9}
10 10
11HandleNode::HandleNode(SDLKey key, CallbackKey callback, int id) {
12 this->Key = key;
13 this->Callback = callback;
14 this->ID = id;
15}
16
17HandleNode::~HandleNode() {
18 //delete this->Key; // lol, will this work?
19 //free(this->Callback);
20}
21
22int InputHandler::RegisterHandle(SDLKey key, CallbackKey callback) { 11int InputHandler::RegisterHandle(SDLKey key, CallbackKey callback) {
23 HandleNode* node = new HandleNode(key, callback, this->currentHandleID); 12 HandleNode* node = new HandleNode(key, callback, this->currentHandleID);
24 this->HandleVector.push_back(*node); 13 this->HandleVector.push_back(*node);
@@ -29,7 +18,8 @@ int InputHandler::RegisterHandle(SDLKey key, CallbackKey callback) {
29void InputHandler::DoCallback(SDLKey key) { 18void InputHandler::DoCallback(SDLKey key) {
30 // There can be more than one callback per key and visa versa. 19 // There can be more than one callback per key and visa versa.
31 for(std::vector<HandleNode>::iterator i = this->HandleVector.begin(); i != this->HandleVector.end(); ++i) { 20 for(std::vector<HandleNode>::iterator i = this->HandleVector.begin(); i != this->HandleVector.end(); ++i) {
32 i->Callback(); 21 if(i->Key == key)
22 i->Callback();
33 } 23 }
34} 24}
35 25
@@ -41,3 +31,16 @@ void InputHandler::UnRegisterHandle(int NodeID) {
41 } 31 }
42 } 32 }
43} 33}
34
35/* HandleNode */
36
37HandleNode::HandleNode(SDLKey key, CallbackKey callback, int id) {
38 this->Key = key;
39 this->Callback = callback;
40 this->ID = id;
41}
42
43HandleNode::~HandleNode() {
44 //delete this->Key; // lol, will this work?
45 //free(this->Callback);
46}
diff --git a/main.cpp b/main.cpp
index ec76071..d635950 100644
--- a/main.cpp
+++ b/main.cpp
@@ -1,21 +1,24 @@
1#include "main.h" 1#include "main.h"
2 2
3int main(int argc, char* argv[]) { 3int main(int argc, char* argv[]) {
4 // FIXME: this shit doesn't work for whatever reason. Keeps putting a ''
5 // character on the end of everything.
6 //SetWorkingDir(argv[0]);
4 SetWorkingDir("D:\\code\\cpp\\space-zap\\x64\\Debug"); 7 SetWorkingDir("D:\\code\\cpp\\space-zap\\x64\\Debug");
5 InitStuff(); 8 InitStuff();
6 9
7 Level* currentLevel = new Level(); 10 Level* currentLevel = new Level();
8 InputHandler* input; 11 InputHandler* input = new InputHandler();
9 12
10 Running = true; 13 Running = true;
11 14
15 input->RegisterHandle(SDLK_ESCAPE, &exit);
16
12 while(Running) { 17 while(Running) {
13 SDL_Event evnt; 18 SDL_Event evnt;
14 while(SDL_PollEvent(&evnt)) { 19 while(SDL_PollEvent(&evnt)) {
15 // Exit if escape is hit. 20 if(evnt.type == SDL_KEYDOWN) {
16 if(evnt.type == SDL_KEYDOWN && evnt.key.keysym.sym == SDLK_ESCAPE) { 21 input->DoCallback(evnt.key.keysym.sym);
17 Running = false;
18 break;
19 } 22 }
20 } 23 }
21 24
@@ -30,3 +33,6 @@ int main(int argc, char* argv[]) {
30 return 0; 33 return 0;
31} 34}
32 35
36void exit() {
37 Running = false;
38} \ No newline at end of file
diff --git a/main.h b/main.h
index 99de43f..b82a08b 100644
--- a/main.h
+++ b/main.h
@@ -10,6 +10,7 @@ char* WorkingDir;
10SDL_Surface* VideoSurface; 10SDL_Surface* VideoSurface;
11bool Running; 11bool Running;
12 12
13void exit();
13 14
14#include <SDL.h> 15#include <SDL.h>
15#include <SDL_image.h> 16#include <SDL_image.h>
diff --git a/space-zap.vcxproj b/space-zap.vcxproj
index 35401d1..78034cb 100644
--- a/space-zap.vcxproj
+++ b/space-zap.vcxproj
@@ -169,6 +169,7 @@ copy D:\code\cpp\SDL_image-1.2.12\lib\x64\*.dll $(OutDir)</Command>
169 <ClInclude Include="Bullet.h" /> 169 <ClInclude Include="Bullet.h" />
170 <ClInclude Include="Enemy.h" /> 170 <ClInclude Include="Enemy.h" />
171 <ClInclude Include="Enums.h" /> 171 <ClInclude Include="Enums.h" />
172 <ClInclude Include="InputHandler.h" />
172 <ClInclude Include="Level.h" /> 173 <ClInclude Include="Level.h" />
173 <ClInclude Include="main.h" /> 174 <ClInclude Include="main.h" />
174 <ClInclude Include="Player.h" /> 175 <ClInclude Include="Player.h" />
diff --git a/space-zap.vcxproj.filters b/space-zap.vcxproj.filters
index 4418398..75174b6 100644
--- a/space-zap.vcxproj.filters
+++ b/space-zap.vcxproj.filters
@@ -65,5 +65,8 @@
65 <ClInclude Include="SDL_rotozoom.h"> 65 <ClInclude Include="SDL_rotozoom.h">
66 <Filter>Header Files</Filter> 66 <Filter>Header Files</Filter>
67 </ClInclude> 67 </ClInclude>
68 <ClInclude Include="InputHandler.h">
69 <Filter>Header Files</Filter>
70 </ClInclude>
68 </ItemGroup> 71 </ItemGroup>
69</Project> \ No newline at end of file 72</Project> \ No newline at end of file