Design initial event framework

This commit is contained in:
UnknownShadow200 2017-05-09 19:16:56 +10:00
parent 97de676d31
commit 68e815e6ed
11 changed files with 619 additions and 495 deletions

View file

@ -20,7 +20,6 @@ namespace ClassicalSharp.Model {
BlockID block = Block.Air;
float height;
TerrainAtlas1D atlas;
bool bright;
Vector3 minBB, maxBB;
public bool SwitchOrder = false;
ModelCache cache;

View file

@ -17,11 +17,8 @@ namespace ClassicalSharp {
public delegate void Action();
public delegate void Action<T1, T2>(T1 arg1, T2 arg2);
public delegate void Action<T1, T2, T3>(T1 arg1, T2 arg2, T3 arg3);
public delegate void Action<T1, T2, T3, T4>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
public delegate TResult Func<TResult>();
public delegate TResult Func<T1, TResult>(T1 arg1);
public delegate TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2);
public delegate TResult Func<T1, T2, T3, TResult>(T1 arg1, T2 arg2, T3 arg3);
// ################################################################
public static partial class Utils {

View file

@ -173,9 +173,12 @@
<ClInclude Include="BlockID.h" />
<ClInclude Include="Block.h" />
<ClInclude Include="Compiler.h" />
<ClInclude Include="D3D9Api.h" />
<ClInclude Include="DefaultSet.h" />
<ClInclude Include="Bitmap.h" />
<ClInclude Include="ErrorHandler.h" />
<ClInclude Include="WorldEvents.h" />
<ClInclude Include="EventHandler.h" />
<ClInclude Include="FastColour.h" />
<ClInclude Include="Funcs.h" />
<ClInclude Include="Game.h" />
@ -201,9 +204,9 @@
<ItemGroup>
<ClCompile Include="Block.c" />
<ClCompile Include="Compiler.c" />
<ClCompile Include="D3D9Api.c" />
<ClCompile Include="DefaultSet.c" />
<ClCompile Include="Bitmap.c" />
<ClCompile Include="Direct3D9Api.c" />
<ClCompile Include="ExtMath.c" />
<ClCompile Include="FastColour.c" />
<ClCompile Include="GraphicsCommon.c" />

View file

@ -70,6 +70,9 @@
<Filter Include="Source Files\Map">
<UniqueIdentifier>{cb26e83f-9153-4964-9dc2-13502fbb1d1b}</UniqueIdentifier>
</Filter>
<Filter Include="Header Files\Events">
<UniqueIdentifier>{a912f0d5-3ceb-4e7e-ba4e-28170c634047}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="NotchyGenerator.h">
@ -156,6 +159,15 @@
<ClInclude Include="WorldEnv.h">
<Filter>Header Files\Map</Filter>
</ClInclude>
<ClInclude Include="D3D9Api.h">
<Filter>Header Files\Graphics</Filter>
</ClInclude>
<ClInclude Include="EventHandler.h">
<Filter>Header Files\Events</Filter>
</ClInclude>
<ClInclude Include="WorldEvents.h">
<Filter>Header Files\Events</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="NotchyGenerator.c">
@ -203,9 +215,6 @@
<ClCompile Include="GraphicsCommon.c">
<Filter>Source Files\Graphics</Filter>
</ClCompile>
<ClCompile Include="Direct3D9Api.c">
<Filter>Source Files\Graphics</Filter>
</ClCompile>
<ClCompile Include="Vector3I.c">
<Filter>Source Files\Math</Filter>
</ClCompile>
@ -218,5 +227,8 @@
<ClCompile Include="WorldEnv.c">
<Filter>Source Files\Map</Filter>
</ClCompile>
<ClCompile Include="D3D9Api.c">
<Filter>Source Files\Graphics</Filter>
</ClCompile>
</ItemGroup>
</Project>

View file

@ -22,6 +22,13 @@ void Gfx_Init(Game* game) {
}
bool Gfx_SetTexturing(bool enabled) {
if (enabled) return;
ReturnCode hresult = IDirect3DDevice9_SetTexture(device, 0, NULL);
ErrorHandler_CheckOrFail(hresult, "D3D9_SetTexturing");
}
bool d3d9_fogEnable = false;
void Gfx_SetFog(bool enabled) {
if (d3d9_fogEnable == enabled) return;

37
src/Client/EventHandler.h Normal file
View file

@ -0,0 +1,37 @@
#ifndef CS_EVENTHANDLER_H
#define CS_EVENTHANDLER_H
#include "Typedefs.h"
/* Helper method for managing events.
Copyright 2017 ClassicalSharp | Licensed under BSD-3
*/
/* Event handler that takes no arguments. */
typedef void (*Event_Void)(void);
/* Event handler that takes single 32 bit signed integer argument. */
typedef void (*Event_Int32)(Int32 argument);
/* Event handler that takes single floating-point argument. */
typedef void(*Event_Float32)(Real32 argument);
/* Maximum number of event handlers that can be registered. */
#define EventHandler_Size 32
/* Adds given event handler to handlers list for the given event. */
void EventHandler_Register(void** handlers, Int32* count, void* handler);
/* Removes given event handler from handlers list of the given event. */
void EventHandler_Unregister(void** handlers, Int32* count, void* handler);
/* Calls handlers for an event that has no arguments.*/
void EventHandler_Call_Void(Event_Void* handlers, Int32 handlersCount);
/* Calls handlers for an event that has no arguments.*/
void EventHandler_Call_Int32(Event_Int32* handlers, Int32 handlersCount);
/* Calls handlers for an event that has no arguments.*/
void EventHandler_Call_Float32(Event_Float32* handlers, Int32 handlersCount);
#endif

View file

@ -1,6 +1,7 @@
#ifndef CS_GFXAPI_H
#define CS_GFXAPI_H
#include "Typedefs.h"
#include "EventHandler.h"
#include "Bitmap.h"
#include "FastColour.h"
#include "String.h"
@ -16,26 +17,28 @@
void Gfx_Init(Game* game);
/* Maximum supported length of a dimension (width and height) of a 2D texture. */
Int32 Gfx_MaxTextureDimensions;
Int32 Gfx_MaxTextureDimensions = 0;
/* Minimum near plane value supported by the graphics API. */
float Gfx_MinZNear;
float Gfx_MinZNear = 0.0f;
/* Returns whether this graphics api had a valid context. */
bool Gfx_LostContext;
bool Gfx_LostContext = false;
/* Maximum number of vertices that can be indexed. */
#define Gfx_MaxIndices (65536 / 4 * 6)
// TODO: define these, we need an action interface
/*/// <summary> Event raised when a context is destroyed after having been previously lost. </summary>
public event Action ContextLost;
/* Event raised when a context is destroyed after having been previously lost. */
Event_Void Gfx_ContextLost[EventHandler_Size];
Int32 Gfx_ContextLostCount = 0;
/// <summary> Event raised when a context is recreated after having been previously lost. </summary>
public event Action ContextRecreated;
/* Event raised when a context is recreated after having been previously lost. */
Event_Void Gfx_ContextRecreated[EventHandler_Size];
Int32 Gfx_ContextRecreatedCount = 0;
/// <summary> Delegate that is invoked when the current context is lost,
// TODO: IMPLEMENT THIS
/* /// <summary> Delegate that is invoked when the current context is lost,
/// and is repeatedly invoked until the context can be retrieved. </summary>
public Action<ScheduledTask> LostContextFunction;*/

View file

@ -19,7 +19,7 @@ void GfxCommon_LoseContext(String reason) {
Platform_Log(String_FromConstant("Lost graphics context:"));
Platform_Log(reason);
if (ContextLost != null) ContextLost();
EventHandler_Call_Void(Gfx_ContextLost, Gfx_ContextLostCount);
GfxCommon_Free();
}
@ -27,7 +27,7 @@ void GfxCommon_RecreateContext() {
Gfx_LostContext = false;
Platform_Log(String_FromConstant("Recreating graphics context"));
if (ContextRecreated != null) ContextRecreated();
EventHandler_Call_Void(Gfx_ContextRecreated, Gfx_ContextRecreatedCount);
GfxCommon_Init();
}

View file

@ -2,11 +2,12 @@
#include "BlockID.h"
#include "ErrorHandler.h"
#include "String.h"
#include "WorldEnv.h"
void World_Reset() {
World_Width = 0; World_Height = 0; World_Length = 0;
World_Blocks = NULL; World_BlocksSize = 0;
Uuid = Guid.NewGuid();
World_Uuid = Guid.NewGuid();
}
void World_SetNewMap(BlockID* blocks, Int32 blocksSize, Int32 width, Int32 height, Int32 length) {
@ -18,8 +19,12 @@ void World_SetNewMap(BlockID* blocks, Int32 blocksSize, Int32 width, Int32 heigh
ErrorHandler_Fail(String_FromConstant("Blocks array size does not match volume of map."));
}
if (Env.EdgeHeight == -1) Env.EdgeHeight = height / 2;
if (Env.CloudHeight == -1) Env.CloudHeight = height + 2;
if (WorldEnv_EdgeHeight == -1) {
WorldEnv_EdgeHeight = height / 2;
}
if (WorldEnv_CloudHeight == -1) {
WorldEnv_CloudHeight = height + 2;
}
}
BlockID World_GetPhysicsBlock(Int32 x, Int32 y, Int32 z) {

View file

@ -66,5 +66,5 @@ bool World_IsValidPos(Int32 x, Int32 y, Int32 z);
bool World_IsValidPos_3I(Vector3I p);
/* Unpacks the given index into the map's block array into its original world coordinates. */
Vector3I World_GetCoords(int index);
Vector3I World_GetCoords(Int32 index);
#endif

61
src/Client/WorldEvents.h Normal file
View file

@ -0,0 +1,61 @@
#ifndef CS_WORLDEVENTS_H
#define CS_WORLDEVENTS_H
#include "EventHandler.h"
#include "Typedefs.h"
/* World related events.
Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3
*/
/* Raised when the player joins and begins loading a new world. */
Event_Void WorldEvents_NewMap[EventHandler_Size];
Int32 WorldEvents_NewMapCount = 0;
/* Raises NewMap event. */
void WorldEvents_RaiseNewMap();
/* Raised when a portion of the world is read and decompressed, or generated.
The floating point argument is progress (from 0 to 1). */
Event_Float32 WorldEvents_MapLoading[EventHandler_Size];
Int32 WorldEvents_MapLoadingCount = 0;
/* Raises MapLoading event. */
void WorldEvents_RaiseMapLoading(Real32 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;
/* Raises NewMapLoaded event. */
void WorldEvents_RaiseNewMapLoaded();
/* 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;
/* Raises EnvVariableChanged event. */
void WorldEvents_RaiseEnvVariableChanged(Int32 envVar);
/* Environment variable identifiers*/
#define EnvVar_SidesBlock 0
#define EnvVar_EdgeBlock 1
#define EnvVar_EdgeLevel 2
#define EnvVar_CloudsLevel 3
#define EnvVar_CloudsSpeed 4
#define EnvVar_Weather 5
#define EnvVar_SkyCol 6
#define EnvVar_CloudsCol 7
#define EnvVar_FogCol 8
#define EnvVar_SunCol 9
#define EnvVar_ShadowCol 10
#define EnvVar_WeatherSpeed 11
#define EnvVar_WeatherFade 12
#define EnvVar_ExpFog 13
#define EnvVar_SidesOffset 14
#endif