comparison src/video/win32/SDL_win32opengl.c @ 3057:089a77aebb7d

Added test program for SDL_CreateWindowFrom() Make sure OpenGL library is loaded before working with OpenGL windows, even those created with SDL_CreateWindowFrom()
author Sam Lantinga <slouken@libsdl.org>
date Mon, 09 Feb 2009 05:32:12 +0000
parents 99210400e8b9
children 7dc982143c06
comparison
equal deleted inserted replaced
3056:a434fe6360df 3057:089a77aebb7d
35 WIN_GL_LoadLibrary(_THIS, const char *path) 35 WIN_GL_LoadLibrary(_THIS, const char *path)
36 { 36 {
37 LPTSTR wpath; 37 LPTSTR wpath;
38 HANDLE handle; 38 HANDLE handle;
39 39
40 if (_this->gl_config.driver_loaded) {
41 if (path) {
42 SDL_SetError("OpenGL library already loaded");
43 return -1;
44 } else {
45 ++_this->gl_config.driver_loaded;
46 return 0;
47 }
48 }
49 if (path == NULL) { 40 if (path == NULL) {
50 path = SDL_getenv("SDL_OPENGL_LIBRARY"); 41 path = SDL_getenv("SDL_OPENGL_LIBRARY");
51 } 42 }
52 if (path == NULL) { 43 if (path == NULL) {
53 path = DEFAULT_OPENGL; 44 path = DEFAULT_OPENGL;
54 } 45 }
55 wpath = WIN_UTF8ToString(path); 46 wpath = WIN_UTF8ToString(path);
56 handle = LoadLibrary(wpath); 47 _this->gl_config.dll_handle = LoadLibrary(wpath);
57 SDL_free(wpath); 48 SDL_free(wpath);
58 if (!handle) { 49 if (!_this->gl_config.dll_handle) {
59 char message[1024]; 50 char message[1024];
60 SDL_snprintf(message, SDL_arraysize(message), "LoadLibrary(\"%s\")", 51 SDL_snprintf(message, SDL_arraysize(message), "LoadLibrary(\"%s\")",
61 path); 52 path);
62 WIN_SetError(message); 53 WIN_SetError(message);
63 return -1; 54 return -1;
64 } 55 }
56 SDL_strlcpy(_this->gl_config.driver_path, path,
57 SDL_arraysize(_this->gl_config.driver_path));
58
59 /* Allocate OpenGL memory */
60 _this->gl_data =
61 (struct SDL_GLDriverData *) SDL_calloc(1,
62 sizeof(struct
63 SDL_GLDriverData));
64 if (!_this->gl_data) {
65 SDL_OutOfMemory();
66 return -1;
67 }
65 68
66 /* Load function pointers */ 69 /* Load function pointers */
70 handle = _this->gl_config.dll_handle;
67 _this->gl_data->wglGetProcAddress = (void *(WINAPI *) (const char *)) 71 _this->gl_data->wglGetProcAddress = (void *(WINAPI *) (const char *))
68 GetProcAddress(handle, "wglGetProcAddress"); 72 GetProcAddress(handle, "wglGetProcAddress");
69 _this->gl_data->wglCreateContext = (HGLRC(WINAPI *) (HDC)) 73 _this->gl_data->wglCreateContext = (HGLRC(WINAPI *) (HDC))
70 GetProcAddress(handle, "wglCreateContext"); 74 GetProcAddress(handle, "wglCreateContext");
71 _this->gl_data->wglDeleteContext = (BOOL(WINAPI *) (HGLRC)) 75 _this->gl_data->wglDeleteContext = (BOOL(WINAPI *) (HGLRC))
84 SDL_SetError("Could not retrieve OpenGL functions"); 88 SDL_SetError("Could not retrieve OpenGL functions");
85 FreeLibrary(handle); 89 FreeLibrary(handle);
86 return -1; 90 return -1;
87 } 91 }
88 92
89 _this->gl_config.dll_handle = handle;
90 SDL_strlcpy(_this->gl_config.driver_path, path,
91 SDL_arraysize(_this->gl_config.driver_path));
92 _this->gl_config.driver_loaded = 1;
93 return 0; 93 return 0;
94 } 94 }
95 95
96 void * 96 void *
97 WIN_GL_GetProcAddress(_THIS, const char *proc) 97 WIN_GL_GetProcAddress(_THIS, const char *proc)
105 func = GetProcAddress(_this->gl_config.dll_handle, proc); 105 func = GetProcAddress(_this->gl_config.dll_handle, proc);
106 } 106 }
107 return func; 107 return func;
108 } 108 }
109 109
110 static void 110 void
111 WIN_GL_UnloadLibrary(_THIS) 111 WIN_GL_UnloadLibrary(_THIS)
112 { 112 {
113 if (_this->gl_config.driver_loaded > 0) { 113 FreeLibrary((HMODULE) _this->gl_config.dll_handle);
114 if (--_this->gl_config.driver_loaded > 0) { 114 _this->gl_config.dll_handle = NULL;
115 return; 115
116 } 116 /* Free OpenGL memory */
117 FreeLibrary((HMODULE) _this->gl_config.dll_handle); 117 SDL_free(_this->gl_data);
118 _this->gl_config.dll_handle = NULL; 118 _this->gl_data = NULL;
119 }
120 } 119 }
121 120
122 static void 121 static void
123 WIN_GL_SetupPixelFormat(_THIS, PIXELFORMATDESCRIPTOR * pfd) 122 WIN_GL_SetupPixelFormat(_THIS, PIXELFORMATDESCRIPTOR * pfd)
124 { 123 {
376 WIN_PumpEvents(_this); 375 WIN_PumpEvents(_this);
377 376
378 return pixel_format; 377 return pixel_format;
379 } 378 }
380 379
381 static int
382 WIN_GL_Initialize(_THIS)
383 {
384 if (_this->gl_data) {
385 ++_this->gl_data->initialized;
386 return 0;
387 }
388
389 _this->gl_data =
390 (struct SDL_GLDriverData *) SDL_calloc(1,
391 sizeof(struct
392 SDL_GLDriverData));
393 if (!_this->gl_data) {
394 SDL_OutOfMemory();
395 return -1;
396 }
397 _this->gl_data->initialized = 1;
398
399 if (WIN_GL_LoadLibrary(_this, NULL) < 0) {
400 return -1;
401 }
402
403 return 0;
404 }
405
406 static void
407 WIN_GL_Shutdown(_THIS)
408 {
409 if (!_this->gl_data || (--_this->gl_data->initialized > 0)) {
410 return;
411 }
412
413 WIN_GL_UnloadLibrary(_this);
414
415 SDL_free(_this->gl_data);
416 _this->gl_data = NULL;
417 }
418
419 int 380 int
420 WIN_GL_SetupWindow(_THIS, SDL_Window * window) 381 WIN_GL_SetupWindow(_THIS, SDL_Window * window)
421 { 382 {
422 HDC hdc = ((SDL_WindowData *) window->driverdata)->hdc; 383 HDC hdc = ((SDL_WindowData *) window->driverdata)->hdc;
423 PIXELFORMATDESCRIPTOR pfd; 384 PIXELFORMATDESCRIPTOR pfd;
424 int pixel_format; 385 int pixel_format;
425 int iAttribs[64]; 386 int iAttribs[64];
426 int *iAttr; 387 int *iAttr;
427 float fAttribs[1] = { 0 }; 388 float fAttribs[1] = { 0 };
428
429 if (WIN_GL_Initialize(_this) < 0) {
430 return -1;
431 }
432 389
433 WIN_GL_SetupPixelFormat(_this, &pfd); 390 WIN_GL_SetupPixelFormat(_this, &pfd);
434 391
435 /* setup WGL_ARB_pixel_format attribs */ 392 /* setup WGL_ARB_pixel_format attribs */
436 iAttr = &iAttribs[0]; 393 iAttr = &iAttribs[0];
520 return (-1); 477 return (-1);
521 } 478 }
522 return 0; 479 return 0;
523 } 480 }
524 481
525 void
526 WIN_GL_CleanupWindow(_THIS, SDL_Window * window)
527 {
528 WIN_GL_Shutdown(_this);
529 }
530
531 SDL_GLContext 482 SDL_GLContext
532 WIN_GL_CreateContext(_THIS, SDL_Window * window) 483 WIN_GL_CreateContext(_THIS, SDL_Window * window)
533 { 484 {
534 HDC hdc = ((SDL_WindowData *) window->driverdata)->hdc; 485 HDC hdc = ((SDL_WindowData *) window->driverdata)->hdc;
535 HGLRC context; 486 HGLRC context;