diff --git a/ClassicalSharp/Entities/Player.cs b/ClassicalSharp/Entities/Player.cs index b3a96061c..670d389bd 100644 --- a/ClassicalSharp/Entities/Player.cs +++ b/ClassicalSharp/Entities/Player.cs @@ -130,8 +130,8 @@ namespace ClassicalSharp.Entities { Bitmap bmp = (Bitmap)item.Data; game.Graphics.DeleteTexture(ref TextureId); - SetSkinAll(true); - EnsurePow2(ref bmp); + EnsurePow2(ref bmp); + SetSkinAll(true); SkinType = Utils.GetSkinType(bmp); if (bmp.Width > game.Graphics.MaxTexWidth || bmp.Height > game.Graphics.MaxTexHeight) { diff --git a/src/Animations.c b/src/Animations.c index 0ae87fea3..5ce067a5e 100644 --- a/src/Animations.c +++ b/src/Animations.c @@ -354,6 +354,10 @@ static void Animations_FileChanged(void* obj, struct Stream* stream, const Strin } } + +/*########################################################################################################################* +*--------------------------------------------------Animations component---------------------------------------------------* +*#########################################################################################################################*/ static void Animations_Init(void) { Event_RegisterVoid(&TextureEvents_PackChanged, NULL, Animations_PackChanged); Event_RegisterEntry(&TextureEvents_FileChanged, NULL, Animations_FileChanged); @@ -365,7 +369,7 @@ static void Animations_Free(void) { Event_UnregisterEntry(&TextureEvents_FileChanged, NULL, Animations_FileChanged); } -void Animations_MakeComponent(struct IGameComponent* comp) { - comp->Init = Animations_Init; - comp->Free = Animations_Free; -} +struct IGameComponent Animations_Component = { + Animations_Init, /* Init */ + Animations_Free /* Free */ +}; diff --git a/src/Animations.h b/src/Animations.h index bef13db5d..ffd0f4ba2 100644 --- a/src/Animations.h +++ b/src/Animations.h @@ -10,6 +10,7 @@ struct IGameComponent; struct ScheduledTask; -void Animations_MakeComponent(struct IGameComponent* comp); +extern struct IGameComponent Animations_Component; + void Animations_Tick(struct ScheduledTask* task); #endif diff --git a/src/AsyncDownloader.c b/src/AsyncDownloader.c index 4707e03fe..4fc9e1073 100644 --- a/src/AsyncDownloader.c +++ b/src/AsyncDownloader.c @@ -291,6 +291,9 @@ static void AsyncDownloader_WorkerFunc(void) { } +/*########################################################################################################################* +*------------------------------------------------AsyncDownloader component------------------------------------------------* +*#########################################################################################################################*/ static void AsyncDownloader_Init(void) { AsyncRequestList_Init(&async_pending); AsyncRequestList_Init(&async_processed); @@ -327,8 +330,8 @@ static void AsyncDownloader_Free(void) { Mutex_Free(async_curRequestMutex); } -void AsyncDownloader_MakeComponent(struct IGameComponent* comp) { - comp->Init = AsyncDownloader_Init; - comp->Reset = AsyncDownloader_Reset; - comp->Free = AsyncDownloader_Free; -} +struct IGameComponent AsyncDownloader_Component = { + AsyncDownloader_Init, /* Init */ + AsyncDownloader_Free, /* Free */ + AsyncDownloader_Reset /* Reset */ +}; diff --git a/src/AsyncDownloader.h b/src/AsyncDownloader.h index 5217cdfc6..7453f042c 100644 --- a/src/AsyncDownloader.h +++ b/src/AsyncDownloader.h @@ -7,6 +7,7 @@ */ struct IGameComponent; struct ScheduledTask; +extern struct IGameComponent AsyncDownloader_Component; enum REQUEST_TYPE { REQUEST_TYPE_DATA, REQUEST_TYPE_CONTENT_LENGTH }; enum AsyncProgress { diff --git a/src/Audio.c b/src/Audio.c index 4fe40cea9..c1e7282ad 100644 --- a/src/Audio.c +++ b/src/Audio.c @@ -551,7 +551,7 @@ static void AudioManager_Free(void) { Event_UnregisterBlock(&UserEvents_BlockChanged, NULL, Audio_PlayBlockSound); } -void Audio_MakeComponent(struct IGameComponent* comp) { - comp->Init = AudioManager_Init; - comp->Free = AudioManager_Free; -} +struct IGameComponent Audio_Component = { + AudioManager_Init, /* Init */ + AudioManager_Free /* Free */ +}; diff --git a/src/Audio.h b/src/Audio.h index f437c128b..fa74de73e 100644 --- a/src/Audio.h +++ b/src/Audio.h @@ -5,8 +5,8 @@ Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 */ struct IGameComponent; +extern struct IGameComponent Audio_Component; -void Audio_MakeComponent(struct IGameComponent* comp); void Audio_SetMusic(int volume); void Audio_SetSounds(int volume); void Audio_PlayDigSound(uint8_t type); diff --git a/src/AxisLinesRenderer.c b/src/AxisLinesRenderer.c index 613f797fc..97c7dd136 100644 --- a/src/AxisLinesRenderer.c +++ b/src/AxisLinesRenderer.c @@ -12,24 +12,6 @@ static GfxResourceID axisLines_vb; #define AXISLINES_THICKNESS (1.0f / 32.0f) #define AXISLINES_LENGTH 3.0f -static void AxisLinesRenderer_ContextLost(void* obj) { - Gfx_DeleteVb(&axisLines_vb); -} - -static void AxisLinesRenderer_Init(void) { - Event_RegisterVoid(&GfxEvents_ContextLost, NULL, AxisLinesRenderer_ContextLost); -} - -static void AxisLinesRenderer_Free(void) { - AxisLinesRenderer_ContextLost(NULL); - Event_UnregisterVoid(&GfxEvents_ContextLost, NULL, AxisLinesRenderer_ContextLost); -} - -void AxisLinesRenderer_MakeComponent(struct IGameComponent* comp) { - comp->Init = AxisLinesRenderer_Init; - comp->Free = AxisLinesRenderer_Free; -} - void AxisLinesRenderer_Render(double delta) { static uint8_t indices[36] = { 2,2,1, 2,2,3, 4,2,3, 4,2,1, /* X arrow */ @@ -73,3 +55,25 @@ void AxisLinesRenderer_Render(double delta) { Gfx_SetVertexFormat(VERTEX_FORMAT_P3FC4B); Gfx_UpdateDynamicVb_IndexedTris(axisLines_vb, vertices, count); } + + +/*########################################################################################################################* +*-----------------------------------------------AxisLinesRenderer component-----------------------------------------------* +*#########################################################################################################################*/ +static void AxisLinesRenderer_ContextLost(void* obj) { + Gfx_DeleteVb(&axisLines_vb); +} + +static void AxisLinesRenderer_Init(void) { + Event_RegisterVoid(&GfxEvents_ContextLost, NULL, AxisLinesRenderer_ContextLost); +} + +static void AxisLinesRenderer_Free(void) { + AxisLinesRenderer_ContextLost(NULL); + Event_UnregisterVoid(&GfxEvents_ContextLost, NULL, AxisLinesRenderer_ContextLost); +} + +struct IGameComponent AxisLinesRenderer_Component = { + AxisLinesRenderer_Init, /* Init */ + AxisLinesRenderer_Free, /* Free */ +}; \ No newline at end of file diff --git a/src/AxisLinesRenderer.h b/src/AxisLinesRenderer.h index 6f4ad3b73..818a29d91 100644 --- a/src/AxisLinesRenderer.h +++ b/src/AxisLinesRenderer.h @@ -6,6 +6,7 @@ */ struct IGameComponent; -void AxisLinesRenderer_MakeComponent(struct IGameComponent* comp); +extern struct IGameComponent AxisLinesRenderer_Component; + void AxisLinesRenderer_Render(double delta); #endif diff --git a/src/Chat.c b/src/Chat.c index ddeaf0b8a..85964cf5c 100644 --- a/src/Chat.c +++ b/src/Chat.c @@ -603,8 +603,8 @@ static void Chat_Free(void) { StringsBuffer_Clear(&Chat_InputLog); } -void Chat_MakeComponent(struct IGameComponent* comp) { - comp->Init = Chat_Init; - comp->Reset = Chat_Reset; - comp->Free = Chat_Free; -} +struct IGameComponent Chat_Component = { + Chat_Init, /* Init */ + Chat_Free, /* Free */ + Chat_Reset /* Reset */ +}; diff --git a/src/Chat.h b/src/Chat.h index ec079589c..ce5cf7400 100644 --- a/src/Chat.h +++ b/src/Chat.h @@ -7,6 +7,7 @@ Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 */ struct IGameComponent; +extern struct IGameComponent Chat_Component; typedef enum MsgType_ { MSG_TYPE_NORMAL = 0, @@ -41,7 +42,6 @@ struct ChatCommand { /* Registers a client-side command, allowing it to be used with /client [cmd name] */ CC_EXPORT void Commands_Register(struct ChatCommand* cmd); -void Chat_MakeComponent(struct IGameComponent* comp); void Chat_SetLogName(const String* name); /* Sends a chat message, raising ChatEvents_ChatSending event. */ /* NOTE: /client is always interpreted as client-side commands. */ diff --git a/src/Entity.c b/src/Entity.c index 615d20939..0e05c3c88 100644 --- a/src/Entity.c +++ b/src/Entity.c @@ -460,10 +460,11 @@ static void TabList_Reset(void) { StringsBuffer_Clear(&TabList_Buffer); } -void TabList_MakeComponent(struct IGameComponent* comp) { - comp->Free = TabList_Free; - comp->Reset = TabList_Reset; -} +struct IGameComponent TabList_Component = { + NULL, /* Init */ + TabList_Free, /* Free */ + TabList_Reset /* Reset */ +}; /*########################################################################################################################* @@ -727,8 +728,8 @@ static void Player_CheckSkin(struct Player* p) { } Gfx_DeleteTexture(&e->TextureId); - Player_SetSkinAll(p, true); Player_EnsurePow2(p, &bmp); + Player_SetSkinAll(p, true); e->SkinType = Utils_GetSkinType(&bmp); if (bmp.Width > Gfx_MaxTexWidth || bmp.Height > Gfx_MaxTexHeight) { @@ -923,11 +924,12 @@ static void LocalPlayer_OnNewMap(void) { p->_WarnedNoclip = false; } -void LocalPlayer_MakeComponent(struct IGameComponent* comp) { - comp->Init = LocalPlayer_Init_; - comp->Ready = LocalPlayer_Reset; - comp->OnNewMap = LocalPlayer_OnNewMap; -} +struct IGameComponent LocalPlayer_Component = { + LocalPlayer_Init_, /* Init */ + NULL, /* Free */ + LocalPlayer_Reset, /* Reset */ + LocalPlayer_OnNewMap, /* OnNewMap */ +}; struct EntityVTABLE localPlayer_VTABLE = { LocalPlayer_Tick, Player_Despawn, LocalPlayer_SetLocation, Entity_GetCol, diff --git a/src/Entity.h b/src/Entity.h index 7c6db3da0..949eb796e 100644 --- a/src/Entity.h +++ b/src/Entity.h @@ -11,6 +11,8 @@ struct Model; struct IGameComponent; struct ScheduledTask; +extern struct IGameComponent TabList_Component; +extern struct IGameComponent LocalPlayer_Component; /* Offset used to avoid floating point roundoff errors. */ #define ENTITY_ADJUSTMENT 0.001f @@ -123,7 +125,6 @@ uint8_t TabList_GroupRanks[TABLIST_MAX_NAMES]; bool TabList_Valid(EntityID id); bool TabList_Remove(EntityID id); void TabList_Set(EntityID id, const String* player, const String* list, const String* group, uint8_t rank); -void TabList_MakeComponent(struct IGameComponent* comp); #define TabList_UNSAFE_GetPlayer(id) StringsBuffer_UNSAFE_Get(&TabList_Buffer, TabList_PlayerNames[id]); #define TabList_UNSAFE_GetList(id) StringsBuffer_UNSAFE_Get(&TabList_Buffer, TabList_ListNames[id]); @@ -159,7 +160,6 @@ struct LocalPlayer { }; struct LocalPlayer LocalPlayer_Instance; -void LocalPlayer_MakeComponent(struct IGameComponent* comp); void LocalPlayer_Init(void); float LocalPlayer_JumpHeight(void); void LocalPlayer_CheckHacksConsistency(void); diff --git a/src/EnvRenderer.c b/src/EnvRenderer.c index ca0e2d76d..8713ae4ba 100644 --- a/src/EnvRenderer.c +++ b/src/EnvRenderer.c @@ -824,18 +824,6 @@ static void EnvRenderer_ContextRecreated(void* obj) { EnvRenderer_UpdateAll(); } -static void EnvRenderer_Reset(void) { - Gfx_SetFog(false); - EnvRenderer_DeleteVbs(); - Mem_Free(Weather_Heightmap); - Weather_Heightmap = NULL; - weather_lastPos = Vector3I_MaxValue(); -} - -static void EnvRenderer_OnNewMapLoaded(void) { - EnvRenderer_ContextRecreated(NULL); -} - void EnvRenderer_UseLegacyMode(bool legacy) { EnvRenderer_Legacy = legacy; EnvRenderer_ContextRecreated(NULL); @@ -898,6 +886,10 @@ static void EnvRenderer_EnvVariableChanged(void* obj, int envVar) { } } + +/*########################################################################################################################* +*--------------------------------------------------EnvRenderer component--------------------------------------------------* +*#########################################################################################################################*/ static void EnvRenderer_Init(void) { Event_RegisterEntry(&TextureEvents_FileChanged, NULL, EnvRenderer_FileChanged); Event_RegisterVoid(&TextureEvents_PackChanged, NULL, EnvRenderer_TexturePackChanged); @@ -931,6 +923,18 @@ static void EnvRenderer_Free(void) { Gfx_DeleteTexture(&snow_tex); } +static void EnvRenderer_Reset(void) { + Gfx_SetFog(false); + EnvRenderer_DeleteVbs(); + Mem_Free(Weather_Heightmap); + Weather_Heightmap = NULL; + weather_lastPos = Vector3I_MaxValue(); +} + +static void EnvRenderer_OnNewMapLoaded(void) { + EnvRenderer_ContextRecreated(NULL); +} + void EnvRenderer_MakeComponent(struct IGameComponent* comp) { comp->Init = EnvRenderer_Init; comp->Reset = EnvRenderer_Reset; @@ -938,3 +942,11 @@ void EnvRenderer_MakeComponent(struct IGameComponent* comp) { comp->OnNewMapLoaded = EnvRenderer_OnNewMapLoaded; comp->Free = EnvRenderer_Free; } + +struct IGameComponent EnvRenderer_Component = { + EnvRenderer_Init, /* Init */ + EnvRenderer_Free, /* Free */ + EnvRenderer_Reset, /* Reset */ + EnvRenderer_Reset, /* OnNewMap */ + EnvRenderer_OnNewMapLoaded /* OnNewMapLoaded */ +}; diff --git a/src/EnvRenderer.h b/src/EnvRenderer.h index 04c3ec98e..e2aa2903d 100644 --- a/src/EnvRenderer.h +++ b/src/EnvRenderer.h @@ -5,6 +5,7 @@ Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 */ struct IGameComponent; +extern struct IGameComponent EnvRenderer_Component; void EnvRenderer_RenderSky(double deltaTime); void EnvRenderer_RenderClouds(double deltaTime); @@ -22,5 +23,4 @@ void EnvRenderer_RenderWeather(double deltaTime); bool EnvRenderer_Legacy, EnvRenderer_Minimal; void EnvRenderer_UseMinimalMode(bool minimal); void EnvRenderer_UseLegacyMode(bool legacy); -void EnvRenderer_MakeComponent(struct IGameComponent* comp); #endif diff --git a/src/Game.c b/src/Game.c index f9f8a8213..4d29a6ede 100644 --- a/src/Game.c +++ b/src/Game.c @@ -35,8 +35,6 @@ #include "Stream.h" #include "TerrainAtlas.h" -static struct IGameComponent Game_Components[26]; -static int Game_ComponentsCount; static struct ScheduledTask Game_Tasks[6]; static int Game_TasksCount, entTaskI; @@ -50,22 +48,16 @@ String Game_Mppass = String_FromArray(Game_MppassBuffer); String Game_IPAddress = String_FromArray(Game_IPAddressBuffer); String Game_FontName = String_FromArray(Game_FontNameBuffer); +static struct IGameComponent* comps_head; +static struct IGameComponent* comps_tail; void Game_AddComponent(struct IGameComponent* comp) { - if (Game_ComponentsCount == Array_Elems(Game_Components)) { - ErrorHandler_Fail("Game_AddComponent - hit max count"); + if (!comps_head) { + comps_head = comp; + } else { + comps_tail->Next = comp; } - Game_Components[Game_ComponentsCount++] = *comp; - IGameComponent_MakeEmpty(comp); -} - -void IGameComponent_NullFunc(void) { } -void IGameComponent_MakeEmpty(struct IGameComponent* comp) { - comp->Init = IGameComponent_NullFunc; - comp->Free = IGameComponent_NullFunc; - comp->Ready = IGameComponent_NullFunc; - comp->Reset = IGameComponent_NullFunc; - comp->OnNewMap = IGameComponent_NullFunc; - comp->OnNewMapLoaded = IGameComponent_NullFunc; + comps_tail = comp; + comp->Next = NULL; } int ScheduledTask_Add(double interval, ScheduledTaskCallback callback) { @@ -168,7 +160,7 @@ void Game_UpdateProjection(void) { } void Game_Disconnect(const String* title, const String* reason) { - int i; + struct IGameComponent* comp; World_Reset(); Event_RaiseVoid(&WorldEvents_NewMap); Gui_FreeActive(); @@ -178,8 +170,8 @@ void Game_Disconnect(const String* title, const String* reason) { Block_Reset(); TexturePack_ExtractDefault(); - for (i = 0; i < Game_ComponentsCount; i++) { - Game_Components[i].Reset(); + for (comp = comps_head; comp; comp = comp->Next) { + if (comp->Reset) comp->Reset(); } } @@ -275,16 +267,16 @@ static void Game_OnResize(void* obj) { } static void Game_OnNewMapCore(void* obj) { - int i; - for (i = 0; i < Game_ComponentsCount; i++) { - Game_Components[i].OnNewMap(); + struct IGameComponent* comp; + for (comp = comps_head; comp; comp = comp->Next) { + if (comp->OnNewMap) comp->OnNewMap(); } } static void Game_OnNewMapLoadedCore(void* obj) { - int i; - for (i = 0; i < Game_ComponentsCount; i++) { - Game_Components[i].OnNewMapLoaded(); + struct IGameComponent* comp; + for (comp = comps_head; comp; comp = comp->Next) { + if (comp->OnNewMapLoaded) comp->OnNewMapLoaded(); } } @@ -417,8 +409,8 @@ void Game_Free(void* obj); void Game_Load(void) { String renderType; char renderTypeBuffer[STRING_SIZE]; String title; char titleBuffer[STRING_SIZE]; - struct IGameComponent comp; - int i, flags; + struct IGameComponent* comp; + int flags; Game_ViewDistance = 512; Game_MaxViewDistance = 32768; @@ -426,7 +418,6 @@ void Game_Load(void) { Game_Fov = 70; Game_AutoRotate = true; - IGameComponent_MakeEmpty(&comp); Gfx_Init(); Gfx_SetVSync(true); Gfx_MakeApiInfo(); @@ -439,12 +430,12 @@ void Game_Load(void) { /* TODO: Survival vs Creative game mode */ InputHandler_Init(); - Particles_MakeComponent(&comp); Game_AddComponent(&comp); - TabList_MakeComponent(&comp); Game_AddComponent(&comp); + Game_AddComponent(&Particles_Component); + Game_AddComponent(&TabList_Component); Game_LoadOptions(); Game_LoadGuiOptions(); - Chat_MakeComponent(&comp); Game_AddComponent(&comp); + Game_AddComponent(&Chat_Component); Event_RegisterVoid(&WorldEvents_NewMap, NULL, Game_OnNewMapCore); Event_RegisterVoid(&WorldEvents_MapLoaded, NULL, Game_OnNewMapLoadedCore); @@ -460,24 +451,24 @@ void Game_Load(void) { Block_Init(); ModelCache_Init(); - AsyncDownloader_MakeComponent(&comp); Game_AddComponent(&comp); - Lighting_MakeComponent(&comp); Game_AddComponent(&comp); + Game_AddComponent(&AsyncDownloader_Component); + Game_AddComponent(&Lighting_Component); Drawer2D_BitmappedText = Game_ClassicMode || !Options_GetBool(OPT_USE_CHAT_FONT, false); Drawer2D_BlackTextShadows = Options_GetBool(OPT_BLACK_TEXT, false); Gfx_Mipmaps = Options_GetBool(OPT_MIPMAPS, false); - Animations_MakeComponent(&comp); Game_AddComponent(&comp); - Inventory_MakeComponent(&comp); Game_AddComponent(&comp); + Game_AddComponent(&Animations_Component); + Game_AddComponent(&Inventory_Component); Block_SetDefaultPerms(); Env_Reset(); LocalPlayer_Init(); - LocalPlayer_MakeComponent(&comp); Game_AddComponent(&comp); + Game_AddComponent(&LocalPlayer_Component); Entities_List[ENTITIES_SELF_ID] = &LocalPlayer_Instance.Base; MapRenderer_Init(); - EnvRenderer_MakeComponent(&comp); Game_AddComponent(&comp); + Game_AddComponent(&EnvRenderer_Component); String_InitArray(renderType, renderTypeBuffer); Options_Get(OPT_RENDER_TYPE, &renderType, "normal"); @@ -491,16 +482,16 @@ void Game_Load(void) { } else { ServerConnection_InitMultiplayer(); } - ServerConnection_MakeComponent(&comp); Game_AddComponent(&comp); + Game_AddComponent(&ServerConnection_Component); String_AppendConst(&ServerConnection_AppName, PROGRAM_APP_NAME); Gfx_LostContextFunction = ServerConnection_Tick; Camera_Init(); Game_UpdateProjection(); - Gui_MakeComponent(&comp); Game_AddComponent(&comp); - Selections_MakeComponent(&comp); Game_AddComponent(&comp); - HeldBlockRenderer_MakeComponent(&comp); Game_AddComponent(&comp); + Game_AddComponent(&Gui_Component); + Game_AddComponent(&Selections_Component); + Game_AddComponent(&HeldBlockRenderer_Component); Gfx_SetDepthTest(true); Gfx_SetDepthTestFunc(COMPARE_FUNC_LESSEQUAL); @@ -508,20 +499,20 @@ void Game_Load(void) { Gfx_SetAlphaBlendFunc(BLEND_FUNC_SRC_ALPHA, BLEND_FUNC_INV_SRC_ALPHA); Gfx_SetAlphaTestFunc(COMPARE_FUNC_GREATER, 0.5f); - PickedPosRenderer_MakeComponent(&comp); Game_AddComponent(&comp); - Audio_MakeComponent(&comp); Game_AddComponent(&comp); - AxisLinesRenderer_MakeComponent(&comp); Game_AddComponent(&comp); + Game_AddComponent(&PickedPosRenderer_Component); + Game_AddComponent(&Audio_Component); + Game_AddComponent(&AxisLinesRenderer_Component); /* TODO: plugin dll support */ /* List nonLoaded = PluginLoader.LoadAll(); */ - for (i = 0; i < Game_ComponentsCount; i++) { - Game_Components[i].Init(); + for (comp = comps_head; comp; comp = comp->Next) { + if (comp->Init) comp->Init(); } Game_ExtractInitialTexturePack(); - for (i = 0; i < Game_ComponentsCount; i++) { - Game_Components[i].Ready(); + for (comp = comps_head; comp; comp = comp->Next) { + if (comp->Ready) comp->Ready(); } Game_InitScheduledTasks(); @@ -719,7 +710,7 @@ static void Game_RenderFrame(double delta) { } void Game_Free(void* obj) { - int i; + struct IGameComponent* comp; MapRenderer_Free(); Atlas2D_Free(); @@ -735,8 +726,8 @@ void Game_Free(void* obj) { Event_UnregisterVoid(&WindowEvents_Resized, NULL, Game_OnResize); Event_UnregisterVoid(&WindowEvents_Closed, NULL, Game_Free); - for (i = 0; i < Game_ComponentsCount; i++) { - Game_Components[i].Free(); + for (comp = comps_head; comp; comp = comp->Next) { + if (comp->Free) comp->Free(); } Drawer2D_Free(); diff --git a/src/GameStructs.h b/src/GameStructs.h index a25d10d2c..28609a384 100644 --- a/src/GameStructs.h +++ b/src/GameStructs.h @@ -4,6 +4,7 @@ /* Represents Game related structures. Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 */ +struct IGameComponent; /* Represents a game component. */ struct IGameComponent { @@ -13,15 +14,15 @@ struct IGameComponent { void (*Free)(void); /* Called to reset the component's state. (e.g. reconnecting to server) */ void (*Reset)(void); - /* Called when the texture pack has been loaded and all components have been initialised. */ - void (*Ready)(void); /* Called to update the component's state when the user begins loading a new map. */ void (*OnNewMap)(void); /* Called to update the component's state when the user has finished loading a new map. */ void (*OnNewMapLoaded)(void); + /* Called when the texture pack has been loaded and all components have been initialised. */ + void (*Ready)(void); + /* Next component in linked list of components. */ + struct IGameComponent* Next; }; - -void IGameComponent_MakeEmpty(struct IGameComponent* comp); CC_NOINLINE void Game_AddComponent(struct IGameComponent* comp); /* Represents a task that periodically runs on the main thread every specified interval. */ diff --git a/src/Gui.c b/src/Gui.c index a53f8a673..b0520cdbf 100644 --- a/src/Gui.c +++ b/src/Gui.c @@ -88,15 +88,16 @@ static void Gui_FileChanged(void* obj, struct Stream* stream, const String* name } static void Gui_Init(void) { - struct IGameComponent comp; IGameComponent_MakeEmpty(&comp); - StatusScreen_MakeComponent(&comp); Game_AddComponent(&comp); - HUDScreen_MakeComponent(&comp); Game_AddComponent(&comp); - Event_RegisterEntry(&TextureEvents_FileChanged, NULL, Gui_FileChanged); Gui_Status = StatusScreen_MakeInstance(); Gui_HUD = HUDScreen_MakeInstance(); } +static void Gui_Ready(void) { + Elem_Init(Gui_Status); + Elem_Init(Gui_HUD); +} + static void Gui_Reset(void) { int i; for (i = 0; i < Gui_OverlaysCount; i++) { @@ -117,11 +118,14 @@ static void Gui_Free(void) { Gui_Reset(); } -void Gui_MakeComponent(struct IGameComponent* comp) { - comp->Init = Gui_Init; - comp->Reset = Gui_Reset; - comp->Free = Gui_Free; -} +struct IGameComponent Gui_Component = { + Gui_Init, /* Init */ + Gui_Free, /* Free */ + Gui_Reset, /* Reset */ + NULL, /* OnNewMap */ + NULL, /* OnNewMapLoaded */ + Gui_Ready +}; struct Screen* Gui_GetActiveScreen(void) { return Gui_OverlaysCount ? Gui_Overlays[0] : Gui_GetUnderlyingScreen(); diff --git a/src/Gui.h b/src/Gui.h index 3d8bd4bda..aff584240 100644 --- a/src/Gui.h +++ b/src/Gui.h @@ -15,6 +15,7 @@ enum GuiAnchor { struct IGameComponent; struct GuiElem; +extern struct IGameComponent Gui_Component; #define GuiElemVTABLE_Layout() \ void (*Init)(void* elem); \ diff --git a/src/HeldBlockRenderer.c b/src/HeldBlockRenderer.c index 34039b19e..3a93e5711 100644 --- a/src/HeldBlockRenderer.c +++ b/src/HeldBlockRenderer.c @@ -249,7 +249,7 @@ static void HeldBlockRenderer_Free(void) { Event_UnregisterBlock(&UserEvents_BlockChanged, NULL, HeldBlockRenderer_BlockChanged); } -void HeldBlockRenderer_MakeComponent(struct IGameComponent* comp) { - comp->Init = HeldBlockRenderer_Init; - comp->Free = HeldBlockRenderer_Free; -} +struct IGameComponent HeldBlockRenderer_Component = { + HeldBlockRenderer_Init, /* Init */ + HeldBlockRenderer_Free /* Free */ +}; diff --git a/src/HeldBlockRenderer.h b/src/HeldBlockRenderer.h index a054ffe38..9c36367fa 100644 --- a/src/HeldBlockRenderer.h +++ b/src/HeldBlockRenderer.h @@ -5,8 +5,8 @@ Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 */ struct IGameComponent; +extern struct IGameComponent HeldBlockRenderer_Component; void HeldBlockRenderer_ClickAnim(bool digging); void HeldBlockRenderer_Render(double delta); -void HeldBlockRenderer_MakeComponent(struct IGameComponent* comp); #endif diff --git a/src/Inventory.c b/src/Inventory.c index b0929bc5c..27c7f9864 100644 --- a/src/Inventory.c +++ b/src/Inventory.c @@ -94,6 +94,10 @@ void Inventory_Remove(BlockID block) { } } + +/*########################################################################################################################* +*--------------------------------------------------Inventory component----------------------------------------------------* +*#########################################################################################################################*/ static void Inventory_Reset(void) { Inventory_SetDefaultMapping(); Inventory_CanChangeHeldBlock = true; @@ -109,7 +113,8 @@ static void Inventory_Init(void) { inv[6] = BLOCK_LEAVES; inv[7] = BLOCK_GRASS; inv[8] = BLOCK_SLAB; } -void Inventory_MakeComponent(struct IGameComponent* comp) { - comp->Init = Inventory_Init; - comp->Reset = Inventory_Reset; -} +struct IGameComponent Inventory_Component = { + Inventory_Init, /* Init */ + NULL, /* Free */ + Inventory_Reset, /* Reset */ +}; diff --git a/src/Inventory.h b/src/Inventory.h index 4f7b922f9..508768cac 100644 --- a/src/Inventory.h +++ b/src/Inventory.h @@ -7,6 +7,7 @@ Copyright 2017 ClassicalSharp | Licensed under BSD-3 */ struct IGameComponent; +extern struct IGameComponent Inventory_Component; #define INVENTORY_BLOCKS_PER_HOTBAR 9 #define INVENTORY_HOTBARS 9 @@ -14,7 +15,6 @@ struct IGameComponent; BlockID Inventory_Table[INVENTORY_HOTBARS * INVENTORY_BLOCKS_PER_HOTBAR]; /* Mapping of indices in inventory menu to block IDs. */ BlockID Inventory_Map[BLOCK_COUNT]; -void Inventory_MakeComponent(struct IGameComponent* comp); int Inventory_SelectedIndex; int Inventory_Offset; diff --git a/src/Lighting.c b/src/Lighting.c index 104a47e34..5ab003091 100644 --- a/src/Lighting.c +++ b/src/Lighting.c @@ -361,9 +361,10 @@ static void Lighting_OnNewMapLoaded(void) { Lighting_Refresh(); } -void Lighting_MakeComponent(struct IGameComponent* comp) { - comp->Free = Lighting_Reset; - comp->OnNewMap = Lighting_Reset; - comp->OnNewMapLoaded = Lighting_OnNewMapLoaded; - comp->Reset = Lighting_Reset; -} +struct IGameComponent Lighting_Component = { + NULL, /* Init */ + Lighting_Reset, /* Free */ + Lighting_Reset, /* Reset */ + Lighting_Reset, /* OnNewMap */ + Lighting_OnNewMapLoaded /* OnNewMapLoaded */ +}; diff --git a/src/Lighting.h b/src/Lighting.h index a0b158a89..28a3ff694 100644 --- a/src/Lighting.h +++ b/src/Lighting.h @@ -6,11 +6,11 @@ BasicLighting: Uses a simple heightmap, where each block is either in sun or sha Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 */ struct IGameComponent; +extern struct IGameComponent Lighting_Component; #define Lighting_Pack(x, z) ((x) + World_Width * (z)) int16_t* Lighting_Heightmap; -void Lighting_MakeComponent(struct IGameComponent* comp); /* Equivalent to (but far more optimised form of) * for x = startX; x < startX + 18; x++ * for z = startZ; z < startZ + 18; z++ diff --git a/src/Particle.c b/src/Particle.c index 3ca7f8084..2bc96fcee 100644 --- a/src/Particle.c +++ b/src/Particle.c @@ -321,46 +321,6 @@ static void Particles_FileChanged(void* obj, struct Stream* stream, const String } } -static void Particles_ContextLost(void* obj) { - Gfx_DeleteVb(&Particles_VB); -} -static void Particles_ContextRecreated(void* obj) { - Particles_VB = Gfx_CreateDynamicVb(VERTEX_FORMAT_P3FT2FC4B, PARTICLES_MAX * 4); -} - -static void Particles_BreakBlockEffect_Handler(void* obj, Vector3I coords, BlockID old, BlockID now) { - Particles_BreakBlockEffect(coords, old, now); -} - -static void Particles_Init(void) { - Random_InitFromCurrentTime(&rnd); - Particles_ContextRecreated(NULL); - - Event_RegisterBlock(&UserEvents_BlockChanged, NULL, Particles_BreakBlockEffect_Handler); - Event_RegisterEntry(&TextureEvents_FileChanged, NULL, Particles_FileChanged); - Event_RegisterVoid(&GfxEvents_ContextLost, NULL, Particles_ContextLost); - Event_RegisterVoid(&GfxEvents_ContextRecreated, NULL, Particles_ContextRecreated); -} - -static void Particles_Reset(void) { rain_count = 0; terrain_count = 0; } - -static void Particles_Free(void) { - Gfx_DeleteTexture(&Particles_TexId); - Particles_ContextLost(NULL); - - Event_UnregisterBlock(&UserEvents_BlockChanged, NULL, Particles_BreakBlockEffect_Handler); - Event_UnregisterEntry(&TextureEvents_FileChanged, NULL, Particles_FileChanged); - Event_UnregisterVoid(&GfxEvents_ContextLost, NULL, Particles_ContextLost); - Event_UnregisterVoid(&GfxEvents_ContextRecreated, NULL, Particles_ContextRecreated); -} - -void Particles_MakeComponent(struct IGameComponent* comp) { - comp->Init = Particles_Init; - comp->Reset = Particles_Reset; - comp->OnNewMap = Particles_Reset; - comp->Free = Particles_Free; -} - void Particles_Render(double delta, float t) { if (!terrain_count && !rain_count) return; if (Gfx_LostContext) return; @@ -491,3 +451,46 @@ void Particles_RainSnowEffect(Vector3 pos) { p->Base.Size = (uint8_t)(type >= 28 ? 2 : (type >= 25 ? 4 : 3)); } } + + +/*########################################################################################################################* +*---------------------------------------------------Particles component---------------------------------------------------* +*#########################################################################################################################*/ +static void Particles_ContextLost(void* obj) { + Gfx_DeleteVb(&Particles_VB); +} +static void Particles_ContextRecreated(void* obj) { + Particles_VB = Gfx_CreateDynamicVb(VERTEX_FORMAT_P3FT2FC4B, PARTICLES_MAX * 4); +} +static void Particles_BreakBlockEffect_Handler(void* obj, Vector3I coords, BlockID old, BlockID now) { + Particles_BreakBlockEffect(coords, old, now); +} + +static void Particles_Init(void) { + Random_InitFromCurrentTime(&rnd); + Particles_ContextRecreated(NULL); + + Event_RegisterBlock(&UserEvents_BlockChanged, NULL, Particles_BreakBlockEffect_Handler); + Event_RegisterEntry(&TextureEvents_FileChanged, NULL, Particles_FileChanged); + Event_RegisterVoid(&GfxEvents_ContextLost, NULL, Particles_ContextLost); + Event_RegisterVoid(&GfxEvents_ContextRecreated, NULL, Particles_ContextRecreated); +} + +static void Particles_Free(void) { + Gfx_DeleteTexture(&Particles_TexId); + Particles_ContextLost(NULL); + + Event_UnregisterBlock(&UserEvents_BlockChanged, NULL, Particles_BreakBlockEffect_Handler); + Event_UnregisterEntry(&TextureEvents_FileChanged, NULL, Particles_FileChanged); + Event_UnregisterVoid(&GfxEvents_ContextLost, NULL, Particles_ContextLost); + Event_UnregisterVoid(&GfxEvents_ContextRecreated, NULL, Particles_ContextRecreated); +} + +static void Particles_Reset(void) { rain_count = 0; terrain_count = 0; } + +struct IGameComponent Particles_Component = { + Particles_Init, /* Init */ + Particles_Free, /* Free */ + Particles_Reset, /* Reset */ + Particles_Reset /* OnNewMap */ +}; diff --git a/src/Particle.h b/src/Particle.h index 57073f64e..cf058ea2e 100644 --- a/src/Particle.h +++ b/src/Particle.h @@ -8,6 +8,8 @@ struct IGameComponent; struct ScheduledTask; +extern struct IGameComponent Particles_Component; + struct Particle { Vector3 Velocity; float Lifetime; diff --git a/src/PickedPosRenderer.c b/src/PickedPosRenderer.c index 273120545..5bb3a6950 100644 --- a/src/PickedPosRenderer.c +++ b/src/PickedPosRenderer.c @@ -12,26 +12,6 @@ static GfxResourceID pickedPos_vb; #define PICKEDPOS_NUM_VERTICES (16 * 6) static VertexP3fC4b pickedPos_vertices[PICKEDPOS_NUM_VERTICES]; -static void PickedPosRenderer_ContextLost(void* obj) { - Gfx_DeleteVb(&pickedPos_vb); -} - -static void PickedPosRenderer_ContextRecreated(void* obj) { - pickedPos_vb = Gfx_CreateDynamicVb(VERTEX_FORMAT_P3FC4B, PICKEDPOS_NUM_VERTICES); -} - -static void PickedPosRenderer_Init(void) { - PickedPosRenderer_ContextRecreated(NULL); - Event_RegisterVoid(&GfxEvents_ContextLost, NULL, PickedPosRenderer_ContextLost); - Event_RegisterVoid(&GfxEvents_ContextRecreated, NULL, PickedPosRenderer_ContextRecreated); -} - -static void PickedPosRenderer_Free(void) { - PickedPosRenderer_ContextLost(NULL); - Event_UnregisterVoid(&GfxEvents_ContextLost, NULL, PickedPosRenderer_ContextLost); - Event_UnregisterVoid(&GfxEvents_ContextRecreated, NULL, PickedPosRenderer_ContextRecreated); -} - void PickedPosRenderer_Render(double delta) { if (Gfx_LostContext) return; @@ -117,7 +97,31 @@ void PickedPosRenderer_Update(struct PickedPos* selected) { } } -void PickedPosRenderer_MakeComponent(struct IGameComponent* comp) { - comp->Init = PickedPosRenderer_Init; - comp->Free = PickedPosRenderer_Free; + +/*########################################################################################################################* +*-----------------------------------------------PickedPosRenderer component-----------------------------------------------* +*#########################################################################################################################*/ +static void PickedPosRenderer_ContextLost(void* obj) { + Gfx_DeleteVb(&pickedPos_vb); } + +static void PickedPosRenderer_ContextRecreated(void* obj) { + pickedPos_vb = Gfx_CreateDynamicVb(VERTEX_FORMAT_P3FC4B, PICKEDPOS_NUM_VERTICES); +} + +static void PickedPosRenderer_Init(void) { + PickedPosRenderer_ContextRecreated(NULL); + Event_RegisterVoid(&GfxEvents_ContextLost, NULL, PickedPosRenderer_ContextLost); + Event_RegisterVoid(&GfxEvents_ContextRecreated, NULL, PickedPosRenderer_ContextRecreated); +} + +static void PickedPosRenderer_Free(void) { + PickedPosRenderer_ContextLost(NULL); + Event_UnregisterVoid(&GfxEvents_ContextLost, NULL, PickedPosRenderer_ContextLost); + Event_UnregisterVoid(&GfxEvents_ContextRecreated, NULL, PickedPosRenderer_ContextRecreated); +} + +struct IGameComponent PickedPosRenderer_Component = { + PickedPosRenderer_Init, /* Init */ + PickedPosRenderer_Free, /* Free */ +}; diff --git a/src/PickedPosRenderer.h b/src/PickedPosRenderer.h index c869c2112..f59d6db8b 100644 --- a/src/PickedPosRenderer.h +++ b/src/PickedPosRenderer.h @@ -6,8 +6,8 @@ */ struct PickedPos; struct IGameComponent; +extern struct IGameComponent PickedPosRenderer_Component; -void PickedPosRenderer_MakeComponent(struct IGameComponent* comp); void PickedPosRenderer_Render(double delta); void PickedPosRenderer_Update(struct PickedPos* selected); #endif diff --git a/src/Screens.c b/src/Screens.c index d28263971..54f1df7bb 100644 --- a/src/Screens.c +++ b/src/Screens.c @@ -449,11 +449,6 @@ struct Screen* StatusScreen_MakeInstance(void) { return (struct Screen*)s; } -static void StatusScreen_Ready(void) { Elem_Init(&StatusScreen_Instance); } -void StatusScreen_MakeComponent(struct IGameComponent* comp) { - comp->Ready = StatusScreen_Ready; -} - /*########################################################################################################################* *------------------------------------------------------LoadingScreen------------------------------------------------------* @@ -1388,11 +1383,6 @@ struct Screen* HUDScreen_MakeInstance(void) { return (struct Screen*)s; } -static void HUDScreen_Ready(void) { Elem_Init(&HUDScreen_Instance); } -void HUDScreen_MakeComponent(struct IGameComponent* comp) { - comp->Ready = HUDScreen_Ready; -} - void HUDScreen_OpenInput(struct Screen* hud, const String* text) { struct Screen* chat = ((struct HUDScreen*)hud)->Chat; ChatScreen_OpenInput((struct ChatScreen*)chat, text); diff --git a/src/Screens.h b/src/Screens.h index 7fb303890..a302a414e 100644 --- a/src/Screens.h +++ b/src/Screens.h @@ -6,15 +6,12 @@ */ struct Screen; struct Widget; -struct IGameComponent; struct Screen* InventoryScreen_MakeInstance(void); struct Screen* StatusScreen_MakeInstance(void); -void StatusScreen_MakeComponent(struct IGameComponent* comp); struct Screen* LoadingScreen_MakeInstance(const String* title, const String* message); struct Screen* GeneratingScreen_MakeInstance(void); struct Screen* HUDScreen_MakeInstance(void); -void HUDScreen_MakeComponent(struct IGameComponent* comp); struct Screen* DisconnectScreen_MakeInstance(const String* title, const String* message); /* Raw pointer to inventory screen. DO NOT USE THIS. Use InventoryScreen_MakeInstance() */ diff --git a/src/SelectionBox.c b/src/SelectionBox.c index d3d49c893..250bfeb35 100644 --- a/src/SelectionBox.c +++ b/src/SelectionBox.c @@ -203,6 +203,10 @@ void Selections_Render(double delta) { Gfx_SetAlphaBlending(false); } + +/*########################################################################################################################* +*--------------------------------------------------Selections component---------------------------------------------------* +*#########################################################################################################################*/ static void Selections_Init(void) { Event_RegisterVoid(&GfxEvents_ContextLost, NULL, Selections_ContextLost); Event_RegisterVoid(&GfxEvents_ContextRecreated, NULL, Selections_ContextRecreated); @@ -218,9 +222,9 @@ static void Selections_Free(void) { Event_UnregisterVoid(&GfxEvents_ContextRecreated, NULL, Selections_ContextRecreated); } -void Selections_MakeComponent(struct IGameComponent* comp) { - comp->Init = Selections_Init; - comp->Free = Selections_Free; - comp->Reset = Selections_Reset; - comp->OnNewMap = Selections_Reset; -} +struct IGameComponent Selections_Component = { + Selections_Init, /* Init */ + Selections_Free, /* Free */ + Selections_Reset, /* Reset */ + Selections_Reset /* OnNewMap */ +}; diff --git a/src/SelectionBox.h b/src/SelectionBox.h index d52a9b619..34475a57f 100644 --- a/src/SelectionBox.h +++ b/src/SelectionBox.h @@ -6,8 +6,8 @@ Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 */ struct IGameComponent; +extern struct IGameComponent Selections_Component; -void Selections_MakeComponent(struct IGameComponent* comp); void Selections_Render(double delta); void Selections_Add(uint8_t id, Vector3I p1, Vector3I p2, PackedCol col); void Selections_Remove(uint8_t id); diff --git a/src/ServerConnection.c b/src/ServerConnection.c index 0787f807e..b5c2ee35f 100644 --- a/src/ServerConnection.c +++ b/src/ServerConnection.c @@ -550,8 +550,9 @@ static void ServerConnection_Free(void) { } } -void ServerConnection_MakeComponent(struct IGameComponent* comp) { - comp->OnNewMap = MPConnection_OnNewMap; - comp->Reset = MPConnection_Reset; - comp->Free = ServerConnection_Free; -} +struct IGameComponent ServerConnection_Component = { + NULL, /* Init */ + ServerConnection_Free, /* Free */ + MPConnection_Reset, /* Reset */ + MPConnection_OnNewMap /* OnNewMap */ +}; diff --git a/src/ServerConnection.h b/src/ServerConnection.h index df09bbbc3..3b88448bf 100644 --- a/src/ServerConnection.h +++ b/src/ServerConnection.h @@ -40,6 +40,7 @@ struct PickedPos; struct Stream; struct IGameComponent; struct ScheduledTask; +extern struct IGameComponent ServerConnection_Component; int PingList_NextPingData(void); void PingList_Update(int data);