From 34172f31f2e992b122fa290028c1ba4c7ce5cd59 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Thu, 8 Nov 2018 22:36:20 +1100 Subject: [PATCH] Start exporting some functions for C client, also allow page up/down in list menus. --- ClassicalSharp/2D/Screens/Menu/ListScreen.cs | 4 +- src/Core.h | 5 +- src/Deflate.h | 17 +++++-- src/MapGenerator.h | 3 ++ src/Menus.c | 4 +- src/ModelCache.c | 8 ++-- src/ModelCache.h | 16 +++++-- src/Options.h | 22 ++++----- src/PackedCol.h | 2 +- src/Platform.h | 48 ++++++++++---------- src/Stream.h | 18 ++++---- src/Window.h | 8 ++-- 12 files changed, 90 insertions(+), 65 deletions(-) diff --git a/ClassicalSharp/2D/Screens/Menu/ListScreen.cs b/ClassicalSharp/2D/Screens/Menu/ListScreen.cs index 9910795f9..676254f8e 100644 --- a/ClassicalSharp/2D/Screens/Menu/ListScreen.cs +++ b/ClassicalSharp/2D/Screens/Menu/ListScreen.cs @@ -118,9 +118,9 @@ namespace ClassicalSharp.Gui.Screens { public override bool HandlesKeyDown(Key key) { if (key == Key.Escape) { game.Gui.SetNewScreen(null); - } else if (key == Key.Left) { + } else if (key == Key.Left || key == Key.PageUp) { PageClick(false); - } else if (key == Key.Right) { + } else if (key == Key.Right || key == Key.PageDown) { PageClick(true); } else { return false; diff --git a/src/Core.h b/src/Core.h index 946a4a3f4..941554618 100644 --- a/src/Core.h +++ b/src/Core.h @@ -20,9 +20,13 @@ typedef signed __int16 int16_t; typedef signed __int32 int32_t; typedef signed __int64 int64_t; #define NOINLINE_ __declspec(noinline) +#define ALIGN_HINT_(x) /* TODO: Why does this cause LNK2005 errors */ +#define EXPORT_ __declspec(dllexport, noinline) #elif __GNUC__ #include #define NOINLINE_ __attribute__((noinline)) +#define ALIGN_HINT_(x) __attribute__((aligned(x))) +#define EXPORT_ __attribute__((noinline)) #else #error "I don't recognise this compiler. You'll need to add required definitions in Core.h!" #endif @@ -61,7 +65,6 @@ typedef struct TextureRec_ { float U1, V1, U2, V2; } TextureRec; typedef struct Bitmap_ { uint8_t* Scan0; int Width, Height; } Bitmap; /*#define CC_BUILD_GL11*/ -/*#define CC_BUILD_OSX*/ /*#define CC_BUILD_SOLARIS*/ #ifndef CC_BUILD_MANUAL #ifdef _WIN32 diff --git a/src/Deflate.h b/src/Deflate.h index 7fd5a43d6..132c74ad0 100644 --- a/src/Deflate.h +++ b/src/Deflate.h @@ -67,7 +67,10 @@ struct InflateState { void Inflate_Init(struct InflateState* state, struct Stream* source); void Inflate_Process(struct InflateState* state); -NOINLINE_ void Inflate_MakeStream(struct Stream* stream, struct InflateState* state, struct Stream* underlying); +/* Deompresses input data read from another stream using DEFLATE. Read only stream. */ +/* NOTE: This only uncompresses pure DEFLATE compressed data. */ +/* If the data starts with a GZIP or ZLIB header, use GZipHeader_Read or ZLibHeader_Read to skip it. */ +EXPORT_ void Inflate_MakeStream(struct Stream* stream, struct InflateState* state, struct Stream* underlying); #define DEFLATE_BUFFER_SIZE 16384 @@ -89,10 +92,16 @@ struct DeflateState { uint16_t Prev[DEFLATE_BUFFER_SIZE]; bool WroteHeader; }; -NOINLINE_ void Deflate_MakeStream(struct Stream* stream, struct DeflateState* state, struct Stream* underlying); +/* Compresses input data using DEFLATE, then writes compressed output to another stream. Write only stream. */ +/* DEFLATE compression is pure compressed data, there is no header or footer. */ +EXPORT_ void Deflate_MakeStream(struct Stream* stream, struct DeflateState* state, struct Stream* underlying); struct GZipState { struct DeflateState Base; uint32_t Crc32, Size; }; -NOINLINE_ void GZip_MakeStream(struct Stream* stream, struct GZipState* state, struct Stream* underlying); +/* Compresses input data using GZIP, then writes compressed output to another stream. Write only stream. */ +/* GZIP compression is GZIP header, followed by DEFLATE compressed data, followed by GZIP footer. */ +EXPORT_ void GZip_MakeStream(struct Stream* stream, struct GZipState* state, struct Stream* underlying); struct ZLibState { struct DeflateState Base; uint32_t Adler32; }; -NOINLINE_ void ZLib_MakeStream(struct Stream* stream, struct ZLibState* state, struct Stream* underlying); +/* Compresses input data using ZLIB, then writes compressed output to another stream. Write only stream. */ +/* ZLIB compression is ZLIB header, followed by DEFLATE compressed data, followed by ZLIB footer. */ +EXPORT_ void ZLib_MakeStream(struct Stream* stream, struct ZLibState* state, struct Stream* underlying); #endif diff --git a/src/MapGenerator.h b/src/MapGenerator.h index 31c61fa45..121e0bdae 100644 --- a/src/MapGenerator.h +++ b/src/MapGenerator.h @@ -39,6 +39,9 @@ RNGState* Tree_Rnd; /* Appropriate buffer size to hold positions and blocks generated by the tree generator. */ #define TREE_MAX_COUNT 96 +/* Whether a tree can actually be generated at the given coordinates. */ bool TreeGen_CanGrow(int treeX, int treeY, int treeZ, int treeHeight); +/* Generates the blocks (and their positions in the world) that actually make up a tree. */ +/* Returns the number of blocks generated, which will be <= TREE_MAX_COUNT */ int TreeGen_Grow(int treeX, int treeY, int treeZ, int height, Vector3I* coords, BlockRaw* blocks); #endif diff --git a/src/Menus.c b/src/Menus.c index 15eebf144..12af78e66 100644 --- a/src/Menus.c +++ b/src/Menus.c @@ -523,9 +523,9 @@ static bool ListScreen_KeyDown(void* screen, Key key) { struct ListScreen* s = screen; if (key == Key_Escape) { Gui_CloseActive(); - } else if (key == Key_Left) { + } else if (key == Key_Left || key == Key_PageUp) { ListScreen_PageClick(s, false); - } else if (key == Key_Right) { + } else if (key == Key_Right || key == Key_PageDown) { ListScreen_PageClick(s, true); } else { return false; diff --git a/src/ModelCache.c b/src/ModelCache.c index a41143105..c8b4e992c 100644 --- a/src/ModelCache.c +++ b/src/ModelCache.c @@ -73,14 +73,16 @@ int ModelCache_GetTextureIndex(const String* texName) { } void ModelCache_Register(STRING_REF const char* name, const char* defaultTexName, struct Model* instance) { - if (ModelCache_modelCount < MODELCACHE_MAX_MODELS) { - struct CachedModel model; + struct CachedModel model; + String defaultTex; + + if (ModelCache_modelCount < MODELCACHE_MAX_MODELS) { model.Name = String_FromReadonly(name); model.Instance = instance; ModelCache_Models[ModelCache_modelCount++] = model; if (defaultTexName) { - String defaultTex = String_FromReadonly(defaultTexName); + defaultTex = String_FromReadonly(defaultTexName); instance->defaultTexIndex = ModelCache_GetTextureIndex(&defaultTex); } } else { diff --git a/src/ModelCache.h b/src/ModelCache.h index 3ecd7b35c..d39a377f7 100644 --- a/src/ModelCache.h +++ b/src/ModelCache.h @@ -23,8 +23,16 @@ VertexP3fT2fC4b ModelCache_Vertices[MODELCACHE_MAX_VERTICES]; void ModelCache_Init(void); void ModelCache_Free(void); -NOINLINE_ struct Model* ModelCache_Get(const String* name); -NOINLINE_ int ModelCache_GetTextureIndex(const String* texName); -NOINLINE_ void ModelCache_Register(STRING_REF const char* name, const char* defaultTexName, struct Model* instance); -NOINLINE_ void ModelCache_RegisterTexture(STRING_REF const char* texName); +/* Returns pointer to model whose name caselessly matches given name. */ +EXPORT_ struct Model* ModelCache_Get(const String* name); +/* Returns index of cached texture whose name caselessly matches given name. */ +EXPORT_ int ModelCache_GetTextureIndex(const String* texName); +/* Adds a model to the list of cached models. (e.g. "skeleton") */ +/* Cached models can be applied to entities to change their appearance. Use Entity_SetModel for that. */ +/* NOTE: defaultTexName can be NULL, and is for some models. (such as the "block" model) */ +EXPORT_ void ModelCache_Register(STRING_REF const char* name, const char* defaultTexName, struct Model* instance); +/* Adds a texture to the list of cached textures. (e.g. "skeleton.png") */ +/* Cached textures are automatically loaded from texture packs. Used as a 'default skin' for models. */ +/* NOTE: Textures should be registered BEFORE models are registered. */ +EXPORT_ void ModelCache_RegisterTexture(STRING_REF const char* texName); #endif diff --git a/src/Options.h b/src/Options.h index 432cfd0f0..c9a50cec7 100644 --- a/src/Options.h +++ b/src/Options.h @@ -79,27 +79,27 @@ NOINLINE_ bool Options_HasAnyChanged(void); NOINLINE_ void Options_Free(void); /* Returns value of given option, or defalt value if not found. */ -NOINLINE_ void Options_Get(const char* key, String* value, const char* defValue); +EXPORT_ void Options_Get(const char* key, String* value, const char* defValue); /* Returns value of given option as an integer, or defalt value if could not be converted. */ -NOINLINE_ int Options_GetInt(const char* key, int min, int max, int defValue); +EXPORT_ int Options_GetInt(const char* key, int min, int max, int defValue); /* Returns value of given option as a bool, or defalt value if could not be converted. */ -NOINLINE_ bool Options_GetBool(const char* key, bool defValue); +EXPORT_ bool Options_GetBool(const char* key, bool defValue); /* Returns value of given option as a float, or defalt value if could not be converted. */ -NOINLINE_ float Options_GetFloat(const char* key, float min, float max, float defValue); +EXPORT_ float Options_GetFloat(const char* key, float min, float max, float defValue); /* Returns value of given option as an integer, or defalt value if could not be converted. */ /* NOTE: Conversion is done by going through all elements of names, returning index of a match. */ -NOINLINE_ int Options_GetEnum(const char* key, int defValue, const char** names, int namesCount); +EXPORT_ int Options_GetEnum(const char* key, int defValue, const char** names, int namesCount); /* Sets value of given option to either "true" or "false". */ -NOINLINE_ void Options_SetBool(const char* keyRaw, bool value); +EXPORT_ void Options_SetBool(const char* keyRaw, bool value); /* Sets value of given option to given integer converted to a string. */ -NOINLINE_ void Options_SetInt(const char* keyRaw, int value); +EXPORT_ void Options_SetInt(const char* keyRaw, int value); /* Sets value of given option to given string. */ -NOINLINE_ void Options_Set(const char* keyRaw, const String* value); +EXPORT_ void Options_Set(const char* keyRaw, const String* value); /* Sets value of given option to given string. */ -NOINLINE_ void Options_SetString(const String* key, const String* value); +EXPORT_ void Options_SetString(const String* key, const String* value); /* Loads options from disc. Leaves options changed in this session alone. */ -NOINLINE_ void Options_Load(void); +EXPORT_ void Options_Load(void); /* Saves all options to disc. */ -NOINLINE_ void Options_Save(void); +EXPORT_ void Options_Save(void); #endif diff --git a/src/PackedCol.h b/src/PackedCol.h index 54071dbac..185db0443 100644 --- a/src/PackedCol.h +++ b/src/PackedCol.h @@ -6,7 +6,7 @@ */ /* Represents an ARGB colour, in a format suitable for the native graphics api. */ -typedef struct PackedCol_ { + typedef ALIGN_HINT_(4) struct PackedCol_ { #ifdef CC_BUILD_D3D9 uint8_t B, G, R, A; #else diff --git a/src/Platform.h b/src/Platform.h index f10bd328c..f32f7984c 100644 --- a/src/Platform.h +++ b/src/Platform.h @@ -49,7 +49,7 @@ void GraphicsMode_MakeDefault(struct GraphicsMode* m); /* Encodes a string in platform specific format. (e.g. unicode on windows, UTF8 on linux) */ /* NOTE: Only useful for platform specific function calls - do NOT try to interpret the data. Returns the number of bytes written, excluding trailing NULL terminator. */ -NOINLINE_ int Platform_ConvertString(void* data, const String* src); +EXPORT_ int Platform_ConvertString(void* data, const String* src); /* Initalises the platform specific state. */ void Platform_Init(void); /* Frees the platform specific state. */ @@ -61,16 +61,16 @@ void Platform_Exit(ReturnCode code); /* Gets the command line arguments passed to the program. */ int Platform_GetCommandLineArgs(int argc, STRING_REF const char** argv, String* args); /* Starts the platform's shell with the given arguments. (e.g. open http:// url in web browser) */ -ReturnCode Platform_StartShell(const String* args); +EXPORT_ ReturnCode Platform_StartShell(const String* args); /* Allocates a block of memory, with undetermined contents. Exits process on allocation failure. */ -NOINLINE_ void* Mem_Alloc(uint32_t numElems, uint32_t elemsSize, const char* place); +EXPORT_ void* Mem_Alloc(uint32_t numElems, uint32_t elemsSize, const char* place); /* Allocates a block of memory, with contents of all 0. Exits process on allocation failure. */ -NOINLINE_ void* Mem_AllocCleared(uint32_t numElems, uint32_t elemsSize, const char* place); +EXPORT_ void* Mem_AllocCleared(uint32_t numElems, uint32_t elemsSize, const char* place); /* Reallocates a block of memory, with undetermined contents. Exits process on reallocation failure. */ -NOINLINE_ void* Mem_Realloc(void* mem, uint32_t numElems, uint32_t elemsSize, const char* place); +EXPORT_ void* Mem_Realloc(void* mem, uint32_t numElems, uint32_t elemsSize, const char* place); /* Frees an allocated a block of memory. Does nothing when passed NULL. */ -NOINLINE_ void Mem_Free(void* mem); +EXPORT_ void Mem_Free(void* mem); /* Sets the contents of a block of memory to the given value. */ void Mem_Set(void* dst, uint8_t value, uint32_t numBytes); /* Copies a block of memory to another block. NOTE: These blocks MUST NOT overlap. */ @@ -130,47 +130,47 @@ ReturnCode File_Position(void* file, uint32_t* position); ReturnCode File_Length(void* file, uint32_t* length); /* Blocks the current thread for the given number of milliseconds. */ -void Thread_Sleep(uint32_t milliseconds); +EXPORT_ void Thread_Sleep(uint32_t milliseconds); typedef void Thread_StartFunc(void); /* Starts a new thread, optionally immediately detaching it. (See Thread_Detach) */ -void* Thread_Start(Thread_StartFunc* func, bool detach); +EXPORT_ void* Thread_Start(Thread_StartFunc* func, bool detach); /* Frees the platform specific persistent data associated with the thread. */ /* NOTE: You must either detach or join threads, as this data otherwise leaks. */ -void Thread_Detach(void* handle); +EXPORT_ void Thread_Detach(void* handle); /* Blocks the current thread, until the given thread has finished. */ /* NOTE: Once a thread has been detached, you can no longer use this method. */ -void Thread_Join(void* handle); +EXPORT_ void Thread_Join(void* handle); /* Allocates a new mutex. (used to synchronise access to a shared resource) */ -void* Mutex_Create(void); +EXPORT_ void* Mutex_Create(void); /* Frees an allocated mutex. */ -void Mutex_Free(void* handle); +EXPORT_ void Mutex_Free(void* handle); /* Locks the given mutex, blocking other threads from entering. */ -void Mutex_Lock(void* handle); +EXPORT_ void Mutex_Lock(void* handle); /* Unlocks the given mutex, allowing other threads to enter. */ -void Mutex_Unlock(void* handle); +EXPORT_ void Mutex_Unlock(void* handle); /* Allocates a new waitable. (used to conditionally wake-up a blocked thread) */ -void* Waitable_Create(void); +EXPORT_ void* Waitable_Create(void); /* Frees an allocated waitable. */ -void Waitable_Free(void* handle); +EXPORT_ void Waitable_Free(void* handle); /* Signals a waitable, waking up blocked threads. */ -void Waitable_Signal(void* handle); +EXPORT_ void Waitable_Signal(void* handle); /* Blocks the calling thread until the waitable gets signalled. */ -void Waitable_Wait(void* handle); +EXPORT_ void Waitable_Wait(void* handle); /* Blocks the calling thread until the waitable gets signalled, or milliseconds delay passes. */ -void Waitable_WaitFor(void* handle, uint32_t milliseconds); +EXPORT_ void Waitable_WaitFor(void* handle, uint32_t milliseconds); /* Gets the list of all supported font names on this platform. */ -NOINLINE_ void Font_GetNames(StringsBuffer* buffer); +EXPORT_ void Font_GetNames(StringsBuffer* buffer); /* Allocates a new font from the given arguments. */ -NOINLINE_ void Font_Make(FontDesc* desc, const String* fontName, int size, int style); +EXPORT_ void Font_Make(FontDesc* desc, const String* fontName, int size, int style); /* Frees an allocated font. */ -NOINLINE_ void Font_Free(FontDesc* desc); +EXPORT_ void Font_Free(FontDesc* desc); /* Measures dimensions of the given text, if it was drawn with the given font. */ -NOINLINE_ Size2D Platform_TextMeasure(struct DrawTextArgs* args); +EXPORT_ Size2D Platform_TextMeasure(struct DrawTextArgs* args); /* Draws the given text with the given font onto the given bitmap. */ -NOINLINE_ Size2D Platform_TextDraw(struct DrawTextArgs* args, Bitmap* bmp, int x, int y, PackedCol col); +EXPORT_ Size2D Platform_TextDraw(struct DrawTextArgs* args, Bitmap* bmp, int x, int y, PackedCol col); /* Allocates a new socket. */ void Socket_Create(SocketPtr* socket); diff --git a/src/Stream.h b/src/Stream.h index 4e14cc195..8a7a8d18b 100644 --- a/src/Stream.h +++ b/src/Stream.h @@ -50,20 +50,20 @@ void Stream_Init(struct Stream* s); ReturnCode Stream_DefaultReadU8(struct Stream* s, uint8_t* data); /* Wrapper for File_Open() then Stream_FromFile() */ -NOINLINE_ ReturnCode Stream_OpenFile(struct Stream* s, const String* path); +EXPORT_ ReturnCode Stream_OpenFile(struct Stream* s, const String* path); /* Wrapper for File_Create() then Stream_FromFile() */ -NOINLINE_ ReturnCode Stream_CreateFile(struct Stream* s, const String* path); +EXPORT_ ReturnCode Stream_CreateFile(struct Stream* s, const String* path); /* Wraps a file, allowing reading from/writing to/seeking in the file. */ -NOINLINE_ void Stream_FromFile(struct Stream* s, void* file); +EXPORT_ void Stream_FromFile(struct Stream* s, void* file); /* Wraps another Stream, only allows reading up to 'len' bytes from the wrapped stream. */ -NOINLINE_ void Stream_ReadonlyPortion(struct Stream* s, struct Stream* source, uint32_t len); +EXPORT_ void Stream_ReadonlyPortion(struct Stream* s, struct Stream* source, uint32_t len); /* Wraps a block of memory, allowing reading from and seeking in the block. */ -NOINLINE_ void Stream_ReadonlyMemory(struct Stream* s, void* data, uint32_t len); +EXPORT_ void Stream_ReadonlyMemory(struct Stream* s, void* data, uint32_t len); /* Wraps a block of memory, allowing writing to and seeking in the block. */ -NOINLINE_ void Stream_WriteonlyMemory(struct Stream* s, void* data, uint32_t len); +EXPORT_ void Stream_WriteonlyMemory(struct Stream* s, void* data, uint32_t len); /* Wraps another Stream, reading through an intermediary buffer. (Useful for files, since each read call is expensive) */ -NOINLINE_ void Stream_ReadonlyBuffered(struct Stream* s, struct Stream* source, void* data, uint32_t size); +EXPORT_ void Stream_ReadonlyBuffered(struct Stream* s, struct Stream* source, void* data, uint32_t size); /* Reads a little-endian 16 bit unsigned integer from memory. */ uint16_t Stream_GetU16_LE(uint8_t* data); @@ -87,7 +87,7 @@ ReturnCode Stream_ReadU32_BE(struct Stream* s, uint32_t* value); /* Reads a line of UTF8 encoded character from the stream. */ /* NOTE: Reads one byte at a time. May want to use Stream_ReadonlyBuffered. */ -ReturnCode Stream_ReadLine(struct Stream* s, String* text); +EXPORT_ ReturnCode Stream_ReadLine(struct Stream* s, String* text); /* Writes a line of UTF8 encoded text to the stream. */ -ReturnCode Stream_WriteLine(struct Stream* s, String* text); +EXPORT_ ReturnCode Stream_WriteLine(struct Stream* s, String* text); #endif diff --git a/src/Window.h b/src/Window.h index 7b5543064..33650525c 100644 --- a/src/Window.h +++ b/src/Window.h @@ -85,14 +85,14 @@ void Window_SetClientSize(int width, int height); void Window_Close(void); /* Processes all pending window messages/events. */ void Window_ProcessEvents(void); -/* Transforms the specified point from screen to client coordinates. */ +/* Converts the specified point from screen to client coordinates. */ Point2D Window_PointToClient(int x, int y); -/* Transforms the specified point from client to screen coordinates. */ +/* Converts the specified point from client to screen coordinates. */ Point2D Window_PointToScreen(int x, int y); /* Gets the position of the cursor in screen coordinates. */ Point2D Window_GetScreenCursorPos(void); -/* Sets the position of the cursor in screen coordinates.*/ +/* Sets the position of the cursor in screen coordinates. */ void Window_SetScreenCursorPos(int x, int y); /* Whether the cursor is visible when over this window. */ bool Window_GetCursorVisible(void); @@ -102,7 +102,7 @@ so setting invisible multiple times means you must then set visible multiple tim void Window_SetCursorVisible(bool visible); #ifndef CC_BUILD_D3D9 -/* Initalises an OpenGL context that most closely matches the input arguments. */ +/* Initialises an OpenGL context that most closely matches the input arguments. */ /* NOTE: You must have created a window beforehand, as the GL context is attached to the window. */ void GLContext_Init(struct GraphicsMode* mode); /* Updates the OpenGL context after the window is resized. */