Basic window opening
This commit is contained in:
parent
26a3775406
commit
f7107f5885
|
@ -4,10 +4,15 @@
|
|||
cmake_minimum_required (VERSION 3.8)
|
||||
|
||||
# Add source to this project's executable.
|
||||
add_library (GLFramework "alibi.cpp")
|
||||
add_library (GLFramework "alibi.cpp" "window.cpp")
|
||||
|
||||
file(GLOB vendor_SRC
|
||||
"${CMAKE_SOURCE_DIR}/vendor/src/*.cpp"
|
||||
"${CMAKE_SOURCE_DIR}/vendor/src/*.c"
|
||||
)
|
||||
|
||||
target_sources(GLFramework PRIVATE
|
||||
${CMAKE_SOURCE_DIR}/vendor/src
|
||||
${vendor_SRC}
|
||||
)
|
||||
|
||||
target_include_directories(GLFramework PRIVATE
|
||||
|
|
|
@ -1,2 +1,24 @@
|
|||
#include <iostream>
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#ifndef glfInit
|
||||
|
||||
void _glfInit()
|
||||
{
|
||||
// Initialize GLFW
|
||||
int result = glfwInit();
|
||||
if (result == GLFW_FALSE)
|
||||
throw std::exception("Failed to initialize GLFW");
|
||||
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
}
|
||||
|
||||
#define glfInit _glfInit
|
||||
|
||||
#endif // glfInit
|
||||
|
||||
#include "window.hpp"
|
||||
|
|
31
GLFramework/include/window.hpp
Normal file
31
GLFramework/include/window.hpp
Normal file
|
@ -0,0 +1,31 @@
|
|||
#pragma once
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
namespace glf
|
||||
{
|
||||
#define USER_CALLBACK virtual void
|
||||
#define LIB_CALLBACK static void
|
||||
|
||||
class IWindow
|
||||
{
|
||||
public:
|
||||
void Create(int width, int height, const char* title, bool fullscreen = false, GLFWwindow* share = nullptr);
|
||||
void Close() noexcept;
|
||||
|
||||
int ShouldClose();
|
||||
|
||||
void Display();
|
||||
void PollEvents(); // TODO: needed?
|
||||
|
||||
protected:
|
||||
IWindow();
|
||||
|
||||
protected:
|
||||
GLFWwindow* window;
|
||||
|
||||
GLFWmonitor* monitor;
|
||||
GLFWwindow* share;
|
||||
};
|
||||
}
|
59
GLFramework/window.cpp
Normal file
59
GLFramework/window.cpp
Normal file
|
@ -0,0 +1,59 @@
|
|||
#include "window.hpp"
|
||||
|
||||
#include <exception>
|
||||
|
||||
namespace glf
|
||||
{
|
||||
IWindow::IWindow() :
|
||||
window(nullptr), monitor(nullptr), share(nullptr)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void IWindow::Create(int width, int height, const char* title, bool fullscreen /*= false*/, GLFWwindow* share /*= nullptr*/)
|
||||
{
|
||||
// Create window
|
||||
if (window == nullptr)
|
||||
{
|
||||
if (fullscreen)
|
||||
true; // TODO: Implement monitor stuff
|
||||
|
||||
this->share = share;
|
||||
window = glfwCreateWindow(width, height, title, monitor, share);
|
||||
if (window == nullptr)
|
||||
{
|
||||
throw std::exception("Failed to create GLFWwindow");
|
||||
}
|
||||
|
||||
glfwMakeContextCurrent(window);
|
||||
|
||||
// Initialize GLAD
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
|
||||
throw std::exception("Failed to initialize GLAD");
|
||||
|
||||
// Set viewport
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
}
|
||||
|
||||
void IWindow::Close() noexcept
|
||||
{
|
||||
glfwDestroyWindow(window);
|
||||
}
|
||||
|
||||
int IWindow::ShouldClose()
|
||||
{
|
||||
return glfwWindowShouldClose(window);
|
||||
}
|
||||
|
||||
void IWindow::Display()
|
||||
{
|
||||
glfwSwapBuffers(window);
|
||||
}
|
||||
|
||||
void IWindow::PollEvents()
|
||||
{
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
|
||||
add_executable(example
|
||||
"main.cpp"
|
||||
)
|
||||
"MyWindow.hpp")
|
||||
|
||||
target_include_directories(example PRIVATE
|
||||
${CMAKE_SOURCE_DIR}/GLFramework/include
|
||||
|
|
22
example/MyWindow.hpp
Normal file
22
example/MyWindow.hpp
Normal file
|
@ -0,0 +1,22 @@
|
|||
#include "glf.hpp"
|
||||
|
||||
using namespace glf;
|
||||
|
||||
class MyWindow :
|
||||
public IWindow
|
||||
{
|
||||
public:
|
||||
MyWindow() :
|
||||
IWindow()
|
||||
{
|
||||
Create(800, 600, "Example Window");
|
||||
|
||||
glfwSetFramebufferSizeCallback(window, FramebufferSizeCallback);
|
||||
}
|
||||
|
||||
private:
|
||||
static void FramebufferSizeCallback(GLFWwindow* window, int width, int height)
|
||||
{
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
};
|
|
@ -1,8 +1,27 @@
|
|||
#include "glf.hpp"
|
||||
#include "MyWindow.hpp"
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
glfwInit();
|
||||
MyWindow* window = nullptr;
|
||||
try {
|
||||
glfInit();
|
||||
window = new MyWindow;
|
||||
}
|
||||
catch (std::exception e)
|
||||
{
|
||||
std::cout << e.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (!window->ShouldClose())
|
||||
{
|
||||
glfwPollEvents();
|
||||
window->Display();
|
||||
}
|
||||
|
||||
window->Close();
|
||||
glfwTerminate();
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in a new issue