fix
authorhgn <hgodden00@gmail.com>
Thu, 13 Apr 2023 04:58:23 +0000 (05:58 +0100)
committerhgn <hgodden00@gmail.com>
Thu, 13 Apr 2023 04:58:23 +0000 (05:58 +0100)
vg.h

diff --git a/vg.h b/vg.h
index 3377b3147a8813e57ac596603d4146569331730e..141bea392b8c18b16226629f311ab12127b6b99a 100644 (file)
--- a/vg.h
+++ b/vg.h
@@ -1,4 +1,4 @@
-/* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
+/* Copyright (C) 2021-2023 Harry Godden (hgn) - All Rights Reserved */
 
 /*
  
 
 */
 
-#ifndef VG_STATIC
-  #define VG_STATIC 
+#ifdef VG_RELEASE
+ #define VG_STATIC static
+#else
+ #define VG_STATIC
 #endif
 
 /* API */
@@ -205,10 +207,8 @@ VG_STATIC void _vg_ensure_engine_running(void)
    enum engine_status status = vg.engine_status;
    SDL_AtomicUnlock( &vg.sl_context );
 
-   if( status != k_engine_status_running )
-   {
-      while(1) 
-      {
+   if( status != k_engine_status_running ){
+      while(1) {
          VG_SYNC_LOG( "[%d] No longer running...\n");
          SDL_Delay(1000);
       }
@@ -219,13 +219,11 @@ VG_STATIC enum vg_thread_purpose vg_thread_purpose(void)
 {
    SDL_AtomicLock( &vg.sl_context );
 
-   if( vg.thread_id_main == SDL_GetThreadID(NULL) )
-   {
+   if( vg.thread_id_main == SDL_GetThreadID(NULL) ){
       SDL_AtomicUnlock( &vg.sl_context );
       return k_thread_purpose_main;
    }
-   else
-   {
+   else{
       SDL_AtomicUnlock( &vg.sl_context );
       return k_thread_purpose_loader;
    }
@@ -239,14 +237,12 @@ VG_STATIC void vg_acquire_thread_sync(void)
 {
    /* We dont want to do anything if this is the main thread */
 
-   if( vg_thread_purpose() == k_thread_purpose_loader )
-   {
+   if( vg_thread_purpose() == k_thread_purpose_loader ){
       VG_SYNC_LOG( "[%d] vg_acquire_thread_sync()\n" );
       _vg_ensure_engine_running();
 
       SDL_AtomicLock( &vg.sl_context );
-      if( vg.context_ownership_depth == 0 )
-      {
+      if( vg.context_ownership_depth == 0 ){
          vg.context_ownership_depth ++;
          vg.exec_context = 1;
          SDL_AtomicUnlock( &vg.sl_context );
@@ -260,8 +256,7 @@ VG_STATIC void vg_acquire_thread_sync(void)
          SDL_GL_MakeCurrent( vg.window, vg.gl_context );
          VG_SYNC_LOG( "[%d] granted\n" );
       }
-      else
-      {
+      else{
          vg.context_ownership_depth ++;
          VG_SYNC_LOG( "[%d] granted\n" );
          SDL_AtomicUnlock( &vg.sl_context );
@@ -275,15 +270,13 @@ VG_STATIC void vg_acquire_thread_sync(void)
  */
 VG_STATIC void vg_release_thread_sync(void)
 {
-   if( vg_thread_purpose() == k_thread_purpose_loader )
-   {
+   if( vg_thread_purpose() == k_thread_purpose_loader ){
       VG_SYNC_LOG( "[%d] vg_release_thread_sync()\n" );
 
       SDL_AtomicLock( &vg.sl_context );
       vg.context_ownership_depth --;
 
-      if( vg.context_ownership_depth == 0 )
-      {
+      if( vg.context_ownership_depth == 0 ){
          SDL_AtomicUnlock( &vg.sl_context );
          VG_SYNC_LOG( "[%d] Releasing context.\n" );
          SDL_GL_MakeCurrent( NULL, NULL );
@@ -298,8 +291,7 @@ VG_STATIC void _vg_run_synced(void)
 {
    SDL_AtomicLock( &vg.sl_context );
 
-   if( vg.exec_context != 0 )
-   {
+   if( vg.exec_context != 0 ){
       VG_SYNC_LOG( "[%d] _vg_run_synced() (%d).\n", vg.exec_context );
       vg.exec_context = 0;
       SDL_AtomicUnlock( &vg.sl_context );
@@ -319,8 +311,7 @@ VG_STATIC void _vg_run_synced(void)
       VG_SYNC_LOG( "[%d] Re-engaging.\n" );
       SDL_GL_MakeCurrent( vg.window, vg.gl_context );
    }
-   else
-   {
+   else{
       VG_SYNC_LOG( "[%d] Nothing to do.\n" );
       SDL_AtomicUnlock( &vg.sl_context );
    }
@@ -357,8 +348,7 @@ VG_STATIC void vg_checkgl( const char *src_info )
    int fail = 0;
 
    GLenum err;
-   while( (err = glGetError()) != GL_NO_ERROR )
-   {
+   while( (err = glGetError()) != GL_NO_ERROR ){
       vg_error( "(%s) OpenGL Error: #%d\n", src_info, err );
       fail = 1;
    }
@@ -402,14 +392,11 @@ VG_STATIC void _vg_process_events(void)
 
    /* SDL event loop */
    SDL_Event event;
-   while( SDL_PollEvent( &event ) )
-   {
-      if( event.type == SDL_KEYDOWN )
-      {
+   while( SDL_PollEvent( &event ) ){
+      if( event.type == SDL_KEYDOWN ){
          console_proc_key( event.key.keysym );
       }
-      else if( event.type == SDL_MOUSEWHEEL )
-      {
+      else if( event.type == SDL_MOUSEWHEEL ){
          vg.mouse_wheel[0] += event.wheel.preciseX;
          vg.mouse_wheel[1] += event.wheel.preciseY;
       }
@@ -422,38 +409,31 @@ VG_STATIC void _vg_process_events(void)
       {
          vg_input_controller_event( &event );
       }
-      else if( event.type == SDL_MOUSEMOTION )
-      {
+      else if( event.type == SDL_MOUSEMOTION ){
          vg.mouse_delta[0] += event.motion.xrel;
          vg.mouse_delta[1] += event.motion.yrel;
       }
-      else if( event.type == SDL_WINDOWEVENT )
-      {
-         if( event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED )
-         {
+      else if( event.type == SDL_WINDOWEVENT ){
+         if( event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED ){
             int w, h;
             SDL_GL_GetDrawableSize( vg.window, &w, &h );
 
-            if( !w || !h )
-            {
+            if( !w || !h ){
                vg_warn( "Got a invalid framebuffer size: "
                         "%dx%d... ignoring\n", w, h );
             }
-            else
-            {
+            else{
                vg.window_x = w;
                vg.window_y = h;
 
                vg_framebuffer_resize(w,h);
             }
          }
-         else if( event.window.event == SDL_WINDOWEVENT_CLOSE )
-         {
+         else if( event.window.event == SDL_WINDOWEVENT_CLOSE ){
             vg.window_should_close = 1;
          }
       }
-      else if( event.type == SDL_TEXTINPUT )
-      {
+      else if( event.type == SDL_TEXTINPUT ){
          console_proc_utf8( event.text.text );
       }
    }
@@ -478,8 +458,7 @@ VG_STATIC void _vg_gameloop_update(void)
 
    vg.fixed_iterations = 0;
    vg_lines.allow_input = 1;
-   while( vg.accumulator >= (VG_TIMESTEP_FIXED-0.00125) )
-   {
+   while( vg.accumulator >= (VG_TIMESTEP_FIXED-0.00125) ){
       vg_update_fixed();
       vg_lines.allow_input = 0;
 
@@ -487,8 +466,7 @@ VG_STATIC void _vg_gameloop_update(void)
       vg.accumulator  = VG_MAX( 0.0, vg.accumulator );
 
       vg.fixed_iterations ++;
-      if( vg.fixed_iterations == 8 )
-      {
+      if( vg.fixed_iterations == 8 ){
          break;
       }
    }
@@ -503,8 +481,7 @@ VG_STATIC void _vg_gameloop_render(void)
 {
    vg_profile_begin( &vg_prof_render );
 
-   if( vg.is_loaded )
-   {
+   if( vg.is_loaded ){
       /* render */
       vg.engine_stage = k_engine_stage_rendering;
       vg_render();
@@ -523,9 +500,7 @@ VG_STATIC void _vg_gameloop_render(void)
                (ui_rect){ 4, 4, 250, 0 }, 0
          );
 
-         if( vg_profiler )
-         {
-            
+         if( vg_profiler ){
             char perf[128];
             
             snprintf( perf, 127, 
@@ -560,8 +535,7 @@ VG_STATIC void _vg_gameloop(void)
    vg.accumulator = 0.75f * (1.0f/60.0f);
 
    int post_start = 0;
-   while(1)
-   {
+   while(1){
       _vg_process_events();
 
       if( vg.window_should_close )
@@ -571,16 +545,13 @@ VG_STATIC void _vg_gameloop(void)
       vg.time_delta = vg.frame_delta * vg.time_rate;
       vg.time += vg.time_delta;
       
-      if( vg.is_loaded )
-      {
-         if( !post_start )
-         {
+      if( vg.is_loaded ){
+         if( !post_start ){
             vg_start();
             post_start = 1;
          }
       }
-      else
-      {
+      else{
          _vg_loader_render();
       }
 
@@ -595,30 +566,24 @@ VG_STATIC void _vg_gameloop(void)
 VG_STATIC void _vg_process_launch_opts_internal( int argc, char *argv[] )
 {
    char *arg;
-   while( vg_argp( argc, argv ) )
-   {
-      if( (arg = vg_opt_arg( 'w' )) )
-      {
+   while( vg_argp( argc, argv ) ){
+      if( (arg = vg_opt_arg( 'w' )) ){
          vg.window_x = atoi( arg );
       }
 
-      if( (arg = vg_opt_arg( 'h' )) )
-      {
+      if( (arg = vg_opt_arg( 'h' )) ){
          vg.window_y = atoi( arg );
       }
 
-      if( (arg = vg_long_opt_arg( "samples" )) )
-      {
+      if( (arg = vg_long_opt_arg( "samples" )) ){
          vg.samples = VG_MAX( 0, VG_MIN( 8, atoi( arg ) ) );
       }
 
-      if(  vg_long_opt( "use-libc-malloc" ) )
-      {
+      if(  vg_long_opt( "use-libc-malloc" ) ){
          vg_mem.use_libc_malloc = 1;
       }
 
-      if( vg_long_opt( "high-performance" ) )
-      {
+      if( vg_long_opt( "high-performance" ) ){
          vg.quality_profile = k_quality_profile_low;
       }
 
@@ -732,20 +697,17 @@ VG_STATIC void _vg_init_window( const char *window_name )
    /* 
     * OpenGL loading 
     */
-   if( (vg.gl_context = SDL_GL_CreateContext(vg.window) ))
-   {
+   if( (vg.gl_context = SDL_GL_CreateContext(vg.window) )){
       SDL_GL_GetDrawableSize( vg.window, &vg.window_x, &vg.window_y );
       vg_success( "Window created (%dx%d)\n", vg.window_x, vg.window_y );
    }
-   else
-   {
+   else{
       vg_error( "SDL_GL_CreateContext failed: %s\n", SDL_GetError() );
       SDL_Quit();
       exit(0);
    }
 
-   if( !gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress) ) 
-   {
+   if( !gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress) ) {
       vg_error( "Glad Failed to initialize\n" );
       SDL_GL_DeleteContext( vg.gl_context );
       SDL_Quit();
@@ -757,12 +719,10 @@ VG_STATIC void _vg_init_window( const char *window_name )
 
    vg_info( "Setting swap interval\n" );
 
-   if( SDL_GL_SetSwapInterval( -1 ) == -1 )
-   {
+   if( SDL_GL_SetSwapInterval( -1 ) == -1 ){
       vg_warn( "Adaptive Vsync not supported\n" );
 
-      if( SDL_GL_SetSwapInterval( 1 ) == -1 )
-      {
+      if( SDL_GL_SetSwapInterval( 1 ) == -1 ){
          vg_fatal_exit_loop( "Cannot enable Vsync! You might be overriding it"
                              " in your graphics control panel.\n" );
       }
@@ -773,10 +733,8 @@ VG_STATIC void _vg_init_window( const char *window_name )
       vg_success( "Using adaptive Vsync\n" );
 
    SDL_DisplayMode dispmode;
-   if( !SDL_GetWindowDisplayMode( vg.window, &dispmode ) )
-   {
-      if( dispmode.refresh_rate )
-      {
+   if( !SDL_GetWindowDisplayMode( vg.window, &dispmode ) ){
+      if( dispmode.refresh_rate ){
          vg.refresh_rate = dispmode.refresh_rate;
          vg_info( "Refresh rate: %d\n", dispmode.refresh_rate );
       }
@@ -832,8 +790,7 @@ void vg_print_backtrace(void)
    size = backtrace( array, 20 );
    strings = backtrace_symbols( array, size );
 
-   if( strings != NULL )
-   {
+   if( strings != NULL ){
       vg_error( "---------------- gnu backtrace -------------\n" );
 
       for( int i=0; i<size; i++ )
@@ -867,15 +824,13 @@ VG_STATIC void vg_fatal_exit_loop( const char *error )
    vg_error( "Fatal error: %s\n", error );
 
    SDL_AtomicLock( &vg.sl_context );
-   if( vg.engine_status == k_engine_status_none )
-   {
+   if( vg.engine_status == k_engine_status_none ){
       SDL_AtomicUnlock( &vg.sl_context );
 
       /* TODO: Correct shutdown before other systems */
       exit(0);
    }
-   else
-   {
+   else{
       SDL_AtomicUnlock( &vg.sl_context );
       
       vg_acquire_thread_sync();
@@ -886,8 +841,7 @@ VG_STATIC void vg_fatal_exit_loop( const char *error )
       SDL_AtomicUnlock( &vg.sl_context );
 
       /* Notify other thread for curtosey */
-      if( vg_thread_purpose() == k_thread_purpose_main )
-      {
+      if( vg_thread_purpose() == k_thread_purpose_main ){
          SDL_AtomicLock( &vg.sl_context );
 
          if( vg.exec_context != 0 )
@@ -898,8 +852,7 @@ VG_STATIC void vg_fatal_exit_loop( const char *error )
 
       vg_audio_free();
 
-      while(1)
-      {
+      while(1){
          _vg_process_events();
          if( vg.window_should_close )
             break;