Skip to content

Commit

Permalink
Port InventoryScreen to C.
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Nov 28, 2017
1 parent 6812ba9 commit 71a2171
Show file tree
Hide file tree
Showing 14 changed files with 213 additions and 80 deletions.
5 changes: 2 additions & 3 deletions ClassicalSharp/Rendering/HeldBlockRenderer.cs
Expand Up @@ -222,9 +222,8 @@ public class HeldBlockRenderer : IGameComponent {
}
}

/// <summary> Skeleton implementation of a player entity so we can reuse
/// block model rendering code. </summary>
internal class FakePlayer : Player {
/// <summary> Skeleton implementation of player entity so we can reuse block model rendering code. </summary>
class FakePlayer : Player {

public FakePlayer(Game game) : base(game) {
NoShade = true;
Expand Down
1 change: 0 additions & 1 deletion src/Client/Client.vcxproj
Expand Up @@ -295,7 +295,6 @@
<ClCompile Include="OpenGLApi.c" />
<ClCompile Include="Options.c" />
<ClCompile Include="PackedCol.c" />
<ClCompile Include="Funcs.c" />
<ClCompile Include="GraphicsCommon.c" />
<ClCompile Include="Matrix.c" />
<ClCompile Include="Noise.c" />
Expand Down
3 changes: 0 additions & 3 deletions src/Client/Client.vcxproj.filters
Expand Up @@ -419,9 +419,6 @@
<ClCompile Include="VertexStructs.c">
<Filter>Source Files\Math</Filter>
</ClCompile>
<ClCompile Include="Funcs.c">
<Filter>Source Files\Utils</Filter>
</ClCompile>
<ClCompile Include="PackedCol.c">
<Filter>Source Files\2D\Utils</Filter>
</ClCompile>
Expand Down
8 changes: 1 addition & 7 deletions src/Client/Entity.h
Expand Up @@ -29,24 +29,18 @@ typedef struct LocationUpdate_ {
Real32 RotX, RotY, RotZ, HeadX;
/* Whether this update includes an absolute or relative position. */
bool IncludesPosition;
/* Whether the positon is absolute, or relative to the last positionreceived from the server. */
/* Whether the positon is absolute, or relative to the last position received from the server. */
bool RelativePosition;
} LocationUpdate;

/* Clamps the given angle so it lies between [0, 360). */
Real32 LocationUpdate_Clamp(Real32 degrees);

/* Constructs a location update with values for every field.
You should generally prefer using the alternative constructors. */
void LocationUpdate_Construct(LocationUpdate* update, Real32 x, Real32 y, Real32 z,
Real32 rotX, Real32 rotY, Real32 rotZ, Real32 headX, bool incPos, bool relPos);
/* Constructs a location update that does not have any position or orientation information. */
void LocationUpdate_Empty(LocationUpdate* update);
/* Constructs a location update that only consists of orientation information. */
void LocationUpdate_MakeOri(LocationUpdate* update, Real32 rotY, Real32 headX);
/* Constructs a location update that only consists of position information. */
void LocationUpdate_MakePos(LocationUpdate* update, Vector3 pos, bool rel);
/* Constructs a location update that consists of position and orientation information. */
void LocationUpdate_MakePosAndOri(LocationUpdate* update, Vector3 pos, Real32 rotY, Real32 headX, bool rel);


Expand Down
10 changes: 0 additions & 10 deletions src/Client/Funcs.c

This file was deleted.

5 changes: 0 additions & 5 deletions src/Client/Funcs.h
Expand Up @@ -15,11 +15,6 @@
/* returns number of elements in given array. */
#define Array_NumElements(arr) (sizeof(arr) / sizeof(arr[0]))

/* returns whether character is uppercase letter */
bool Char_IsUpper(UInt8 c);
/* Converts uppercase letter to lowercase */
UInt8 Char_ToLower(UInt8 c);

#define QuickSort_Swap_Maybe()\
if (i <= j) {\
key = keys[i]; keys[i] = keys[j]; keys[j] = key;\
Expand Down
12 changes: 6 additions & 6 deletions src/Client/Gui.c
Expand Up @@ -36,12 +36,12 @@ void GuiElement_Reset(GuiElement* elem) {

void Screen_Reset(Screen* screen) {
GuiElement_Reset(&screen->Base);
screen->HandlesAllInput = false;
screen->BlocksWorld = false;
screen->HidesHUD = false;
screen->RenderHUDOver = false;
screen->OnResize = NULL;
screen->OnContextLost = NULL;
screen->HandlesAllInput = false;
screen->BlocksWorld = false;
screen->HidesHUD = false;
screen->RenderHUDOver = false;
screen->OnResize = NULL;
screen->OnContextLost = NULL;
screen->OnContextRecreated = NULL;
}

Expand Down
70 changes: 35 additions & 35 deletions src/Client/Hotkeys.c
Expand Up @@ -24,6 +24,41 @@ UInt8 Hotkeys_LWJGL[256] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};

void Hotkeys_QuickSort(Int32 left, Int32 right) {
HotkeyData* keys = HotkeysList; HotkeyData key;

while (left < right) {
Int32 i = left, j = right;
UInt8 pivot = keys[(i + j) / 2].Flags;

/* partition the list */
while (i <= j) {
while (pivot > keys[i].Flags) i++;
while (pivot < keys[j].Flags) j--;
QuickSort_Swap_Maybe();
}
/* recurse into the smaller subset */
QuickSort_Recurse(Hotkeys_QuickSort)
}
}

void Hotkeys_AddNewHotkey(Key baseKey, UInt8 flags, STRING_PURE String* text, bool more) {
HotkeyData hKey;
hKey.BaseKey = baseKey;
hKey.Flags = flags;
hKey.TextIndex = HotkeysText.Count;
hKey.StaysOpen = more;

if (HotkeysText.Count == HOTKEYS_MAX_COUNT) {
ErrorHandler_Fail("Cannot define more than 256 hotkeys");
}

HotkeysList[HotkeysText.Count] = hKey;
StringsBuffer_Add(&HotkeysText, text);
/* sort so that hotkeys with largest modifiers are first */
Hotkeys_QuickSort(0, HotkeysText.Count - 1);
}

void Hotkeys_Add(Key baseKey, UInt8 flags, STRING_PURE String* text, bool more) {
UInt32 i;
for (i = 0; i < HotkeysText.Count; i++) {
Expand Down Expand Up @@ -53,41 +88,6 @@ bool Hotkeys_Remove(Key baseKey, UInt8 flags) {
return false;
}

void Hotkeys_AddNewHotkey(Key baseKey, UInt8 flags, STRING_PURE String* text, bool more) {
HotkeyData hKey;
hKey.BaseKey = baseKey;
hKey.Flags = flags;
hKey.TextIndex = HotkeysText.Count;
hKey.StaysOpen = more;

if (HotkeysText.Count == HOTKEYS_MAX_COUNT) {
ErrorHandler_Fail("Cannot define more than 256 hotkeys");
}

HotkeysList[HotkeysText.Count] = hKey;
StringsBuffer_Add(&HotkeysText, text);
/* sort so that hotkeys with largest modifiers are first */
Hotkeys_QuickSort(0, HotkeysText.Count - 1);
}

void Hotkeys_QuickSort(Int32 left, Int32 right) {
HotkeyData* keys = HotkeysList; HotkeyData key;

while (left < right) {
Int32 i = left, j = right;
UInt8 pivot = keys[(i + j) / 2].Flags;

/* partition the list */
while (i <= j) {
while (pivot > keys[i].Flags) i++;
while (pivot < keys[j].Flags) j--;
QuickSort_Swap_Maybe();
}
/* recurse into the smaller subset */
QuickSort_Recurse(Hotkeys_QuickSort)
}
}

bool Hotkeys_IsHotkey(Key key, STRING_TRANSIENT String* text, bool* moreInput) {
UInt8 flags = 0;
if (Key_IsControlPressed()) flags |= 1;
Expand Down
4 changes: 4 additions & 0 deletions src/Client/Hotkeys.h
Expand Up @@ -3,6 +3,10 @@
#include "Typedefs.h"
#include "Input.h"
#include "String.h"
/* Maintains list of hotkeys defined by the client and SetTextHotkey packets.
Copyright 2017 ClassicalSharp | Licensed under BSD-3
*/


extern UInt8 Hotkeys_LWJGL[256];
typedef struct HotkeyData_ {
Expand Down
2 changes: 1 addition & 1 deletion src/Client/Input.c
Expand Up @@ -154,7 +154,7 @@ void KeyBind_Save(void) {
for (i = 0; i < KeyBind_Count; i++) {
KeyBind_MakeName(name);
String value = String_FromReadonly(Key_Names[i]);
Options_Set(name.buffer, value);
Options_Set(name.buffer, &value);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Client/ModelCache.c
Expand Up @@ -23,7 +23,7 @@ IModel* ModelCache_Get(STRING_PURE String* name) {
Int32 i;
for (i = 0; i < ModelCache_modelCount; i++) {
CachedModel* m = &ModelCache_Models[i];
if (!String_Equals(&m->Name, name)) continue;
if (!String_CaselessEquals(&m->Name, name)) continue;

if (!m->Instance->initalised) {
m->Instance->CreateParts();
Expand All @@ -38,7 +38,7 @@ Int32 ModelCache_GetTextureIndex(STRING_PURE String* texName) {
Int32 i;
for (i = 0; i < ModelCache_texCount; i++) {
CachedTexture* tex = &ModelCache_Textures[i];
if (String_Equals(&tex->Name, texName)) return 1;
if (String_CaselessEquals(&tex->Name, texName)) return 1;
}
return -1;
}
Expand Down
152 changes: 150 additions & 2 deletions src/Client/Screens.c
Expand Up @@ -3,6 +3,8 @@
#include "Game.h"
#include "Event.h"
#include "GraphicsCommon.h"
#include "Platform.h"
#include "Inventory.h"

void Screen_FreeWidgets(Widget** widgets, UInt32 widgetsCount) {
if (widgets == NULL) return;
Expand Down Expand Up @@ -96,5 +98,151 @@ void ClickableScreen_Create(ClickableScreen* screen) {
screen->OnWidgetSelected = ClickableScreen_DefaultWidgetSelected;
}

Screen AA;
extern Screen* InventoryScreen_Unsafe_RawPointer = &AA;
typedef struct InventoryScreen_ {
Screen Base;
FontDesc Font;
TableWidget Table;
bool ReleasedInv;
} InventoryScreen;
InventoryScreen InventoryScreen_Instance;

void InventoryScreen_OnBlockChanged(void) {
TableWidget_OnInventoryChanged(&InventoryScreen_Instance.Table);
}

void InventoryScreen_ContextLost(void) {
GuiElement* elem = &InventoryScreen_Instance.Table.Base.Base;
elem->Free(elem);
}

void InventoryScreen_ContextRecreated(void) {
GuiElement* elem = &InventoryScreen_Instance.Table.Base.Base;
elem->Recreate(elem);
}

void InventoryScreen_Init(GuiElement* elem) {
InventoryScreen* screen = (InventoryScreen*)elem;
screen->Font.Size = 16;
Platform_MakeFont(&screen->Font, &Game_FontName);
elem = &screen->Table.Base.Base;
TableWidget_Create(&screen->Table);
screen->Table.Font = screen->Font;
screen->Table.ElementsPerRow = Game_PureClassic ? 9 : 10;
elem->Init(&elem);

Key_KeyRepeat = true;
Event_RegisterVoid(&BlockEvents_PermissionsChanged, InventoryScreen_OnBlockChanged);
Event_RegisterVoid(&BlockEvents_BlockDefChanged, InventoryScreen_OnBlockChanged);
Event_RegisterVoid(&GfxEvents_ContextLost, InventoryScreen_ContextLost);
Event_RegisterVoid(&GfxEvents_ContextRecreated, InventoryScreen_ContextRecreated);
}

void InventoryScreen_Render(GuiElement* elem, Real64 delta) {
InventoryScreen* screen = (InventoryScreen*)elem;
elem = &screen->Table.Base.Base;
elem->Render(elem, delta);
}

void InventoryScreen_OnResize(Screen* elem) {
InventoryScreen* screen = (InventoryScreen*)elem;
Widget* widget = &screen->Table.Base;
widget->Reposition(widget);
}

void InventoryScreen_Free(GuiElement* elem) {
InventoryScreen* screen = (InventoryScreen*)elem;
Platform_FreeFont(&screen->Font);
elem = &screen->Table.Base.Base;
elem->Free(&elem);

Key_KeyRepeat = false;
Event_UnregisterVoid(&BlockEvents_PermissionsChanged, InventoryScreen_OnBlockChanged);
Event_UnregisterVoid(&BlockEvents_BlockDefChanged, InventoryScreen_OnBlockChanged);
Event_UnregisterVoid(&GfxEvents_ContextLost, InventoryScreen_ContextLost);
Event_UnregisterVoid(&GfxEvents_ContextRecreated, InventoryScreen_ContextRecreated);
}

bool InventoryScreen_HandlesKeyDown(GuiElement* elem, Key key) {
InventoryScreen* screen = (InventoryScreen*)elem;
TableWidget* table = &screen->Table;
GuiElement* elem = &screen->Table.Base.Base;
if (key == KeyBind_Get(KeyBind_PauseOrExit)) {
gGui_SetNewScreen(NULL);
} else if (key == KeyBind_Get(KeyBind_Inventory) && screen->ReleasedInv) {
Gui_SetNewScreen(NULL);
} else if (key == Key_Enter && table->SelectedIndex != -1) {
Inventory_SetSelectedBlock(table->Elements[table->SelectedIndex]);
Gui_SetNewScreen(NULL);
} else if (elem->HandlesKeyDown(elem, key)) {
} else {
game.Gui.hudScreen.hotbar.HandlesKeyDown(key);
}
return true;
}

bool InventoryScreen_HandlesKeyUp(GuiElement* elem, Key key) {
InventoryScreen* screen = (InventoryScreen*)elem;
if (key == KeyBind_Get(KeyBind_Inventory)) {
screen->ReleasedInv = true; return true;
}
return game.Gui.hudScreen.hotbar.HandlesKeyUp(key);
}

bool InventoryScreen_HandlesMouseDown(GuiElement* elem, Int32 x, Int32 y, MouseButton btn) {
InventoryScreen* screen = (InventoryScreen*)elem;
TableWidget* table = &screen->Table;
GuiElement* elem = &screen->Table.Base.Base;
if (table->Scroll.DraggingMouse || game.Gui.hudScreen.hotbar.HandlesMouseDown(x, y, btn))
return true;

bool handled = elem->HandlesMouseDown(elem, x, y, btn);
if ((!handled || table->PendingClose) && btn == MouseButton_Left) {
bool hotbar = Key_IsControlPressed();
if (!hotbar) Gui_SetNewScreen(NULL);
}
return true;
}

bool InventoryScreen_HandlesMouseUp(GuiElement* elem, Int32 x, Int32 y, MouseButton btn) {
InventoryScreen* screen = (InventoryScreen*)elem;
GuiElement* elem = &screen->Table.Base.Base;
return elem->HandlesMouseUp(elem, x, y, btn);
}

bool InventoryScreen_HandlesMouseMove(GuiElement* elem, Int32 x, Int32 y) {
InventoryScreen* screen = (InventoryScreen*)elem;
GuiElement* elem = &screen->Table.Base.Base;
return elem->HandlesMouseMove(elem, x, y);
}

bool InventoryScreen_HandlesMouseScroll(GuiElement* elem, Real32 delta) {
InventoryScreen* screen = (InventoryScreen*)elem;
GuiElement* elem = &screen->Table.Base.Base;

bool hotbar = Key_IsAltPressed() || Key_IsControlPressed() || Key_IsShiftPressed();
if (hotbar) return false;
return elem->HandlesMouseScroll(elem, delta);
}

Screen* InventoryScreen_GetInstance(void) {
InventoryScreen* screen = &InventoryScreen_Instance;
Platform_MemSet(&screen, 0, sizeof(InventoryScreen));
Screen_Reset(&screen->Base);

screen->Base.Base.HandlesKeyDown = InventoryScreen_HandlesKeyDown;
screen->Base.Base.HandlesKeyUp = InventoryScreen_HandlesKeyUp;
screen->Base.Base.HandlesMouseDown = InventoryScreen_HandlesMouseDown;
screen->Base.Base.HandlesMouseUp = InventoryScreen_HandlesMouseUp;
screen->Base.Base.HandlesMouseMove = InventoryScreen_HandlesMouseMove;
screen->Base.Base.HandlesMouseScroll = InventoryScreen_HandlesMouseScroll;

screen->Base.OnContextLost = InventoryScreen_ContextLost;
screen->Base.OnContextRecreated = InventoryScreen_ContextRecreated;
screen->Base.OnResize = InventoryScreen_OnResize;
screen->Base.Base.Init = InventoryScreen_Init;
screen->Base.Base.Render = InventoryScreen_Render;
screen->Base.Base.Free = InventoryScreen_Free;

return &screen->Base;
}
extern Screen* InventoryScreen_Unsafe_RawPointer = &InventoryScreen_Instance.Base;

0 comments on commit 71a2171

Please sign in to comment.