Skip to content
Snippets Groups Projects
Commit 4808969a authored by Pascal Engeler's avatar Pascal Engeler
Browse files

Added input polling

parent 4982c82d
No related branches found
No related tags found
No related merge requests found
#pragma once
#include <SDL.h>
class InputState {
public:
static void update(const unsigned width, const unsigned height);
//state of mouse buttons
static bool mwheel_pressed;
static bool lmb_pressed;
static bool lmb_click;
static bool rmb_pressed;
//state of tracked keyboard buttons
static bool esc_click; //esc is down
//mouse position
static float mpos_x;
static float mpos_y;
static unsigned umpos_x;
static unsigned umpos_y;
//relative mouse motion
static float mmot_x;
static float mmot_y;
//relative mouse wheel motion
static float mwheelmot_y;
//quit event
static bool should_quit;
};
#include <input_state.hpp>
#include <iostream>
#include <imgui_bundle.hpp>
bool InputState::mwheel_pressed = false;
bool InputState::lmb_pressed = false;
bool InputState::lmb_click = false;
bool InputState::rmb_pressed = false;
bool InputState::esc_click = false;
float InputState::mpos_x = 0.f;
float InputState::mpos_y = 0.f;
unsigned InputState::umpos_x = 0;
unsigned InputState::umpos_y = 0;
float InputState::mmot_x = 0.f;
float InputState::mmot_y = 0.f;
float InputState::mwheelmot_y = 0.f;
bool InputState::should_quit = false;
void InputState::update(const unsigned width, const unsigned height) {
//reset relative data
mmot_x = 0.f;
mmot_y = 0.f;
mwheelmot_y = 0.f;
lmb_click = false;
esc_click = false;
SDL_Event event;
while (SDL_PollEvent(&event)) {
/*Hand event off to ImGui*/
ImGui_ImplSDL2_ProcessEvent(&event);
if (ImGui::GetIO().WantCaptureMouse) {
continue;
}
switch (event.type) {
case SDL_QUIT:
should_quit = true;
break;
case SDL_MOUSEMOTION:
mpos_x = event.motion.x / static_cast<float>(width);
mpos_y = 1. - event.motion.y / static_cast<float>(height);
umpos_x = event.motion.x;
umpos_y = event.motion.y;
mmot_x = event.motion.xrel / static_cast<float>(width);
mmot_y = -event.motion.yrel / static_cast<float>(height);
#ifndef NDEBUG
std::cout << "Mouse Motion: "
<< "(" << event.motion.x << ", " << event.motion.y << "), "
<< "(" << event.motion.xrel << ", " << event.motion.yrel << "), "
<< event.motion.state << std::endl;
#endif
break;
case SDL_MOUSEBUTTONDOWN:
switch (event.button.button) {
case SDL_BUTTON_LEFT:
#ifndef NDEBUG
std::cout << "LMB Pressed" << std::endl << std::flush;
#endif
lmb_pressed = true;
lmb_click = true;
break;
case SDL_BUTTON_RIGHT:
#ifndef NDEBUG
std::cout << "RMB Pressed" << std::endl << std::flush;
#endif
rmb_pressed = true;
break;
case SDL_BUTTON_MIDDLE:
#ifndef NDEBUG
std::cout << "MWHEEL Pressed" << std::endl << std::flush;
#endif
mwheel_pressed = true;
break;
default:
break;
}
#ifndef NDEBUG
std::cout << "Mousebutton Down: "
<< "(" << event.button.x << ", " << event.button.y << "), "
<< event.button.state << ", "
<< event.button.which << std::endl;
#endif
break;
case SDL_MOUSEBUTTONUP:
switch (event.button.button) {
case SDL_BUTTON_LEFT:
#ifndef NDEBUG
std::cout << "LMB Released" << std::endl << std::flush;
#endif
lmb_pressed = false;
break;
case SDL_BUTTON_RIGHT:
#ifndef NDEBUG
std::cout << "RMB Released" << std::endl << std::flush;
#endif
rmb_pressed = false;
break;
case SDL_BUTTON_MIDDLE:
#ifndef NDEBUG
std::cout << "MWHEEL Released" << std::endl << std::flush;
#endif
mwheel_pressed = false;
break;
default:
break;
}
#ifndef NDEBUG
std::cout << "Mousebutton Up: "
<< "(" << event.button.x << ", " << event.button.y << "), "
<< event.button.state << ", "
<< event.button.which << std::endl;
#endif
break;
case SDL_MOUSEWHEEL:
#ifndef NDEBUG
std::cout << "Mousewheel Scrolled: "
<< event.wheel.y << std::endl;
#endif
mwheelmot_y = static_cast<float>(event.wheel.y);
break;
case SDL_KEYDOWN:
#ifndef NDEBUG
std::cout << "Keypress: "
<< static_cast<int>(event.key.keysym.sym) << std::endl;
#endif
if (event.key.keysym.sym == SDLK_ESCAPE) {
if (event.key.type == SDL_KEYDOWN) {
esc_click = true;
}
}
break;
default:
break;
}
}
}
......@@ -4,6 +4,7 @@
#include <camera_arcball.hpp>
#include <layer.hpp>
#include <imgui_bundle.hpp>
#include <input_state.hpp>
#include <screendefs.hpp>
......@@ -25,7 +26,7 @@ int main(int argc, char** argv) {
glm::vec3 lookAt(0., 0., 0.);
glm::vec3 eye(0., -0.5, 50.);
glm::vec3 eye(0., -0.5, 90.);
glm::vec3 upVector(0., 0., 1.);
Camera camera (eye, lookAt, upVector);
......@@ -47,23 +48,34 @@ int main(int argc, char** argv) {
size_t count = 0;
auto start = std::chrono::high_resolution_clock::now();
while (true) {
while (!InputState::should_quit) {
++count; //number of frames processed
InputState::update(WIDTH, HEIGHT);
if (InputState::rmb_pressed) {
//panning
}
else if (InputState::esc_click) {
//reset view and selection
}
else if (InputState::lmb_click) {
//select object
}
/*New Gui Frame*/
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplSDL2_NewFrame(infra.window());
ImGui::NewFrame();
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClearColor(78./255, 97./255., 117./255., 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/*Update projection, view, model*/
/*Draw here*/
int drawing_mode = 1;
int drawing_mode = 0;
int index_selected = -1;
bottom_metal.draw(projection, view, model, drawing_mode, index_selected);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment