-
Notifications
You must be signed in to change notification settings - Fork 0
/
windows.c
158 lines (134 loc) · 3.92 KB
/
windows.c
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
// Copyright 2021 Lim Ding Wen
//
// This file is part of 6502js But C.
//
// 6502js But C is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// 6502js But C is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with 6502js But C. If not, see <https://www.gnu.org/licenses/>.
#include "os.h"
#ifdef WIN32
#include <windows.h>
#include <stdio.h>
#define CLASS_NAME "WMainClass"
#define WINDOW_STYLE (WS_OVERLAPPEDWINDOW & ~WS_SIZEBOX)
// Storing stuff from WinMain so can pass into CreateWindowEx
HINSTANCE hInstanceStored;
int nCmdShowStored;
HWND windowHandle;
HBRUSH *brushes = NULL;
bool shouldExit = false;
LRESULT CALLBACK WindowProc(HWND windowHandle, UINT uMsg, WPARAM wParam,
LPARAM lParam) {
// Main-window-specific events
switch (uMsg) {
case WM_DESTROY: {
PostQuitMessage(0); // Needed to quit... not sure why
break;
}
}
return DefWindowProc(windowHandle, uMsg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR pCmdLine, int nCmdShow) {
hInstanceStored = hInstance;
nCmdShowStored = nCmdShow;
return our_main(__argc, __argv);
}
void os_create_window(const char *name, int width, int height) {
// Register class
WNDCLASS winClass = {
.lpfnWndProc = WindowProc,
.hInstance = hInstanceStored,
.lpszClassName = CLASS_NAME,
.hbrBackground = CreateSolidBrush(RGB(0, 0, 0))
};
RegisterClass(&winClass);
// Calculate window size
RECT winSize = { 0, 0, width, height };
AdjustWindowRect(&winSize, WINDOW_STYLE, false);
// Create window
windowHandle = CreateWindowEx(0, CLASS_NAME, name,
WINDOW_STYLE, CW_USEDEFAULT, CW_USEDEFAULT,
winSize.right - winSize.left, winSize.bottom - winSize.top,
NULL, NULL, hInstanceStored, NULL);
ShowWindow(windowHandle, nCmdShowStored);
}
void os_create_colormap(const float *rgb, int length) {
brushes = malloc(sizeof(HBRUSH) * length);
for (int i = 0; i < length; i++) {
unsigned char r = rgb[i * 3 + 0] * 255;
unsigned char g = rgb[i * 3 + 1] * 255;
unsigned char b = rgb[i * 3 + 2] * 255;
brushes[i] = CreateSolidBrush(RGB(r, g, b));
}
}
bool os_choose_bin(char* path, int pathLength) {
OPENFILENAME ofn = {0};
path[0] = 0;
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = windowHandle;
ofn.lpstrFile = path;
ofn.nMaxFile = pathLength;
ofn.lpstrFilter = "Binary Files\0*.BIN\0";
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
if (GetOpenFileName(&ofn)) {
return true;
}
else {
printf("OPENFILENAME Problem: %lu\n", CommDlgExtendedError());
return false;
}
}
bool os_should_exit(void) {
return shouldExit; // Set in os_poll_event
}
bool os_poll_event(struct event *ev) {
MSG msg;
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
// Handle event
switch (msg.message) {
case WM_QUIT: {
shouldExit = true;
return false; // Not processed by event loop
}
case WM_PAINT: {
ev->type = ET_EXPOSE;
return true;
}
// FIXME: Creates a 1-event-poll (usually 1-frame) delay for input
case WM_CHAR: {
ev->type = ET_KEYPRESS;
ev->kp_key = msg.wParam;
return true;
}
default: {
return false;
}
}
}
return false;
}
void os_draw_rect(int x, int y, int w, int h, const float* rgb, int color) {
// NOTE: If too slow, extract out into separate function.
HDC hdc = GetDC(windowHandle);
RECT rect = { x, y, x + w, y + h };
FillRect(hdc, &rect, brushes[color]);
ReleaseDC(windowHandle, hdc);
}
void os_present(void) {
}
void os_close() {
free(brushes);
}
#endif