first commit

This commit is contained in:
sam 2024-11-23 00:54:53 +13:00
commit 584fb4d5c4
15 changed files with 324 additions and 0 deletions

25
.clang-format Normal file
View file

@ -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

7
.gitignore vendored Normal file
View file

@ -0,0 +1,7 @@
# Xmake cache
.xmake/
build/
vsxmake2022/
# MacOS Cache
.DS_Store

25
src/Backend.cpp Normal file
View file

@ -0,0 +1,25 @@
#include <pch.h>
std::shared_ptr<Window> Backend::window;
void Backend::ReplaceWindow(const char* title, int width, int height) {
window = std::make_shared<Window>(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<Window>& Backend::GetWindow() {
return window;
}

11
src/Backend.h Normal file
View file

@ -0,0 +1,11 @@
#pragma once
#include <pch.h>
namespace Backend {
extern std::shared_ptr<Window> 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<Window>& GetWindow();
}

73
src/Callbacks.cpp Normal file
View file

@ -0,0 +1,73 @@
#include <pch.h>
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;
}

7
src/Callbacks.h Normal file
View file

@ -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);
}

5
src/Engine.cpp Normal file
View file

@ -0,0 +1,5 @@
#include <pch.h>
void Engine::Init() {
Backend::Init();
}

5
src/Engine.h Normal file
View file

@ -0,0 +1,5 @@
#pragma once
namespace Engine {
void Init();
}

47
src/Input.cpp Normal file
View file

@ -0,0 +1,47 @@
#include <pch.h>
std::unordered_set<int> Input::pressed_keys;
std::unordered_set<int> Input::released_keys;
const std::shared_ptr<Window>& 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;
}
}

14
src/Input.h Normal file
View file

@ -0,0 +1,14 @@
#pragma once
#include <pch.h>
namespace Input {
extern std::unordered_set<int> pressed_keys;
extern std::unordered_set<int> released_keys;
extern const std::shared_ptr<Window>& window;
bool IsKeyDown(int key);
bool IsKeyUp(int key);
bool IsKeyPressed(int key);
bool IsKeyReleased(int key);
}

31
src/Window.cpp Normal file
View file

@ -0,0 +1,31 @@
#include <pch.h>
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);
}

17
src/Window.h Normal file
View file

@ -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;
};

25
src/main.cpp Normal file
View file

@ -0,0 +1,25 @@
#include <pch.h>
int main(int argc, char** argv) {
Engine::Init();
const std::shared_ptr<Window>& 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;
}

14
src/pch.h Normal file
View file

@ -0,0 +1,14 @@
#include <format>
#include <iostream>
#include <memory>
#include <unordered_set>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <Backend.h>
#include <Callbacks.h>
#include <Engine.h>
#include <Input.h>
#include <Window.h>

18
xmake.lua Normal file
View file

@ -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")