src/video/android/SDL_androidvideo.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 08 Apr 2011 13:03:26 -0700
changeset 5535 96594ac5fd1a
parent 5262 b530ef003506
child 6044 35448a5ea044
permissions -rw-r--r--
SDL 1.3 is now under the zlib license.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2011 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 /* Android SDL video driver implementation
    24 */
    25 
    26 #include "SDL_video.h"
    27 #include "SDL_mouse.h"
    28 #include "../SDL_sysvideo.h"
    29 #include "../SDL_pixels_c.h"
    30 #include "../../events/SDL_events_c.h"
    31 #include "../../events/SDL_windowevents_c.h"
    32 
    33 #include "SDL_androidvideo.h"
    34 #include "SDL_androidevents.h"
    35 #include "SDL_androidkeyboard.h"
    36 #include "SDL_androidwindow.h"
    37 
    38 #define ANDROID_VID_DRIVER_NAME "Android"
    39 
    40 /* Initialization/Query functions */
    41 static int Android_VideoInit(_THIS);
    42 static void Android_VideoQuit(_THIS);
    43 
    44 /* GL functions (SDL_androidgl.c) */
    45 extern int Android_GL_LoadLibrary(_THIS, const char *path);
    46 extern void *Android_GL_GetProcAddress(_THIS, const char *proc);
    47 extern void Android_GL_UnloadLibrary(_THIS);
    48 //extern int *Android_GL_GetVisual(_THIS, Display * display, int screen);
    49 extern SDL_GLContext Android_GL_CreateContext(_THIS, SDL_Window * window);
    50 extern int Android_GL_MakeCurrent(_THIS, SDL_Window * window,
    51                               SDL_GLContext context);
    52 extern int Android_GL_SetSwapInterval(_THIS, int interval);
    53 extern int Android_GL_GetSwapInterval(_THIS);
    54 extern void Android_GL_SwapWindow(_THIS, SDL_Window * window);
    55 extern void Android_GL_DeleteContext(_THIS, SDL_GLContext context);
    56 
    57 /* Android driver bootstrap functions */
    58 
    59 
    60 // These are filled in with real values in Android_SetScreenResolution on 
    61 // init (before SDL_main())
    62 int Android_ScreenWidth = 0;
    63 int Android_ScreenHeight = 0;
    64 Uint32 Android_ScreenFormat = SDL_PIXELFORMAT_UNKNOWN;
    65 
    66 /* Currently only one window */
    67 SDL_Window *Android_Window = NULL;
    68 
    69 static int
    70 Android_Available(void)
    71 {
    72     return 1;
    73 }
    74 
    75 static void
    76 Android_DeleteDevice(SDL_VideoDevice * device)
    77 {
    78     SDL_free(device);
    79 }
    80 
    81 static SDL_VideoDevice *
    82 Android_CreateDevice(int devindex)
    83 {
    84     printf("Creating video device\n");
    85     SDL_VideoDevice *device;
    86 
    87     /* Initialize all variables that we clean on shutdown */
    88     device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
    89     if (!device) {
    90         SDL_OutOfMemory();
    91         if (device) {
    92             SDL_free(device);
    93         }
    94         return (0);
    95     }
    96 
    97     /* Set the function pointers */
    98     device->VideoInit = Android_VideoInit;
    99     device->VideoQuit = Android_VideoQuit;
   100     device->PumpEvents = Android_PumpEvents;
   101 
   102     device->CreateWindow = Android_CreateWindow;
   103     device->SetWindowTitle = Android_SetWindowTitle;
   104     device->DestroyWindow = Android_DestroyWindow;
   105 
   106     device->free = Android_DeleteDevice;
   107 
   108     /* GL pointers */
   109     device->GL_LoadLibrary = Android_GL_LoadLibrary;
   110     device->GL_GetProcAddress = Android_GL_GetProcAddress;
   111     device->GL_UnloadLibrary = Android_GL_UnloadLibrary;
   112     device->GL_CreateContext = Android_GL_CreateContext;
   113     device->GL_MakeCurrent = Android_GL_MakeCurrent;
   114     device->GL_SetSwapInterval = Android_GL_SetSwapInterval;
   115     device->GL_GetSwapInterval = Android_GL_GetSwapInterval;
   116     device->GL_SwapWindow = Android_GL_SwapWindow;
   117     device->GL_DeleteContext = Android_GL_DeleteContext;
   118 
   119     return device;
   120 }
   121 
   122 VideoBootStrap Android_bootstrap = {
   123     ANDROID_VID_DRIVER_NAME, "SDL Android video driver",
   124     Android_Available, Android_CreateDevice
   125 };
   126 
   127 
   128 int
   129 Android_VideoInit(_THIS)
   130 {
   131     SDL_DisplayMode mode;
   132 
   133     mode.format = Android_ScreenFormat;
   134     mode.w = Android_ScreenWidth;
   135     mode.h = Android_ScreenHeight;
   136     mode.refresh_rate = 0;
   137     mode.driverdata = NULL;
   138     if (SDL_AddBasicVideoDisplay(&mode) < 0) {
   139         return -1;
   140     }
   141 
   142     SDL_zero(mode);
   143     SDL_AddDisplayMode(&_this->displays[0], &mode);
   144 
   145     Android_InitKeyboard();
   146 
   147     /* We're done! */
   148     return 0;
   149 }
   150 
   151 void
   152 Android_VideoQuit(_THIS)
   153 {
   154 }
   155 
   156 /* This function gets called before VideoInit() */
   157 void
   158 Android_SetScreenResolution(int width, int height, Uint32 format)
   159 {
   160     Android_ScreenWidth = width;
   161     Android_ScreenHeight = height;   
   162     Android_ScreenFormat = format;
   163 
   164     if (Android_Window) {
   165         SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESIZED, width, height);
   166     }
   167 }
   168 
   169 /* vi: set ts=4 sw=4 expandtab: */