From 584fb4d5c419479ef59bdc81e7f2941e163f3c65 Mon Sep 17 00:00:00 2001 From: sam Date: Sat, 23 Nov 2024 00:54:53 +1300 Subject: [PATCH] first commit --- .clang-format | 25 ++++++++++++++++ .gitignore | 7 +++++ src/Backend.cpp | 25 ++++++++++++++++ src/Backend.h | 11 +++++++ src/Callbacks.cpp | 73 +++++++++++++++++++++++++++++++++++++++++++++++ src/Callbacks.h | 7 +++++ src/Engine.cpp | 5 ++++ src/Engine.h | 5 ++++ src/Input.cpp | 47 ++++++++++++++++++++++++++++++ src/Input.h | 14 +++++++++ src/Window.cpp | 31 ++++++++++++++++++++ src/Window.h | 17 +++++++++++ src/main.cpp | 25 ++++++++++++++++ src/pch.h | 14 +++++++++ xmake.lua | 18 ++++++++++++ 15 files changed, 324 insertions(+) create mode 100644 .clang-format create mode 100644 .gitignore create mode 100644 src/Backend.cpp create mode 100644 src/Backend.h create mode 100644 src/Callbacks.cpp create mode 100644 src/Callbacks.h create mode 100644 src/Engine.cpp create mode 100644 src/Engine.h create mode 100644 src/Input.cpp create mode 100644 src/Input.h create mode 100644 src/Window.cpp create mode 100644 src/Window.h create mode 100644 src/main.cpp create mode 100644 src/pch.h create mode 100644 xmake.lua diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..c17e64a --- /dev/null +++ b/.clang-format @@ -0,0 +1,25 @@ +BasedOnStyle: WebKit +IndentWidth: 4 +TabWidth: 4 +UseTab: Never +AlignConsecutiveDeclarations: false +AlignConsecutiveAssignments: false +AlignTrailingComments: true +ColumnLimit: 105 +BreakBeforeBraces: Attach +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +AllowShortFunctionsOnASingleLine: false +AllowShortLambdasOnASingleLine: false +PointerAlignment: Left +SpaceBeforeParens: Never +SpacesInParentheses: false +SpacesInConditionalStatement: false +SpacesInContainerLiterals: false +SpaceAfterCStyleCast: false +SpaceBeforeCpp11BracedList: false +SpaceBeforeSquareBrackets: false +SpacesBeforeTrailingComments: 2 +PenaltyBreakAssignment: 1000 +NamespaceIndentation: All + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..88b3a52 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +# Xmake cache +.xmake/ +build/ +vsxmake2022/ + +# MacOS Cache +.DS_Store diff --git a/src/Backend.cpp b/src/Backend.cpp new file mode 100644 index 0000000..c52c5e0 --- /dev/null +++ b/src/Backend.cpp @@ -0,0 +1,25 @@ +#include + +std::shared_ptr Backend::window; + +void Backend::ReplaceWindow(const char* title, int width, int height) { + window = std::make_shared(title, width, height); +} + +void Backend::Init(const char* title, int width, int height) { + glfwSetErrorCallback(Callbacks::GLFWError); + glfwInit(); + + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + + ReplaceWindow(title, width, height); + + gladLoadGLLoader((GLADloadproc)glfwGetProcAddress); + glDebugMessageCallback(Callbacks::OpenGLMessage, nullptr); +} + +const std::shared_ptr& Backend::GetWindow() { + return window; +} diff --git a/src/Backend.h b/src/Backend.h new file mode 100644 index 0000000..f6e7192 --- /dev/null +++ b/src/Backend.h @@ -0,0 +1,11 @@ +#pragma once + +#include + +namespace Backend { + extern std::shared_ptr window; + + void ReplaceWindow(const char* title, int width, int height); + void Init(const char* title = "Lodestone", int width = 800, int height = 600); + const std::shared_ptr& GetWindow(); +} diff --git a/src/Callbacks.cpp b/src/Callbacks.cpp new file mode 100644 index 0000000..32819f3 --- /dev/null +++ b/src/Callbacks.cpp @@ -0,0 +1,73 @@ +#include + +void Callbacks::GLFWError(int code, const char* message) { + std::cout << std::format("GLFW Error: {}", message) << std::endl; +} + +void Callbacks::OpenGLMessage(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, + GLchar const* message, void const* user_param) { + const char* source_string; + switch(source) { + case GL_DEBUG_SOURCE_API: + source_string = "API"; + break; + case GL_DEBUG_SOURCE_WINDOW_SYSTEM: + source_string = "WINDOW SYSTEM"; + break; + case GL_DEBUG_SOURCE_SHADER_COMPILER: + source_string = "SHADER COMPILER"; + break; + case GL_DEBUG_SOURCE_THIRD_PARTY: + source_string = "THIRD PARTY"; + break; + case GL_DEBUG_SOURCE_APPLICATION: + source_string = "APPLICATION"; + break; + case GL_DEBUG_SOURCE_OTHER: + source_string = "OTHER"; + break; + } + + const char* type_string; + switch(type) { + case GL_DEBUG_TYPE_ERROR: + type_string = "ERROR"; + break; + case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: + type_string = "DEPRECATED_BEHAVIOR"; + break; + case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR: + type_string = "UNDEFINED_BEHAVIOR"; + break; + case GL_DEBUG_TYPE_PORTABILITY: + type_string = "PORTABILITY"; + break; + case GL_DEBUG_TYPE_PERFORMANCE: + type_string = "PERFORMANCE"; + break; + case GL_DEBUG_TYPE_MARKER: + type_string = "MARKER"; + break; + case GL_DEBUG_TYPE_OTHER: + type_string = "OTHER"; + break; + } + + const char* severity_string; + switch(severity) { + case GL_DEBUG_SEVERITY_NOTIFICATION: + severity_string = "NOTIFICATION"; + break; + case GL_DEBUG_SEVERITY_LOW: + severity_string = "LOW"; + break; + case GL_DEBUG_SEVERITY_MEDIUM: + severity_string = "MEDIUM"; + break; + case GL_DEBUG_SEVERITY_HIGH: + severity_string = "HIGH"; + break; + } + + std::cout << std::format("OpenGL: {}", type_string) << std::endl; +} diff --git a/src/Callbacks.h b/src/Callbacks.h new file mode 100644 index 0000000..5e8cf69 --- /dev/null +++ b/src/Callbacks.h @@ -0,0 +1,7 @@ +#pragma once + +namespace Callbacks { + void GLFWError(int code, const char* message); + void OpenGLMessage(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, + GLchar const* message, void const* user_param); +} diff --git a/src/Engine.cpp b/src/Engine.cpp new file mode 100644 index 0000000..06b58ce --- /dev/null +++ b/src/Engine.cpp @@ -0,0 +1,5 @@ +#include + +void Engine::Init() { + Backend::Init(); +} diff --git a/src/Engine.h b/src/Engine.h new file mode 100644 index 0000000..892408d --- /dev/null +++ b/src/Engine.h @@ -0,0 +1,5 @@ +#pragma once + +namespace Engine { + void Init(); +} diff --git a/src/Input.cpp b/src/Input.cpp new file mode 100644 index 0000000..b5d4600 --- /dev/null +++ b/src/Input.cpp @@ -0,0 +1,47 @@ +#include + +std::unordered_set Input::pressed_keys; +std::unordered_set Input::released_keys; +const std::shared_ptr& Input::window = Backend::GetWindow(); + +bool Input::IsKeyDown(int key) { + return window->IsKeyDown(key); +} + +bool Input::IsKeyUp(int key) { + return window->IsKeyUp(key); +} + +bool Input::IsKeyPressed(int key) { + if(IsKeyUp(key)) { + if(pressed_keys.contains(key)) { + pressed_keys.erase(key); + } + + return false; + } + + if(pressed_keys.contains(key)) { + return false; + } else { + pressed_keys.insert(key); + return true; + } +} + +bool Input::IsKeyReleased(int key) { + if(IsKeyDown(key)) { + if(!released_keys.contains(key)) { + released_keys.insert(key); + } + + return false; + } + + if(!released_keys.contains(key)) { + return false; + } else { + released_keys.erase(key); + return true; + } +} diff --git a/src/Input.h b/src/Input.h new file mode 100644 index 0000000..5e3b9fa --- /dev/null +++ b/src/Input.h @@ -0,0 +1,14 @@ +#pragma once + +#include + +namespace Input { + extern std::unordered_set pressed_keys; + extern std::unordered_set released_keys; + extern const std::shared_ptr& window; + + bool IsKeyDown(int key); + bool IsKeyUp(int key); + bool IsKeyPressed(int key); + bool IsKeyReleased(int key); +} diff --git a/src/Window.cpp b/src/Window.cpp new file mode 100644 index 0000000..4d88a24 --- /dev/null +++ b/src/Window.cpp @@ -0,0 +1,31 @@ +#include + +Window::Window(const char* title, int width, int height) { + window = glfwCreateWindow(width, height, title, nullptr, nullptr); + glfwMakeContextCurrent(window); +} + +Window::~Window() { + std::cout << "freeing window" << std::endl; + glfwDestroyWindow(window); +} + +bool Window::ShouldClose() { + return glfwWindowShouldClose(window); +} + +void Window::SwapBuffers() { + glfwSwapBuffers(window); +} + +bool Window::IsKeyDown(int key) { + return glfwGetKey(window, key) == GLFW_PRESS; +} + +bool Window::IsKeyUp(int key) { + return glfwGetKey(window, key) == GLFW_RELEASE; +} + +void Window::SetVSync(bool enabled) { + glfwSwapInterval(enabled); +} diff --git a/src/Window.h b/src/Window.h new file mode 100644 index 0000000..30aaa12 --- /dev/null +++ b/src/Window.h @@ -0,0 +1,17 @@ +#pragma once + +class Window { +public: + Window() = default; + Window(const char* title, int width, int height); + ~Window(); + + bool ShouldClose(); + void SwapBuffers(); + bool IsKeyDown(int key); + bool IsKeyUp(int key); + void SetVSync(bool enabled); + +private: + GLFWwindow* window; +}; diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..80d1d96 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,25 @@ +#include + +int main(int argc, char** argv) { + Engine::Init(); + const std::shared_ptr& window = Backend::GetWindow(); + window->SetVSync(true); + while(!window->ShouldClose()) { + glfwPollEvents(); + + if(Input::IsKeyPressed(GLFW_KEY_W)) { + std::cout << "pressed w" << std::endl; + } + + if(Input::IsKeyReleased(GLFW_KEY_W)) { + std::cout << "released w" << std::endl; + } + + glClearColor(0.2f, 0.4f, 0.3f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); + + window->SwapBuffers(); + } + + return 0; +} diff --git a/src/pch.h b/src/pch.h new file mode 100644 index 0000000..dbfc971 --- /dev/null +++ b/src/pch.h @@ -0,0 +1,14 @@ +#include +#include +#include +#include + +#include + +#include + +#include +#include +#include +#include +#include diff --git a/xmake.lua b/xmake.lua new file mode 100644 index 0000000..02809cf --- /dev/null +++ b/xmake.lua @@ -0,0 +1,18 @@ +add_rules("mode.debug", "mode.release") + +if is_mode("debug") then + set_optimize("none") +end + +add_requires("glfw") +add_requires("glad", { configs = { static = true, api = "gl=4.6", profile = "core" } }) + +target("lodestone") + set_kind("binary") + set_rundir(".") + set_languages("c++20") + set_pcxxheader("src/pch.h") + add_files("src/*.cpp") + add_headerfiles("src/*.h") + add_includedirs("src") + add_packages("glfw", "glad")