mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-09-18 03:55:19 -04:00
Abstract away platform specific window/input handling.
This commit is contained in:
parent
f7e8e41346
commit
be005c791f
@ -222,6 +222,7 @@
|
|||||||
<Compile Include="Physics\PickedPos.cs" />
|
<Compile Include="Physics\PickedPos.cs" />
|
||||||
<Compile Include="Physics\Picking.cs" />
|
<Compile Include="Physics\Picking.cs" />
|
||||||
<Compile Include="Platform\Font.cs" />
|
<Compile Include="Platform\Font.cs" />
|
||||||
|
<Compile Include="Platform\IPlatformWindow.cs" />
|
||||||
<Compile Include="Platform\Platform.cs" />
|
<Compile Include="Platform\Platform.cs" />
|
||||||
<Compile Include="Program.cs" />
|
<Compile Include="Program.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
@ -13,10 +13,11 @@ using ClassicalSharp.Renderers;
|
|||||||
using ClassicalSharp.Selections;
|
using ClassicalSharp.Selections;
|
||||||
using ClassicalSharp.TexturePack;
|
using ClassicalSharp.TexturePack;
|
||||||
using OpenTK;
|
using OpenTK;
|
||||||
|
using OpenTK.Input;
|
||||||
|
|
||||||
namespace ClassicalSharp {
|
namespace ClassicalSharp {
|
||||||
|
|
||||||
public partial class Game : GameWindow {
|
public partial class Game {
|
||||||
|
|
||||||
/// <summary> Abstracts the underlying 3D graphics rendering API. </summary>
|
/// <summary> Abstracts the underlying 3D graphics rendering API. </summary>
|
||||||
public IGraphicsApi Graphics;
|
public IGraphicsApi Graphics;
|
||||||
@ -190,5 +191,38 @@ namespace ClassicalSharp {
|
|||||||
Options.Set( OptionsKey.DefaultTexturePack, value );
|
Options.Set( OptionsKey.DefaultTexturePack, value );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal IPlatformWindow window;
|
||||||
|
public MouseDevice Mouse;
|
||||||
|
public KeyboardDevice Keyboard;
|
||||||
|
|
||||||
|
public int Width { get { return window.Width; } }
|
||||||
|
|
||||||
|
public int Height { get { return window.Height; } }
|
||||||
|
|
||||||
|
public Size ClientSize { get { return window.ClientSize; } }
|
||||||
|
|
||||||
|
public bool Focused { get { return window.Focused; } }
|
||||||
|
|
||||||
|
public bool Exists { get { return window.Exists; } }
|
||||||
|
|
||||||
|
public Point PointToScreen( Point coords ) {
|
||||||
|
return window.PointToScreen( coords );
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool VSync {
|
||||||
|
get { return window.VSync; }
|
||||||
|
set { window.VSync = value; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool CursorVisible {
|
||||||
|
get { return window.CursorVisible; }
|
||||||
|
set { window.CursorVisible = value; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public Point DesktopCursorPos {
|
||||||
|
get { return window.DesktopCursorPos; }
|
||||||
|
set { window.DesktopCursorPos = value; }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -14,7 +14,6 @@ using ClassicalSharp.Renderers;
|
|||||||
using ClassicalSharp.Selections;
|
using ClassicalSharp.Selections;
|
||||||
using ClassicalSharp.TexturePack;
|
using ClassicalSharp.TexturePack;
|
||||||
using OpenTK;
|
using OpenTK;
|
||||||
using OpenTK.Graphics;
|
|
||||||
using OpenTK.Input;
|
using OpenTK.Input;
|
||||||
#if ANDROID
|
#if ANDROID
|
||||||
using Android.Graphics;
|
using Android.Graphics;
|
||||||
@ -22,7 +21,7 @@ using Android.Graphics;
|
|||||||
|
|
||||||
namespace ClassicalSharp {
|
namespace ClassicalSharp {
|
||||||
|
|
||||||
public partial class Game : GameWindow {
|
public partial class Game : IDisposable {
|
||||||
|
|
||||||
void LoadAtlas( Bitmap bmp ) {
|
void LoadAtlas( Bitmap bmp ) {
|
||||||
TerrainAtlas1D.Dispose();
|
TerrainAtlas1D.Dispose();
|
||||||
@ -36,7 +35,14 @@ namespace ClassicalSharp {
|
|||||||
Events.RaiseTerrainAtlasChanged();
|
Events.RaiseTerrainAtlasChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnLoad( EventArgs e ) {
|
public void Run() { window.Run(); }
|
||||||
|
|
||||||
|
public void Exit() { window.Exit(); }
|
||||||
|
|
||||||
|
|
||||||
|
internal void OnLoad() {
|
||||||
|
Mouse = window.Mouse;
|
||||||
|
Keyboard = window.Keyboard;
|
||||||
#if !USE_DX
|
#if !USE_DX
|
||||||
Graphics = new OpenGLApi();
|
Graphics = new OpenGLApi();
|
||||||
#else
|
#else
|
||||||
@ -151,12 +157,11 @@ namespace ClassicalSharp {
|
|||||||
void LoadIcon() {
|
void LoadIcon() {
|
||||||
string launcherPath = Path.Combine( Program.AppDirectory, "Launcher2.exe" );
|
string launcherPath = Path.Combine( Program.AppDirectory, "Launcher2.exe" );
|
||||||
if( File.Exists( launcherPath ) ) {
|
if( File.Exists( launcherPath ) ) {
|
||||||
Icon = Icon.ExtractAssociatedIcon( launcherPath );
|
window.Icon = Icon.ExtractAssociatedIcon( launcherPath ); return;
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
launcherPath = Path.Combine( Program.AppDirectory, "Launcher.exe" );
|
launcherPath = Path.Combine( Program.AppDirectory, "Launcher.exe" );
|
||||||
if( File.Exists( launcherPath ) ) {
|
if( File.Exists( launcherPath ) ) {
|
||||||
Icon = Icon.ExtractAssociatedIcon( launcherPath );
|
window.Icon = Icon.ExtractAssociatedIcon( launcherPath );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -185,7 +190,7 @@ namespace ClassicalSharp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Stopwatch frameTimer = new Stopwatch();
|
Stopwatch frameTimer = new Stopwatch();
|
||||||
protected override void OnRenderFrame( FrameEventArgs e ) {
|
internal void RenderFrame( FrameEventArgs e ) {
|
||||||
frameTimer.Reset();
|
frameTimer.Reset();
|
||||||
frameTimer.Start();
|
frameTimer.Start();
|
||||||
|
|
||||||
@ -196,7 +201,6 @@ namespace ClassicalSharp {
|
|||||||
if( !Focused && !ScreenLockedInput )
|
if( !Focused && !ScreenLockedInput )
|
||||||
SetNewScreen( new PauseScreen( this ) );
|
SetNewScreen( new PauseScreen( this ) );
|
||||||
|
|
||||||
base.OnRenderFrame( e );
|
|
||||||
CheckScheduledTasks( e.Time );
|
CheckScheduledTasks( e.Time );
|
||||||
float t = (float)( ticksAccumulator / ticksPeriod );
|
float t = (float)( ticksAccumulator / ticksPeriod );
|
||||||
LocalPlayer.SetInterpPosition( t );
|
LocalPlayer.SetInterpPosition( t );
|
||||||
@ -308,8 +312,7 @@ namespace ClassicalSharp {
|
|||||||
Graphics.SetMatrixMode( MatrixType.Modelview );
|
Graphics.SetMatrixMode( MatrixType.Modelview );
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnResize( object sender, EventArgs e ) {
|
internal void OnResize() {
|
||||||
base.OnResize( sender, e );
|
|
||||||
Graphics.OnWindowResize( this );
|
Graphics.OnWindowResize( this );
|
||||||
UpdateProjection();
|
UpdateProjection();
|
||||||
if( activeScreen != null )
|
if( activeScreen != null )
|
||||||
@ -426,7 +429,7 @@ namespace ClassicalSharp {
|
|||||||
|
|
||||||
public Key Mapping( KeyBinding mapping ) { return InputHandler.Keys[mapping]; }
|
public Key Mapping( KeyBinding mapping ) { return InputHandler.Keys[mapping]; }
|
||||||
|
|
||||||
public override void Dispose() {
|
public void Dispose() {
|
||||||
MapRenderer.Dispose();
|
MapRenderer.Dispose();
|
||||||
MapBordersRenderer.Dispose();
|
MapBordersRenderer.Dispose();
|
||||||
EnvRenderer.Dispose();
|
EnvRenderer.Dispose();
|
||||||
@ -459,7 +462,6 @@ namespace ClassicalSharp {
|
|||||||
|
|
||||||
if( Options.HasChanged )
|
if( Options.HasChanged )
|
||||||
Options.Save();
|
Options.Save();
|
||||||
base.Dispose();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal bool CanPick( byte block ) {
|
internal bool CanPick( byte block ) {
|
||||||
@ -472,8 +474,8 @@ namespace ClassicalSharp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Game( string username, string mppass, string skinServer,
|
public Game( string username, string mppass, string skinServer,
|
||||||
bool nullContext, int width, int height )
|
bool nullContext, int width, int height ) {
|
||||||
: base( width, height, GraphicsMode.Default, Program.AppName + " (" + username + ")", nullContext, 0, DisplayDevice.Default ) {
|
window = new DesktopWindow( this, username, nullContext, width, height );
|
||||||
Username = username;
|
Username = username;
|
||||||
Mppass = mppass;
|
Mppass = mppass;
|
||||||
this.skinServer = skinServer;
|
this.skinServer = skinServer;
|
||||||
|
@ -20,7 +20,7 @@ namespace ClassicalSharp {
|
|||||||
void RegisterInputHandlers() {
|
void RegisterInputHandlers() {
|
||||||
game.Keyboard.KeyDown += KeyDownHandler;
|
game.Keyboard.KeyDown += KeyDownHandler;
|
||||||
game.Keyboard.KeyUp += KeyUpHandler;
|
game.Keyboard.KeyUp += KeyUpHandler;
|
||||||
game.KeyPress += KeyPressHandler;
|
game.window.KeyPress += KeyPressHandler;
|
||||||
game.Mouse.WheelChanged += MouseWheelChanged;
|
game.Mouse.WheelChanged += MouseWheelChanged;
|
||||||
game.Mouse.Move += MouseMove;
|
game.Mouse.Move += MouseMove;
|
||||||
game.Mouse.ButtonDown += MouseButtonDown;
|
game.Mouse.ButtonDown += MouseButtonDown;
|
||||||
@ -328,9 +328,9 @@ namespace ClassicalSharp {
|
|||||||
} else if( key == Keys[KeyBinding.HideFps] ) {
|
} else if( key == Keys[KeyBinding.HideFps] ) {
|
||||||
game.ShowFPS = !game.ShowFPS;
|
game.ShowFPS = !game.ShowFPS;
|
||||||
} else if( key == Keys[KeyBinding.Fullscreen] ) {
|
} else if( key == Keys[KeyBinding.Fullscreen] ) {
|
||||||
WindowState state = game.WindowState;
|
WindowState state = game.window.WindowState;
|
||||||
if( state != WindowState.Minimized ) {
|
if( state != WindowState.Minimized ) {
|
||||||
game.WindowState = state == WindowState.Fullscreen ?
|
game.window.WindowState = state == WindowState.Fullscreen ?
|
||||||
WindowState.Normal : WindowState.Fullscreen;
|
WindowState.Normal : WindowState.Fullscreen;
|
||||||
}
|
}
|
||||||
} else if( key == Keys[KeyBinding.ShowAxisLines] ) {
|
} else if( key == Keys[KeyBinding.ShowAxisLines] ) {
|
||||||
|
@ -32,7 +32,7 @@ namespace ClassicalSharp.GraphicsAPI {
|
|||||||
|
|
||||||
public Direct3D9Api( Game game ) {
|
public Direct3D9Api( Game game ) {
|
||||||
MinZNear = 0.05f;
|
MinZNear = 0.05f;
|
||||||
IntPtr windowHandle = ((WinWindowInfo)game.WindowInfo).WindowHandle;
|
IntPtr windowHandle = ((WinWindowInfo)game.window.WindowInfo).WindowHandle;
|
||||||
d3d = new Direct3D();
|
d3d = new Direct3D();
|
||||||
int adapter = d3d.Adapters[0].Adapter;
|
int adapter = d3d.Adapters[0].Adapter;
|
||||||
InitFields();
|
InitFields();
|
||||||
@ -395,11 +395,11 @@ namespace ClassicalSharp.GraphicsAPI {
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
public override void BeginFrame( GameWindow game ) {
|
public override void BeginFrame( Game game ) {
|
||||||
device.BeginScene();
|
device.BeginScene();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void EndFrame( GameWindow game ) {
|
public override void EndFrame( Game game ) {
|
||||||
device.EndScene();
|
device.EndScene();
|
||||||
int code = device.Present();
|
int code = device.Present();
|
||||||
if( code >= 0 ) return;
|
if( code >= 0 ) return;
|
||||||
@ -426,17 +426,17 @@ namespace ClassicalSharp.GraphicsAPI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool vsync = false;
|
bool vsync = false;
|
||||||
public override void SetVSync( GameWindow game, bool value ) {
|
public override void SetVSync( Game game, bool value ) {
|
||||||
vsync = value;
|
vsync = value;
|
||||||
game.VSync = value;
|
game.VSync = value;
|
||||||
RecreateDevice( game );
|
RecreateDevice( game );
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void OnWindowResize( GameWindow game ) {
|
public override void OnWindowResize( Game game ) {
|
||||||
RecreateDevice( game );
|
RecreateDevice( game );
|
||||||
}
|
}
|
||||||
|
|
||||||
void RecreateDevice( GameWindow game ) {
|
void RecreateDevice( Game game ) {
|
||||||
PresentParameters args = GetPresentArgs( game.Width, game.Height );
|
PresentParameters args = GetPresentArgs( game.Width, game.Height );
|
||||||
for( int i = 0; i < dynamicvBuffers.Length; i++ ) {
|
for( int i = 0; i < dynamicvBuffers.Length; i++ ) {
|
||||||
DynamicDataBuffer buffer = dynamicvBuffers[i];
|
DynamicDataBuffer buffer = dynamicvBuffers[i];
|
||||||
|
@ -218,17 +218,17 @@ namespace ClassicalSharp.GraphicsAPI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary> Informs the graphic api to update its state in preparation for a new frame. </summary>
|
/// <summary> Informs the graphic api to update its state in preparation for a new frame. </summary>
|
||||||
public abstract void BeginFrame( GameWindow game );
|
public abstract void BeginFrame( Game game );
|
||||||
|
|
||||||
/// <summary> Informs the graphic api to update its state in preparation for the end of a frame,
|
/// <summary> Informs the graphic api to update its state in preparation for the end of a frame,
|
||||||
/// and to prepare that frame for display on the monitor. </summary>
|
/// and to prepare that frame for display on the monitor. </summary>
|
||||||
public abstract void EndFrame( GameWindow game );
|
public abstract void EndFrame( Game game );
|
||||||
|
|
||||||
/// <summary> Sets whether the graphics api should tie frame rendering to the refresh rate of the monitor. </summary>
|
/// <summary> Sets whether the graphics api should tie frame rendering to the refresh rate of the monitor. </summary>
|
||||||
public abstract void SetVSync( GameWindow game, bool value );
|
public abstract void SetVSync( Game game, bool value );
|
||||||
|
|
||||||
/// <summary> Raised when the dimensions of the game's window have changed. </summary>
|
/// <summary> Raised when the dimensions of the game's window have changed. </summary>
|
||||||
public abstract void OnWindowResize( GameWindow game );
|
public abstract void OnWindowResize( Game game );
|
||||||
|
|
||||||
/// <summary> Delegate that is invoked when the current context is lost,
|
/// <summary> Delegate that is invoked when the current context is lost,
|
||||||
/// and is repeatedly invoked until the context can be retrieved. </summary>
|
/// and is repeatedly invoked until the context can be retrieved. </summary>
|
||||||
|
72
ClassicalSharp/Platform/IPlatformWindow.cs
Normal file
72
ClassicalSharp/Platform/IPlatformWindow.cs
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
using System;
|
||||||
|
using System.Drawing;
|
||||||
|
using OpenTK;
|
||||||
|
using OpenTK.Graphics;
|
||||||
|
using OpenTK.Input;
|
||||||
|
using OpenTK.Platform;
|
||||||
|
|
||||||
|
namespace ClassicalSharp {
|
||||||
|
|
||||||
|
/// <summary> Abstracts away a platform specific window, and input handler mechanism. </summary>
|
||||||
|
public interface IPlatformWindow {
|
||||||
|
|
||||||
|
int Width { get; }
|
||||||
|
|
||||||
|
int Height { get; }
|
||||||
|
|
||||||
|
Size ClientSize { get; }
|
||||||
|
|
||||||
|
bool VSync { get; set; }
|
||||||
|
|
||||||
|
bool Exists { get; }
|
||||||
|
|
||||||
|
bool Focused { get; }
|
||||||
|
|
||||||
|
bool CursorVisible { get; set; }
|
||||||
|
|
||||||
|
Point DesktopCursorPos { get; set; }
|
||||||
|
|
||||||
|
MouseDevice Mouse { get; }
|
||||||
|
|
||||||
|
KeyboardDevice Keyboard { get; }
|
||||||
|
|
||||||
|
Icon Icon { get; set; }
|
||||||
|
|
||||||
|
Point PointToScreen( Point coords );
|
||||||
|
|
||||||
|
WindowState WindowState { get; set; }
|
||||||
|
|
||||||
|
IWindowInfo WindowInfo { get; }
|
||||||
|
|
||||||
|
void Run();
|
||||||
|
|
||||||
|
void Exit();
|
||||||
|
|
||||||
|
event EventHandler<KeyPressEventArgs> KeyPress;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary> Implementation of a native window and native input handling mechanism on Windows, OSX, and Linux. </summary>
|
||||||
|
public sealed class DesktopWindow : GameWindow, IPlatformWindow {
|
||||||
|
|
||||||
|
Game game;
|
||||||
|
public DesktopWindow( Game game, string username, bool nullContext, int width, int height ) :
|
||||||
|
base( width, height, GraphicsMode.Default, Program.AppName + " (" + username + ")", nullContext, 0, DisplayDevice.Default ) {
|
||||||
|
this.game = game;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnLoad( EventArgs e ) {
|
||||||
|
game.OnLoad();
|
||||||
|
base.OnLoad( e );
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Dispose() {
|
||||||
|
game.Dispose();
|
||||||
|
base.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnRenderFrame( FrameEventArgs e ) {
|
||||||
|
game.RenderFrame( e );
|
||||||
|
base.OnRenderFrame( e );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user