diff --git a/CMakeLists.txt b/CMakeLists.txt index 6b965dc..33d0af5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,4 +2,8 @@ cmake_minimum_required(VERSION 3.31) project(Ugly) +find_package(GLEW REQUIRED) +find_package(glfw3 CONFIG REQUIRED) +find_package(glm CONFIG REQUIRED) + add_subdirectory(./source/app) diff --git a/source/app/CMakeLists.txt b/source/app/CMakeLists.txt index e43b8f7..2bedf77 100644 --- a/source/app/CMakeLists.txt +++ b/source/app/CMakeLists.txt @@ -1,3 +1,16 @@ project(UglyMain) -add_executable(${PROJECT_NAME} main.c) +add_executable(${PROJECT_NAME} + main.cpp + src/App.cpp +) + +target_include_directories(${PROJECT_NAME} + PRIVATE ./src +) + +target_link_libraries(${PROJECT_NAME} + GLEW::GLEW + glfw + glm::glm +) diff --git a/source/app/main.c b/source/app/main.c deleted file mode 100644 index f47badb..0000000 --- a/source/app/main.c +++ /dev/null @@ -1,5 +0,0 @@ -#include - -int main(void) { - printf("hello world!\n"); -} diff --git a/source/app/main.cpp b/source/app/main.cpp new file mode 100644 index 0000000..d16406b --- /dev/null +++ b/source/app/main.cpp @@ -0,0 +1,12 @@ +#include +#include +#include + +#include "App.hpp" + + +int main(int argc, char *argv[]) { + auto args = std::vector{}; + std::copy_n(argv, argc, std::back_inserter(args)); + return App::main(args); +} diff --git a/source/app/src/App.cpp b/source/app/src/App.cpp new file mode 100644 index 0000000..9d7438a --- /dev/null +++ b/source/app/src/App.cpp @@ -0,0 +1,63 @@ +#include "App.hpp" + +#include +#include + +#include +#include + + +int App::main(std::vector &args) { + auto app = App{}; + app.game_loop(); + return EXIT_SUCCESS; +} + + +void App::error_callback_s(int error, const char *description) +{ + std::cerr << "[GLFW error]: " << description << '\n'; + exit(EXIT_FAILURE); +} + + +void App::key_callback_s(GLFWwindow *window, int key, int scancode, int action, int mods) { + auto app = static_cast(glfwGetWindowUserPointer(window)); + app->key_callback(key, scancode, action, mods); +} + + +App::App(): + mpWindow{NULL} { + glfwSetErrorCallback(error_callback_s); + glfwInit(); + + mpWindow = glfwCreateWindow(640, 480, "uGLy", NULL, NULL); + glfwSetWindowUserPointer(mpWindow, this); + glfwSetKeyCallback(mpWindow, key_callback_s); + + glfwMakeContextCurrent(mpWindow); + if(glewInit() != GLEW_OK) { + error_callback_s(GLFW_NO_ERROR, "glewInit() failed"); + } +} + + +App::~App() { + glfwTerminate(); + glfwSetErrorCallback(NULL); +} + + +void App::game_loop() { + while(!glfwWindowShouldClose(mpWindow)) { + glfwPollEvents(); + } +} + + +void App::key_callback(int key, int scancode, int action, int mods) { + if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) { + glfwSetWindowShouldClose(mpWindow, GLFW_TRUE); + } +} diff --git a/source/app/src/App.hpp b/source/app/src/App.hpp new file mode 100644 index 0000000..a9554bd --- /dev/null +++ b/source/app/src/App.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include +#include + +struct GLFWwindow; + +class App { +public: + static int main(std::vector &args); +private: + static void error_callback_s(int error, const char *description); + static void key_callback_s(GLFWwindow *window, int key, int scancode, int action, int mods); + +private: + GLFWwindow *mpWindow; + +private: + App(); + ~App(); + void game_loop(); + void key_callback(int key, int scancode, int action, int mods); +};