src/video/android/SDL_androidvideo.c
author Gabriel Jacobo <gabomdq@gmail.com>
Tue, 19 Jun 2012 13:57:42 -0300
changeset 6330 0fa55ca2efdd
parent 6138 4c64952a58fb
child 6392 fa7eb111f994
permissions -rwxr-xr-x
Fixes #1422, restores GL context automatically under Android
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "SDL_config.h"
    22 
    23 #if SDL_VIDEO_DRIVER_ANDROID
    24 
    25 /* Android SDL video driver implementation
    26 */
    27 
    28 #include "SDL_video.h"
    29 #include "SDL_mouse.h"
    30 #include "../SDL_sysvideo.h"
    31 #include "../SDL_pixels_c.h"
    32 #include "../../events/SDL_events_c.h"
    33 #include "../../events/SDL_windowevents_c.h"
    34 
    35 #include "SDL_androidvideo.h"
    36 #include "SDL_androidevents.h"
    37 #include "SDL_androidkeyboard.h"
    38 #include "SDL_androidwindow.h"
    39 
    40 #define ANDROID_VID_DRIVER_NAME "Android"
    41 
    42 /* Initialization/Query functions */
    43 static int Android_VideoInit(_THIS);
    44 static void Android_VideoQuit(_THIS);
    45 
    46 /* GL functions (SDL_androidgl.c) */
    47 extern int Android_GL_LoadLibrary(_THIS, const char *path);
    48 extern void *Android_GL_GetProcAddress(_THIS, const char *proc);
    49 extern void Android_GL_UnloadLibrary(_THIS);
    50 //extern int *Android_GL_GetVisual(_THIS, Display * display, int screen);
    51 extern SDL_GLContext Android_GL_CreateContext(_THIS, SDL_Window * window);
    52 extern int Android_GL_MakeCurrent(_THIS, SDL_Window * window,
    53                               SDL_GLContext context);
    54 extern int Android_GL_SetSwapInterval(_THIS, int interval);
    55 extern int Android_GL_GetSwapInterval(_THIS);
    56 extern void Android_GL_SwapWindow(_THIS, SDL_Window * window);
    57 extern void Android_GL_DeleteContext(_THIS, SDL_GLContext context);
    58 
    59 /* Android driver bootstrap functions */
    60 
    61 
    62 // These are filled in with real values in Android_SetScreenResolution on 
    63 // init (before SDL_main())
    64 int Android_ScreenWidth = 0;
    65 int Android_ScreenHeight = 0;
    66 Uint32 Android_ScreenFormat = SDL_PIXELFORMAT_UNKNOWN;
    67 SDL_sem *Android_PauseSem = NULL, *Android_ResumeSem = NULL;
    68 
    69 /* Currently only one window */
    70 SDL_Window *Android_Window = NULL;
    71 
    72 static int
    73 Android_Available(void)
    74 {
    75     return 1;
    76 }
    77 
    78 static void
    79 Android_DeleteDevice(SDL_VideoDevice * device)
    80 {
    81     SDL_free(device);
    82 }
    83 
    84 static SDL_VideoDevice *
    85 Android_CreateDevice(int devindex)
    86 {
    87     printf("Creating video device\n");
    88     SDL_VideoDevice *device;
    89 
    90     /* Initialize all variables that we clean on shutdown */
    91     device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
    92     if (!device) {
    93         SDL_OutOfMemory();
    94         if (device) {
    95             SDL_free(device);
    96         }
    97         return (0);
    98     }
    99 
   100     /* Set the function pointers */
   101     device->VideoInit = Android_VideoInit;
   102     device->VideoQuit = Android_VideoQuit;
   103     device->PumpEvents = Android_PumpEvents;
   104 
   105     device->CreateWindow = Android_CreateWindow;
   106     device->SetWindowTitle = Android_SetWindowTitle;
   107     device->DestroyWindow = Android_DestroyWindow;
   108 
   109     device->free = Android_DeleteDevice;
   110 
   111     /* GL pointers */
   112     device->GL_LoadLibrary = Android_GL_LoadLibrary;
   113     device->GL_GetProcAddress = Android_GL_GetProcAddress;
   114     device->GL_UnloadLibrary = Android_GL_UnloadLibrary;
   115     device->GL_CreateContext = Android_GL_CreateContext;
   116     device->GL_MakeCurrent = Android_GL_MakeCurrent;
   117     device->GL_SetSwapInterval = Android_GL_SetSwapInterval;
   118     device->GL_GetSwapInterval = Android_GL_GetSwapInterval;
   119     device->GL_SwapWindow = Android_GL_SwapWindow;
   120     device->GL_DeleteContext = Android_GL_DeleteContext;
   121 
   122     return device;
   123 }
   124 
   125 VideoBootStrap Android_bootstrap = {
   126     ANDROID_VID_DRIVER_NAME, "SDL Android video driver",
   127     Android_Available, Android_CreateDevice
   128 };
   129 
   130 
   131 int
   132 Android_VideoInit(_THIS)
   133 {
   134     SDL_DisplayMode mode;
   135 
   136     mode.format = Android_ScreenFormat;
   137     mode.w = Android_ScreenWidth;
   138     mode.h = Android_ScreenHeight;
   139     mode.refresh_rate = 0;
   140     mode.driverdata = NULL;
   141     if (SDL_AddBasicVideoDisplay(&mode) < 0) {
   142         return -1;
   143     }
   144 
   145     SDL_zero(mode);
   146     SDL_AddDisplayMode(&_this->displays[0], &mode);
   147 
   148     Android_InitKeyboard();
   149 
   150     /* We're done! */
   151     return 0;
   152 }
   153 
   154 void
   155 Android_VideoQuit(_THIS)
   156 {
   157 }
   158 
   159 /* This function gets called before VideoInit() */
   160 void
   161 Android_SetScreenResolution(int width, int height, Uint32 format)
   162 {
   163     Android_ScreenWidth = width;
   164     Android_ScreenHeight = height;   
   165     Android_ScreenFormat = format;
   166 
   167     if (Android_Window) {
   168         SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESIZED, width, height);
   169     }
   170 }
   171 
   172 #endif /* SDL_VIDEO_DRIVER_ANDROID */
   173 
   174 /* vi: set ts=4 sw=4 expandtab: */