diff --git a/CMakeLists.txt b/CMakeLists.txt index 0b78b46..496ea18 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -76,18 +76,21 @@ target_link_libraries(imgui PUBLIC glfw glad) add_library(glad STATIC third_party/glad/src/glad.c) target_include_directories(glad PUBLIC third_party/glad/include) +# Engine als statische Lib +add_library(Engine STATIC + engine/src/core/Window.h + engine/src/core/Window.cpp +) +target_include_directories(Engine PUBLIC engine/src) +target_link_libraries(Engine PUBLIC OpenGL::GL glfw glad glm::glm imgui) + # --- Executable --- add_executable(ColorRace game/src/main.cpp - third_party/stb_image_impl.cpp ) target_link_libraries(ColorRace PRIVATE - OpenGL::GL - glfw - glad - glm::glm + Engine tinyobjloader stb_image - imgui ) \ No newline at end of file diff --git a/engine/src/core/Window.cpp b/engine/src/core/Window.cpp new file mode 100644 index 0000000..904daa8 --- /dev/null +++ b/engine/src/core/Window.cpp @@ -0,0 +1,56 @@ +// +// Created by sebastian on 29.07.26. +// + +#include "Window.h" + +#include + +#include "glad/glad.h" +#include "GLFW/glfw3.h" + +namespace engine { + static void GLAPIENTRY DebugCallback(GLenum source, GLenum type, GLuint id, + GLenum severity, GLsizei length, + const GLchar* message, const void* userParam) { + if (severity == GL_DEBUG_SEVERITY_NOTIFICATION) return; // Spam raus + std::cerr << "[GL DEBUG] " << message << "\n"; + } + + Window::Window(int width, int height, const std::string& title) { + if (!glfwInit()) throw std::runtime_error("GLFW init failed"); + + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE); + + m_handle = glfwCreateWindow(width, height, title.c_str(), nullptr, nullptr); + if (!m_handle) { + glfwTerminate(); + throw std::runtime_error("Window creation failed"); + } + + glfwMakeContextCurrent(m_handle); + + if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { + throw std::runtime_error("GLAD init failed"); + } + + glEnable(GL_DEBUG_OUTPUT); + glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS); + glDebugMessageCallback(DebugCallback, nullptr); + + glViewport(0, 0, width, height); + } + + Window::~Window() { + glfwDestroyWindow(m_handle); + glfwTerminate(); + } + + bool Window::shouldClose() const { return glfwWindowShouldClose(m_handle); } + void Window::swapBuffers() { glfwSwapBuffers(m_handle); } + void Window::pollEvents() { glfwPollEvents(); } + +} diff --git a/engine/src/core/Window.h b/engine/src/core/Window.h new file mode 100644 index 0000000..5f244ac --- /dev/null +++ b/engine/src/core/Window.h @@ -0,0 +1,35 @@ +// +// Created by sebastian on 29.07.26. +// + +#ifndef COLORRACE_WINDOW_H +#define COLORRACE_WINDOW_H + +#pragma once +#include + +struct GLFWwindow; + +namespace engine { + class Window { + public: + Window(int width, int height, const std::string& title); + ~Window(); + + bool shouldClose() const; + void swapBuffers(); + void pollEvents(); + + GLFWwindow* getHandle() const { + return m_handle; + } + private: + GLFWwindow* m_handle; + }; +} + + + + + +#endif //COLORRACE_WINDOW_H diff --git a/game/src/main.cpp b/game/src/main.cpp index 0b3fa1c..a7ccc46 100644 --- a/game/src/main.cpp +++ b/game/src/main.cpp @@ -1,18 +1,20 @@ #include +#include "core/Window.h" +#include "glad/glad.h" + // TIP To Run code, press or click the icon in the gutter. int main() { - // TIP Press when your caret is at the lang variable name to see how CLion can help you rename it. + engine::Window window(1280, 720, "ColorRace"); - const auto lang = "C++"; - std::cout << "Hello and welcome to " << lang << "!\n"; + while (!window.shouldClose()) { + window.pollEvents(); - for (int i = 1; i <= 5; i++) { - // TIP Press to start debugging your code. We have set one breakpoint for you, but you can always add more by pressing . - std::cout << "i = " << i << std::endl; + glClearColor(0.1f, 0.1f, 0.15f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); + + window.swapBuffers(); } - return 0; - // TIP See CLion help at jetbrains.com/help/clion/. Also, you can try interactive lessons for CLion by selecting 'Help | Learn IDE Features' from the main menu. } \ No newline at end of file