-
Notifications
You must be signed in to change notification settings - Fork 2
/
GameWindow.cs
217 lines (166 loc) · 6.24 KB
/
GameWindow.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
using SFML.Window;
using SFML.Graphics;
using SFML.System;
using System.IO;
using System;
namespace Nitemare3D
{
public static class GameWindow
{
public static uint width = 320;
public static uint height = 200;
static Image renderTarget;
public static float scale;
public static byte[] pal = new byte[1924];
static Text sfText = new Text("DEBUG", new Font("data/FFFFORWA.TTF"), 7);
public static byte[,] frameBuffer;
public static RenderWindow sfWindow;
static RectangleShape rect;
public static void Init()
{
//load config
var config = File.ReadAllLines("config.ini");
width = uint.Parse(config[0]);
uint fps = uint.Parse(config[1]);
height = (uint)(width / 1.6f);
//to simulate tall pixels
uint winHeight = (uint)(height * 1.2f);
scale = width / 320;
//init window
sfWindow = new RenderWindow(new VideoMode(width, winHeight), Game.gameTitle);
renderTarget = new Image(width, height);
sfWindow.SetFramerateLimit(fps);
sfWindow.Closed += (sender, e) => sfWindow.Close();
sfWindow.Resized += (sender, e) => PreserveAspectRatio();
rect = new RectangleShape(new Vector2f(width, winHeight));
//init framebuffer
frameBuffer = new byte[width, height];
//load pal
BinaryReader reader = new BinaryReader(File.OpenRead("data/GAME.PAL"));
reader.BaseStream.Position = 1156;
pal = reader.ReadBytes(768);
reader.BaseStream.Close();
rect.Texture = new Texture(renderTarget);
// rect.Position = new Vector2f(8, 4);
}
public static byte GetDarkenedPixel(byte pixel)
{
var pixelRow = pixel / 10;
if (pixelRow == 0) { return 0; }
var newPixel = pixel - 10;
if (newPixel / 10 != pixelRow)
{
newPixel = pixel;
}
return (byte)newPixel;
}
//todo: bitmap fonts
public static void DrawText(uint x, uint y, string text, int color)
{
sfText.Position = new Vector2f(x, y) * scale;
sfText.DisplayedString = text;
sfText.Scale = new Vector2f(scale, scale);
var r = pal[3 * (color)];
var g = pal[3 * (color) + 1];
var b = pal[3 * (color) + 2];
sfText.FillColor = new Color(r, g, b);
sfWindow.Draw(sfText);
}
public static void Clear()
{
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
frameBuffer[x, y] = 0;
}
}
sfWindow.DispatchEvents();
sfWindow.Clear();
}
public static void DrawFrameBuffer()
{
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
var i = frameBuffer[x, y];
if (i == 0) { continue; }
var r = pal[3 * (i)];
var g = pal[3 * (i) + 1];
var b = pal[3 * (i) + 2];
renderTarget.SetPixel((uint)x, (uint)y, new Color(r, g, b));
}
}
sfWindow.Draw(rect);
}
public static void Display()
{
rect.Texture = new Texture(renderTarget);
GC.Collect();
sfWindow.Display();
}
static float AspectRatio = 1.33333333f;
static void PreserveAspectRatio()//mm letterboxing
{
var m_window_width = sfWindow.Size.X;
var m_window_height = sfWindow.Size.Y;
float new_width = AspectRatio * m_window_height;
float new_height = m_window_width / AspectRatio;
float offset_width = (m_window_width - new_width) / 2.0f;
float offset_height = (m_window_height - new_height) / 2.0f;
View view = sfWindow.GetView();
if (m_window_width >= AspectRatio * m_window_height)
{
view.Viewport = new FloatRect(offset_width / m_window_width, 0.0f, new_width / m_window_width, 1.0f);
}
else
{
view.Viewport = new FloatRect(0.0f, offset_height / m_window_height, 1.0f, new_height / m_window_height);
}
sfWindow.SetView(view);
}
public static bool IsOpen()
{
return sfWindow.IsOpen;
}
public static void DrawImg(int id, Vec2i position)
{
var img = Img.current.entries[id];
for (int tx = 0; tx < img.width; tx++)
{
for (int ty = 0; ty < img.height; ty++)
{
var i = img.data[tx, ty];
if (i == 31) { continue; } //white is transparent
for(int px = (int)(tx * scale); px < (int)(tx * scale + scale); px++)
{
for(int py = (int)(ty * scale); py < (int)(ty * scale + scale); py++)
{
frameBuffer[(int)(position.X * scale) + px, (int)(position.Y * scale) + py] = i;
}
}
}
}
}
public static void DrawPcx()
{
if (Scene.currentScene.pcx == null) { return; }
for (int x = 0; x < 320; x++)
{
for (int y = 0; y < 200; y++)
{
var i = Scene.currentScene.pcx.ImageData[x, y];
if (i == 0) { continue; } //don't draw black pixels
for(int px = (int)(x * scale); px < (int)(x * scale + scale); px++)
{
for(int py = (int)(y * scale); py < (int)(y * scale + scale); py++)
{
frameBuffer[px,py] = i;
}
}
}
}
}
}
}