move location of main camera to g_render
[carveJwlIkooP6JGAAIwe30JlM.git] / player_remote.c
index 2234c6dc994aa3f50b8fdc0cf217603762dd551a..e8ca56c62cb65d4e98ccfccc8bfe368f6f0687dc 100644 (file)
@@ -1,16 +1,22 @@
 #include "player_remote.h"
 #include "skeleton.h"
 #include "player_render.h"
+#include "player_api.h"
 #include "network_common.h"
 #include "addon.h"
 #include "font.h"
 #include "gui.h"
 #include "ent_miniworld.h"
 #include "ent_region.h"
+#include "shaders/model_entity.h"
+#include "vg/vg_steam_friends.h"
+
+struct global_netplayers netplayers;
 
 static i32 k_show_own_name = 0;
 
-static void player_remote_clear( struct network_player *player ){
+static void player_remote_clear( struct network_player *player )
+{
    addon_cache_unwatch( k_addon_type_player, player->playermodel_view_slot );
    addon_cache_unwatch( k_addon_type_board, player->board_view_slot );
 
@@ -52,7 +58,8 @@ static void relink_remote_player_worlds( u32 client_id ){
  *
  * Run if local worlds change
  */
-static void relink_all_remote_player_worlds(void){
+void relink_all_remote_player_worlds(void)
+{
    for( u32 i=0; i<vg_list_size(netplayers.list); i++ ){
       struct network_player *player = &netplayers.list[i];
       if( player->active )
@@ -60,7 +67,8 @@ static void relink_all_remote_player_worlds(void){
    }
 }
 
-static void player_remote_update_friendflags( struct network_player *remote ){
+void player_remote_update_friendflags( struct network_player *remote )
+{
    ISteamFriends *hSteamFriends = SteamAPI_SteamFriends();
    remote->isfriend = SteamAPI_ISteamFriends_HasFriend( hSteamFriends,
                         remote->steamid, k_EFriendFlagImmediate );
@@ -68,7 +76,8 @@ static void player_remote_update_friendflags( struct network_player *remote ){
                         remote->steamid, k_EFriendFlagBlocked );
 }
 
-static void player_remote_rx_200_300( SteamNetworkingMessage_t *msg ){
+void player_remote_rx_200_300( SteamNetworkingMessage_t *msg )
+{
    netmsg_blank *tmp = msg->m_pData;
 
    if( tmp->inetmsg_id == k_inetmsg_playerjoin ){
@@ -139,6 +148,7 @@ static void player_remote_rx_200_300( SteamNetworkingMessage_t *msg ){
       }
       
       netmsg_playerframe *frame = msg->m_pData;
+
       if( frame->client >= vg_list_size(netplayers.list) ){
          vg_error( "inetmsg_playerframe: player index out of range\n" );
          return;
@@ -169,7 +179,7 @@ static void player_remote_rx_200_300( SteamNetworkingMessage_t *msg ){
 
       dest->active = 1;
       dest->subsystem = frame->subsystem;
-      dest->instance_id = frame->instance_id;
+      dest->flags = frame->flags;
 
       bitpack_ctx ctx = {
          .mode = k_bitpack_decompress,
@@ -188,13 +198,11 @@ static void player_remote_rx_200_300( SteamNetworkingMessage_t *msg ){
       struct player_subsystem_interface *sys = 
          player_subsystems[ frame->subsystem ];
 
-      if( sys->network_animator_exchange ){
-         memset( &dest->data, 0, sys->animator_size );
+      memset( &dest->data, 0, sys->animator_size );
+      if( sys->network_animator_exchange )
          sys->network_animator_exchange( &ctx, &dest->data );
-      }
-      else {
+      else 
          bitpack_bytes( &ctx, sys->animator_size, sys->animator_data );
-      }
 
       /* sfx
        * -------------------------------------------------------------*/
@@ -229,6 +237,15 @@ static void player_remote_rx_200_300( SteamNetworkingMessage_t *msg ){
          }
       }
 
+      /* glider
+       * -------------------------------------------------------------*/
+
+      memset( &dest->data_glider, 0, sizeof(struct remote_glider_animator) );
+      if( dest->flags & (NETMSG_PLAYERFRAME_HAVE_GLIDER|
+                         NETMSG_PLAYERFRAME_GLIDER_ORPHAN) ){
+         player_glide_remote_animator_exchange( &ctx, &dest->data_glider );
+      }
+
       player->subsystem = frame->subsystem;
       player->down_bytes += msg->m_cbSize;
    }
@@ -305,7 +322,8 @@ static void player_remote_rx_200_300( SteamNetworkingMessage_t *msg ){
 /*
  * Write localplayer pose to network
  */
-static void remote_player_send_playerframe(void){
+void remote_player_send_playerframe(void)
+{
    u8 sysid = localplayer.subsystem;
    if( sysid >= k_player_subsystem_max ) return;
 
@@ -320,7 +338,7 @@ static void remote_player_send_playerframe(void){
       frame->inetmsg_id = k_inetmsg_playerframe;
       frame->client = 0xff;
       frame->subsystem = localplayer.subsystem;
-      frame->instance_id = world_static.active_instance;
+      frame->flags = world_static.active_instance;
 
       bitpack_ctx ctx = {
          .mode = k_bitpack_compress,
@@ -346,6 +364,25 @@ static void remote_player_send_playerframe(void){
       for( u32 i=0; i<localplayer.sfx_buffer_count; i ++ )
          net_sfx_exchange( &ctx, &localplayer.sfx_buffer[i] );
 
+      /* glider
+       * -------------------------------------------------------------*/
+
+      if( localplayer.have_glider || 
+            (localplayer.subsystem == k_player_subsystem_glide) ) {
+         frame->flags |= NETMSG_PLAYERFRAME_HAVE_GLIDER;
+      }
+
+      if( localplayer.glider_orphan )
+         frame->flags |= NETMSG_PLAYERFRAME_GLIDER_ORPHAN;
+
+      if( frame->flags & (NETMSG_PLAYERFRAME_HAVE_GLIDER|
+                          NETMSG_PLAYERFRAME_GLIDER_ORPHAN) ){
+         player_glide_remote_animator_exchange( &ctx,    
+                                                &player_glide.remote_animator );
+      }
+
+      /* ------- */
+
       u32 wire_size = base_size + ctx.bytes;
       netplayers.up_bytes += wire_size;
 
@@ -359,7 +396,8 @@ static void remote_player_send_playerframe(void){
 /*
  * Updates network traffic stats
  */
-static void remote_player_debug_update(void){
+void remote_player_debug_update(void)
+{
    if( (vg.time_real - netplayers.last_data_measurement) > 1.0 ){
       netplayers.last_data_measurement = vg.time_real;
       u32 total_down = 0;
@@ -382,8 +420,10 @@ static void remote_player_debug_update(void){
 /*
  * Debugging information
  */
-static void remote_player_network_imgui( m4x4f pv ){
-   if( network_client.user_intent == k_server_intent_online ){
+void remote_player_network_imgui( ui_context *ctx, m4x4f pv )
+{
+   if( network_client.user_intent == k_server_intent_online )
+   {
       if( !(steam_ready &&
          (network_client.state == k_ESteamNetworkingConnectionState_Connected)))
       {
@@ -392,7 +432,7 @@ static void remote_player_network_imgui( m4x4f pv ){
          vg_strnull( &str, buf, sizeof(buf) );
          u32 fg = 0;
          network_status_string( &str, &fg );
-         ui_text( (ui_rect){ vg.window_x - 200, 0, 200, 48 }, buf, 1, 
+         ui_text( ctx, (ui_rect){ vg.window_x - 200, 0, 200, 48 }, buf, 1, 
                   k_ui_align_middle_center, fg );
       }
    }
@@ -401,7 +441,13 @@ static void remote_player_network_imgui( m4x4f pv ){
       return;
 
    ui_rect panel = { (vg.window_x / 2) - 200, 0, 400, 600 };
-   ui_fill( panel, (ui_colour(k_ui_bg)&0x00ffffff)|0x50000000 );
+   ui_fill( ctx, panel, (ui_colour(ctx, k_ui_bg)&0x00ffffff)|0x50000000 );
+
+   ctx->font = &vgf_default_title;
+   ui_info( ctx, panel, "Network" );
+   ctx->font = &vgf_default_large;
+   ui_info( ctx, panel, "Status" );
+   ctx->font = &vgf_default_small;
 
    char buf[512];
    const char *netstatus = "PROGRAMMING ERROR";
@@ -424,39 +470,46 @@ static void remote_player_network_imgui( m4x4f pv ){
           { k_ESteamNetworkingConnectionState_Linger, "Linger" },
           { k_ESteamNetworkingConnectionState_Dead, "Dead" }
    };
-   for( u32 i=0; i<vg_list_size(states); i ++ ){
-      if( states[i].state == network_client.state ){
+   for( u32 i=0; i<vg_list_size(states); i ++ )
+   {
+      if( states[i].state == network_client.state )
+      {
          netstatus = states[i].str;
          break;
       }
    }
    snprintf( buf, 512, "Network: %s", netstatus );
-   ui_info( panel, buf );
-   ui_info( panel, "---------------------" );
+   ui_info( ctx, panel, buf );
+   ui_info( ctx, panel, "---------------------" );
 
-   if( network_client.state == k_ESteamNetworkingConnectionState_Connected ){
-      ui_info( panel, "#-1: localplayer" );
+   if( network_client.state == k_ESteamNetworkingConnectionState_Connected )
+   {
+      ui_info( ctx, panel, "#-1: localplayer" );
       
       snprintf( buf, 512, "U%.3f/D%.3fkbs", 
                 netplayers.up_kbs, netplayers.down_kbs );
-      ui_info( panel, buf );
+      ui_info( ctx, panel, buf );
 
-      for( u32 i=0; i<vg_list_size(netplayers.list); i++ ){
+      for( u32 i=0; i<vg_list_size(netplayers.list); i++ )
+      {
          struct network_player *player = &netplayers.list[i];
-         if( player->active ){
+         if( player->active )
+         {
             const char *sysname = "invalid";
 
-            if( player->subsystem < k_player_subsystem_max ){
+            if( player->subsystem < k_player_subsystem_max )
+            {
                sysname = player_subsystems[ player->subsystem ]->name;
             }
             snprintf( buf, 512, "#%u: %s [%s] D%.1fkbs", 
                       i, player->username, sysname, player->down_kbs );
-            ui_info( panel, buf );
+            ui_info( ctx, panel, buf );
          }
       }
    }
-   else {
-      ui_info( panel, "offline" );
+   else 
+   {
+      ui_info( ctx, panel, "offline" );
    }
 }
 
@@ -490,9 +543,11 @@ static void pose_remote_player( u32 index,
 
    u8 instance_id = 0;
 
+   f32 t = 0.0f;
+
    if( f1 ){
-      f32 t = (buf->t - f0->timestamp) / (f1->timestamp - f0->timestamp);
-          t = vg_clampf( t, 0.0f, 1.0f );
+      t = (buf->t - f0->timestamp) / (f1->timestamp - f0->timestamp);
+      t = vg_clampf( t, 0.0f, 1.0f );
 
       sys1 = player_subsystems[f1->subsystem];
       sys1->pose( &f1->data, &pose1 );
@@ -507,7 +562,7 @@ static void pose_remote_player( u32 index,
          t = 1.0f;
       }
 
-      instance_id = f1->instance_id;
+      instance_id = f1->flags & NETMSG_PLAYERFRAME_INSTANCE_ID;
       lerp_player_pose( &pose0, &pose1, t, &posed );
       effect_blink_apply( &player->effect_data.blink, &posed, vg.time_delta );
 
@@ -525,7 +580,7 @@ static void pose_remote_player( u32 index,
       memcpy( board_pose, &posed.board, sizeof(*board_pose) );
    }
    else {
-      instance_id = f0->instance_id;
+      instance_id = f0->flags & NETMSG_PLAYERFRAME_INSTANCE_ID;
       effect_blink_apply( &player->effect_data.blink, &pose0, vg.time_delta );
       apply_full_skeleton_pose( sk, &pose0, final_mtx );
       if( sys0->effects ) 
@@ -533,6 +588,33 @@ static void pose_remote_player( u32 index,
       memcpy( board_pose, &pose0.board, sizeof(*board_pose) );
    }
 
+   if( f0->flags & (NETMSG_PLAYERFRAME_HAVE_GLIDER|
+                    NETMSG_PLAYERFRAME_GLIDER_ORPHAN) ){
+      player->render_glider = 1;
+
+      v3f co;
+      v4f q;
+      f32 s;
+
+      if( f1 ){
+         v3_lerp( f0->data_glider.root_co, f1->data_glider.root_co, t, co );
+         q_nlerp( f0->data_glider.root_q,  f1->data_glider.root_q,  t, q );
+         s = vg_lerpf( f0->data_glider.s, f1->data_glider.s, t );
+      }
+      else {
+         v3_copy( f0->data_glider.root_co, co );
+         v4_copy( f0->data_glider.root_q, q );
+         s = f0->data_glider.s;
+      }
+
+      v3f *mtx = netplayers.glider_mtx[ index ];
+      q_m3x3( q, mtx );
+      m3x3_scalef( mtx, s );
+      v3_copy( co, mtx[3] );
+   }
+   else
+      player->render_glider = 0;
+
    if( player->world_match[ instance_id ] )
       player->active_world = &world_static.instances[ instance_id ];
 }
@@ -540,8 +622,8 @@ static void pose_remote_player( u32 index,
 /* 
  * animate remote player and store in final_mtx
  */
-static void animate_remote_player( u32 index ){
-
+void animate_remote_player( u32 index )
+{
    /*
     * Trys to keep the cursor inside the buffer
     */
@@ -596,7 +678,8 @@ static void animate_remote_player( u32 index ){
 /*
  * Update full final_mtx for all remote players
  */
-static void animate_remote_players(void){
+void animate_remote_players(void)
+{
    for( u32 i=0; i<vg_list_size(netplayers.list); i ++ ){
       struct network_player *player = &netplayers.list[i];
       if( !player->active ) continue;
@@ -608,18 +691,37 @@ static void animate_remote_players(void){
 /*
  * Draw remote players
  */
-static void render_remote_players( world_instance *world, camera *cam ){
-   SDL_AtomicLock( &addon_system.sl_cache_using_resources );
-   struct skeleton *sk = &localplayer.skeleton;
+void render_remote_players( world_instance *world, vg_camera *cam )
+{
+   u32 draw_list[ NETWORK_MAX_PLAYERS ],
+       draw_list_count = 0,
+       gliders = 0;
 
    for( u32 i=0; i<NETWORK_MAX_PLAYERS; i ++ ){
       struct network_player *player = &netplayers.list[i];
       if( !player->active || player->isblocked ) continue;
       if( player->active_world != world ) continue;
+
+#if 0
       if( !player->isfriend && 
             (world-world_static.instances == k_world_purpose_hub)) continue;
-      
-      m4x3f *final_mtx = &netplayers.final_mtx[ sk->bone_count*i ];
+#endif
+
+      draw_list[draw_list_count ++] = i;
+
+      if( player->render_glider )
+         gliders ++;
+   }
+
+   struct skeleton *sk = &localplayer.skeleton;
+
+   SDL_AtomicLock( &addon_system.sl_cache_using_resources );
+
+   for( u32 j=0; j<draw_list_count; j ++ ){
+      u32 index = draw_list[j];
+
+      struct network_player *player = &netplayers.list[index];
+      m4x3f *final_mtx = &netplayers.final_mtx[ sk->bone_count*index ];
 
       struct player_model *model = 
          addon_cache_item_if_loaded( k_addon_type_player, 
@@ -632,20 +734,48 @@ static void render_remote_players( world_instance *world, camera *cam ){
          addon_cache_item_if_loaded( k_addon_type_board,
                                      player->board_view_slot );
       render_board( cam, world, board, final_mtx[localplayer.id_board],
-                     &netplayers.board_poses[ i ], k_board_shader_player );
+                     &netplayers.board_poses[ index ], k_board_shader_player );
    }
 
    SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
+
+   if( !gliders )
+      return;
+
+   /* TODO: we really, really only need to do all this once. at some point
+    *       PLEASE figure out a good place to do this once per frame!
+    */
+   
+   shader_model_entity_use();
+   shader_model_entity_uTexMain( 0 );
+   shader_model_entity_uCamera( cam->transform[3] );
+   shader_model_entity_uPv( cam->mtx.pv );
+   
+   WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world, model_entity );
+
+   for( u32 j=0; j<draw_list_count; j ++ ){
+      u32 index = draw_list[j];
+
+      struct network_player *player = &netplayers.list[index];
+      if( !player->render_glider ) continue;
+
+      if( player->render_glider ){
+         v3f *glider_mtx = netplayers.glider_mtx[ index ];
+         render_glider_model( cam, world, glider_mtx, k_board_shader_entity );
+      }
+   }
 }
 
 static int remote_players_randomize( int argc, const char *argv[] ){
    for( int i=0; i<NETWORK_MAX_PLAYERS; i ++ ){
       struct network_player *player = &netplayers.list[i];
 
-      player->active = (vg_randu32() & 0x1)? 2: 0;
-      player->isfriend = vg_randu32() & vg_randu32() & 0x1;
-      player->isblocked = vg_randu32() & vg_randu32() & vg_randu32() & 0x1;
-      player->world_match[ 0 ] = vg_randu32() & 0x1;
+      player->active = (vg_randu32(&vg.rand) & 0x1)? 2: 0;
+      player->isfriend = vg_randu32(&vg.rand) & vg_randu32(&vg.rand) & 0x1;
+      player->isblocked = vg_randu32(&vg.rand) & 
+                          vg_randu32(&vg.rand) & 
+                          vg_randu32(&vg.rand) & 0x1;
+      player->world_match[ 0 ] = vg_randu32(&vg.rand) & 0x1;
       player->world_match[ 1 ] = 0;
 
       if( player->world_match[0] )
@@ -654,20 +784,20 @@ static int remote_players_randomize( int argc, const char *argv[] ){
          player->active_world = NULL;
 
       for( int i=0; i<sizeof(player->username)-1; i ++ ){
-         player->username[i] = 'a' + (vg_randu32() % 30);
+         player->username[i] = 'a' + (vg_randu32(&vg.rand) % 30);
          player->username[i+1] = '\0';
 
-         if( (vg_randu32() % 8) == 3 )
+         if( (vg_randu32(&vg.rand) % 8) == 3 )
             break;
       }
 
       for( int i=0; i<3; i ++ ){
-         player->medals[i] = vg_randu32() % 3;
+         player->medals[i] = vg_randu32(&vg.rand) % 3;
       }
 
       v3f pos;
 
-      vg_rand_sphere( pos );
+      vg_rand_sphere( &vg.rand, pos );
       v3_muladds( localplayer.rb.co, pos, 100.0f,
                   netplayers.final_mtx[ i*localplayer.skeleton.bone_count][3] );
    }
@@ -675,7 +805,8 @@ static int remote_players_randomize( int argc, const char *argv[] ){
    return 0;
 }
 
-static void remote_players_init(void){
+void remote_players_init(void)
+{
    vg_console_reg_cmd( "add_test_players", remote_players_randomize, NULL );
    vg_console_reg_var( "k_show_own_name", &k_show_own_name, 
                        k_var_dtype_i32, 0 );
@@ -684,7 +815,8 @@ static void remote_players_init(void){
    }
 }
 
-static void remote_sfx_pre_update(void){
+void remote_sfx_pre_update(void)
+{
    for( u32 i=0; i<NETWORK_SFX_QUEUE_LENGTH; i ++ ){
       struct net_sfx *si = &netplayers.sfx_queue[i];
 
@@ -741,37 +873,39 @@ static int player_tag_position( m4x4f pv, v3f root_co, ui_point out_point ){
 /*
  * Draw chat box relative to the root tag position on the screen
  */
-static void chat_box( ui_point tag_root, f64 time, const char *message ){
+static void chat_box( ui_context *ctx,
+                      ui_point tag_root, f64 time, const char *message )
+{
    if( (vg.time_real - time) > 15.0 ) 
       return;
 
    ui_rect wr;
-   wr[2] = ui_text_line_width( message ) + 8;
-   wr[3] = vg_ui.font->glyph_height + 2;
+   wr[2] = ui_text_line_width( ctx, message ) + 8;
+   wr[3] = ctx->font->ch + 2;
    wr[0] = tag_root[0]-(wr[2]/2);
    wr[1] = tag_root[1] - wr[3] - 8;
 
-   ui_fill( wr, ui_opacity( ui_colour(k_ui_bg), 0.23f ) );
-   ui_text( wr, message, 1, k_ui_align_middle_center, 0 );
+   ui_fill( ctx, wr, ui_opacity( ui_colour(ctx, k_ui_bg), 0.23f ) );
+   ui_text( ctx, wr, message, 1, k_ui_align_middle_center, 0 );
 }
 
 /*
  * Draw full imgui for remote player
  */
-static void remote_player_nametag( ui_point tag_root, 
-                                   struct network_player *player ){
-   vg_ui.font = &vg_ui_font_big;
+static void remote_player_nametag( ui_context *ctx, ui_point tag_root, 
+                                   struct network_player *player )
+{
+   ctx->font = &vgf_default_large;
 
    ui_rect wr;
-   wr[2] = VG_MAX( ui_text_line_width( player->username ), 140 ) + 8;
+   wr[2] = VG_MAX( ui_text_line_width( ctx, player->username ), 140 ) + 8;
    wr[3] = 32;
    wr[0] = tag_root[0]-(wr[2]/2);
    wr[1] = tag_root[1]-(wr[3]/2);
 
-   ui_fill( wr, ui_opacity( ui_colour(k_ui_bg), 0.23f ) );
-   ui_text( wr, player->username, 1, k_ui_align_middle_center, 0 );
-
-   vg_ui.font = &vg_ui_font_small;
+   ui_fill( ctx, wr, ui_opacity( ui_colour(ctx, k_ui_bg), 0.23f ) );
+   ui_text( ctx, wr, player->username, 1, k_ui_align_middle_center, 0 );
+   ctx->font = &vgf_default_small;
 
    /* medals */
    int cols = 0;
@@ -782,21 +916,26 @@ static void remote_player_nametag( ui_point tag_root,
    char buf[32];
    vg_str str;
 
-   if( cols ){
+   if( cols )
+   {
       f32 w = (f32)wr[2] / (f32)cols;
       cols = 0;
 
-      for( int i=0; i<3; i ++ ){
-         if( player->medals[i] ){
-            ui_rect col = { wr[0] + (f32)cols*w, wr[1] + wr[3], w, 
-                           vg_ui.font->glyph_height };
+      for( int i=0; i<3; i ++ )
+      {
+         if( player->medals[i] )
+         {
+            ui_rect col = { wr[0] + (f32)cols*w, wr[1] + wr[3], 
+                            w, ctx->font->ch };
 
             vg_strnull( &str, buf, 32 );
+#if 0
             vg_strcatch( &str, (char)k_SRglyph_vg_circle );
+#endif
             vg_strcati32( &str, player->medals[i] );
 
-            ui_text( col, buf, 1, k_ui_align_middle_center, 
-                     ui_colour( (enum ui_scheme_colour[]){ 
+            ui_text( ctx, col, buf, 1, k_ui_align_middle_center, 
+                     ui_colour( ctx, (enum ui_scheme_colour[]){ 
                         k_ui_yellow, k_ui_gray, k_ui_orange }[i] ) );
             
             cols ++;
@@ -805,25 +944,30 @@ static void remote_player_nametag( ui_point tag_root,
    }
 }
 
-static void remote_player_world_gui( m4x4f pv, v3f root_co, 
+static void remote_player_world_gui( ui_context *ctx, m4x4f pv, v3f root_co, 
                                      struct network_player *player  ){
    ui_point tag_root;
    if( !player_tag_position( pv, root_co, tag_root ) )
       return;
 
-   if( player ){
-      remote_player_nametag( tag_root, player );
-      chat_box( tag_root, player->chat_time, player->chat );
+   if( player )
+   {
+      remote_player_nametag( ctx, tag_root, player );
+      chat_box( ctx, tag_root, player->chat_time, player->chat );
    }
-   else {
+   else 
+   {
       if( netplayers.chatting )
-         chat_box( tag_root, vg.time_real, netplayers.chat_buffer );
+         chat_box( ctx, tag_root, vg.time_real, netplayers.chat_buffer );
       else
-         chat_box( tag_root, netplayers.chat_time, netplayers.chat_message );
+      {
+         chat_box( ctx, tag_root, 
+                   netplayers.chat_time, netplayers.chat_message );
+      }
    }
 }
 
-static void remote_player_gui_info( ui_rect box, 
+static void remote_player_gui_info( ui_context *ctx, ui_rect box, 
                                     const char *username,
                                     const char *activity, 
                                     enum remote_player_gui_type type,
@@ -832,12 +976,12 @@ static void remote_player_gui_info( ui_rect box,
    f32 opacity = in_world? 0.6f: 0.3f;
 
    if( type == k_remote_player_gui_type_you )
-      ui_fill( box, ui_opacity( 0xff555555, opacity ) );
+      ui_fill( ctx, box, ui_opacity( 0xff555555, opacity ) );
    else
-      ui_fill( box, ui_opacity( 0xff000000, opacity ) );
+      ui_fill( ctx, box, ui_opacity( 0xff000000, opacity ) );
 
    if( type == k_remote_player_gui_type_friend )
-      ui_outline( box, -1, ui_opacity( 0xff00c4f0, opacity ), 0 );
+      ui_outline( ctx, box, -1, ui_opacity( 0xff00c4f0, opacity ), 0 );
 
    ui_rect top, bottom;
    ui_split_ratio( box, k_ui_axis_h, 0.6666f, 1, top, bottom );
@@ -845,18 +989,18 @@ static void remote_player_gui_info( ui_rect box,
    u32 fg;
    
    if( type == k_remote_player_gui_type_friend )
-      fg = ui_colour( k_ui_yellow + (in_world? k_ui_brighter: 0) );
+      fg = ui_colour( ctx, k_ui_yellow + (in_world? k_ui_brighter: 0) );
    else
-      fg = ui_colour( in_world? k_ui_fg: k_ui_fg+4 );
+      fg = ui_colour( ctx, in_world? k_ui_fg: k_ui_fg+4 );
 
-   vg_ui.font = &vg_ui_font_big;
-   ui_text( top, username, 1, k_ui_align_middle_center, fg );
-   vg_ui.font = &vg_ui_font_small;
-
-   ui_text( bottom, activity, 1, k_ui_align_middle_center, fg );
+   ctx->font = &vgf_default_large;
+   ui_text( ctx, top, username, 1, k_ui_align_middle_center, fg );
+   ctx->font = &vgf_default_small;
+   ui_text( ctx, bottom, activity, 1, k_ui_align_middle_center, fg );
 }
 
-static void remote_players_imgui_lobby(void){
+void remote_players_imgui_lobby( ui_context *ctx )
+{
    if( network_client.user_intent == k_server_intent_online ){
       if( !(steam_ready &&
          (network_client.state == k_ESteamNetworkingConnectionState_Connected)))
@@ -868,19 +1012,20 @@ static void remote_players_imgui_lobby(void){
    ui_px y = 50, width = 200, height = 42, gap = 2,
          x = vg.window_x - width;
 
-   vg_ui.font = &vg_ui_font_big;
-   ui_text( (ui_rect){ x, 0, width, height }, 
+   ctx->font = &vgf_default_large;
+   ui_text( ctx, (ui_rect){ x, 0, width, height }, 
             "In World", 1, k_ui_align_middle_center, 0 );
-   vg_ui.font = &vg_ui_font_small;
+   ctx->font = &vgf_default_small;
 
 
    ui_rect us = { x, y, width, height };
    /* FIXME: your location */
-   remote_player_gui_info( us, steam_username_at_startup, "you",
+   remote_player_gui_info( ctx, us, steam_username_at_startup, "you",
                            k_remote_player_gui_type_you, 1 );
    y += height + gap;
 
-   for( u32 i=0; i<NETWORK_MAX_PLAYERS; i ++ ){
+   for( u32 i=0; i<NETWORK_MAX_PLAYERS; i ++ )
+   {
       struct network_player *player = &netplayers.list[i];
       if( !player->active || player->isblocked ) continue;
 
@@ -894,19 +1039,23 @@ static void remote_players_imgui_lobby(void){
       }
       
       ui_rect box = { x, y, width, height };
-      remote_player_gui_info( box, player->username, location,
+      remote_player_gui_info( ctx, box, player->username, location,
                               player->isfriend, in_same_world );
       y += height + gap;
    }
 }
 
-static void remote_players_imgui_world( world_instance *world, m4x4f pv,
-                                        f32 max_dist, int geo_cull ){
-   ui_flush( k_ui_shader_colour, vg.window_x, vg.window_y );
+void remote_players_imgui_world( ui_context *ctx, 
+                                 world_instance *world, m4x4f pv,
+                                 f32 max_dist, int geo_cull )
+{
+   ui_flush( ctx, k_ui_shader_colour, NULL );
 
-   for( u32 i=0; i<NETWORK_MAX_PLAYERS; i++ ){
+   for( u32 i=0; i<NETWORK_MAX_PLAYERS; i++ )
+   {
       struct network_player *player = &netplayers.list[i];
-      if( player->active ){
+      if( player->active )
+      {
          v3f co;
          remote_player_position( i, co );
 
@@ -916,7 +1065,8 @@ static void remote_players_imgui_world( world_instance *world, m4x4f pv,
                (world-world_static.instances == k_world_purpose_hub)) continue;
 
          /* their in our active subworld */
-         if( player->active_world != world ){
+         if( player->active_world != world )
+         {
             m4x3_mulv( global_miniworld.mmdl, co, co );
             co[1] -= 2.0f; /* HACK lol */
          }
@@ -934,10 +1084,10 @@ static void remote_players_imgui_world( world_instance *world, m4x4f pv,
             hit.dist = dist;
 
             v3f dir;
-            v3_sub( co, skaterift.cam.pos, dir );
+            v3_sub( co, g_render.cam.pos, dir );
             v3_normalize( dir );
 
-            if( ray_world( world, skaterift.cam.pos, dir, &hit, 
+            if( ray_world( world, g_render.cam.pos, dir, &hit, 
                            k_material_flag_ghosts ) ){
                opacity *= 0.5f;
             }
@@ -946,24 +1096,24 @@ static void remote_players_imgui_world( world_instance *world, m4x4f pv,
          player->opacity = vg_lerpf( player->opacity, opacity,
                                      vg.time_frame_delta * 2.0f );
          
-         remote_player_world_gui( pv, co, player );
+         remote_player_world_gui( ctx, pv, co, player );
 
          vg_ui.colour[3] = player->opacity;
-         ui_flush( k_ui_shader_colour, vg.window_x, vg.window_y );
+         ui_flush( ctx, k_ui_shader_colour, NULL );
       }
    }
 
    vg_ui.colour[3] = 1.0f;
-
-   remote_player_world_gui( pv, localplayer.rb.co, NULL );
-   ui_flush( k_ui_shader_colour, vg.window_x, vg.window_y );
+   remote_player_world_gui( ctx, pv, localplayer.rb.co, NULL );
+   ui_flush( ctx, k_ui_shader_colour, NULL );
 }
 
-static void chat_escape(void){
+static void chat_escape( ui_context *ctx )
+{
    netplayers.chatting = -1;
 }
 
-static void chat_enter( char *buf, u32 len ){
+static void chat_enter( ui_context *ctx, char *buf, u32 len ){
    vg_strncpy( buf, netplayers.chat_message, NETWORK_MAX_CHAT, 
                k_strncpy_always_add_null );
    netplayers.chatting = -1;
@@ -971,27 +1121,33 @@ static void chat_enter( char *buf, u32 len ){
    chat_send_message( buf );
 }
 
-static void remote_players_chat_imgui(void){
-   if( netplayers.chatting == 1 ){
+void remote_players_chat_imgui( ui_context *ctx )
+{
+   if( netplayers.chatting == 1 )
+   {
       ui_rect box = { 0, 0, 400, 40 },
               window = { 0, 0, vg.window_x, vg.window_y };
       ui_rect_center( window, box );
 
-      struct ui_textbox_callbacks callbacks = {
+      struct ui_textbox_callbacks callbacks = 
+      {
          .enter = chat_enter,
          .escape = chat_escape
       };
 
-      ui_textbox( box, NULL, 
+      ui_textbox( ctx, box, NULL, 
                   netplayers.chat_buffer, NETWORK_MAX_CHAT, 1,
                   UI_TEXTBOX_AUTOFOCUS, &callbacks );
    }
-   else {
-      if( netplayers.chatting == -1 ){
+   else 
+   {
+      if( netplayers.chatting == -1 )
+      {
          netplayers.chatting = 0;
          srinput.state = k_input_state_resume;
       }
-      else {
+      else 
+      {
          if( (skaterift.activity == k_skaterift_default) && 
                button_down( k_srbind_chat ) ){
             netplayers.chatting = 1;