diff --git a/Capy64/Capy64.csproj b/Capy64/Capy64.csproj index edd60b6..2e577d1 100644 --- a/Capy64/Capy64.csproj +++ b/Capy64/Capy64.csproj @@ -54,6 +54,9 @@ + + + _WINDOWS diff --git a/Capy64/Core/SDL.cs b/Capy64/Core/SDL.cs index 25306f1..a59b493 100644 --- a/Capy64/Core/SDL.cs +++ b/Capy64/Core/SDL.cs @@ -21,16 +21,19 @@ public class SDL { public static string GetClipboardText() { - return SDL2.UTF8_ToManaged(SDL2.Native_SDL_GetClipboardText(), true); + return SDL2.SDL.SDL_GetClipboardText(); + //return SDL2.UTF8_ToManaged(SDL2.Native_SDL_GetClipboardText(), true); } public static void SetClipboardText(string contents) { - SDL2.Native_SDL_SetClipboardText(contents); + SDL2.SDL.SDL_SetClipboardText(contents); + //SDL2.Native_SDL_SetClipboardText(contents); } public static bool HasClipboardText() { - return SDL2.SDL_HasClipboardText() == 1; + return SDL2.SDL.SDL_HasClipboardText() == SDL2.SDL.SDL_bool.SDL_TRUE; + //return SDL2.SDL_HasClipboardText() == 1; } } diff --git a/Capy64/Eventing/InputManager.cs b/Capy64/Eventing/InputManager.cs index 04b8325..2521b93 100644 --- a/Capy64/Eventing/InputManager.cs +++ b/Capy64/Eventing/InputManager.cs @@ -108,7 +108,7 @@ public class InputManager UpdateGamePad(GamePad.GetState(PlayerIndex.One), IsActive); } - private void UpdateMouse(MouseState state, bool isActive) + public void UpdateMouse(MouseState state, bool isActive) { if (!isActive) return; @@ -217,7 +217,7 @@ public class InputManager mouseButtonStates[MouseButton.Button5] = state.XButton2; } - private void UpdateKeyboard(KeyboardState state, bool isActive) + public void UpdateKeyboard(KeyboardState state, bool isActive) { var keys = state.GetPressedKeys(); @@ -306,7 +306,7 @@ public class InputManager } private GamePadState oldGamePadState = new(); - private void UpdateGamePad(GamePadState state, bool isActive) + public void UpdateGamePad(GamePadState state, bool isActive) { if (!isActive) return; diff --git a/Capy64/Extensions/GameWindowExtensions.cs b/Capy64/Extensions/GameWindowExtensions.cs index bf4e8d4..75ed9ae 100644 --- a/Capy64/Extensions/GameWindowExtensions.cs +++ b/Capy64/Extensions/GameWindowExtensions.cs @@ -58,12 +58,14 @@ public static class GameWindowExtensions public static WindowFlags GetWindowFlags(this GameWindow window) { - return (WindowFlags)SDL2.SDL_GetWindowFlags(window.Handle); + return (WindowFlags)SDL2.SDL.SDL_GetWindowFlags(window.Handle); + //return (WindowFlags)SDL2.SDL_GetWindowFlags(window.Handle); } public static void MaximizeWindow(this GameWindow window) { - SDL2.SDL_MaximizeWindow(window.Handle); + SDL2.SDL.SDL_MaximizeWindow(window.Handle); + //SDL2.SDL_MaximizeWindow(window.Handle); } public static bool IsMaximized(this GameWindow window) { diff --git a/Capy64/Program.cs b/Capy64/Program.cs index db9bb22..a9dae91 100644 --- a/Capy64/Program.cs +++ b/Capy64/Program.cs @@ -17,9 +17,9 @@ using Capy64; if (args.Length > 0 && args[0] == "sdl") { - var entry = new SDLEntry(); + using var game = new SDLEntry(); - entry.Run(); + game.Run(); } else { diff --git a/Capy64/Properties/launchSettings.json b/Capy64/Properties/launchSettings.json index d11e4a2..21014d1 100644 --- a/Capy64/Properties/launchSettings.json +++ b/Capy64/Properties/launchSettings.json @@ -5,7 +5,8 @@ "distributionName": "" }, "Capy64": { - "commandName": "Project" + "commandName": "Project", + "commandLineArgs": "sdl" } } } \ No newline at end of file diff --git a/Capy64/SDLEntry.cs b/Capy64/SDLEntry.cs index 6678f05..c7144e4 100644 --- a/Capy64/SDLEntry.cs +++ b/Capy64/SDLEntry.cs @@ -15,11 +15,157 @@ namespace Capy64; -public class SDLEntry +using System; +using System.IO; +using System.Reflection; +using System.Runtime.InteropServices; +using static global::Capy64.Utils; +using static SDL2.SDL; + +public class SDLEntry : IDisposable { + public const string Version = "1.1.0-beta"; + public nint Window { get; private set; } = 0; + public nint Renderer { get; private set; } = 0; + public nint VideoSurface { get; private set; } = 0; + public int WindowWidth { get; private set; } + public int WindowHeight { get; private set; } + public int Width { get; set; } = DefaultParameters.Width; + public int Height { get; set; } = DefaultParameters.Height; + public float Scale { get; set; } = DefaultParameters.Scale; + public uint BorderColor { get; set; } = 0x0; + + public Borders Borders + { + get => _borders; + set + { + _borders = value; + + } + } + private SDL_Rect _bordersRect; + private Borders _borders; + + public static readonly string AssemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); + public static readonly string AssetsPath = Path.Combine(AssemblyPath, "Assets"); + + public static class DefaultParameters + { + public const int Width = 318; + public const int Height = 240; + public const float Scale = 2f; + public const float BorderMultiplier = 1.5f; + public static readonly EngineMode EngineMode = EngineMode.Classic; + + public const int ClassicTickrate = 30; + public const int FreeTickrate = 60; + } + + public static string AppDataPath + { + get + { + string baseDir = + RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? + Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData, + Environment.SpecialFolderOption.Create) : + RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? + Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData, + Environment.SpecialFolderOption.Create) : + RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? + Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData, + Environment.SpecialFolderOption.Create) : + "./"; + + return Path.Combine(baseDir, "Capy64"); + } + } + + public SDLEntry() + { + Borders = new() + { + Top = 0, + Bottom = 0, + Left = 0, + Right = 0, + }; + WindowWidth = (int)(Width * Scale) + Borders.Left + Borders.Right; + WindowHeight = (int)(Height * Scale) + Borders.Top + Borders.Bottom; + } public void Run() { - +#if DEBUG + SDL_SetHint(SDL_HINT_WINDOWS_DISABLE_THREAD_NAMING, "1"); +#endif + + if (SDL_Init(SDL_INIT_EVERYTHING) != 0) + { + Console.WriteLine(SDL_GetError()); + return; + } + Window = SDL_CreateWindow("Capy64 " + Version, + SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, + WindowWidth, WindowHeight, + SDL_WindowFlags.SDL_WINDOW_OPENGL); + + if (Window == nint.Zero) + { + Console.WriteLine(SDL_GetError()); + return; + } + + Renderer = SDL_CreateRenderer(Window, + 0, + SDL_RendererFlags.SDL_RENDERER_ACCELERATED | SDL_RendererFlags.SDL_RENDERER_PRESENTVSYNC); + + if (Renderer == nint.Zero) + { + Console.WriteLine(SDL_GetError()); + return; + } + + VideoSurface = SDL_CreateRGBSurfaceWithFormat(0, Width, Height, 32, SDL_PIXELFORMAT_ARGB8888); + + var running = true; + while (running) + { + while (SDL_PollEvent(out var ev) != 0) + { + switch (ev.type) + { + case SDL_EventType.SDL_QUIT: + running = false; + break; + case SDL_EventType.SDL_KEYDOWN: + if (ev.key.keysym.scancode == SDL_Scancode.SDL_SCANCODE_ESCAPE) + running = false; + + unsafe + { + var pitch = ((SDL_Surface*)VideoSurface)->pitch; + ((uint*)((SDL_Surface*)VideoSurface)->pixels)[10 + 10 * pitch / 4] = 0xFFFF00FF; + } + + break; + } + } + + SDL_RenderClear(Renderer); + var texture = SDL_CreateTextureFromSurface(Renderer, VideoSurface); + SDL_RenderCopy(Renderer, texture, 0, 0); + SDL_DestroyTexture(texture); + SDL_RenderPresent(Renderer); + } + } + + public void Dispose() + { + SDL_DestroyRenderer(Renderer); + SDL_DestroyWindow(Window); + + SDL_Quit(); } } - +