Skip to content

Commit 7594dd2

Browse files
committed
Initial commit with example code
1 parent 7f49e43 commit 7594dd2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+15572
-1
lines changed

.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Directories
2+
.vs/
3+
/bin/
4+
bin-int/
5+
6+
# Files
7+
*.user

.gitmodules

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[submodule "OpenGL-Core/vendor/glfw"]
2+
path = OpenGL-Core/vendor/glfw
3+
url = https://github.com/TheCherno/glfw
4+
[submodule "OpenGL-Core/vendor/spdlog"]
5+
path = OpenGL-Core/vendor/spdlog
6+
url = https://github.com/gabime/spdlog
7+
[submodule "OpenGL-Core/vendor/glm"]
8+
path = OpenGL-Core/vendor/glm
9+
url = https://github.com/g-truc/glm
10+
[submodule "OpenGL-Core/vendor/imgui"]
11+
path = OpenGL-Core/vendor/imgui
12+
url = https://github.com/TheCherno/imgui

OpenGL-Core/src/GLCore.h

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
3+
// Main header file - include into application for complete access
4+
5+
#include <glad/glad.h>
6+
#include <glm/glm.hpp>
7+
#include <glm/gtc/type_ptr.hpp>
8+
#include <glm/gtc/matrix_transform.hpp>
9+
#include <imgui.h>
10+
11+
#include "GLCore/Core/Application.h"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include "glpch.h"
2+
#include "Application.h"
3+
4+
#include "Log.h"
5+
6+
#include "Input.h"
7+
8+
#include <glfw/glfw3.h>
9+
10+
namespace GLCore {
11+
12+
#define BIND_EVENT_FN(x) std::bind(&Application::x, this, std::placeholders::_1)
13+
14+
Application* Application::s_Instance = nullptr;
15+
16+
Application::Application()
17+
{
18+
if (!s_Instance)
19+
{
20+
// Initialize core
21+
Log::Init();
22+
}
23+
24+
GLCORE_ASSERT(!s_Instance, "Application already exists!");
25+
s_Instance = this;
26+
27+
m_Window = std::unique_ptr<Window>(Window::Create());
28+
m_Window->SetEventCallback(BIND_EVENT_FN(OnEvent));
29+
30+
// Renderer::Init();
31+
32+
m_ImGuiLayer = new ImGuiLayer();
33+
PushOverlay(m_ImGuiLayer);
34+
}
35+
36+
void Application::PushLayer(Layer* layer)
37+
{
38+
m_LayerStack.PushLayer(layer);
39+
}
40+
41+
void Application::PushOverlay(Layer* layer)
42+
{
43+
m_LayerStack.PushOverlay(layer);
44+
}
45+
46+
void Application::OnEvent(Event& e)
47+
{
48+
EventDispatcher dispatcher(e);
49+
dispatcher.Dispatch<WindowCloseEvent>(BIND_EVENT_FN(OnWindowClose));
50+
51+
for (auto it = m_LayerStack.end(); it != m_LayerStack.begin(); )
52+
{
53+
(*--it)->OnEvent(e);
54+
if (e.Handled)
55+
break;
56+
}
57+
}
58+
59+
void Application::Run()
60+
{
61+
while (m_Running)
62+
{
63+
float time = (float)glfwGetTime();
64+
Timestep timestep = time - m_LastFrameTime;
65+
m_LastFrameTime = time;
66+
67+
for (Layer* layer : m_LayerStack)
68+
layer->OnUpdate(timestep);
69+
70+
m_ImGuiLayer->Begin();
71+
for (Layer* layer : m_LayerStack)
72+
layer->OnImGuiRender();
73+
m_ImGuiLayer->End();
74+
75+
m_Window->OnUpdate();
76+
}
77+
}
78+
79+
bool Application::OnWindowClose(WindowCloseEvent& e)
80+
{
81+
m_Running = false;
82+
return true;
83+
}
84+
85+
}
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#pragma once
2+
3+
#include "Core.h"
4+
5+
#include "Window.h"
6+
#include "LayerStack.h"
7+
#include "../Events/Event.h"
8+
#include "../Events/ApplicationEvent.h"
9+
10+
#include "Timestep.h"
11+
12+
#include "../ImGui/ImGuiLayer.h"
13+
14+
namespace GLCore {
15+
16+
class Application
17+
{
18+
public:
19+
Application();
20+
virtual ~Application() = default;
21+
22+
void Run();
23+
24+
void OnEvent(Event& e);
25+
26+
void PushLayer(Layer* layer);
27+
void PushOverlay(Layer* layer);
28+
29+
inline Window& GetWindow() { return *m_Window; }
30+
31+
inline static Application& Get() { return *s_Instance; }
32+
private:
33+
bool OnWindowClose(WindowCloseEvent& e);
34+
private:
35+
std::unique_ptr<Window> m_Window;
36+
ImGuiLayer* m_ImGuiLayer;
37+
bool m_Running = true;
38+
LayerStack m_LayerStack;
39+
float m_LastFrameTime = 0.0f;
40+
private:
41+
static Application* s_Instance;
42+
};
43+
44+
}

OpenGL-Core/src/GLCore/Core/Core.h

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Mostly from Hazel
2+
#pragma once
3+
4+
#include <memory>
5+
6+
#ifdef GLCORE_DEBUG
7+
#define GLCORE_ENABLE_ASSERTS
8+
#endif
9+
10+
#ifdef GLCORE_ENABLE_ASSERTS
11+
#define GLCORE_ASSERT(x, ...) { if(!(x)) { LOG_ERROR("Assertion Failed: {0}", __VA_ARGS__); __debugbreak(); } }
12+
#else
13+
#define GLCORE_ASSERT(x, ...)
14+
#endif
15+
16+
#define BIT(x) (1 << x)
17+
18+
#define GLCORE_BIND_EVENT_FN(fn) std::bind(&fn, this, std::placeholders::_1)

OpenGL-Core/src/GLCore/Core/Input.h

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#pragma once
2+
3+
#include "Core.h"
4+
5+
namespace GLCore {
6+
7+
class Input
8+
{
9+
protected:
10+
Input() = default;
11+
public:
12+
Input(const Input&) = delete;
13+
Input& operator=(const Input&) = delete;
14+
15+
inline static bool IsKeyPressed(int keycode) { return s_Instance->IsKeyPressedImpl(keycode); }
16+
17+
inline static bool IsMouseButtonPressed(int button) { return s_Instance->IsMouseButtonPressedImpl(button); }
18+
inline static std::pair<float, float> GetMousePosition() { return s_Instance->GetMousePositionImpl(); }
19+
inline static float GetMouseX() { return s_Instance->GetMouseXImpl(); }
20+
inline static float GetMouseY() { return s_Instance->GetMouseYImpl(); }
21+
protected:
22+
virtual bool IsKeyPressedImpl(int keycode) = 0;
23+
24+
virtual bool IsMouseButtonPressedImpl(int button) = 0;
25+
virtual std::pair<float, float> GetMousePositionImpl() = 0;
26+
virtual float GetMouseXImpl() = 0;
27+
virtual float GetMouseYImpl() = 0;
28+
private:
29+
static Input* s_Instance;
30+
};
31+
32+
}
+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#pragma once
2+
3+
// From glfw3.h
4+
#define HZ_KEY_SPACE 32
5+
#define HZ_KEY_APOSTROPHE 39 /* ' */
6+
#define HZ_KEY_COMMA 44 /* , */
7+
#define HZ_KEY_MINUS 45 /* - */
8+
#define HZ_KEY_PERIOD 46 /* . */
9+
#define HZ_KEY_SLASH 47 /* / */
10+
#define HZ_KEY_0 48
11+
#define HZ_KEY_1 49
12+
#define HZ_KEY_2 50
13+
#define HZ_KEY_3 51
14+
#define HZ_KEY_4 52
15+
#define HZ_KEY_5 53
16+
#define HZ_KEY_6 54
17+
#define HZ_KEY_7 55
18+
#define HZ_KEY_8 56
19+
#define HZ_KEY_9 57
20+
#define HZ_KEY_SEMICOLON 59 /* ; */
21+
#define HZ_KEY_EQUAL 61 /* = */
22+
#define HZ_KEY_A 65
23+
#define HZ_KEY_B 66
24+
#define HZ_KEY_C 67
25+
#define HZ_KEY_D 68
26+
#define HZ_KEY_E 69
27+
#define HZ_KEY_F 70
28+
#define HZ_KEY_G 71
29+
#define HZ_KEY_H 72
30+
#define HZ_KEY_I 73
31+
#define HZ_KEY_J 74
32+
#define HZ_KEY_K 75
33+
#define HZ_KEY_L 76
34+
#define HZ_KEY_M 77
35+
#define HZ_KEY_N 78
36+
#define HZ_KEY_O 79
37+
#define HZ_KEY_P 80
38+
#define HZ_KEY_Q 81
39+
#define HZ_KEY_R 82
40+
#define HZ_KEY_S 83
41+
#define HZ_KEY_T 84
42+
#define HZ_KEY_U 85
43+
#define HZ_KEY_V 86
44+
#define HZ_KEY_W 87
45+
#define HZ_KEY_X 88
46+
#define HZ_KEY_Y 89
47+
#define HZ_KEY_Z 90
48+
#define HZ_KEY_LEFT_BRACKET 91 /* [ */
49+
#define HZ_KEY_BACKSLASH 92 /* \ */
50+
#define HZ_KEY_RIGHT_BRACKET 93 /* ] */
51+
#define HZ_KEY_GRAVE_ACCENT 96 /* ` */
52+
#define HZ_KEY_WORLD_1 161 /* non-US #1 */
53+
#define HZ_KEY_WORLD_2 162 /* non-US #2 */
54+
55+
/* Function keys */
56+
#define HZ_KEY_ESCAPE 256
57+
#define HZ_KEY_ENTER 257
58+
#define HZ_KEY_TAB 258
59+
#define HZ_KEY_BACKSPACE 259
60+
#define HZ_KEY_INSERT 260
61+
#define HZ_KEY_DELETE 261
62+
#define HZ_KEY_RIGHT 262
63+
#define HZ_KEY_LEFT 263
64+
#define HZ_KEY_DOWN 264
65+
#define HZ_KEY_UP 265
66+
#define HZ_KEY_PAGE_UP 266
67+
#define HZ_KEY_PAGE_DOWN 267
68+
#define HZ_KEY_HOME 268
69+
#define HZ_KEY_END 269
70+
#define HZ_KEY_CAPS_LOCK 280
71+
#define HZ_KEY_SCROLL_LOCK 281
72+
#define HZ_KEY_NUM_LOCK 282
73+
#define HZ_KEY_PRINT_SCREEN 283
74+
#define HZ_KEY_PAUSE 284
75+
#define HZ_KEY_F1 290
76+
#define HZ_KEY_F2 291
77+
#define HZ_KEY_F3 292
78+
#define HZ_KEY_F4 293
79+
#define HZ_KEY_F5 294
80+
#define HZ_KEY_F6 295
81+
#define HZ_KEY_F7 296
82+
#define HZ_KEY_F8 297
83+
#define HZ_KEY_F9 298
84+
#define HZ_KEY_F10 299
85+
#define HZ_KEY_F11 300
86+
#define HZ_KEY_F12 301
87+
#define HZ_KEY_F13 302
88+
#define HZ_KEY_F14 303
89+
#define HZ_KEY_F15 304
90+
#define HZ_KEY_F16 305
91+
#define HZ_KEY_F17 306
92+
#define HZ_KEY_F18 307
93+
#define HZ_KEY_F19 308
94+
#define HZ_KEY_F20 309
95+
#define HZ_KEY_F21 310
96+
#define HZ_KEY_F22 311
97+
#define HZ_KEY_F23 312
98+
#define HZ_KEY_F24 313
99+
#define HZ_KEY_F25 314
100+
#define HZ_KEY_KP_0 320
101+
#define HZ_KEY_KP_1 321
102+
#define HZ_KEY_KP_2 322
103+
#define HZ_KEY_KP_3 323
104+
#define HZ_KEY_KP_4 324
105+
#define HZ_KEY_KP_5 325
106+
#define HZ_KEY_KP_6 326
107+
#define HZ_KEY_KP_7 327
108+
#define HZ_KEY_KP_8 328
109+
#define HZ_KEY_KP_9 329
110+
#define HZ_KEY_KP_DECIMAL 330
111+
#define HZ_KEY_KP_DIVIDE 331
112+
#define HZ_KEY_KP_MULTIPLY 332
113+
#define HZ_KEY_KP_SUBTRACT 333
114+
#define HZ_KEY_KP_ADD 334
115+
#define HZ_KEY_KP_ENTER 335
116+
#define HZ_KEY_KP_EQUAL 336
117+
#define HZ_KEY_LEFT_SHIFT 340
118+
#define HZ_KEY_LEFT_CONTROL 341
119+
#define HZ_KEY_LEFT_ALT 342
120+
#define HZ_KEY_LEFT_SUPER 343
121+
#define HZ_KEY_RIGHT_SHIFT 344
122+
#define HZ_KEY_RIGHT_CONTROL 345
123+
#define HZ_KEY_RIGHT_ALT 346
124+
#define HZ_KEY_RIGHT_SUPER 347
125+
#define HZ_KEY_MENU 348

OpenGL-Core/src/GLCore/Core/Layer.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "glpch.h"
2+
#include "Layer.h"
3+
4+
namespace GLCore {
5+
6+
Layer::Layer(const std::string& debugName)
7+
: m_DebugName(debugName)
8+
{
9+
}
10+
11+
}

OpenGL-Core/src/GLCore/Core/Layer.h

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#pragma once
2+
3+
#include "Core.h"
4+
#include "Timestep.h"
5+
#include "../Events/Event.h"
6+
7+
namespace GLCore {
8+
9+
class Layer
10+
{
11+
public:
12+
Layer(const std::string& name = "Layer");
13+
virtual ~Layer() = default;
14+
15+
virtual void OnAttach() {}
16+
virtual void OnDetach() {}
17+
virtual void OnUpdate(Timestep ts) {}
18+
virtual void OnImGuiRender() {}
19+
virtual void OnEvent(Event& event) {}
20+
21+
inline const std::string& GetName() const { return m_DebugName; }
22+
protected:
23+
std::string m_DebugName;
24+
};
25+
26+
}

0 commit comments

Comments
 (0)