src/SDL12_compat.c
changeset 17 473158daa1de
parent 16 ad3e9ebcfd90
child 18 221a624a97fb
equal deleted inserted replaced
16:ad3e9ebcfd90 17:473158daa1de
    39 // !!! IMPLEMENT_ME SDL_CreateCursor
    39 // !!! IMPLEMENT_ME SDL_CreateCursor
    40 // !!! IMPLEMENT_ME SDL_CreateThread
    40 // !!! IMPLEMENT_ME SDL_CreateThread
    41 // !!! IMPLEMENT_ME SDL_EventState
    41 // !!! IMPLEMENT_ME SDL_EventState
    42 // !!! IMPLEMENT_ME SDL_FillRect
    42 // !!! IMPLEMENT_ME SDL_FillRect
    43 // !!! IMPLEMENT_ME SDL_FreeCursor
    43 // !!! IMPLEMENT_ME SDL_FreeCursor
    44 // !!! IMPLEMENT_ME SDL_FreeSurface
       
    45 // !!! IMPLEMENT_ME SDL_GL_GetAttribute
    44 // !!! IMPLEMENT_ME SDL_GL_GetAttribute
    46 // !!! IMPLEMENT_ME SDL_GL_GetProcAddress
       
    47 // !!! IMPLEMENT_ME SDL_GL_LoadLibrary
       
    48 // !!! IMPLEMENT_ME SDL_GL_Lock
    45 // !!! IMPLEMENT_ME SDL_GL_Lock
    49 // !!! IMPLEMENT_ME SDL_GL_SetAttribute
    46 // !!! IMPLEMENT_ME SDL_GL_SetAttribute
    50 // !!! IMPLEMENT_ME SDL_GL_Unlock
    47 // !!! IMPLEMENT_ME SDL_GL_Unlock
    51 // !!! IMPLEMENT_ME SDL_GL_UpdateRects
    48 // !!! IMPLEMENT_ME SDL_GL_UpdateRects
    52 // !!! IMPLEMENT_ME SDL_GetClipRect
    49 // !!! IMPLEMENT_ME SDL_GetClipRect
    54 // !!! IMPLEMENT_ME SDL_GetEventFilter
    51 // !!! IMPLEMENT_ME SDL_GetEventFilter
    55 // !!! IMPLEMENT_ME SDL_GetKeyName
    52 // !!! IMPLEMENT_ME SDL_GetKeyName
    56 // !!! IMPLEMENT_ME SDL_GetKeyState
    53 // !!! IMPLEMENT_ME SDL_GetKeyState
    57 // !!! IMPLEMENT_ME SDL_GetModState
    54 // !!! IMPLEMENT_ME SDL_GetModState
    58 // !!! IMPLEMENT_ME SDL_GetMouseState
    55 // !!! IMPLEMENT_ME SDL_GetMouseState
    59 // !!! IMPLEMENT_ME SDL_GetRGB
       
    60 // !!! IMPLEMENT_ME SDL_GetRGBA
       
    61 // !!! IMPLEMENT_ME SDL_GetRelativeMouseState
    56 // !!! IMPLEMENT_ME SDL_GetRelativeMouseState
    62 // !!! IMPLEMENT_ME SDL_LockSurface
    57 // !!! IMPLEMENT_ME SDL_LockSurface
    63 // !!! IMPLEMENT_ME SDL_LowerBlit
    58 // !!! IMPLEMENT_ME SDL_LowerBlit
    64 // !!! IMPLEMENT_ME SDL_MapRGB
       
    65 // !!! IMPLEMENT_ME SDL_MapRGBA
       
    66 // !!! IMPLEMENT_ME SDL_PeepEvents
    59 // !!! IMPLEMENT_ME SDL_PeepEvents
    67 // !!! IMPLEMENT_ME SDL_PollEvent
    60 // !!! IMPLEMENT_ME SDL_PollEvent
    68 // !!! IMPLEMENT_ME SDL_PumpEvents
    61 // !!! IMPLEMENT_ME SDL_PumpEvents
    69 // !!! IMPLEMENT_ME SDL_PushEvent
    62 // !!! IMPLEMENT_ME SDL_PushEvent
    70 // !!! IMPLEMENT_ME SDL_SetClipRect
    63 // !!! IMPLEMENT_ME SDL_SetClipRect
   167 #define SDL12_RLEACCEL 0x00004000
   160 #define SDL12_RLEACCEL 0x00004000
   168 #define SDL12_SRCALPHA 0x00010000
   161 #define SDL12_SRCALPHA 0x00010000
   169 #define SDL12_PREALLOC 0x01000000
   162 #define SDL12_PREALLOC 0x01000000
   170 
   163 
   171 
   164 
       
   165 static SDL12_VideoInfo VideoInfo;
   172 static SDL_Window *VideoWindow = NULL;
   166 static SDL_Window *VideoWindow = NULL;
   173 static SDL_Surface *WindowSurface = NULL;
   167 static SDL_Surface *WindowSurface = NULL;
   174 static SDL_Surface *VideoSurface = NULL;
   168 static SDL_Surface *VideoSurface = NULL;
   175 static SDL_Surface *ShadowSurface = NULL;
   169 static SDL_Surface *ShadowSurface = NULL;
   176 static SDL_Surface *PublicSurface = NULL;
   170 static SDL_Surface *PublicSurface = NULL;
   345     #undef SETFLAG
   339     #undef SETFLAG
   346 
   340 
   347     if (sdl12flags & SDL12_INIT_CDROM)
   341     if (sdl12flags & SDL12_INIT_CDROM)
   348         CDRomInit = 0;
   342         CDRomInit = 0;
   349 
   343 
       
   344     // !!! FIXME: reset a bunch of other global variables too.
       
   345     if (sdl12flags & SDL12_INIT_VIDEO) {
       
   346         SDL20_FreeFormat(VideoInfo.vfmt);
       
   347         SDL_zero(VideoInfo);
       
   348     }
       
   349 
   350     // !!! FIXME: do something about SDL12_INIT_EVENTTHREAD
   350     // !!! FIXME: do something about SDL12_INIT_EVENTTHREAD
   351     SDL20_QuitSubSystem(sdl20flags);
   351     SDL20_QuitSubSystem(sdl20flags);
   352 
   352 
   353     // !!! FIXME: UnloadSDL20() ?
   353     // !!! FIXME: UnloadSDL20() ?
   354 }
   354 }
   355 
   355 
   356 void
   356 void
   357 SDL_Quit(void)
   357 SDL_Quit(void)
   358 {
   358 {
       
   359     // !!! FIXME: reset a bunch of other global variables too.
       
   360     SDL20_FreeFormat(VideoInfo.vfmt);
       
   361     SDL_zero(VideoInfo);
   359     CDRomInit = 0;
   362     CDRomInit = 0;
   360     SDL20_Quit();
   363     SDL20_Quit();
   361     UnloadSDL20();
   364     UnloadSDL20();
   362 }
   365 }
   363 
   366 
   485     #define MAPSURFACEFLAGS(fl) { if (surface20->flags & SDL_##fl) { surface12->flags |= SDL12_##fl; flags &= ~SDL_##fl; } }
   488     #define MAPSURFACEFLAGS(fl) { if (surface20->flags & SDL_##fl) { surface12->flags |= SDL12_##fl; flags &= ~SDL_##fl; } }
   486     MAPSURFACEFLAGS(PREALLOC);
   489     MAPSURFACEFLAGS(PREALLOC);
   487     MAPSURFACEFLAGS(RLEACCEL);
   490     MAPSURFACEFLAGS(RLEACCEL);
   488     MAPSURFACEFLAGS(DONTFREE);
   491     MAPSURFACEFLAGS(DONTFREE);
   489     #undef MAPSURFACEFLAGS
   492     #undef MAPSURFACEFLAGS
   490     SDL_assert(flags == 0);  /* non-zero if there's a flag we didn't map. */
   493     assert(flags == 0);  /* non-zero if there's a flag we didn't map. */
   491 
   494 
   492     surface12->format = format12;
   495     surface12->format = format12;
   493     surface12->w = surface20->w;
   496     surface12->w = surface20->w;
   494     surface12->h = surface20->h;
   497     surface12->h = surface20->h;
   495     surface12->pitch = (Uint16) surface20->pitch;  /* !!! FIXME: make sure this fits in a Uint16 */
   498     surface12->pitch = (Uint16) surface20->pitch;  /* !!! FIXME: make sure this fits in a Uint16 */
   516     if (!surface12) {
   519     if (!surface12) {
   517         SDL20_FreeSurface(surface20);
   520         SDL20_FreeSurface(surface20);
   518         return NULL;
   521         return NULL;
   519     }
   522     }
   520 
   523 
   521     SDL_assert(surface12->flags == 0);  // shouldn't have prealloc, rleaccel, or dontfree.
   524     assert(surface12->flags == 0);  // shouldn't have prealloc, rleaccel, or dontfree.
   522     return surface12;
   525     return surface12;
   523 }
   526 }
   524 
   527 
   525 SDL12_Surface *
   528 SDL12_Surface *
   526 SDL_CreateRGBSurfaceFrom(void *pixels, int width, int height, int depth, int pitch, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
   529 SDL_CreateRGBSurfaceFrom(void *pixels, int width, int height, int depth, int pitch, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
   530     if (!surface12) {
   533     if (!surface12) {
   531         SDL20_FreeSurface(surface20);
   534         SDL20_FreeSurface(surface20);
   532         return NULL;
   535         return NULL;
   533     }
   536     }
   534 
   537 
   535     SDL_assert(surface12->flags == SDL12_PREALLOC);  // should _only_ have prealloc.
   538     assert(surface12->flags == SDL12_PREALLOC);  // should _only_ have prealloc.
   536     return surface12;
   539     return surface12;
   537 }
   540 }
   538 
   541 
   539 void SDL_FreeSurface(SDL12_Surface *surface12)
   542 void SDL_FreeSurface(SDL12_Surface *surface12)
   540 {
   543 {
   546         }
   549         }
   547         SDL20_free(surface12);
   550         SDL20_free(surface12);
   548     }
   551     }
   549 }
   552 }
   550 
   553 
       
   554 static SDL_PixelFormat *
       
   555 PixelFormat12to20(SDL_PixelFormat *format20, SDL_Palette *palette20, SDL12_PixelFormat *format12)
       
   556 {
       
   557     palette20->ncolors = format12->palette->ncolors;
       
   558     palette20->colors = format12->palette->colors;
       
   559     palette20->version = 1;
       
   560     palette20->refcount = 1;
       
   561     format20->format = SDL_MasksToPixelFormatEnum(format12->BitsPerPixel, format12->Rmask, format12->Gmask, format12->Bmask, format12->Amask);
       
   562     format20->palette = palette20;
       
   563     format20->BitsPerPixel = format12->BitsPerPixel;
       
   564     format20->BytesPerPixel = format12->BytesPerPixel;
       
   565     format20->Rmask = format12->Rmask;
       
   566     format20->Gmask = format12->Gmask;
       
   567     format20->Bmask = format12->Bmask;
       
   568     format20->Amask = format12->Amask;
       
   569     format20->Rloss = format12->Rloss;
       
   570     format20->Gloss = format12->Gloss;
       
   571     format20->Bloss = format12->Bloss;
       
   572     format20->Aloss = format12->Aloss;
       
   573     format20->Rshift = format12->Rshift;
       
   574     format20->Gshift = format12->Gshift;
       
   575     format20->Bshift = format12->Bshift;
       
   576     format20->Ashift = format12->Ashift;
       
   577     format20->refcount = 1;
       
   578     format20->next = NULL;
       
   579     return format20;
       
   580 }
       
   581 
       
   582 Uint32
       
   583 SDL_MapRGB(const SDL12_PixelFormat *format12, Uint8 r, Uint8 g, Uint8 b)
       
   584 {
       
   585     /* This is probably way slower than apps expect. */
       
   586     SDL_PixelFormat format20;
       
   587     SDL_Palette palette20;
       
   588     return SDL20_MapRGB(PixelFormat12to20(&format20, &palette20, format12), r, g, b);
       
   589 }
       
   590 
       
   591 Uint32
       
   592 SDL_MapRGBA(const SDL12_PixelFormat *format12, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
       
   593 {
       
   594     /* This is probably way slower than apps expect. */
       
   595     SDL_PixelFormat format20;
       
   596     SDL_Palette palette20;
       
   597     return SDL20_MapRGBA(PixelFormat12to20(&format20, &palette20, format12), r, g, b, a);
       
   598 }
       
   599 
       
   600 void
       
   601 SDL_GetRGB(Uint32 pixel, const SDL12_PixelFormat *format12, Uint8 *r, Uint8 *g, Uint8 *b)
       
   602 {
       
   603     /* This is probably way slower than apps expect. */
       
   604     SDL_PixelFormat format20;
       
   605     SDL_Palette palette20;
       
   606     return SDL20_GetRGB(pixel, PixelFormat12to20(&format20, &palette20, format12), r, g, b);
       
   607 }
       
   608 
       
   609 void
       
   610 SDL_GetRGBA(Uint32 pixel, const SDL12_PixelFormat *format12, Uint8 *r, Uint8 *g, Uint8 *b, Uint8 *a)
       
   611 {
       
   612     /* This is probably way slower than apps expect. */
       
   613     SDL_PixelFormat format20;
       
   614     SDL_Palette palette20;
       
   615     return SDL20_GetRGB(pixel, PixelFormat12to20(&format20, &palette20, format12), r, g, b, a);
       
   616 }
   551 
   617 
   552 const SDL12_VideoInfo *
   618 const SDL12_VideoInfo *
   553 SDL_GetVideoInfo(void)
   619 SDL_GetVideoInfo(void)
   554 {
   620 {
   555     static SDL12_VideoInfo info;
       
   556     SDL_DisplayMode mode;
   621     SDL_DisplayMode mode;
   557 
   622 
   558     /* !!! FIXME: Memory leak, compatibility code, who cares? */
   623     if (!VideoInfo.vfmt && SDL20_GetDesktopDisplayMode(VideoDisplayIndex, &mode) == 0) {
   559     if (!info.vfmt && SDL20_GetDesktopDisplayMode(VideoDisplayIndex, &mode) == 0) {
   624         VideoInfo.vfmt = SDL20_AllocFormat(mode.format);
   560         info.vfmt = SDL20_AllocFormat(mode.format);
   625         VideoInfo.current_w = mode.w;
   561         info.current_w = mode.w;
   626         VideoInfo.current_h = mode.h;
   562         info.current_h = mode.h;
       
   563         // !!! FIXME
   627         // !!! FIXME
   564         //info.wm_available = 1;
   628         //VideoInfo.wm_available = 1;
   565         //info.video_mem = 1024 * 256;
   629         //VideoInfo.video_mem = 1024 * 256;
   566     }
   630     }
   567     return &info;
   631     return &VideoInfo;
   568 }
   632 }
   569 
   633 
   570 int
   634 int
   571 SDL_VideoModeOK(int width, int height, int bpp, Uint32 sdl12flags)
   635 SDL_VideoModeOK(int width, int height, int bpp, Uint32 sdl12flags)
   572 {
   636 {
   597     }
   661     }
   598     return actual_bpp;
   662     return actual_bpp;
   599 }
   663 }
   600 
   664 
   601 SDL_Rect **
   665 SDL_Rect **
   602 SDL_ListModes(const SDL12_PixelFormat * format, Uint32 flags)
   666 SDL_ListModes(const SDL12_PixelFormat *format, Uint32 flags)
   603 {
   667 {
   604     int i, nmodes;
   668     int i, nmodes;
   605     SDL_Rect **modes;
   669     SDL_Rect **modes;
   606 
   670 
   607     if (!SDL20_WasInit(SDL_INIT_VIDEO)) {
   671     if (!SDL20_WasInit(SDL_INIT_VIDEO)) {
   611     if (!(flags & SDL12_FULLSCREEN)) {
   675     if (!(flags & SDL12_FULLSCREEN)) {
   612         return (SDL_Rect **) (-1);
   676         return (SDL_Rect **) (-1);
   613     }
   677     }
   614 
   678 
   615     if (!format) {
   679     if (!format) {
   616         format = SDL_GetVideoInfo()->vfmt;
   680         format = VideoInfo.vfmt;
   617     }
   681     }
   618 
   682 
   619     /* Memory leak, but this is a compatibility function, who cares? */
   683     /* !!! FIXME: Memory leak */
   620     nmodes = 0;
   684     nmodes = 0;
   621     modes = NULL;
   685     modes = NULL;
   622     for (i = 0; i < SDL20_GetNumDisplayModes(VideoDisplayIndex); ++i) {
   686     for (i = 0; i < SDL20_GetNumDisplayModes(VideoDisplayIndex); ++i) {
   623         SDL_DisplayMode mode;
   687         SDL_DisplayMode mode;
   624         int bpp;
   688         int bpp;
   641         if (nmodes > 0 && modes[nmodes - 1]->w == mode.w
   705         if (nmodes > 0 && modes[nmodes - 1]->w == mode.w
   642             && modes[nmodes - 1]->h == mode.h) {
   706             && modes[nmodes - 1]->h == mode.h) {
   643             continue;
   707             continue;
   644         }
   708         }
   645 
   709 
   646         modes = SDL_realloc(modes, (nmodes + 2) * sizeof(*modes));
   710         modes = SDL20_realloc(modes, (nmodes + 2) * sizeof(*modes));
   647         if (!modes) {
   711         if (!modes) {
   648             return NULL;
   712             return NULL;
   649         }
   713         }
   650         modes[nmodes] = (SDL_Rect *) SDL_malloc(sizeof(SDL_Rect));
   714         modes[nmodes] = (SDL_Rect *) SDL20_malloc(sizeof(SDL_Rect));
   651         if (!modes[nmodes]) {
   715         if (!modes[nmodes]) {
   652             return NULL;
   716             return NULL;
   653         }
   717         }
   654         modes[nmodes]->x = 0;
   718         modes[nmodes]->x = 0;
   655         modes[nmodes]->y = 0;
   719         modes[nmodes]->y = 0;
   661         modes[nmodes] = NULL;
   725         modes[nmodes] = NULL;
   662     }
   726     }
   663     return modes;
   727     return modes;
   664 }
   728 }
   665 
   729 
       
   730 /* !!! FIXME: don't need a filter, just do this in the SDL_PumpEvents() implementation. */
   666 static int
   731 static int
   667 SDL_CompatEventFilter(void *userdata, SDL_Event * event)
   732 SDL_CompatEventFilter(void *userdata, SDL_Event * event)
   668 {
   733 {
   669     SDL_Event fake;
   734     SDL_Event fake;
   670 
   735 
   671     switch (event->type) {
   736     switch (event->type) {
   672     case SDL_WINDOWEVENT:
   737     case SDL_WINDOWEVENT:
   673         switch (event->window.event) {
   738         switch (event->window.event) {
   674         case SDL_WINDOWEVENT_EXPOSED:
   739         case SDL_WINDOWEVENT_EXPOSED:
   675             if (!SDL_HasEvent(SDL_VIDEOEXPOSE)) {
   740             if (!SDL20_HasEvent(SDL_VIDEOEXPOSE)) {
   676                 fake.type = SDL_VIDEOEXPOSE;
   741                 fake.type = SDL_VIDEOEXPOSE;
   677                 SDL_PushEvent(&fake);
   742                 SDL20_PushEvent(&fake);
   678             }
   743             }
   679             break;
   744             break;
   680         case SDL_WINDOWEVENT_RESIZED:
   745         case SDL_WINDOWEVENT_RESIZED:
   681             SDL_FlushEvent(SDL_VIDEORESIZE);
   746             SDL_FlushEvent(SDL_VIDEORESIZE);
   682             /* We don't want to expose that the window width and height will
   747             /* We don't want to expose that the window width and height will
   826 
   891 
   827 static void
   892 static void
   828 ClearVideoSurface()
   893 ClearVideoSurface()
   829 {
   894 {
   830     if (ShadowSurface) {
   895     if (ShadowSurface) {
   831         SDL_FillRect(ShadowSurface, NULL,
   896         SDL20_FillRect(ShadowSurface, NULL,
   832             SDL_MapRGB(ShadowSurface->format, 0, 0, 0));
   897             SDL20_MapRGB(ShadowSurface->format, 0, 0, 0));
   833     }
   898     }
   834     SDL_FillRect(WindowSurface, NULL, 0);
   899     SDL20_FillRect(WindowSurface, NULL, 0);
   835     SDL_UpdateWindowSurface(VideoWindow);
   900     SDL20_UpdateWindowSurface(VideoWindow);
   836 }
   901 }
   837 
   902 
   838 static void
   903 static void
   839 SetupScreenSaver(int flags)
   904 SetupScreenSaver(int flags)
   840 {
   905 {
   849         allow_screensaver = SDL_FALSE;
   914         allow_screensaver = SDL_FALSE;
   850     } else {
   915     } else {
   851         allow_screensaver = SDL_TRUE;
   916         allow_screensaver = SDL_TRUE;
   852     }
   917     }
   853     if (allow_screensaver) {
   918     if (allow_screensaver) {
   854         SDL_EnableScreenSaver();
   919         SDL20_EnableScreenSaver();
   855     } else {
   920     } else {
   856         SDL_DisableScreenSaver();
   921         SDL20_DisableScreenSaver();
   857     }
   922     }
   858 }
   923 }
   859 
   924 
   860 static int
   925 static int
   861 SDL_ResizeVideoMode(int width, int height, int bpp, Uint32 flags)
   926 ResizeVideoMode(int width, int height, int bpp, Uint32 flags)
   862 {
   927 {
   863     int w, h;
   928     int w, h;
   864 
   929 
   865     /* We can't resize something we don't have... */
   930     /* We can't resize something we don't have... */
   866     if (!VideoSurface) {
   931     if (!VideoSurface) {
   953     if (bpp == 0) {
  1018     if (bpp == 0) {
   954         bpp = SDL_BITSPERPIXEL(desktop_mode.format);
  1019         bpp = SDL_BITSPERPIXEL(desktop_mode.format);
   955     }
  1020     }
   956 
  1021 
   957     /* See if we can simply resize the existing window and surface */
  1022     /* See if we can simply resize the existing window and surface */
   958     if (SDL_ResizeVideoMode(width, height, bpp, flags) == 0) {
  1023     if (ResizeVideoMode(width, height, bpp, flags) == 0) {
   959         return PublicSurface;
  1024         return PublicSurface;
   960     }
  1025     }
   961 
  1026 
   962     /* Destroy existing window */
  1027     /* Destroy existing window */
   963     PublicSurface = NULL;
  1028     PublicSurface = NULL;