forked from dvdking/BlueberryEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlueberryGame.cs
219 lines (198 loc) · 6.59 KB
/
BlueberryGame.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
using System;
using System.Collections.Generic;
using Blueberry.Audio;
using Blueberry.Diagnostics;
using Blueberry.Graphics;
using Blueberry.Input;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;
namespace Blueberry
{
/// <summary>
/// Description of BlueberryGame.
/// </summary>
public class BlueberryGame:IDisposable
{
private static BlueberryGame _current;
public static BlueberryGame CurrentGame{get {return _current;}}
private GameWindow _window;
//public bool CursorVisible
//{
// get {return _window.CursorVisible; }
//}
public VSyncMode VSync
{
get {return _window.VSync;}
set {_window.VSync = value;}
}
public GameWindow Window
{
get{ return _window; }
}
int _framebuffer = -1;
GameFrame _currentFrame;
GameFrame _nextFrame;
public GameFrame CurrentFrame {get{return _currentFrame;} set {_nextFrame = value;}}
public BlueberryGame(int width, int height, string name, bool fullscreen, double contextVersion = 0)
{
_current = this;
if (contextVersion == 0)
{
int major = (int)contextVersion;
int minor = (int)((contextVersion - major) * 10);
_window = new GameWindow(width, height,
GraphicsMode.Default, name,
fullscreen ? GameWindowFlags.Fullscreen : GameWindowFlags.Default,
DisplayDevice.Default);
}
else
{
int major = (int)contextVersion;
int minor = (int)((contextVersion - major) * 10);
_window = new GameWindow(width, height,
GraphicsMode.Default, name,
fullscreen ? GameWindowFlags.Fullscreen : GameWindowFlags.Default,
DisplayDevice.Default, major, minor,
#if DEBUG
GraphicsContextFlags.Default);
#else
GraphicsContextFlags.ForwardCompatible);
#endif
}
#if DEBUG
new DiagnosticsCenter();
#endif
#if (WAV || OGG)
new AudioManager(16, 8, 4096, true);
#endif
VSync = VSyncMode.Off;
InputMgr.Init(_window);
//_window.VSync = VSyncMode.On;
_window.UpdateFrame += InternalUpdate;
_window.RenderFrame += InternalRender;
_window.Load += (a, b)=>Load();
Capabilities.Test();
if (Capabilities.Framebuffers == GLExtensionSupport.Core)
{
GL.GenFramebuffers(1, out _framebuffer);
GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
} else if (Capabilities.Framebuffers == GLExtensionSupport.Extension)
{
GL.Ext.GenFramebuffers(1, out _framebuffer);
GL.Ext.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
}
GL.ClearColor(Color4.CornflowerBlue);
GL.Viewport(0, 0, width, height);
GL.Enable(EnableCap.Blend);
GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
}
public void SetFrame(GameFrame frame)
{
_nextFrame = frame;
}
public void SetRenderTarget(Texture target)
{
if (target == null)
{
if (Capabilities.Framebuffers == GLExtensionSupport.Core)
GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
else
GL.Ext.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
}
else
{
if (Capabilities.Framebuffers == GLExtensionSupport.Core)
{
GL.BindFramebuffer(FramebufferTarget.Framebuffer, _framebuffer);
GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, target.ID, 0);
}
else
{
GL.Ext.BindFramebuffer(FramebufferTarget.Framebuffer, _framebuffer);
GL.Ext.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, target.ID, 0);
}
GL.Clear(ClearBufferMask.ColorBufferBit);
}
}
protected virtual void Load()
{
}
protected virtual void Update()
{
}
internal virtual void InternalUpdate(object sender, FrameEventArgs e)
{
GS.Delta = (float)e.Time;
GS.Total += (float)e.Time;
InputMgr.Update();
if(_currentFrame != null)
_currentFrame.Update(GS.Delta);
if(_nextFrame != null)
{
if(_currentFrame != null)
_currentFrame.Unload();
_currentFrame = _nextFrame;
_currentFrame.Load();
_nextFrame = null;
}
#if DEBUG
DiagnosticsCenter.Instance.Update(GS.Delta);
if (InputMgr.Keyboard[Key.Tilde])
if (DiagnosticsCenter.Instance.Visible) DiagnosticsCenter.Instance.Hide();
else DiagnosticsCenter.Instance.Show();
#endif
Update();
}
protected virtual void Render()
{
}
internal virtual void InternalRender(object sender, FrameEventArgs e)
{
GS.Delta = (float)e.Time;
if(_currentFrame != null)
_currentFrame.Render(GS.Delta);
Render();
#if DEBUG
DiagnosticsCenter.Instance.Draw(GS.Delta);
#endif
_window.SwapBuffers();
}
public void Run()
{
_window.Run();
}
public void Run(double updateRate)
{
_window.Run(updateRate);
}
public void Run(double ups, double fps)
{
_window.Run(ups, fps);
}
public void Exit()
{
Dispose();
_window.Exit();
}
public void Dispose()
{
if (_currentFrame != null)
_currentFrame.Unload();
if(SpriteBatch.HasInstance)
SpriteBatch.Please.Dispose();
if(AudioManager.HasInstance)
AudioManager.Instance.Dispose();
if (_framebuffer != -1)
{
if(Capabilities.Framebuffers == GLExtensionSupport.Core)
GL.DeleteFramebuffers(1, ref _framebuffer);
else
GL.Ext.DeleteFramebuffers(1, ref _framebuffer);
_framebuffer = -1;
}
}
}
}