From 527d7db897a2dd8de98d137623565bb76b4caca6 Mon Sep 17 00:00:00 2001 From: hgn Date: Sat, 29 Nov 2025 19:26:38 +0000 Subject: [PATCH] fffffffffff --- .../{audio_mixer.c => audio_mixer.c_UNUSED} | 0 source/engine/engine.kv | 365 +++++++++----- .../engine/{profiler.c => profiler.c_UNUSED} | 0 source/engine/steamworks.c_UNUSED | 364 ------------- source/engine/ui.c_UNUSED | 71 --- source/engine/{array_file.c => vg_af.c} | 2 +- source/engine/{array_file.h => vg_af.h} | 0 source/engine/vg_audio.h | 1 + source/engine/vg_audio_dsp.h | 1 + source/engine/{console.c => vg_console.c} | 0 source/engine/vg_console.h | 5 + source/engine/vg_engine.c | 48 +- source/engine/vg_engine.h | 12 + source/engine/vg_engine_core.kv | 379 -------------- source/engine/vg_entity.c | 15 + source/engine/{model_entity.h => vg_entity.h} | 101 ++-- source/engine/vg_framebuffer.h | 2 + source/engine/vg_input.h | 3 + source/engine/vg_lines.h | 2 + source/engine/{metascene.c => vg_metascene.c} | 8 +- source/engine/{metascene.h => vg_metascene.h} | 7 +- source/engine/{model.c => vg_model.c} | 4 +- source/engine/{model.h => vg_model.h} | 19 +- source/engine/vg_render.h | 2 + source/engine/vg_shader.h | 1 + source/engine/vg_tex.h | 2 + source/engine/vg_ui.h | 1 + source/foundation/foundation.h | 2 + source/foundation/foundation.kv | 22 - source/foundation/keyvalues.c | 62 +-- source/tools/metacompiler.c | 477 +++++++++++++----- 31 files changed, 784 insertions(+), 1194 deletions(-) rename source/engine/{audio_mixer.c => audio_mixer.c_UNUSED} (100%) rename source/engine/{profiler.c => profiler.c_UNUSED} (100%) delete mode 100644 source/engine/steamworks.c_UNUSED delete mode 100644 source/engine/ui.c_UNUSED rename source/engine/{array_file.c => vg_af.c} (99%) rename source/engine/{array_file.h => vg_af.h} (100%) rename source/engine/{console.c => vg_console.c} (100%) create mode 100644 source/engine/vg_console.h delete mode 100644 source/engine/vg_engine_core.kv create mode 100644 source/engine/vg_entity.c rename source/engine/{model_entity.h => vg_entity.h} (88%) rename source/engine/{metascene.c => vg_metascene.c} (99%) rename source/engine/{metascene.h => vg_metascene.h} (98%) rename source/engine/{model.c => vg_model.c} (99%) rename source/engine/{model.h => vg_model.h} (96%) diff --git a/source/engine/audio_mixer.c b/source/engine/audio_mixer.c_UNUSED similarity index 100% rename from source/engine/audio_mixer.c rename to source/engine/audio_mixer.c_UNUSED diff --git a/source/engine/engine.kv b/source/engine/engine.kv index 4c570b3..0561b73 100644 --- a/source/engine/engine.kv +++ b/source/engine/engine.kv @@ -23,55 +23,9 @@ enable SDL append ../foundation/foundation.kv add vg_engine.c -hook -{ - event OPTIONS - function _engine_options -} -event -{ - name ENGINE_NEW_FRAME - prototype "void" -} -event -{ - name ENGINE_FIXED_UPDATE - prototype "void" -} -event -{ - name ENGINE_RENDER - prototype "void" -} -event -{ - name ENGINE_UI - prototype "void" -} -event -{ - name ENGINE_WINDOW_RESIZE - prototype "void" -} - add vg_ui.c -hook -{ - event START - function _engine_ui_init -} - add vg_input.c -hook -{ - event START - function _input_init -} -hook -{ - event ENGINE_NEW_FRAME - function _input_update -} + ccmd { name bind @@ -101,38 +55,9 @@ ccmd } append ../console/console_system.kv -add console.c -hook -{ - event START - function _engine_console_init -} -hook -{ - event ENGINE_NEW_FRAME - function _engine_console_update -} -hook -{ - event ENGINE_UI - function _engine_console_ui - affinity 9999 -} - +add vg_console.c add vg_framebuffer.c -hook -{ - event ENGINE_WINDOW_RESIZE - function _framebuffer_resize -} - add vg_render.c -hook -{ - event START - function _vg_render_init -} - add vg_shader.c input_layer @@ -337,11 +262,6 @@ shader } } -hook -{ - event START - function "_shader_init" -} ccmd { name reload_shaders @@ -349,25 +269,9 @@ ccmd description "Recompile shaders (DEVELOPER ONLY!)" } - - add vg_camera.c - - add vg_tex.c -hook -{ - event START - function _vg_tex_init -} - - add vg_lines.c -hook -{ - event START - function _vg_lines_init -} cvar { name vg_lines_enable @@ -400,18 +304,6 @@ shader add vg_audio.c add vg_audio_dsp.c add vg_audio_vorbis.c -hook -{ - affinity -3008 - event START - function _audio_init -} -hook -{ - affinity -6004 - event START - function _dsp_init -} cvar { name vg_audio @@ -421,6 +313,253 @@ cvar description "Show audio info" } -add model.c -add metascene.c -add array_file.c +add vg_model.c +add vg_entity.c +add vg_metascene.c +add vg_af.c + +entity +{ + name ent_spawn + id 2 + + parameter + { + name transform + type transform + } + parameter + { + name name + type pstr + } + parameter + { + name flags + type u32 + flag + { + name locked + value 0x1 + } + flag + { + name group1 + value 0x2 + } + flag + { + name group2 + value 0x4 + } + flag + { + name group3 + value 0x8 + } + } + function + { + name lantern + } + function + { + name boat + } + function + { + name me + } +} +entity +{ + name ent_water + id 5 + function + { + name enable + } + function + { + name disable + } + function + { + name show + } +} +entity +{ + name ent_volume + id 6 + function + { + name enable + } + function + { + name disable + } +} +entity +{ + name ent_audio + id 7 +} +entity +{ + name ent_marker + id 8 + function + { + name push + } + function + { + name tooltip + } + function + { + name tooltip_special + } +} +entity +{ + name ent_camera + id 13 + function + { + name focus + } + function + { + name unfocus + } +} +entity +{ + name ent_ccmd + id 17 + function + { + name exec + } +} +entity +{ + name ent_cubemap + id 21 +} +entity +{ + name ent_prop + id 23 +} +entity +{ + name ent_armature + id 28 +} +entity +{ + name ent_atom + id 30 + function + { + name pass_equal + } + function + { + name pass_greater + } + function + { + name set + } + function + { + name set_or + } + function + { + name set_and + } +} +entity +{ + name ent_cutscene + id 31 + function + { + name play + } + function + { + name pause + } + function + { + name resume + } + function + { + name end + } +} +entity +{ + name ent_light + id 32 + + parameter + { + name transform + type transform + } + parameter + { + name flags + type u32 + flag + { + name daytime + value 0x1 + } + flag + { + name off + value 0x2 + } + } + parameter + { + name type + type u32 + } + parameter + { + name colour + type vec4 + ui rgbe + } + parameter + { + name angle + type f32 + } + parameter + { + name range + type f32 + } + + function + { + name on + argument + { + name enable + type i32 + } + } +} diff --git a/source/engine/profiler.c b/source/engine/profiler.c_UNUSED similarity index 100% rename from source/engine/profiler.c rename to source/engine/profiler.c_UNUSED diff --git a/source/engine/steamworks.c_UNUSED b/source/engine/steamworks.c_UNUSED deleted file mode 100644 index 549baf9..0000000 --- a/source/engine/steamworks.c_UNUSED +++ /dev/null @@ -1,364 +0,0 @@ -struct vg_steam_api _steam_api; - -static void cb_steam_warning( i32 severity, const c8 *pchMessage ) -{ - if( severity == 0 ) - vg_logsteam( "[message]" KBLK " '%s'\n", pchMessage ); - else - vg_logsteam( "[message]" KYEL " '%s'\n", pchMessage ); -} - -#if defined( VG_ENGINE ) -static void cb_auth_ticket_recieved( void *result, void *context ) -{ - EncryptedAppTicketResponse_t *response = result; - - if( response->m_eResult == k_EResultOK ) - vg_logsteam( " New app ticket ready\n" ); - else - vg_logsteam( KYEL " Could not request new encrypted app ticket (%u)\n", response->m_eResult ); - - if( SteamAPI_ISteamUser_GetEncryptedAppTicket( _steam_api.pSteamUser, _steam_api.app_symmetric_key, - VG_ARRAY_LEN(_steam_api.app_symmetric_key), &_steam_api.app_key_length )) - { - vg_logsteam( KGRN " Loaded app ticket\n" ); - } - else - { - vg_logsteam( KRED " No ticket availible\n" ); - _steam_api.app_key_length = 0; - } -} -#endif - -#if defined( VG_SERVER ) -static u8 vg_char_base16( c8 c ) -{ - if( c >= '0' && c <= '9' ) - return c-'0'; - if( c >= 'a' && c <= 'f' ) - return (c-'a') + 10; - return 0; -} -#endif - -#if defined( VG_SERVER ) -VG_API b8 _vg_steam_init( u32 unIP, u16 usGamePort, u16 usQueryPort, EServerMode eServerMode, - const c8 *pchVersionString, const c8 *appid_str ) -#else -VG_API b8 _vg_steam_init(void) -#endif -{ - if( _steam_api.disabled ) - return 0; - - SteamErrMsg err; - - /* Steamworks init step - * ---------------------------------------------------------------------------- */ -#if defined( VG_ENGINE ) - VG_ASSERT( _vg_thread_has_flags( VG_THREAD_OWNS_STEAM ) ); - - const char *pszInternalCheckInterfaceVersions = - STEAMUTILS_INTERFACE_VERSION "\0" - STEAMNETWORKINGUTILS_INTERFACE_VERSION "\0" - STEAMAPPS_INTERFACE_VERSION "\0" - STEAMCONTROLLER_INTERFACE_VERSION "\0" - STEAMFRIENDS_INTERFACE_VERSION "\0" - STEAMGAMESEARCH_INTERFACE_VERSION "\0" - STEAMHTMLSURFACE_INTERFACE_VERSION "\0" - STEAMHTTP_INTERFACE_VERSION "\0" - STEAMINPUT_INTERFACE_VERSION "\0" - STEAMINVENTORY_INTERFACE_VERSION "\0" - STEAMMATCHMAKINGSERVERS_INTERFACE_VERSION "\0" - STEAMMATCHMAKING_INTERFACE_VERSION "\0" - STEAMMUSICREMOTE_INTERFACE_VERSION "\0" - STEAMMUSIC_INTERFACE_VERSION "\0" - STEAMNETWORKINGMESSAGES_INTERFACE_VERSION "\0" - STEAMNETWORKINGSOCKETS_INTERFACE_VERSION "\0" - STEAMNETWORKING_INTERFACE_VERSION "\0" - STEAMPARENTALSETTINGS_INTERFACE_VERSION "\0" - STEAMPARTIES_INTERFACE_VERSION "\0" - STEAMREMOTEPLAY_INTERFACE_VERSION "\0" - STEAMREMOTESTORAGE_INTERFACE_VERSION "\0" - STEAMSCREENSHOTS_INTERFACE_VERSION "\0" - STEAMUGC_INTERFACE_VERSION "\0" - STEAMUSERSTATS_INTERFACE_VERSION "\0" - STEAMUSER_INTERFACE_VERSION "\0" - STEAMVIDEO_INTERFACE_VERSION "\0" - - "\0"; - - if( SteamInternal_SteamAPI_Init( pszInternalCheckInterfaceVersions, &err ) != k_ESteamAPIInitResult_OK ) - { - _steam_api.disabled = 1; - vg_logsteam( KRED "SteamInternal_SteamAPI_Init() failed: '%s'\nAll steam interactions disabled for this session\n", err ); - return 0; - } -#endif - -#if defined( VG_SERVER ) - FILE *txt = fopen( "steam_appid.txt", "w" ); - fputs( appid_str, txt ); - fclose( txt ); - - // FIXME: Add no-auth launch option (hoist from skaterift, as we have it there, to vg steam module?) - vg_stack_allocator stack; - vg_stack_init( &stack, NULL, VG_KB(256), NULL ); - u32 size; - c8 *src = vg_file_read( &stack, "application_key", &size, 0 ); - if( src ) - { - if( size < k_nSteamEncryptedAppTicketSymmetricKeyLen ) - { - vg_error( "Application key was invalid size\n" ); - return 0; - } - - for( int i=0; iuserdata = NULL; - call->cb = cb_auth_ticket_recieved; - call->id = SteamAPI_ISteamUser_RequestEncryptedAppTicket( _steam_api.pSteamUser, NULL, 0 ); - } -# endif -#endif - - return 1; -} - -static const c8 *string_ESteamNetworkingConnectionState( ESteamNetworkingConnectionState s ) -{ - if( s == k_ESteamNetworkingConnectionState_None ) return "None"; - if( s == k_ESteamNetworkingConnectionState_Connecting) return "Connecting"; - if( s == k_ESteamNetworkingConnectionState_FindingRoute) return "Finding route"; - if( s == k_ESteamNetworkingConnectionState_Connected) return "Connected"; - if( s == k_ESteamNetworkingConnectionState_ClosedByPeer) return "Closed By Peer"; - if( s == k_ESteamNetworkingConnectionState_ProblemDetectedLocally) return "Problem detected locally"; - if( s == k_ESteamNetworkingConnectionState_FinWait) return "Finwait"; - if( s == k_ESteamNetworkingConnectionState_Linger) return "Linger"; - if( s == k_ESteamNetworkingConnectionState_Dead) return "Dead"; - return "enum-out-of-range"; -} - -static const c8 *string_ESteamAPICallFailure( ESteamAPICallFailure e ) -{ - if( e == k_ESteamAPICallFailureNone ) return "None"; - if( e == k_ESteamAPICallFailureSteamGone ) return "Steam Gone"; - if( e == k_ESteamAPICallFailureNetworkFailure ) return "Network Failure"; - if( e == k_ESteamAPICallFailureInvalidHandle ) return KBLK "Invalid Handle"; - if( e == k_ESteamAPICallFailureMismatchedCallback ) return "Mismatched Callback"; - return "enum-out-of-range"; -} - -void vg_steam_frame(void) -{ - if( _steam_api.disabled ) - return; - - SteamAPI_ManualDispatch_RunFrame( _steam_api.hPipe ); - - CallbackMsg_t callback; - while( SteamAPI_ManualDispatch_GetNextCallback( _steam_api.hPipe, &callback ) ) - { - /* Check for dispatching API call results */ - i32 type = callback.m_iCallback; - if( type == k_iSteamUtils_SteamAPICallCompleted ) - { - SteamAPICallCompleted_t *inf = callback.m_pubParam; - - b8 bFailed; - void *call_data = alloca( inf->m_cubParam ); - - if( SteamAPI_ManualDispatch_GetAPICallResult( _steam_api.hPipe, inf->m_hAsyncCall, - call_data, inf->m_cubParam, - inf->m_iCallback, &bFailed ) ) - { - vg_logsteam( "api_call_completed %lu\n", inf->m_hAsyncCall ); - - int j=0; - for( int i=0; i<_steam_api.api_call_count; i++ ) - { - if( _steam_api.api_calls[i].id != inf->m_hAsyncCall ) - _steam_api.api_calls[j ++] = _steam_api.api_calls[i]; - else - _steam_api.api_calls[i].cb( call_data, _steam_api.api_calls[i].userdata ); - } - - if( _steam_api.api_call_count == j ) - vg_error( "No tracker was register for API call\n" ); - - _steam_api.api_call_count = j; - } - else - { - enum ESteamAPICallFailure e = - SteamAPI_ISteamUtils_GetAPICallFailureReason( _steam_api.pSteamUtils, inf->m_hAsyncCall ); - const c8 *fail_str = string_ESteamAPICallFailure( e ); - vg_logsteam( KRED "Error getting call result on %lu (code %d)\n", inf->m_hAsyncCall, fail_str ); - } - } - else - { - /* - * Look at callback.m_iCallback to see what kind of callback it is, - * and dispatch to appropriate handler(s) - * void *data = callback.m_pubParam; - */ - if( type == k_iSteamUser_SteamServersConnected ) - vg_success( "Steam servers connected" ); - else if( type == k_iSteamUser_SteamConnectFailure ) - { - SteamServerConnectFailure_t *inf = callback.m_pubParam; - vg_logsteam( KRED "Steam server connect failure, code %d, retrying: %d\n", inf->m_eResult, inf->m_bStillRetrying ); - } - else if( type == k_iSteamUser_SteamServersDisconnected ) - { - SteamServersDisconnected_t *inf = callback.m_pubParam; - vg_logsteam( "Steam servers disconnect, code %d\n", inf->m_eResult ); - } - else if( type == k_iSteamNetworkingSockets_SteamNetConnectionStatusChangedCallback ) - { - SteamNetConnectionStatusChangedCallback_t *inf = callback.m_pubParam; - const c8 *status_string = string_ESteamNetworkingConnectionState( inf->m_info.m_eState ); - vg_logsteam( "Connection status changed: %x -> %s\n Debug: '%s'\n EndDebug: '%s'\n", - inf->m_hConn, status_string, inf->m_info.m_szConnectionDescription, - inf->m_info.m_szEndDebug ); - - if( _steam_api.cb_connection_changed ) - _steam_api.cb_connection_changed( inf ); - } - else if( type == k_iSteamNetworkingSockets_SteamNetAuthenticationStatus ) - { - SteamNetAuthenticationStatus_t *inf = callback.m_pubParam; - vg_logsteam( "Steam Authentication Status: %d\n Debug: '%s'\n", inf->m_eAvail, inf->m_debugMsg ); - } - } - - SteamAPI_ManualDispatch_FreeLastCallback( _steam_api.hPipe ); - } -} - -VG_API void _vg_steam_shutdown(void) -{ -#if defined( VG_SERVER ) - if( _steam_api.is_connected ) - { - SteamAPI_ISteamGameServer_LogOff( _steam_api.pSteamGameServer ); - _steam_api.is_connected = 0; - } - SteamGameServer_Shutdown(); -#else - SteamAPI_Shutdown(); -#endif -} - -vg_steam_api_call *vg_alloc_async_steam_api_call(void) -{ - if( _steam_api.api_call_count == VG_ARRAY_LEN(_steam_api.api_calls) ) - return NULL; - return &_steam_api.api_calls[ _steam_api.api_call_count ++ ]; -} - -#if defined( VG_ENGINE ) -void vg_steam_set_achievement( const c8 *name, b8 yes ) -{ - if( _steam_api.disabled || _steam_api.demo_mode ) - return; - - if( yes ) - { - if( SteamAPI_ISteamUserStats_SetAchievement( _steam_api.pSteamUserStats, name ) ) - { - vg_logsteam( KGRN "Set achievement '%s'\n", name ); - SteamAPI_ISteamUserStats_StoreStats( _steam_api.pSteamUserStats ); - } - } - else - { - if( SteamAPI_ISteamUserStats_ClearAchievement( _steam_api.pSteamUserStats, name ) ) - { - vg_logsteam( KBLK "Clear achievement '%s'\n", name ); - SteamAPI_ISteamUserStats_StoreStats( _steam_api.pSteamUserStats ); - } - } -} -#endif diff --git a/source/engine/ui.c_UNUSED b/source/engine/ui.c_UNUSED deleted file mode 100644 index 14ca3cc..0000000 --- a/source/engine/ui.c_UNUSED +++ /dev/null @@ -1,71 +0,0 @@ -#include "foundation.h" -#include "vg_engine.h" -#include "vg_graphics.h" -#include "vg_render.h" -#include "vg_opengl.h" -#include "vg_shader.h" - -struct graphics_target _ui_surface = -{ - .mode = k_graphics_mode_software, - .size = { 1920, 1080 }, -}; - -static GLuint _ui_surface_texture; - -#include -void _engine_ui_init(void) -{ - _ui_surface.buffer = _heap_allocate( 1920*1080*4 ); - - glGenTextures( 1, &_ui_surface_texture ); - glActiveTexture( GL_TEXTURE0 ); - glBindTexture( GL_TEXTURE_2D, _ui_surface_texture ); - glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 1920, 1080, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL ); - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE ); - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE ); -} - -void _engine_ui_pre_render(void) -{ - ASSERT_CRITICAL( _engine.w <= 1920 ); - ASSERT_CRITICAL( _engine.h <= 1080 ); - - f64 x = 0, y = 0; -#if 0 - glfwGetCursorPos( _engine.window_handle, &x, &y ); -#endif - _ui_set_mouse( x, y ); - _ui_update(); - - _graphics_set_target( &_ui_surface ); - _graphics_viewport( 0, 0, _engine.w, _engine.h ); - //_graphics_fill_rect( (i16[]){ 0, 0, _engine.w, _engine.h }, (union colour){{0,0,0,255}} ); -} - -void _engine_ui_input_character( u32 codepoint ) -{ - if( (codepoint >= (u32)' ') && (codepoint <= (u32)'~') ) - _ui_input_text( (const c8[]){ codepoint, 0 } ); -} - -void _engine_ui_post_render(void) -{ - glEnable( GL_BLEND ); - glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); - glBlendEquation( GL_FUNC_ADD ); - - glActiveTexture( GL_TEXTURE0 ); - glBindTexture( GL_TEXTURE_2D, _ui_surface_texture ); - glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, 1920, 1080, GL_BGRA, GL_UNSIGNED_BYTE, _ui_surface.buffer ); - - _shader_bind( k_shader_blit ); - _shader_blit_uTexMain( 0 ); - _shader_blit_uFlip( 1 ); - _shader_blit_uInverseRatio( (f32[2]){ (f64)_engine.w/1920.0, (f64)_engine.h/1080.0 } ); - - _render_fullscreen_quad(); - glDisable( GL_BLEND ); -} diff --git a/source/engine/array_file.c b/source/engine/vg_af.c similarity index 99% rename from source/engine/array_file.c rename to source/engine/vg_af.c index 3c34f72..b1b73fb 100644 --- a/source/engine/array_file.c +++ b/source/engine/vg_af.c @@ -1,5 +1,5 @@ #include "foundation.h" -#include "array_file.h" +#include "vg_af.h" u32 af_str_hash( const void *packed_strings, u32 pstr ) { diff --git a/source/engine/array_file.h b/source/engine/vg_af.h similarity index 100% rename from source/engine/array_file.h rename to source/engine/vg_af.h diff --git a/source/engine/vg_audio.h b/source/engine/vg_audio.h index 0005b3c..3c6601a 100644 --- a/source/engine/vg_audio.h +++ b/source/engine/vg_audio.h @@ -63,6 +63,7 @@ struct audio_clip }; }; +void _audio_init(void); void vg_audio_device_init(void); void vg_audio_begin(void); diff --git a/source/engine/vg_audio_dsp.h b/source/engine/vg_audio_dsp.h index c9feb99..5e20ed0 100644 --- a/source/engine/vg_audio_dsp.h +++ b/source/engine/vg_audio_dsp.h @@ -1,5 +1,6 @@ //#define VG_ECHO_LPF_BUTTERWORTH #pragma once +void _dsp_init(void); void _dsp_update_tunings( f32 echo_distances[14] ); void _dsp_process( f32 *stereo_in, f32 *stereo_out ); diff --git a/source/engine/console.c b/source/engine/vg_console.c similarity index 100% rename from source/engine/console.c rename to source/engine/vg_console.c diff --git a/source/engine/vg_console.h b/source/engine/vg_console.h new file mode 100644 index 0000000..09750a3 --- /dev/null +++ b/source/engine/vg_console.h @@ -0,0 +1,5 @@ +#pragma once + +void _engine_console_init(void); +void _engine_console_update(void); +void _engine_console_ui(void); diff --git a/source/engine/vg_engine.c b/source/engine/vg_engine.c index d508054..8a16dc4 100644 --- a/source/engine/vg_engine.c +++ b/source/engine/vg_engine.c @@ -9,10 +9,17 @@ #include "vg_ui.h" #include "vg_audio.h" #include "vg_graphics.h" +#include "vg_audio_dsp.h" +#include "vg_audio.h" +#include "vg_shader.h" +#include "vg_render.h" +#include "vg_tex.h" +#include "vg_lines.h" +#include "vg_framebuffer.h" +#include "vg_console.h" // TODO: temp #include "console_system.h" -#include "generated/hooks.h" #if defined( __EMSCRIPTEN__ ) #include @@ -89,7 +96,12 @@ SDL_Thread *_async_thread_handle = NULL; i32 main( i32 argc, const c8 *argv[] ) { - VG_PRE_MAIN; + _exit_init(); + _log_init(); + _options_init( argc, argv ); + _engine_options(); + if( _vg_engine_hooks.options ) _vg_engine_hooks.options(); + _options_check_end(); $log( $info, {"SDL_INIT_VIDEO"} ); if( SDL_Init( SDL_INIT_VIDEO ) == 0 ) @@ -236,7 +248,19 @@ i32 main( i32 argc, const c8 *argv[] ) mt_random_seed( &_engine.random, 887765 ); - EVENT_CALL( START ); + _async_init(); + _dsp_init(); + _audio_init(); + _engine_ui_init(); + _input_init(); + _console_init(); + _engine_console_init(); + _vg_render_init(); + _shader_init(); + _vg_tex_init(); + _vg_lines_init(); + if( _vg_engine_hooks.start ) _vg_engine_hooks.start(); + _async_thread_handle = SDL_CreateThread( _async_thread, "ASync thread", NULL ); #if defined( __EMSCRIPTEN__ ) @@ -288,7 +312,9 @@ void _vg_engine_frame(void) while( _task_queue_process( 0 ) ) {} /* normal update */ - EVENT_CALL( ENGINE_NEW_FRAME ); + _input_update(); + _engine_console_update(); + if( _vg_engine_hooks.frame_start ) _vg_engine_hooks.frame_start(); /* fixed update */ _fixed_accumulator += _engine.time_delta; @@ -302,7 +328,7 @@ void _vg_engine_frame(void) _fixed_accumulator -= 1.0/60.0; _fixed_time += _engine.time_delta; _engine.time = _fixed_time; - EVENT_CALL( ENGINE_FIXED_UPDATE ); + if( _vg_engine_hooks.fixed_frame ) _vg_engine_hooks.fixed_frame(); } else break; } @@ -314,13 +340,13 @@ void _vg_engine_frame(void) //glfwGetFramebufferSize( _engine.window_handle, &_engine.w, &_engine.h ); SDL_GetWindowSizeInPixels( _engine.window_handle, &_engine.w, &_engine.h ); if( (pw != _engine.w) || (ph != _engine.h) ) - { - EVENT_CALL( ENGINE_WINDOW_RESIZE ); - } + _framebuffer_resize(); - EVENT_CALL( ENGINE_RENDER ); + if( _vg_engine_hooks.frame_render ) _vg_engine_hooks.frame_render(); _engine_ui_pre_render(); - EVENT_CALL( ENGINE_UI ); + + if( _vg_engine_hooks.frame_ui ) _vg_engine_hooks.frame_ui(); + _engine_console_ui(); _audio_gui(); _engine_ui_post_render(); @@ -333,7 +359,7 @@ void _vg_engine_frame(void) if( e.type == SDL_EVENT_QUIT ) { SDL_DestroyWindow( _engine.window_handle ); - EVENT_CALL( END ); + if( _vg_engine_hooks.end ) _vg_engine_hooks.end(); _task_queue_stop_normal( k_thread_async ); SDL_WaitThread( _async_thread_handle, NULL ); SDL_Quit(); diff --git a/source/engine/vg_engine.h b/source/engine/vg_engine.h index 8f6e563..ea1c8f4 100644 --- a/source/engine/vg_engine.h +++ b/source/engine/vg_engine.h @@ -23,3 +23,15 @@ struct _engine struct mt_random random; } extern _engine; + +struct _vg_engine_hooks +{ + void (*options)( void ); + void (*start)( void ); + void (*frame_start)( void ); + void (*fixed_frame)( void ); + void (*frame_render)( void ); + void (*frame_ui)( void ); + void (*end)( void ); +} +extern _vg_engine_hooks; diff --git a/source/engine/vg_engine_core.kv b/source/engine/vg_engine_core.kv deleted file mode 100644 index d639c16..0000000 --- a/source/engine/vg_engine_core.kv +++ /dev/null @@ -1,379 +0,0 @@ -{ - add vg_engine.c - hook - { - event OPTIONS - function _engine_options - } - event - { - name ENGINE_NEW_FRAME - prototype "void" - } - event - { - name ENGINE_FIXED_UPDATE - prototype "void" - } - event - { - name ENGINE_RENDER - prototype "void" - } - event - { - name ENGINE_UI - prototype "void" - } - event - { - name ENGINE_WINDOW_RESIZE - prototype "void" - } -} - -{ - add ui.c - hook - { - event START - function _engine_ui_init - } -} - -{ - add input.c - hook - { - event START - function _input_init - } - hook - { - event ENGINE_NEW_FRAME - function _input_update - } - ccmd - { - name bind - function _input_bind_ccmd - description "Bind device input to a button, action or axis" - - parameter - { - description "Device input alias" - } - - parameter - { - description "button,action,axis name" - } - } - ccmd - { - name unbind - description "Unbind all bindings that match" - function _input_unbind_ccmd - - parameter - { - description "binding" - } - } -} - -{ - add console.c - hook - { - event START - function _engine_console_init - } - hook - { - event ENGINE_NEW_FRAME - function _engine_console_update - } - hook - { - event ENGINE_UI - function _engine_console_ui - affinity 9999 - } -} - -{ - add vg_framebuffer.c - hook - { - event ENGINE_WINDOW_RESIZE - function _framebuffer_resize - } -} - -{ -add source/engine/shader.c -input_layer -{ - name console -} -input_layer -{ - name ui -} -input -{ - name console - type action - layer_mask console -} - -append async.kv -thread -{ - name main - queue_size_m 40 - flag MAIN - flag OPENGL -} -thread -{ - name async - queue_size_m 40 - flag ASYNC -} -thread -{ - name audio -} -thread -{ - name exitor - queue_size_m 1 -} - -append graphics.kv - -ccmd -{ - name exec - function _console_exec_ccmd - description "Execute a configuration file" - - parameter - { - description "The path to the config" - } -} -config "bind ALT+GRAVE_ACCENT console" - -config "bind BACKSPACE ui_backspace" -config "bind DELETE ui_delete" -config "bind ENTER ui_enter" -config "bind TAB ui_indent" -config "bind HOME ui_home" -config "bind END ui_end" -config "bind LEFT ui_left" -config "bind RIGHT ui_right" -config "bind UP ui_up" -config "bind DOWN ui_down" -config "bind MOUSE_LEFT ui_click" - -config "bind LEFT_SHIFT ui_select" -config "bind RIGHT_SHIFT ui_select" - -input -{ - name ui_delete - type action - layer_mask ui -} -input -{ - name ui_backspace - type action - layer_mask ui -} -input -{ - name ui_enter - type action - layer_mask ui -} -input -{ - name ui_indent - type action - layer_mask ui -} - -input -{ - name ui_select - type button - layer_mask ui -} -input -{ - name ui_home - type button - layer_mask ui -} -input -{ - name ui_end - type button - layer_mask ui -} -input -{ - name ui_left - type button - layer_mask ui -} -input -{ - name ui_right - type button - layer_mask ui -} -input -{ - name ui_up - type button - layer_mask ui -} -input -{ - name ui_down - type button - layer_mask ui -} -input -{ - name ui_click - type action - layer_mask ui -} - -shader -{ - name blit - - subshader - { - type vertex - add shaders/blit.vs - - uniform - { - type vec2 - alias uInverseRatio - } - uniform - { - type int - alias uFlip - } - } - - subshader - { - type fragment - add shaders/blit_tex.fs - - uniform - { - type sampler2D - alias uTexMain - } - } -} - -cvar -{ - name test - type u32 - default 5 - cheat 1 - description "This is just a test variable!" -} - -hook -{ - event START - function "_shaders_compile" -} -} - - - - - - -add vg_camera.c - - -add vg_tex.c -hook -{ - event START - function _vg_tex_init -} - - -add vg_lines.c -hook -{ - event START - function _vg_lines_init -} -cvar -{ - name debug_lines - type u32 - default 0 - cheat 1 - description "Show line debuggers" -} -shader -{ - name debug_lines - subshader - { - type vertex - add shaders/debug_lines.vs - uniform - { - type mat4 - alias uPv - } - } - subshader - { - type fragment - add shaders/debug_lines.fs - } -} - - -add vg_audio.c -add vg_audio_dsp.c -add vorbis.c -hook -{ - affinity -3008 - event START - function _audio_init -} -hook -{ - affinity -6004 - event START - function _dsp_init -} - - -add model.c -add metascene.c -add array_file.c diff --git a/source/engine/vg_entity.c b/source/engine/vg_entity.c new file mode 100644 index 0000000..99aa597 --- /dev/null +++ b/source/engine/vg_entity.c @@ -0,0 +1,15 @@ +#include "vg_entity.h" +#include "generated/entities.c" + +i32 _vg_entity_link_function( u16 entity_type, const c8 *alias ) +{ + struct entity_info *info = _vg_entity_info( entity_type ); + for( u16 i=0; ifunction_count; i ++ ) + { + struct entity_function_info *func_info = &_entity_function_infos[ info->function_start + i ]; + if( compare_buffers( alias, 0, func_info->name, 0 ) ) + return (i32)i; + } + $log( $error, {"Failed to link function from alias '"}, {alias}, {"'"} ); + return -1; +} diff --git a/source/engine/model_entity.h b/source/engine/vg_entity.h similarity index 88% rename from source/engine/model_entity.h rename to source/engine/vg_entity.h index 682bb66..40e835b 100644 --- a/source/engine/model_entity.h +++ b/source/engine/vg_entity.h @@ -1,6 +1,9 @@ #pragma once -#include "model.h" +#include "vg_model.h" +#include "generated/entities.h" + +#if 0 enum entity_alias { k_ent_none = 0, @@ -51,60 +54,11 @@ enum entity_alias k_ent_deep_npc = 208, k_ent_max }; +#endif -// FIXME -static const char *_entity_alias_str[] = -{ - [k_ent_none] = "none/null", - [k_ent_gate] = "ent_gate", - [k_ent_spawn] = "ent_spawn", - [k_ent_route_node] = "ent_route_node", - [k_ent_route] = "ent_route", - [k_ent_water] = "ent_water", - [k_ent_volume] = "ent_volume", - [k_ent_audio] = "ent_audio", - [k_ent_marker] = "ent_marker", - [k_ent_font] = "ent_font", - [k_ent_font_variant] = "ent_font_variant", - [k_ent_traffic] = "ent_traffic", - [k_ent_skateshop] = "ent_skateshop", - [k_ent_camera] = "ent_camera", - [k_ent_swspreview] = "ent_swspreview", - //[k_ent_menuitem] = "ent_menuitem", - [k_ent_worldinfo] = "ent_worldinfo", - [k_ent_ccmd] = "ent_ccmd", - [k_ent_objective] = "ent_objective", - [k_ent_challenge] = "ent_challenge", - //[k_ent_relay] = "ent_relay", - [k_ent_cubemap] = "ent_cubemap", - [k_ent_miniworld] = "ent_miniworld", - [k_ent_prop] = "ent_prop", - [k_ent_region] = "ent_region", - [k_ent_glider] = "ent_glider", - [k_ent_npc] = "ent_npc", - [k_ent_armature] = "mdl_armature", - [k_ent_script] = "ent_script", - [k_ent_atom] = "ent_atom", - [k_ent_cutscene] = "ent_cutscene", - [k_ent_light] = "ent_light", - [k_ent_max] = NULL -}; - -static inline u32 mdl_entity_id_type( u32 entity_id ) -{ - return (entity_id & 0x0fff0000) >> 16; -} - -static inline u32 mdl_entity_id_id( u32 entity_id ) -{ - return entity_id & 0x0000ffff; -} - -static inline u32 mdl_entity_id( u32 type, u32 index ) -{ - return (type & 0xfffff)<<16 | (index & 0xfffff); -} +const c8 *_vg_entity_name( u32 entity_id ); +#if 0 enum ent_spawn_flag { k_ent_spawn_flag_locked = 0x1, @@ -120,6 +74,7 @@ struct ent_spawn u32 pstr_name; u32 flags; }; +#endif enum light_type { @@ -130,6 +85,7 @@ enum light_type #define ENT_LIGHT_FLAG_DAYTIME 0x1 #define ENT_LIGHT_FLAG_OFF 0x2 +#if 0 struct ent_light { struct mdl_transform transform; @@ -155,6 +111,7 @@ struct ent_light }; }; }; +#endif /* v101 */ #if 0 @@ -181,7 +138,8 @@ struct ent_list struct file_entity_ref { - u32 entity_id, pstr_alias; + union entity_id entity; + u32 pstr_alias; }; /* v102+ */ @@ -745,7 +703,8 @@ enum entity_event_result { k_entity_event_result_OK, k_entity_event_result_unhandled, - k_entity_event_result_invalid + k_entity_event_result_invalid, + k_entity_event_result_invalid_parameter, }; enum ent_event_flags @@ -756,27 +715,43 @@ enum ent_event_flags k_ent_event_data_const_entity_id = 0x4, k_ent_event_data_const_string = 0x8, k_ent_event_data_data_alias = 0x10, - k_ent_event_data_v3f = 0x20 + k_ent_event_data_v3f = 0x20, }; struct ent_event { - u32 pstr_source_event, - pstr_recieve_event, - source_entity_id, - recieve_entity_id, - flags; + u32 pstr_source_event; + u32 pstr_recieve_event; + + union entity_id source; + union entity_id reciever; + u32 flags; f32 delay; - u32 unused0; + u8 function_index, activator, unused0, unused1; // FIXME: REPLACES PSTR!!!!!!!!!!! union { i32 const_i32; f32 const_f32; - u32 const_entity_id; + union entity_id const_entity_id; u32 const_pstr; u32 pstr_data_alias; } data; }; + +struct entity_info +{ + u16 id, unused0; + u16 function_start, function_count; + const c8 *name; +}; +struct entity_function_info +{ + const c8 *name; +}; + +struct entity_info *_vg_entity_info( u16 entity_type ); +enum entity_event_result _vg_entity_event_dispatch( struct ent_event *event ); +i32 _vg_entity_link_function( u16 entity_type, const c8 *alias ); diff --git a/source/engine/vg_framebuffer.h b/source/engine/vg_framebuffer.h index a086efa..ed3b41f 100644 --- a/source/engine/vg_framebuffer.h +++ b/source/engine/vg_framebuffer.h @@ -56,6 +56,8 @@ struct framebuffer attachments[]; }; +void _framebuffer_resize(void); + struct framebuffer *_framebuffer_allocate( struct stack_allocator *stack, u32 attachment_count, b8 track ); /* diff --git a/source/engine/vg_input.h b/source/engine/vg_input.h index c26b669..8fadff2 100644 --- a/source/engine/vg_input.h +++ b/source/engine/vg_input.h @@ -33,6 +33,9 @@ enum input_button_action }; void _input_callback_buttonlike( enum input_device device, i32 id, enum input_button_action action, u32 mods ); +void _input_init(void); +void _input_update(void); + u8 _input_button_down( enum input_id id, b8 allow_repeats ); u8 _input_button_up( enum input_id id ); u8 _input_button( enum input_id id ); diff --git a/source/engine/vg_lines.h b/source/engine/vg_lines.h index 62fb781..6a2a356 100644 --- a/source/engine/vg_lines.h +++ b/source/engine/vg_lines.h @@ -1,3 +1,4 @@ +#pragma once #define LINE_RED 0xff0000ff #define LINE_GREEN 0xff00ff00 #define LINE_BLUE 0xffff0000 @@ -9,6 +10,7 @@ #define LINE_CYAN 0xffffff00 #define LINE_NONE 0x00000000 +void _vg_lines_init(void); void vg_lines_clear(void); void vg_lines_draw( f32 pv[4][4] ); diff --git a/source/engine/metascene.c b/source/engine/vg_metascene.c similarity index 99% rename from source/engine/metascene.c rename to source/engine/vg_metascene.c index f7328ac..783a965 100644 --- a/source/engine/metascene.c +++ b/source/engine/vg_metascene.c @@ -1,10 +1,10 @@ #include "vg_engine.h" -#include "metascene.h" +#include "vg_metascene.h" #include "common_maths.h" -#include "model.h" -#include "model_entity.h" +#include "vg_model.h" +#include "vg_entity.h" #include "shader_props.h" -#include "array_file.h" +#include "vg_af.h" #include "vg_async.h" void metascene_load( struct metascene *ms, const c8 *path, struct stack_allocator *stack ) diff --git a/source/engine/metascene.h b/source/engine/vg_metascene.h similarity index 98% rename from source/engine/metascene.h rename to source/engine/vg_metascene.h index 505d353..6b3c1ac 100644 --- a/source/engine/metascene.h +++ b/source/engine/vg_metascene.h @@ -1,7 +1,8 @@ #pragma once #include "foundation.h" -#include "array_file.h" -#include "model.h" +#include "vg_af.h" +#include "vg_model.h" +#include "vg_entity.h" #define MS_VERSION_NR 2 #define MS_VERSION_MIN 2 @@ -73,7 +74,7 @@ struct ms_strip struct { - u32 entity_id; + union entity_id entity; } camera; diff --git a/source/engine/model.c b/source/engine/vg_model.c similarity index 99% rename from source/engine/model.c rename to source/engine/vg_model.c index c02999c..fb1787a 100644 --- a/source/engine/model.c +++ b/source/engine/vg_model.c @@ -2,8 +2,8 @@ #include "common_maths.h" #include "vg_async.h" -#include "model.h" -#include "array_file.h" +#include "vg_model.h" +#include "vg_af.h" #include "vg_opengl.h" #include "shader_props.h" #include diff --git a/source/engine/model.h b/source/engine/vg_model.h similarity index 96% rename from source/engine/model.h rename to source/engine/vg_model.h index f6b260c..936e065 100644 --- a/source/engine/model.h +++ b/source/engine/vg_model.h @@ -1,11 +1,21 @@ #pragma once #include "vg_tex.h" #include "shader_props.h" -#include "array_file.h" +#include "vg_af.h" #define VG_MODEL_VERSION_MIN 110 #define VG_MODEL_VERSION_NR 110 +union entity_id +{ + u32 id; + struct + { + u16 index; + u16 type; + }; +}; + enum mdl_shader { k_shader_standard = 0, @@ -178,10 +188,9 @@ struct mdl_mesh struct mdl_transform transform; u32 submesh_start, submesh_count, - pstr_name, - entity_id, /* upper 16 bits: type, lower 16 bits: index. hgn: 11.06.2025 Is this still used??? - hgn: 25.10.2025 Yes it is. */ - armature_id; + pstr_name; + union entity_id entity; + union entity_id armature; }; struct mdl_file diff --git a/source/engine/vg_render.h b/source/engine/vg_render.h index f052b0a..84946a5 100644 --- a/source/engine/vg_render.h +++ b/source/engine/vg_render.h @@ -1,2 +1,4 @@ #pragma once + +void _vg_render_init(void); void _render_fullscreen_quad(void); diff --git a/source/engine/vg_shader.h b/source/engine/vg_shader.h index 76ee8e7..1f2c925 100644 --- a/source/engine/vg_shader.h +++ b/source/engine/vg_shader.h @@ -1,6 +1,7 @@ #include "vg_opengl.h" #include "generated/shaders.h" +void _shader_init(void); GLuint compile_opengl_subshader( GLint type, const c8 *sources[], u32 source_count, b8 critical, const c8 *name ); b8 link_opengl_program( GLuint program, b8 critical ); void _shader_bind( enum shader_id id ); diff --git a/source/engine/vg_tex.h b/source/engine/vg_tex.h index 89c14c4..df597d4 100644 --- a/source/engine/vg_tex.h +++ b/source/engine/vg_tex.h @@ -28,6 +28,8 @@ struct qoi_desc # define QOI_SRGB 0 # define QOI_LINEAR 1 +void _vg_tex_init(void); + /* Reading qois */ u32 vg_qoi_stream_init( struct qoi_desc *desc, struct stream *file ); void vg_qoi_stream_decode( struct qoi_desc *desc, struct stream *file, u8 *dest_buffer, b8 v_flip ); diff --git a/source/engine/vg_ui.h b/source/engine/vg_ui.h index 22d8b25..fe8fec3 100644 --- a/source/engine/vg_ui.h +++ b/source/engine/vg_ui.h @@ -7,6 +7,7 @@ struct _engine_ui } extern _engine_ui; +void _engine_ui_init(void); void _engine_ui_pre_render(void); void _engine_ui_post_render(void); b8 _engine_ui_want_text_input(void); diff --git a/source/foundation/foundation.h b/source/foundation/foundation.h index ad108a4..fe6fb81 100644 --- a/source/foundation/foundation.h +++ b/source/foundation/foundation.h @@ -1,12 +1,14 @@ /* Voyager common application interface */ #pragma once +#if 0 #define VG_PRE_MAIN \ _exit_init(); \ _log_init(); \ _options_init( argc, argv ); \ EVENT_CALL( OPTIONS ); \ _options_check_end(); +#endif #define BYTES_KB( X ) X*1024 #define BYTES_MB( X ) X*1024*1024 diff --git a/source/foundation/foundation.kv b/source/foundation/foundation.kv index 6145269..c4c4991 100644 --- a/source/foundation/foundation.kv +++ b/source/foundation/foundation.kv @@ -1,28 +1,6 @@ include "" - -event -{ - name OPTIONS - prototype "void" -} -event -{ - name START - prototype "void" -} -event -{ - name END - prototype "void" -} - add options.c add logging.c -{ - hook OPTIONS - function _log_options -} - add allocator_heap.c add allocator_stack.c add allocator_pool.c diff --git a/source/foundation/keyvalues.c b/source/foundation/keyvalues.c index dc0984a..39898fd 100644 --- a/source/foundation/keyvalues.c +++ b/source/foundation/keyvalues.c @@ -393,10 +393,11 @@ void keyvalues_parse_stream( struct keyvalues *kvs, u32 root_offset, struct stre while( stream_read( in_stream, &c, 1 ) ) { parser.stat_source_characters ++; - if( c == '\0' ) - break; b8 is_control_character = 0; + if( c == 0 ) + is_control_character = 1; + if( parser.token0_deliminator ) { if( c == parser.token0_deliminator ) @@ -439,40 +440,38 @@ void keyvalues_parse_stream( struct keyvalues *kvs, u32 root_offset, struct stre parser.token0_length = 0; } - if( c=='{'||c=='}'||c=='\n' ) + if( c == '{' ) { - if( c == '{' ) + struct keyvalue *kv = keyvalues_new( parser.kvs ); + if( parser.token1_length ) { - struct keyvalue *kv = keyvalues_new( parser.kvs ); - if( parser.token1_length ) - { - kv->key_info = (0xFFFFF & parser.token1_hash) | (0x3FF & parser.token1_length) << 20; - kv->key_offset = parser.token1_start_offset; - } - else - kv->key_info = 5381; - - u32 id = stack_offset( parser.kvs->stack, kv ), - depth = parser.depth; - - parser.depth ++; - parser.frame_stack[ parser.depth ].latest_child_offset = 0; - parser.frame_stack[ parser.depth ].frame_offset = id; - keyvalue_parser_link( &parser, id, depth ); - parser.token1_length = 0; + kv->key_info = (0xFFFFF & parser.token1_hash) | (0x3FF & parser.token1_length) << 20; + kv->key_offset = parser.token1_start_offset; } - else if( c == '}' ) + else + kv->key_info = 5381; + + u32 id = stack_offset( parser.kvs->stack, kv ), + depth = parser.depth; + + parser.depth ++; + parser.frame_stack[ parser.depth ].latest_child_offset = 0; + parser.frame_stack[ parser.depth ].frame_offset = id; + keyvalue_parser_link( &parser, id, depth ); + parser.token1_length = 0; + } + else if( c == '}' ) + { + if( parser.depth ) + parser.depth --; + else { - if( parser.depth ) - parser.depth --; - else - { - /* We specify that a depth of -1 by way of closing bracket is just an END marker and not an error */ - return; - } - parser.token1_length = 0; + /* We specify that a depth of -1 by way of closing bracket is just an END marker and not an error */ + return; } + parser.token1_length = 0; } + parser.token0_deliminator = 0; } else @@ -506,6 +505,9 @@ void keyvalues_parse_stream( struct keyvalues *kvs, u32 root_offset, struct stre } } } + + if( c == '\0' ) + break; } } diff --git a/source/tools/metacompiler.c b/source/tools/metacompiler.c index 8d0c6e0..8601165 100644 --- a/source/tools/metacompiler.c +++ b/source/tools/metacompiler.c @@ -13,6 +13,12 @@ u32 _option_optimise = 0; struct stream _assemble_file; struct keyvalues _assembly; +b8 _option_codegen_shaders = 0; +b8 _option_codegen_inputs = 0; +b8 _option_codegen_console = 0; +b8 _option_codegen_threads = 0; +b8 _option_codegen_entities= 0; + enum libc_version { k_libc_version_native = 0, @@ -58,13 +64,6 @@ const c8 *platform_names[] = [k_platform_web] = "web" }; -struct -{ - struct stream name, tripple, folder; - const c8 *enabled_features[ 64 ]; -} -static _metacompiler; - struct shell_command { u32 temp_frame; @@ -87,27 +86,79 @@ void _shell_run( struct shell_command *command ) _end_temporary_frame( command->temp_frame ); } -u32 _assembly_kv_file_context( u32 kv ) +struct +{ + struct stream name, tripple, folder; + const c8 *enabled_features[ 64 ]; +} +static _metacompiler; + +struct stack_allocator _source_text; +struct file_context { - u32 vgc_block = keyvalues_get( &_assembly, 0, "vgc", 0 ); - ASSERT_CRITICAL( vgc_block ); + const c8 *full_path; + u32 assembly_kv_start, assembly_kv_end; +}; - u32 narrowest = 0; - u32 it = 0; - while( keyvalues_foreach( &_assembly, &it, vgc_block, "file_context" ) ) +struct stretchy_allocator _files; + +struct file_context *_assembly_kv_file_context( u32 kv ) +{ + i32 narrowest = -1; + for( i32 i=0; iassembly_kv_start && (kv >= file->assembly_kv_start) && (kv < file->assembly_kv_end) ) + narrowest = i; + } + + ASSERT_CRITICAL( narrowest != -1 ); + return stretchy_get( &_files, narrowest ); +} + +struct file_context *_create_file_context( const c8 *path ) +{ + c8 *full_path = realpath( path, NULL ); + + /* Add it */ + struct file_context *file_context = stretchy_append( &_files ); + u32 path_length = buffer_last_index(full_path,0,0); + c8 *dest = stack_allocate( &_source_text, path_length+1, 8, "" ); + buffer_copy( full_path, 0, dest, path_length+1 ); + file_context->full_path = dest; + file_context->assembly_kv_start = 0; + file_context->assembly_kv_end = 0; + free( full_path ); + return file_context; +} + +/* This is for getting a path for external tools */ +void _cannonicalize( struct file_context *file_context, const c8 *path, struct stream *dest_string ) +{ + c8 path_buf[4096]; + struct stream cannon_path; + stream_open_buffer_write( &cannon_path, path_buf, sizeof(path_buf), k_stream_null_terminate ); - if( (kv >= min) && (kv <= max) ) - narrowest = it; + if( path[0] != '/' ) + { + i32 context_path_folder_seperator = buffer_last_index( file_context->full_path, '/', 0 ); + ASSERT_CRITICAL( context_path_folder_seperator > -1 ); + string_append( &cannon_path, file_context->full_path, context_path_folder_seperator ); + string_append( &cannon_path, "/", 0 ); + } + string_append( &cannon_path, path, 0 ); + c8 *source_file_path = realpath( string_get( &cannon_path ), NULL ); + if( !source_file_path ) + { + $log( $error, {string_get(&cannon_path)}, {"... "}, $errno() ); + ASSERT_CRITICAL( source_file_path ); } - return narrowest; + string_append( dest_string, source_file_path, 0 ); + free( source_file_path ); } -void _assemble_kv_file( const c8 *path, u32 assembly_block, u32 feature_count ); -void _parse_kv_block( struct keyvalues *kvs, u32 block, u32 file_context, u32 assembly_block, u32 feature_count ) +void _assemble_unit_file( const c8 *path, u32 assembly_block, u32 feature_count ); +void _parse_kv_block( struct keyvalues *kvs, u32 block, struct file_context *file_context, u32 assembly_block, u32 feature_count ) { u32 it = 0; while( keyvalues_foreach( kvs, &it, block, NULL ) ) @@ -150,12 +201,8 @@ void _parse_kv_block( struct keyvalues *kvs, u32 block, u32 file_context, u32 as { struct stream path_string; stream_open_stack( &path_string, _temporary_stack_allocator(), k_stream_null_terminate ); - - const c8 *folder = keyvalues_read_string( &_assembly, file_context, "folder", NULL ); - ASSERT_CRITICAL( folder ); - - $v_string( &path_string, {folder}, {"/"}, {it_v} ); - _assemble_kv_file( string_get( &path_string ), assembly_block, feature_count ); + _cannonicalize( file_context, it_v, &path_string ); + _assemble_unit_file( string_get( &path_string ), assembly_block, feature_count ); } _end_temporary_frame( temp_frame ); } @@ -167,38 +214,19 @@ void _parse_kv_block( struct keyvalues *kvs, u32 block, u32 file_context, u32 as } } -void _assemble_kv_file( const c8 *path, u32 assembly_block, u32 feature_count ) +void _assemble_unit_file( const c8 *path, u32 assembly_block, u32 feature_count ) { - /* Append context block */ - u32 vgc_block = keyvalues_get( &_assembly, 0, "vgc", 0 ); - ASSERT_CRITICAL( vgc_block ); - u32 context_block = keyvalues_append_frame( &_assembly, vgc_block, "file_context" ); - - u32 start_offset = keyvalues_current_offset( &_assembly ); - u32 temp_frame = _start_temporary_frame(); - c8 *folder = realpath( path, NULL ); - if( !folder ) { - $log( $fatal, {"'"}, {path}, {"' "}, $errno() ); - _fatal_exit(); - } - - i32 s = buffer_last_index( folder, '/', 0 ); - if( s != -1 ) - folder[s] = '\0'; + struct file_context *file_context = _create_file_context( path ); + file_context->assembly_kv_start = keyvalues_current_offset( &_assembly ); - keyvalues_append_string( &_assembly, context_block, "folder", folder ); - free( folder ); + struct keyvalues kvs; + ASSERT_CRITICAL( keyvalues_read_file( &kvs, path, _temporary_stack_allocator() ) ); + _parse_kv_block( &kvs, 0, file_context, assembly_block, feature_count ); - /* read file and pre-process it into the assembly */ - struct keyvalues kvs; - ASSERT_CRITICAL( keyvalues_read_file( &kvs, path, _temporary_stack_allocator() ) ); - _parse_kv_block( &kvs, 0, context_block, assembly_block, feature_count ); - - u32 end_offset = keyvalues_current_offset( &_assembly ); - keyvalues_append_u32s( &_assembly, context_block, "kv_start", &start_offset, 1 ); - keyvalues_append_u32s( &_assembly, context_block, "kv_end", &end_offset, 1 ); + file_context->assembly_kv_end = keyvalues_current_offset( &_assembly ); + } _end_temporary_frame( temp_frame ); } @@ -254,6 +282,14 @@ void _metacompiler_options(void) if( _option_flag( 'O', "Run optimisers" ) ) _option_optimise = 3; + b8 all = 0; + if( _option_long( "meta-all", "All metadata programs" ) ) all = 1; + if( _option_long( "meta-shaders", "Make shader headers" ) || all ) _option_codegen_shaders = 1; + if( _option_long( "meta-inputs", "Make input headers" ) || all ) _option_codegen_inputs = 1; + if( _option_long( "meta-console", "Make console headers" )|| all ) _option_codegen_console = 1; + if( _option_long( "meta-threads", "Make thread headers" ) || all ) _option_codegen_threads = 1; + if( _option_long( "meta-entities", "Make entity headers" ) || all ) _option_codegen_entities = 1; + if( (arg = _option_argument( 'E', "Assemble target KV's only, and output it to file" )) ) { _option_assemble_only = 1; @@ -261,7 +297,6 @@ void _metacompiler_options(void) } _option_file_path = _option(); - ASSERT_CRITICAL( _option_file_path ); } void (*_event_OPTIONS_subscribers[])( void ) = @@ -280,60 +315,6 @@ void index_sort( struct sort_index *indices, u32 indice_count ) qsort( indices, indice_count, sizeof(struct sort_index), compar ); } -void _codegen_hooks(void) -{ - $log( $info, {"[CODEGEN] hooks.h/hooks.c"} ); - struct stream hooks_c, hooks_h; - stream_open_file( &hooks_h, "generated/hooks.h", k_stream_write ); - stream_open_file( &hooks_c, "generated/hooks.c", k_stream_write ); - - u32 it = 0; - while( keyvalues_foreach( &_assembly, &it, 0, "event" ) ) - { - const c8 *name = keyvalues_read_string( &_assembly, it, "name", NULL ); - const c8 *prototype = keyvalues_read_string( &_assembly, it, "prototype", NULL ); - const c8 *returns = keyvalues_read_string( &_assembly, it, "returns", "void" ); - ASSERT_CRITICAL( name && prototype && returns ); - - $v_string( &hooks_h, {"extern "}, {returns}, {" (*_event_"}, {name}, {"_subscribers[])( "}, {prototype}, {" );\n"} ); - - struct sort_index indices[ 128 ]; - u32 indice_count = 0; - - u32 user_it = 0; - while( keyvalues_foreach( &_assembly, &user_it, 0, "hook" ) ) - { - if( compare_buffers( keyvalues_read_string( &_assembly, user_it, "event", NULL ), 0, name, 0 ) ) - { - i32 affinity = 0; - keyvalues_read_i32s( &_assembly, user_it, "affinity", NULL, &affinity, 1 ); - - ASSERT_CRITICAL( indice_count < ARRAY_COUNT( indices ) ); - indices[ indice_count ].index = user_it; - indices[ indice_count ++ ].value = affinity; - - const c8 *function = keyvalues_read_string( &_assembly, user_it, "function", NULL ); - ASSERT_CRITICAL( function ); - $v_string( &hooks_c, {returns}, {" "}, {function}, {"( "}, {prototype}, {" );\n"} ); - } - } - - index_sort( indices, indice_count ); - - $v_string( &hooks_c, {returns}, {" (*_event_"}, {name}, {"_subscribers[])( "}, {prototype}, {" ) = \n{\n"} ); - for( u32 i=0; i 0) && (id < 0xffff) ); + entity_ids[ entity_count ].value = id; + + for( u32 i=0; ifunction_start = function_count; + + if( !keyvalues_get( &_assembly, ent->block, "parameter", 0 ) ) + { + $log( $warning, {"Entity '"}, {ent->name}, {"' has no parameters. Structure wont be created!"} ); + } + else + { + /* Parameter flags */ + { + u32 parameter_it = 0; + while( keyvalues_foreach( &_assembly, ¶meter_it, ent->block, "parameter" ) ) + { + if( keyvalues_get( &_assembly, parameter_it, "flag", 0 ) ) + { + const c8 *param_name = keyvalues_read_string( &_assembly, parameter_it, "name", NULL ); + ASSERT_CRITICAL( param_name ); + $v_string( &H, {"enum "}, {ent->name}, {"_"}, {param_name}, {"\n{\n"} ); + u32 flag_it = 0; + while( keyvalues_foreach( &_assembly, &flag_it, parameter_it, "flag" ) ) + { + const c8 *flag_name = keyvalues_read_string( &_assembly, flag_it, "name", NULL ); + ASSERT_CRITICAL( flag_name ); + const c8 *value = keyvalues_read_string( &_assembly, flag_it, "value", NULL ); + ASSERT_CRITICAL( value ); + $v_string( &H, {" k_"}, {ent->name}, {"_"}, {param_name}, {"_"}, {flag_name}, {" = "}, {value}, {",\n"} ); + } + $v_string( &H, {"};\n"} ); + } + } + } + + /* structure definition */ + { + $v_string( &H, {"struct "}, {ent->name}, {"\n{\n"} ); + u32 parameter_it = 0; + while( keyvalues_foreach( &_assembly, ¶meter_it, ent->block, "parameter" ) ) + { + const c8 *name = keyvalues_read_string( &_assembly, parameter_it, "name", NULL ); + ASSERT_CRITICAL( name ); + + const c8 *type = keyvalues_read_string( &_assembly, parameter_it, "type", NULL ); + ASSERT_CRITICAL( type ); + struct type_trans + { + const c8 *alias, + *c_type, + *c_post, + *py_type; + } + const types[] = + { + { "transform", "struct mdl_transform", "", "mdl_transform" }, + { "pstr", "u32", "", "c_uint32" }, + { "vec2", "f32", "[2]", "c_float*2" }, + { "vec3", "f32", "[3]", "c_float*3" }, + { "vec4", "f32", "[4]", "c_float*4" }, + { "quat", "f32", "[4]", "c_float*4" }, + { "bbx", "f32", "[2][3]","(c_float*3)*2" }, + { "mat3x3", "f32", "[3][3]","(c_float*3)*3" }, + { "mat4x3", "f32", "[4][3]","(c_float*4)*3" }, + { "mat4x4", "f32", "[4][4]","(c_float*4)*4" }, + + { "u8", "u8", "","c_uint8" }, + { "u16", "u16", "","c_uint16" }, + { "u32", "u32", "","c_uint32" }, + { "u64", "u64", "","c_uint64" }, + { "i8", "i8", "","c_int8" }, + { "i16", "i16", "","c_int16" }, + { "i32", "i32", "","c_int32" }, + { "i64", "i64", "","c_int64" }, + + { "f32", "f32", "","c_float" }, + { "f64", "f64", "","c_double" }, + }; + + i32 type_index = -1; + for( i32 i=0; iblock, "function" ) ) + { + const c8 *name = keyvalues_read_string( &_assembly, function_it, "name", NULL ); + ASSERT_CRITICAL( name ); + $v_string( &C, {"enum entity_event_result "}, {ent->name}, {"_event_"}, {name}, {"( struct ent_event *event );\n"} ); + function_count ++; + ent->function_count ++; + } + } + + /* entity info structures */ + $v_string( &C, {"/* Enitity information */\n"} ); + $v_string( &C, {"struct entity_info _entity_infos[] = {\n"} ); + for( u32 i=0; iname}, {"\", .function_start="}, + $unsigned( ent->function_start ), {", .function_count = "}, + $unsigned( ent->function_count ), {" },\n"} ); + } + $v_string( &C, {"};\n"} ); + + + /* function info structures (indexed by entity structures) */ + $v_string( &C, {"/* Function information */\n"} ); + $v_string( &C, {"struct entity_function_info _entity_function_infos[] = {\n"} ); + for( u32 i=0; iblock, "function" ) ) + { + const c8 *name = keyvalues_read_string( &_assembly, function_it, "name", NULL ); + ASSERT_CRITICAL( name ); + $v_string( &C, {" { .name = \""}, {name}, {"\" },\n"} ); + } + } + $v_string( &C, {"};\n"} ); + + /* Entity info, and function to get it by entity ID */ + $v_string( &C, {"struct entity_info *_vg_entity_info( u16 entity_type )\n{\n"} ); + for( u32 i=0; ireciever.type == "}, $signed(ent_id), {" ){\n"} ); + u32 function_it = 0; + u32 j = 0; + while( keyvalues_foreach( &_assembly, &function_it, ent->block, "function" ) ) + { + const c8 *name = keyvalues_read_string( &_assembly, function_it, "name", NULL ); + ASSERT_CRITICAL( name ); + $v_string( &C, {" if( event->function_index == "}, $unsigned(j), {" ) return "}, + {ent->name}, {"_event_"}, {name}, {"( event );\n"} ); + j ++; + } + $v_string( &C, {" }\n"} ); + } + $v_string( &C, {" return k_entity_event_result_invalid;\n"} ); + $v_string( &C, {"}\n"} ); + + stream_close( &H ); + stream_close( &C ); + stream_close( &PY ); +} + void _default_config_gen(void) { $log( $info, {"[CODEGEN] default.cfg"} ); @@ -691,13 +907,19 @@ void _default_config_gen(void) i32 main( i32 argc, const c8 *argv[] ) { - VG_PRE_MAIN; + _exit_init(); + _log_init(); + _options_init( argc, argv ); + _metacompiler_options(); + _options_check_end(); + ASSERT_CRITICAL( _option_file_path ); /* Get name of target and do a few checks */ i32 name_start = buffer_last_index( _option_file_path, '/', 0 ); if( name_start == -1 ) name_start = 0; i32 name_end = buffer_last_index( _option_file_path+name_start, '.', 0 ); +#if 0 b8 valid_kv = 1; if( name_end == -1 ) valid_kv = 0; @@ -709,6 +931,7 @@ i32 main( i32 argc, const c8 *argv[] ) $log( $fatal, {"Target file must be a text KV file"} ); _fatal_exit(); } +#endif struct stream open_test; if( stream_open_file( &open_test, _option_file_path, k_stream_read ) ) @@ -763,6 +986,9 @@ i32 main( i32 argc, const c8 *argv[] ) stack_init( &stack, NULL, BYTES_MB(16), "Assembly KV" ); keyvalues_init( &_assembly, &stack ); + stack_init( &_source_text, NULL, BYTES_MB(32), "Source text" ); + stretchy_init( &_files, sizeof(struct file_context) ); + u32 info_block = keyvalues_append_frame( &_assembly, 0, "vgc" ); keyvalues_append_string( &_assembly, info_block, "version", "0.2" ); keyvalues_append_string( &_assembly, info_block, "name", string_get( &_metacompiler.name ) ); @@ -775,7 +1001,7 @@ i32 main( i32 argc, const c8 *argv[] ) if( _option_platform == k_platform_windows ) _metacompiler.enabled_features[start_features ++] = "windows"; if( _option_platform == k_platform_web ) _metacompiler.enabled_features[start_features ++] = "web"; - _assemble_kv_file( _option_file_path, 0, start_features ); + _assemble_unit_file( _option_file_path, 0, start_features ); if( _option_assemble_only ) { @@ -796,11 +1022,11 @@ i32 main( i32 argc, const c8 *argv[] ) _shell_run( cmd ); } - _codegen_hooks(); - _codegen_shaders(); - _codegen_inputs(); - _codegen_console(); - _codegen_threads(); + if( _option_codegen_shaders ) _codegen_shaders(); + if( _option_codegen_inputs ) _codegen_inputs(); + if( _option_codegen_console ) _codegen_console(); + if( _option_codegen_threads ) _codegen_threads(); + if( _option_codegen_entities )_codegen_entities(); { struct shell_command *cmd = _shell_command(); @@ -888,12 +1114,11 @@ i32 main( i32 argc, const c8 *argv[] ) if( include || link_path || add ) { - u32 context_block = _assembly_kv_file_context( compile_it ); if( include ) $v_string( &cmd->line, {" -I"} ); if( link_path ) $v_string( &cmd->line, {" -L"} ); if( add ) $v_string( &cmd->line, {" "} ); - $v_string( &cmd->line, {keyvalues_read_string( &_assembly, context_block, "folder", NULL )}, - {"/"}, {keyvalues_value( &_assembly, compile_it, NULL )}, {" \\\n"} ); + _cannonicalize( _assembly_kv_file_context( compile_it ), keyvalues_value( &_assembly, compile_it, NULL ), &cmd->line ); + string_append( &cmd->line, " \\\n", 0 ); } else if( define || link ) { -- 2.25.1