-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.cs
89 lines (88 loc) · 2.57 KB
/
template.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using System.IO;
using System.Drawing;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;
namespace Template
{
public class OpenTKApp : GameWindow
{
static int screenID;
static Game game;
static bool terminated = false;
protected override void OnLoad( EventArgs e )
{
// called upon app init
GL.ClearColor( Color.Black );
GL.Enable( EnableCap.Texture2D );
GL.Disable( EnableCap.DepthTest );
GL.Hint( HintTarget.PerspectiveCorrectionHint, HintMode.Nicest );
ClientSize = new Size( 640, 400 );
game = new Game();
game.screen = new Surface( Width, Height );
Sprite.target = game.screen;
screenID = game.screen.GenTexture();
game.Init();
}
protected override void OnUnload( EventArgs e )
{
// called upon app close
GL.DeleteTextures( 1, ref screenID );
Environment.Exit( 0 ); // bypass wait for key on CTRL-F5
}
protected override void OnResize( EventArgs e )
{
// called upon window resize
GL.Viewport(0, 0, Width, Height);
GL.MatrixMode( MatrixMode.Projection );
GL.LoadIdentity();
GL.Ortho( -1.0, 1.0, -1.0, 1.0, 0.0, 4.0 );
}
protected override void OnUpdateFrame( FrameEventArgs e )
{
// called once per frame; app logic
var keyboard = OpenTK.Input.Keyboard.GetState();
if (keyboard[OpenTK.Input.Key.Escape]) this.Exit();
}
protected override void OnRenderFrame( FrameEventArgs e )
{
// called once per frame; render
game.Tick();
if (terminated)
{
Exit();
return;
}
// convert Game.screen to OpenGL texture
GL.BindTexture( TextureTarget.Texture2D, screenID );
GL.TexImage2D( TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba,
game.screen.width, game.screen.height, 0,
OpenTK.Graphics.OpenGL.PixelFormat.Bgra,
PixelType.UnsignedByte, game.screen.pixels
);
// clear window contents
GL.Clear( ClearBufferMask.ColorBufferBit );
// setup camera
GL.MatrixMode( MatrixMode.Modelview );
GL.LoadIdentity();
GL.MatrixMode( MatrixMode.Projection );
GL.LoadIdentity();
// draw screen filling quad
GL.Begin( PrimitiveType.Quads );
GL.TexCoord2( 0.0f, 1.0f ); GL.Vertex2( -1.0f, -1.0f );
GL.TexCoord2( 1.0f, 1.0f ); GL.Vertex2( 1.0f, -1.0f );
GL.TexCoord2( 1.0f, 0.0f ); GL.Vertex2( 1.0f, 1.0f );
GL.TexCoord2( 0.0f, 0.0f ); GL.Vertex2( -1.0f, 1.0f );
GL.End();
// tell OpenTK we're done rendering
SwapBuffers();
}
public static void Main( string[] args )
{
// entry point
using (OpenTKApp app = new OpenTKApp()) { app.Run( 30.0, 0.0 ); }
}
}
}