1 // Copyright 2014 Emil Velikov
3 // All rights reserved.
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
8 // - Redistributions of source code must retain the above copyright notice, this
9 // list of conditions and the following disclaimer.
11 // - Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "wcore_error.h"
31 #include "wgl_config.h"
32 #include "wgl_context.h"
33 #include "wgl_display.h"
35 #include "wgl_platform.h"
36 #include "wgl_window.h"
38 static const struct wcore_platform_vtbl wgl_platform_vtbl
;
40 const char* wfl_class_name
= "waffle";
43 wgl_platform_destroy(struct wcore_platform
*wc_self
)
45 struct wgl_platform
*self
= wgl_platform(wc_self
);
52 ok
&= wgl_dl_close(wc_self
);
55 ok
&= UnregisterClass(self
->class_name
, GetModuleHandle(NULL
));
57 ok
&= wcore_platform_teardown(wc_self
);
63 wgl_platform_register_class(const char* class_name
)
68 memset(&wc
, 0, sizeof(wc
));
69 wc
.style
= CS_OWNDC
| CS_HREDRAW
| CS_VREDRAW
;
70 // XXX: Use a non-default window_proc ?
71 wc
.lpfnWndProc
= DefWindowProc
;
72 wc
.hIcon
= LoadIcon(NULL
, IDI_APPLICATION
);
73 wc
.hInstance
= GetModuleHandle(NULL
);
74 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
75 wc
.hbrBackground
= (HBRUSH
) (COLOR_BTNFACE
+ 1);;
76 wc
.lpszClassName
= class_name
;
78 ok
= !!RegisterClass(&wc
);
81 int error
= GetLastError();
84 wcore_errorf(WAFFLE_ERROR_UNKNOWN
,
85 "RegisterClass() failed: %d",
93 struct wcore_platform
*
94 wgl_platform_create(void)
96 struct wgl_platform
*self
;
99 self
= wcore_calloc(sizeof(*self
));
103 ok
= wcore_platform_init(&self
->wcore
);
107 ok
= wgl_platform_register_class(wfl_class_name
);
110 self
->class_name
= wfl_class_name
;
112 self
->wcore
.vtbl
= &wgl_platform_vtbl
;
116 wgl_platform_destroy(&self
->wcore
);
121 wgl_make_current(struct wcore_platform
*wc_self
,
122 struct wcore_display
*wc_dpy
,
123 struct wcore_window
*wc_window
,
124 struct wcore_context
*wc_ctx
)
126 HDC hDC
= wc_window
? wgl_window(wc_window
)->hDC
: NULL
;
127 HGLRC hglrc
= wc_ctx
? wgl_context(wc_ctx
)->hglrc
: NULL
;
129 return wglMakeCurrent(hDC
, hglrc
);
133 wgl_get_proc_address(struct wcore_platform
*wc_self
, const char *name
)
135 return wglGetProcAddress(name
);
138 static const struct wcore_platform_vtbl wgl_platform_vtbl
= {
139 .destroy
= wgl_platform_destroy
,
141 .make_current
= wgl_make_current
,
142 .get_proc_address
= wgl_get_proc_address
,
143 .dl_can_open
= wgl_dl_can_open
,
144 .dl_sym
= wgl_dl_sym
,
147 .connect
= wgl_display_connect
,
148 .destroy
= wgl_display_destroy
,
149 .supports_context_api
= wgl_display_supports_context_api
,
154 .choose
= wgl_config_choose
,
155 .destroy
= wgl_config_destroy
,
160 .create
= wgl_context_create
,
161 .destroy
= wgl_context_destroy
,
166 .create
= wgl_window_create
,
167 .destroy
= wgl_window_destroy
,
168 .show
= wgl_window_show
,
169 .resize
= wgl_window_resize
,
170 .swap_buffers
= wgl_window_swap_buffers
,