demotool/main.cc

207 lines
4.3 KiB
C++

#include "externals.hh"
#include "imgui_impl_sdl.h"
#include "demo.hh"
#include "globals.hh"
#include "profiling.hh"
#include "window.hh"
#include "shaders.hh"
// FIXME ^^
/*= T_Main ===================================================================*/
struct T_Main
{
T_Main( );
~T_Main( );
void mainLoop( );
private:
bool done = false;
bool capture = false;
ImVec2 mouseInitial;
ImVec2 mouseMove;
std::unique_ptr< T_ShaderProgram > spCopy;
std::unique_ptr< T_Demo > demo;
void initDemo( );
void startIteration( );
void handleCapture( );
void makeUI( );
void render( );
void initProgram( );
};
/*----------------------------------------------------------------------------*/
T_Main::T_Main( )
{
Globals::Init( );
initProgram( );
}
void T_Main::mainLoop( )
{
auto& p( Globals::Profiler( ) );
while ( !done ) {
if ( demo ) {
auto const& dspSize( ImGui::GetIO( ).DisplaySize );
if ( demo->width != dspSize.x || demo->height != dspSize.y ) {
demo.reset( );
}
}
if ( !demo ) {
initDemo( );
}
Globals::Watcher( ).check( );
Globals::Shaders( ).update( );
glFinish( );
p.startFrame( );
p.start( "Full frame" );
startIteration( );
if ( !done ) {
handleCapture( );
makeUI( );
render( );
p.end( "Full frame" );
Globals::Window( ).swap( );
p.endFrame( );
}
}
}
T_Main::~T_Main( )
{
demo.reset( );
Globals::Shutdown( );
}
/*----------------------------------------------------------------------------*/
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 >( dspSize.x , dspSize.y );
if ( demo->initialise( ) ) {
Globals::Profiler( ).clear( );
} else {
demo.reset( );
}
}
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;
}
}
Globals::Window( ).startFrame( 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 );
Globals::Window( ).warpMouse( mouseInitial );
ImGui::SetMouseCursor( ImGuiMouseCursor_Arrow );
} else if ( capture ) {
ImGui::SetMouseCursor( ImGuiMouseCursor_Move );
if ( demo ) {
demo->handleDND( mouseMove , ctrl , shift , lmb );
}
} else if ( appCanGrab && mb ) {
capture = true;
mouseInitial = ImGui::GetMousePos( );
ImGui::CaptureMouseFromApp( true );
SDL_SetRelativeMouseMode( SDL_TRUE );
ImGui::SetMouseCursor( ImGuiMouseCursor_Move );
}
if ( ( appCanGrab || capture ) && io.MouseWheel && demo ) {
demo->handleWheel( io.MouseWheel , ctrl , shift );
}
}
void T_Main::makeUI( )
{
auto const& dspSize( ImGui::GetIO( ).DisplaySize );
ImGui::SetNextWindowSize( ImVec2( 300 , dspSize.y ) ,
ImGuiSetCond_Once );
ImGui::SetNextWindowPos( ImVec2( ) , ImGuiSetCond_Once );
ImGui::Begin( "Yay! Demo!" );
ImGui::Checkbox( "Profiler" , &Globals::Profiler( ).uiEnabled( ) );
if ( demo ) {
demo->makeUI( );
}
ImGui::End( );
Globals::Profiler( ).makeUI( );
}
void T_Main::render( )
{
if ( demo ) {
Globals::Profiler( ).start( "Render" );
demo->render( );
glFinish( ); Globals::Profiler( ).end( "Render" );
}
glUseProgram( 0 );
glBindProgramPipeline( 0 );
Globals::Textures( ).reset( );
ImGui::Render( );
}
void T_Main::initProgram( )
{
spCopy = std::make_unique< T_ShaderProgram >( GL_FRAGMENT_SHADER );
spCopy->addFile( "copy.glsl" );
spCopy->load( );
}
/*============================================================================*/
int main( int , char** )
{
T_Main m;
m.mainLoop( );
return 0;
}