Skip to content

Commit

Permalink
Early input polling implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
timmypidashev committed Feb 25, 2022
1 parent 7d5383c commit 3d8650d
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Core/Application/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ namespace Iridescent {
void Application::Run()
{
while (m_Running)
{
{
for (Layer* layer : m_LayerStack)
layer->OnUpdate();

m_Window->OnUpdate();
}
}
}

bool Application::OnWindowClose(WindowCloseEvent& e)
Expand Down
7 changes: 6 additions & 1 deletion Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ set(SOURCE_FILES
# Predefines
Exports.h

# Input
Input.h

# Application sources
Application/Application.cpp
Application/Application.h
Expand All @@ -37,8 +40,10 @@ set(SOURCE_FILES

# Window
Window/Windows.cpp
Window/WindowsInput.cpp
Window/WindowsInput.h
Window/Windows.h
Window.h
Window.h

# Layerstack
Layer/Layer.cpp
Expand Down
27 changes: 27 additions & 0 deletions Core/Input.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include "Exports.h"

namespace Iridescent {

class IRID_EXPORT Input
{
public:
inline static bool IsKeyPressed(int keycode) { return s_Instance->IsKeyPressedImpl(keycode); }

inline static bool IsMouseButtonPressed(int button) { return s_Instance->IsMouseButtonPressedImpl(button); }
inline static std::pair<float, float> GetMousePosition() { return s_Instance->GetMousePositionImpl(); }
inline static float GetMouseX() { return s_Instance->GetMouseXImpl(); }
inline static float GetMouseY() { return s_Instance->GetMouseYImpl(); }
protected:
virtual bool IsKeyPressedImpl(int keycode) = 0;

virtual bool IsMouseButtonPressedImpl(int button) = 0;
virtual std::pair<float, float> GetMousePositionImpl() = 0;
virtual float GetMouseXImpl() = 0;
virtual float GetMouseYImpl() = 0;
private:
static Input* s_Instance;
};

}
4 changes: 3 additions & 1 deletion Core/Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ namespace Iridescent {
virtual void SetVSync(bool enabled) = 0;
virtual bool IsVSync() const = 0;

virtual void* GetNativeWindow() const = 0;

static Window* Create(const WindowProps& props = WindowProps());
};

}
}
4 changes: 3 additions & 1 deletion Core/Window/Windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ namespace Iridescent {
inline void SetEventCallback(const EventCallbackFn& callback) override { m_Data.EventCallback = callback; }
void SetVSync(bool enabled) override;
bool IsVSync() const override;

inline virtual void* GetNativeWindow() const { return m_Window; }
private:
virtual void Init(const WindowProps& props);
virtual void Shutdown();
Expand All @@ -39,4 +41,4 @@ namespace Iridescent {
WindowData m_Data;
};

}
}
46 changes: 46 additions & 0 deletions Core/Window/WindowsInput.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "Iridpch.h"
#include "WindowsInput.h"

#include "Application/Application.h"
#include <GLFW/glfw3.h>

namespace Iridescent {

Input* Input::s_Instance = new WindowsInput();

bool WindowsInput::IsKeyPressedImpl(int keycode)
{
auto window = static_cast<GLFWwindow*>(Application::Get().GetWindow().GetNativeWindow());
auto state = glfwGetKey(window, keycode);
return state == GLFW_PRESS || state == GLFW_REPEAT;
}

bool WindowsInput::IsMouseButtonPressedImpl(int button)
{
auto window = static_cast<GLFWwindow*>(Application::Get().GetWindow().GetNativeWindow());
auto state = glfwGetMouseButton(window, button);
return state == GLFW_PRESS;
}

std::pair<float, float> WindowsInput::GetMousePositionImpl()
{
auto window = static_cast<GLFWwindow*>(Application::Get().GetWindow().GetNativeWindow());
double xpos, ypos;
glfwGetCursorPos(window, &xpos, &ypos);

return { (float)xpos, (float)ypos };
}

float WindowsInput::GetMouseXImpl()
{
auto[x, y] = GetMousePositionImpl();
return x;
}

float WindowsInput::GetMouseYImpl()
{
auto[x, y] = GetMousePositionImpl();
return y;
}

}
18 changes: 18 additions & 0 deletions Core/Window/WindowsInput.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include "Input.h"

namespace Iridescent {

class WindowsInput : public Input
{
protected:
virtual bool IsKeyPressedImpl(int keycode) override;

virtual bool IsMouseButtonPressedImpl(int button) override;
virtual std::pair<float, float> GetMousePositionImpl() override;
virtual float GetMouseXImpl() override;
virtual float GetMouseYImpl() override;
};

}
97 changes: 95 additions & 2 deletions imgui.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,100 @@ Size=400,400
Collapsed=0

[Window][Dear ImGui Demo]
Pos=575,1
Size=550,680
Pos=192,106
Size=978,1021
Collapsed=0

[Window][Example: Simple layout]
Pos=582,300
Size=500,489
Collapsed=0

[Table][0xC9935533,3]
Column 0 Weight=1.0000
Column 1 Weight=1.0000
Column 2 Weight=1.0000

[Table][0x64418101,3]
RefScale=13
Column 0 Width=0
Column 1 Width=0
Column 2 Width=0

[Table][0x47600645,3]
RefScale=13
Column 0 Width=0
Column 1 Width=0
Column 2 Weight=1.0000

[Table][0xDE6957FF,6]
RefScale=13
Column 0 Width=0
Column 1 Width=0
Column 2 Width=-1
Column 3 Weight=1.0000
Column 4 Weight=1.0000
Column 5 Weight=-1.0000

[Table][0x861D378E,3]
Column 0 Weight=1.0000
Column 1 Weight=1.0000
Column 2 Weight=1.0000

[Table][0x9B8E7538,3]
Column 0 Weight=1.0000
Column 1 Weight=1.0000
Column 2 Weight=1.0000

[Table][0xE0773582,3]
Column 0 Weight=1.0000
Column 1 Weight=1.0000
Column 2 Weight=1.0000

[Table][0xC179E37C,3]
RefScale=13
Column 0 Width=0 Sort=0v
Column 1 Width=0
Column 2 Width=-1

[Table][0x8DFA6E86,2]
Column 0 Weight=1.0000
Column 1 Weight=1.0000

[Table][0xFABAAEF7,2]
Column 0 Weight=1.0000
Column 1 Weight=1.0000

[Table][0xA43C3885,3]
RefScale=13
Column 0 Width=0
Column 1 Width=0
Column 2 Width=0

[Table][0x49F8DCEA,3]
RefScale=13
Column 0 Weight=1.0000
Column 1 Width=84
Column 2 Width=126

[Table][0x82CBB907,3]
Column 0 Weight=1.0000
Column 1 Weight=1.0000
Column 2 Weight=1.0000

[Table][0x20DF7A63,4]
RefScale=13
Column 0 Width=0 Sort=0v
Column 1 Width=0
Column 2 Width=0
Column 3 Weight=1.0000

[Table][0xDA36A7E0,6]
RefScale=13
Column 0 Width=0 Sort=0v
Column 1 Width=0
Column 2 Width=0
Column 3 Width=0
Column 4 Weight=1.0000
Column 5 Width=-1

0 comments on commit 3d8650d

Please sign in to comment.