5#include <cgv/utils/convert.h>
14typedef std::basic_string<_TCHAR> tstring;
16struct win32_gl_context :
public gl_context
33 static LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
35 bool init_application();
36 bool ensure_init_application();
37 bool init_instance(
const tstring& title,
int);
38 bool set_pixel_format();
39 bool create(
const std::string& title,
bool show);
41 win32_gl_context(
unsigned int w = -1,
unsigned int h = -1);
49 void render_pass(RenderPass render_pass = RP_MAIN,
50 RenderPassFlags render_pass_flags = RPF_ALL) {}
54 void set_default_render_pass_flags(RenderPassFlags) { }
56 bool in_render_process()
const {
return false; }
58 bool is_created()
const {
return true; }
60 bool is_current()
const {
return true; }
62 bool make_current()
const;
64 void clear_current()
const;
69 unsigned int get_width()
const {
return width; }
71 unsigned int get_height()
const {
return height; }
73 void resize(
unsigned int width,
unsigned int height) { }
75 void set_bg_color(
float r,
float g,
float b,
float a) {}
79 void force_redraw() {}
80 void attach_depth_buffer(
bool) {}
81 void attach_alpha_buffer(
bool) {}
82 void attach_stencil_buffer(
bool) {}
83 bool is_stereo_buffer_supported()
const {
return false; }
84 void attach_stereo_buffer(
bool) {}
85 void attach_accumulation_buffer(
bool) {}
86 void attach_multi_sample_buffer(
bool) {}
93 float get_current_font_size()
const {
return 12; }
99 std::ostream& output_stream() {
return std::cout; }
125bool win32_gl_context::init_application()
130 wcx.cbSize =
sizeof(wcx);
131 wcx.style = CS_HREDRAW |
133 wcx.lpfnWndProc = MainWndProc;
136 wcx.hInstance = hinst;
137 wcx.hIcon = LoadIcon(NULL,
139 wcx.hCursor = LoadCursor(NULL,
141 wcx.hbrBackground = NULL;
142 wcx.lpszMenuName = _T(
"MainMenu");
143 wcx.lpszClassName = _T(
"MainWClass");
148 ATOM atom = RegisterClassEx(&wcx);
152bool win32_gl_context::init_instance(
const tstring& title,
int nCmdShow)
161 width == -1 ? CW_USEDEFAULT : width,
162 height == -1 ? CW_USEDEFAULT : height,
172 ShowWindow(hwnd, nCmdShow);
177LRESULT CALLBACK win32_gl_context::MainWndProc(
206 return DefWindowProc(hwnd, uMsg, wParam, lParam);
212bool win32_gl_context::make_current()
const
216 if (!wglMakeCurrent(hdc,glc)) {
217 DWORD error = GetLastError();
218 std::cerr <<
"failed to make current [hdc=" << hdc <<
", glc=" << glc <<
"] with error " << error << std::endl;
224void win32_gl_context::clear_current()
const
226 wglMakeCurrent(NULL,NULL);
229win32_gl_context::win32_gl_context(
unsigned int w,
unsigned int h)
239win32_gl_context::~win32_gl_context()
241 wglMakeCurrent(NULL,NULL);
249bool win32_gl_context::set_pixel_format()
251 PIXELFORMATDESCRIPTOR pfd = {
252 sizeof(PIXELFORMATDESCRIPTOR),
274 iPixelFormat = ChoosePixelFormat(hdc, &pfd);
277 return SetPixelFormat(hdc, iPixelFormat, &pfd) == TRUE;
280bool win32_gl_context::ensure_init_application()
282 static bool is_initialized =
false;
283 static bool result =
false;
284 if (!is_initialized) {
285 result = init_application();
286 is_initialized =
true;
291bool win32_gl_context::create(
const std::string& title,
bool show)
293 hinst = GetModuleHandle(NULL);
294 if (!ensure_init_application()) {
295 std::cout <<
"failed to init application" << std::endl;
299 if (!init_instance(str2wstr(title), show ? SW_SHOW : SW_HIDE)) {
301 if (!init_instance(title, show ? SW_SHOW : SW_HIDE)) {
303 std::cout <<
"failed to init instance" << std::endl;
308 std::cout <<
"failed to get DC" << std::endl;
311 if (!set_pixel_format()) {
312 std::cout <<
"failed to choose pixel format" << std::endl;
315 glc = wglCreateContext(hdc);
317 std::cout <<
"failed to create gl context" << std::endl;
323context* create_win32_gl_context(
RenderAPI api,
unsigned int w,
unsigned int h,
324 const std::string& title,
bool show)
326 if (api != RA_OPENGL)
328 win32_gl_context* ctx_ptr =
new win32_gl_context(w,h);
338context_factory_registration create_win32_gl_context_registration(gl::create_win32_gl_context);
bool ensure_glew_initialized()
initialize glew in the first call to this function and always return whether this was successful
RenderAPI
enumeration of rendering APIs which can be queried from the context
RenderPass
Enumeration of different render passes, which can be queried from the context and used to specify a n...
RenderPassFlags
available flags that can be queried from the context and set for a new render pass
@ RPF_NONE
no frame initialization is performed
namespace that holds tools that dont fit any other namespace