src/video/win32/SDL_win32video.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 09 Feb 2009 05:32:12 +0000
changeset 3057 089a77aebb7d
parent 3013 8cc00819c8d6
child 3168 6338b7f2d024
permissions -rw-r--r--
Added test program for SDL_CreateWindowFrom()
Make sure OpenGL library is loaded before working with OpenGL windows,
even those created with SDL_CreateWindowFrom()
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2009 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 #include "SDL_main.h"
    25 #include "SDL_video.h"
    26 #include "SDL_mouse.h"
    27 #include "../SDL_sysvideo.h"
    28 #include "../SDL_pixels_c.h"
    29 
    30 #include "SDL_win32video.h"
    31 #include "SDL_d3drender.h"
    32 #include "SDL_gdirender.h"
    33 
    34 /* Initialization/Query functions */
    35 static int WIN_VideoInit(_THIS);
    36 static void WIN_VideoQuit(_THIS);
    37 
    38 int total_mice = 0;             /* total mouse count */
    39 HANDLE *mice = NULL;            /* the handles to the detected mice */
    40 HCTX *g_hCtx = NULL;            /* handles to tablet contexts */
    41 int tablet = -1;                /* we're assuming that there is no tablet */
    42 
    43 /* WIN32 driver bootstrap functions */
    44 
    45 static int
    46 WIN_Available(void)
    47 {
    48     return (1);
    49 }
    50 
    51 static void
    52 WIN_DeleteDevice(SDL_VideoDevice * device)
    53 {
    54     SDL_VideoData *data = (SDL_VideoData *) device->driverdata;
    55 
    56     SDL_UnregisterApp();
    57 #if SDL_VIDEO_RENDER_D3D
    58     if (data->d3d) {
    59         IDirect3D9_Release(data->d3d);
    60         FreeLibrary(data->d3dDLL);
    61     }
    62 #endif
    63     if (data->wintabDLL) {
    64         FreeLibrary(data->wintabDLL);
    65     }
    66     SDL_free(device->driverdata);
    67     SDL_free(device);
    68 }
    69 
    70 static SDL_VideoDevice *
    71 WIN_CreateDevice(int devindex)
    72 {
    73     SDL_VideoDevice *device;
    74     SDL_VideoData *data;
    75 
    76     SDL_RegisterApp(NULL, 0, NULL);
    77 
    78     /* Initialize all variables that we clean on shutdown */
    79     device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
    80     if (device) {
    81         data = (struct SDL_VideoData *) SDL_calloc(1, sizeof(SDL_VideoData));
    82     }
    83     if (!device || !data) {
    84         SDL_OutOfMemory();
    85         if (device) {
    86             SDL_free(device);
    87         }
    88         return NULL;
    89     }
    90     device->driverdata = data;
    91 
    92 #if SDL_VIDEO_RENDER_D3D
    93     data->d3dDLL = LoadLibrary(TEXT("D3D9.DLL"));
    94     if (data->d3dDLL) {
    95         IDirect3D9 *(WINAPI * D3DCreate) (UINT SDKVersion);
    96 
    97         D3DCreate =
    98             (IDirect3D9 * (WINAPI *) (UINT)) GetProcAddress(data->d3dDLL,
    99                                                             "Direct3DCreate9");
   100         if (D3DCreate) {
   101             data->d3d = D3DCreate(D3D_SDK_VERSION);
   102         }
   103         if (!data->d3d) {
   104             FreeLibrary(data->d3dDLL);
   105             data->d3dDLL = NULL;
   106         }
   107     }
   108 #endif /* SDL_VIDEO_RENDER_D3D */
   109 
   110     data->wintabDLL = LoadLibrary(TEXT("WINTAB32.DLL"));
   111     if (data->wintabDLL) {
   112 #define PROCNAME(X) #X
   113         data->WTInfoA =
   114             (UINT(*)(UINT, UINT, LPVOID)) GetProcAddress(data->wintabDLL,
   115                                                          PROCNAME(WTInfoA));
   116         data->WTOpenA =
   117             (HCTX(*)(HWND, LPLOGCONTEXTA, BOOL)) GetProcAddress(data->
   118                                                                 wintabDLL,
   119                                                                 PROCNAME
   120                                                                 (WTOpenA));
   121         data->WTPacket =
   122             (int (*)(HCTX, UINT, LPVOID)) GetProcAddress(data->wintabDLL,
   123                                                          PROCNAME(WTPacket));
   124         data->WTClose =
   125             (BOOL(*)(HCTX)) GetProcAddress(data->wintabDLL,
   126                                            PROCNAME(WTClose));
   127 #undef PROCNAME
   128 
   129         if (!data->WTInfoA || !data->WTOpenA || !data->WTPacket
   130             || !data->WTClose) {
   131             FreeLibrary(data->wintabDLL);
   132             data->wintabDLL = NULL;
   133         }
   134     }
   135 
   136     /* Set the function pointers */
   137     device->VideoInit = WIN_VideoInit;
   138     device->VideoQuit = WIN_VideoQuit;
   139     device->GetDisplayModes = WIN_GetDisplayModes;
   140     device->SetDisplayMode = WIN_SetDisplayMode;
   141     device->SetDisplayGammaRamp = WIN_SetDisplayGammaRamp;
   142     device->GetDisplayGammaRamp = WIN_GetDisplayGammaRamp;
   143     device->PumpEvents = WIN_PumpEvents;
   144 
   145 #undef CreateWindow
   146     device->CreateWindow = WIN_CreateWindow;
   147     device->CreateWindowFrom = WIN_CreateWindowFrom;
   148     device->SetWindowTitle = WIN_SetWindowTitle;
   149     device->SetWindowIcon = WIN_SetWindowIcon;
   150     device->SetWindowPosition = WIN_SetWindowPosition;
   151     device->SetWindowSize = WIN_SetWindowSize;
   152     device->ShowWindow = WIN_ShowWindow;
   153     device->HideWindow = WIN_HideWindow;
   154     device->RaiseWindow = WIN_RaiseWindow;
   155     device->MaximizeWindow = WIN_MaximizeWindow;
   156     device->MinimizeWindow = WIN_MinimizeWindow;
   157     device->RestoreWindow = WIN_RestoreWindow;
   158     device->SetWindowGrab = WIN_SetWindowGrab;
   159     device->DestroyWindow = WIN_DestroyWindow;
   160     device->GetWindowWMInfo = WIN_GetWindowWMInfo;
   161 #ifdef SDL_VIDEO_OPENGL_WGL
   162     device->GL_LoadLibrary = WIN_GL_LoadLibrary;
   163     device->GL_GetProcAddress = WIN_GL_GetProcAddress;
   164     device->GL_UnloadLibrary = WIN_GL_UnloadLibrary;
   165     device->GL_CreateContext = WIN_GL_CreateContext;
   166     device->GL_MakeCurrent = WIN_GL_MakeCurrent;
   167     device->GL_SetSwapInterval = WIN_GL_SetSwapInterval;
   168     device->GL_GetSwapInterval = WIN_GL_GetSwapInterval;
   169     device->GL_SwapWindow = WIN_GL_SwapWindow;
   170     device->GL_DeleteContext = WIN_GL_DeleteContext;
   171 #endif
   172 
   173     device->free = WIN_DeleteDevice;
   174 
   175     return device;
   176 }
   177 
   178 VideoBootStrap WIN32_bootstrap = {
   179     "win32", "SDL Win32/64 video driver", WIN_Available, WIN_CreateDevice
   180 };
   181 
   182 
   183 int
   184 WIN_VideoInit(_THIS)
   185 {
   186     WIN_InitModes(_this);
   187 
   188 #if SDL_VIDEO_RENDER_D3D
   189     D3D_AddRenderDriver(_this);
   190 #endif
   191 #if SDL_VIDEO_RENDER_GDI
   192     GDI_AddRenderDriver(_this);
   193 #endif
   194 
   195     g_hCtx = SDL_malloc(sizeof(HCTX));
   196     WIN_InitKeyboard(_this);
   197     WIN_InitMouse(_this);
   198 
   199     return 0;
   200 }
   201 
   202 void
   203 WIN_VideoQuit(_THIS)
   204 {
   205     WIN_QuitModes(_this);
   206     WIN_QuitKeyboard(_this);
   207     WIN_QuitMouse(_this);
   208     SDL_free(g_hCtx);
   209 }
   210 
   211 /* vim: set ts=4 sw=4 expandtab: */