Skip to content

Commit

Permalink
Improved input class
Browse files Browse the repository at this point in the history
  • Loading branch information
L4ZZA committed Aug 15, 2020
1 parent 560b38c commit d2a593f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 52 deletions.
18 changes: 8 additions & 10 deletions pyro/src/platform/windows/win_input.cpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
#include "pyro_pch.h"
#include "win_input.h"
#include "pyro/core/input.h"
#include "pyro/core/application.h"
#include "GLFW/glfw3.h"

namespace pyro
{
// some how we'll have to delete this?
input* input::s_instance = new win_input();

static bool s_first_mouse = true;
static std::pair<float, float> s_last_position;
static bool s_first_mouse = true;
}

bool pyro::win_input::key_pressed_impl(int32_t key_code) const
bool pyro::input::key_pressed(int32_t key_code)
{
auto window = application::window().native_window();
auto state = glfwGetKey(static_cast<GLFWwindow*>(window), key_code);
return state == GLFW_PRESS || state == GLFW_REPEAT;
}

bool pyro::win_input::mouse_button_pressed_impl(int32_t button) const
bool pyro::input::mouse_button_pressed(int32_t button)
{
auto window = application::window().native_window();
auto state = glfwGetMouseButton(static_cast<GLFWwindow*>(window), button);
return state == GLFW_PRESS;
}

std::pair<float, float> pyro::win_input::mouse_position_impl() const
std::pair<float, float> pyro::input::mouse_position()
{
auto& our_window = application::window();
auto window = our_window.native_window();
Expand Down Expand Up @@ -54,7 +52,7 @@ std::pair<float, float> pyro::win_input::mouse_position_impl() const
return { static_cast<float>(x_pos), static_cast<float>(y_pos) };
}

float pyro::win_input::mouse_x_impl() const
float pyro::input::mouse_x()
{
// c++ 17 way to assign std::pair(s)
auto window = application::window().native_window();
Expand All @@ -63,7 +61,7 @@ float pyro::win_input::mouse_x_impl() const
return static_cast<float>(x_pos);
}

float pyro::win_input::mouse_y_impl() const
float pyro::input::mouse_y()
{
auto window = application::window().native_window();
double x_pos, y_pos;
Expand Down
21 changes: 0 additions & 21 deletions pyro/src/platform/windows/win_input.h

This file was deleted.

26 changes: 5 additions & 21 deletions pyro/src/pyro/core/input.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#pragma once

#include "pyro/core/core.h"

namespace pyro
Expand All @@ -8,33 +7,18 @@ namespace pyro
/// input polling allows to retrieve input states at any time.
class PYRO_API input
{
protected:
input() = default;
public:
input(const input&) = delete;
input& operator=(const input&) = delete;

/// Returns if the specified key is being pressed.
static bool key_pressed(int p_key_code) { return s_instance->key_pressed_impl(p_key_code); }
static bool key_pressed(int p_key_code);

/// Returns if the specified mouse button is being pressed.
static bool mouse_button_pressed(int p_button) { return s_instance->mouse_button_pressed_impl(p_button); }
static bool mouse_button_pressed(int p_button);

/// Returns the mouse position as a pair of float values.
static std::pair<float, float> mouse_position() { return s_instance->mouse_position_impl(); }
static std::pair<float, float> mouse_position();
/// Returns the x coordinate of the mouse position.
static float mouse_x() { return s_instance->mouse_x_impl(); }
static float mouse_x();
/// Returns the y coordinate of the mouse position.
static float mouse_y() { return s_instance->mouse_y_impl(); }

protected:
virtual bool key_pressed_impl(int key_code) const = 0;
virtual bool mouse_button_pressed_impl(int key_code) const = 0;
virtual std::pair<float, float> mouse_position_impl() const = 0;
virtual float mouse_x_impl() const = 0;
virtual float mouse_y_impl() const = 0;

private:
static input* s_instance;
static float mouse_y();
};
}

0 comments on commit d2a593f

Please sign in to comment.