17ca2a761cabf61aef25902a31eee67af5c778bf
[mesa.git] / src / gallium / targets / graw-gdi / graw_gdi.c
1 /**************************************************************************
2 *
3 * Copyright 2010 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
25 *
26 *
27 **************************************************************************/
28
29 #include "gdi/gdi_sw_winsys.h"
30 #include "pipe/p_screen.h"
31 #include "state_tracker/graw.h"
32 #include "target-helpers/inline_debug_helper.h"
33 #include "target-helpers/inline_sw_helper.h"
34 #include <windows.h>
35
36
37 static LRESULT CALLBACK
38 window_proc(HWND hWnd,
39 UINT uMsg,
40 WPARAM wParam,
41 LPARAM lParam)
42 {
43 switch (uMsg) {
44 case WM_DESTROY:
45 PostQuitMessage(0);
46 break;
47
48 default:
49 return DefWindowProc(hWnd, uMsg, wParam, lParam);
50 }
51
52 return 0;
53 }
54
55 static struct {
56 void (* draw)(void);
57 } graw;
58
59 struct pipe_screen *
60 graw_create_window_and_screen(int x,
61 int y,
62 unsigned width,
63 unsigned height,
64 enum pipe_format format,
65 void **handle)
66 {
67 struct sw_winsys *winsys = NULL;
68 struct pipe_screen *screen = NULL;
69 WNDCLASSEX wc = {sizeof(wc)};
70 UINT style = WS_VISIBLE | WS_TILEDWINDOW;
71 RECT rect;
72 HWND hWnd = NULL;
73 HDC hDC = NULL;
74
75 if (format != PIPE_FORMAT_R8G8B8A8_UNORM)
76 goto fail;
77
78 winsys = gdi_create_sw_winsys();
79 if (winsys == NULL)
80 goto fail;
81
82 screen = sw_screen_create(winsys);
83 if (screen == NULL)
84 goto fail;
85
86 wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
87 wc.lpfnWndProc = window_proc;
88 wc.lpszClassName = "graw-gdi";
89 wc.hInstance = GetModuleHandle(NULL);
90 wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
91 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
92 wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
93 RegisterClassEx(&wc);
94
95 SetRect(&rect, 0, 0, width, height);
96 AdjustWindowRectEx(&rect, style, FALSE, 0);
97
98 hWnd = CreateWindowEx(0,
99 wc.lpszClassName,
100 wc.lpszClassName,
101 style,
102 x,
103 y,
104 rect.right - rect.left,
105 rect.bottom - rect.top,
106 NULL,
107 NULL,
108 wc.hInstance,
109 0);
110 if (hWnd == NULL)
111 goto fail;
112
113 hDC = GetDC(hWnd);
114 if (hDC == NULL)
115 goto fail;
116
117 *handle = (void *)hDC;
118
119 return debug_screen_wrap(screen);
120
121 fail:
122 if (hWnd)
123 DestroyWindow(hWnd);
124
125 if (screen)
126 screen->destroy(screen);
127
128 return NULL;
129 }
130
131 void
132 graw_set_display_func(void (* draw)(void))
133 {
134 graw.draw = draw;
135 }
136
137 void
138 graw_main_loop(void)
139 {
140 for (;;) {
141 MSG msg;
142
143 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
144 if (msg.message == WM_QUIT) {
145 return;
146 }
147 TranslateMessage(&msg);
148 DispatchMessage(&msg);
149 }
150
151 if (graw.draw) {
152 graw.draw();
153 }
154
155 Sleep(0);
156 }
157 }