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-04 11:20:27 +02:00
|
|
|
#include "globals.hh"
|
2017-10-01 11:37:04 +02:00
|
|
|
#include "profiling.hh"
|
2017-10-04 11:20:27 +02:00
|
|
|
#include "window.hh"
|
|
|
|
#include "shaders.hh"
|
2017-10-06 14:29:01 +02:00
|
|
|
#include "odbg.hh"
|
2017-09-30 10:37:45 +02:00
|
|
|
|
2017-11-03 09:08:19 +01:00
|
|
|
using ebcl::T_Optional;
|
|
|
|
|
2017-09-30 10:37:45 +02:00
|
|
|
|
|
|
|
/*= T_Main ===================================================================*/
|
|
|
|
|
|
|
|
struct T_Main
|
|
|
|
{
|
2017-10-05 08:45:40 +02:00
|
|
|
static constexpr uint32_t ResizeDelay = 50;
|
|
|
|
|
2017-09-30 10:37:45 +02:00
|
|
|
T_Main( );
|
|
|
|
~T_Main( );
|
|
|
|
|
|
|
|
void mainLoop( );
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool done = false;
|
|
|
|
bool capture = false;
|
|
|
|
ImVec2 mouseInitial;
|
|
|
|
ImVec2 mouseMove;
|
|
|
|
|
2017-10-05 08:45:40 +02:00
|
|
|
uint32_t stopResize = 0;
|
|
|
|
ImVec2 prevSize;
|
|
|
|
|
2017-10-07 17:39:38 +02:00
|
|
|
bool demoCtrl_ = false;
|
2017-11-03 09:08:19 +01:00
|
|
|
T_Optional< T_Demo > demo;
|
2017-10-02 13:51:08 +02:00
|
|
|
|
|
|
|
void initDemo( );
|
2017-09-30 17:58:48 +02:00
|
|
|
|
2017-09-30 10:37:45 +02:00
|
|
|
void startIteration( );
|
|
|
|
void handleCapture( );
|
|
|
|
void makeUI( );
|
|
|
|
void render( );
|
|
|
|
};
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
T_Main::T_Main( )
|
|
|
|
{
|
2017-10-04 11:20:27 +02:00
|
|
|
Globals::Init( );
|
2017-10-05 08:45:40 +02:00
|
|
|
prevSize = ImVec2( 1280 , 720 );
|
2017-09-30 10:37:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void T_Main::mainLoop( )
|
|
|
|
{
|
2017-10-04 11:20:27 +02:00
|
|
|
auto& p( Globals::Profiler( ) );
|
2017-09-30 10:37:45 +02:00
|
|
|
while ( !done ) {
|
2017-11-03 09:08:19 +01:00
|
|
|
if ( demo.present( ) ) {
|
2017-10-02 13:51:08 +02:00
|
|
|
auto const& dspSize( ImGui::GetIO( ).DisplaySize );
|
2017-10-05 08:45:40 +02:00
|
|
|
if ( prevSize.x != dspSize.x || prevSize.y != dspSize.y ) {
|
|
|
|
stopResize = ResizeDelay;
|
|
|
|
prevSize = dspSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( stopResize > 0 ) {
|
|
|
|
stopResize --;
|
|
|
|
if ( stopResize == 0 ) {
|
2017-11-03 09:08:19 +01:00
|
|
|
demo.clear( );
|
2017-10-02 13:51:08 +02:00
|
|
|
}
|
|
|
|
}
|
2017-11-03 09:08:19 +01:00
|
|
|
if ( !demo.present( ) ) {
|
2017-10-02 13:51:08 +02:00
|
|
|
initDemo( );
|
|
|
|
}
|
|
|
|
|
2017-10-04 17:29:48 +02:00
|
|
|
Globals::Watcher( ).check( );
|
|
|
|
Globals::Shaders( ).update( );
|
2017-10-31 14:21:42 +01:00
|
|
|
Globals::Sync( ).checkCurveFile( );
|
2017-10-04 17:29:48 +02:00
|
|
|
|
2017-10-03 09:56:37 +02:00
|
|
|
glFinish( );
|
2017-10-01 12:48:55 +02:00
|
|
|
p.startFrame( );
|
2017-10-04 11:20:27 +02:00
|
|
|
p.start( "Full frame" );
|
2017-09-30 10:37:45 +02:00
|
|
|
startIteration( );
|
|
|
|
if ( !done ) {
|
|
|
|
handleCapture( );
|
|
|
|
makeUI( );
|
|
|
|
render( );
|
2017-10-04 11:20:27 +02:00
|
|
|
p.end( "Full frame" );
|
|
|
|
Globals::Window( ).swap( );
|
2017-10-01 12:48:55 +02:00
|
|
|
p.endFrame( );
|
2017-09-30 10:37:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
T_Main::~T_Main( )
|
|
|
|
{
|
2017-11-03 09:08:19 +01:00
|
|
|
demo.clear( );
|
2017-10-04 11:20:27 +02:00
|
|
|
Globals::Shutdown( );
|
2017-09-30 10:37:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
2017-10-02 13:51:08 +02:00
|
|
|
void T_Main::initDemo( )
|
|
|
|
{
|
2017-11-03 09:08:19 +01:00
|
|
|
assert( !demo.present( ) );
|
2017-10-02 13:51:08 +02:00
|
|
|
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 ) );
|
2017-11-03 09:08:19 +01:00
|
|
|
demo.setNew( dspSize.x , dspSize.y );
|
|
|
|
if ( ((T_Demo&)demo).initialise( ) ) {
|
2017-10-04 11:20:27 +02:00
|
|
|
Globals::Profiler( ).clear( );
|
2017-10-02 13:51:08 +02:00
|
|
|
} else {
|
2017-11-03 09:08:19 +01:00
|
|
|
demo.clear( );
|
2017-10-02 13:51:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-04 11:20:27 +02:00
|
|
|
Globals::Window( ).startFrame( capture , mouseInitial );
|
2017-09-30 10:37:45 +02:00
|
|
|
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 );
|
2017-10-04 11:20:27 +02:00
|
|
|
Globals::Window( ).warpMouse( mouseInitial );
|
2017-09-30 10:37:45 +02:00
|
|
|
ImGui::SetMouseCursor( ImGuiMouseCursor_Arrow );
|
|
|
|
} else if ( capture ) {
|
|
|
|
ImGui::SetMouseCursor( ImGuiMouseCursor_Move );
|
2017-11-03 09:08:19 +01:00
|
|
|
if ( demo.present( ) ) {
|
|
|
|
demo.target( )->handleDND( mouseMove , ctrl , shift , lmb );
|
2017-10-02 13:51:08 +02:00
|
|
|
}
|
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-11-03 09:08:19 +01:00
|
|
|
if ( ( appCanGrab || capture ) && io.MouseWheel && demo.present( ) ) {
|
|
|
|
demo.target( )->handleWheel( io.MouseWheel , ctrl , shift );
|
2017-09-30 10:37:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void T_Main::makeUI( )
|
|
|
|
{
|
2017-10-06 14:29:01 +02:00
|
|
|
ImGui::SetNextWindowSize( ImVec2( 300 , 150 ) ,
|
2017-09-30 12:25:24 +02:00
|
|
|
ImGuiSetCond_Once );
|
|
|
|
ImGui::SetNextWindowPos( ImVec2( ) , ImGuiSetCond_Once );
|
2017-10-06 14:29:01 +02:00
|
|
|
ImGui::Begin( "Tools" );
|
|
|
|
|
2017-10-06 18:42:51 +02:00
|
|
|
ImGui::Checkbox( "Demo controls" , &demoCtrl_ );
|
|
|
|
ImGui::Checkbox( "Output debugger" , &Globals::ODbg( ).uiEnabled( ) );
|
2017-10-07 17:39:38 +02:00
|
|
|
ImGui::Checkbox( "Profiler" , &Globals::Profiler( ).uiEnabled( ) );
|
2017-10-06 18:42:51 +02:00
|
|
|
ImGui::Checkbox( "Shaders" , &Globals::Shaders( ).uiEnabled( ) );
|
2017-10-06 14:29:01 +02:00
|
|
|
|
2017-11-03 09:08:19 +01:00
|
|
|
if ( demo.present( ) ) {
|
2017-10-07 17:39:38 +02:00
|
|
|
ImGui::Separator( );
|
2017-10-30 18:29:52 +01:00
|
|
|
auto& sync( Globals::Sync( ) );
|
|
|
|
const float duration( sync.duration( ) );
|
|
|
|
float time( sync.time( ) );
|
|
|
|
if ( ImGui::SliderFloat( "" , &time , 0 , duration , "%.1fs" ) ) {
|
|
|
|
sync.setTime( time );
|
2017-11-03 09:08:19 +01:00
|
|
|
demo.target( )->playing = demo.target( )->playing && ! sync.finished( );
|
2017-10-07 17:39:38 +02:00
|
|
|
}
|
|
|
|
ImGui::SameLine( );
|
2017-11-03 09:08:19 +01:00
|
|
|
if ( ImGui::Button( demo.target( )->playing ? "Stop" : "Play" ) ) {
|
|
|
|
demo.target( )->playing = !demo.target( )->playing;
|
2017-10-07 17:39:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-06 14:29:01 +02:00
|
|
|
ImGui::End( );
|
|
|
|
|
2017-11-03 09:08:19 +01:00
|
|
|
if ( demo.present( ) && demoCtrl_ ) {
|
|
|
|
demo.target( )->makeUI( );
|
2017-10-02 13:51:08 +02:00
|
|
|
}
|
2017-10-01 14:10:00 +02:00
|
|
|
|
2017-10-04 11:20:27 +02:00
|
|
|
Globals::Profiler( ).makeUI( );
|
2017-10-06 14:29:01 +02:00
|
|
|
Globals::ODbg( ).makeUI( );
|
2017-10-06 18:42:51 +02:00
|
|
|
Globals::Shaders( ).makeUI( );
|
2017-09-30 10:37:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void T_Main::render( )
|
|
|
|
{
|
2017-11-03 09:08:19 +01:00
|
|
|
if ( demo.present( ) ) {
|
2017-10-04 11:20:27 +02:00
|
|
|
Globals::Profiler( ).start( "Render" );
|
2017-11-03 09:08:19 +01:00
|
|
|
demo.target( )->render( );
|
2017-10-04 11:20:27 +02:00
|
|
|
glFinish( ); Globals::Profiler( ).end( "Render" );
|
2017-10-06 14:29:01 +02:00
|
|
|
|
|
|
|
Globals::Profiler( ).start( "Debug" );
|
|
|
|
T_Rendertarget::MainOutput( );
|
|
|
|
if ( Globals::ODbg( ).isActive( ) ) {
|
|
|
|
Globals::ODbg( ).debugOutput( );
|
|
|
|
}
|
|
|
|
glFinish( ); Globals::Profiler( ).end( "Debug" );
|
|
|
|
|
2017-10-05 18:35:35 +02:00
|
|
|
} else {
|
|
|
|
T_Rendertarget::MainOutput( );
|
|
|
|
glClearColor( 0 , 0 , 0 , 1 );
|
|
|
|
glClear( GL_COLOR_BUFFER_BIT );
|
2017-10-02 13:51:08 +02:00
|
|
|
}
|
2017-10-06 14:29:01 +02:00
|
|
|
|
2017-09-30 10:37:45 +02:00
|
|
|
glUseProgram( 0 );
|
2017-10-04 17:37:55 +02:00
|
|
|
glBindProgramPipeline( 0 );
|
2017-10-04 11:20:27 +02:00
|
|
|
Globals::Textures( ).reset( );
|
2017-10-05 18:35:35 +02:00
|
|
|
glClearColor( 0 , 0 , 0 , 1 );
|
2017-09-30 10:37:45 +02:00
|
|
|
ImGui::Render( );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*============================================================================*/
|
|
|
|
|
|
|
|
int main( int , char** )
|
|
|
|
{
|
|
|
|
T_Main m;
|
2017-10-04 11:20:27 +02:00
|
|
|
m.mainLoop( );
|
2017-09-30 10:37:45 +02:00
|
|
|
return 0;
|
|
|
|
}
|