Added window
This commit is contained in:
commit
2a8b38ffc4
12 changed files with 187 additions and 0 deletions
49
src/Application.cpp
Normal file
49
src/Application.cpp
Normal file
|
@ -0,0 +1,49 @@
|
|||
#include "Application.hpp"
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
#include "Window.hpp"
|
||||
|
||||
void Application::Run()
|
||||
{
|
||||
if (!valid)
|
||||
{
|
||||
spdlog::error("Application could not be launched");
|
||||
return;
|
||||
}
|
||||
|
||||
spdlog::debug("Launched Application");
|
||||
while (!window->ShouldClose())
|
||||
{
|
||||
glfwPollEvents();
|
||||
window->Display();
|
||||
}
|
||||
}
|
||||
|
||||
Application::Application() :
|
||||
valid(true), window(nullptr)
|
||||
{
|
||||
int success = glfwInit();
|
||||
if (success != GLFW_TRUE)
|
||||
{
|
||||
const char* buffer;
|
||||
int result = glfwGetError(&buffer);
|
||||
spdlog::critical("Failed to initialize GLFW: ({}) {}", result, buffer);
|
||||
|
||||
valid = false;
|
||||
return;
|
||||
}
|
||||
spdlog::debug("GLFW initialized");
|
||||
|
||||
window = new Window(800, 800, "Koi Koi");
|
||||
valid = window->IsValid();
|
||||
}
|
||||
|
||||
Application::~Application()
|
||||
{
|
||||
delete window;
|
||||
|
||||
glfwTerminate();
|
||||
spdlog::debug("GLFW terminated");
|
||||
}
|
24
src/Application.hpp
Normal file
24
src/Application.hpp
Normal file
|
@ -0,0 +1,24 @@
|
|||
#pragma once
|
||||
|
||||
class Window;
|
||||
|
||||
class Application
|
||||
{
|
||||
public:
|
||||
static Application& GetInstance()
|
||||
{
|
||||
static Application app;
|
||||
return app;
|
||||
}
|
||||
|
||||
public:
|
||||
void Run();
|
||||
|
||||
private:
|
||||
bool valid;
|
||||
Window* window;
|
||||
|
||||
private:
|
||||
Application();
|
||||
~Application();
|
||||
};
|
15
src/CMakeLists.txt
Normal file
15
src/CMakeLists.txt
Normal file
|
@ -0,0 +1,15 @@
|
|||
add_executable(koikoi
|
||||
"main.cpp"
|
||||
"Application.cpp"
|
||||
"Window.cpp"
|
||||
)
|
||||
|
||||
target_include_directories(koikoi PRIVATE
|
||||
spdlog
|
||||
glfw
|
||||
)
|
||||
|
||||
target_link_libraries(koikoi PRIVATE
|
||||
spdlog
|
||||
glfw
|
||||
)
|
36
src/Window.cpp
Normal file
36
src/Window.cpp
Normal file
|
@ -0,0 +1,36 @@
|
|||
#include "Window.hpp"
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
Window::Window(int width, int height, const std::string& title) :
|
||||
window(NULL), valid(true)
|
||||
{
|
||||
window = glfwCreateWindow(width, height, title.c_str(), NULL, NULL);
|
||||
if (window == NULL)
|
||||
{
|
||||
const char* buffer;
|
||||
int result = glfwGetError(&buffer);
|
||||
spdlog::critical("Failed to create GLFWwindow: ({}) {}", result, buffer);
|
||||
|
||||
valid = false;
|
||||
return;
|
||||
}
|
||||
|
||||
spdlog::debug("Created GLFWwindow \n\tDimensions = ({}px, {}px), \n\tTitle = {}, \n\tFullscreen = {}", width, height, title, "NO");
|
||||
glfwMakeContextCurrent(window);
|
||||
}
|
||||
|
||||
Window::~Window()
|
||||
{
|
||||
if (valid)
|
||||
{
|
||||
glfwDestroyWindow(window);
|
||||
spdlog::debug("Destroyed GLFWwindow");
|
||||
}
|
||||
}
|
||||
|
||||
void Window::Display()
|
||||
{
|
||||
glfwSwapBuffers(window);
|
||||
}
|
20
src/Window.hpp
Normal file
20
src/Window.hpp
Normal file
|
@ -0,0 +1,20 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
class Window
|
||||
{
|
||||
public:
|
||||
Window(int width, int height, const std::string& title);
|
||||
~Window();
|
||||
|
||||
inline bool IsValid() { return valid; }
|
||||
inline bool ShouldClose() { return glfwWindowShouldClose(window); }
|
||||
|
||||
void Display();
|
||||
|
||||
private:
|
||||
bool valid;
|
||||
GLFWwindow* window;
|
||||
};
|
14
src/main.cpp
Normal file
14
src/main.cpp
Normal file
|
@ -0,0 +1,14 @@
|
|||
#include <spdlog/spdlog.h>
|
||||
#include "Application.hpp"
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
spdlog::set_pattern("[%H:%M:%S] [%^%l%$] %v");
|
||||
|
||||
#ifndef NDEBUG
|
||||
spdlog::set_level(spdlog::level::debug);
|
||||
#endif
|
||||
|
||||
Application::GetInstance().Run();
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue