-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#5 - Cleanup Games and implement WIP SDL2 and Windows (Forms) applica…
…tion.
- Loading branch information
1 parent
975e71f
commit d13c50e
Showing
27 changed files
with
278 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright © Amer Koleci and Contributors. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repository root for more information. | ||
|
||
using System; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using static SDL2.SDL; | ||
using static SDL2.SDL.SDL_EventType; | ||
|
||
namespace Vortice | ||
{ | ||
public sealed class SDL2GameContext : GameContext | ||
{ | ||
private const int _eventsPerPeep = 64; | ||
private readonly SDL_Event[] _events = new SDL_Event[_eventsPerPeep]; | ||
|
||
private bool _exiting = false; | ||
|
||
public override void ConfigureServices(IServiceCollection services) | ||
{ | ||
base.ConfigureServices(services); | ||
|
||
// Init SDL2 | ||
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER) != 0) | ||
{ | ||
SDL_Log($"Unable to initialize SDL: {SDL_GetError()}"); | ||
return; | ||
} | ||
|
||
services.AddSingleton<GameView>(new SDL2GameView()); | ||
} | ||
|
||
public override void RunMainLoop(Action init, Action callback) | ||
{ | ||
init(); | ||
|
||
while (!_exiting) | ||
{ | ||
PollSDLEvents(); | ||
callback(); | ||
} | ||
|
||
SDL_Quit(); | ||
} | ||
|
||
private void PollSDLEvents() | ||
{ | ||
SDL_PumpEvents(); | ||
int eventsRead; | ||
|
||
do | ||
{ | ||
eventsRead = SDL_PeepEvents(_events, _eventsPerPeep, SDL_eventaction.SDL_GETEVENT, SDL_EventType.SDL_FIRSTEVENT, SDL_EventType.SDL_LASTEVENT); | ||
for (int i = 0; i < eventsRead; i++) | ||
{ | ||
handleSDLEvent(_events[i]); | ||
} | ||
} while (eventsRead == _eventsPerPeep); | ||
} | ||
|
||
private void handleSDLEvent(SDL_Event e) | ||
{ | ||
switch (e.type) | ||
{ | ||
case SDL_QUIT: | ||
case SDL_APP_TERMINATING: | ||
_exiting = true; | ||
break; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright (c) Amer Koleci and contributors. | ||
// Distributed under the MIT license. See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Drawing; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Runtime.InteropServices; | ||
using Vortice.Graphics; | ||
using static SDL2.SDL; | ||
using static SDL2.SDL.SDL_WindowFlags; | ||
|
||
namespace Vortice | ||
{ | ||
internal class SDL2GameView : GameView | ||
{ | ||
private readonly IntPtr _window; | ||
|
||
public SDL2GameView() | ||
{ | ||
SDL_WindowFlags flags = SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_HIDDEN | SDL_WINDOW_RESIZABLE; | ||
|
||
_window = SDL_CreateWindow("Vortice", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1200, 800, flags); | ||
|
||
SDL_GetWindowSize(_window, out int width, out int height); | ||
ClientSize = new Size(width, height); | ||
|
||
// Native handle | ||
var wmInfo = new SDL_SysWMinfo(); | ||
SDL_GetWindowWMInfo(_window, ref wmInfo); | ||
|
||
// Window handle is selected per subsystem as defined at: | ||
// https://wiki.libsdl.org/SDL_SysWMinfo | ||
switch (wmInfo.subsystem) | ||
{ | ||
case SDL_SYSWM_TYPE.SDL_SYSWM_WINDOWS: | ||
Source = SurfaceSource.CreateWin32( | ||
wmInfo.info.win.hinstance, | ||
wmInfo.info.win.window | ||
); | ||
break; | ||
|
||
case SDL_SYSWM_TYPE.SDL_SYSWM_X11: | ||
//return wmInfo.info.x11.window; | ||
break; | ||
|
||
case SDL_SYSWM_TYPE.SDL_SYSWM_COCOA: | ||
//return wmInfo.info.cocoa.window; | ||
break; | ||
|
||
case SDL_SYSWM_TYPE.SDL_SYSWM_UIKIT: | ||
//return wmInfo.info.uikit.window; | ||
break; | ||
|
||
case SDL_SYSWM_TYPE.SDL_SYSWM_WAYLAND: | ||
//return wmInfo.info.wl.shell_surface; | ||
break; | ||
|
||
case SDL_SYSWM_TYPE.SDL_SYSWM_ANDROID: | ||
//return wmInfo.info.android.window; | ||
break; | ||
|
||
default: | ||
break; | ||
} | ||
|
||
SDL_ShowWindow(_window); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override Size ClientSize { get; } | ||
|
||
/// <inheritdoc /> | ||
public override SurfaceSource Source { get; } | ||
|
||
private void OnControlClientSizeChanged(object? sender, EventArgs e) | ||
{ | ||
OnSizeChanged(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.