ADD: Basic Window

This commit is contained in:
sebastian 2026-07-29 11:14:15 +02:00
parent a7c2e586eb
commit 7c7ac67fc8
4 changed files with 110 additions and 14 deletions

View File

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

View File

@ -0,0 +1,56 @@
//
// Created by sebastian on 29.07.26.
//
#include "Window.h"
#include <iostream>
#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(); }
}

35
engine/src/core/Window.h Normal file
View File

@ -0,0 +1,35 @@
//
// Created by sebastian on 29.07.26.
//
#ifndef COLORRACE_WINDOW_H
#define COLORRACE_WINDOW_H
#pragma once
#include <string>
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

View File

@ -1,18 +1,20 @@
#include <iostream>
#include "core/Window.h"
#include "glad/glad.h"
// TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
int main() {
// TIP Press <shortcut actionId="RenameElement"/> when your caret is at the <b>lang</b> 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 <shortcut actionId="Debug"/> to start debugging your code. We have set one <icon src="AllIcons.Debugger.Db_set_breakpoint"/> breakpoint for you, but you can always add more by pressing <shortcut actionId="ToggleLineBreakpoint"/>.
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 <a href="https://www.jetbrains.com/help/clion/">jetbrains.com/help/clion/</a>. Also, you can try interactive lessons for CLion by selecting 'Help | Learn IDE Features' from the main menu.
}