-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnotherWorld.cpp
453 lines (360 loc) · 13.9 KB
/
AnotherWorld.cpp
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
// AnotherWorld.cpp : Defines the entry point for the application.
//
#include <chrono>
#include "windowsx.h"
#include "framework.h"
#include "AnotherWorld.h"
#include "another-world/virtual-machine.hpp"
using namespace another_world;
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
WCHAR szTitle[MAX_LOADSTRING]; // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
HWND hWnd;
HACCEL hAccelTable;
RECT rcSurface;
BITMAPINFO bmpInfo;
LPBYTE pbmpRender;
HBITMAP bmpRender;
RECT rcView;
HFONT hFont;
uint8_t screen[320 * 200 / 2];
uint8_t palette[16][3];
uint8_t pixelSize;
std::chrono::steady_clock::time_point start;
uint8_t keys;
uint32_t now() {
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start);
return (uint32_t)elapsed.count();
}
// from Erik Aronesty's response on https://stackoverflow.com/a/8098080
std::string string_format(const std::string fmt, va_list ap) {
int size = ((int)fmt.size()) * 2 + 50; // Use a rubric appropriate for your code
std::string str;
//va_list ap;
while (1) { // Maximum two passes on a POSIX system...
str.resize(size);
//va_start(ap, fmt);
int n = vsnprintf((char*)str.data(), size, fmt.c_str(), ap);
//va_end(ap);
if (n > -1 && n < size) { // Everything worked
str.resize(n);
return str;
}
if (n > -1) // Needed size returned
size = n + 1; // For null char
else
size *= 2; // Guess at a larger size (OS specific)
}
return str;
}
VirtualMachine vm;
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: Place code here.
// Initialize global strings
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_ANOTHERWORLD, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
SetRect(&rcSurface, 0, 0, 320, 200);
hFont = CreateFontA(14, 0, 0, 0, 700, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY, FIXED_PITCH, "Consolas");
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_ANOTHERWORLD));
MSG msg;
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfo.bmiHeader.biWidth = 320;
bmpInfo.bmiHeader.biHeight = 200;
bmpInfo.bmiHeader.biPlanes = 1;
bmpInfo.bmiHeader.biBitCount = 24;
bmpInfo.bmiHeader.biCompression = BI_RGB;
bmpRender = CreateDIBSection(NULL, &bmpInfo, DIB_RGB_COLORS, (void**)&pbmpRender, NULL, 0);
another_world::read_file = [](std::string filename, uint32_t offset, uint32_t length, char* buffer) {
filename = "c:\\another-world-data\\" + filename;
std::wstring wfilename(filename.begin(), filename.end());
HANDLE fh = CreateFile(wfilename.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
SetFilePointer(fh, offset, NULL, FILE_BEGIN);
DWORD bytes_read = NULL;
BOOL result = ReadFile(fh, buffer, length, &bytes_read, NULL);
CloseHandle(fh);
return result && bytes_read == length;
};
another_world::write_file = [](std::string filename, uint32_t length, char* buffer) {
filename = "c:\\another-world-data\\" + filename;
std::wstring wfilename(filename.begin(), filename.end());
HANDLE fh = CreateFile(wfilename.c_str(), GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
SetFilePointer(fh, 0, NULL, FILE_BEGIN);
DWORD bytes_written = NULL;
BOOL result = WriteFile(fh, buffer, length, &bytes_written, NULL);
CloseHandle(fh);
return result && bytes_written == length;
};
another_world::debug = [](const char *fmt, ...) {
uint32_t last_debug_flush_ms = 0;
va_list args;
va_start(args, fmt);
std::string line = string_format(fmt, args) + "\n";
va_end(args);
std::wstring filename = L"vm.log";
HANDLE log_file_handle = CreateFile(filename.c_str(), FILE_APPEND_DATA, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
WriteFile(log_file_handle, line.c_str(), line.length(), NULL, NULL);
CloseHandle(log_file_handle);
};
another_world::update_screen = [](uint8_t *buffer) {
memcpy(screen, buffer, 320 * 200 / 2);
};
another_world::set_palette = [](uint16_t* p) {
// sixten palette entries in the format 0x0RGB
for (uint8_t i = 0; i < 16; i++) {
uint16_t color = p[i];
uint8_t r = (color & 0b0000000000001111) >> 0;
uint8_t g = (color & 0b1111000000000000) >> 12;
uint8_t b = (color & 0b0000111100000000) >> 8;
palette[i][0] = (r << 4) | r;
palette[i][1] = (g << 4) | g;
palette[i][2] = (b << 4) | b;
}
};
/*
another_world::debug_display_update = []() {
InvalidateRect(hWnd, NULL, FALSE);
UpdateWindow(hWnd);
};*/
vm.init();
// initialise the first chapter
vm.initialise_chapter(16001);
start = std::chrono::steady_clock::now();
uint32_t last_frame_clock = now();
while (true) {
// if there is a message to process then do it
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
if (WM_QUIT == msg.message) {
break;
}
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
uint32_t clock = now();
// aim for 50 frames per second
uint16_t pause_ms = 20 * vm.registers[0xff];
// pause_ms = 0;
if(clock - last_frame_clock > pause_ms) {
uint32_t frame_start = now();
vm.execute_threads();
//debug("Frame took %dms", now() - frame_start);
last_frame_clock = clock;
InvalidateRect(hWnd, NULL, FALSE);
}
}
return (int) msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ANOTHERWORLD));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_ANOTHERWORLD);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassExW(&wcex);
}
//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
100, 100, 320 * 4 + 100, 200 * 4 + 100, nullptr, nullptr, hInstance, nullptr);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
void vram_to_bmp(LPBYTE pd, uint8_t* ps) {
for (uint16_t y = 0; y < 200; y++) {
uint8_t* ppd = &pd[(199 - y) * 320 * 3];
for (uint16_t x = 0; x < 320; x += 2) {
uint8_t p = *ps;
uint8_t p1 = p >> 4;
uint8_t p2 = p & 0x0f;
*ppd++ = palette[p1][2];
*ppd++ = palette[p1][1];
*ppd++ = palette[p1][0];
*ppd++ = palette[p2][2];
*ppd++ = palette[p2][1];
*ppd++ = palette[p2][0];
ps++;
}
}
}
//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_SIZE:
{
uint16_t height = HIWORD(lParam);
uint16_t width = LOWORD(lParam);
uint16_t width_scale = floor(width / rcSurface.right);
uint16_t height_scale = floor(height / rcSurface.bottom);
pixelSize = (uint8_t)(width_scale < height_scale ? width_scale : height_scale);
// scale and centre the view rectangle
SetRect(&rcView, 0, 0, rcSurface.right * pixelSize, rcSurface.bottom * pixelSize);
OffsetRect(&rcView, (width - rcView.right) / 2, (height - rcView.bottom) / 2);
}break;
case WM_KEYDOWN:
{
switch (wParam) {
case VK_UP: { input.up = true; break; }
case VK_DOWN: { input.down = true; break; }
case VK_LEFT: { input.left = true; break; }
case VK_RIGHT: { input.right = true; break; }
case VK_SPACE: { input.action = true; break; }
}
}break;
case WM_KEYUP:
{
switch (wParam) {
case VK_UP: { input.up = false; break; }
case VK_DOWN: { input.down = false; break; }
case VK_LEFT: { input.left = false; break; }
case VK_RIGHT: { input.right = false; break; }
case VK_SPACE: { input.action = false; break; }
}
}break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
RECT rcClient;
GetClientRect(hWnd, &rcClient);
// setup a back buffer device context to render into
HDC drawDC = CreateCompatibleDC(hdc);
HBITMAP bmpDraw = CreateCompatibleBitmap(hdc, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top);
HBITMAP hbmOld = (HBITMAP)SelectObject(drawDC, bmpDraw);
HBRUSH backgroundBrush = CreateSolidBrush(RGB(30, 40, 50));
FillRect(drawDC, &rcClient, backgroundBrush);
if (bmpRender) {
uint16_t thumb_width = (rcView.right - rcView.left) / 4;
uint16_t thumb_height = (rcView.bottom - rcView.top) / 4;
RECT rcThumb;
SetRect(&rcThumb, rcView.left, rcView.bottom - thumb_height, rcView.left + thumb_width, rcView.bottom);
InflateRect(&rcThumb, -10, -10);
vram_to_bmp(pbmpRender, screen); // background
StretchDIBits(drawDC, rcView.left, rcView.top, rcView.right - rcView.left, rcView.bottom - rcView.top, 0, 0, 320, 200, pbmpRender, &bmpInfo, DIB_RGB_COLORS, SRCCOPY);
vram_to_bmp(pbmpRender, vram0); // screen
StretchDIBits(drawDC, rcThumb.left, rcThumb.top, rcThumb.right - rcThumb.left, rcThumb.bottom - rcThumb.top, 0, 0, 320, 200, pbmpRender, &bmpInfo, DIB_RGB_COLORS, SRCCOPY);
rcThumb.left += thumb_width; rcThumb.right += thumb_width;
vram_to_bmp(pbmpRender, vram1); // screen
StretchDIBits(drawDC, rcThumb.left, rcThumb.top, rcThumb.right - rcThumb.left, rcThumb.bottom - rcThumb.top, 0, 0, 320, 200, pbmpRender, &bmpInfo, DIB_RGB_COLORS, SRCCOPY);
rcThumb.left += thumb_width; rcThumb.right += thumb_width;
vram_to_bmp(pbmpRender, vram2); // screen
StretchDIBits(drawDC, rcThumb.left, rcThumb.top, rcThumb.right - rcThumb.left, rcThumb.bottom - rcThumb.top, 0, 0, 320, 200, pbmpRender, &bmpInfo, DIB_RGB_COLORS, SRCCOPY);
rcThumb.left += thumb_width; rcThumb.right += thumb_width;
vram_to_bmp(pbmpRender, vram3); // screen
StretchDIBits(drawDC, rcThumb.left, rcThumb.top, rcThumb.right - rcThumb.left, rcThumb.bottom - rcThumb.top, 0, 0, 320, 200, pbmpRender, &bmpInfo, DIB_RGB_COLORS, SRCCOPY);
}
BitBlt(hdc, rcClient.left, rcClient.top, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, drawDC, 0, 0, SRCCOPY);
std::wstring tick_count = std::to_wstring(vm.ticks);
SelectFont(hdc, hFont);
DrawText(hdc, tick_count.c_str(), -1, &rcClient, DT_TOP | DT_LEFT);
SelectObject(drawDC, hbmOld);
DeleteDC(drawDC);
DeleteObject(bmpDraw);
DeleteObject(backgroundBrush);
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}