Skip to content

Commit

Permalink
Window events
Browse files Browse the repository at this point in the history
  • Loading branch information
TerraCrafterE3 committed Jul 29, 2023
1 parent a1d6322 commit 63abb26
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 7 deletions.
7 changes: 6 additions & 1 deletion TitanEngine/Engine.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@
<AdditionalIncludeDirectories>src;vendor\spdlog\include;vendor\GLFW\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<DebugInformationFormat>EditAndContinue</DebugInformationFormat>
<Optimization>Disabled</Optimization>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<AdditionalOptions>/MD /MDd %(AdditionalOptions)</AdditionalOptions>
<LanguageStandard>stdcpp17</LanguageStandard>
<ExternalWarningLevel>Level3</ExternalWarningLevel>
</ClCompile>
Expand All @@ -106,7 +108,8 @@
<IntrinsicFunctions>true</IntrinsicFunctions>
<MinimalRebuild>false</MinimalRebuild>
<StringPooling>true</StringPooling>
<AdditionalOptions>/MT %(AdditionalOptions)</AdditionalOptions>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<AdditionalOptions>/MD /MT %(AdditionalOptions)</AdditionalOptions>
<LanguageStandard>stdcpp17</LanguageStandard>
<ExternalWarningLevel>Level3</ExternalWarningLevel>
</ClCompile>
Expand All @@ -132,6 +135,8 @@
<IntrinsicFunctions>true</IntrinsicFunctions>
<MinimalRebuild>false</MinimalRebuild>
<StringPooling>true</StringPooling>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<AdditionalOptions>/MD %(AdditionalOptions)</AdditionalOptions>
<LanguageStandard>stdcpp17</LanguageStandard>
<ExternalWarningLevel>Level3</ExternalWarningLevel>
</ClCompile>
Expand Down
87 changes: 86 additions & 1 deletion TitanEngine/src/Platform/Windows/WindowsWindow.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
#include "tipch.h"

#include "WindowsWindow.h"
#include <Titan/Events/MouseEvent.h>
#include <Titan/Events/KeyEvent.h>
#include <Titan/Events/ApplicationEvent.h>

namespace Titan {
static bool s_GLFWInitialized = false;

static void GLFWErrorCallback(int error, const char* description) {
TITAN_CORE_ERROR("GLFWError ({0}):\n{1}", error, description);
}

Window* Window::Create(const WindowProps& props)
{
return new WindowsWindow(props);
Expand Down Expand Up @@ -33,14 +40,92 @@ namespace Titan {
// TODO: glfwTerminate on system shutdown
int success = glfwInit();
TI_CORE_ASSERT(success, "Could not intialize GLFW!");

glfwSetErrorCallback(GLFWErrorCallback);
s_GLFWInitialized = true;
}

m_Window = glfwCreateWindow((int)props.Width, (int)props.Height, m_Data.Title.c_str(), nullptr, nullptr);
glfwMakeContextCurrent(m_Window);
glfwSetWindowUserPointer(m_Window, &m_Data);
SetVSync(true);

//Set GLFW callbacks
glfwSetWindowSizeCallback(m_Window, [](GLFWwindow* window, int width, int height)
{
WindowData& data = *(WindowData*) glfwGetWindowUserPointer(window);
data.Width = width;
data.Height = height;

WindowResizeEvent event(width, height);
data.EventCallback(event);
});

glfwSetWindowCloseCallback(m_Window, [](GLFWwindow* window)
{
WindowData& data = *(WindowData*)glfwGetWindowUserPointer(window);
WindowCloseEvent event;
data.EventCallback(event);
});

glfwSetKeyCallback(m_Window, [](GLFWwindow* window, int key, int scancode, int action, int modes) {
WindowData& data = *(WindowData*)glfwGetWindowUserPointer(window);
switch (action)
{
case GLFW_PRESS:
{
KeyPressedEvent event(key, 0);
data.EventCallback(event);
break;
}
case GLFW_RELEASE:
{
KeyReleasedEvent event(key);
data.EventCallback(event);
break;
}
case GLFW_REPEAT:
{
KeyPressedEvent event(key, 1);
data.EventCallback(event);
break;
}
}
});

glfwSetMouseButtonCallback(m_Window, [](GLFWwindow* window, int button, int action, int modes)
{
WindowData& data = *(WindowData*)glfwGetWindowUserPointer(window);

switch (action)
{
case GLFW_PRESS:
{
MouseButtonPressedEvent event(button);
data.EventCallback(event);
break;
}
case GLFW_RELEASE:
{
MouseButtonReleasedEvent event(button);
data.EventCallback(event);
break;
}
}
});

glfwSetScrollCallback(m_Window, [](GLFWwindow* window, double xOffset, double yOffset) {
WindowData& data = *(WindowData*)glfwGetWindowUserPointer(window);

MouseScrolledEvent event((float)xOffset, (float)yOffset);
data.EventCallback(event);
});

glfwSetCursorPosCallback(m_Window, [](GLFWwindow* window, double xPos, double yPos) {
WindowData& data = *(WindowData*)glfwGetWindowUserPointer(window);

MouseMovedEvent event((float)xPos, (float)yPos);
data.EventCallback(event);
});
}

void WindowsWindow::Shutdown()
Expand Down
19 changes: 18 additions & 1 deletion TitanEngine/src/Titan/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@

namespace Titan {

#define BIND_EVENT_FN(x) std::bind(&x, this, std::placeholders::_1)

Application::Application()
{
m_Window = std::unique_ptr<Window>(Window::Create());
m_Window->SetEventCallback(BIND_EVENT_FN(Application::OnEvent));
}

Application::~Application()
Expand All @@ -22,10 +25,24 @@ namespace Titan {
{
while (m_Running)
{
glClearColor(1, 0, 1, 1);
glClearColor(0, 0, 1, 1);
glClear(GL_COLOR_BUFFER_BIT);
m_Window->OnUpdate();
}
}

void Application::OnEvent(Event& e)
{
EventDispatcher dispatcher(e);
dispatcher.Dispatch<WindowCloseEvent>(BIND_EVENT_FN(Application::OnWindowClosed));

TITAN_CORE_TRACE("{0}", e.ToString());
}

bool Application::OnWindowClosed(WindowCloseEvent& e)
{
m_Running = false;
return true;
}

}
5 changes: 5 additions & 0 deletions TitanEngine/src/Titan/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Core.h"
#include "Titan/Log.h"
#include "Window.h"
#include "Events/ApplicationEvent.h"


namespace Titan {
Expand All @@ -13,7 +14,11 @@ namespace Titan {
virtual ~Application();

void Run();

void OnEvent(Event& e);
private:
bool OnWindowClosed(WindowCloseEvent& e);

std::unique_ptr<Window> m_Window;
bool m_Running = true;
};
Expand Down
Binary file modified bin/Debug-windows-x86_64/Engine/Engine.dll
Binary file not shown.
Binary file modified bin/Debug-windows-x86_64/Engine/Engine.exp
Binary file not shown.
Binary file modified bin/Debug-windows-x86_64/Engine/Engine.lib
Binary file not shown.
Binary file modified bin/Debug-windows-x86_64/Engine/Engine.pdb
Binary file not shown.
Binary file modified bin/Debug-windows-x86_64/Sandbox/Engine.dll
Binary file not shown.
Binary file modified bin/Debug-windows-x86_64/Sandbox/Sandbox.exe
Binary file not shown.
Binary file modified bin/Debug-windows-x86_64/Sandbox/Sandbox.pdb
Binary file not shown.
6 changes: 2 additions & 4 deletions premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ project "Engine"
"opengl32.lib"
}

staticruntime "on"
runtime "Release"
buildoptions "/MD"

filter "system:windows"
cppdialect "C++17"
Expand All @@ -70,8 +69,7 @@ project "Engine"
filter "configurations:Debug"
defines "TI_DEBUG"
symbols "On"
staticruntime "on"
runtime "Debug"
buildoptions "/MDd"

filter "configurations:Release"
defines "TI_RELEASE"
Expand Down

0 comments on commit 63abb26

Please sign in to comment.