demotool/main.cc

243 lines
5.4 KiB
C++
Raw Normal View History

2017-09-30 10:37:45 +02:00
#include "externals.hh"
#include "imgui_impl_sdl.h"
2017-10-02 13:51:08 +02:00
#include "demo.hh"
2017-10-01 11:37:04 +02:00
#include "profiling.hh"
2017-09-30 10:37:45 +02:00
/*= T_Main ===================================================================*/
struct T_Main
{
T_Main( );
~T_Main( );
void mainLoop( );
private:
const std::string projectFile;
SDL_Window * window;
SDL_GLContext gl;
bool done = false;
bool capture = false;
ImVec2 mouseInitial;
ImVec2 mouseMove;
2017-09-30 11:47:28 +02:00
T_FilesWatcher watcher;
2017-09-30 14:59:15 +02:00
std::unique_ptr< T_ShaderProgram > spCopy;
2017-10-02 13:51:08 +02:00
std::unique_ptr< T_Demo > demo;
void initDemo( );
2017-09-30 10:37:45 +02:00
void startIteration( );
void handleCapture( );
void makeUI( );
void render( );
2017-09-30 11:23:14 +02:00
2017-09-30 12:08:18 +02:00
void initProgram( );
2017-09-30 10:37:45 +02:00
};
/*----------------------------------------------------------------------------*/
T_Main::T_Main( )
{
SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER );
// Setup window
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER , 1 );
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE , 24 );
SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE , 8 );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION , 2 );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION , 2 );
SDL_DisplayMode current;
SDL_GetCurrentDisplayMode( 0 , &current );
window = SDL_CreateWindow( "DEMO",
SDL_WINDOWPOS_CENTERED , SDL_WINDOWPOS_CENTERED ,
1280 , 720 ,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE );
gl = SDL_GL_CreateContext( window );
glewInit();
ImGui_ImplSdl_Init( window );
2017-09-30 11:23:14 +02:00
2017-09-30 12:08:18 +02:00
initProgram( );
2017-09-30 10:37:45 +02:00
}
void T_Main::mainLoop( )
{
2017-10-01 12:48:55 +02:00
auto& p( T_Profiler::Profiler );
2017-09-30 10:37:45 +02:00
while ( !done ) {
2017-10-02 13:51:08 +02:00
if ( demo ) {
auto const& dspSize( ImGui::GetIO( ).DisplaySize );
if ( demo->width != dspSize.x || demo->height != dspSize.y ) {
demo.reset( );
}
}
if ( !demo ) {
initDemo( );
}
2017-10-03 09:56:37 +02:00
glFinish( );
2017-10-01 12:48:55 +02:00
p.startFrame( );
2017-10-01 11:37:04 +02:00
T_Profiler::Profiler.start( "Full frame" );
2017-09-30 10:37:45 +02:00
startIteration( );
if ( !done ) {
handleCapture( );
makeUI( );
watcher.check( );
render( );
2017-10-01 11:37:04 +02:00
T_Profiler::Profiler.end( "Full frame" );
SDL_GL_SwapWindow( window );
2017-10-01 12:48:55 +02:00
p.endFrame( );
2017-09-30 10:37:45 +02:00
}
}
}
T_Main::~T_Main( )
{
2017-10-02 13:51:08 +02:00
demo.reset( );
T_TextureManagement::Shutdown( );
2017-09-30 10:37:45 +02:00
ImGui_ImplSdl_Shutdown( );
SDL_GL_DeleteContext( gl );
SDL_DestroyWindow( window );
SDL_Quit( );
}
/*----------------------------------------------------------------------------*/
2017-10-02 13:51:08 +02:00
void T_Main::initDemo( )
{
assert( !demo );
auto const& dspSize( ImGui::GetIO( ).DisplaySize );
if ( dspSize.x < 0 || dspSize.y < 0 ) {
return;
}
printf( "init w/ dspsize %dx%d\n" , int( dspSize.x ) , int( dspSize.y ) );
demo = std::make_unique< T_Demo >( watcher , dspSize.x , dspSize.y );
if ( demo->initialise( ) ) {
T_Profiler::Profiler.clear( );
} else {
demo.reset( );
}
}
2017-09-30 10:37:45 +02:00
void T_Main::startIteration( )
{
SDL_Event event;
mouseMove = ImVec2( );
while ( SDL_PollEvent( &event ) ) {
ImGui_ImplSdl_ProcessEvent( &event );
if ( event.type == SDL_QUIT ) {
done = true;
return;
}
if ( capture && event.type == SDL_MOUSEMOTION ) {
mouseMove.x += event.motion.xrel;
mouseMove.y += event.motion.yrel;
}
}
ImGui_ImplSdl_NewFrame( window , capture , mouseInitial );
ImGui::GetIO( ).MouseDrawCursor = true;
}
void T_Main::handleCapture( )
{
auto const& io( ImGui::GetIO( ) );
const bool lmb( ImGui::IsMouseDown( 0 ) );
const bool mb( lmb || ImGui::IsMouseDown( 1 ) );
const bool appCanGrab( !( ImGui::IsMouseHoveringAnyWindow( )
|| io.WantCaptureMouse
|| io.WantCaptureKeyboard ) );
const bool shift( io.KeyShift );
const bool ctrl( io.KeyCtrl );
if ( capture && !mb ) {
capture = false;
ImGui::CaptureMouseFromApp( false );
SDL_SetRelativeMouseMode( SDL_FALSE );
SDL_WarpMouseInWindow( window ,
int( mouseInitial.x ) ,
int( mouseInitial.y ) );
ImGui::SetMouseCursor( ImGuiMouseCursor_Arrow );
} else if ( capture ) {
ImGui::SetMouseCursor( ImGuiMouseCursor_Move );
2017-10-02 13:51:08 +02:00
if ( demo ) {
demo->handleDND( mouseMove , ctrl , shift , lmb );
}
2017-09-30 10:37:45 +02:00
} else if ( appCanGrab && mb ) {
capture = true;
mouseInitial = ImGui::GetMousePos( );
ImGui::CaptureMouseFromApp( true );
SDL_SetRelativeMouseMode( SDL_TRUE );
ImGui::SetMouseCursor( ImGuiMouseCursor_Move );
}
2017-10-02 13:51:08 +02:00
if ( ( appCanGrab || capture ) && io.MouseWheel && demo ) {
demo->handleWheel( io.MouseWheel , ctrl , shift );
2017-09-30 10:37:45 +02:00
}
}
void T_Main::makeUI( )
{
2017-09-30 12:25:24 +02:00
auto const& dspSize( ImGui::GetIO( ).DisplaySize );
ImGui::SetNextWindowSize( ImVec2( 300 , dspSize.y ) ,
ImGuiSetCond_Once );
ImGui::SetNextWindowPos( ImVec2( ) , ImGuiSetCond_Once );
ImGui::Begin( "Yay! Demo!" );
2017-10-01 14:10:00 +02:00
ImGui::Checkbox( "Profiler" , &T_Profiler::Profiler.uiEnabled( ) );
2017-10-02 13:51:08 +02:00
if ( demo ) {
demo->makeUI( );
}
2017-09-30 12:25:24 +02:00
ImGui::End( );
2017-10-01 14:10:00 +02:00
T_Profiler::Profiler.makeUI( );
2017-09-30 10:37:45 +02:00
}
void T_Main::render( )
{
2017-10-02 13:51:08 +02:00
if ( demo ) {
T_Profiler::Profiler.start( "Render" );
demo->render( );
glFinish( ); T_Profiler::Profiler.end( "Render" );
}
2017-09-30 10:37:45 +02:00
glUseProgram( 0 );
T_TextureManagement::TM( ).reset( );
2017-09-30 10:37:45 +02:00
ImGui::Render( );
}
2017-09-30 12:08:18 +02:00
void T_Main::initProgram( )
2017-09-30 11:23:14 +02:00
{
spCopy = std::make_unique< T_ShaderProgram >( GL_FRAGMENT_SHADER , watcher );
2017-09-30 14:59:15 +02:00
spCopy->addFile( "copy.glsl" );
spCopy->load( );
2017-09-30 11:23:14 +02:00
}
2017-09-30 10:37:45 +02:00
/*============================================================================*/
int main( int , char** )
{
T_Main m;
m.mainLoop( );
#if 0
// Frame time history
const int nFrameTimes = 200;
float frameTimes[ nFrameTimes ];
memset( frameTimes , 0 , sizeof( float ) * nFrameTimes );
#endif
#if 0
// Update frame time history
memmove( frameTimes , &frameTimes[ 1 ] ,
( nFrameTimes - 1 ) * sizeof( float ) );
frameTimes[ nFrameTimes - 1 ] = 1000.0f / ImGui::GetIO( ).Framerate;
#endif
return 0;
}