scoreboard stuff
authorhgn <hgodden00@gmail.com>
Fri, 3 Nov 2023 02:41:36 +0000 (02:41 +0000)
committerhgn <hgodden00@gmail.com>
Fri, 3 Nov 2023 02:41:36 +0000 (02:41 +0000)
world_sfd.c
world_sfd.h

index bf41bf26b172edb46676455be7668d7b3cf23d6e..6d663dec54dc348e569bc39b420e07d5c7fdbf2a 100644 (file)
@@ -42,41 +42,59 @@ static f32 sfd_encode_glyph( char c ){
    return (float)value;
 }
 
-static void sfd_encode( u32 row, const char *str ){
-   int end=0;
+static void sfd_clear( u32 row ){
    u32 row_h = world_sfd.h -1 -row;
-
    for( int i=0; i<world_sfd.w; i++ ){
       u32 idx = (world_sfd.w*row_h + i) * 2;
+      world_sfd.buffer[idx] = 0.0f;
+   }
+}
 
-      if( end ){
-         world_sfd.buffer[idx] = 0.0f;
-      }
-      else{
-         if( !str[i] )
-            end = 1;
+static void sfd_encode( v2i co, const char *str, enum world_sfd_align align ){
+   i32 row_h = world_sfd.h -1 -co[1];
+   i32 offset_x = 0;
 
-         world_sfd.buffer[idx] = sfd_encode_glyph( str[i] );
-      }
+   i32 w = VG_MIN( strlen(str), world_sfd.w );
+   if( align == k_world_sfd_center )
+      offset_x = (world_sfd.w - w) / 2;
+   else if( align == k_world_sfd_right )
+      offset_x = world_sfd.w - w;
+   else
+      offset_x = co[0];
+
+   for( i32 i=0; i<world_sfd.w; i++ ){
+      i32 u = i + offset_x,
+          idx = (world_sfd.w*row_h + u) * 2;
+
+      if( (u >= world_sfd.w) || (u < 0) )
+         continue;
+
+      if( !str[i] ) 
+         return;
+
+      world_sfd.buffer[idx] = sfd_encode_glyph( str[i] );
    }
 }
 
-static void world_sfd_compile_scores( struct leaderboard_cache *board ){
+static void world_sfd_compile_scores( struct leaderboard_cache *board,
+                                      const char *title ){
    for( u32 i=0; i<13; i++ )
-      sfd_encode( i, "" );
+      sfd_clear(i);
+
+   sfd_encode( (v2i){0,0}, title, k_world_sfd_left );
 
    if( !board ){
-      sfd_encode( 4, "Error out of range" );
+      sfd_encode( (v2i){-1,4}, "Error out of range", k_world_sfd_center );
       return;
    }
 
    if( !network_connected() ){
-      sfd_encode( 4, "Offline" );
+      sfd_encode( (v2i){-1,0}, "Offline", k_world_sfd_right );
       return;
    }
 
    if( board->status == k_request_status_not_found ){
-      sfd_encode( 4, "No records" );
+      sfd_encode( (v2i){-1,4}, "No records", k_world_sfd_center );
       return;
    }
 
@@ -86,7 +104,7 @@ static void world_sfd_compile_scores( struct leaderboard_cache *board ){
       vg_strnull( &s, buf, 32 );
       vg_strcat( &s, "Error: " );
       vg_strcati32( &s, board->status );
-      sfd_encode( 4, buf );
+      sfd_encode( (v2i){-1,4}, buf, k_world_sfd_center );
       return;
    }
 
@@ -97,27 +115,54 @@ static void world_sfd_compile_scores( struct leaderboard_cache *board ){
 
    if( world_sfd.view_weekly ){
       alias = "rows_weekly";
-      sfd_encode( 0, "Weekly" );
+      sfd_encode( (v2i){-1,0}, "Weekly", k_world_sfd_right );
    }
    else {
-      sfd_encode( 0, "All-Time" );
+      sfd_encode( (v2i){-1,0}, "All-Time", k_world_sfd_right );
    }
 
    u32 l = 1;
    if( vg_msg_seekframe( &body, alias ) ){
       while( vg_msg_seekframe( &body, NULL ) ){
+         /* name */
          const char *username = vg_msg_getkvstr( &body, "username" );
 
+         char buf[100];
+         vg_str str;
+         vg_strnull( &str, buf, 100 );
+         vg_strcati32( &str, l );
+         vg_strcat( &str, " " );
+
          if( username )
-            sfd_encode( l ++, username );
+            vg_strcat( &str, username );
          else
-            sfd_encode( l ++, "UNKNOWN USER" );
+            vg_strcat( &str, "??????" );
+
+         sfd_encode( (v2i){0,l}, str.buffer, k_world_sfd_left );
+
+         /* time */
+         vg_strnull( &str, buf, 100 );
+         i32 centiseconds = vg_msg_getkvi32( &body, "time", 0 ),
+             seconds      = centiseconds / 100,
+             minutes      = seconds / 60;
+
+         centiseconds %= 100;
+         seconds     %= 60;
+         minutes     %= 60;
+         if( minutes > 9 ) vg_strcat( &str, "?" );
+         else vg_strcati32( &str, minutes );
+         vg_strcat( &str, ":" );
+         vg_strcati32r( &str, seconds, 2, '0' );
+         vg_strcat( &str, "." );
+         vg_strcati32r( &str, centiseconds, 2, '0' );
+         sfd_encode( (v2i){-1,l}, str.buffer, k_world_sfd_right );
+         l ++;
 
          vg_msg_skip_frame( &body );
       }
    }
    else {
-      sfd_encode( 4, "No records" );
+      sfd_encode( (v2i){-1,4}, "No records", k_world_sfd_center );
    }
 }
 
@@ -125,11 +170,16 @@ static void world_sfd_compile_active_scores(void){
    world_instance *world = world_current_instance();
    
    struct leaderboard_cache *board = NULL;
+   const char *name = "Out of range";
 
-   if( world_sfd.active_route_board < mdl_arrcount( &world->ent_route ) )
+   if( world_sfd.active_route_board < mdl_arrcount( &world->ent_route ) ){
       board = &world->leaderboard_cache[ world_sfd.active_route_board ];
+      ent_route *route = mdl_arritm( &world->ent_route, 
+                                     world_sfd.active_route_board );
+      name = mdl_pstr( &world->meta, route->pstr_name );
+   }
          
-   world_sfd_compile_scores( board );
+   world_sfd_compile_scores( board, name );
 }
 
 static void world_sfd_update( world_instance *world, v3f pos ){
@@ -185,8 +235,8 @@ static void world_sfd_update( world_instance *world, v3f pos ){
       
       if( fabsf(d1) > rate ){
          *cur += rate;
-         if( *cur > 60.0f )
-            *cur -= 60.0f;
+         if( *cur > 49.0f )
+            *cur -= 49.0f;
       }
       else
          *cur = *target;
@@ -240,20 +290,9 @@ static void sfd_render( world_instance *world, camera *cam, m4x3f transform ){
    mdl_draw_submesh( &world_sfd.sm_base );
 }
 
-static int world_sfd_test( int argc, const char *argv[] ){
-   if( argc == 2 ){
-      int row = vg_min( vg_max(atoi(argv[0]),0), world_sfd.h);
-      sfd_encode( row, argv[1] );
-   }
-
-   return 0;
-}
-
 static void world_sfd_init(void){
    vg_info( "world_sfd_init\n" );
    shader_scene_scoretext_register();
-   vg_console_reg_cmd( "sfd", world_sfd_test, NULL );
-
    vg_linear_clear( vg_mem.scratch );
 
    mdl_context mscoreboard;
index 19c867c9a28ecc447b4f5fe901feb025c60b0898..9e8d38c14a88ad50d8b9d904eb60bdc93f519479 100644 (file)
@@ -25,10 +25,17 @@ struct world_sfd{
 static world_sfd;
 static void world_sfd_init(void);
 
-static void sfd_encode( u32 row, const char *str );
+enum world_sfd_align {
+   k_world_sfd_left,
+   k_world_sfd_right,
+   k_world_sfd_center
+};
+
+static void sfd_encode( v2i co, const char *str, enum world_sfd_align align );
 static void sfd_render( world_instance *world, camera *cam, 
                            m4x3f transform );
-static void world_sfd_compile_scores( struct leaderboard_cache *leaderboard );
+static void world_sfd_compile_scores( struct leaderboard_cache *leaderboard,
+                                      const char *title );
 static void world_sfd_compile_active_scores(void);
 
 #endif /* SFD_H */