diff options
| author | Mel <einebeere@gmail.com> | 2022-10-01 05:12:50 +0200 |
|---|---|---|
| committer | Mel <einebeere@gmail.com> | 2022-10-01 05:13:05 +0200 |
| commit | e2234b4afebb266878435d10267e7b162b1fe984 (patch) | |
| tree | a582ab01a9a95a3dfd199702315d3f43c6b5b046 | |
| download | meowcraft-e2234b4afebb266878435d10267e7b162b1fe984.tar.zst meowcraft-e2234b4afebb266878435d10267e7b162b1fe984.zip | |
Setup window and GL functions
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | CMakeLists.txt | 11 | ||||
| -rw-r--r-- | src/Window.cpp | 45 | ||||
| -rw-r--r-- | src/Window.hpp | 24 | ||||
| -rw-r--r-- | src/main.cpp | 51 |
5 files changed, 133 insertions, 0 deletions
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 <stdexcept> +#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 <cstdint> +#include <GLFW/glfw3.h> + +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 <iostream> +#include <GL/glew.h> +#include <GLFW/glfw3.h> + +#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<const char*>(glewGetErrorString(error))); + throw std::runtime_error("Failed to load GL functions: " + error_string); + } +} |
