diff --git a/src/Camera.c b/src/Camera.c index ee3e75e97..3a54aa85a 100644 --- a/src/Camera.c +++ b/src/Camera.c @@ -114,7 +114,7 @@ static void PerspectiveCamera_CalcViewBobbing(float t, float velTiltScale) { struct Entity* e = &p->Base; struct Matrix Camera_tiltY, Camera_velX; - Matrix_RotateZ(&Camera_TiltM, -p->Tilt.TiltX * e->Anim.BobStrength); + Matrix_RotateZ(&Camera_TiltM, -p->Tilt.TiltX * e->Anim.BobStrength); Matrix_RotateX(&Camera_tiltY, Math_AbsF(p->Tilt.TiltY) * 3.0f * e->Anim.BobStrength); Matrix_MulBy(&Camera_TiltM, &Camera_tiltY); @@ -157,10 +157,10 @@ static bool FirstPersonCamera_Zoom(float amount) { return false; } static void FirstPersonCamera_Init(struct Camera* cam) { PerspectiveCamera_Init(cam); - cam->IsThirdPerson = false; + cam->IsThirdPerson = false; cam->GetOrientation = FirstPersonCamera_GetOrientation; - cam->GetPosition = FirstPersonCamera_GetPosition; - cam->Zoom = FirstPersonCamera_Zoom; + cam->GetPosition = FirstPersonCamera_GetPosition; + cam->Zoom = FirstPersonCamera_Zoom; } diff --git a/src/Camera.h b/src/Camera.h index f11b2b12b..921c1d2ea 100644 --- a/src/Camera.h +++ b/src/Camera.h @@ -1,30 +1,45 @@ #ifndef CC_CAMERA_H #define CC_CAMERA_H -#include "Picking.h" +#include "Vectors.h" /* Represents a camera, may be first or third person. Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 */ +struct PickedPos; +/* Tilt effect applied to the camera. */ struct Matrix Camera_TiltM; +/* Bobbing offset of camera from player's eye. */ float Camera_BobbingVer, Camera_BobbingHor; struct Camera { + /* Whether this camera is third person. (i.e. not allowed when -thirdperson in MOTD) */ bool IsThirdPerson; + /* Calculates the current projection matrix of this camera. */ void (*GetProjection)(struct Matrix* proj); + /* Calculates the current modelview matrix of this camera. */ void (*GetView)(struct Matrix* view); + /* Returns the current orientation of the camera. */ Vector2 (*GetOrientation)(void); + /* Returns the current interpolated position of the camera. */ Vector3 (*GetPosition)(float t); void (*UpdateMouse)(void); + /* Called when user closes all menus, and is interacting with camera again. */ + /* Typically, this is used to move mouse cursor to centre of the window. */ void (*RegrabMouse)(void); + /* Calculates selected block in the world, based on camera's current state */ void (*GetPickedBlock)(struct PickedPos* pos); + /* Zooms the camera in or out when scrolling mouse wheel. */ bool (*Zoom)(float amount); }; +/* Camera user is currently using. */ struct Camera* Camera_Active; +/* Initialises the list of cameras. */ void Camera_Init(void); +/* Switches to the next camera in the list. */ void Camera_CycleActive(void); #endif diff --git a/src/Input.h b/src/Input.h index 3c56e30fc..fe8703f6d 100644 --- a/src/Input.h +++ b/src/Input.h @@ -31,7 +31,7 @@ */ typedef enum Key_ { - Key_None, /* Key outside the known keys */ + Key_None, /* Unrecognised key */ Key_ShiftLeft, Key_ShiftRight, Key_ControlLeft, Key_ControlRight, Key_AltLeft, Key_AltRight, Key_WinLeft, Key_WinRight, Key_Menu, @@ -62,13 +62,14 @@ typedef enum Key_ { Key_Tilde, Key_Minus, Key_Plus, Key_BracketLeft, Key_BracketRight, Key_Semicolon, Key_Quote, Key_Comma, Key_Period, Key_Slash, Key_BackSlash, - Key_XButton1, Key_XButton2, + Key_XButton1, Key_XButton2, /* so these can be used for hotkeys */ Key_Count, } Key; -/* Gets whether key repeating is on or not. If on (desirable for text input), multiple KeyDowns (varies by OS) -are generated for the same key when it is held down for a period of time. Should be off for game input. */ +/* Gets whether key repeating is on or not. When on, multiple KeyDown events are raised when the same key is +held down for a period of time (frequency depends on platform). Should be on for menu input, off for game input. */ bool Key_KeyRepeat; +/* Simple names for each keyboard button. */ extern const char* Key_Names[Key_Count]; #define Key_IsWinPressed() (Key_Pressed[Key_WinLeft] || Key_Pressed[Key_WinRight]) @@ -76,8 +77,13 @@ extern const char* Key_Names[Key_Count]; #define Key_IsControlPressed() (Key_Pressed[Key_ControlLeft] || Key_Pressed[Key_ControlRight]) #define Key_IsShiftPressed() (Key_Pressed[Key_ShiftLeft] || Key_Pressed[Key_ShiftRight]) +/* Pressed state of each keyboard button. Use Key_SetPressed to change. */ bool Key_Pressed[Key_Count]; +/* Sets the pressed state of a keyboard button. */ +/* Raises KeyEvents_Up or KeyEvents_Down if state differs, or Key_KeyRepeat is on. */ void Key_SetPressed(Key key, bool pressed); +/* Resets all keys to not pressed state. */ +/* Raises KeyEvents_Up for each previously pressed key. */ void Key_Clear(void); @@ -86,16 +92,23 @@ typedef enum MouseButton_ { MouseButton_Count, } MouseButton; +/* Wheel position of the mouse. Use Mouse_SetWheel to change. */ float Mouse_Wheel; +/* X and Y coordinates of the mouse. Use Mouse_SetPosition to change. */ int Mouse_X, Mouse_Y; +/* Pressed state of each mouse button. Use Mouse_SetPressed to change. */ bool Mouse_Pressed[MouseButton_Count]; +/* Sets the pressed state of a mouse button. */ +/* Raises MouseEvents_Up or MouseEvents_Down if state differs. */ void Mouse_SetPressed(MouseButton btn, bool pressed); +/* Sets wheel position of the mouse, always raising MouseEvents_Wheel. */ void Mouse_SetWheel(float wheel); +/* Sets X and Y position of the mouse, always raising MouseEvents_Moved. */ void Mouse_SetPosition(int x, int y); -/* Enumeration of all custom key bindings. */ +/* Enumeration of all key bindings. */ typedef enum KeyBind_ { KeyBind_Forward, KeyBind_Back, KeyBind_Left, KeyBind_Right, KeyBind_Jump, KeyBind_Respawn, KeyBind_SetSpawn, KeyBind_Chat, @@ -109,11 +122,15 @@ typedef enum KeyBind_ { KeyBind_Count } KeyBind; +/* Gets the key that is bound to the the given key binding. */ Key KeyBind_Get(KeyBind binding); +/* Gets the default key that the given key binding is bound to */ Key KeyBind_GetDefault(KeyBind binding); +/* Gets whether the key bound to the given key binding is pressed. */ bool KeyBind_IsPressed(KeyBind binding); +/* Set the key that the given key binding is bound to. (also updates options list) */ void KeyBind_Set(KeyBind binding, Key key); -/* Initalises and loads key bindings. */ +/* Initalises and loads key bindings from options. */ void KeyBind_Init(void); @@ -132,10 +149,17 @@ enum HOTKEY_FLAGS { HOTKEY_FLAG_CTRL = 1, HOTKEY_FLAG_SHIFT = 2, HOTKEY_FLAG_ALT = 4, }; +/* Adds or updates a new hotkey. */ void Hotkeys_Add(Key trigger, int flags, const String* text, bool more); +/* Removes the given hotkey. */ bool Hotkeys_Remove(Key trigger, int flags); +/* Returns the first hotkey which is bound to the given key and has its modifiers pressed. */ +/* NOTE: The hotkeys list is sorted, so hotkeys with most modifiers are checked first. */ int Hotkeys_FindPartial(Key key); +/* Initalises and loads hotkeys from options. */ void Hotkeys_Init(void); +/* Called when user has removed a hotkey. (removes it from options) */ void Hotkeys_UserRemovedHotkey(Key trigger, int flags); +/* Called when user has added a hotkey. (Adds it to options) */ void Hotkeys_UserAddedHotkey(Key trigger, int flags, bool moreInput, const String* text); #endif diff --git a/src/Options.h b/src/Options.h index d21364c1f..caf78f2eb 100644 --- a/src/Options.h +++ b/src/Options.h @@ -73,19 +73,33 @@ extern const char* FpsLimit_Names[FpsLimit_Count]; StringsBuffer Options_Keys; StringsBuffer Options_Values; +/* Returns whether user has changed any options this session. */ NOINLINE_ bool Options_HasAnyChanged(void); +/* Frees any memory allocated in storing options. */ 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); +/* 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); +/* 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); +/* 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); +/* 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); +/* Sets value of given option to either "true" or "false" */ NOINLINE_ 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); +/* Sets value of given option to given string. */ NOINLINE_ 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); +/* Loads options from disc. Leaves options changed in this session alone. */ NOINLINE_ void Options_Load(void); +/* Saves all options to disc. */ NOINLINE_ void Options_Save(void); #endif diff --git a/src/String.c b/src/String.c index 5b13bedb1..92ff6aa17 100644 --- a/src/String.c +++ b/src/String.c @@ -679,7 +679,7 @@ bool Convert_TryParseBool(const String* str, bool* value) { #define STRINGSBUFFER_LEN_MASK 0x1FFUL #define STRINGSBUFFER_BUFFER_EXPAND_SIZE 8192 -void StringsBuffer_Init(StringsBuffer* buffer) { +NOINLINE_ static void StringsBuffer_Init(StringsBuffer* buffer) { buffer->Count = 0; buffer->TotalLength = 0; buffer->TextBuffer = buffer->_DefaultBuffer; @@ -689,7 +689,7 @@ void StringsBuffer_Init(StringsBuffer* buffer) { } void StringsBuffer_Clear(StringsBuffer* buffer) { - /* never initialised to begin with */ + /* Never initialised to begin with */ if (!buffer->_FlagsBufferSize) return; if (buffer->TextBuffer != buffer->_DefaultBuffer) { @@ -716,7 +716,7 @@ String StringsBuffer_UNSAFE_Get(StringsBuffer* buffer, int i) { } void StringsBuffer_Add(StringsBuffer* buffer, const String* text) { - /* Forgot to initalise flags buffer, so do it here */ + /* StringsBuffer hasn't been initalised yet, do it here */ if (!buffer->_FlagsBufferSize) { StringsBuffer_Init(buffer); } if (buffer->Count == buffer->_FlagsBufferSize) { @@ -815,7 +815,6 @@ void WordWrap_Do(STRING_REF String* text, String* lines, int numLines, int lineL } } -/* Calculates where the given raw index is located in the wrapped lines. */ void WordWrap_GetCoords(int index, const String* lines, int numLines, int* coordX, int* coordY) { if (index == -1) index = Int32_MaxValue; int offset = 0; *coordX = -1; *coordY = 0; diff --git a/src/String.h b/src/String.h index 3d2a1c57c..1ed69280d 100644 --- a/src/String.h +++ b/src/String.h @@ -12,6 +12,7 @@ /* Indicates that a reference to the buffer in a string argument is persisted after the function has completed. Thus it is **NOT SAFE** to allocate a string on the stack. */ #define STRING_REF +/* Converts a character from A-Z to a-z, otherwise is left untouched. */ #define Char_MakeLower(c) if ((c) >= 'A' && (c) <= 'Z') { (c) += ' '; } typedef struct String_ { @@ -20,15 +21,20 @@ typedef struct String_ { uint16_t capacity; /* Max number of characters */ } String; +/* Counts number of characters until a '\0' is found. */ uint16_t String_CalcLen(const char* raw, uint16_t capacity); +/* Constructs a string that points to NULL, and has 0 length. */ String String_MakeNull(void); +/* Constructs a string from the given arguments. */ String String_Init(STRING_REF char* buffer, uint16_t length, uint16_t capacity); +/* Constructs a string from the given arguments, then sets all characters to '\0'. */ String String_InitAndClear(STRING_REF char* buffer, uint16_t capacity); -/* Constructs a new string from a (maybe null terminated) buffer. */ +/* Constructs a string from a (maybe null terminated) buffer. */ NOINLINE_ String String_FromRaw(STRING_REF char* buffer, uint16_t capacity); -/* Constructs a new string from a null-terminated constant readonly buffer. */ +/* Constructs a string from a null-terminated constant readonly buffer. */ NOINLINE_ String String_FromReadonly(STRING_REF const char* buffer); +/* Constructs a string from a compile time array, then sets all characters to '\0'. */ #define String_ClearedArray(buffer) String_InitAndClear(buffer, (uint16_t)sizeof(buffer)) /* Constructs a string from a compile time string constant */ #define String_FromConst(text) { text, (uint16_t)(sizeof(text) - 1), (uint16_t)(sizeof(text) - 1)} @@ -39,41 +45,87 @@ NOINLINE_ String String_FromReadonly(STRING_REF const char* buffer); /* Constructs a string from a compile time array (leaving 1 byte of room for null terminator) */ #define String_NT_Array(buffer) { buffer, 0, (uint16_t)(sizeof(buffer) - 1)} +/* Removes all colour codes from the given string. */ NOINLINE_ void String_StripCols(String* str); +/* Sets length of dst to 0, then appends all characters in src. */ NOINLINE_ void String_Copy(String* dst, const String* src); +/* UNSAFE: Returns a substring of the given string. (sub.buffer is within str.buffer + str.length) */ NOINLINE_ String String_UNSAFE_Substring(STRING_REF const String* str, int offset, int length); +/* UNSAFE: Returns a substring of the given string. (sub.buffer is within str.buffer + str.length) */ #define String_UNSAFE_SubstringAt(str, offset) (String_UNSAFE_Substring(str, offset, (str)->length - (offset))) NOINLINE_ void String_UNSAFE_Split(STRING_REF const String* str, char c, String* subs, int* subsCount); +/* UNSAFE: Splits a string of the form [key][c][value] into two substrings. */ +/* e.g., allowed =true becomes 'allowed' and 'true', and excludes the space. */ NOINLINE_ bool String_UNSAFE_Separate(STRING_REF const String* str, char c, String* key, String* value); +/* Returns whether all characters of the strings are equal. */ NOINLINE_ bool String_Equals(const String* a, const String* b); +/* Returns whether all characters of the strings are case-insensitively equal. */ NOINLINE_ bool String_CaselessEquals(const String* a, const String* b); +/* Returns whether all characters of the strings are case-insensitively equal. */ +/* NOTE: This is faster than String_CaselessEquals(a, String_FromReadonly(b)) */ NOINLINE_ bool String_CaselessEqualsConst(const String* a, const char* b); +/* Breaks down an integer into an array of digits. */ +/* NOTE: Digits are in reverse order, so e.g. '200' becomes '0','0','2' */ NOINLINE_ int String_MakeUInt32(uint32_t num, char* numBuffer); +/* Appends a character to the end of a string. */ +/* Returns false when str->length == str->capcity, true otherwise. */ bool String_Append(String* str, char c); +/* Appends a boolean as either "true" or "false" to the end of a string. */ NOINLINE_ bool String_AppendBool(String* str, bool value); +/* Appends the digits of an integer (and -sign if negative) to the end of a string. */ NOINLINE_ bool String_AppendInt(String* str, int num); +/* Appends the digits of an unsigned 32 bit integer to the end of a string. */ NOINLINE_ bool String_AppendUInt32(String* str, uint32_t num); +/* Appends the digits of an unsigned 64 bit integer to the end of a string. */ NOINLINE_ bool String_AppendUInt64(String* str, uint64_t num); +/* Appends the digits of a float as a decimal. */ +/* NOTE: If the number is an integer, no decimal point is added. */ +/* Otherwise, fracDigits digits are added after a decimal point. */ +/* e.g. 1.0f produces "1", 2.6745f produces "2.67" when fracDigits is 2 */ NOINLINE_ bool String_AppendFloat(String* str, float num, int fracDigits); /* TODO: Need to account for , or . for decimal */ +/* Appends characters to the end of a string. src MUST be null-terminated. */ NOINLINE_ bool String_AppendConst(String* str, const char* src); +/* Appends characters to the end of a string. */ NOINLINE_ bool String_AppendString(String* str, const String* src); +/* Appends characters to the end of a string, skipping any colour codes. */ NOINLINE_ bool String_AppendColorless(String* str, const String* src); +/* Appends the two hex digits of a byte to the end of a string. */ NOINLINE_ bool String_AppendHex(String* str, uint8_t value); +/* Returns first index of the given character in the given string, -1 if not found. */ NOINLINE_ int String_IndexOf(const String* str, char c, int offset); +/* Returns last index of the given character in the given string, -1 if not found. */ NOINLINE_ int String_LastIndexOf(const String* str, char c); +/* Inserts the given character into the given string. Exits process if this fails. */ +/* e.g. inserting 'd' at offset '1' into "abc" produces "adbc" */ NOINLINE_ void String_InsertAt(String* str, int offset, char c); +/* Deletes a character from the given string. Exits process if this fails. */ +/* e.g. deleting at offset '1' from "adbc" produces "abc" */ NOINLINE_ void String_DeleteAt(String* str, int offset); +/* Trims leading spaces from the given string. */ +/* NOTE: Works by adjusting buffer/length, the characters in memory are not shifted. */ NOINLINE_ void String_TrimStart(String* str); +/* Trims trailing spaces from the given string. */ +/* NOTE: Works by adjusting buffer/length, the characters in memory are not shifted. */ NOINLINE_ void String_TrimEnd(String* str); +/* Returns first index of the given substring in the given string, -1 if not found. */ +/* e.g. index of "ab" within "cbabd" is 2 */ NOINLINE_ int String_IndexOfString(const String* str, const String* sub); +/* Returns whether given substring is inside the given string. */ #define String_ContainsString(str, sub) (String_IndexOfString(str, sub) >= 0) +/* Returns whether given substring is case-insensitively inside the given string. */ NOINLINE_ bool String_CaselessContains(const String* str, const String* sub); +/* Returns whether given substring is case-insensitively equal to the beginning of the given string. */ NOINLINE_ bool String_CaselessStarts(const String* str, const String* sub); +/* Returns whether given substring is case-insensitively equal to the ending of the given string. */ NOINLINE_ bool String_CaselessEnds(const String* str, const String* sub); +/* Compares the length of the given strings, then compares the characters if same length. Returns: */ +/* -X if a.length < b.length, X if a.length > b.length */ +/* -X if a.buffer[i] < b.buffer[i], X if a.buffer[i] > b.buffer[i] */ +/* else returns 0. NOTE: The return value is not just in -1,0,1! */ NOINLINE_ int String_Compare(const String* a, const String* b); void String_Format1(String* str, const char* format, const void* a1); @@ -81,17 +133,29 @@ void String_Format2(String* str, const char* format, const void* a1, const void* void String_Format3(String* str, const char* format, const void* a1, const void* a2, const void* a3); void String_Format4(String* str, const char* format, const void* a1, const void* a2, const void* a3, const void* a4); +/* Converts a code page 437 character to its unicode equivalent. */ Codepoint Convert_CP437ToUnicode(char c); +/* Converts a unicode character to its code page 437 equivalent, or '?' if no match. */ char Convert_UnicodeToCP437(Codepoint cp); +/* Attempts to convert a unicode character to its code page 437 equivalent. */ bool Convert_TryUnicodeToCP437(Codepoint cp, char* value); +/* Appends all characters from UTF8 encoded data to the given string. */ void String_DecodeUtf8(String* str, uint8_t* data, uint32_t len); +/* Attempts to convert the given string into an unsigned 8 bit integer. */ NOINLINE_ bool Convert_TryParseUInt8(const String* str, uint8_t* value); +/* Attempts to convert the given string into an signed 16 bit integer. */ NOINLINE_ bool Convert_TryParseInt16(const String* str, int16_t* value); +/* Attempts to convert the given string into an unsigned 16 bit integer. */ NOINLINE_ bool Convert_TryParseUInt16(const String* str, uint16_t* value); +/* Attempts to convert the given string into an integer. */ NOINLINE_ bool Convert_TryParseInt(const String* str, int* value); +/* Attempts to convert the given string into an unsigned 64 bit integer. */ NOINLINE_ bool Convert_TryParseUInt64(const String* str, uint64_t* value); +/* Attempts to convert the given string into a floating point number. */ NOINLINE_ bool Convert_TryParseFloat(const String* str, float* value); +/* Attempts to convert the given string into a bool. */ +/* NOTE: String must case-insensitively equal "true" or "false" */ NOINLINE_ bool Convert_TryParseBool(const String* str, bool* value); #define STRINGSBUFFER_BUFFER_DEF_SIZE 4096 @@ -106,15 +170,24 @@ typedef struct StringsBuffer_ { uint32_t _DefaultFlags[STRINGSBUFFER_FLAGS_DEF_ELEMS]; } StringsBuffer; -NOINLINE_ void StringsBuffer_Init(StringsBuffer* buffer); +/* Resets counts to 0, and frees any allocated memory. */ NOINLINE_ void StringsBuffer_Clear(StringsBuffer* buffer); +/* Copies the characters from the i'th string in the given buffer into the given string. */ NOINLINE_ void StringsBuffer_Get(StringsBuffer* buffer, int i, String* text); +/* UNSAFE: Returns a direct pointer to the i'th string in the given buffer. */ NOINLINE_ STRING_REF String StringsBuffer_UNSAFE_Get(StringsBuffer* buffer, int i); +/* Adds a given string to the end of the given buffer. */ NOINLINE_ void StringsBuffer_Add(StringsBuffer* buffer, const String* text); +/* Removes the i'th string from the given buffer, shifting following strings downwards. */ NOINLINE_ void StringsBuffer_Remove(StringsBuffer* buffer, int index); +/* Performs line wrapping on the given string. */ +/* e.g. "some random tex|t* (| is lineLen) becomes "some random" "text" */ NOINLINE_ void WordWrap_Do(STRING_REF String* text, String* lines, int numLines, int lineLen); +/* Calculates the position of a raw index in the wrapped lines. */ NOINLINE_ void WordWrap_GetCoords(int index, const String* lines, int numLines, int* coordX, int* coordY); +/* Returns number of characters from current character to end of previous word. */ NOINLINE_ int WordWrap_GetBackLength(const String* text, int index); +/* Returns number of characters from current character to start of next word. */ NOINLINE_ int WordWrap_GetForwardLength(const String* text, int index); #endif diff --git a/src/Vectors.h b/src/Vectors.h index 74a67c2cd..63fc34c62 100644 --- a/src/Vectors.h +++ b/src/Vectors.h @@ -59,7 +59,7 @@ void Vector3I_ToVector3(Vector3* result, Vector3I* a); void Vector3I_Min(Vector3I* result, Vector3I* a, Vector3I* b); void Vector3I_Max(Vector3I* result, Vector3I* a, Vector3I* b); -/* Returns a normalised vector that faces in the direction described by the given yaw and pitch. */ +/* Returns a normalised vector facing in the direction described by the given yaw and pitch. */ Vector3 Vector3_GetDirVector(float yawRad, float pitchRad); /* Returns the yaw and pitch of the given direction vector. NOTE: This is not an identity function. Returned pitch is always within [-90, 90] degrees.*/