First push.

This commit is contained in:
Edward R. Gonzalez 2020-02-12 03:29:35 -05:00
parent c46f44a8d8
commit 4829622988
7 changed files with 693 additions and 0 deletions

81
Cpp_Alias.hpp Normal file
View File

@ -0,0 +1,81 @@
/*
Title: C++ Aliasing Library
Author: Edward R. Gonzalez
Date:
Description:
This is a segment of the C++ Assist Library I use for other code.
This merely removes the need to use operators I don't like and wraps them in easier to manage typedefs, functions or macros
*/
#pragma once
// Macros
#define sfn \
auto
#define deduce \
auto
#define Ref(_type) \
_type&
#define rRef(_type) \
_type&&
// Aliases
// Fundamental
using uInt64 = unsigned long long int;
// Pointers
template<typename Type>
using ptr = Type*;
template<typename ReturnType, typename... ParamTypes>
using FnPtr = ReturnType(*)(ParamTypes...);
// Strings
template<typename CharType>
using RawString = ptr<CharType>;
// Enum
enum class ExitCode
{
Success = EXIT_SUCCESS,
Failed = EXIT_FAILURE
};
// Functions
// Ptr
template<typename Type>
sfn Address(Ref(Type) _instance) -> ptr<Type>
{
return &_instance;
}
template<typename Type>
sfn Dref(ptr<Type> _type) -> Ref(Type)
{
return *_type;
}
// Exit
sfn Exit(ExitCode _code)
{
exit(int(_code));
}

273
DGL.hpp Normal file
View File

@ -0,0 +1,273 @@
#pragma once
// C++ STL
#include <iostream>
#include <stdexcept>
#include <vector>
// GL
#include <glew.h>
#include <glfw3.h>
#include "DGL_FundamentalTypes.hpp"
#include "DGL_MiscTypes.hpp"
#include "DGL_Enum.hpp"
#include "DGL_Shader.hpp"
#include "DGL_Buffers.hpp"
// Non-Standard C++
#include "Cpp_Alias.hpp"
namespace GL
{
// Aliases
// C++ STL
using std::cout;
using std::endl;
// GLFW
using Monitor = GLFWmonitor ;
using TimeValInt = uint64_t ;
using TimeValDec = double ;
using Window = GLFWwindow ;
using WindowRefList = std::vector< ptr<Window> >;
// Object Instances
WindowRefList Windows;
// Error Stuff
sfn ErrorRuntime(Ref(std::runtime_error) _error)
{
cout << "Runtime error occurred: " << _error.what() << endl;
return;
}
// Constants
sfn constexpr NotShared () -> ptr<Window > { return NULL; }
sfn constexpr WindowedMode() -> ptr<Monitor> { return NULL; }
// Functionality
sfn SwapBuffers(const ptr<Window> _window) -> void
{
glfwSwapBuffers(_window);
return;
}
sfn CanClose(const ptr<Window> _theWindow)
{
return glfwWindowShouldClose(_theWindow);
}
sfn CreateWindow
(
int _width ,
int _height ,
RawString<const char> _title ,
ptr <Monitor > _monitorToFullscreen ,
ptr <Window > _windowToShareResourcesWith
)
-> 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();
}
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;
}
sfn GetTime() -> TimeValDec
{
return glfwGetTime();
}
sfn GetRawTime() -> TimeValInt
{
return glfwGetTimerValue();
}
sfn InitalizeGLFW()
{
try
{
std::cout << "Initializing GLFW Version: " << glfwGetVersionString() << std::endl;
/* Initialize the library */
if (!glfwInit())
{
throw std::runtime_error("Failed to initialize GLFW");
}
}
catch (std::runtime_error _error)
{
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;
}
catch (std::runtime_error _error)
{
ErrorRuntime(_error);
Exit(ExitCode::Failed);
}
}
sfn PollEvents()
{
glfwPollEvents();
return;
}
sfn RunBasicWindowLoop(const ptr<Window> _window)
{
/* Loop until the user closes the window */
while (not CanClose(_window))
{
ClearBuffer();
SwapBuffers(_window);
PollEvents();
}
return;
}
sfn RunBasicWindowLoop_Timed(const ptr<Window> _window, TimeValDec _interval, FnPtr<void> _renderProcedure)
{
TimeValDec start, end, deltaSinceClear = 0.0;
while (not CanClose(_window))
{
start = GetTime();
if (deltaSinceClear > _interval)
{
ClearBuffer();
_renderProcedure();
SwapBuffers(_window);
PollEvents();
}
end = GetTime();
deltaSinceClear = deltaSinceClear + end - start;
}
return;
}
sfn RunTimingLoop()
{
return;
}
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) );
}
}
catch (std::runtime_error _error)
{
ErrorRuntime(_error);
Exit(ExitCode::Failed);
}
return;
}
sfn TerminateGLFW()
{
glfwTerminate();
return;
}
}

81
DGL_Buffers.hpp Normal file
View File

@ -0,0 +1,81 @@
#pragma once
// GLEW
#include <glew.h>
// DGL
#include "DGL_FundamentalTypes.hpp"
#include "DGL_MiscTypes.hpp"
#include "DGL_Enum.hpp"
// Non-Standard C++
#include "Cpp_Alias.hpp"
namespace GL
{
sfn ClearBuffer()
{
glClear(GL_COLOR_BUFFER_BIT);
return;
}
sfn BindBuffer(EBufferTarget _targetType, ID<Buffer> _buffer)
{
glBindBuffer(GLenum(_targetType), _buffer);
return;
}
sfn BindVertexArray(ptr<gUInt> _referenceToTrackArray)
{
glBindVertexArray(Dref(_referenceToTrackArray));
return;
}
template<typename TypeOfData>
sfn BufferData(ptr<TypeOfData> _data, EBufferTarget _targetType, EBufferUsage _usageType)
{
glBufferData(GLenum(_targetType), sizeof(TypeOfData), _data, GLenum(_usageType));
return;
}
template<typename VertType>
sfn FormatVertexAttributes
(
gUInt _attributeIndex ,
EDataType _vertexComponenetType ,
ptr<void> _firstVertexComponentLocation,
gInt _numberOfVertexComponents ,
EBool _shouldNormalize
)
{
glVertexAttribPointer
(
_attributeIndex ,
_numberOfVertexComponents ,
GLenum(_vertexComponenetType),
GLenum(_shouldNormalize ),
sizeof(VertType ),
_firstVertexComponentLocation
);
}
sfn GenerateBuffers(ptr<gUInt> _bufferReferencer, uInt64 _numberOfBuffersToReserve)
{
glGenBuffers(_numberOfBuffersToReserve, _bufferReferencer);
return;
}
sfn GenerateVertexBuffers(ptr<gUInt> __referenceRetainerToBuffer, uInt64 _numOfObjectsToReserveFor)
{
glGenVertexArrays(_numOfObjectsToReserveFor, __referenceRetainerToBuffer);
return;
}
}

86
DGL_Enum.hpp Normal file
View File

@ -0,0 +1,86 @@
#pragma once
// GLEW
#include <glew.h>
namespace GL
{
enum class EBool
{
True = GL_TRUE ,
False = GL_FALSE
};
enum class EBufferTarget
{
VertexAttributes = GL_ARRAY_BUFFER ,
AtomicCounter = GL_ATOMIC_COUNTER_BUFFER ,
CopySource = GL_COPY_READ_BUFFER ,
CopyDestination = GL_COPY_WRITE_BUFFER ,
IndirectComputeDispatchCommands = GL_DISPATCH_INDIRECT_BUFFER ,
IndirectCommandArguments = GL_DRAW_INDIRECT_BUFFER ,
VertexIndices = GL_ELEMENT_ARRAY_BUFFER ,
PixelReadTarget = GL_PIXEL_PACK_BUFFER ,
TextureDataSource = GL_PIXEL_UNPACK_BUFFER ,
QueryResult = GL_QUERY_BUFFER ,
ReadWriteShaderStorage = GL_SHADER_STORAGE_BUFFER ,
TextureData = GL_TEXTURE_BUFFER ,
TransformFeedback = GL_TRANSFORM_FEEDBACK_BUFFER,
UniformBlockStorage = GL_UNIFORM_BUFFER
};
enum class EBufferUsage
{
DynamicCopy = GL_DYNAMIC_COPY,
DynamicDraw = GL_DYNAMIC_DRAW,
DynamicRead = GL_DYNAMIC_READ,
StreamCopy = GL_STREAM_COPY,
StreamDraw = GL_STREAM_DRAW,
StreamRead = GL_STREAM_READ,
StaticCopy = GL_STATIC_COPY,
StaticDraw = GL_STATIC_DRAW,
StaticRead = GL_STATIC_READ
};
enum class EDataType
{
Byte = GL_BYTE ,
UnsignedByte = GL_UNSIGNED_BYTE ,
Short = GL_SHORT ,
UnsignedShort = GL_UNSIGNED_SHORT,
Int = GL_INT ,
UnsignedInt = GL_UNSIGNED_INT ,
Fixed = GL_FIXED ,
Half = GL_HALF_FLOAT ,
Float = GL_FLOAT ,
Double = GL_DOUBLE
};
enum class EShaderType
{
Vertex = GL_VERTEX_SHADER ,
Fragment = GL_FRAGMENT_SHADER
};
enum class EPrimitives
{
Points = GL_POINTS,
Lines = GL_LINES ,
LineStrip = GL_LINE_STRIP,
LineLoop = GL_LINE_LOOP ,
Triangles = GL_TRIANGLES ,
TriangleStrip = GL_TRIANGLE_STRIP,
TriangleFan = GL_TRIANGLE_FAN ,
Quads = GL_QUADS ,
QuadsStrip = GL_QUAD_STRIP,
Patches = GL_PATCHES
};
}

17
DGL_FundamentalTypes.hpp Normal file
View File

@ -0,0 +1,17 @@
#pragma once
// GLEW
#include <glew.h>
namespace GL
{
// Fundamental Types
using gChar = GLchar ;
using gFloat = GLfloat;
using gInt = GLint ;
using gUInt = GLuint ;
using gSize = GLsizei;
}

29
DGL_MiscTypes.hpp Normal file
View File

@ -0,0 +1,29 @@
#pragma once
// OpenGL
#include <glew.h>
// Duck Tape
#include "DGL_FundamentalTypes.hpp"
// Non-Standard C++
#include "Cpp_Alias.hpp"
namespace GL
{
// Made up types.
template<typename ReferenceType>
using ID = gUInt;
// ID Reference Types
class Buffer ;
class Shader ;
class ShaderProgram;
using DataPtr = ptr<GLvoid>;
using VertexBuffer = gUInt ;
}

126
DGL_Shader.hpp Normal file
View File

@ -0,0 +1,126 @@
#pragma once
// C++
#include <cstdarg>
//DGL
#include "DGL_FundamentalTypes.hpp"
#include "DGL_MiscTypes.hpp"
#include "DGL_Enum.hpp"
namespace GL
{
// Some Raw Source Defaults:
RawString<const char> RawVertextShaderSource =
"#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";
RawString<const char> RawFragmentShaderSource =
"#version 400\n"
"out vec4 frag_colour;"
"void main() {"
" frag_colour = vec4(0.5, 0.0, 0.5, 1.0);"
"}";
// Functions
sfn AttachShader(ID<ShaderProgram> _shaderProgram, ID<Shader> _shaderToAttach)
{
glAttachShader(_shaderProgram, _shaderToAttach);
return;
}
sfn BindShaderSource(ID<Shader> _shader, uInt64 _numberOfStringElements, ptr< RawString<const gChar>> _sourceCode, ptr<const gInt> _lengthsOfStrings)
{
glShaderSource(_shader, _numberOfStringElements, _sourceCode, _lengthsOfStrings);
return;
}
sfn CompileShader(ID<Shader> _shaderToCompile)
{
glCompileShader(_shaderToCompile);
return;
}
sfn CreateShader(EShaderType _typeOfShader) -> ID<Shader>
{
return glCreateShader(GLenum(_typeOfShader));
}
sfn CreateShaderProgram() -> ID<ShaderProgram>
{
return glCreateProgram();
}
sfn DeleteShader(ID<Shader> _shaderToDelete)
{
glDeleteShader(_shaderToDelete);
return;
}
sfn LinkProgramShader(ID<ShaderProgram> _shaderProgramToLink)
{
glLinkProgram(_shaderProgramToLink);
return;
}
sfn UseProgramShader(ID<ShaderProgram> _shaderProgramToUse)
{
glUseProgram(_shaderProgramToUse);
return;
}
// Raw Tape
sfn MakeShader
(
Ref(ID<Shader>) _shaderIDHolder ,
EShaderType _typeOfShader ,
uInt64 _numberOfStringElements,
ptr<RawString<const gChar>> _sourceCode ,
ptr< const gInt > _lengthsOfStrings
)
{
_shaderIDHolder = CreateShader(_typeOfShader);
BindShaderSource(_shaderIDHolder, _numberOfStringElements, _sourceCode, _lengthsOfStrings);
CompileShader(_shaderIDHolder);
}
sfn MakeShaderProgram(Ref(ID<ShaderProgram>) _shaderProgramIDHolder, ID<Shader> _shadersToAttach...)
{
_shaderProgramIDHolder = CreateShaderProgram();
va_list args;
va_start(args, _shadersToAttach);
AttachShader(_shaderProgramIDHolder, va_arg(args, ID<Shader>));
va_end(args);
LinkProgramShader(_shaderProgramIDHolder);
}
}