more message changes
[vg.git] / vg.h
diff --git a/vg.h b/vg.h
index a89d750d80aaf9c7cbad96a188e08d85dcf38cd5..2a52614b5fc2803c56c0e328edae5e88d580a507 100644 (file)
--- a/vg.h
+++ b/vg.h
@@ -52,6 +52,8 @@
 #ifndef VG_HEADER_H
   #define VG_HEADER_H
 
+  const char *vg_get_basepath(void);
+
   #include "vg_platform.h"
   #include "vg_mem.h"
 
@@ -86,7 +88,7 @@ VG_STATIC void vg_print_backtrace(void)
   
   #ifdef VG_GAME
     #include "dep/glad/glad.h"
-    #include "submodules/SDL/include/SDL.h"
+    #include "dep/sdl/include/SDL.h"
     #include "vg_stdint.h"
     
     void vg_register_exit( void( *funcptr )(void), const char *name );
@@ -95,7 +97,6 @@ VG_STATIC void vg_print_backtrace(void)
     #include "vg_io.h"
     #include "vg_log.h"
     #include "vg_steam.h"
-    #include <setjmp.h>
 
   //#define VG_SYNC_DEBUG
   #ifdef VG_SYNC_DEBUG
@@ -122,24 +123,23 @@ VG_STATIC void vg_update_fixed(void);
 VG_STATIC void vg_update_post(void);
 
 VG_STATIC void vg_render(void);
-VG_STATIC void vg_ui(void);
+VG_STATIC void vg_gui(void);
 
-struct vg
-{
+struct vg{
    /* Engine sync */
    SDL_Window     *window;
    SDL_GLContext  gl_context;
+   const char     *base_path;
 
    SDL_sem *sem_loader;        /* allows only one loader at a time */
-
    jmp_buf env_loader_exit;
 
    SDL_threadID  thread_id_main,
                  thread_id_loader;
+   void         *thread_data;
 
    SDL_SpinLock sl_status;
-   enum engine_status
-   {
+   enum engine_status{
       k_engine_status_none,
       k_engine_status_load_internal,
       k_engine_status_running,
@@ -156,8 +156,7 @@ struct vg
    int display_refresh_rate,
        fps_limit;             /* 0: use vsync, >0: cap fps to this, no vsync */
 
-   enum vsync_feature
-   {
+   enum vsync_feature{
       k_vsync_feature_disabled=0,
       k_vsync_feature_enabled=1,
       k_vsync_feature_enabled_adaptive=2,
@@ -182,8 +181,7 @@ struct vg
 
    int fixed_iterations;
 
-   enum engine_stage
-   {
+   enum engine_stage{
       k_engine_stage_none,
       k_engine_stage_update,
       k_engine_stage_update_fixed,
@@ -194,8 +192,7 @@ struct vg
 
    /* graphics */
    m4x4f pv;
-   enum quality_profile
-   {
+   enum quality_profile{
       k_quality_profile_high = 0,
       k_quality_profile_low = 1,
    }
@@ -204,6 +201,9 @@ struct vg
    float loader_ring;
 }
 VG_STATIC vg = { .time_rate = 1.0 };
+const char *vg_get_basepath(void){
+   return vg.base_path;
+}
 
 enum vg_thread_purpose
 {
@@ -252,7 +252,6 @@ VG_STATIC void vg_checkgl( const char *src_info );
 #include "vg_shader.h"
 #include "vg_tex.h"
 #include "vg_input.h"
-#include "vg_ui.h"
 #include "vg_imgui.h"
 #include "vg_lines.h"
 #include "vg_loader.h"
@@ -304,7 +303,7 @@ void async_internal_complete( void *payload, u32 size )
    SDL_AtomicUnlock( &vg.sl_status );
 }
 
-VG_STATIC void _vg_load_full(void)
+VG_STATIC void _vg_load_full( void *data )
 {
    vg_preload();
 
@@ -332,7 +331,33 @@ VG_STATIC void _vg_process_events(void)
    SDL_Event event;
    while( SDL_PollEvent( &event ) ){
       if( event.type == SDL_KEYDOWN ){
-         console_proc_key( event.key.keysym );
+         if( vg_console.enabled && 
+               (vg_ui.focused_control_type != k_ui_control_modal) ){
+            if( event.key.keysym.sym == SDLK_ESCAPE ||
+                event.key.keysym.sym == SDLK_BACKQUOTE ){
+               vg_console.enabled = 0;
+               ui_defocus_all();
+            }
+            else if( (event.key.keysym.mod & KMOD_CTRL) && 
+                      event.key.keysym.sym == SDLK_n ){
+               _console_suggest_next();
+            }
+            else if( (event.key.keysym.mod & KMOD_CTRL ) &&
+                      event.key.keysym.sym == SDLK_p ){
+               _console_suggest_prev();
+            }
+            else{
+               _ui_proc_key( event.key.keysym );
+            }
+         }
+         else{
+            if( event.key.keysym.sym == SDLK_BACKQUOTE ){
+               vg_console.enabled = 1;
+            }
+            else {
+               _ui_proc_key( event.key.keysym );
+            }
+         }
       }
       else if( event.type == SDL_MOUSEWHEEL ){
          vg.mouse_wheel[0] += event.wheel.preciseX;
@@ -374,7 +399,7 @@ VG_STATIC void _vg_process_events(void)
          }
       }
       else if( event.type == SDL_TEXTINPUT ){
-         console_proc_utf8( event.text.text );
+         ui_proc_utf8( event.text.text );
       }
    }
 
@@ -428,27 +453,27 @@ VG_STATIC void _vg_gameloop_render(void)
    vg.engine_stage = k_engine_stage_ui;
    {
       ui_prerender();
-      vg_ui();
-      ui_postrender();
-#if 0
-      ui_begin( vg.window_x, vg.window_y );
+      if( vg_console.enabled ){ 
+         vg_ui.ignore_input_frames = 10;
+         vg_gui();
+         vg_ui.ignore_input_frames = 0;
+         vg_ui.wants_mouse = 1;
+         _vg_console_draw();
+      }
+      else vg_gui();
 
-      /* TODO */
-      ui_set_mouse( vg.mouse_pos[0], vg.mouse_pos[1], 0 );
+      /* vg tools */
+      audio_debug_ui( vg.pv );
 
+               /* profiling */
       int frame_target = vg.display_refresh_rate;
-
-      if( vg.fps_limit > 0 ){
-         frame_target = vg.fps_limit;
-      }
-      
+      if( vg.fps_limit > 0 ) frame_target = vg.fps_limit;
       vg_profile_drawn( 
             (struct vg_profile *[]){
                &vg_prof_update,&vg_prof_render,&vg_prof_swap}, 3,
             (1.0f/(float)frame_target)*1000.0f, 
             (ui_rect){ 4, 4, 250, 0 }, 0
       );
-
       if( vg_profiler ){
          char perf[256];
          
@@ -458,11 +483,7 @@ VG_STATIC void _vg_gameloop_render(void)
                "samples: %d\n"
                "iterations: %d (acc: %.3fms%%)\n"
                "time: real(%.2f) delta(%.2f) rate(%.2f)\n"
-#ifdef _WIN32
-               "      extrap(%.2f) frame(%.2f) spin( %llu )\n",
-#else
-               "      extrap(%.2f) frame(%.2f) spin( %lu )\n",
-#endif
+               "      extrap(%.2f) frame(%.2f) spin( "PRINTF_U64" )\n",
                vg.window_x, vg.window_y, 
                frame_target, (1.0f/(float)frame_target)*1000.0f,
                vg.samples, 
@@ -472,17 +493,9 @@ VG_STATIC void _vg_gameloop_render(void)
                vg.time_fixed_extrapolate, vg.time_frame_delta,
                vg.time_spinning );
 
-         ui_text( (ui_rect){258, 4+24+12+12,0,0},perf, 1,0);
+         ui_text( (ui_rect){258, 4+24+12+12,900,900},perf,1,0,k_ui_align_left);
       }
-
-      /* FIXME */
-      audio_debug_ui( vg.pv );
-      vg_ui();
-      _vg_console_draw();
-      
-      ui_resolve();
-      ui_draw( NULL );
-#endif
+      ui_postrender();
    }
 
    vg_profile_end( &vg_prof_render );
@@ -676,6 +689,15 @@ VG_STATIC void _vg_init_window( const char *window_name )
    SDL_InitSubSystem( SDL_INIT_AUDIO );
    SDL_InitSubSystem( SDL_INIT_GAMECONTROLLER );
 
+   char *exe_basepath = SDL_GetBasePath();
+   u32 len = vg_align8( strlen(exe_basepath)+1 );
+   char *dest  = vg_linear_alloc( vg_mem.rtmemory, len );
+   strcpy( dest, exe_basepath );
+   SDL_free( exe_basepath );
+   vg.base_path = dest;
+
+   vg_info( "Basepath: %s\n", vg.base_path );
+
    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
    SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 3 );
    SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 3 );
@@ -730,7 +752,7 @@ VG_STATIC void _vg_init_window( const char *window_name )
 
 #ifdef VG_DEVWINDOW
          0, 0, vg.window_x, vg.window_y, 
-         SDL_WINDOW_BORDERLESS|SDL_WINDOW_OPENGL
+         SDL_WINDOW_BORDERLESS|SDL_WINDOW_OPENGL|SDL_WINDOW_INPUT_GRABBED
       ))){}
 #else
                                      0, 0,
@@ -757,6 +779,9 @@ VG_STATIC void _vg_init_window( const char *window_name )
 
    vg_info( "CreateContext\n" );
 
+   /* ????? */
+   if( SDL_IsTextInputActive() ) SDL_StopTextInput();
+
    /* 
     * OpenGL loading 
     */
@@ -795,11 +820,9 @@ VG_STATIC void _vg_init_window( const char *window_name )
 
    vg_info( "Display refresh rate: %d\n", dispmode.refresh_rate );
 
-#ifdef _WIN32
+#if defined(_WIN32) || defined(VG_DEVWINDOW)
    vg.fps_limit = vg.display_refresh_rate;
 #else
-   /* request vsync by default on linux to avoid screen tearing.
-    * this does have its own issues with compositing on X11. */
    vg.fps_limit = 0;
 #endif
 }
@@ -824,6 +847,7 @@ VG_STATIC void _vg_terminate(void)
 
 VG_STATIC void vg_enter( int argc, char *argv[], const char *window_name )
 {
+   vg_rand_seed( 461 );
    _vg_process_launch_opts_internal( argc, argv );
 
    /* Systems init */
@@ -834,10 +858,7 @@ VG_STATIC void vg_enter( int argc, char *argv[], const char *window_name )
    _vg_init_window( window_name );
 
    vg_async_init();
-   
-#ifndef VG_DEVWINDOW
    SDL_SetRelativeMouseMode(1);
-#endif
 
    vg.thread_id_main = SDL_GetThreadID(NULL);
    
@@ -848,7 +869,7 @@ VG_STATIC void vg_enter( int argc, char *argv[], const char *window_name )
    vg.engine_status = k_engine_status_load_internal;
 
    _vg_opengl_sync_init();
-    vg_loader_start( _vg_load_full );
+    vg_loader_start( _vg_load_full, NULL );
    _vg_gameloop();
    _vg_terminate();
 }