180 lines
4 KiB
C++
180 lines
4 KiB
C++
|
#include "externals.hh"
|
||
|
|
||
|
#include "imgui_impl_sdl.h"
|
||
|
#include "utilities.hh"
|
||
|
|
||
|
|
||
|
/*= T_Main ===================================================================*/
|
||
|
|
||
|
struct T_Main
|
||
|
{
|
||
|
T_Main( );
|
||
|
~T_Main( );
|
||
|
|
||
|
void mainLoop( );
|
||
|
|
||
|
private:
|
||
|
const std::string projectFile;
|
||
|
SDL_Window * window;
|
||
|
SDL_GLContext gl;
|
||
|
T_FilesWatcher watcher;
|
||
|
std::string loadError;
|
||
|
|
||
|
bool done = false;
|
||
|
bool capture = false;
|
||
|
ImVec2 mouseInitial;
|
||
|
ImVec2 mouseMove;
|
||
|
|
||
|
void startIteration( );
|
||
|
void handleCapture( );
|
||
|
void makeUI( );
|
||
|
void render( );
|
||
|
};
|
||
|
|
||
|
/*----------------------------------------------------------------------------*/
|
||
|
|
||
|
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 , ¤t );
|
||
|
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 );
|
||
|
}
|
||
|
|
||
|
void T_Main::mainLoop( )
|
||
|
{
|
||
|
while ( !done ) {
|
||
|
startIteration( );
|
||
|
if ( !done ) {
|
||
|
handleCapture( );
|
||
|
makeUI( );
|
||
|
watcher.check( );
|
||
|
render( );
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
T_Main::~T_Main( )
|
||
|
{
|
||
|
ImGui_ImplSdl_Shutdown( );
|
||
|
SDL_GL_DeleteContext( gl );
|
||
|
SDL_DestroyWindow( window );
|
||
|
SDL_Quit( );
|
||
|
}
|
||
|
|
||
|
/*----------------------------------------------------------------------------*/
|
||
|
|
||
|
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 );
|
||
|
//project->handleDND( mouseMove , ctrl , shift , lmb );
|
||
|
// FIXME: D'n'D
|
||
|
} 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 ) {
|
||
|
//project->handleWheel( io.MouseWheel , ctrl , shift );
|
||
|
// FIXME wheel
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void T_Main::makeUI( )
|
||
|
{
|
||
|
}
|
||
|
|
||
|
void T_Main::render( )
|
||
|
{
|
||
|
auto const& dspSize( ImGui::GetIO( ).DisplaySize );
|
||
|
glViewport( 0 , 0 , (int) dspSize.x, (int) dspSize.y );
|
||
|
glClearColor( 1 , 0 , 1 , 1 );
|
||
|
glClear( GL_COLOR_BUFFER_BIT );
|
||
|
|
||
|
// FIXME draw the fuck
|
||
|
//project->render( );
|
||
|
|
||
|
glUseProgram( 0 );
|
||
|
ImGui::Render( );
|
||
|
SDL_GL_SwapWindow( window );
|
||
|
}
|
||
|
|
||
|
|
||
|
/*============================================================================*/
|
||
|
|
||
|
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;
|
||
|
}
|