f1e75a5f548f1abb12b52553b5fe8077c2ceaba7
[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
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(dc);
60         DestroyWindow(win);
61         UnregisterClass("gphgfxtest", hinst);
62         return 0;
63 }
64
65 static int create_window(const char *title, int width, int height)
66 {
67         if(!(win = CreateWindow("gphgfxtest", title, WS_OVERLAPPEDWINDOW, 0, 0, width,
68                                         height, 0, 0, hinst, 0))) {
69                 fprintf(stderr, "failed to create window\n");
70                 return -1;
71         }
72         dc = GetDC(win);
73
74         ShowWindow(win, 1);
75         return 0;
76 }
77
78 static HRESULT CALLBACK handle_message(HWND win, unsigned int msg, WPARAM wparam, LPARAM lparam)
79 {
80         static int mouse_x, mouse_y;
81         int x, y, key;
82
83         switch(msg) {
84         case WM_CLOSE:
85                 quit = 1;
86                 PostQuitMessage(0);
87                 break;
88
89         case WM_PAINT:
90                 /*upd_pending = 1;*/
91                 ValidateRect(win, 0);
92                 break;
93
94         case WM_SIZE:
95                 x = lparam & 0xffff;
96                 y = lparam >> 16;
97                 if(x != win_width && y != win_height) {
98                         win_width = x;
99                         win_height = y;
100                         reshape(win_width, win_height);
101                 }
102                 break;
103
104         case WM_KEYDOWN:
105         case WM_SYSKEYDOWN:
106                 key = translate_vkey(wparam);
107                 keydown(key, mouse_x, mouse_y);
108                 break;
109
110         case WM_KEYUP:
111         case WM_SYSKEYUP:
112                 key = translate_vkey(wparam);
113                 keyup(key, mouse_x, mouse_y);
114                 break;
115
116         case WM_LBUTTONDOWN:
117                 handle_mbutton(0, 1, wparam, lparam);
118                 break;
119         case WM_MBUTTONDOWN:
120                 handle_mbutton(1, 1, wparam, lparam);
121                 break;
122         case WM_RBUTTONDOWN:
123                 handle_mbutton(2, 1, wparam, lparam);
124                 break;
125         case WM_LBUTTONUP:
126                 handle_mbutton(0, 0, wparam, lparam);
127                 break;
128         case WM_MBUTTONUP:
129                 handle_mbutton(1, 0, wparam, lparam);
130                 break;
131         case WM_RBUTTONUP:
132                 handle_mbutton(2, 0, wparam, lparam);
133                 break;
134
135         case WM_MOUSEMOVE:
136                 motion(lparam & 0xffff, lparam >> 16);
137                 break;
138
139         case WM_SYSCOMMAND:
140                 wparam &= 0xfff0;
141                 if(wparam == SC_KEYMENU || wparam == SC_SCREENSAVE || wparam == SC_MONITORPOWER) {
142                         return 0;
143                 }
144         default:
145                 return DefWindowProc(win, msg, wparam, lparam);
146         }
147
148         return 0;
149 }
150
151
152 static int translate_vkey(int vkey)
153 {
154         if(vkey >= 'A' && vkey <= 'Z') {
155                 vkey += 32;
156         }
157
158         return vkey;
159 }
160
161 static void handle_mbutton(int bn, int st, WPARAM wparam, LPARAM lparam)
162 {
163         int x = lparam & 0xffff;
164         int y = lparam >> 16;
165         mouse(bn, st, x, y);
166 }