From 985f76fd6b8241b4e1866248741cc4b428cf07a8 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Tue, 15 Aug 2017 22:49:01 +1000 Subject: [PATCH] Port KeyboardDevice and Key to C. --- OpenTK/WindowState.cs | 50 ++++++++--------- src/Client/Block.c | 6 +-- src/Client/Block.h | 2 - src/Client/Client.vcxproj | 3 ++ src/Client/Client.vcxproj.filters | 9 ++++ src/Client/DefaultSet.c | 6 +-- src/Client/DefaultSet.h | 1 - src/Client/Event.c | 4 +- src/Client/Event.h | 12 ++--- src/Client/Events.h | 9 +++- src/Client/Key.c | 24 +++++++++ src/Client/Key.h | 90 +++++++++++++++++++++++++++++++ src/Client/TerrainAtlas2D.c | 4 +- src/Client/TerrainAtlas2D.h | 2 - src/Client/Window.h | 29 ++++++---- 15 files changed, 191 insertions(+), 60 deletions(-) create mode 100644 src/Client/Key.c create mode 100644 src/Client/Key.h diff --git a/OpenTK/WindowState.cs b/OpenTK/WindowState.cs index 7c1f57866..ea520828c 100644 --- a/OpenTK/WindowState.cs +++ b/OpenTK/WindowState.cs @@ -1,25 +1,25 @@ -#region --- License --- -/* Licensed under the MIT/X11 license. - * Copyright (c) 2006-2008 the OpenTK Team. - * This notice may not be removed from any source distribution. - * See license.txt for licensing details. - */ -#endregion - -using System; - -namespace OpenTK { - - /// Enumerates available window states. - public enum WindowState { - - /// The window is in its normal state. - Normal = 0, - /// The window is minimized to the taskbar (also known as 'iconified'). - Minimized, - /// The window covers the whole working area, which includes the desktop but not the taskbar and/or panels. - Maximized, - /// The window covers the whole screen, including all taskbars and/or panels. - Fullscreen, - } -} +#region --- License --- +/* Licensed under the MIT/X11 license. + * Copyright (c) 2006-2008 the OpenTK Team. + * This notice may not be removed from any source distribution. + * See license.txt for licensing details. + */ +#endregion + +using System; + +namespace OpenTK { + + /// Enumerates available window states. + public enum WindowState { + + /// The window is in its normal state. + Normal = 0, + /// The window is minimized to the taskbar (also known as 'iconified'). + Minimized, + /// The window covers the whole working area, which includes the desktop but not the taskbar and/or panels. + Maximized, + /// The window covers the whole screen, including all taskbars and/or panels. + Fullscreen, + } +} diff --git a/src/Client/Block.c b/src/Client/Block.c index 737d011a4..23b1100b2 100644 --- a/src/Client/Block.c +++ b/src/Client/Block.c @@ -1,8 +1,7 @@ -#if 0 +#include "Block.h" #include "DefaultSet.h" #include "Funcs.h" #include "ExtMath.h" -#include "Block.h" #include "TerrainAtlas2D.h" void Block_Reset(void) { @@ -426,5 +425,4 @@ void Block_SetXStretch(BlockID block, bool stretch) { void Block_SetZStretch(BlockID block, bool stretch) { Block_CanStretch[block] &= 0xFC; /* ~0x03 */ Block_CanStretch[block] |= (stretch ? 0x03 : (UInt8)0); -} -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/src/Client/Block.h b/src/Client/Block.h index b5047f375..fb4629224 100644 --- a/src/Client/Block.h +++ b/src/Client/Block.h @@ -1,4 +1,3 @@ -#if 0 #ifndef CS_BLOCK_H #define CS_BLOCK_H #include "Typedefs.h" @@ -277,5 +276,4 @@ static TextureLoc bottomTex[Block_CpeCount] = { 0, 1, 2, 2, 16, 4, 15, 17, 1 72, 73, 74, 75, 76, 77, 78, 79, 13, 12, 29, 28, 56, 55, 6, 6, 7, 10, 4, 36, 37, 16, 11, 57, 50, 38, 80, 81, 82, 83, 84, 51, 54, 86, 58, 53, 52 }; -#endif #endif \ No newline at end of file diff --git a/src/Client/Client.vcxproj b/src/Client/Client.vcxproj index 77a7c7243..00c540e9e 100644 --- a/src/Client/Client.vcxproj +++ b/src/Client/Client.vcxproj @@ -212,6 +212,7 @@ + @@ -249,6 +250,7 @@ + @@ -282,6 +284,7 @@ + diff --git a/src/Client/Client.vcxproj.filters b/src/Client/Client.vcxproj.filters index 87de7877d..a445d958b 100644 --- a/src/Client/Client.vcxproj.filters +++ b/src/Client/Client.vcxproj.filters @@ -405,6 +405,12 @@ Header Files\Events + + Header Files\Platform\Window + + + Header Files\Platform\Window + @@ -620,5 +626,8 @@ Source Files\Events + + Source Files\Platform\Window + \ No newline at end of file diff --git a/src/Client/DefaultSet.c b/src/Client/DefaultSet.c index 29290d827..f08721c19 100644 --- a/src/Client/DefaultSet.c +++ b/src/Client/DefaultSet.c @@ -1,7 +1,6 @@ -#if 0 +#include "DefaultSet.h" #include "BlockID.h" #include "Block.h" -#include "DefaultSet.h" Real32 DefaultSet_Height(BlockID b) { if (b == BlockID_Slab) return 0.5f; @@ -111,5 +110,4 @@ SoundType DefaultSet_DigSound(BlockID b) { if (b >= BlockID_Stone && b <= BlockID_StoneBrick) return SoundType_Stone; return SoundType_None; -} -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/src/Client/DefaultSet.h b/src/Client/DefaultSet.h index dcdf4859d..9f77e493e 100644 --- a/src/Client/DefaultSet.h +++ b/src/Client/DefaultSet.h @@ -1,4 +1,3 @@ -#if 0 #ifndef CS_DEFAULT_BLOCKS_H #define CS_DEFAULT_BLOCKS_H #include "Typedefs.h" diff --git a/src/Client/Event.c b/src/Client/Event.c index 6802c4344..71cd07ef4 100644 --- a/src/Client/Event.c +++ b/src/Client/Event.c @@ -41,7 +41,7 @@ void Event_RaiseVoid(Event_Void* handlers) { void Event_RegisterVoid(Event_Void* handlers, Event_Void_Callback handler) { Event_RegisterImpl(handlers, handler); } -void Event_UnregisterVoid(&Event_Void* handlers, Event_Void_Callback handler) { +void Event_UnregisterVoid(Event_Void* handlers, Event_Void_Callback handler) { Event_UnregisterImpl(handlers, handler); } @@ -54,7 +54,7 @@ void Event_RaiseInt32(Event_Int32* handlers, Int32 arg) { void Event_RegisterInt32(Event_Int32* handlers,Event_Int32_Callback handler) { Event_RegisterImpl((Event_Void*)handlers, (Event_Void_Callback)handler); } -void Event_UnregisterInt32Impl(Event_Int32* handlers, Event_Int32_Callback handler) { +void Event_UnregisterInt32(Event_Int32* handlers, Event_Int32_Callback handler) { Event_UnregisterImpl((Event_Void*)handlers, (Event_Void_Callback)handler); } diff --git a/src/Client/Event.h b/src/Client/Event.h index 5ed22058d..f8d76e01b 100644 --- a/src/Client/Event.h +++ b/src/Client/Event.h @@ -14,37 +14,37 @@ /* Event that takes no arguments. */ typedef void(*Event_Void_Callback)(void); typedef struct Event_Void_ { - Event_Void_Callback Handlers[Event_MaxCallbacks]; Int32 HandlersCount; + Event_Void_Callback Handlers[Event_MaxCallbacks]; Int32 Count; } Event_Void; /* Event that takes single 32 bit signed integer argument. */ typedef void(*Event_Int32_Callback)(Int32 argument); typedef struct Event_Int32_ { - Event_Int32_Callback Handlers[Event_MaxCallbacks]; Int32 HandlersCount; + Event_Int32_Callback Handlers[Event_MaxCallbacks]; Int32 Count; } Event_Int32; /* Event handler that takes single floating-point argument. */ typedef void(*Event_Real32_Callback)(Real32 argument); typedef struct Event_Real32_ { - Event_Real32_Callback Handlers[Event_MaxCallbacks]; Int32 HandlersCount; + Event_Real32_Callback Handlers[Event_MaxCallbacks]; Int32 Count; } Event_Real32; /* Event handler that takes an entity ID as an argument. */ typedef void(*Event_EntityID_Callback)(EntityID argument); typedef struct Event_EntityID_ { - Event_EntityID_Callback Handlers[Event_MaxCallbacks]; Int32 HandlersCount; + Event_EntityID_Callback Handlers[Event_MaxCallbacks]; Int32 Count; } Event_EntityID; /* Event handler that takes stream as an argument. */ typedef void(*Event_Stream_Callback)(Stream* stream); typedef struct Event_Stream_ { - Event_Stream_Callback Handlers[Event_MaxCallbacks]; Int32 HandlersCount; + Event_Stream_Callback Handlers[Event_MaxCallbacks]; Int32 Count; } Event_Stream; /* Event handler that takes a block change argument. */ typedef void(*Event_Block_Callback)(Vector3I coords, BlockID oldBlock, BlockID block); typedef struct Event_Block_ { - Event_Block_Callback Handlers[Event_MaxCallbacks]; Int32 HandlersCount; + Event_Block_Callback Handlers[Event_MaxCallbacks]; Int32 Count; } Event_Block; diff --git a/src/Client/Events.h b/src/Client/Events.h index ca17fbab0..526a84347 100644 --- a/src/Client/Events.h +++ b/src/Client/Events.h @@ -111,10 +111,15 @@ Event_Void WindowEvents_OnVisibleChanged; Event_Void WindowEvents_OnFocusedChanged; /* Raised when the WindowState of the window changes. */ Event_Void WindowEvents_OnWindowStateChanged; -/// Occurs whenever a character is typed. -event EventHandler KeyPress; /* Raised whenever the mouse cursor leaves the bounds of the window. */ Event_Void WindowEvents_OnMouseLeave; /* Raised whenever the mouse cursor enters the bounds of the window. */ Event_Void WindowEvents_OnMouseEnter; + +/* Raised when a character is typed. */ +Event_Int32 KeyEvents_KeyPress; +/* Raised when a key is pressed. */ +Event_Int32 KeyEvents_KeyDown; +/* Raised when a key is released. */ +Event_Int32 KeyEvents_KeyUp; #endif \ No newline at end of file diff --git a/src/Client/Key.c b/src/Client/Key.c new file mode 100644 index 000000000..97bb4cb42 --- /dev/null +++ b/src/Client/Key.c @@ -0,0 +1,24 @@ +#include "Key.h" +#include "Events.h" + +bool Key_States[Key_Count]; +bool Key_GetPressed(Key key) { return Key_States[key]; } + +void Key_SetPressed(Key key, bool pressed) { + if (Key_States[key] != pressed || Key_KeyRepeat) { + Key_States[key] = pressed; + + if (pressed) { + Event_RaiseInt32(&KeyEvents_KeyDown, key); + } else { + Event_RaiseInt32(&KeyEvents_KeyUp, key); + } + } +} + +void Key_Clear(void) { + Int32 i; + for (i = 0; i < Key_Count; i++) { + if (Key_States[i]) Key_SetPressed((Key)i, false); + } +} \ No newline at end of file diff --git a/src/Client/Key.h b/src/Client/Key.h new file mode 100644 index 000000000..f0c032e51 --- /dev/null +++ b/src/Client/Key.h @@ -0,0 +1,90 @@ +#ifndef CS_KEY_H +#define CS_KEY_H +#include "Typedefs.h" +/* Manages the keyboard, and raises events when keys are pressed etc. + Copyright 2017 ClassicalSharp | Licensed under BSD-3 | Based on OpenTK code +*/ + +/* + The Open Toolkit Library License + + Copyright (c) 2006 - 2009 the Open Toolkit library. + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights to + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + the Software, and to permit persons to whom the Software is furnished to do + so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + OTHER DEALINGS IN THE SOFTWARE. +*/ + +typedef enum Key_ { + /* Key outside the known keys */ + Key_Unknown = 0, + + /* Modifiers */ + Key_ShiftLeft, Key_ShiftRight, Key_ControlLeft, Key_ControlRight, + Key_AltLeft, Key_AltRight, Key_WinLeft, Key_WinRight, Key_Menu, + + /* Function keys (hopefully enough for most keyboards - mine has 26) + / on X11 reports up to 35 function keys. */ + F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, + F11, F12, F13, F14, F15, F16, F17, F18, F19, F20, + F21, F22, F23, F24, F25, F26, F27, F28, F29, F30, + F31, F32, F33, F34, F35, + + /* Direction arrows */ + Key_Up, Key_Down, Key_Left, Key_Right, + + /* Action keys */ + Key_Enter, Key_Escape, Key_Space, Key_Tab, Key_BackSpace, Key_Insert, + Key_Delete, Key_PageUp, Key_PageDown, Key_Home, Key_End, Key_CapsLock, + Key_ScrollLock, Key_PrintScreen, Key_Pause, Key_NumLock, + + // Keypad keys + Key_Keypad0, Key_Keypad1, Key_Keypad2, Key_Keypad3, Key_Keypad4, + Key_Keypad5, Key_Keypad6, Key_Keypad7, Key_Keypad8, Key_Keypad9, + Key_KeypadDivide, Key_KeypadMultiply, Key_KeypadSubtract, + Key_KeypadAdd, Key_KeypadDecimal, Key_KeypadEnter, + + /* Letters */ + Key_A, Key_B, Key_C, Key_D, Key_E, Key_F, Key_G, Key_H, Key_I, Key_J, + Key_K, Key_L, Key_M, Key_N, Key_O, Key_P, Key_Q, Key_R, Key_S, Key_T, + Key_U, Key_V, Key_W, Key_X, Key_Y, Key_Z, + + /* Numbers */ + Key_Number0, Key_Number1, Key_Number2, Key_Number3, Key_Number4, + Key_Number5, Key_Number6, Key_Number7, Key_Number8, Key_Number9, + + /* Symbols */ + Key_Tilde, Key_Minus, Key_Plus, Key_BracketLeft, Key_BracketRight, + Key_Semicolon, Key_Quote, Key_Comma, Key_Period, Key_Slash, Key_BackSlash, + + /* Last available keyboard key */ + Key_Count, +} Key; + +/* Gets whether the given key is currently being pressed. */ +bool Key_GetPressed(Key key); +/* Sets whether the given key is currently being pressed. */ +void Key_SetPressed(Key key, bool pressed); + +/* 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. */ +bool Key_KeyRepeat; + +/* Unpresses all keys that were previously pressed. */ +void Key_Clear(void); +#endif \ No newline at end of file diff --git a/src/Client/TerrainAtlas2D.c b/src/Client/TerrainAtlas2D.c index ee3d6d93c..d9745d997 100644 --- a/src/Client/TerrainAtlas2D.c +++ b/src/Client/TerrainAtlas2D.c @@ -1,4 +1,3 @@ -#if 0 #include "TerrainAtlas2D.h" #include "Platform.h" #include "Block.h" @@ -44,5 +43,4 @@ Int32 Atlas2D_LoadTextureElement_Raw(TextureLoc texLoc, Bitmap* element) { void Atlas2D_Free(void) { if (Atlas2D_Bitmap.Scan0 == NULL) return; Platform_MemFree(Atlas2D_Bitmap.Scan0); -} -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/src/Client/TerrainAtlas2D.h b/src/Client/TerrainAtlas2D.h index d37129842..a35827c34 100644 --- a/src/Client/TerrainAtlas2D.h +++ b/src/Client/TerrainAtlas2D.h @@ -1,4 +1,3 @@ -#if 0 #ifndef CS_TERRAINATLAS2D_H #define CS_TERRAINATLAS2D_H #include "Typedefs.h" @@ -30,5 +29,4 @@ static Int32 Atlas2D_LoadTextureElement_Raw(TextureLoc texLoc, Bitmap* element); /* Disposes of the underlying atlas bitmap. */ void Atlas2D_Free(void); -#endif #endif \ No newline at end of file diff --git a/src/Client/Window.h b/src/Client/Window.h index c7620d864..86fb6b6ac 100644 --- a/src/Client/Window.h +++ b/src/Client/Window.h @@ -5,9 +5,11 @@ #include "Compiler.h" #include "Bitmap.h" #include "2DStructs.h" +#include "DisplayDevice.h" /* Abstracts creating and managing a native window. Copyright 2017 ClassicalSharp | Licensed under BSD-3 | Based on OpenTK code */ + /* The Open Toolkit Library License @@ -31,7 +33,20 @@ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ +*/ + +typedef UInt8 WindowState; +/* The window is in its normal state. */ +#define WindowState_Normal 0 +/* The window is minimized to the taskbar (also known as 'iconified'). */ +#define WindowState_Minimized 1 +/* The window covers the whole working area, which includes the desktop but not the taskbar and/or panels. */ +#define WindowState_Maximized 2 +/* The window covers the whole screen, including all taskbars and/or panels. */ +#define WindowState_Fullscreen 3 + +/* Creates a new window. */ +void Window_Create(Int32 x, Int32 y, Int32 width, Int32 height, STRING_TRANSIENT String* title, DisplayDevice* device) { /* Gets the current contents of the clipboard. */ void Window_GetClipboardText(STRING_TRANSIENT String* value); @@ -53,8 +68,10 @@ bool Window_GetExists(void); /// Gets the for this window. IWindowInfo WindowInfo{ get; } - /// Gets or sets the for this window. - WindowState WindowState{ get; set; } +/* Gets the WindowState of this window. */ +WindowState Window_GetWindowState(void); +/* Sets the WindowState of this window. */ +void Window_SetWindowState(WindowState value); /* Gets the external bounds of this window, in screen coordinates. External bounds include title bar, borders and drawing area of the window. */ @@ -95,12 +112,6 @@ Point2D Window_PointToClient(Point2D point); /* Transforms the specified point from client to screen coordinates. */ Point2D Window_PointToScreen(Point2D point); - /// Gets the available KeyboardDevice. - KeyboardDevice Keyboard{ get; } - - /// Gets the available MouseDevice. - MouseDevice Mouse{ get; } - /* Gets the cursor position in screen coordinates. */ Point2D Window_GetDesktopCursorPos(void); /* Sets the cursor position in screen coordinates. */