Implement event handlers

This commit is contained in:
UnknownShadow200 2017-05-11 13:27:20 +10:00
parent dde7c6258e
commit c452405510
8 changed files with 95 additions and 12 deletions

View File

@ -207,6 +207,7 @@
<ClCompile Include="D3D9Api.c" />
<ClCompile Include="DefaultSet.c" />
<ClCompile Include="Bitmap.c" />
<ClCompile Include="EventHandler.c" />
<ClCompile Include="ExtMath.c" />
<ClCompile Include="FastColour.c" />
<ClCompile Include="GraphicsCommon.c" />

View File

@ -73,6 +73,9 @@
<Filter Include="Header Files\Events">
<UniqueIdentifier>{a912f0d5-3ceb-4e7e-ba4e-28170c634047}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\Events">
<UniqueIdentifier>{03efa5de-9e49-4185-bb40-fa5e5a2846e6}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="NotchyGenerator.h">
@ -230,5 +233,8 @@
<ClCompile Include="D3D9Api.c">
<Filter>Source Files\Graphics</Filter>
</ClCompile>
<ClCompile Include="EventHandler.c">
<Filter>Source Files\Events</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -4,6 +4,25 @@
#include <d3d9caps.h>
#include <d3d9types.h>
/* Abstracts Direct3D9 3D graphics rendering API.
Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3
*/
/* Maximum number of matrices that go on a stack. */
#define MatrixStack_Capacity 24
typedef struct MatrixStack {
/* Raw array of matrices.*/
Matrix Stack[MatrixStack_Capacity];
/* Current active matrix. */
Int32 Index;
/* Type of transformation this stack is for. */
D3DTRANSFORMSTATETYPE TransformType;
} MatrixStack;
D3DPRIMITIVETYPE d3d9_modeMappings[2] = { D3DPT_TRIANGLELIST, D3DPT_LINELIST };
D3DFORMAT d3d9_depthFormats[6] = { D3DFMT_D32, D3DFMT_D24X8, D3DFMT_D24S8, D3DFMT_D24X4S4, D3DFMT_D16, D3DFMT_D15S1 };
D3DFORMAT d3d9_viewFormats[4] = { D3DFMT_X8R8G8B8, D3DFMT_R8G8B8, D3DFMT_R5G6B5, D3DFMT_X1R5G5B5 };

49
src/Client/EventHandler.c Normal file
View File

@ -0,0 +1,49 @@
#include "EventHandler.h"
#include "ErrorHandler.h"
void EventHandler_Register(void** handlers, Int32* count, void* handler) {
for (int i = 0; i < *count; i++) {
if (handlers[i] == handler) return;
}
if (*count == EventHandler_Size) {
ErrorHandler_Fail(String_FromConstant("Unable to add another event handler"));
} else {
handlers[*count] = handler;
*count++;
}
}
void EventHandler_Unregister(void** handlers, Int32* count, void* handler) {
for (int i = 0; i < *count; i++) {
if (handlers[i] != handler) continue;
// Remove this event handler from the list
for (int j = i; j < *count - 1; j++) {
handlers[j] = handlers[j + 1];
}
handlers[*count - 1] = NULL;
*count--;
return;
}
}
void EventHandler_Raise_Void(Event_Void* handlers, Int32 handlersCount) {
for (int i = 0; i < handlersCount; i++) {
handlers[i]();
}
}
void EventHandler_Raise_Int32(Event_Int32* handlers, Int32 handlersCount, Int32 arg) {
for (int i = 0; i < handlersCount; i++) {
handlers[i](arg);
}
}
void EventHandler_Raise_Float32(Event_Float32* handlers, Int32 handlersCount, Real32 arg) {
for (int i = 0; i < handlersCount; i++) {
handlers[i](arg);
}
}

View File

@ -58,7 +58,7 @@ void Gfx_BindTexture(Int32 texId);
void Gfx_DeleteTexture(Int32* texId);
/* Sets whether texturing is applied when rasterizing primitives. */
bool Gfx_SetTexturing(bool enabled);
void Gfx_SetTexturing(bool enabled);
/* Sets whether fog is currently enabled. */

View File

@ -31,6 +31,9 @@ void Platform_MemCpy(void* dst, void* src, UInt32 numBytes);
/* Logs a message to console (if attached). Implictly puts each entry on a newline. */
void Platform_Log(String message);
/* Generates a new random uuid / guid. */
void Platform_NewUuid(UInt8* uuid);
/* Returns whether a file with the given name exists. */
bool Platform_FileExists(String path);

View File

@ -3,11 +3,12 @@
#include "ErrorHandler.h"
#include "String.h"
#include "WorldEnv.h"
#include "Platform.h"
void World_Reset() {
World_Width = 0; World_Height = 0; World_Length = 0;
World_Blocks = NULL; World_BlocksSize = 0;
World_Uuid = Guid.NewGuid();
Platform_NewUuid(World_Uuid);
}
void World_SetNewMap(BlockID* blocks, Int32 blocksSize, Int32 width, Int32 height, Int32 length) {
@ -22,8 +23,8 @@ void World_SetNewMap(BlockID* blocks, Int32 blocksSize, Int32 width, Int32 heigh
if (WorldEnv_EdgeHeight == -1) {
WorldEnv_EdgeHeight = height / 2;
}
if (WorldEnv_CloudHeight == -1) {
WorldEnv_CloudHeight = height + 2;
if (WorldEnv_CloudsHeight == -1) {
WorldEnv_CloudsHeight = height + 2;
}
}

View File

@ -11,7 +11,8 @@ Event_Void WorldEvents_NewMap[EventHandler_Size];
Int32 WorldEvents_NewMapCount = 0;
/* Raises NewMap event. */
void WorldEvents_RaiseNewMap();
#define WorldEvents_RaiseNewMap()\
EventHandler_Raise_Void(WorldEvents_NewMap, WorldEvents_NewMapCount);
/* Raised when a portion of the world is read and decompressed, or generated.
@ -20,23 +21,26 @@ Event_Float32 WorldEvents_MapLoading[EventHandler_Size];
Int32 WorldEvents_MapLoadingCount = 0;
/* Raises MapLoading event. */
void WorldEvents_RaiseMapLoading(Real32 progress);
#define WorldEvents_RaiseMapLoading(progress)\
EventHandler_Raise_Float32(WorldEvents_MapLoading, WorldEvents_MapLoadingCount, progress);
/* Raised when new world has finished loading and the player can now interact with it. */
Event_Void WorldEvents_NewMapLoaded[EventHandler_Size];
Int32 WorldEvents_NewMapLoadedCount = 0;
Event_Void WorldEvents_MapLoaded[EventHandler_Size];
Int32 WorldEvents_MapLoadedCount = 0;
/* Raises NewMapLoaded event. */
void WorldEvents_RaiseNewMapLoaded();
#define WorldEvents_RaiseMapLoaded()\
EventHandler_Raise_Void(WorldEvents_MapLoaded, WorldEvents_MapLoadedCount);
/* Raised when an environment variable of the world is changed by the user, CPE, or WoM config. */
Event_Int32 WorldEvents_EnvVariableChanged[EventHandler_Size];
Int32 WorldEvents_EnvVariableChangedCount = 0;
Event_Int32 WorldEvents_EnvVarChanged[EventHandler_Size];
Int32 WorldEvents_EnvVarChangedCount = 0;
/* Raises EnvVariableChanged event. */
void WorldEvents_RaiseEnvVariableChanged(Int32 envVar);
#define WorldEvents_RaiseEnvVariableChanged(envVar)\
EventHandler_Raise_Int32(WorldEvents_EnvVarChanged, WorldEvents_EnvVarChangedCount, envVar);
/* Environment variable identifiers*/