Blank window

This commit is contained in:
2025-07-27 12:38:06 -04:00
parent ca54519926
commit 1eb263f836
6 changed files with 116 additions and 6 deletions

View File

@@ -2,4 +2,8 @@ cmake_minimum_required(VERSION 3.31)
project(Ugly) project(Ugly)
find_package(GLEW REQUIRED)
find_package(glfw3 CONFIG REQUIRED)
find_package(glm CONFIG REQUIRED)
add_subdirectory(./source/app) add_subdirectory(./source/app)

View File

@@ -1,3 +1,16 @@
project(UglyMain) 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
)

View File

@@ -1,5 +0,0 @@
#include <stdio.h>
int main(void) {
printf("hello world!\n");
}

12
source/app/main.cpp Normal file
View File

@@ -0,0 +1,12 @@
#include <iterator>
#include <string>
#include <vector>
#include "App.hpp"
int main(int argc, char *argv[]) {
auto args = std::vector<std::string>{};
std::copy_n(argv, argc, std::back_inserter(args));
return App::main(args);
}

63
source/app/src/App.cpp Normal file
View File

@@ -0,0 +1,63 @@
#include "App.hpp"
#include <cstdlib>
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
int App::main(std::vector<std::string> &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<App*>(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);
}
}

23
source/app/src/App.hpp Normal file
View File

@@ -0,0 +1,23 @@
#pragma once
#include <string>
#include <vector>
struct GLFWwindow;
class App {
public:
static int main(std::vector<std::string> &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);
};