fixed build on windows
[gph-gfx] / test_win.c
1 #include <stdio.h>
2 #include <windows.h>
3 #include "gphgfx.h"
4
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);
11
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);
16
17 static HWND win;
18 static HINSTANCE hinst;
19 static HDC dc;
20 static int win_width, win_height;
21 static int quit;
22
23 int WINAPI WinMain(HINSTANCE hinst_, HINSTANCE hprev, char *cmdline, int showcmd)
24 {
25         WNDCLASSEX wc = {0};
26         MSG msg;
27
28         hinst = hinst_;
29
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);
34         wc.hInstance = hinst;
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");
40                 return 1;
41         }
42
43         if(create_window("gph-gfx test", 800, 600) == -1) {
44                 UnregisterClass("gphgfxtest", hinst);
45                 return 1;
46         }
47
48         for(;;) {
49                 while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
50                         TranslateMessage(&msg);
51                         DispatchMessage(&msg);
52                         if(quit) goto end;
53                 }
54                 redraw();
55         }
56 end:
57
58         ggfx_shutdown();
59         ReleaseDC(win, dc);
60         DestroyWindow(win);
61         UnregisterClass("gphgfxtest", hinst);
62         return 0;
63 }
64
65
66 static void redraw(void)
67 {
68         RECT rect;
69         HBRUSH brush;
70
71         rect.left = rect.top = 0;
72         rect.right = win_width;
73         rect.bottom = win_height;
74
75         brush = CreateSolidBrush(RGB(128, 0, 0));
76
77         FillRect(dc, &rect, brush);
78 }
79
80 static void reshape(int x, int y)
81 {
82 }
83
84 static void keydown(int key, int x, int y)
85 {
86         switch(key) {
87         case 27:
88                 quit = 1;
89                 break;
90         }
91 }
92
93 static void keyup(int key, int x, int y)
94 {
95 }
96
97 static void button(int bn, int st, int x, int y)
98 {
99 }
100
101 static void motion(int x, int y)
102 {
103 }
104
105 static int create_window(const char *title, int width, int height)
106 {
107         RECT rect;
108
109         rect.left = rect.top = 0;
110         rect.right = width;
111         rect.bottom = height;
112         AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, 0);
113
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");
117                 return -1;
118         }
119         dc = GetDC(win);
120
121         ShowWindow(win, 1);
122         return 0;
123 }
124
125 static HRESULT CALLBACK handle_message(HWND win, unsigned int msg, WPARAM wparam, LPARAM lparam)
126 {
127         static int mouse_x, mouse_y;
128         int x, y, key;
129
130         switch(msg) {
131         case WM_CLOSE:
132                 quit = 1;
133                 PostQuitMessage(0);
134                 break;
135
136         case WM_PAINT:
137                 /*upd_pending = 1;*/
138                 ValidateRect(win, 0);
139                 break;
140
141         case WM_SIZE:
142                 x = lparam & 0xffff;
143                 y = lparam >> 16;
144                 if(x != win_width && y != win_height) {
145                         win_width = x;
146                         win_height = y;
147                         reshape(win_width, win_height);
148                 }
149                 break;
150
151         case WM_KEYDOWN:
152         case WM_SYSKEYDOWN:
153                 key = translate_vkey(wparam);
154                 keydown(key, mouse_x, mouse_y);
155                 break;
156
157         case WM_KEYUP:
158         case WM_SYSKEYUP:
159                 key = translate_vkey(wparam);
160                 keyup(key, mouse_x, mouse_y);
161                 break;
162
163         case WM_LBUTTONDOWN:
164                 handle_mbutton(0, 1, wparam, lparam);
165                 break;
166         case WM_MBUTTONDOWN:
167                 handle_mbutton(1, 1, wparam, lparam);
168                 break;
169         case WM_RBUTTONDOWN:
170                 handle_mbutton(2, 1, wparam, lparam);
171                 break;
172         case WM_LBUTTONUP:
173                 handle_mbutton(0, 0, wparam, lparam);
174                 break;
175         case WM_MBUTTONUP:
176                 handle_mbutton(1, 0, wparam, lparam);
177                 break;
178         case WM_RBUTTONUP:
179                 handle_mbutton(2, 0, wparam, lparam);
180                 break;
181
182         case WM_MOUSEMOVE:
183                 motion(lparam & 0xffff, lparam >> 16);
184                 break;
185
186         case WM_SYSCOMMAND:
187                 wparam &= 0xfff0;
188                 if(wparam == SC_KEYMENU || wparam == SC_SCREENSAVE || wparam == SC_MONITORPOWER) {
189                         return 0;
190                 }
191         default:
192                 return DefWindowProc(win, msg, wparam, lparam);
193         }
194
195         return 0;
196 }
197
198
199 static int translate_vkey(int vkey)
200 {
201         if(vkey >= 'A' && vkey <= 'Z') {
202                 vkey += 32;
203         }
204
205         return vkey;
206 }
207
208 static void handle_mbutton(int bn, int st, WPARAM wparam, LPARAM lparam)
209 {
210         int x = lparam & 0xffff;
211         int y = lparam >> 16;
212         button(bn, st, x, y);
213 }