X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=ent_skateshop.c;h=05999f9322c5793df9cccb00c7e547f14c03b460;hb=bececcbb7b2e886e72425e7c070e1fdc3aa126dc;hp=962edf4b28f092a6562bb087f23f2569fcc6c6cb;hpb=d3021395f97b2ae244835c6656008386c8874343;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/ent_skateshop.c b/ent_skateshop.c index 962edf4..05999f9 100644 --- a/ent_skateshop.c +++ b/ent_skateshop.c @@ -1,21 +1,23 @@ -#ifndef ENT_SKATESHOP_C -#define ENT_SKATESHOP_C - -#define VG_GAME -#include "vg/vg.h" #include "vg/vg_steam_ugc.h" #include "vg/vg_msg.h" +#include "vg/vg_tex.h" +#include "vg/vg_image.h" +#include "vg/vg_loader.h" #include "ent_skateshop.h" #include "world.h" #include "player.h" #include "gui.h" #include "menu.h" -#include "highscores.h" #include "steam.h" #include "addon.h" #include "save.h" #include "network.h" +struct global_skateshop global_skateshop = +{ + .render={.reg_id=0xffffffff,.world_reg=0xffffffff} +}; + /* * Checks string equality but does a hash check first */ @@ -44,72 +46,137 @@ static void skateshop_update_viewpage(void){ } } -/* - * op/subroutine: k_workshop_op_item_load - * ----------------------------------------------------------------------------- - */ - -static void world_scan_thread( void *_args ){ - addon_mount_content_folder( k_addon_type_world, "maps", ".mdl" ); - addon_mount_workshop_items(); - vg_async_call( async_addon_reg_update, NULL, 0 ); -} +struct async_preview_load_thread_data{ + void *data; + addon_reg *reg; +}; -/* - * Asynchronous scan of local disk for worlds - */ -static void skateshop_op_world_scan(void){ - vg_loader_start( world_scan_thread, NULL ); -} +static void skateshop_async_preview_imageload( void *data, u32 len ){ + struct async_preview_load_thread_data *inf = data; -static void board_processview_thread( void *_args ){ - addon_cache_load_loop(); -} + if( inf->data ){ + glBindTexture( GL_TEXTURE_2D, global_skateshop.tex_preview ); + glTexSubImage2D( GL_TEXTURE_2D, 0,0,0, + WORKSHOP_PREVIEW_WIDTH, WORKSHOP_PREVIEW_HEIGHT, + GL_RGB, GL_UNSIGNED_BYTE, inf->data ); + glGenerateMipmap( GL_TEXTURE_2D ); + stbi_image_free( inf->data ); -static void board_scan_thread( void *_args ){ - addon_mount_content_folder( k_addon_type_board, "boards", ".mdl" ); - addon_mount_workshop_items(); - vg_async_call( async_addon_reg_update, NULL, 0 ); - vg_async_stall(); - board_processview_thread(NULL); -} + skaterift.rt_textures[k_skaterift_rt_workshop_preview] = + global_skateshop.tex_preview; + } + else { + skaterift.rt_textures[k_skaterift_rt_workshop_preview] = vg.tex_missing; + } -/* TODO: migrate to addon.c */ -static void skateshop_op_board_scan(void){ - vg_loader_start( board_scan_thread, NULL ); + SDL_AtomicLock( &addon_system.sl_cache_using_resources ); + global_skateshop.reg_loaded_preview = inf->reg; + SDL_AtomicUnlock( &addon_system.sl_cache_using_resources ); } -/* TODO: migrate to addon.c */ -static void skateshop_autostart_loading(void){ - if( !vg_loader_availible() ) return; +static void skateshop_update_preview_image_thread(void *_args) +{ + char path_buf[4096]; + vg_str folder; + vg_strnull( &folder, path_buf, sizeof(path_buf) ); SDL_AtomicLock( &addon_system.sl_cache_using_resources ); - for( u32 type=0; typepool.count; id++ ){ - addon_cache_entry *entry = vg_pool_item( &cache->pool, id ); - if( entry->state == k_addon_cache_state_load_request ){ - SDL_AtomicUnlock( &addon_system.sl_cache_using_resources ); - goto launch; + if( !addon_get_content_folder( reg_preview, &folder, 1 ) ) + { + SDL_AtomicLock( &addon_system.sl_cache_using_resources ); + global_skateshop.reg_loaded_preview = reg_preview; + SDL_AtomicUnlock( &addon_system.sl_cache_using_resources ); + return; + } + + vg_strcat( &folder, "/preview.jpg" ); + vg_async_item *call = + vg_async_alloc( sizeof(struct async_preview_load_thread_data) ); + struct async_preview_load_thread_data *inf = call->payload; + + inf->reg = reg_preview; + + if( vg_strgood( &folder ) ) + { + stbi_set_flip_vertically_on_load(1); + int x, y, nc; + inf->data = stbi_load( folder.buffer, &x, &y, &nc, 3 ); + + if( inf->data ) + { + if( (x != WORKSHOP_PREVIEW_WIDTH) || (y != WORKSHOP_PREVIEW_HEIGHT) ) + { + vg_error( "Resolution does not match framebuffer, so we can't" + " show it\n" ); + stbi_image_free( inf->data ); + inf->data = NULL; } } + + vg_async_dispatch( call, skateshop_async_preview_imageload ); + } + else + { + vg_error( "Path too long to workshop preview image.\n" ); + + SDL_AtomicLock( &addon_system.sl_cache_using_resources ); + global_skateshop.reg_loaded_preview = reg_preview; + SDL_AtomicUnlock( &addon_system.sl_cache_using_resources ); } - SDL_AtomicUnlock( &addon_system.sl_cache_using_resources ); - return; -launch: - vg_loader_start( board_processview_thread, NULL ); } +void skateshop_world_preview_preupdate(void) +{ + /* try to load preview image if we availible to do. */ + if( vg_loader_availible() ) + { + SDL_AtomicLock( &addon_system.sl_cache_using_resources ); + if( global_skateshop.reg_preview != global_skateshop.reg_loaded_preview ) + { + SDL_AtomicUnlock( &addon_system.sl_cache_using_resources ); + vg_loader_start( skateshop_update_preview_image_thread, NULL ); + } + else SDL_AtomicUnlock( &addon_system.sl_cache_using_resources ); + } +} + +/* + * op/subroutine: k_workshop_op_item_load + * ----------------------------------------------------------------------------- + */ + /* * Regular stuff * ----------------------------------------------------------------------------- */ +static void skateshop_init_async(void *_data,u32 size){ + glGenTextures( 1, &global_skateshop.tex_preview ); + glBindTexture( GL_TEXTURE_2D, global_skateshop.tex_preview ); + glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, + WORKSHOP_PREVIEW_WIDTH, WORKSHOP_PREVIEW_HEIGHT, + 0, GL_RGB, GL_UNSIGNED_BYTE, NULL ); + + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, + GL_LINEAR_MIPMAP_LINEAR ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE ); + + skaterift.rt_textures[ k_skaterift_rt_workshop_preview ] = vg.tex_missing; + skaterift.rt_textures[ k_skaterift_rt_server_status ] = vg.tex_missing; + render_server_status_gui(); +} + /* * VG event init */ -static void skateshop_init(void){ +void skateshop_init(void) +{ + vg_async_call( skateshop_init_async, NULL, 0 ); } static u16 skateshop_selected_cache_id(void){ @@ -122,15 +189,33 @@ static u16 skateshop_selected_cache_id(void){ else return 0; } +static void skateshop_server_helper_update(void){ + vg_str text; + vg_strnull( &text, global_skateshop.helper_toggle->text, + sizeof(global_skateshop.helper_toggle->text) ); + + if( skaterift.demo_mode ){ + vg_strcat( &text, "Not availible in demo" ); + } + else { + if( network_client.user_intent == k_server_intent_online ) + vg_strcat( &text, "Disconnect" ); + else + vg_strcat( &text, "Go Online" ); + } +} + /* * VG event preupdate */ void temp_update_playermodel(void); -static void ent_skateshop_preupdate( ent_skateshop *shop, int active ){ - if( !active ) return; +void ent_skateshop_preupdate( ent_focus_context *ctx ) +{ + if( !ctx->active ) + return; - /* input filter */ - world_instance *world = world_current_instance(); + world_instance *world = ctx->world; + ent_skateshop *shop = mdl_arritm( &world->ent_skateshop, ctx->index ); /* camera positioning */ ent_camera *ref = mdl_arritm( &world->ent_camera, @@ -147,8 +232,14 @@ static void ent_skateshop_preupdate( ent_skateshop *shop, int active ){ mdl_entity_id_id(shop->boards.id_display) ); v3_sub( display->transform.co, localplayer.rb.co, lookat ); } - else if( shop->type == k_skateshop_type_charshop ) + else if( shop->type == k_skateshop_type_charshop ){ v3_sub( ref->transform.co, localplayer.rb.co, lookat ); + } + else if( shop->type == k_skateshop_type_server ){ + ent_prop *prop = mdl_arritm( &world->ent_prop, + mdl_entity_id_id(shop->server.id_lever) ); + v3_sub( prop->transform.co, localplayer.rb.co, lookat ); + } else vg_fatal_error( "Unknown store (%u)\n", shop->type ); @@ -198,7 +289,8 @@ static void ent_skateshop_preupdate( ent_skateshop *shop, int active ){ localplayer.board_view_slot = cache_id; network_send_item( k_netmsg_playeritem_board ); - world_entity_unfocus(); + world_entity_exit_modal(); + world_entity_clear_focus(); gui_helper_clear(); skaterift_autosave(1); return; @@ -245,7 +337,8 @@ static void ent_skateshop_preupdate( ent_skateshop *shop, int active ){ if( button_down( k_srbind_maccept ) ){ network_send_item( k_netmsg_playeritem_player ); - world_entity_unfocus(); + world_entity_exit_modal(); + world_entity_clear_focus(); gui_helper_clear(); } } @@ -258,14 +351,15 @@ static void ent_skateshop_preupdate( ent_skateshop *shop, int active ){ if( valid_count && vg_loader_availible() ) browseable = 1; - if( vg_loader_availible() ) + if( valid_count && vg_loader_availible() ) loadable = 1; global_skateshop.helper_browse->greyed = !browseable; global_skateshop.helper_pick->greyed = !loadable; - int change = 0; + addon_reg *selected_world = NULL; + int change = 0; if( browseable ){ if( button_down( k_srbind_mleft ) ){ if( global_skateshop.selected_world_id > 0 ){ @@ -280,31 +374,75 @@ static void ent_skateshop_preupdate( ent_skateshop *shop, int active ){ change = 1; } } + + selected_world = get_addon_from_index( k_addon_type_world, + global_skateshop.selected_world_id, ADDON_REG_HIDDEN ); + + if( change || (global_skateshop.reg_preview == NULL) ){ + SDL_AtomicLock( &addon_system.sl_cache_using_resources ); + global_skateshop.reg_preview = selected_world; + SDL_AtomicUnlock( &addon_system.sl_cache_using_resources ); + } } if( loadable ){ if( button_down( k_srbind_maccept ) ){ - skaterift_change_world_start( - get_addon_from_index( k_addon_type_world, - global_skateshop.selected_world_id, - ADDON_REG_HIDDEN )); + skaterift_change_world_start( selected_world ); } } } + else if( shop->type == k_skateshop_type_server ){ + f64 delta = vg.time_real - network_client.last_intent_change; + + if( (delta > 5.0) && (!skaterift.demo_mode) ){ + global_skateshop.helper_pick->greyed = 0; + if( button_down( k_srbind_maccept ) ){ + network_client.user_intent = !network_client.user_intent; + network_client.last_intent_change = vg.time_real; + skateshop_server_helper_update(); + render_server_status_gui(); + } + } + else { + global_skateshop.helper_pick->greyed = 1; + } + } else{ vg_fatal_error( "Unknown store (%u)\n", shop->type ); } - if( button_down( k_srbind_mback ) ){ + if( button_down( k_srbind_mback ) ) + { if( shop->type == k_skateshop_type_charshop ) network_send_item( k_netmsg_playeritem_player ); - world_entity_unfocus(); + world_entity_exit_modal(); + world_entity_clear_focus(); gui_helper_clear(); return; } } +void skateshop_world_preupdate( world_instance *world ) +{ + for( u32 i=0; ient_skateshop); i++ ){ + ent_skateshop *shop = mdl_arritm( &world->ent_skateshop, i ); + + if( shop->type == k_skateshop_type_server ){ + f32 a = network_client.user_intent; + + vg_slewf( &network_client.fintent, a, vg.time_frame_delta ); + a = (vg_smoothstepf( network_client.fintent ) - 0.5f) * (VG_PIf/2.0f); + + ent_prop *lever = mdl_arritm( &world->ent_prop, + mdl_entity_id_id(shop->server.id_lever) ); + + /* we need parent transforms now? */ + q_axis_angle( lever->transform.q, (v3f){0,0,1}, a ); + } + } +} + static void skateshop_render_boardshop( ent_skateshop *shop ){ world_instance *world = world_current_instance(); u32 slot_count = vg_list_size(global_skateshop.shop_view_slots); @@ -366,11 +504,6 @@ fade_out:; mdl_transform_m4x3( &mark_info->transform, mtext ); mdl_transform_m4x3( &mark_rack->transform, mrack ); -#if 0 - const char *text_title = "Fish - Title"; - const char *text_author = "by Shaniqua"; -#endif - m4x3f mlocal, mmdl; m4x3_identity( mlocal ); @@ -478,21 +611,34 @@ static void skateshop_render_worldshop( ent_skateshop *shop ){ addon_reg *reg = get_addon_from_index( k_addon_type_world, global_skateshop.selected_world_id, ADDON_REG_HIDDEN ); - vg_msg msg; - vg_msg_init( &msg, reg->metadata, reg->metadata_len ); - global_skateshop.render.world_loc = vg_msg_getkvstr( &msg, "location" ); - global_skateshop.render.world_reg = global_skateshop.selected_world_id; + if( !reg ) + goto none; - if( vg_msg_seekframe( &msg, "workshop" ) ){ - global_skateshop.render.world_title = vg_msg_getkvstr( &msg, "title"); - vg_msg_skip_frame( &msg ); + if( reg->alias.workshop_id ) + { + vg_msg msg; + vg_msg_init( &msg, reg->metadata, reg->metadata_len ); + + global_skateshop.render.world_loc = vg_msg_getkvstr(&msg,"location"); + global_skateshop.render.world_reg = global_skateshop.selected_world_id; + + if( vg_msg_seekframe( &msg, "workshop" ) ) + { + global_skateshop.render.world_title = vg_msg_getkvstr(&msg,"title"); + vg_msg_skip_frame( &msg ); + } + else { + vg_warn( "No workshop body\n" ); + } } else { - vg_warn( "No workshop body\n" ); + global_skateshop.render.world_title = reg->alias.foldername; } } +none:; + /* Text */ char buftext[128], bufsubtext[128]; vg_str info, subtext; @@ -500,17 +646,20 @@ static void skateshop_render_worldshop( ent_skateshop *shop ){ vg_strnull( &subtext, bufsubtext, 128 ); u32 valid_count = addon_count(k_addon_type_world,ADDON_REG_HIDDEN); - if( valid_count ){ + if( valid_count ) + { vg_strcati32( &info, global_skateshop.selected_world_id+1 ); vg_strcatch( &info, '/' ); vg_strcati32( &info, valid_count ); vg_strcatch( &info, ' ' ); vg_strcat( &info, global_skateshop.render.world_title ); - if( !vg_loader_availible() ){ + if( !vg_loader_availible() ) + { vg_strcat( &subtext, "Loading..." ); } - else{ + else + { addon_reg *reg = get_addon_from_index( k_addon_type_world, global_skateshop.selected_world_id, ADDON_REG_HIDDEN ); @@ -520,7 +669,8 @@ static void skateshop_render_worldshop( ent_skateshop *shop ){ vg_strcat( &subtext, global_skateshop.render.world_loc ); } } - else{ + else + { vg_strcat( &info, "No workshop worlds installed" ); } @@ -546,37 +696,64 @@ static void skateshop_render_worldshop( ent_skateshop *shop ){ mlocal[3][1] = -scale1*0.3f; m4x3_mul( mtext, mlocal, mtextmdl ); font3d_simple_draw( 0, bufsubtext, &skaterift.cam, mtextmdl ); - -#if 0 - /* pointcloud */ - m4x3f mmdl; - mdl_transform_m4x3( &mark_display->transform, mmdl ); - m4x3_rotate_y( mmdl, vg.time * 0.2 ); - - glEnable(GL_BLEND); - glBlendFunc(GL_ONE, GL_ONE); - glDisable(GL_DEPTH_TEST); - pointcloud_render( world, &skaterift.cam, mmdl ); - glDisable(GL_BLEND); - glEnable(GL_DEPTH_TEST); -#endif } /* * World: render event */ -static void skateshop_render( ent_skateshop *shop ){ +void skateshop_render( ent_skateshop *shop ) +{ if( shop->type == k_skateshop_type_boardshop ) skateshop_render_boardshop( shop ); else if( shop->type == k_skateshop_type_charshop ) skateshop_render_charshop( shop ); else if( shop->type == k_skateshop_type_worldshop ) skateshop_render_worldshop( shop ); + else if( shop->type == k_skateshop_type_server ){ + } else vg_fatal_error( "Unknown store (%u)\n", shop->type ); } -static void ent_skateshop_helpers_pickable( const char *acceptance ){ +void skateshop_render_nonfocused( world_instance *world, vg_camera *cam ) +{ + for( u32 j=0; jent_skateshop ); j ++ ) + { + ent_skateshop *shop = mdl_arritm(&world->ent_skateshop, j ); + + if( shop->type != k_skateshop_type_boardshop ) continue; + + f32 dist2 = v3_dist2( cam->pos, shop->transform.co ), + maxdist = 50.0f; + + if( dist2 > maxdist*maxdist ) continue; + ent_marker *mark_rack = mdl_arritm( &world->ent_marker, + mdl_entity_id_id(shop->boards.id_rack)); + + if( !mark_rack ) + continue; + + u32 slot_count = vg_list_size(global_skateshop.shop_view_slots); + for( u32 i=0; itransform, &xform, &xform ); + + struct player_board_pose pose = {0}; + m4x3f mmdl; + mdl_transform_m4x3( &xform, mmdl ); + render_board( cam, world, board, mmdl, &pose, k_board_shader_entity ); + } + } +} + +static void ent_skateshop_helpers_pickable( const char *acceptance ) +{ vg_str text; if( gui_new_helper( input_button_list[k_srbind_mback], &text )) @@ -593,38 +770,81 @@ static void ent_skateshop_helpers_pickable( const char *acceptance ){ } } +static void board_scan_thread( void *_args ) +{ + addon_mount_content_folder( k_addon_type_board, "boards", ".mdl" ); + addon_mount_workshop_items(); + vg_async_call( async_addon_reg_update, NULL, 0 ); + vg_async_stall(); + + /* 04.03.24 + * REVIEW: This is removed as it *should* be done on the preupdate of the + * addon system. + * + * Verify that it works the same. + */ +#if 0 + board_processview_thread(NULL); +#endif +} + +static void world_scan_thread( void *_args ) +{ + addon_mount_content_folder( k_addon_type_world, "maps", ".mdl" ); + addon_mount_workshop_items(); + vg_async_call( async_addon_reg_update, NULL, 0 ); +} + /* * Entity logic: entrance event */ -static void ent_skateshop_call( world_instance *world, ent_call *call ){ +entity_call_result ent_skateshop_call( world_instance *world, ent_call *call ) +{ u32 index = mdl_entity_id_id( call->id ); ent_skateshop *shop = mdl_arritm( &world->ent_skateshop, index ); vg_info( "skateshop_call\n" ); - if( skaterift.activity != k_skaterift_default ) return; - if( !vg_loader_availible() ) return; + if( (skaterift.activity != k_skaterift_default) || + !vg_loader_availible() ) + return k_entity_call_result_invalid; - if( call->function == k_ent_function_trigger ){ - if( localplayer.subsystem != k_player_subsystem_walk ) return; + if( call->function == k_ent_function_trigger ) + { + if( localplayer.subsystem != k_player_subsystem_walk ) + return k_entity_call_result_OK; vg_info( "Entering skateshop\n" ); - world_entity_focus( call->id ); + world_entity_set_focus( call->id ); + world_entity_focus_modal(); gui_helper_clear(); - if( shop->type == k_skateshop_type_boardshop ){ + if( shop->type == k_skateshop_type_boardshop ) + { skateshop_update_viewpage(); - skateshop_op_board_scan(); + vg_loader_start( board_scan_thread, NULL ); ent_skateshop_helpers_pickable( "pick" ); } - else if( shop->type == k_skateshop_type_charshop ){ + else if( shop->type == k_skateshop_type_charshop ) + { ent_skateshop_helpers_pickable( "pick" ); } - else if( shop->type == k_skateshop_type_worldshop ){ + else if( shop->type == k_skateshop_type_worldshop ) + { ent_skateshop_helpers_pickable( "open rift" ); - skateshop_op_world_scan(); + vg_loader_start( world_scan_thread, NULL ); + } + else if( shop->type == k_skateshop_type_server ) + { + vg_str text; + global_skateshop.helper_pick = gui_new_helper( + input_button_list[k_srbind_maccept], &text); + if( gui_new_helper( input_button_list[k_srbind_mback], &text )) + vg_strcat( &text, "exit" ); + skateshop_server_helper_update(); } + return k_entity_call_result_OK; } + else + return k_entity_call_result_unhandled; } - -#endif /* ENT_SKATESHOP_C */