Permalink
Browse files

Start porting GUI code to C.

  • Loading branch information...
UnknownShadow200 committed Oct 10, 2017
1 parent 057a733 commit 4171ae4e37dfd7e62904094fcf1969112b2ae9fb
Showing with 328 additions and 35 deletions.
  1. +8 −9 src/Client/Camera.c
  2. +1 −2 src/Client/D3D9Api.c
  3. +8 −8 src/Client/Event.c
  4. +7 −7 src/Client/Event.h
  5. +2 −0 src/Client/Game.h
  6. +0 −6 src/Client/GameStructs.h
  7. +181 −0 src/Client/Gui.c
  8. +117 −0 src/Client/Gui.h
  9. +1 −2 src/Client/OpenGLApi.c
  10. +3 −1 src/Client/Stream.c
View
@@ -5,6 +5,7 @@
#include "GraphicsAPI.h"
#include "Player.h"
#include "Funcs.h"
#include "Gui.h"
Real32 Camera_AdjustHeadX(Real32 value) {
if (value >= 90.00f && value <= 90.10f) return 90.10f * MATH_DEG2RAD;
@@ -23,8 +24,7 @@ Vector3 PerspectiveCamera_GetDirVector(void) {
void PerspectiveCamera_GetProjection(Matrix* proj) {
Real32 fovy = Game_Fov * MATH_DEG2RAD;
Size2D size = Window_GetClientSize();
Real32 aspectRatio = (Real32)size.Width / (Real32)size.Height;
Real32 aspectRatio = (Real32)Game_Width / (Real32)Game_Height;
Matrix_PerspectiveFieldOfView(proj, fovy, aspectRatio, Gfx_MinZNear, Game_ViewDistance);
}
@@ -43,9 +43,8 @@ void PerspectiveCamera_CentreMousePosition(void) {
delta = Point2D_Make(current.X - previous.X, current.Y - previous.Y);
Point2D topLeft = Window_PointToScreen(Point2D_Empty);
Size2D size = Window_GetClientSize();
Int32 cenX = topLeft.X + size.Width / 2;
Int32 cenY = topLeft.Y + size.Height / 2;
Int32 cenX = topLeft.X + Game_Width / 2;
Int32 cenY = topLeft.Y + Game_Height / 2;
Window_SetDesktopCursorPos(Point2D_Make(cenX, cenY));
/* Fixes issues with large DPI displays on Windows >= 8.0. */
@@ -56,9 +55,8 @@ void PerspectiveCamera_RegrabMouse(void) {
if (!Window_GetExists()) return;
Point2D topLeft = Window_PointToScreen(Point2D_Empty);
Size2D size = Window_GetClientSize();
Int32 cenX = topLeft.X + size.Width / 2;
Int32 cenY = topLeft.Y + size.Height / 2;
Int32 cenX = topLeft.X + Game_Width / 2;
Int32 cenY = topLeft.Y + Game_Height / 2;
Point2D point = Point2D_Make(cenX, cenY);
Window_SetDesktopCursorPos(point);
@@ -101,7 +99,8 @@ void PerspectiveCamera_UpdateMouseRotation(void) {
}
void PerspectiveCamera_UpdateMouse(void) {
/* if (Gui.ActiveScreen.HandlesAllInput) return; TODO: NEED TO IMPLEMENT GUI CODE FIRSTLY */
Screen* screen = Gui_GetActiveScreen();
if (screen->HandlesAllInput) return;
PerspectiveCamera_CentreMousePosition();
PerspectiveCamera_UpdateMouseRotation();
}
View
@@ -191,8 +191,7 @@ void D3D9_GetPresentArgs(Int32 width, Int32 height, D3DPRESENT_PARAMETERS* args)
void D3D9_RecreateDevice(void) {
D3DPRESENT_PARAMETERS args;
Size2D size = Window_GetClientSize();
D3D9_GetPresentArgs(size.Width, size.Height, &args);
D3D9_GetPresentArgs(Game_Width, Game_Height, &args);
while (IDirect3DDevice9_Reset(device, &args) == D3DERR_DEVICELOST) {
D3D9_LoopUntilRetrieved();
View
@@ -2,7 +2,7 @@
#include "ErrorHandler.h"
void Event_RegisterImpl(Event_Void* handlers, Event_Void_Callback handler) {
Int32 i;
UInt32 i;
for (i = 0; i < handlers->Count; i++) {
if (handlers->Handlers[i] == handler) return;
}
@@ -16,7 +16,7 @@ void Event_RegisterImpl(Event_Void* handlers, Event_Void_Callback handler) {
}
void Event_UnregisterImpl(Event_Void* handlers, Event_Void_Callback handler) {
Int32 i, j;
UInt32 i, j;
for (i = 0; i < handlers->Count; i++) {
if (handlers->Handlers[i] != handler) continue;
@@ -33,7 +33,7 @@ void Event_UnregisterImpl(Event_Void* handlers, Event_Void_Callback handler) {
}
void Event_RaiseVoid(Event_Void* handlers) {
Int32 i;
UInt32 i;
for (i = 0; i < handlers->Count; i++) {
handlers->Handlers[i]();
}
@@ -46,7 +46,7 @@ void Event_UnregisterVoid(Event_Void* handlers, Event_Void_Callback handler) {
}
void Event_RaiseInt32(Event_Int32* handlers, Int32 arg) {
Int32 i;
UInt32 i;
for (i = 0; i < handlers->Count; i++) {
handlers->Handlers[i](arg);
}
@@ -59,7 +59,7 @@ void Event_UnregisterInt32(Event_Int32* handlers, Event_Int32_Callback handler)
}
void Event_RaiseReal32(Event_Real32* handlers, Real32 arg) {
Int32 i;
UInt32 i;
for (i = 0; i < handlers->Count; i++) {
handlers->Handlers[i](arg);
}
@@ -73,7 +73,7 @@ void Event_UnregisterReal32(Event_Real32* handlers, Event_Real32_Callback handle
}
void Event_RaiseEntityID(Event_EntityID* handlers, EntityID arg) {
Int32 i;
UInt32 i;
for (i = 0; i < handlers->Count; i++) {
handlers->Handlers[i](arg);
}
@@ -99,7 +99,7 @@ void Event_UnregisterStream(Event_Stream* handlers, Event_Stream_Callback handle
}
void Event_RaiseBlock(Event_Block* handlers, Vector3I coords, BlockID oldBlock, BlockID block) {
Int32 i;
UInt32 i;
for (i = 0; i < handlers->Count; i++) {
handlers->Handlers[i](coords, oldBlock, block);
}
@@ -112,7 +112,7 @@ void Event_UnregisterBlock(Event_Block* handlers, Event_Block_Callback handler)
}
void Event_RaiseMouseMove(Event_MouseMove* handlers, Int32 xDelta, Int32 yDelta) {
Int32 i;
UInt32 i;
for (i = 0; i < handlers->Count; i++) {
handlers->Handlers[i](xDelta, yDelta);
}
View
@@ -13,37 +13,37 @@
typedef void(*Event_Void_Callback)(void);
typedef struct Event_Void_ {
Event_Void_Callback Handlers[Event_MaxCallbacks]; Int32 Count;
Event_Void_Callback Handlers[Event_MaxCallbacks]; UInt32 Count;
} Event_Void;
typedef void(*Event_Int32_Callback)(Int32 argument);
typedef struct Event_Int32_ {
Event_Int32_Callback Handlers[Event_MaxCallbacks]; Int32 Count;
Event_Int32_Callback Handlers[Event_MaxCallbacks]; UInt32 Count;
} Event_Int32;
typedef void(*Event_Real32_Callback)(Real32 argument);
typedef struct Event_Real32_ {
Event_Real32_Callback Handlers[Event_MaxCallbacks]; Int32 Count;
Event_Real32_Callback Handlers[Event_MaxCallbacks]; UInt32 Count;
} Event_Real32;
typedef void(*Event_EntityID_Callback)(EntityID argument);
typedef struct Event_EntityID_ {
Event_EntityID_Callback Handlers[Event_MaxCallbacks]; Int32 Count;
Event_EntityID_Callback Handlers[Event_MaxCallbacks]; UInt32 Count;
} Event_EntityID;
typedef void(*Event_Stream_Callback)(Stream* stream);
typedef struct Event_Stream_ {
Event_Stream_Callback Handlers[Event_MaxCallbacks]; Int32 Count;
Event_Stream_Callback Handlers[Event_MaxCallbacks]; UInt32 Count;
} Event_Stream;
typedef void(*Event_Block_Callback)(Vector3I coords, BlockID oldBlock, BlockID block);
typedef struct Event_Block_ {
Event_Block_Callback Handlers[Event_MaxCallbacks]; Int32 Count;
Event_Block_Callback Handlers[Event_MaxCallbacks]; UInt32 Count;
} Event_Block;
typedef void(*Event_MouseMove_Callback)(Int32 xDelta, Int32 yDelta);
typedef struct Event_MouseMove_ {
Event_MouseMove_Callback Handlers[Event_MaxCallbacks]; Int32 Count;
Event_MouseMove_Callback Handlers[Event_MaxCallbacks]; UInt32 Count;
} Event_MouseMove;
View
@@ -9,6 +9,8 @@
Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3
*/
/* Width and height of game window */
Int32 Game_Width, Game_Height;
/* Total rendering time(in seconds) elapsed since the client was started. */
Real64 Game_Accumulator;
/* Accumulator for the number of chunk updates performed. Reset every second. */
View
@@ -10,22 +10,16 @@ typedef void (*Void_Callback)(void);
/* Represents a game component. */
typedef struct IGameComponent_ {
/* Called when the game is being loaded. */
Void_Callback Init;
/* Called when the component is being freed, due to game being closed. */
Void_Callback Free;
/* Called when the texture pack has been loaded and all components have been initalised. */
Void_Callback Ready;
/* Called to reset the component's state when the user is reconnecting to a server */
Void_Callback Reset;
/* Called to update the component's state when the user begins loading a new map. */
Void_Callback OnNewMap;
/* Called to update the component's state when the user has finished loading a new map. */
Void_Callback OnNewMapLoaded;
} IGameComponent;
View
@@ -0,0 +1,181 @@
#include "Gui.h"
#include "Window.h"
#include "Game.h"
#include "GraphicsCommon.h"
#include "GraphicsAPI.h"
#include "Events.h"
void GuiElement_Fail(void) {
}
bool GuiElement_ReturnFalse(void) {
return false;
}
void GuiElement_Init(GuiElement* elem) {
/* Initalises state of this GUI element */
Gui_Void Init;
/* Draws this gui element on screen */
Gui_Render Render;
/* Frees the state of this GUI element */
Gui_Void Free;
/* Recreates all sub-elements and/or textures. (e.g. for when bitmap font changes) */
Gui_Void Recreate;
/* Returns whether this GUI element handles a key being pressed. */
Gui_KeyHandler HandlesKeyDown;
/* Returns whether this GUI element handles a key being released. */
Gui_KeyHandler HandlesKeyUp;
/* Returns whether this GUI element handles a character being input */
Gui_KeyPress HandlesKeyPress;
/* Returns whether this GUI element handles a mouse button being pressed. */
Gui_MouseHandler HandlesMouseDown;
/* Returns whether this GUI element handles a mouse button being released. */
Gui_MouseHandler HandlesMouseUp;
/* Returns whether this GUI element handles the mouse being moved. */
Gui_MouseMove HandlesMouseMove;
/* Returns whether this GUI element handles the mouse being scrolled. */
Gui_MouseScroll HandlesMouseScroll;
}
void Screen_Init(Screen* screen) {
GuiElement_Init(&screen->Base);
screen->HandlesAllInput = false;
screen->BlocksWorld = false;
screen->HidesHUD = false;
screen->RenderHUDOver = false;
screen->
}
void Widget_Init(Widget* widget) {
}
Int32 Gui_CalcPos(Anchor anchor, Int32 offset, Int32 size, Int32 axisLen) {
if (anchor == ANCHOR_LEFT_OR_TOP) return offset;
if (anchor == ANCHOR_BOTTOM_OR_RIGHT) return axisLen - size - offset;
return (axisLen - size) / 2 + offset;
}
bool Gui_Contains(Int32 recX, Int32 recY, Int32 width, Int32 height, Int32 x, Int32 y) {
return x >= recX && y >= recY && x < recX + width && y < recY + height;
}
void Gui_FileChanged(Stream* stream) {
String gui = String_FromConstant("gui.png");
String guiClassic = String_FromConstant("gui_classic.png");
String icons = String_FromConstant("icons.png");
if (String_Equals(&stream->Name, &gui)) {
Game_UpdateTexture(&Gui_GuiTex, stream, false);
} else if (String_Equals(&stream->Name, &guiClassic)) {
Game_UpdateTexture(&Gui_GuiClassicTex, stream, false);
} else if (String_Equals(&stream->Name, &icons)) {
Game_UpdateTexture(&Gui_IconsTex, stream, false);
}
}
void Gui_Init() {
Event_RegisterStream(&TextureEvents_FileChanged, Gui_FileChanged);
}
void Gui_Reset(void) {
UInt32 i;
for (i = 0; i < Gui_OverlayCount; i++) {
overlays[i].Dispose();
Gui_Overlays[i]->Base.Free(Gui_Overlays[i]);
}
Gui_OverlayCount = 0;
}
void Gui_Free(void) {
Event_UnregisterStream(&TextureEvents_FileChanged, Gui_FileChanged);
Gui_SetScreen(NULL);
statusScreen.Dispose();
if (Gui_Active != null) {
Gui_Active->Base.Free(Gui_Active);
}
Gfx_DeleteTexture(&Gui_GuiTex);
Gfx_DeleteTexture(&Gui_GuiClassicTex);
Gfx_DeleteTexture(&Gui_IconsTex);
Gui_Reset();
}
IGameComponent Gui_MakeGameComponent(void) {
IGameComponent comp = IGameComponent_MakeEmpty();
comp.Init = Gui_Init;
comp.Reset = Gui_Reset;
comp.Free = Gui_Free;
return comp;
}
Screen* Gui_GetActiveScreen(void) {
return Gui_OverlayCount > 0 ? Gui_Overlays[0] : Gui_GetUnderlyingScreen();
}
Screen* Gui_GetUnderlyingScreen(void) {
return Gui_Active == NULL ? Gui_HUD : Gui_Active;
}
void Gui_SetScreen(Screen* screen) {
game.Input.ScreenChanged(activeScreen, screen);
if (activeScreen != NULL && disposeOld)
activeScreen.Dispose();
if (screen == NULL) {
hudScreen.GainFocus();
} else if (activeScreen == null) {
hudScreen.LoseFocus();
}
if (screen != null)
screen.Init();
activeScreen = screen;
}
void Gui_RefreshHud(void) {
Gui_HUD->Base.Recreate(Gui_HUD);
}
void Gui_ShowOverlay(Screen* overlay)
bool cursorVis = game.CursorVisible;
if (overlays.Count == 0) game.CursorVisible = true;
overlays.Add(overlay);
if (overlays.Count == 1) game.CursorVisible = cursorVis;
// Save cursor visibility state
overlay.Init();
}
void Gui_RenderGui(Real64 delta) {
GfxCommon_Mode2D(Game_Width, Game_Height);
if (Gui_Active == NULL || !Gui_Active->HidesHUD) {
statusScreen.Render(delta)
}
if (Gui_Active == NULL || !Gui_Active->HidesHUD && !Gui_Active->RenderHUDOver) {
Gui_HUD->Base.Render(Gui_HUD, delta);
}
if (Gui_Active != NULL) {
Gui_Active->Base.Render(Gui_Active, delta);
}
if (Gui_Active != NULL && !Gui_Active->HidesHUD && Gui_Active->RenderHUDOver) {
Gui_HUD->Base.Render(Gui_HUD, delta);
}
if (Gui_OverlayCount > 0) {
Gui_Overlays[0]->Base.Render(Gui_Overlays[0], delta);
}
GfxCommon_Mode3D();
}
void Gui_OnResize(void) {
if (Gui_Active != NULL) {
Gui_Active->OnResize(Gui_Active);
}
Gui_HUD->OnResize(Gui_HUD);
UInt32 i;
for (i = 0; i < Gui_OverlayCount; i++) {
Gui_Overlays[i]->OnResize(Gui_Overlays[i]);
}
}
Oops, something went wrong.

0 comments on commit 4171ae4

Please sign in to comment.