From 2d49fd29abf3310dcf8bfca1479603d7265f83e6 Mon Sep 17 00:00:00 2001 From: Peter Schaefer Date: Sat, 4 Apr 2015 09:58:27 +0200 Subject: [PATCH] corrected Events (right Keys/Action) --- Weave/Events.cpp | 146 +++++++++++++++++++++++++--------------- Weave/Events.h | 30 +++++---- Weave/Graphix/Model.cpp | 26 +++---- Weave/Graphix/Model.h | 2 +- Weave/Graphix/Scene.cpp | 42 ++++++------ 5 files changed, 144 insertions(+), 102 deletions(-) diff --git a/Weave/Events.cpp b/Weave/Events.cpp index c6908af..e7e6ea3 100644 --- a/Weave/Events.cpp +++ b/Weave/Events.cpp @@ -7,7 +7,7 @@ using std::cout; using std::endl; -int returnSet(int& value, int action2 = 0); + void Events::processEvents() @@ -21,60 +21,50 @@ void Events::processEvents() break; } case SDL_WINDOWEVENT: - { switch (sdl_event.window.event) { case SDL_WINDOWEVENT_FOCUS_GAINED: - { window_focus = true; Graphix::catchMouse(); break; - } case SDL_WINDOWEVENT_FOCUS_LOST: - { window_focus = false; Graphix::freeMouse(); break; - } case SDL_WINDOWEVENT_RESIZED: - { Graphix::setWindowSize(sdl_event.window.data1, sdl_event.window.data2); break; - } default: break; } break; - } - case SDL_KEYDOWN:{ + case SDL_KEYDOWN: if (sdl_event.key.keysym.sym == SDLK_ESCAPE) { - quit = true; + quit = true; //TODO nicht gleich alles beenden bei Escape + halt = true; } - key_held[sdl_event.key.keysym.sym]=true; + if (!isKHeld(sdl_event.key.keysym.sym)) + KeyDown(sdl_event.key.keysym.sym); + + key_held[sdl_event.key.keysym.sym] = true; break; - } - case SDL_KEYUP:{ + case SDL_KEYUP: + KeyUp(sdl_event.key.keysym.sym); key_held[sdl_event.key.keysym.sym]=false; - fKey(sdl_event.key.keysym.sym); break; - } - case SDL_MOUSEMOTION:{ + case SDL_MOUSEMOTION: if (window_focus) mousemotion(sdl_event.motion); break; - } - case SDL_MOUSEBUTTONDOWN:{ - if (sdl_event.button.button == SDL_BUTTON_LEFT) - { - action1++; - } - if (sdl_event.button.button == SDL_BUTTON_RIGHT) - { - action2++; - } + case SDL_MOUSEBUTTONDOWN: + MouseDown(sdl_event.button.button); + mouse_held[sdl_event.button.button] = true; + break; + case SDL_MOUSEBUTTONUP: + MouseUp(sdl_event.button.button); + mouse_held[sdl_event.button.button] = false; break; - } default: break; } @@ -89,8 +79,8 @@ void Events::mousemotion(SDL_MouseMotionEvent sdl_motion) //if (sdl_motion.x!=400 && sdl_motion.y!=200) { - mouse_x = sdl_motion.xrel; - mouse_y = sdl_motion.yrel; + view_x = sdl_motion.xrel; + view_y = sdl_motion.yrel; SDL_WarpMouseInWindow(NULL, Graphix::getWindowWidth() / 2, Graphix::getWindowHeight() / 2); @@ -98,7 +88,7 @@ void Events::mousemotion(SDL_MouseMotionEvent sdl_motion) } } -void Events::fKey(int _key) +void Events::KeyDown(int _key) { switch (_key) { @@ -132,6 +122,51 @@ void Events::fKey(int _key) case SDLK_F10: //??? break; + case SDLK_SPACE: + jump++; + break; + case SDLK_LSHIFT: + timeshift = true; + break; + default: + break; + } +} + +void Events::KeyUp(int _key) +{ + switch (_key) + { + case SDLK_LSHIFT: + timeshift = false; + break; + default: + break; + } +} + +void Events::MouseDown(int _key) +{ + switch (_key) + { + case SDL_BUTTON_LEFT: + jump++; + break; + case SDL_BUTTON_RIGHT: + timeshift = true; + break; + default: + break; + } +} + +void Events::MouseUp(int _key) +{ + switch (_key) + { + case SDL_BUTTON_RIGHT: + timeshift = false; + break; default: break; } @@ -139,9 +174,8 @@ void Events::fKey(int _key) void Events::keymotion() { - axis(view_x, SDLK_w, SDLK_s); - axis(view_y, SDLK_a, SDLK_d); - axis(view_z, SDLK_e, SDLK_q); + axis(move_x, SDLK_w, SDLK_s); + axis(move_y, SDLK_a, SDLK_d); } void Events::padmotion() @@ -174,45 +208,46 @@ void Events::axis(int& axis, int keyUP, int keyDOWN) int Events::getViewX() { - return view_x; + return returnSet(view_x); } int Events::getViewY() { - return view_y; + return returnSet(view_y); } -int Events::getViewZ() -{ - return view_z; -} -int Events::getMouseX() +int Events::getMoveX() { - return returnSet(mouse_x); + return move_x; } -int Events::getMouseY() +int Events::getMoveY() { - return returnSet(mouse_y); + return move_y; } -int Events::getAction1() +int Events::getJump() { - return returnSet(action1); + return returnSet(jump); } -int Events::getAction2() +bool Events::getTimeShift() { - return returnSet(action2); + return timeshift; } -bool Events::isHeld(int const _key) +bool Events::isKHeld(int const _key) { return key_held[_key]; } +bool Events::isMHeld(int const _key) +{ + return mouse_held[_key]; +} + -int returnSet(int& value, int action2) +int Events::returnSet(int& value, int action2) { int tmp = value; value = action2; @@ -225,15 +260,16 @@ map Events::pad_held; int Events::view_x = 0; int Events::view_y = 0; -int Events::view_z = 0; -int Events::mouse_x = 0; -int Events::mouse_y = 0; +int Events::move_x = 0; +int Events::move_y = 0; -int Events::action1 = 0; -int Events::action2 = 0; +int Events::jump = 0; +bool Events::timeshift = false; bool Events::quit = false; +bool Events::halt = false; + bool Events::window_focus = true; diff --git a/Weave/Events.h b/Weave/Events.h index 16d7248..b0158f5 100644 --- a/Weave/Events.h +++ b/Weave/Events.h @@ -14,20 +14,21 @@ public: //virtual void keydown(SDL_Keycode sdl_event); bool static quit; + bool static halt; bool static window_focus; int static getViewX(); int static getViewY(); - int static getViewZ(); - int static getMouseX(); - int static getMouseY(); + int static getMoveX(); + int static getMoveY(); - int static getAction1(); - int static getAction2(); + int static getJump(); + bool static getTimeShift(); - bool static isHeld(int const); + bool static isKHeld(int const); + bool static isMHeld(int const); private: @@ -35,10 +36,16 @@ private: void static keymotion(); void static padmotion(); - void static fKey(int); + void static KeyDown(int); + void static KeyUp(int); + + void static MouseDown(int); + void static MouseUp(int); void static axis(int&, int, int); + int static returnSet(int& value, int action2 = 0); + //const Uint8* keymap; map static key_held; map static mouse_held; @@ -46,13 +53,12 @@ private: int static view_x; int static view_y; - int static view_z; - int static mouse_x; - int static mouse_y; + int static move_x; + int static move_y; - int static action1; - int static action2; + int static jump; + bool static timeshift; }; diff --git a/Weave/Graphix/Model.cpp b/Weave/Graphix/Model.cpp index 5e97749..55de010 100644 --- a/Weave/Graphix/Model.cpp +++ b/Weave/Graphix/Model.cpp @@ -48,19 +48,19 @@ Model::~Model() } -void Model::useModel(Shader* _shader) -{ - uint vao=-1; - auto i = shader_map.find(*_shader); - if (i == shader_map.end()) - vao = bindShader(_shader); - else - vao = i->second; - - glBindVertexArray(vao); - _shader->useShader(); - -} +//void Model::useModel(Shader* _shader) +//{ +// uint vao=-1; +// auto i = shader_map.find(*_shader); +// if (i == shader_map.end()) +// vao = bindShader(_shader); +// else +// vao = i->second; +// +// glBindVertexArray(vao); +// _shader->useShader(); +// +//} void Model::useModel(Shader* _shader) const { diff --git a/Weave/Graphix/Model.h b/Weave/Graphix/Model.h index 9435323..f1af0d1 100644 --- a/Weave/Graphix/Model.h +++ b/Weave/Graphix/Model.h @@ -18,7 +18,7 @@ public: virtual ~Model(); - void useModel(Shader* shader); +// void useModel(Shader* shader); void useModel(Shader* shader) const; void drawModel() const; unsigned int bindShader(Shader* shader); diff --git a/Weave/Graphix/Scene.cpp b/Weave/Graphix/Scene.cpp index 2ab84d1..6e441d7 100644 --- a/Weave/Graphix/Scene.cpp +++ b/Weave/Graphix/Scene.cpp @@ -79,10 +79,10 @@ void Scene::update(float deltaT) { Message::error("GameOver"); } - int move_x = Events::getMouseX(); - int move_y = Events::getMouseY(); - int shoot = Events::getAction1(); - int reset = Events::getAction2(); + int move_x = Events::getMoveX(); + int move_y = Events::getMoveY(); + //int shoot = Events::getAction1(); + //int reset = Events::getAction2(); //cout << move_x << " " << move_y << endl; @@ -90,13 +90,13 @@ void Scene::update(float deltaT) lookat->turn(0.3f *move_x, vec3(0.f, 1.f, 0.f)); if (move_y) lookat->turn(0.3f *move_y, vec3(1.f, 0.f, 0.f)); - if (shoot) - { - vec3 direction = lookat->push(vec3(0.f, 0.f, shoot*0.1f)); - //SceneObjects->push_back(self->copy();) - } - if (reset) - lookat->stop(); + //if (shoot) + //{ + // vec3 direction = lookat->push(vec3(0.f, 0.f, shoot*0.1f)); + // //SceneObjects->push_back(self->copy();) + //} + //if (reset) + // lookat->stop(); // XYAchse um den Player if (Events::getViewX() || Events::getViewY()) @@ -112,16 +112,16 @@ void Scene::update(float deltaT) } - // Zoom auf Player - if (Events::getViewZ()) - { - // float dist = 0; - // if (lookat!=NULL) - // dist = VektorAbs(getViewerPos() - lookat->getPosition()); - // if (true) - view = translate( vec3(0.f,0.f,(float)(5*deltaT *sign(Events::getViewZ()))))*view; - //view = view*scale(vec3((float)(1 + deltaT * sign(Events::getViewZ())))); - } + //// Zoom auf Player + //if (Events::getViewZ()) + //{ + //// float dist = 0; + //// if (lookat!=NULL) + //// dist = VektorAbs(getViewerPos() - lookat->getPosition()); + //// if (true) + // view = translate( vec3(0.f,0.f,(float)(5*deltaT *sign(Events::getViewZ()))))*view; + // //view = view*scale(vec3((float)(1 + deltaT * sign(Events::getViewZ())))); + //} if (lookat != NULL) pos = lookat->getPosition(); -- 2.47.3