2020-02-12 00:29:35 -08:00
|
|
|
#pragma once
|
|
|
|
|
2020-02-15 19:32:26 -08:00
|
|
|
// GLFW, GLEW, GLM
|
|
|
|
#include <glew.h >
|
|
|
|
#include <glfw3.h >
|
|
|
|
#include <glm/glm.hpp >
|
2020-02-13 22:13:16 -08:00
|
|
|
#include <glm/gtc/matrix_transform.hpp>
|
2020-02-12 00:29:35 -08:00
|
|
|
|
2020-02-15 19:32:26 -08:00
|
|
|
// DGL
|
2020-02-12 00:29:35 -08:00
|
|
|
#include "DGL_FundamentalTypes.hpp"
|
|
|
|
#include "DGL_MiscTypes.hpp"
|
|
|
|
#include "DGL_Enum.hpp"
|
|
|
|
#include "DGL_Shader.hpp"
|
|
|
|
#include "DGL_Buffers.hpp"
|
2020-02-13 22:13:16 -08:00
|
|
|
#include "DGL_Space.hpp"
|
2020-02-16 22:28:24 -08:00
|
|
|
#include "DGL_Model.hpp"
|
2020-02-12 00:29:35 -08:00
|
|
|
|
|
|
|
// Non-Standard C++
|
|
|
|
#include "Cpp_Alias.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-02-14 23:21:27 -08:00
|
|
|
namespace DGL
|
2020-02-12 00:29:35 -08:00
|
|
|
{
|
|
|
|
// Aliases
|
|
|
|
|
|
|
|
// C++ STL
|
|
|
|
|
|
|
|
using std::cout;
|
|
|
|
using std::endl;
|
|
|
|
|
|
|
|
|
|
|
|
// GLFW
|
|
|
|
|
2020-02-14 23:21:27 -08:00
|
|
|
using Monitor = GLFWmonitor;
|
|
|
|
using TimeValInt = uint64_t;
|
|
|
|
using TimeValDec = double;
|
|
|
|
using Window = GLFWwindow;
|
2020-02-12 00:29:35 -08:00
|
|
|
using WindowRefList = std::vector< ptr<Window> >;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Object Instances
|
|
|
|
|
|
|
|
WindowRefList Windows;
|
|
|
|
|
|
|
|
|
|
|
|
// Constants
|
|
|
|
|
|
|
|
sfn constexpr NotShared () -> ptr<Window > { return NULL; }
|
|
|
|
sfn constexpr WindowedMode() -> ptr<Monitor> { return NULL; }
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-02-13 22:13:16 -08:00
|
|
|
// Forward Declares
|
2020-02-12 00:29:35 -08:00
|
|
|
|
2020-02-13 22:13:16 -08:00
|
|
|
sfn SwapBuffers(const ptr<Window> _window) -> void;
|
2020-02-12 00:29:35 -08:00
|
|
|
|
2020-02-13 22:13:16 -08:00
|
|
|
|
|
|
|
// Functionality
|
2020-02-12 00:29:35 -08:00
|
|
|
|
|
|
|
sfn CanClose(const ptr<Window> _theWindow)
|
|
|
|
{
|
|
|
|
return glfwWindowShouldClose(_theWindow);
|
|
|
|
}
|
|
|
|
|
2020-02-15 19:32:26 -08:00
|
|
|
sfn CanUseRawMouse()
|
|
|
|
{
|
|
|
|
return glfwRawMouseMotionSupported();
|
|
|
|
}
|
|
|
|
|
2020-02-12 00:29:35 -08:00
|
|
|
sfn CreateWindow
|
|
|
|
(
|
2020-02-14 23:21:27 -08:00
|
|
|
int _width,
|
|
|
|
int _height,
|
|
|
|
RawString<const char> _title,
|
|
|
|
ptr <Monitor > _monitorToFullscreen,
|
2020-02-12 00:29:35 -08:00
|
|
|
ptr <Window > _windowToShareResourcesWith
|
2020-02-14 23:21:27 -08:00
|
|
|
)
|
2020-02-12 00:29:35 -08:00
|
|
|
-> ptr<Window>
|
|
|
|
{
|
|
|
|
Windows.push_back(glfwCreateWindow(_width, _height, _title, _monitorToFullscreen, _windowToShareResourcesWith));
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (Windows.back() == NULL)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Failed to create a window");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (std::runtime_error _error)
|
|
|
|
{
|
|
|
|
ErrorRuntime(_error);
|
|
|
|
|
|
|
|
Exit(ExitCode::Failed);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Windows.back();
|
|
|
|
}
|
|
|
|
|
2020-02-15 19:32:26 -08:00
|
|
|
sfn CursorPositionUpdateBind(ptr<Window> _window, FnPtr<void, double, double> _functionToCall)
|
|
|
|
{
|
|
|
|
glfwSetCursorPosCallback(_window, GLFWcursorposfun(_functionToCall));
|
|
|
|
}
|
|
|
|
|
2020-02-12 00:29:35 -08:00
|
|
|
sfn DestoryWindow(const ptr<Window> _window)
|
|
|
|
{
|
|
|
|
using ElementType = decltype(Windows.begin());
|
|
|
|
|
|
|
|
for (ElementType element = Windows.begin(); element != Windows.end(); element++)
|
|
|
|
{
|
|
|
|
if (*element == _window)
|
|
|
|
{
|
|
|
|
glfwDestroyWindow(_window);
|
|
|
|
|
|
|
|
Windows.erase(element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-16 22:28:24 -08:00
|
|
|
|
2020-02-13 22:13:16 -08:00
|
|
|
|
2020-02-15 19:32:26 -08:00
|
|
|
sfn GetCursorPosition(ptr<Window> _window, ptr<double> _xAxis, ptr<double> _yAxis)
|
2020-02-14 23:21:27 -08:00
|
|
|
{
|
2020-02-15 19:32:26 -08:00
|
|
|
glfwGetCursorPos(_window, _xAxis, _yAxis);
|
2020-02-14 23:21:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
sfn GetMouseInputMode(ptr<Window> _contextWindowRef, EMouseMode _mode)
|
|
|
|
{
|
|
|
|
return glfwGetInputMode(_contextWindowRef, GLenum(_mode));
|
|
|
|
}
|
|
|
|
|
2020-02-12 00:29:35 -08:00
|
|
|
sfn GetRawTime() -> TimeValInt
|
|
|
|
{
|
|
|
|
return glfwGetTimerValue();
|
|
|
|
}
|
|
|
|
|
2020-02-15 19:32:26 -08:00
|
|
|
sfn GetTime() -> TimeValDec
|
2020-02-14 23:21:27 -08:00
|
|
|
{
|
2020-02-15 19:32:26 -08:00
|
|
|
return glfwGetTime();
|
2020-02-14 23:21:27 -08:00
|
|
|
}
|
|
|
|
|
2020-02-12 00:29:35 -08:00
|
|
|
sfn InitalizeGLFW()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
std::cout << "Initializing GLFW Version: " << glfwGetVersionString() << std::endl;
|
|
|
|
|
|
|
|
/* Initialize the library */
|
|
|
|
if (!glfwInit())
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Failed to initialize GLFW");
|
|
|
|
}
|
|
|
|
}
|
2020-02-13 22:13:16 -08:00
|
|
|
catch (const std::runtime_error _error)
|
2020-02-12 00:29:35 -08:00
|
|
|
{
|
|
|
|
ErrorRuntime(_error);
|
|
|
|
|
|
|
|
Exit(ExitCode::Failed);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sfn InitalizeGLEW()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// If using GLEW version 1.13 or earlier
|
|
|
|
//glewExperimental = true;
|
|
|
|
|
|
|
|
std::cout << "Initializing Glew Version: " << glewGetString(GLEW_VERSION) << std::endl;
|
|
|
|
|
|
|
|
GLenum err = glewInit();
|
|
|
|
|
|
|
|
if (err != GLEW_OK)
|
|
|
|
{
|
|
|
|
// Problem: glewInit failed, something is seriously wrong.
|
|
|
|
std::cout << "glewInit failed: " << glewGetErrorString(err) << std::endl;
|
|
|
|
|
|
|
|
throw std::runtime_error("Failed to initialize GLFW");
|
|
|
|
}
|
|
|
|
|
|
|
|
cout << "OpenGL Version: " << glGetString(GL_VERSION) << endl;
|
|
|
|
}
|
2020-02-13 22:13:16 -08:00
|
|
|
catch (const std::runtime_error _error)
|
2020-02-12 00:29:35 -08:00
|
|
|
{
|
|
|
|
ErrorRuntime(_error);
|
|
|
|
|
|
|
|
Exit(ExitCode::Failed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-15 19:32:26 -08:00
|
|
|
sfn KeyPressed(ptr<Window> _contextWindowRef, EKeyCodes _keyToCheck) -> bool
|
|
|
|
{
|
|
|
|
return glfwGetKey(_contextWindowRef, int(_keyToCheck));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... CodeType, typename = EKeyCodes>
|
|
|
|
sfn KeysPressed(ptr<Window> _contextWindowRef, CodeType... _otherKeys) -> bool
|
|
|
|
{
|
|
|
|
return (KeyPressed(_contextWindowRef, _otherKeys) && ...) == true;
|
|
|
|
}
|
|
|
|
|
2020-02-12 00:29:35 -08:00
|
|
|
sfn PollEvents()
|
|
|
|
{
|
|
|
|
glfwPollEvents();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-15 19:32:26 -08:00
|
|
|
sfn ResetCursor(ptr<Window> _window, gFloat _screenCenterWidth, gFloat _screenCenterHeight)
|
|
|
|
{
|
|
|
|
glfwSetCursorPos(_window, _screenCenterWidth, _screenCenterHeight);
|
|
|
|
|
|
|
|
|
|
|
|
glfwSetCursorPos(_window, 0, 0);
|
|
|
|
}
|
|
|
|
|
2020-02-12 00:29:35 -08:00
|
|
|
sfn RunBasicWindowLoop(const ptr<Window> _window)
|
|
|
|
{
|
|
|
|
/* Loop until the user closes the window */
|
|
|
|
while (not CanClose(_window))
|
|
|
|
{
|
2020-02-13 22:13:16 -08:00
|
|
|
ClearBuffer(EFrameBuffer::Color);
|
2020-02-12 00:29:35 -08:00
|
|
|
|
|
|
|
SwapBuffers(_window);
|
|
|
|
|
|
|
|
PollEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-14 23:21:27 -08:00
|
|
|
sfn RunBasicWindowLoop_Timed(const ptr<Window> _window, TimeValDec _interval, Delegate< Func<void>> _renderProcedure)
|
2020-02-12 00:29:35 -08:00
|
|
|
{
|
|
|
|
TimeValDec start, end, deltaSinceClear = 0.0;
|
|
|
|
|
|
|
|
while (not CanClose(_window))
|
|
|
|
{
|
|
|
|
start = GetTime();
|
|
|
|
|
|
|
|
if (deltaSinceClear > _interval)
|
|
|
|
{
|
2020-02-13 22:13:16 -08:00
|
|
|
|
|
|
|
ClearBuffer(EFrameBuffer::Color, EFrameBuffer::Depth);
|
|
|
|
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
glClearColor(0.25f, 0.25f, 0.25f, 1.0f);
|
|
|
|
|
|
|
|
//glMatrixMode(GL_MODELVIEW);
|
|
|
|
|
|
|
|
//glLoadIdentity();
|
2020-02-12 00:29:35 -08:00
|
|
|
|
|
|
|
_renderProcedure();
|
|
|
|
|
|
|
|
SwapBuffers(_window);
|
|
|
|
|
|
|
|
PollEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
end = GetTime();
|
|
|
|
|
|
|
|
deltaSinceClear = deltaSinceClear + end - start;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-14 23:21:27 -08:00
|
|
|
sfn SetClearColor(LinearColor _colorToSet)
|
|
|
|
{
|
|
|
|
glClearColor(_colorToSet.Red, _colorToSet.Green, _colorToSet.Blue, _colorToSet.Alpha);
|
|
|
|
}
|
2020-02-12 00:29:35 -08:00
|
|
|
|
|
|
|
sfn SetCurrentContext(const ptr<Window> _window)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
glfwMakeContextCurrent(_window);
|
|
|
|
|
|
|
|
ptr< RawString<const char> > ErrorMsg = NULL;
|
|
|
|
|
|
|
|
int code = glfwGetError(ErrorMsg);
|
|
|
|
|
|
|
|
if (code == GLFW_NO_WINDOW_CONTEXT)
|
|
|
|
{
|
|
|
|
throw std::runtime_error( Dref(ErrorMsg) );
|
|
|
|
}
|
|
|
|
}
|
2020-02-13 22:13:16 -08:00
|
|
|
catch (const std::runtime_error _error)
|
2020-02-12 00:29:35 -08:00
|
|
|
{
|
|
|
|
ErrorRuntime(_error);
|
|
|
|
|
|
|
|
Exit(ExitCode::Failed);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-15 19:32:26 -08:00
|
|
|
template<typename ModeParam>
|
|
|
|
sfn SetInputMode(ptr<Window> _window, EMouseMode _mouseMode, ModeParam _modeParam)
|
|
|
|
{
|
|
|
|
glfwSetInputMode(_window, GLenum(_mouseMode), GLenum(_modeParam));
|
|
|
|
}
|
|
|
|
|
2020-02-14 23:21:27 -08:00
|
|
|
sfn SetPolygonMode(EFace _desiredFaces, ERenderMode _desiredMode)
|
2020-02-13 22:13:16 -08:00
|
|
|
{
|
|
|
|
glPolygonMode(GLenum(_desiredFaces), GLenum(_desiredMode));
|
|
|
|
}
|
|
|
|
|
|
|
|
sfn SwapBuffers(const ptr<Window> _window) -> void
|
|
|
|
{
|
|
|
|
glfwSwapBuffers(_window);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-12 00:29:35 -08:00
|
|
|
sfn TerminateGLFW()
|
|
|
|
{
|
|
|
|
glfwTerminate();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2020-02-16 22:28:24 -08:00
|
|
|
|
|
|
|
|
|
|
|
// RawTape
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-02-12 00:29:35 -08:00
|
|
|
}
|