From e2234b4afebb266878435d10267e7b162b1fe984 Mon Sep 17 00:00:00 2001 From: Mel Date: Sat, 1 Oct 2022 05:12:50 +0200 Subject: Setup window and GL functions --- .gitignore | 2 ++ CMakeLists.txt | 11 +++++++++++ src/Window.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/Window.hpp | 24 ++++++++++++++++++++++++ src/main.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 133 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 src/Window.cpp create mode 100644 src/Window.hpp create mode 100644 src/main.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1b72d0a --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +cmake-build-debug +.idea \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..ee5ffc5 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.23) +project(meowcraft) + +set(CMAKE_CXX_STANDARD 17) + +find_package(OpenGL REQUIRED) +find_package(glfw3 3.3 REQUIRED) +find_package(GLEW REQUIRED) + +add_executable(meowcraft src/main.cpp src/Window.cpp src/Window.hpp) +target_link_libraries(meowcraft glfw OpenGL GLEW::GLEW) diff --git a/src/Window.cpp b/src/Window.cpp new file mode 100644 index 0000000..8bb8580 --- /dev/null +++ b/src/Window.cpp @@ -0,0 +1,45 @@ +#include +#include "Window.hpp" + +namespace MC { + +Window::Window(const char *title, uint32_t width, uint32_t height) { + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); + + m_window = glfwCreateWindow(width, height, title, nullptr, nullptr); + if (m_window == nullptr) { + throw std::runtime_error("Failed to create window."); + } + + glfwMakeContextCurrent(m_window); +} + +Window::~Window() { + glfwDestroyWindow(m_window); +} + +bool Window::should_close() { + return glfwWindowShouldClose(m_window); +} + +GLFWwindow* Window::get() { + return m_window; +} + +void Window::close() { + glfwSetWindowShouldClose(m_window, true); +} + +bool Window::key(int key, int type) { + return (glfwGetKey(m_window, key) == type); +} + +void Window::start_frame() { + glfwSwapBuffers(m_window); + glfwPollEvents(); +} + +} \ No newline at end of file diff --git a/src/Window.hpp b/src/Window.hpp new file mode 100644 index 0000000..acd39fe --- /dev/null +++ b/src/Window.hpp @@ -0,0 +1,24 @@ +#pragma once + +#include +#include + +namespace MC { + +class Window { +public: + Window(const char* title, uint32_t width, uint32_t height); + ~Window(); + + GLFWwindow* get(); + + void close(); + void start_frame(); + + bool key(int key, int type); + bool should_close(); +private: + GLFWwindow* m_window; +}; + +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..91e1b23 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,51 @@ +#include +#include +#include + +#include "Window.hpp" + +#define APP_NAME "Meowcraft" + +#define WINDOW_WIDTH 1000 +#define WINDOW_HEIGHT 800 + +void run(); +void setup_gl(); + +int main() { + glfwInit(); + + try { + run(); + } catch (std::runtime_error& error) { + std::cout << "An error occurred: " << error.what() << std::endl; + glfwTerminate(); + return 1; + } + + glfwTerminate(); + return 0; +} + +void run() { + MC::Window window(APP_NAME, WINDOW_WIDTH, WINDOW_HEIGHT); + setup_gl(); + + glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); + + while (!window.should_close()) { + window.start_frame(); + + if (window.key(GLFW_KEY_ESCAPE, GLFW_PRESS)) { + window.close(); + } + } +} + +void setup_gl() { + GLenum error; + if ((error = glewInit()) != GLEW_OK) { + std::string error_string = std::string(reinterpret_cast(glewGetErrorString(error))); + throw std::runtime_error("Failed to load GL functions: " + error_string); + } +} -- cgit 1.4.1