5 static void redraw(void);
6 static void reshape(int x, int y);
7 static void keydown(int key, int x, int y);
8 static void keyup(int key, int x, int y);
9 static void button(int bn, int st, int x, int y);
10 static void motion(int x, int y);
12 static int create_window(const char *title, int width, int height);
13 static HRESULT CALLBACK handle_message(HWND win, unsigned int msg, WPARAM wparam, LPARAM lparam);
14 static int translate_vkey(int vkey);
15 static void handle_mbutton(int bn, int st, WPARAM wparam, LPARAM lparam);
18 static HINSTANCE hinst;
20 static int win_width, win_height;
23 int WINAPI WinMain(HINSTANCE hinst_, HINSTANCE hprev, char *cmdline, int showcmd)
30 wc.cbSize = sizeof wc;
31 wc.hbrBackground = GetStockObject(BLACK_BRUSH);
32 wc.hCursor = LoadCursor(0, IDC_ARROW);
33 wc.hIcon = wc.hIconSm = LoadIcon(0, IDI_APPLICATION);
35 wc.lpfnWndProc = handle_message;
36 wc.lpszClassName = "gphgfxtest";
37 wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
38 if(!RegisterClassEx(&wc)) {
39 fprintf(stderr, "failed to register window class\n");
43 if(create_window("gph-gfx test", 800, 600) == -1) {
44 UnregisterClass("gphgfxtest", hinst);
49 while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
50 TranslateMessage(&msg);
51 DispatchMessage(&msg);
61 UnregisterClass("gphgfxtest", hinst);
66 static void redraw(void)
71 rect.left = rect.top = 0;
72 rect.right = win_width;
73 rect.bottom = win_height;
75 brush = CreateSolidBrush(RGB(128, 0, 0));
77 FillRect(dc, &rect, brush);
80 static void reshape(int x, int y)
84 static void keydown(int key, int x, int y)
93 static void keyup(int key, int x, int y)
97 static void button(int bn, int st, int x, int y)
101 static void motion(int x, int y)
105 static int create_window(const char *title, int width, int height)
109 rect.left = rect.top = 0;
111 rect.bottom = height;
112 AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, 0);
114 if(!(win = CreateWindow("gphgfxtest", title, WS_OVERLAPPEDWINDOW, 0, 0,
115 rect.right - rect.left, rect.bottom - rect.top, 0, 0, hinst, 0))) {
116 fprintf(stderr, "failed to create window\n");
125 static HRESULT CALLBACK handle_message(HWND win, unsigned int msg, WPARAM wparam, LPARAM lparam)
127 static int mouse_x, mouse_y;
138 ValidateRect(win, 0);
144 if(x != win_width && y != win_height) {
147 reshape(win_width, win_height);
153 key = translate_vkey(wparam);
154 keydown(key, mouse_x, mouse_y);
159 key = translate_vkey(wparam);
160 keyup(key, mouse_x, mouse_y);
164 handle_mbutton(0, 1, wparam, lparam);
167 handle_mbutton(1, 1, wparam, lparam);
170 handle_mbutton(2, 1, wparam, lparam);
173 handle_mbutton(0, 0, wparam, lparam);
176 handle_mbutton(1, 0, wparam, lparam);
179 handle_mbutton(2, 0, wparam, lparam);
183 motion(lparam & 0xffff, lparam >> 16);
188 if(wparam == SC_KEYMENU || wparam == SC_SCREENSAVE || wparam == SC_MONITORPOWER) {
192 return DefWindowProc(win, msg, wparam, lparam);
199 static int translate_vkey(int vkey)
201 if(vkey >= 'A' && vkey <= 'Z') {
208 static void handle_mbutton(int bn, int st, WPARAM wparam, LPARAM lparam)
210 int x = lparam & 0xffff;
211 int y = lparam >> 16;
212 button(bn, st, x, y);