demotool/main.cc

297 lines
6.1 KiB
C++
Raw Normal View History

2017-09-30 10:37:45 +02:00
#include "externals.hh"
2017-10-02 13:51:08 +02:00
#include "demo.hh"
#include "common.hh"
#include "c-sync.hh"
#include "c-undo.hh"
#include "ui.hh"
#include "ui-app.hh"
#include "ui-imgui-sdl.hh"
#include "ui-odbg.hh"
#include "ui-opemu.hh"
#include "ui-profiling.hh"
#include "ui-rendertarget.hh"
#include "ui-sequencer.hh"
#include "ui-shaders.hh"
#include "ui-sync.hh"
#include "ui-utilities.hh"
2017-11-03 09:08:19 +01:00
2017-09-30 10:37:45 +02:00
/*= T_Main ===================================================================*/
struct T_Main
{
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;
uint32_t stopResize = 0;
ImVec2 prevSize;
2017-11-03 09:08:19 +01:00
T_Optional< T_Demo > demo;
T_Optional< T_SyncView > sequencer;
2017-10-02 13:51:08 +02:00
void initDemo( );
2017-09-30 10:37:45 +02:00
void startIteration( );
void handleCapture( );
void makeUI( );
void render( );
};
/*----------------------------------------------------------------------------*/
T_Main::T_Main( )
{
Common::Init( );
UI::Init( );
2017-11-15 23:09:52 +01:00
prevSize = ImVec2( -1 , -1 );
2017-09-30 10:37:45 +02:00
}
void T_Main::mainLoop( )
{
auto& p( UI::Profiler( ) );
2017-09-30 10:37:45 +02:00
while ( !done ) {
2017-11-15 23:09:52 +01:00
auto const& dspSize( ImGui::GetIO( ).DisplaySize );
if ( prevSize.x != dspSize.x || prevSize.y != dspSize.y ) {
const auto doit( prevSize.x > 0 );
prevSize = dspSize;
if ( doit ) {
stopResize = ResizeDelay;
}
}
2017-11-15 23:09:52 +01:00
bool needInit( !demo );
if ( stopResize > 0 ) {
stopResize --;
if ( stopResize == 0 ) {
2017-11-15 23:09:52 +01:00
needInit = true;
2017-10-02 13:51:08 +02:00
}
}
2017-11-15 23:09:52 +01:00
if ( needInit ) {
2017-10-02 13:51:08 +02:00
initDemo( );
}
#warning FIXME remove this
if ( !sequencer ) {
sequencer.setNew( );
}
2017-10-02 13:51:08 +02:00
Common::Watcher( ).check( );
UI::Shaders( ).update( );
2017-10-03 09:56:37 +02:00
glFinish( );
2017-10-01 12:48:55 +02:00
p.startFrame( );
p.start( "Full frame" );
2017-09-30 10:37:45 +02:00
startIteration( );
if ( !done ) {
handleCapture( );
makeUI( );
render( );
p.end( "Full frame" );
UI::Main( ).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( );
UI::Shutdown( );
Common::Shutdown( );
2017-09-30 10:37:45 +02:00
}
/*----------------------------------------------------------------------------*/
2017-10-02 13:51:08 +02:00
void T_Main::initDemo( )
{
auto const& dspSize( ImGui::GetIO( ).DisplaySize );
if ( dspSize.x < 0 || dspSize.y < 0 ) {
return;
}
2017-11-15 23:09:52 +01:00
if ( !demo ) {
demo.setNew( );
}
2017-10-02 13:51:08 +02:00
printf( "init w/ dspsize %dx%d\n" , int( dspSize.x ) , int( dspSize.y ) );
2017-11-15 23:09:52 +01:00
if ( demo->initialise( (uint32_t) dspSize.x , (uint32_t) dspSize.y ) ) {
UI::Profiler( ).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;
}
}
UI::Main( ).startFrame( capture , mouseInitial );
2017-09-30 10:37:45 +02:00
ImGui::GetIO( ).MouseDrawCursor = true;
}
void T_Main::handleCapture( )
{
using namespace ImGui;
auto const& io( GetIO( ) );
const T_MouseButtons mb( ([]() {;
T_MouseButtons mb;
if ( IsMouseDown( 0 ) ) {
mb |= E_MouseButton::LEFT;
}
if ( IsMouseDown( 1 ) ) {
mb |= E_MouseButton::RIGHT;
}
if ( IsMouseDown( 2 ) ) {
mb |= E_MouseButton::MIDDLE;
}
return mb;
})() );
const T_KeyboardModifiers kb( ([&io]() {
T_KeyboardModifiers kb;
if ( io.KeyCtrl ) {
kb |= E_KeyboardModifier::CTRL;
}
if ( io.KeyShift ) {
kb |= E_KeyboardModifier::SHIFT;
}
if ( io.KeyAlt ) {
kb |= E_KeyboardModifier::ALT;
}
return kb;
})() );
2017-09-30 10:37:45 +02:00
const bool appCanGrab( !( ImGui::IsMouseHoveringAnyWindow( )
|| io.WantCaptureMouse
|| io.WantCaptureKeyboard ) );
if ( capture && !mb ) {
capture = false;
CaptureMouseFromApp( false );
2017-09-30 10:37:45 +02:00
SDL_SetRelativeMouseMode( SDL_FALSE );
UI::Main( ).warpMouse( mouseInitial );
SetMouseCursor( ImGuiMouseCursor_Arrow );
2017-09-30 10:37:45 +02:00
} else if ( capture ) {
SetMouseCursor( ImGuiMouseCursor_Move );
UI::Sync( ).handleDragAndDrop( mouseMove , kb , mb );
2017-09-30 10:37:45 +02:00
} else if ( appCanGrab && mb ) {
capture = true;
mouseInitial = GetMousePos( );
CaptureMouseFromApp( true );
2017-09-30 10:37:45 +02:00
SDL_SetRelativeMouseMode( SDL_TRUE );
SetMouseCursor( ImGuiMouseCursor_Move );
2017-09-30 10:37:45 +02:00
}
if ( ( appCanGrab || capture ) && io.MouseWheel ) {
UI::Sync( ).handleWheel( io.MouseWheel , kb , mb );
2017-09-30 10:37:45 +02:00
}
}
void T_Main::makeUI( )
{
using namespace ImGui;
auto& undo( Common::Undo( ) );
bool eSequencer{ sequencer };
if ( BeginMainMenuBar( ) ) {
if ( BeginMenu( "File" ) ) {
UI::Main( ).actionMenu( "Save curves" );
UI::Main( ).actionMenu( "Reload curves" );
Separator( );
2017-11-22 14:14:49 +01:00
if ( MenuItem( "Undo" , "C-z" , false , undo.canUndo( ) ) ) {
undo.undo( );
}
if ( MenuItem( "Redo" , "C-Z" , false , undo.canRedo( ) ) ) {
undo.redo( );
}
Separator( );
if ( MenuItem( "Quit" ) ) {
done = true;
}
EndMenu( );
2017-10-07 17:39:38 +02:00
}
if ( BeginMenu( "Views" ) ) {
MenuItemCheckbox( "Input overrides" ,
&UI::Sync( ).overridesWindowEnabled( ) );
MenuItemCheckbox( "Output debugger" ,
&UI::ODbg( ).uiEnabled( ) );
MenuItemCheckbox( "Profiler" ,
&UI::Profiler( ).uiEnabled( ) );
MenuItemCheckbox( "Sequencer" , &eSequencer );
MenuItemCheckbox( "Shaders" ,
&UI::Shaders( ).uiEnabled( ) );
EndMenu( );
2017-10-07 17:39:38 +02:00
}
EndMainMenuBar( );
2017-10-07 17:39:38 +02:00
}
if ( eSequencer && !sequencer ) {
sequencer.setNew( );
} else if ( !eSequencer && sequencer ) {
sequencer.clear( );
}
UI::Profiler( ).makeUI( );
UI::ODbg( ).makeUI( );
UI::Shaders( ).makeUI( );
UI::Sync( ).makeOverridesWindow( );
if ( sequencer && !sequencer->display( ) ) {
sequencer.clear( );
}
2017-09-30 10:37:45 +02:00
}
void T_Main::render( )
{
2017-11-08 09:09:21 +01:00
if ( demo ) {
demo->render( );
UI::Profiler( ).start( "Debug" );
T_Rendertarget::MainOutput( );
if ( UI::ODbg( ).isActive( ) ) {
UI::ODbg( ).debugOutput( );
}
glFinish( ); UI::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
}
UI::Main( ).handleDialogs( );
2017-09-30 10:37:45 +02:00
glUseProgram( 0 );
2017-10-04 17:37:55 +02:00
glBindProgramPipeline( 0 );
UI::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;
m.mainLoop( );
2017-09-30 10:37:45 +02:00
return 0;
}