some OpenGL boilerplate
This commit is contained in:
@@ -3,11 +3,7 @@ project(MingeCraft)
|
|||||||
|
|
||||||
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} CMakeModules/)
|
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} CMakeModules/)
|
||||||
|
|
||||||
<<<<<<< HEAD
|
|
||||||
set(CMAKE_BUILD_TYPE Debug)
|
|
||||||
=======
|
|
||||||
# set(CMAKE_BUILD_TYPE Debug)
|
# set(CMAKE_BUILD_TYPE Debug)
|
||||||
>>>>>>> dev
|
|
||||||
set(CMAKE_CXX_FLAGS "-Ofast")
|
set(CMAKE_CXX_FLAGS "-Ofast")
|
||||||
|
|
||||||
set(executable output)
|
set(executable output)
|
||||||
@@ -46,11 +42,7 @@ include_directories(${executable}
|
|||||||
|
|
||||||
file(GLOB SourceFiles
|
file(GLOB SourceFiles
|
||||||
${SrcDIR}/*
|
${SrcDIR}/*
|
||||||
${SrcDIR}/util/*
|
${SrcDIR}/ThirdParty/*
|
||||||
${SrcDIR}/game/*
|
|
||||||
${SrcDIR}/world/*
|
|
||||||
${SrcDIR}/world/chunk/*
|
|
||||||
${SrcDIR}/renderer/*
|
|
||||||
)
|
)
|
||||||
|
|
||||||
add_executable(${executable} ${SourceFiles})
|
add_executable(${executable} ${SourceFiles})
|
||||||
|
|||||||
120
src/display.cpp
Normal file
120
src/display.cpp
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
#include "display.hpp"
|
||||||
|
|
||||||
|
Display::Display( int w, int h, std::string title )
|
||||||
|
: mLogger()
|
||||||
|
{
|
||||||
|
|
||||||
|
mLogger << LOGGER_INFO << "Initializing display" << LOGGER_ENDL;
|
||||||
|
SDL_Init( SDL_INIT_VIDEO|SDL_INIT_AUDIO );
|
||||||
|
|
||||||
|
SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
|
||||||
|
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
|
||||||
|
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
|
||||||
|
SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8 );
|
||||||
|
SDL_GL_SetAttribute( SDL_GL_BUFFER_SIZE, 32 );
|
||||||
|
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
|
||||||
|
|
||||||
|
SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 1 );
|
||||||
|
SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, 4 );
|
||||||
|
|
||||||
|
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 4 );
|
||||||
|
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 5 );
|
||||||
|
|
||||||
|
// Create GL window
|
||||||
|
mLogger << LOGGER_INFO << "Creating window" << LOGGER_ENDL;
|
||||||
|
mWindow = SDL_CreateWindow( title.c_str(),
|
||||||
|
SDL_WINDOWPOS_CENTERED,
|
||||||
|
SDL_WINDOWPOS_CENTERED, w, h,
|
||||||
|
SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE );
|
||||||
|
|
||||||
|
// Create GL context
|
||||||
|
mLogger << LOGGER_INFO << "Creating OpenGL context" << LOGGER_ENDL;
|
||||||
|
mGlContext = SDL_GL_CreateContext( mWindow );
|
||||||
|
|
||||||
|
SDL_SetRelativeMouseMode( SDL_TRUE );
|
||||||
|
|
||||||
|
// Set VSYNC swap interval
|
||||||
|
SDL_GL_SetSwapInterval( 1 );
|
||||||
|
|
||||||
|
mLogger << LOGGER_INFO << "Display set up" << LOGGER_ENDL;
|
||||||
|
|
||||||
|
// Load OpenGL
|
||||||
|
gladLoadGLLoader( SDL_GL_GetProcAddress );
|
||||||
|
glEnable( GL_MULTISAMPLE );
|
||||||
|
// glEnable(GL_CULL_FACE);
|
||||||
|
glCullFace( GL_BACK );
|
||||||
|
glEnable( GL_DEPTH_TEST );
|
||||||
|
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||||
|
|
||||||
|
mLogger << LOGGER_INFO << "Loaded OpenGL" << LOGGER_ENDL;
|
||||||
|
mLogger << LOGGER_ENDL;
|
||||||
|
|
||||||
|
IsWindowOpen = true;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void Display::Input( SDL_Event* e )
|
||||||
|
{
|
||||||
|
|
||||||
|
Uint8* state = (Uint8*) SDL_GetKeyboardState( NULL );
|
||||||
|
|
||||||
|
while ( SDL_PollEvent( e ) )
|
||||||
|
{
|
||||||
|
|
||||||
|
switch ( e->type )
|
||||||
|
{
|
||||||
|
|
||||||
|
case SDL_KEYDOWN:
|
||||||
|
{
|
||||||
|
if ( e->key.keysym.sym == SDLK_ESCAPE )
|
||||||
|
{
|
||||||
|
IsMouseActive = !IsMouseActive;
|
||||||
|
|
||||||
|
if ( IsMouseActive )
|
||||||
|
SDL_SetRelativeMouseMode( SDL_TRUE );
|
||||||
|
else
|
||||||
|
SDL_SetRelativeMouseMode( SDL_FALSE );
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case SDL_WINDOWEVENT:
|
||||||
|
{
|
||||||
|
if ( e->window.event == SDL_WINDOWEVENT_RESIZED )
|
||||||
|
{
|
||||||
|
// CameraUpdateProjection( e->window.data1, e->window.data2 );
|
||||||
|
glViewport( 0, 0, e->window.data1, e->window.data2 );
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case SDL_QUIT:
|
||||||
|
{
|
||||||
|
IsWindowOpen = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// if ( IsMouseActive ) m_player->HandleMouseSDL( *e );
|
||||||
|
}
|
||||||
|
|
||||||
|
// m_player->MoveSDL( state );
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void Display::PrepareFrame()
|
||||||
|
{
|
||||||
|
static const float clear[] = { 186.0f / 255.0f, 214.0f / 255.0f, 254.0f / 255.0f };
|
||||||
|
|
||||||
|
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
|
||||||
|
glClearBufferfv( GL_COLOR, 0, clear );
|
||||||
|
}
|
||||||
|
|
||||||
|
void Display::NextFrame()
|
||||||
|
{
|
||||||
|
SDL_GL_SwapWindow( mWindow );
|
||||||
|
}
|
||||||
|
|
||||||
42
src/display.hpp
Normal file
42
src/display.hpp
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
#ifndef MINECRAFT_DISPLAY_H_
|
||||||
|
#define MINECRAFT_DISPLAY_H_
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include <logger.h>
|
||||||
|
|
||||||
|
#if _WIN32
|
||||||
|
#include <SDL.h>
|
||||||
|
#else
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <glad/glad.h>
|
||||||
|
#include <KHR/khrplatform.h>
|
||||||
|
|
||||||
|
class Display
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
Display( int w, int h, std::string title );
|
||||||
|
|
||||||
|
void Input( SDL_Event* e );
|
||||||
|
|
||||||
|
void PrepareFrame();
|
||||||
|
void NextFrame();
|
||||||
|
|
||||||
|
bool IsWindowOpen = false;
|
||||||
|
bool IsMouseActive = true;
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
Logger mLogger;
|
||||||
|
|
||||||
|
SDL_Window* mWindow = nullptr;
|
||||||
|
SDL_GLContext mGlContext = nullptr;
|
||||||
|
|
||||||
|
int mW, mH;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
78
src/main.cpp
Normal file
78
src/main.cpp
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
#define LOGGER_DEFINITION
|
||||||
|
#include <logger.h>
|
||||||
|
|
||||||
|
#if _WIN32
|
||||||
|
#include <SDL.h>
|
||||||
|
#else
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "display.hpp"
|
||||||
|
#include "settings.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
#define __DEBUG
|
||||||
|
|
||||||
|
static const int VERSION_MAJOR = 1;
|
||||||
|
static const int VERSION_MINOR = 1;
|
||||||
|
static const int VERSION_PATCH = 0;
|
||||||
|
|
||||||
|
void version()
|
||||||
|
{
|
||||||
|
std::stringstream version;
|
||||||
|
|
||||||
|
auto& container = []( std::string s ) { std::string r = ""; for ( auto& c : s ) { r += "-"; } return r; };
|
||||||
|
|
||||||
|
version << "Minecraft ";
|
||||||
|
version << VERSION_MAJOR << "." << VERSION_MINOR << "." << VERSION_PATCH;
|
||||||
|
|
||||||
|
std::cout << container( version.str() ) << std::endl;
|
||||||
|
std::cout << version.str() << std::endl;
|
||||||
|
std::cout << container( version.str() ) << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Loop( Display* display )
|
||||||
|
{
|
||||||
|
SDL_Event e;
|
||||||
|
|
||||||
|
while ( display->IsWindowOpen )
|
||||||
|
{
|
||||||
|
display->PrepareFrame();
|
||||||
|
|
||||||
|
// make framerate agnostic
|
||||||
|
display->Input( &e );
|
||||||
|
|
||||||
|
|
||||||
|
// rendering here
|
||||||
|
|
||||||
|
display->NextFrame();
|
||||||
|
}
|
||||||
|
|
||||||
|
// cleanup
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int main( int argc, char** argv )
|
||||||
|
{
|
||||||
|
version();
|
||||||
|
|
||||||
|
Logger mLogger;
|
||||||
|
|
||||||
|
#ifdef __DEBUG
|
||||||
|
mLogger << LOGGER_DEBUG << "Debug mode enabled" << LOGGER_ENDL;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// settup display
|
||||||
|
|
||||||
|
std::stringstream version;
|
||||||
|
version << "Minecraft ";
|
||||||
|
version << VERSION_MAJOR << "." << VERSION_MINOR << "." << VERSION_PATCH;
|
||||||
|
|
||||||
|
Display display { WindowWidth, WindowHeight, version.str() };
|
||||||
|
|
||||||
|
Loop( &display );
|
||||||
|
|
||||||
|
}
|
||||||
10
src/settings.hpp
Normal file
10
src/settings.hpp
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#ifndef MINECRAFT_SETTINGS_H_
|
||||||
|
#define MINECRAFT_SETTINGS_H_
|
||||||
|
|
||||||
|
// TODO: import settings and stuff
|
||||||
|
// for now this works
|
||||||
|
|
||||||
|
static const int WindowWidth = 1000;
|
||||||
|
static const int WindowHeight = 600;
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user