Adds binary file buffer and the basics of the Window class
parent
6efe491453
commit
a92e55ee01
@ -1,4 +1,7 @@
|
||||
// the configured options and settings for GLFW_Testing
|
||||
// the configured options and settings for Lunarium
|
||||
#define Lunarium_VERSION_MAJOR @Lunarium_VERSION_MAJOR@
|
||||
#define Lunarium_VERSION_MINOR @Lunarium_VERSION_MINOR@
|
||||
#define Lunarium_VERSION_PATCH @Lunarium_VERSION_PATCH@
|
||||
|
||||
#define OPENGL_MAJOR_VERSION @OpenGL_MAJOR_VERSION@
|
||||
#define OPENGL_MINOR_VERSION @OpenGL_MINOR_VERSION@
|
||||
@ -0,0 +1,183 @@
|
||||
/******************************************************************************
|
||||
* File - window.h
|
||||
* Author - Joey Pollack
|
||||
* Date - 2021/09/01 (y/m/d)
|
||||
* Mod Date - 2021/09/01 (y/m/d)
|
||||
* Description - Manages the window system using GLFW. This is where GLFW
|
||||
* is initialized. I think this file can completely hide GLFW
|
||||
* but I'm not yet sure...
|
||||
******************************************************************************/
|
||||
|
||||
#include "window.h"
|
||||
#include <LunariumConfig.h>
|
||||
#include <utils\logger.h>
|
||||
#include <utils\helpers.h>
|
||||
#include <sstream>
|
||||
|
||||
namespace lunarium
|
||||
{
|
||||
bool Window::mbIsInit = false;
|
||||
Window::Window()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
OpRes Window::Initialize(const State& state)
|
||||
{
|
||||
if (mbIsInit)
|
||||
{
|
||||
return OpRes::Fail("A window is already initialized. Multiple windows is not yet supported");
|
||||
}
|
||||
|
||||
if (state.Display.RenderFramework == Renderer::VULKAN)
|
||||
{
|
||||
return OpRes::Fail("Render Framework VULKAN is not yet implemented");
|
||||
}
|
||||
|
||||
if (state.Display.RenderFramework == Renderer::OPENGL)
|
||||
{
|
||||
int width, height;
|
||||
|
||||
if( !glfwInit() )
|
||||
{
|
||||
char buffer[1024];
|
||||
glfwGetError((char**)&buffer);
|
||||
std::ostringstream oss;
|
||||
oss << "Failed to initialize GLFW: " << buffer;
|
||||
return OpRes::Fail(oss.str().c_str());
|
||||
}
|
||||
|
||||
glfwWindowHint(GLFW_DEPTH_BITS, 16);
|
||||
glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GLFW_TRUE);
|
||||
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, OPENGL_MAJOR_VERSION);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, OPENGL_MINOR_VERSION);
|
||||
|
||||
Logger::GetInstance()->Log(LogCategory::GRAPHICS, LogLevel::INFO,
|
||||
"Creating OpenGL Context version %d, %d and glsl %s",
|
||||
OPENGL_MAJOR_VERSION, OPENGL_MINOR_VERSION, System::GetGLSLVersionString().c_str());
|
||||
|
||||
mpWindow = glfwCreateWindow( state.Display.WindowedSize.Width, state.Display.WindowedSize.Height, "Lunarium", NULL, NULL );
|
||||
if (!mpWindow)
|
||||
{
|
||||
glfwTerminate();
|
||||
char buffer[1024];
|
||||
glfwGetError((char**)&buffer);
|
||||
std::ostringstream oss;
|
||||
oss << "Failed to initialize GLFWFailed to open GLFW window: " << buffer;
|
||||
return OpRes::Fail(oss.str().c_str());
|
||||
}
|
||||
|
||||
glfwSetWindowPos(mpWindow, state.Display.WindowStartPosition.X, state.Display.WindowStartPosition.Y);
|
||||
|
||||
glfwMakeContextCurrent(mpWindow);
|
||||
|
||||
if (state.Display.VSyncEnabled)
|
||||
{
|
||||
glfwSwapInterval(1); // Enable vsync
|
||||
}
|
||||
|
||||
// Use glad2 to load extensions
|
||||
int version = gladLoadGL(glfwGetProcAddress);
|
||||
Logger::GetInstance()->Log(LogCategory::GRAPHICS, LogLevel::INFO,
|
||||
"Glad2 Loaded version: %d.%d", GLAD_VERSION_MAJOR(version), GLAD_VERSION_MINOR(version));
|
||||
}
|
||||
else
|
||||
{
|
||||
return OpRes::Fail("No Render Framework selected");
|
||||
}
|
||||
|
||||
glfwSetWindowUserPointer(mpWindow, this);
|
||||
mbIsInit = true;
|
||||
return OpRes::OK();
|
||||
}
|
||||
|
||||
bool Window::IsInit() const
|
||||
{
|
||||
return mbIsInit;
|
||||
}
|
||||
|
||||
GLFWwindow* Window::GetWindow()
|
||||
{
|
||||
return mpWindow;
|
||||
}
|
||||
|
||||
void Window::Resize(int width, int height)
|
||||
{
|
||||
glfwSetWindowSize(mpWindow, width, height);
|
||||
}
|
||||
|
||||
|
||||
// If fullscreen is true but we're already in fullscreen mode this function will change the
|
||||
// resolution. If fullscreen is false but we're not fullscreen this function will change
|
||||
// the window position and size.
|
||||
void Window::ChangeDisplayMode(bool fullscreen, int xPos, int yPos, int width, int height)
|
||||
{
|
||||
// TODO: Allow the user to select which monitor to use
|
||||
// https://www.glfw.org/docs/latest/monitor_guide.html
|
||||
// For now just use the primary monitor
|
||||
|
||||
GLFWmonitor* pMonitor = glfwGetWindowMonitor(mpWindow);
|
||||
|
||||
if (fullscreen)
|
||||
{
|
||||
if (pMonitor)
|
||||
{
|
||||
// Requsting fullscreen but we're already fullscreen so
|
||||
// just set the size
|
||||
glfwSetWindowSize(mpWindow, width, height);
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Otherwise we need to switch to fullscreen mode
|
||||
// TODO: Allow the user to select the refresh rate
|
||||
glfwSetWindowMonitor(mpWindow, glfwGetPrimaryMonitor(), 0, 0, width, height, 60);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (pMonitor)
|
||||
{
|
||||
// Go to windowed mode
|
||||
glfwSetWindowMonitor(mpWindow, nullptr, xPos, yPos, width, height, 60);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Already in windowed mode so just change the size and position
|
||||
glfwSetWindowSize(mpWindow, width, height);
|
||||
glfwSetWindowPos(mpWindow, xPos, yPos);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Window::GetFramebufferSize(int* width, int* height) const
|
||||
{
|
||||
glfwGetFramebufferSize(mpWindow, width, height);
|
||||
}
|
||||
|
||||
|
||||
bool Window::ShouldWindowClose() const
|
||||
{
|
||||
return glfwWindowShouldClose(mpWindow);
|
||||
}
|
||||
|
||||
void Window::PollEvents()
|
||||
{
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
void Window::SwapBuffers()
|
||||
{
|
||||
glfwSwapBuffers(mpWindow);
|
||||
}
|
||||
|
||||
|
||||
void Window::Shutdown()
|
||||
{
|
||||
glfwDestroyWindow(mpWindow);
|
||||
glfwTerminate();
|
||||
mbIsInit = false;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,54 @@
|
||||
/******************************************************************************
|
||||
* File - window.h
|
||||
* Author - Joey Pollack
|
||||
* Date - 2021/09/01 (y/m/d)
|
||||
* Mod Date - 2021/09/01 (y/m/d)
|
||||
* Description - Manages the window system using GLFW. This is where GLFW
|
||||
* is initialized. I think this file can completely hide GLFW
|
||||
* but I'm not yet sure...
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef WINDOW_H_
|
||||
#define WINDOW_H_
|
||||
|
||||
#include <glad/gl.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <core/state.h>
|
||||
#include <utils/opRes.h>
|
||||
|
||||
namespace lunarium
|
||||
{
|
||||
class Window
|
||||
{
|
||||
public:
|
||||
Window();
|
||||
|
||||
OpRes Initialize(const State& state);
|
||||
bool IsInit() const;
|
||||
|
||||
GLFWwindow* GetWindow();
|
||||
|
||||
|
||||
void Resize(int width, int height);
|
||||
|
||||
// TODO: See https://www.glfw.org/docs/latest/window_guide.html#window_full_screen
|
||||
void ChangeDisplayMode(bool fullscreen, int xPos, int yPos, int width, int height);
|
||||
|
||||
void GetFramebufferSize(int* width, int* height) const;
|
||||
|
||||
bool ShouldWindowClose() const;
|
||||
static void PollEvents();
|
||||
void SwapBuffers();
|
||||
|
||||
void Shutdown();
|
||||
|
||||
private:
|
||||
|
||||
// There can only be 1 window for now
|
||||
static bool mbIsInit;
|
||||
GLFWwindow* mpWindow;
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
#endif // WINDOW_H_
|
||||
@ -0,0 +1,108 @@
|
||||
/******************************************************************************
|
||||
* File - BinaryFileBuffer.cpp
|
||||
* Author - Joey Pollack
|
||||
* Date - 2020/01/24 (y/m/d)
|
||||
* Mod Date - 2020/01/24 (y/m/d)
|
||||
* Description - Reads an entire binary file into memory and allows for
|
||||
* extracting the data in chunks. Much faster than reading
|
||||
* large files chunk by chunk.
|
||||
******************************************************************************/
|
||||
|
||||
#include "BinaryFileBuffer.h"
|
||||
#include <fstream>
|
||||
|
||||
namespace lunarium
|
||||
{
|
||||
BinaryFileBuffer::BinaryFileBuffer()
|
||||
: mbIsLoaded(false), mpData(nullptr), mFileSize(0), mReadPos(0), mFileName("")
|
||||
{
|
||||
}
|
||||
|
||||
BinaryFileBuffer::~BinaryFileBuffer()
|
||||
{
|
||||
Unload();
|
||||
}
|
||||
|
||||
bool BinaryFileBuffer::LoadFile(const char * filename)
|
||||
{
|
||||
if (mbIsLoaded)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
std::ifstream ifs(filename, std::ios_base::binary);
|
||||
if (!ifs.is_open())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
int start = ifs.tellg();
|
||||
ifs.seekg(0, std::ios_base::end);
|
||||
int end = ifs.tellg();
|
||||
ifs.seekg(0, std::ios_base::beg);
|
||||
|
||||
mFileSize = end - start;
|
||||
mpData = new unsigned char[mFileSize];
|
||||
|
||||
ifs.read((char*)mpData, mFileSize);
|
||||
|
||||
ifs.close();
|
||||
ifs.clear();
|
||||
|
||||
mFileName = filename;
|
||||
mbIsLoaded = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void BinaryFileBuffer::Unload()
|
||||
{
|
||||
if (!mbIsLoaded)
|
||||
return;
|
||||
|
||||
delete[] mpData;
|
||||
mpData = nullptr;
|
||||
|
||||
mFileSize = 0;
|
||||
mFileName = "";
|
||||
mReadPos = 0;
|
||||
mbIsLoaded = false;
|
||||
}
|
||||
|
||||
bool BinaryFileBuffer::IsLoaded() const
|
||||
{
|
||||
return mbIsLoaded;
|
||||
}
|
||||
|
||||
const std::string& BinaryFileBuffer::LoadedFileName() const
|
||||
{
|
||||
return mFileName;
|
||||
}
|
||||
|
||||
int BinaryFileBuffer::GetFileSize() const
|
||||
{
|
||||
return mFileSize;
|
||||
}
|
||||
|
||||
bool BinaryFileBuffer::Read(char * buffer, int numBytes)
|
||||
{
|
||||
if (!mbIsLoaded || mReadPos >= mFileSize)
|
||||
return false;
|
||||
|
||||
memcpy_s(buffer, numBytes, &mpData[mReadPos], numBytes);
|
||||
mReadPos += numBytes;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void BinaryFileBuffer::SeekTo(int pos)
|
||||
{
|
||||
mReadPos = pos;
|
||||
|
||||
if (mReadPos < 0)
|
||||
mReadPos = 0;
|
||||
|
||||
if (mReadPos >= mFileSize)
|
||||
mReadPos = mFileSize - 1;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,44 @@
|
||||
/******************************************************************************
|
||||
* File - BinaryFileBuffer.h
|
||||
* Author - Joey Pollack
|
||||
* Date - 2020/01/24 (y/m/d)
|
||||
* Mod Date - 2020/01/24 (y/m/d)
|
||||
* Description - Reads an entire binary file into memory and allows for
|
||||
* extracting the data in chunks. Much faster than reading
|
||||
* large files chunk by chunk.
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef BINARY_FILE_BUFFER_H_
|
||||
#define BINARY_FILE_BUFFER_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace lunarium
|
||||
{
|
||||
class BinaryFileBuffer
|
||||
{
|
||||
public:
|
||||
BinaryFileBuffer();
|
||||
~BinaryFileBuffer();
|
||||
|
||||
bool LoadFile(const char* filename);
|
||||
void Unload();
|
||||
bool IsLoaded() const;
|
||||
const std::string& LoadedFileName() const;
|
||||
int GetFileSize() const;
|
||||
|
||||
bool Read(char* buffer, int numBytes);
|
||||
void SeekTo(int pos);
|
||||
|
||||
private:
|
||||
|
||||
bool mbIsLoaded;
|
||||
std::string mFileName;
|
||||
int mFileSize;
|
||||
unsigned char* mpData;
|
||||
int mReadPos;
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
#endif // BINARY_FILE_BUFFER_H_
|
||||
Loading…
Reference in New Issue