src/video/glsdl/SDL_glsdl.c
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1662 782fd950bd46
equal deleted inserted replaced
1667:1fddae038bc8 1668:4da1ee79c9af
    43 #undef GLSDL_GRAPHICAL_DEBUG
    43 #undef GLSDL_GRAPHICAL_DEBUG
    44 
    44 
    45 /* Initialization/Query functions */
    45 /* Initialization/Query functions */
    46 
    46 
    47 /* Hardware surface functions */
    47 /* Hardware surface functions */
    48 static int glSDL_SetColors (_THIS, int firstcolor, int ncolors,
    48 static int glSDL_SetColors(_THIS, int firstcolor, int ncolors,
    49                             SDL_Color * colors);
    49                            SDL_Color * colors);
    50 static int glSDL_AllocHWSurface (_THIS, SDL_Surface * surface);
    50 static int glSDL_AllocHWSurface(_THIS, SDL_Surface * surface);
    51 static int glSDL_LockHWSurface (_THIS, SDL_Surface * surface);
    51 static int glSDL_LockHWSurface(_THIS, SDL_Surface * surface);
    52 static int glSDL_FlipHWSurface (_THIS, SDL_Surface * surface);
    52 static int glSDL_FlipHWSurface(_THIS, SDL_Surface * surface);
    53 static void glSDL_UnlockHWSurface (_THIS, SDL_Surface * surface);
    53 static void glSDL_UnlockHWSurface(_THIS, SDL_Surface * surface);
    54 static void glSDL_FreeHWSurface (_THIS, SDL_Surface * surface);
    54 static void glSDL_FreeHWSurface(_THIS, SDL_Surface * surface);
    55 static int glSDL_FillHWRect (_THIS, SDL_Surface * dst, SDL_Rect * rect,
    55 static int glSDL_FillHWRect(_THIS, SDL_Surface * dst, SDL_Rect * rect,
    56                              Uint32 color);
    56                             Uint32 color);
    57 static int glSDL_CheckHWBlit (_THIS, SDL_Surface * src, SDL_Surface * dst);
    57 static int glSDL_CheckHWBlit(_THIS, SDL_Surface * src, SDL_Surface * dst);
    58 static int glSDL_SetHWColorKey (_THIS, SDL_Surface * surface, Uint32 key);
    58 static int glSDL_SetHWColorKey(_THIS, SDL_Surface * surface, Uint32 key);
    59 static int glSDL_SetHWAlpha (_THIS, SDL_Surface * surface, Uint8 alpha);
    59 static int glSDL_SetHWAlpha(_THIS, SDL_Surface * surface, Uint8 alpha);
    60 static int glSDL_VideoInit (_THIS, SDL_PixelFormat * vformat);
    60 static int glSDL_VideoInit(_THIS, SDL_PixelFormat * vformat);
    61 static SDL_Rect **glSDL_ListModes (_THIS, SDL_PixelFormat * format,
    61 static SDL_Rect **glSDL_ListModes(_THIS, SDL_PixelFormat * format,
    62                                    Uint32 flags);
    62                                   Uint32 flags);
    63 static void glSDL_VideoQuit (_THIS);
    63 static void glSDL_VideoQuit(_THIS);
    64 static void glSDL_UpdateRects (_THIS, int numrects, SDL_Rect * rects);
    64 static void glSDL_UpdateRects(_THIS, int numrects, SDL_Rect * rects);
    65 static SDL_Surface *glSDL_SetVideoMode (_THIS, SDL_Surface * current,
    65 static SDL_Surface *glSDL_SetVideoMode(_THIS, SDL_Surface * current,
    66                                         int width, int height, int bpp,
    66                                        int width, int height, int bpp,
    67                                         Uint32 flags);
    67                                        Uint32 flags);
    68 
    68 
    69 #define	IS_GLSDL_SURFACE(s)	((s) && glSDL_GetTexInfo(s))
    69 #define	IS_GLSDL_SURFACE(s)	((s) && glSDL_GetTexInfo(s))
    70 
    70 
    71 #define	LOGIC_W(s)	( IS_GLSDL_SURFACE(this,s) ? TEXINFO(s)->lw : (s)->w )
    71 #define	LOGIC_W(s)	( IS_GLSDL_SURFACE(this,s) ? TEXINFO(s)->lw : (s)->w )
    72 #define	LOGIC_H(s)	( IS_GLSDL_SURFACE(this,s) ? TEXINFO(s)->lh : (s)->h )
    72 #define	LOGIC_H(s)	( IS_GLSDL_SURFACE(this,s) ? TEXINFO(s)->lh : (s)->h )
    77  * Special version for glSDL, which ignores the fake SDL_HWSURFACE
    77  * Special version for glSDL, which ignores the fake SDL_HWSURFACE
    78  * flags, so we don't have SDL calling us back whenever we want to
    78  * flags, so we don't have SDL calling us back whenever we want to
    79  * do some internal blitting...
    79  * do some internal blitting...
    80  */
    80  */
    81 static void
    81 static void
    82 glSDL_SoftBlit (SDL_Surface * src, SDL_Rect * srcrect,
    82 glSDL_SoftBlit(SDL_Surface * src, SDL_Rect * srcrect,
    83                 SDL_Surface * dst, SDL_Rect * dstrect)
    83                SDL_Surface * dst, SDL_Rect * dstrect)
    84 {
    84 {
    85     SDL_BlitInfo info;
    85     SDL_BlitInfo info;
    86 
    86 
    87     if (srcrect)
    87     if (srcrect)
    88         if (!srcrect->w || !srcrect->h)
    88         if (!srcrect->w || !srcrect->h)
    89             return;
    89             return;
    90 
    90 
    91     /* Check to make sure the blit mapping is valid */
    91     /* Check to make sure the blit mapping is valid */
    92     if ((src->map->dst != dst) ||
    92     if ((src->map->dst != dst) ||
    93         (src->map->dst->format_version != src->map->format_version))
    93         (src->map->dst->format_version != src->map->format_version))
    94         if (SDL_MapSurface (src, dst) < 0)
    94         if (SDL_MapSurface(src, dst) < 0)
    95             return;
    95             return;
    96 
    96 
    97     /* Set up the blit information */
    97     /* Set up the blit information */
    98     if (srcrect) {
    98     if (srcrect) {
    99         info.s_pixels = (Uint8 *) src->pixels +
    99         info.s_pixels = (Uint8 *) src->pixels +
   126     info.aux_data = src->map->sw_data->aux_data;
   126     info.aux_data = src->map->sw_data->aux_data;
   127     info.src = src->format;
   127     info.src = src->format;
   128     info.table = src->map->table;
   128     info.table = src->map->table;
   129     info.dst = dst->format;
   129     info.dst = dst->format;
   130 
   130 
   131     src->map->sw_data->blit (&info);
   131     src->map->sw_data->blit(&info);
   132 }
   132 }
   133 
   133 
   134 
   134 
   135 /* 
   135 /* 
   136  * Another special version. Doesn't lock/unlock, and doesn't mess
   136  * Another special version. Doesn't lock/unlock, and doesn't mess
   137  * with flags and stuff. It just converts the surface, period.
   137  * with flags and stuff. It just converts the surface, period.
   138  * Does not convert into palletized formats.
   138  * Does not convert into palletized formats.
   139  */
   139  */
   140 static SDL_Surface *
   140 static SDL_Surface *
   141 glSDL_ConvertSurface (SDL_Surface * surface,
   141 glSDL_ConvertSurface(SDL_Surface * surface,
   142                       SDL_PixelFormat * format, Uint32 flags)
   142                      SDL_PixelFormat * format, Uint32 flags)
   143 {
   143 {
   144     SDL_Surface *convert;
   144     SDL_Surface *convert;
   145     Uint32 colorkey = 0;
   145     Uint32 colorkey = 0;
   146     Uint8 alpha = 0;
   146     Uint8 alpha = 0;
   147     Uint32 surface_flags;
   147     Uint32 surface_flags;
   148     SDL_Rect bounds;
   148     SDL_Rect bounds;
   149 
   149 
   150     /* Create a new surface with the desired format */
   150     /* Create a new surface with the desired format */
   151     convert = SDL_CreateRGBSurface (flags,
   151     convert = SDL_CreateRGBSurface(flags,
   152                                     surface->w, surface->h,
   152                                    surface->w, surface->h,
   153                                     format->BitsPerPixel, format->Rmask,
   153                                    format->BitsPerPixel, format->Rmask,
   154                                     format->Gmask, format->Bmask,
   154                                    format->Gmask, format->Bmask,
   155                                     format->Amask);
   155                                    format->Amask);
   156     if (convert == NULL) {
   156     if (convert == NULL) {
   157         return (NULL);
   157         return (NULL);
   158     }
   158     }
   159 
   159 
   160     /* Save the original surface color key and alpha */
   160     /* Save the original surface color key and alpha */
   163         /* Convert colourkeyed surfaces to RGBA if requested */
   163         /* Convert colourkeyed surfaces to RGBA if requested */
   164         if ((flags & SDL_SRCCOLORKEY) != SDL_SRCCOLORKEY && format->Amask) {
   164         if ((flags & SDL_SRCCOLORKEY) != SDL_SRCCOLORKEY && format->Amask) {
   165             surface_flags &= ~SDL_SRCCOLORKEY;
   165             surface_flags &= ~SDL_SRCCOLORKEY;
   166         } else {
   166         } else {
   167             colorkey = surface->format->colorkey;
   167             colorkey = surface->format->colorkey;
   168             SDL_SetColorKey (surface, 0, 0);
   168             SDL_SetColorKey(surface, 0, 0);
   169         }
   169         }
   170     }
   170     }
   171     if ((surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
   171     if ((surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
   172         /* Copy over the alpha channel to RGBA if requested */
   172         /* Copy over the alpha channel to RGBA if requested */
   173         if (format->Amask) {
   173         if (format->Amask) {
   174             surface->flags &= ~SDL_SRCALPHA;
   174             surface->flags &= ~SDL_SRCALPHA;
   175         } else {
   175         } else {
   176             alpha = surface->format->alpha;
   176             alpha = surface->format->alpha;
   177             SDL_SetAlpha (surface, 0, 0);
   177             SDL_SetAlpha(surface, 0, 0);
   178         }
   178         }
   179     }
   179     }
   180 
   180 
   181     /* Copy over the image data */
   181     /* Copy over the image data */
   182     bounds.x = 0;
   182     bounds.x = 0;
   183     bounds.y = 0;
   183     bounds.y = 0;
   184     bounds.w = surface->w;
   184     bounds.w = surface->w;
   185     bounds.h = surface->h;
   185     bounds.h = surface->h;
   186     glSDL_SoftBlit (surface, &bounds, convert, &bounds);
   186     glSDL_SoftBlit(surface, &bounds, convert, &bounds);
   187 
   187 
   188     /* Clean up the original surface, and update converted surface */
   188     /* Clean up the original surface, and update converted surface */
   189     if (convert != NULL) {
   189     if (convert != NULL) {
   190         SDL_SetClipRect (convert, &surface->clip_rect);
   190         SDL_SetClipRect(convert, &surface->clip_rect);
   191     }
   191     }
   192     if ((surface_flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) {
   192     if ((surface_flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) {
   193         Uint32 cflags = surface_flags & (SDL_SRCCOLORKEY | SDL_RLEACCELOK);
   193         Uint32 cflags = surface_flags & (SDL_SRCCOLORKEY | SDL_RLEACCELOK);
   194         if (convert != NULL) {
   194         if (convert != NULL) {
   195             Uint8 keyR, keyG, keyB;
   195             Uint8 keyR, keyG, keyB;
   196 
   196 
   197             SDL_GetRGB (colorkey, surface->format, &keyR, &keyG, &keyB);
   197             SDL_GetRGB(colorkey, surface->format, &keyR, &keyG, &keyB);
   198             SDL_SetColorKey (convert, cflags | (flags & SDL_RLEACCELOK),
   198             SDL_SetColorKey(convert, cflags | (flags & SDL_RLEACCELOK),
   199                              SDL_MapRGB (convert->format, keyR, keyG, keyB));
   199                             SDL_MapRGB(convert->format, keyR, keyG, keyB));
   200         }
   200         }
   201         SDL_SetColorKey (surface, cflags, colorkey);
   201         SDL_SetColorKey(surface, cflags, colorkey);
   202     }
   202     }
   203     if ((surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
   203     if ((surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
   204         Uint32 aflags = surface_flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
   204         Uint32 aflags = surface_flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
   205         if (convert != NULL) {
   205         if (convert != NULL) {
   206             SDL_SetAlpha (convert, aflags | (flags & SDL_RLEACCELOK), alpha);
   206             SDL_SetAlpha(convert, aflags | (flags & SDL_RLEACCELOK), alpha);
   207         }
   207         }
   208         if (format->Amask) {
   208         if (format->Amask) {
   209             surface->flags |= SDL_SRCALPHA;
   209             surface->flags |= SDL_SRCALPHA;
   210         } else {
   210         } else {
   211             SDL_SetAlpha (surface, aflags, alpha);
   211             SDL_SetAlpha(surface, aflags, alpha);
   212         }
   212         }
   213     }
   213     }
   214 
   214 
   215     /* We're ready to go! */
   215     /* We're ready to go! */
   216     return (convert);
   216     return (convert);
   228     GLuint texture;
   228     GLuint texture;
   229     GLenum sfactor, dfactor;
   229     GLenum sfactor, dfactor;
   230 } glstate;
   230 } glstate;
   231 
   231 
   232 static void
   232 static void
   233 glSDL_reset (void)
   233 glSDL_reset(void)
   234 {
   234 {
   235     glstate.do_blend = -1;
   235     glstate.do_blend = -1;
   236     glstate.do_blend = -1;
   236     glstate.do_blend = -1;
   237     glstate.texture = GLSDL_NOTEX;
   237     glstate.texture = GLSDL_NOTEX;
   238     glstate.sfactor = 0xffffffff;
   238     glstate.sfactor = 0xffffffff;
   239     glstate.dfactor = 0xffffffff;
   239     glstate.dfactor = 0xffffffff;
   240 }
   240 }
   241 
   241 
   242 static __inline__ void
   242 static __inline__ void
   243 glSDL_do_blend (_THIS, int on)
   243 glSDL_do_blend(_THIS, int on)
   244 {
   244 {
   245     if (glstate.do_blend == on)
   245     if (glstate.do_blend == on)
   246         return;
   246         return;
   247 
   247 
   248     if (on)
   248     if (on)
   249         this->glEnable (GL_BLEND);
   249         this->glEnable(GL_BLEND);
   250     else
   250     else
   251         this->glDisable (GL_BLEND);
   251         this->glDisable(GL_BLEND);
   252     glstate.do_blend = on;
   252     glstate.do_blend = on;
   253 }
   253 }
   254 
   254 
   255 static __inline__ void
   255 static __inline__ void
   256 glSDL_do_texture (_THIS, int on)
   256 glSDL_do_texture(_THIS, int on)
   257 {
   257 {
   258     if (glstate.do_texture == on)
   258     if (glstate.do_texture == on)
   259         return;
   259         return;
   260 
   260 
   261     if (on)
   261     if (on)
   262         this->glEnable (GL_TEXTURE_2D);
   262         this->glEnable(GL_TEXTURE_2D);
   263     else
   263     else
   264         this->glDisable (GL_TEXTURE_2D);
   264         this->glDisable(GL_TEXTURE_2D);
   265     glstate.do_texture = on;
   265     glstate.do_texture = on;
   266 }
   266 }
   267 
   267 
   268 static __inline__ void
   268 static __inline__ void
   269 glSDL_blendfunc (_THIS, GLenum sfactor, GLenum dfactor)
   269 glSDL_blendfunc(_THIS, GLenum sfactor, GLenum dfactor)
   270 {
   270 {
   271     if ((sfactor == glstate.sfactor) && (dfactor == glstate.dfactor))
   271     if ((sfactor == glstate.sfactor) && (dfactor == glstate.dfactor))
   272         return;
   272         return;
   273 
   273 
   274     this->glBlendFunc (sfactor, dfactor);
   274     this->glBlendFunc(sfactor, dfactor);
   275 
   275 
   276     glstate.sfactor = sfactor;
   276     glstate.sfactor = sfactor;
   277     glstate.dfactor = dfactor;
   277     glstate.dfactor = dfactor;
   278 }
   278 }
   279 
   279 
   280 static __inline__ void
   280 static __inline__ void
   281 glSDL_texture (_THIS, GLuint tx)
   281 glSDL_texture(_THIS, GLuint tx)
   282 {
   282 {
   283     if (tx == glstate.texture)
   283     if (tx == glstate.texture)
   284         return;
   284         return;
   285 
   285 
   286     this->glBindTexture (GL_TEXTURE_2D, tx);
   286     this->glBindTexture(GL_TEXTURE_2D, tx);
   287     glstate.texture = tx;
   287     glstate.texture = tx;
   288 }
   288 }
   289 
   289 
   290 
   290 
   291 
   291 
   324     SDL_Surface *next;          /* The next Surface in our linked list of hardware surfaces ; == NULL if first surface */
   324     SDL_Surface *next;          /* The next Surface in our linked list of hardware surfaces ; == NULL if first surface */
   325     SDL_Surface *prev;          /* The prev Surface in our linked list of hardware surfaces ; == NULL if last surface */
   325     SDL_Surface *prev;          /* The prev Surface in our linked list of hardware surfaces ; == NULL if last surface */
   326 } private_hwdata;
   326 } private_hwdata;
   327 
   327 
   328 /* some function prototypes */
   328 /* some function prototypes */
   329 static void glSDL_Invalidate (SDL_Surface * surface, SDL_Rect * area);
   329 static void glSDL_Invalidate(SDL_Surface * surface, SDL_Rect * area);
   330 static void glSDL_SetLogicSize (_THIS, SDL_Surface * surface, int w, int h);
   330 static void glSDL_SetLogicSize(_THIS, SDL_Surface * surface, int w, int h);
   331 static private_hwdata *glSDL_UploadSurface (_THIS, SDL_Surface * surface);
   331 static private_hwdata *glSDL_UploadSurface(_THIS, SDL_Surface * surface);
   332 static private_hwdata *glSDL_GetTexInfo (SDL_Surface * surface);
   332 static private_hwdata *glSDL_GetTexInfo(SDL_Surface * surface);
   333 static void glSDL_init_formats (_THIS);
   333 static void glSDL_init_formats(_THIS);
   334 static private_hwdata *glSDL_AddTexInfo (_THIS, SDL_Surface * surface);
   334 static private_hwdata *glSDL_AddTexInfo(_THIS, SDL_Surface * surface);
   335 static void glSDL_RemoveTexInfo (_THIS, SDL_Surface * surface);
   335 static void glSDL_RemoveTexInfo(_THIS, SDL_Surface * surface);
   336 static void glSDL_UnloadTexture (_THIS, private_hwdata * txi);
   336 static void glSDL_UnloadTexture(_THIS, private_hwdata * txi);
   337 static int glSDL_BlitGL (_THIS, SDL_Surface * src,
   337 static int glSDL_BlitGL(_THIS, SDL_Surface * src,
   338                          SDL_Rect * srcrect, SDL_Rect * dstrect);
   338                         SDL_Rect * srcrect, SDL_Rect * dstrect);
   339 
   339 
   340 /* some variables */
   340 /* some variables */
   341 static GLint maxtexsize = -1;
   341 static GLint maxtexsize = -1;
   342 static SDL_PixelFormat *RGBfmt = NULL;
   342 static SDL_PixelFormat *RGBfmt = NULL;
   343 static SDL_PixelFormat *RGBAfmt = NULL;
   343 static SDL_PixelFormat *RGBAfmt = NULL;
   348 /* pointer to the beggining of the list used for memory allocation */
   348 /* pointer to the beggining of the list used for memory allocation */
   349 SDL_Surface *first = NULL;
   349 SDL_Surface *first = NULL;
   350 
   350 
   351 #ifdef DEBUG_GLSDL
   351 #ifdef DEBUG_GLSDL
   352 static __inline__ int
   352 static __inline__ int
   353 GLERET (const char *txt)
   353 GLERET(const char *txt)
   354 {
   354 {
   355     fprintf (stderr, "glSDL ERROR: '%s'\n", txt);
   355     fprintf(stderr, "glSDL ERROR: '%s'\n", txt);
   356     return -1;
   356     return -1;
   357 }
   357 }
   358 static __inline__ void
   358 static __inline__ void
   359 GLERR (const char *txt)
   359 GLERR(const char *txt)
   360 {
   360 {
   361     fprintf (stderr, "glSDL ERROR: '%s'\n", txt);
   361     fprintf(stderr, "glSDL ERROR: '%s'\n", txt);
   362 }
   362 }
   363 #else
   363 #else
   364 #define	GLERET(x)	(-1)
   364 #define	GLERET(x)	(-1)
   365 #define	GLERR(x)
   365 #define	GLERR(x)
   366 #endif
   366 #endif
   393 #else
   393 #else
   394     NULL;
   394     NULL;
   395 #endif
   395 #endif
   396 
   396 
   397 static int
   397 static int
   398 glSDL_Available (void)
   398 glSDL_Available(void)
   399 {
   399 {
   400 #ifdef DEBUG_GLSDL
   400 #ifdef DEBUG_GLSDL
   401     fprintf (stderr, "available\n");
   401     fprintf(stderr, "available\n");
   402 #endif
   402 #endif
   403     if (opengl_bootstrap == NULL)
   403     if (opengl_bootstrap == NULL)
   404         return 0;
   404         return 0;
   405     return (opengl_bootstrap->available ());
   405     return (opengl_bootstrap->available());
   406 }
   406 }
   407 
   407 
   408 static void
   408 static void
   409 glSDL_DeleteDevice (SDL_VideoDevice * device)
   409 glSDL_DeleteDevice(SDL_VideoDevice * device)
   410 {
   410 {
   411     SDL_free (device->hidden);
   411     SDL_free(device->hidden);
   412     SDL_free (device);
   412     SDL_free(device);
   413 }
   413 }
   414 
   414 
   415 /* Create a glSDL device */
   415 /* Create a glSDL device */
   416 static SDL_VideoDevice *
   416 static SDL_VideoDevice *
   417 glSDL_CreateDevice (int devindex)
   417 glSDL_CreateDevice(int devindex)
   418 {
   418 {
   419     SDL_VideoDevice *device;
   419     SDL_VideoDevice *device;
   420 #ifdef DEBUG_GLSDL
   420 #ifdef DEBUG_GLSDL
   421     fprintf (stderr, "entering createdevice\n");
   421     fprintf(stderr, "entering createdevice\n");
   422 #endif
   422 #endif
   423 
   423 
   424     /* Create the device with the underlying driver */
   424     /* Create the device with the underlying driver */
   425     device = opengl_bootstrap->create (devindex);
   425     device = opengl_bootstrap->create(devindex);
   426 
   426 
   427     /* Save the video device contents for future use */
   427     /* Save the video device contents for future use */
   428     SDL_memcpy (&underlying_device, device, sizeof (SDL_VideoDevice));
   428     SDL_memcpy(&underlying_device, device, sizeof(SDL_VideoDevice));
   429 
   429 
   430     /* Hook glSDL on the video device */
   430     /* Hook glSDL on the video device */
   431     device->VideoInit = glSDL_VideoInit;
   431     device->VideoInit = glSDL_VideoInit;
   432     device->ListModes = glSDL_ListModes;
   432     device->ListModes = glSDL_ListModes;
   433     device->VideoQuit = glSDL_VideoQuit;
   433     device->VideoQuit = glSDL_VideoQuit;
   460     device->ToggleFullScreen = NULL;
   460     device->ToggleFullScreen = NULL;
   461 
   461 
   462     device->free = glSDL_DeleteDevice;
   462     device->free = glSDL_DeleteDevice;
   463 
   463 
   464 #ifdef DEBUG_GLSDL
   464 #ifdef DEBUG_GLSDL
   465     fprintf (stderr, "leaving createdevice\n");
   465     fprintf(stderr, "leaving createdevice\n");
   466 #endif
   466 #endif
   467 
   467 
   468     return device;
   468     return device;
   469 }
   469 }
   470 
   470 
   473     "glSDL", "glSDL - SDL over OpenGL",
   473     "glSDL", "glSDL - SDL over OpenGL",
   474     glSDL_Available, glSDL_CreateDevice
   474     glSDL_Available, glSDL_CreateDevice
   475 };
   475 };
   476 
   476 
   477 static int
   477 static int
   478 glSDL_VideoInit (_THIS, SDL_PixelFormat * vformat)
   478 glSDL_VideoInit(_THIS, SDL_PixelFormat * vformat)
   479 {
   479 {
   480     int r;
   480     int r;
   481     printf ("glSDL videoinit\n");
   481     printf("glSDL videoinit\n");
   482 #ifdef DEBUG_GLSDL
   482 #ifdef DEBUG_GLSDL
   483     fprintf (stderr, "videoinit\n");
   483     fprintf(stderr, "videoinit\n");
   484 #endif
   484 #endif
   485     r = underlying_device.VideoInit (this, vformat);
   485     r = underlying_device.VideoInit(this, vformat);
   486     this->info.hw_available = 1;
   486     this->info.hw_available = 1;
   487     this->info.blit_hw = 1;
   487     this->info.blit_hw = 1;
   488     this->info.blit_hw_CC = 1;
   488     this->info.blit_hw_CC = 1;
   489     this->info.blit_hw_A = 1;
   489     this->info.blit_hw_A = 1;
   490     this->info.blit_sw = 1;
   490     this->info.blit_sw = 1;
   494 
   494 
   495     return r;
   495     return r;
   496 }
   496 }
   497 
   497 
   498 SDL_Rect **
   498 SDL_Rect **
   499 glSDL_ListModes (_THIS, SDL_PixelFormat * format, Uint32 flags)
   499 glSDL_ListModes(_THIS, SDL_PixelFormat * format, Uint32 flags)
   500 {
   500 {
   501     return ((SDL_Rect **) - 1);
   501     return ((SDL_Rect **) - 1);
   502 }
   502 }
   503 
   503 
   504 static void
   504 static void
   505 glSDL_VideoQuit (_THIS)
   505 glSDL_VideoQuit(_THIS)
   506 {
   506 {
   507     SDL_Surface *scr;
   507     SDL_Surface *scr;
   508 
   508 
   509     /* free all hwdata structures */
   509     /* free all hwdata structures */
   510     while (first != NULL)
   510     while (first != NULL)
   511         glSDL_RemoveTexInfo (this, first);
   511         glSDL_RemoveTexInfo(this, first);
   512 
   512 
   513     SDL_free (mirrorbuf);
   513     SDL_free(mirrorbuf);
   514     mirrorbuf = NULL;
   514     mirrorbuf = NULL;
   515 
   515 
   516     SDL_FreeFormat (RGBfmt);
   516     SDL_FreeFormat(RGBfmt);
   517     SDL_FreeFormat (RGBAfmt);
   517     SDL_FreeFormat(RGBAfmt);
   518     RGBfmt = RGBAfmt = NULL;
   518     RGBfmt = RGBAfmt = NULL;
   519 
   519 
   520     SDL_FreeFormat (this->displayformatalphapixel);
   520     SDL_FreeFormat(this->displayformatalphapixel);
   521     this->displayformatalphapixel = NULL;
   521     this->displayformatalphapixel = NULL;
   522 
   522 
   523     SDL_FreeSurface (OpenGL_Surface);
   523     SDL_FreeSurface(OpenGL_Surface);
   524     OpenGL_Surface = NULL;
   524     OpenGL_Surface = NULL;
   525 
   525 
   526     /* restore the flags to gracefully exit from fullscreen */
   526     /* restore the flags to gracefully exit from fullscreen */
   527     this->screen->flags = old_screen_flags;
   527     this->screen->flags = old_screen_flags;
   528 
   528 
   529     /* keep the screen */
   529     /* keep the screen */
   530     scr = this->screen;
   530     scr = this->screen;
   531 
   531 
   532     /* we cleaned up our stuff, now restore the underlying video driver */
   532     /* we cleaned up our stuff, now restore the underlying video driver */
   533     SDL_memcpy (this, &underlying_device, sizeof (SDL_VideoDevice));
   533     SDL_memcpy(this, &underlying_device, sizeof(SDL_VideoDevice));
   534 
   534 
   535     this->screen = scr;
   535     this->screen = scr;
   536 
   536 
   537     /* call the underlying video driver's VideoQuit function */
   537     /* call the underlying video driver's VideoQuit function */
   538     this->VideoQuit (this);
   538     this->VideoQuit(this);
   539 }
   539 }
   540 
   540 
   541 static SDL_Surface *
   541 static SDL_Surface *
   542 glSDL_SetVideoMode (_THIS, SDL_Surface * current, int width, int height,
   542 glSDL_SetVideoMode(_THIS, SDL_Surface * current, int width, int height,
   543                     int bpp, Uint32 flags)
   543                    int bpp, Uint32 flags)
   544 {
   544 {
   545     SDL_Surface *hooked_screen;
   545     SDL_Surface *hooked_screen;
   546     int i;
   546     int i;
   547     int flag_doublebuf = 0;
   547     int flag_doublebuf = 0;
   548 
   548 
   549     if (opengl_bootstrap == NULL) {
   549     if (opengl_bootstrap == NULL) {
   550         GLERR ("No bootstrap for glSDL compiled in !\n");
   550         GLERR("No bootstrap for glSDL compiled in !\n");
   551         return NULL;
   551         return NULL;
   552     }
   552     }
   553 
   553 
   554     /* we don't have OpenGL */
   554     /* we don't have OpenGL */
   555     if ((flags & SDL_INTERNALOPENGL) == SDL_INTERNALOPENGL) {
   555     if ((flags & SDL_INTERNALOPENGL) == SDL_INTERNALOPENGL) {
   556         GLERR ("OpenGL video modes are not supported by glSDL !\n");
   556         GLERR("OpenGL video modes are not supported by glSDL !\n");
   557         return (NULL);
   557         return (NULL);
   558     }
   558     }
   559 
   559 
   560     /* 
   560     /* 
   561      * Adjust the flags
   561      * Adjust the flags
   564     flags |= SDL_INTERNALOPENGL;
   564     flags |= SDL_INTERNALOPENGL;
   565 
   565 
   566     /* remember whether the user requested DOUBLEBUF */
   566     /* remember whether the user requested DOUBLEBUF */
   567 
   567 
   568     if (flags & SDL_DOUBLEBUF) {
   568     if (flags & SDL_DOUBLEBUF) {
   569         SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1);
   569         SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
   570         flag_doublebuf = 1;
   570         flag_doublebuf = 1;
   571     } else {
   571     } else {
   572         SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 0);
   572         SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0);
   573         flag_doublebuf = 0;
   573         flag_doublebuf = 0;
   574     }
   574     }
   575 
   575 
   576     hooked_screen =
   576     hooked_screen =
   577         underlying_device.SetVideoMode (this, current, width, height, 0,
   577         underlying_device.SetVideoMode(this, current, width, height, 0,
   578                                         flags);
   578                                        flags);
   579 
   579 
   580     if (!hooked_screen) {
   580     if (!hooked_screen) {
   581         GLERR ("Unable to open an OpenGL window !\n");
   581         GLERR("Unable to open an OpenGL window !\n");
   582         return (NULL);
   582         return (NULL);
   583     }
   583     }
   584 
   584 
   585     /* save the screen flags for restore time */
   585     /* save the screen flags for restore time */
   586     old_screen_flags = hooked_screen->flags;
   586     old_screen_flags = hooked_screen->flags;
   587 
   587 
   588 #ifdef DEBUG_GLSDL
   588 #ifdef DEBUG_GLSDL
   589     fprintf (stderr, "got %d bpp\n", bpp);
   589     fprintf(stderr, "got %d bpp\n", bpp);
   590 #endif
   590 #endif
   591 
   591 
   592     /* setup the public surface format
   592     /* setup the public surface format
   593      * glSDL always returns the bpp its asked
   593      * glSDL always returns the bpp its asked
   594      */
   594      */
   595     switch (bpp) {
   595     switch (bpp) {
   596     case 32:
   596     case 32:
   597         this->is_32bit = 1;
   597         this->is_32bit = 1;
   598         this->screen = SDL_CreateRGBSurface (flags, width, height, bpp,
   598         this->screen = SDL_CreateRGBSurface(flags, width, height, bpp,
   599 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
   599 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
   600                                              0x00FF0000,
   600                                             0x00FF0000,
   601                                              0x0000FF00,
   601                                             0x0000FF00, 0x000000FF, 0x00000000
   602                                              0x000000FF, 0x00000000
       
   603 #else
   602 #else
   604                                              0x0000FF00,
   603                                             0x0000FF00,
   605                                              0x00FF0000,
   604                                             0x00FF0000, 0xFF000000, 0x00000000
   606                                              0xFF000000, 0x00000000
       
   607 #endif
   605 #endif
   608             );
   606             );
   609         break;
   607         break;
   610     case 24:
   608     case 24:
   611         this->is_32bit = 0;
   609         this->is_32bit = 0;
   612         this->screen = SDL_CreateRGBSurface (flags, width, height, bpp,
   610         this->screen = SDL_CreateRGBSurface(flags, width, height, bpp,
   613 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
   611 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
   614                                              0x00FF0000,
   612                                             0x00FF0000,
   615                                              0x0000FF00,
   613                                             0x0000FF00, 0x000000FF, 0x00000000
   616                                              0x000000FF, 0x00000000
       
   617 #else
   614 #else
   618                                              0x0000FF00,
   615                                             0x0000FF00,
   619                                              0x00FF0000,
   616                                             0x00FF0000, 0xFF000000, 0x00000000
   620                                              0xFF000000, 0x00000000
       
   621 #endif
   617 #endif
   622             );
   618             );
   623         break;
   619         break;
   624     case 16:
   620     case 16:
   625         this->is_32bit = 0;
   621         this->is_32bit = 0;
   626         this->screen = SDL_CreateRGBSurface (flags, width, height, bpp,
   622         this->screen = SDL_CreateRGBSurface(flags, width, height, bpp,
   627 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
   623 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
   628                                              0x0000F800,
   624                                             0x0000F800,
   629                                              0x000007E0,
   625                                             0x000007E0, 0x0000001F, 0x00000000
   630                                              0x0000001F, 0x00000000
       
   631 #else
   626 #else
   632                                              0x0000001F,
   627                                             0x0000001F,
   633                                              0x000007E0,
   628                                             0x000007E0, 0x0000F800, 0x00000000
   634                                              0x0000F800, 0x00000000
       
   635 #endif
   629 #endif
   636             );
   630             );
   637         break;
   631         break;
   638     case 15:
   632     case 15:
   639         this->is_32bit = 0;
   633         this->is_32bit = 0;
   640         this->screen = SDL_CreateRGBSurface (flags, width, height, bpp,
   634         this->screen = SDL_CreateRGBSurface(flags, width, height, bpp,
   641 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
   635 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
   642                                              0x00007C00,
   636                                             0x00007C00,
   643                                              0x000003E0,
   637                                             0x000003E0, 0x0000001F, 0x00000000
   644                                              0x0000001F, 0x00000000
       
   645 #else
   638 #else
   646                                              0x0000001F,
   639                                             0x0000001F,
   647                                              0x000003E0,
   640                                             0x000003E0, 0x00007C00, 0x00000000
   648                                              0x00007C00, 0x00000000
       
   649 #endif
   641 #endif
   650             );
   642             );
   651         break;
   643         break;
   652     case 8:
   644     case 8:
   653     default:
   645     default:
   654         this->is_32bit = 0;
   646         this->is_32bit = 0;
   655         this->screen =
   647         this->screen =
   656             SDL_CreateRGBSurface (flags, width, height, bpp, 0, 0, 0, 0);
   648             SDL_CreateRGBSurface(flags, width, height, bpp, 0, 0, 0, 0);
   657         /* give it a default palette if 8 bpp
   649         /* give it a default palette if 8 bpp
   658          * note : SDL already takes care of the palette for 4 bits & 1 bit surfaces 
   650          * note : SDL already takes care of the palette for 4 bits & 1 bit surfaces 
   659          */
   651          */
   660 /*			if (bpp==8)
   652 /*			if (bpp==8)
   661 			{
   653 			{
   671     /* add SDL_DOUBLEBUF if it was requested */
   663     /* add SDL_DOUBLEBUF if it was requested */
   672     if (flag_doublebuf)
   664     if (flag_doublebuf)
   673         this->screen->flags |= SDL_DOUBLEBUF;
   665         this->screen->flags |= SDL_DOUBLEBUF;
   674 
   666 
   675     /* Tell SDL the alpha pixel format we'd like to have */
   667     /* Tell SDL the alpha pixel format we'd like to have */
   676     this->displayformatalphapixel = SDL_AllocFormat (32,
   668     this->displayformatalphapixel = SDL_AllocFormat(32,
   677 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
   669 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
   678                                                      0xFF000000,
   670                                                     0xFF000000,
   679                                                      0x00FF0000,
   671                                                     0x00FF0000,
   680                                                      0x0000FF00, 0x000000FF
   672                                                     0x0000FF00, 0x000000FF
   681 #else
   673 #else
   682                                                      0x000000FF,
   674                                                     0x000000FF,
   683                                                      0x0000FF00,
   675                                                     0x0000FF00,
   684                                                      0x00FF0000, 0xFF000000
   676                                                     0x00FF0000, 0xFF000000
   685 #endif
   677 #endif
   686         );
   678         );
   687 
   679 
   688     /* Now create the raw OpenGL surface */
   680     /* Now create the raw OpenGL surface */
   689     OpenGL_Surface = SDL_CreateRGBSurface (flags, width, height, 24,
   681     OpenGL_Surface = SDL_CreateRGBSurface(flags, width, height, 24,
   690 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
   682 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
   691                                            0x000000FF,
   683                                           0x000000FF,
   692                                            0x0000FF00, 0x00FF0000, 0x00000000
   684                                           0x0000FF00, 0x00FF0000, 0x00000000
   693 #else
   685 #else
   694                                            0xFF000000,
   686                                           0xFF000000,
   695                                            0x00FF0000, 0x0000FF00, 0x00000000
   687                                           0x00FF0000, 0x0000FF00, 0x00000000
   696 #endif
   688 #endif
   697         );
   689         );
   698 
   690 
   699     /* Here we have to setup OpenGL funcs ourselves */
   691     /* Here we have to setup OpenGL funcs ourselves */
   700 #ifndef __QNXNTO__
   692 #ifndef __QNXNTO__
   710 #define SDL_PROC(ret,func,params) this->func=func;
   702 #define SDL_PROC(ret,func,params) this->func=func;
   711 #endif /* __QNXNTO__ */
   703 #endif /* __QNXNTO__ */
   712 #include "../SDL_glfuncs.h"
   704 #include "../SDL_glfuncs.h"
   713 #undef SDL_PROC
   705 #undef SDL_PROC
   714 
   706 
   715     if (this->GL_MakeCurrent (this) < 0)
   707     if (this->GL_MakeCurrent(this) < 0)
   716         return (NULL);
   708         return (NULL);
   717 #define SDL_PROC(ret,func,params) \
   709 #define SDL_PROC(ret,func,params) \
   718 	do { \
   710 	do { \
   719 		this->func = SDL_GL_GetProcAddress(#func); \
   711 		this->func = SDL_GL_GetProcAddress(#func); \
   720 			if ( ! this->func ) { \
   712 			if ( ! this->func ) { \
   727 
   719 
   728 
   720 
   729 #ifdef	FAKE_MAXTEXSIZE
   721 #ifdef	FAKE_MAXTEXSIZE
   730     maxtexsize = FAKE_MAXTEXSIZE;
   722     maxtexsize = FAKE_MAXTEXSIZE;
   731 #else
   723 #else
   732     this->glGetIntegerv (GL_MAX_TEXTURE_SIZE, &maxtexsize);
   724     this->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxtexsize);
   733 #endif
   725 #endif
   734 #ifdef DEBUG_GLSDL
   726 #ifdef DEBUG_GLSDL
   735     fprintf (stderr, "glSDL: Max texture size: %d\n", maxtexsize);
   727     fprintf(stderr, "glSDL: Max texture size: %d\n", maxtexsize);
   736 #endif
   728 #endif
   737 
   729 
   738     glSDL_init_formats (this);
   730     glSDL_init_formats(this);
   739 
   731 
   740     if (flag_doublebuf)
   732     if (flag_doublebuf)
   741         this->glDrawBuffer (GL_BACK);
   733         this->glDrawBuffer(GL_BACK);
   742     else
   734     else
   743         this->glDrawBuffer (GL_FRONT);
   735         this->glDrawBuffer(GL_FRONT);
   744 
   736 
   745     this->glDisable (GL_DITHER);
   737     this->glDisable(GL_DITHER);
   746 
   738 
   747     if (glSDL_AddTexInfo (this, this->screen) < 0) {
   739     if (glSDL_AddTexInfo(this, this->screen) < 0) {
   748         GLERR ("HookDevice() failed to add info to screen surface!");
   740         GLERR("HookDevice() failed to add info to screen surface!");
   749         return NULL;
   741         return NULL;
   750     }
   742     }
   751 
   743 
   752     glSDL_SetLogicSize (this, this->screen, this->screen->w, this->screen->h);
   744     glSDL_SetLogicSize(this, this->screen, this->screen->w, this->screen->h);
   753 
   745 
   754     glSDL_do_texture (this, 0);
   746     glSDL_do_texture(this, 0);
   755     glSDL_do_blend (this, 0);
   747     glSDL_do_blend(this, 0);
   756 
   748 
   757     for (i = 0; i < 1 + flag_doublebuf; ++i) {
   749     for (i = 0; i < 1 + flag_doublebuf; ++i) {
   758         this->glBegin (GL_TRIANGLE_FAN);
   750         this->glBegin(GL_TRIANGLE_FAN);
   759         this->glColor3ub (0, 0, 0);
   751         this->glColor3ub(0, 0, 0);
   760         this->glVertex2i (0, 0);
   752         this->glVertex2i(0, 0);
   761         this->glVertex2i (this->screen->w, 0);
   753         this->glVertex2i(this->screen->w, 0);
   762         this->glVertex2i (this->screen->w, this->screen->h);
   754         this->glVertex2i(this->screen->w, this->screen->h);
   763         this->glVertex2i (0, this->screen->h);
   755         this->glVertex2i(0, this->screen->h);
   764         this->glEnd ();
   756         this->glEnd();
   765         if (!i)
   757         if (!i)
   766             this->GL_SwapBuffers (this);
   758             this->GL_SwapBuffers(this);
   767     }
   759     }
   768 
   760 
   769     mirrorbuf = SDL_malloc (this->screen->h * this->screen->pitch);
   761     mirrorbuf = SDL_malloc(this->screen->h * this->screen->pitch);
   770     if (!mirrorbuf) {
   762     if (!mirrorbuf) {
   771         GLERR ("HookDevice() failed to allocate temp buffer for mirroring!");
   763         GLERR("HookDevice() failed to allocate temp buffer for mirroring!");
   772         return NULL;
   764         return NULL;
   773     }
   765     }
   774 
   766 
   775     return this->screen;
   767     return this->screen;
   776 }
   768 }
   777 
   769 
   778 static int
   770 static int
   779 glSDL_SetColors (_THIS, int firstcolor, int ncolors, SDL_Color * colors)
   771 glSDL_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color * colors)
   780 {
   772 {
   781     /* We don't need to fill this one */
   773     /* We don't need to fill this one */
   782     return 0;
   774     return 0;
   783 }
   775 }
   784 
   776 
   785 
   777 
   786 #ifdef DEBUG_GLSDL
   778 #ifdef DEBUG_GLSDL
   787 static void
   779 static void
   788 glSDL_print_glerror (_THIS, int point)
   780 glSDL_print_glerror(_THIS, int point)
   789 {
   781 {
   790     const char *err = "<unknown>";
   782     const char *err = "<unknown>";
   791     switch (this->glGetError ()) {
   783     switch (this->glGetError()) {
   792     case GL_NO_ERROR:
   784     case GL_NO_ERROR:
   793         return;
   785         return;
   794     case GL_INVALID_ENUM:
   786     case GL_INVALID_ENUM:
   795         err = "GL_INVALID_ENUM";
   787         err = "GL_INVALID_ENUM";
   796         break;
   788         break;
   809     case GL_OUT_OF_MEMORY:
   801     case GL_OUT_OF_MEMORY:
   810         err = "GL_OUT_OF_MEMORY";
   802         err = "GL_OUT_OF_MEMORY";
   811     default:
   803     default:
   812         break;
   804         break;
   813     }
   805     }
   814     fprintf (stderr, "OpenGL error \"%s\" at point %d.\n", err, point);
   806     fprintf(stderr, "OpenGL error \"%s\" at point %d.\n", err, point);
   815 }
   807 }
   816 #endif
   808 #endif
   817 
   809 
   818 /* Get texinfo for a surface. */
   810 /* Get texinfo for a surface. */
   819 static __inline__ private_hwdata *
   811 static __inline__ private_hwdata *
   820 glSDL_GetTexInfo (SDL_Surface * surface)
   812 glSDL_GetTexInfo(SDL_Surface * surface)
   821 {
   813 {
   822     if (!surface)
   814     if (!surface)
   823         return NULL;
   815         return NULL;
   824     return surface->hwdata;
   816     return surface->hwdata;
   825 }
   817 }
   826 
   818 
   827 
   819 
   828 /* Allocate a "blank" texinfo for a suface. */
   820 /* Allocate a "blank" texinfo for a suface. */
   829 static private_hwdata *
   821 static private_hwdata *
   830 glSDL_AllocTexInfo (SDL_Surface * surface)
   822 glSDL_AllocTexInfo(SDL_Surface * surface)
   831 {
   823 {
   832     private_hwdata *txi;
   824     private_hwdata *txi;
   833     if (!surface)
   825     if (!surface)
   834         return NULL;
   826         return NULL;
   835 
   827 
   836     txi = glSDL_GetTexInfo (surface);
   828     txi = glSDL_GetTexInfo(surface);
   837     if (txi)
   829     if (txi)
   838         return txi;             /* There already is one! --> */
   830         return txi;             /* There already is one! --> */
   839 
   831 
   840     /* ...and hook a new texinfo struct up to it. */
   832     /* ...and hook a new texinfo struct up to it. */
   841     txi = (private_hwdata *) SDL_calloc (1, sizeof (private_hwdata));
   833     txi = (private_hwdata *) SDL_calloc(1, sizeof(private_hwdata));
   842     if (!txi) {
   834     if (!txi) {
   843         GLERR ("AllocTexInfo(): Failed allocating TexInfo struct!");
   835         GLERR("AllocTexInfo(): Failed allocating TexInfo struct!");
   844         return NULL;
   836         return NULL;
   845     }
   837     }
   846     txi->temporary = 1;
   838     txi->temporary = 1;
   847 #ifdef DEBUG_GLSDL
   839 #ifdef DEBUG_GLSDL
   848     fprintf (stderr, "glSDL: Allocated TexInfo %p.\n", txi);
   840     fprintf(stderr, "glSDL: Allocated TexInfo %p.\n", txi);
   849 #endif
   841 #endif
   850     return txi;
   842     return txi;
   851 }
   843 }
   852 
   844 
   853 
   845 
   854 static void
   846 static void
   855 glSDL_FreeTexInfo (_THIS, private_hwdata * txi)
   847 glSDL_FreeTexInfo(_THIS, private_hwdata * txi)
   856 {
   848 {
   857     if (!txi)
   849     if (!txi)
   858         return;
   850         return;
   859 
   851 
   860     glSDL_UnloadTexture (this, txi);
   852     glSDL_UnloadTexture(this, txi);
   861     SDL_free (txi->texture);
   853     SDL_free(txi->texture);
   862     SDL_free (txi);
   854     SDL_free(txi);
   863 #ifdef DEBUG_GLSDL
   855 #ifdef DEBUG_GLSDL
   864     fprintf (stderr, "glSDL: Freed TexInfo %p.\n", txi);
   856     fprintf(stderr, "glSDL: Freed TexInfo %p.\n", txi);
   865 #endif
   857 #endif
   866 }
   858 }
   867 
   859 
   868 
   860 
   869 /* Detach and free the texinfo of a surface. */
   861 /* Detach and free the texinfo of a surface. */
   870 static void
   862 static void
   871 glSDL_RemoveTexInfo (_THIS, SDL_Surface * surface)
   863 glSDL_RemoveTexInfo(_THIS, SDL_Surface * surface)
   872 {
   864 {
   873     SDL_Surface *next, *prev;
   865     SDL_Surface *next, *prev;
   874     if (!glSDL_GetTexInfo (surface))
   866     if (!glSDL_GetTexInfo(surface))
   875         return;
   867         return;
   876 
   868 
   877     /* maintain our doubly linked list */
   869     /* maintain our doubly linked list */
   878     next = surface->hwdata->next;
   870     next = surface->hwdata->next;
   879     prev = surface->hwdata->prev;
   871     prev = surface->hwdata->prev;
   884     }
   876     }
   885     if (next != NULL) {
   877     if (next != NULL) {
   886         next->hwdata->prev = prev;
   878         next->hwdata->prev = prev;
   887     }
   879     }
   888 
   880 
   889     glSDL_FreeTexInfo (this, surface->hwdata);
   881     glSDL_FreeTexInfo(this, surface->hwdata);
   890     surface->hwdata = NULL;
   882     surface->hwdata = NULL;
   891 }
   883 }
   892 
   884 
   893 
   885 
   894 /*
   886 /*
   895  * Calculate chopping/tiling of a surface to
   887  * Calculate chopping/tiling of a surface to
   896  * fit it into the smallest possible OpenGL
   888  * fit it into the smallest possible OpenGL
   897  * texture.
   889  * texture.
   898  */
   890  */
   899 static int
   891 static int
   900 glSDL_CalcChop (private_hwdata * txi)
   892 glSDL_CalcChop(private_hwdata * txi)
   901 {
   893 {
   902     int rows, vw, vh;
   894     int rows, vw, vh;
   903     int vertical = 0;
   895     int vertical = 0;
   904     int texsize;
   896     int texsize;
   905     int lastw, lasth, minsize;
   897     int lastw, lasth, minsize;
   906 
   898 
   907     vw = txi->virt.w;
   899     vw = txi->virt.w;
   908     vh = txi->virt.h;
   900     vh = txi->virt.h;
   909 
   901 
   910 #ifdef DEBUG_GLSDL_CHOP
   902 #ifdef DEBUG_GLSDL_CHOP
   911     fprintf (stderr, "w=%d, h=%d ", vw, vh);
   903     fprintf(stderr, "w=%d, h=%d ", vw, vh);
   912 #endif
   904 #endif
   913     if (vh > vw) {
   905     if (vh > vw) {
   914         int t = vw;
   906         int t = vw;
   915         vw = vh;
   907         vw = vh;
   916         vh = t;
   908         vh = t;
   917         vertical = 1;
   909         vertical = 1;
   918 #ifdef DEBUG_GLSDL_CHOP
   910 #ifdef DEBUG_GLSDL_CHOP
   919         fprintf (stderr, "(vertical) \t");
   911         fprintf(stderr, "(vertical) \t");
   920 #endif
   912 #endif
   921     }
   913     }
   922 
   914 
   923     /*
   915     /*
   924      * Check whether this is a "huge" surface - at least one dimension
   916      * Check whether this is a "huge" surface - at least one dimension
   925      * must be <= than the maximum texture size, or we'll have to chop
   917      * must be <= than the maximum texture size, or we'll have to chop
   926      * in both directions.
   918      * in both directions.
   927      */
   919      */
   928 #ifdef DEBUG_GLSDL
   920 #ifdef DEBUG_GLSDL
   929     if (maxtexsize < 0)
   921     if (maxtexsize < 0)
   930         return GLERET ("glSDL_CalcChop() called before OpenGL init!");
   922         return GLERET("glSDL_CalcChop() called before OpenGL init!");
   931 #endif
   923 #endif
   932     if (vh > maxtexsize) {
   924     if (vh > maxtexsize) {
   933         /*
   925         /*
   934          * Very simple hack for now; we just tile
   926          * Very simple hack for now; we just tile
   935          * both ways with maximum size textures.
   927          * both ways with maximum size textures.
   943         txi->tilespertex = 1;
   935         txi->tilespertex = 1;
   944 
   936 
   945         /* Calculate number of textures needed */
   937         /* Calculate number of textures needed */
   946         txi->textures = (vw + texsize - 1) / texsize;
   938         txi->textures = (vw + texsize - 1) / texsize;
   947         txi->textures *= (vh + texsize - 1) / texsize;
   939         txi->textures *= (vh + texsize - 1) / texsize;
   948         txi->texture = SDL_malloc (txi->textures * sizeof (int));
   940         txi->texture = SDL_malloc(txi->textures * sizeof(int));
   949         SDL_memset (txi->texture, -1, txi->textures * sizeof (int));
   941         SDL_memset(txi->texture, -1, txi->textures * sizeof(int));
   950 #ifdef DEBUG_GLSDL
   942 #ifdef DEBUG_GLSDL
   951         fprintf (stderr, "two-way tiling; textures=%d\n", txi->textures);
   943         fprintf(stderr, "two-way tiling; textures=%d\n", txi->textures);
   952 #endif
   944 #endif
   953         if (!txi->texture) {
   945         if (!txi->texture) {
   954             fprintf (stderr, "glSDL: INTERNAL ERROR: Failed to allocate"
   946             fprintf(stderr, "glSDL: INTERNAL ERROR: Failed to allocate"
   955                      " texture name table!\n");
   947                     " texture name table!\n");
   956             return -3;
   948             return -3;
   957         }
   949         }
   958         return 0;
   950         return 0;
   959     }
   951     }
   960 
   952 
   981         /* Handle multiple textures for very wide/tall surfaces. */
   973         /* Handle multiple textures for very wide/tall surfaces. */
   982         minsize = maxtexsize;
   974         minsize = maxtexsize;
   983         rows = (vw + minsize - 1) / minsize;
   975         rows = (vw + minsize - 1) / minsize;
   984     }
   976     }
   985 #ifdef DEBUG_GLSDL_CHOP
   977 #ifdef DEBUG_GLSDL_CHOP
   986     fprintf (stderr, "==> minsize=%d ", minsize);
   978     fprintf(stderr, "==> minsize=%d ", minsize);
   987     fprintf (stderr, "(rows=%d) \t", rows);
   979     fprintf(stderr, "(rows=%d) \t", rows);
   988 #endif
   980 #endif
   989 
   981 
   990     /* Recalculate with nearest higher power-of-2 width. */
   982     /* Recalculate with nearest higher power-of-2 width. */
   991     for (texsize = 1; texsize < minsize; texsize <<= 1);
   983     for (texsize = 1; texsize < minsize; texsize <<= 1);
   992     txi->texsize = texsize;
   984     txi->texsize = texsize;
   993     rows = (vw + texsize - 1) / texsize;
   985     rows = (vw + texsize - 1) / texsize;
   994 #ifdef DEBUG_GLSDL_CHOP
   986 #ifdef DEBUG_GLSDL_CHOP
   995     fprintf (stderr, "==> texsize=%d (rows=%d) \t", texsize, rows);
   987     fprintf(stderr, "==> texsize=%d (rows=%d) \t", texsize, rows);
   996 #endif
   988 #endif
   997 
   989 
   998     /* Calculate number of tiles per texture */
   990     /* Calculate number of tiles per texture */
   999     txi->tilespertex = txi->texsize / vh;
   991     txi->tilespertex = txi->texsize / vh;
  1000 #ifdef DEBUG_GLSDL_CHOP
   992 #ifdef DEBUG_GLSDL_CHOP
  1001     fprintf (stderr, "tilespertex=%d \t", txi->tilespertex);
   993     fprintf(stderr, "tilespertex=%d \t", txi->tilespertex);
  1002 #endif
   994 #endif
  1003 
   995 
  1004     /* Calculate number of textures needed */
   996     /* Calculate number of textures needed */
  1005     txi->textures = (rows + txi->tilespertex - 1) / txi->tilespertex;
   997     txi->textures = (rows + txi->tilespertex - 1) / txi->tilespertex;
  1006     txi->texture = (GLuint *) SDL_malloc (txi->textures * sizeof (GLuint));
   998     txi->texture = (GLuint *) SDL_malloc(txi->textures * sizeof(GLuint));
  1007     SDL_memset (txi->texture, GLSDL_NOTEX, txi->textures * sizeof (GLuint));
   999     SDL_memset(txi->texture, GLSDL_NOTEX, txi->textures * sizeof(GLuint));
  1008 #ifdef DEBUG_GLSDL_CHOP
  1000 #ifdef DEBUG_GLSDL_CHOP
  1009     fprintf (stderr, "textures=%d, ", txi->textures);
  1001     fprintf(stderr, "textures=%d, ", txi->textures);
  1010 #endif
  1002 #endif
  1011     if (!txi->texture)
  1003     if (!txi->texture)
  1012         return GLERET ("Failed to allocate texture name table!");
  1004         return GLERET("Failed to allocate texture name table!");
  1013 
  1005 
  1014     /* Set up tile size. (Only one axis supported here!) */
  1006     /* Set up tile size. (Only one axis supported here!) */
  1015     if (1 == rows) {
  1007     if (1 == rows) {
  1016         txi->tilemode = GLSDL_TM_SINGLE;
  1008         txi->tilemode = GLSDL_TM_SINGLE;
  1017         if (vertical) {
  1009         if (vertical) {
  1030         txi->tilew = texsize;
  1022         txi->tilew = texsize;
  1031         txi->tileh = vh;
  1023         txi->tileh = vh;
  1032     }
  1024     }
  1033 
  1025 
  1034 #ifdef DEBUG_GLSDL_CHOP
  1026 #ifdef DEBUG_GLSDL_CHOP
  1035     fprintf (stderr, "tilew=%d, tileh=%d\n", txi->tilew, txi->tileh);
  1027     fprintf(stderr, "tilew=%d, tileh=%d\n", txi->tilew, txi->tileh);
  1036 #endif
  1028 #endif
  1037     return 0;
  1029     return 0;
  1038 }
  1030 }
  1039 
  1031 
  1040 
  1032 
  1041 /* Create a temporary TexInfo struct for an SDL_Surface */
  1033 /* Create a temporary TexInfo struct for an SDL_Surface */
  1042 static private_hwdata *
  1034 static private_hwdata *
  1043 glSDL_CreateTempTexInfo (_THIS, SDL_Surface * surface)
  1035 glSDL_CreateTempTexInfo(_THIS, SDL_Surface * surface)
  1044 {
  1036 {
  1045     private_hwdata *txi;
  1037     private_hwdata *txi;
  1046     if (!surface) {
  1038     if (!surface) {
  1047         GLERR ("CreateTempTexInfo(); no surface!");
  1039         GLERR("CreateTempTexInfo(); no surface!");
  1048         return NULL;
  1040         return NULL;
  1049     }
  1041     }
  1050     if (IS_GLSDL_SURFACE (surface))
  1042     if (IS_GLSDL_SURFACE(surface))
  1051         return glSDL_GetTexInfo (surface);      /* Do nothing */
  1043         return glSDL_GetTexInfo(surface);       /* Do nothing */
  1052 
  1044 
  1053     txi = glSDL_AllocTexInfo (surface);
  1045     txi = glSDL_AllocTexInfo(surface);
  1054     if (!txi) {
  1046     if (!txi) {
  1055         GLERR ("CreateTempTexInfo(); Could not alloc TexInfo!");
  1047         GLERR("CreateTempTexInfo(); Could not alloc TexInfo!");
  1056         return NULL;
  1048         return NULL;
  1057     }
  1049     }
  1058     txi->virt.w = txi->lw = surface->w;
  1050     txi->virt.w = txi->lw = surface->w;
  1059     txi->virt.h = txi->lh = surface->h;
  1051     txi->virt.h = txi->lh = surface->h;
  1060 
  1052 
  1061     if (glSDL_CalcChop (txi) < 0) {
  1053     if (glSDL_CalcChop(txi) < 0) {
  1062         glSDL_FreeTexInfo (this, txi);
  1054         glSDL_FreeTexInfo(this, txi);
  1063         GLERR ("CreateTempTexInfo(); CalcChop() failed!");
  1055         GLERR("CreateTempTexInfo(); CalcChop() failed!");
  1064         return NULL;
  1056         return NULL;
  1065     }
  1057     }
  1066 
  1058 
  1067     return txi;
  1059     return txi;
  1068 }
  1060 }
  1069 
  1061 
  1070 /* Add a glSDL_TexInfo struct to an SDL_Surface */
  1062 /* Add a glSDL_TexInfo struct to an SDL_Surface */
  1071 static private_hwdata *
  1063 static private_hwdata *
  1072 glSDL_AddTexInfo (_THIS, SDL_Surface * surface)
  1064 glSDL_AddTexInfo(_THIS, SDL_Surface * surface)
  1073 {
  1065 {
  1074     private_hwdata *txi = glSDL_CreateTempTexInfo (this, surface);
  1066     private_hwdata *txi = glSDL_CreateTempTexInfo(this, surface);
  1075     if (!txi)
  1067     if (!txi)
  1076         return NULL;
  1068         return NULL;
  1077 
  1069 
  1078     /* Connect the surface to the new TexInfo. */
  1070     /* Connect the surface to the new TexInfo. */
  1079     txi->temporary = 0;
  1071     txi->temporary = 0;
  1085     first = surface;
  1077     first = surface;
  1086     if (txi->next != NULL) {
  1078     if (txi->next != NULL) {
  1087         txi->next->hwdata->prev = surface;
  1079         txi->next->hwdata->prev = surface;
  1088     }
  1080     }
  1089 
  1081 
  1090     SDL_SetClipRect (surface, &txi->virt);
  1082     SDL_SetClipRect(surface, &txi->virt);
  1091     return txi;
  1083     return txi;
  1092 }
  1084 }
  1093 
  1085 
  1094 
  1086 
  1095 /* Create a surface of the prefered OpenGL RGB texture format */
  1087 /* Create a surface of the prefered OpenGL RGB texture format */
  1116 }
  1108 }
  1117 */
  1109 */
  1118 
  1110 
  1119 /* Create a surface of the prefered OpenGL RGBA texture format */
  1111 /* Create a surface of the prefered OpenGL RGBA texture format */
  1120 static SDL_Surface *
  1112 static SDL_Surface *
  1121 glSDL_CreateRGBASurface (int w, int h)
  1113 glSDL_CreateRGBASurface(int w, int h)
  1122 {
  1114 {
  1123     SDL_Surface *s;
  1115     SDL_Surface *s;
  1124     Uint32 rmask, gmask, bmask, amask;
  1116     Uint32 rmask, gmask, bmask, amask;
  1125     int bits = 32;
  1117     int bits = 32;
  1126 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  1118 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  1132     rmask = 0xFF000000;
  1124     rmask = 0xFF000000;
  1133     gmask = 0x00FF0000;
  1125     gmask = 0x00FF0000;
  1134     bmask = 0x0000FF00;
  1126     bmask = 0x0000FF00;
  1135     amask = 0x000000FF;
  1127     amask = 0x000000FF;
  1136 #endif
  1128 #endif
  1137     s = SDL_CreateRGBSurface (SDL_SWSURFACE, w, h,
  1129     s = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h,
  1138                               bits, rmask, gmask, bmask, amask);
  1130                              bits, rmask, gmask, bmask, amask);
  1139     if (s)
  1131     if (s)
  1140         s->flags |= SDL_HWACCEL;
  1132         s->flags |= SDL_HWACCEL;
  1141 
  1133 
  1142     return s;
  1134     return s;
  1143 }
  1135 }
  1144 
  1136 
  1145 
  1137 
  1146 static void
  1138 static void
  1147 glSDL_init_formats (_THIS)
  1139 glSDL_init_formats(_THIS)
  1148 {
  1140 {
  1149     RGBfmt = SDL_AllocFormat (24,
  1141     RGBfmt = SDL_AllocFormat(24,
  1150 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  1142 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  1151                               0x000000FF, 0x0000FF00, 0x00FF0000, 0);
  1143                              0x000000FF, 0x0000FF00, 0x00FF0000, 0);
  1152 #else
  1144 #else
  1153                               0x00FF0000, 0x0000FF00, 0x000000FF, 0);
  1145                              0x00FF0000, 0x0000FF00, 0x000000FF, 0);
  1154 #endif
  1146 #endif
  1155     RGBAfmt = SDL_AllocFormat (32,
  1147     RGBAfmt = SDL_AllocFormat(32,
  1156 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  1148 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  1157                                0x000000FF,
  1149                               0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000);
  1158                                0x0000FF00, 0x00FF0000, 0xFF000000);
       
  1159 #else
  1150 #else
  1160                                0xFF000000,
  1151                               0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
  1161                                0x00FF0000, 0x0000FF00, 0x000000FF);
       
  1162 #endif
  1152 #endif
  1163 }
  1153 }
  1164 
  1154 
  1165 
  1155 
  1166 static int
  1156 static int
  1167 glSDL_FormatIsOk (SDL_Surface * surface)
  1157 glSDL_FormatIsOk(SDL_Surface * surface)
  1168 {
  1158 {
  1169     SDL_PixelFormat *pf;
  1159     SDL_PixelFormat *pf;
  1170     if (!surface)
  1160     if (!surface)
  1171         return 1;               /* Well, there ain't much we can do anyway... */
  1161         return 1;               /* Well, there ain't much we can do anyway... */
  1172 
  1162 
  1204     }
  1194     }
  1205     return 1;
  1195     return 1;
  1206 }
  1196 }
  1207 
  1197 
  1208 static void
  1198 static void
  1209 glSDL_key2alpha (SDL_Surface * surface)
  1199 glSDL_key2alpha(SDL_Surface * surface)
  1210 {
  1200 {
  1211     int x, y;
  1201     int x, y;
  1212     Uint32 ckey = surface->format->colorkey;
  1202     Uint32 ckey = surface->format->colorkey;
  1213 
  1203 
  1214 #ifdef DEBUG_GLSDL
  1204 #ifdef DEBUG_GLSDL
  1215     fprintf (stderr, "glSDL_key2alpha()\n");
  1205     fprintf(stderr, "glSDL_key2alpha()\n");
  1216 #endif
  1206 #endif
  1217     for (y = 0; y < surface->h; ++y) {
  1207     for (y = 0; y < surface->h; ++y) {
  1218         Uint32 *px =
  1208         Uint32 *px =
  1219             (Uint32 *) ((char *) surface->pixels + y * surface->pitch);
  1209             (Uint32 *) ((char *) surface->pixels + y * surface->pitch);
  1220         for (x = 0; x < surface->w; ++x)
  1210         for (x = 0; x < surface->w; ++x)
  1228 /*----------------------------------------------------------
  1218 /*----------------------------------------------------------
  1229   SDL style API
  1219   SDL style API
  1230   ----------------------------------------------------------*/
  1220   ----------------------------------------------------------*/
  1231 
  1221 
  1232 static int
  1222 static int
  1233 glSDL_FlipHWSurface (_THIS, SDL_Surface * surface)
  1223 glSDL_FlipHWSurface(_THIS, SDL_Surface * surface)
  1234 {
  1224 {
  1235 #ifdef GLSDL_GRAPHICAL_DEBUG
  1225 #ifdef GLSDL_GRAPHICAL_DEBUG
  1236     this->glDisable (GL_TEXTURE_2D);
  1226     this->glDisable(GL_TEXTURE_2D);
  1237     this->glBegin (GL_LINE_LOOP);
  1227     this->glBegin(GL_LINE_LOOP);
  1238     this->glColor4ub (0, 0, 255, 128);
  1228     this->glColor4ub(0, 0, 255, 128);
  1239     this->glVertex2i (0, 0);
  1229     this->glVertex2i(0, 0);
  1240     this->glVertex2i (surface->w, 0);
  1230     this->glVertex2i(surface->w, 0);
  1241     this->glVertex2i (surface->w, surface->h);
  1231     this->glVertex2i(surface->w, surface->h);
  1242     this->glVertex2i (0, surface->h);
  1232     this->glVertex2i(0, surface->h);
  1243     this->glEnd ();
  1233     this->glEnd();
  1244     this->glEnable (GL_TEXTURE_2D);
  1234     this->glEnable(GL_TEXTURE_2D);
  1245 #endif
  1235 #endif
  1246     if (this->screen->flags & SDL_DOUBLEBUF)
  1236     if (this->screen->flags & SDL_DOUBLEBUF)
  1247         this->GL_SwapBuffers (this);
  1237         this->GL_SwapBuffers(this);
  1248     else
  1238     else
  1249         this->glFinish ();
  1239         this->glFinish();
  1250     return 0;
  1240     return 0;
  1251 }
  1241 }
  1252 
  1242 
  1253 
  1243 
  1254 static void
  1244 static void
  1255 glSDL_UpdateRects (_THIS, int numrects, SDL_Rect * rects)
  1245 glSDL_UpdateRects(_THIS, int numrects, SDL_Rect * rects)
  1256 {
  1246 {
  1257 #ifdef GLSDL_GRAPHICAL_DEBUG
  1247 #ifdef GLSDL_GRAPHICAL_DEBUG
  1258     int i;
  1248     int i;
  1259     this->glDisable (GL_TEXTURE_2D);
  1249     this->glDisable(GL_TEXTURE_2D);
  1260     for (i = 0; i < numrects; i++) {
  1250     for (i = 0; i < numrects; i++) {
  1261         this->glColor4ub (255, 0, 0, 128);
  1251         this->glColor4ub(255, 0, 0, 128);
  1262         this->glBegin (GL_LINE_LOOP);
  1252         this->glBegin(GL_LINE_LOOP);
  1263         this->glVertex2i (rects[i].x, rects[i].y);
  1253         this->glVertex2i(rects[i].x, rects[i].y);
  1264         this->glVertex2i (rects[i].x + rects[i].w, rects[i].y);
  1254         this->glVertex2i(rects[i].x + rects[i].w, rects[i].y);
  1265         this->glVertex2i (rects[i].x + rects[i].w, rects[i].y + rects[i].h);
  1255         this->glVertex2i(rects[i].x + rects[i].w, rects[i].y + rects[i].h);
  1266         this->glVertex2i (rects[i].x, rects[i].y + rects[i].h);
  1256         this->glVertex2i(rects[i].x, rects[i].y + rects[i].h);
  1267         this->glEnd ();
  1257         this->glEnd();
  1268     }
  1258     }
  1269     this->glEnable (GL_TEXTURE_2D);
  1259     this->glEnable(GL_TEXTURE_2D);
  1270 #endif
  1260 #endif
  1271     if (this->screen->flags & SDL_DOUBLEBUF)
  1261     if (this->screen->flags & SDL_DOUBLEBUF)
  1272         this->GL_SwapBuffers (this);
  1262         this->GL_SwapBuffers(this);
  1273     else
  1263     else
  1274         this->glFinish ();
  1264         this->glFinish();
  1275 }
  1265 }
  1276 
  1266 
  1277 
  1267 
  1278 static int
  1268 static int
  1279 glSDL_AllocHWSurface (_THIS, SDL_Surface * surface)
  1269 glSDL_AllocHWSurface(_THIS, SDL_Surface * surface)
  1280 {
  1270 {
  1281     surface->flags |= (SDL_HWSURFACE | SDL_HWACCEL);
  1271     surface->flags |= (SDL_HWSURFACE | SDL_HWACCEL);
  1282 
  1272 
  1283     surface->pixels = SDL_malloc (surface->h * surface->pitch);
  1273     surface->pixels = SDL_malloc(surface->h * surface->pitch);
  1284     if (surface->pixels == NULL) {
  1274     if (surface->pixels == NULL) {
  1285         SDL_FreeSurface (surface);
  1275         SDL_FreeSurface(surface);
  1286         SDL_OutOfMemory ();
  1276         SDL_OutOfMemory();
  1287         return (-1);
  1277         return (-1);
  1288     }
  1278     }
  1289     SDL_memset (surface->pixels, 0, surface->h * surface->pitch);
  1279     SDL_memset(surface->pixels, 0, surface->h * surface->pitch);
  1290     return 0;
  1280     return 0;
  1291 }
  1281 }
  1292 
  1282 
  1293 
  1283 
  1294 static void
  1284 static void
  1295 glSDL_FreeHWSurface (_THIS, SDL_Surface * surface)
  1285 glSDL_FreeHWSurface(_THIS, SDL_Surface * surface)
  1296 {
  1286 {
  1297     if (!surface)
  1287     if (!surface)
  1298         return;
  1288         return;
  1299     glSDL_RemoveTexInfo (this, surface);
  1289     glSDL_RemoveTexInfo(this, surface);
  1300 }
  1290 }
  1301 
  1291 
  1302 
  1292 
  1303 static int
  1293 static int
  1304 glSDL_LockHWSurface (_THIS, SDL_Surface * surface)
  1294 glSDL_LockHWSurface(_THIS, SDL_Surface * surface)
  1305 {
  1295 {
  1306     int y;
  1296     int y;
  1307 
  1297 
  1308     if (!surface)
  1298     if (!surface)
  1309         return -1;
  1299         return -1;
  1310 
  1300 
  1311 #ifdef DEBUG_GLSDL
  1301 #ifdef DEBUG_GLSDL
  1312     fprintf (stderr, "glSDL: Lock Surface.\n");
  1302     fprintf(stderr, "glSDL: Lock Surface.\n");
  1313 #endif
  1303 #endif
  1314 
  1304 
  1315     if (SDL_VideoSurface == surface) {
  1305     if (SDL_VideoSurface == surface) {
  1316         glSDL_Invalidate (surface, NULL);
  1306         glSDL_Invalidate(surface, NULL);
  1317         this->glPixelStorei (GL_UNPACK_ROW_LENGTH,
  1307         this->glPixelStorei(GL_UNPACK_ROW_LENGTH,
  1318                              surface->pitch / surface->format->BytesPerPixel);
  1308                             surface->pitch / surface->format->BytesPerPixel);
  1319         this->glReadPixels (0, 0, OpenGL_Surface->w, OpenGL_Surface->h,
  1309         this->glReadPixels(0, 0, OpenGL_Surface->w, OpenGL_Surface->h,
  1320                             GL_RGB, GL_UNSIGNED_BYTE, OpenGL_Surface->pixels);
  1310                            GL_RGB, GL_UNSIGNED_BYTE, OpenGL_Surface->pixels);
  1321         for (y = 0; y < OpenGL_Surface->h / 2; ++y) {
  1311         for (y = 0; y < OpenGL_Surface->h / 2; ++y) {
  1322             void *upper = (Uint8 *) OpenGL_Surface->pixels +
  1312             void *upper = (Uint8 *) OpenGL_Surface->pixels +
  1323                 OpenGL_Surface->pitch * y;
  1313                 OpenGL_Surface->pitch * y;
  1324             void *lower = (Uint8 *) OpenGL_Surface->pixels +
  1314             void *lower = (Uint8 *) OpenGL_Surface->pixels +
  1325                 OpenGL_Surface->pitch * (OpenGL_Surface->h - y - 1);
  1315                 OpenGL_Surface->pitch * (OpenGL_Surface->h - y - 1);
  1326             SDL_memcpy (mirrorbuf, upper, OpenGL_Surface->pitch);
  1316             SDL_memcpy(mirrorbuf, upper, OpenGL_Surface->pitch);
  1327             SDL_memcpy (upper, lower, OpenGL_Surface->pitch);
  1317             SDL_memcpy(upper, lower, OpenGL_Surface->pitch);
  1328             SDL_memcpy (lower, mirrorbuf, OpenGL_Surface->pitch);
  1318             SDL_memcpy(lower, mirrorbuf, OpenGL_Surface->pitch);
  1329         }
  1319         }
  1330         /* the mapping has to be invalidated on 8bpp video surfaces in case of a hw palette change. 
  1320         /* the mapping has to be invalidated on 8bpp video surfaces in case of a hw palette change. 
  1331          * Now if someone could tell me why this is not handled by SDL... */
  1321          * Now if someone could tell me why this is not handled by SDL... */
  1332         if (SDL_VideoSurface->format->BitsPerPixel == 8)
  1322         if (SDL_VideoSurface->format->BitsPerPixel == 8)
  1333             SDL_InvalidateMap (OpenGL_Surface->map);
  1323             SDL_InvalidateMap(OpenGL_Surface->map);
  1334 
  1324 
  1335         /* convert this raw surface to the application-requested format 
  1325         /* convert this raw surface to the application-requested format 
  1336          * FIXME this is sometimes overkill, we could use glPixelStore smartly
  1326          * FIXME this is sometimes overkill, we could use glPixelStore smartly
  1337          * But this would be slow anyway :) */
  1327          * But this would be slow anyway :) */
  1338 
  1328 
  1339         glSDL_SoftBlit (OpenGL_Surface, NULL, SDL_VideoSurface, NULL);
  1329         glSDL_SoftBlit(OpenGL_Surface, NULL, SDL_VideoSurface, NULL);
  1340     } else
  1330     } else
  1341         glSDL_Invalidate (surface, NULL);
  1331         glSDL_Invalidate(surface, NULL);
  1342 
  1332 
  1343     return 0;
  1333     return 0;
  1344 }
  1334 }
  1345 
  1335 
  1346 
  1336 
  1347 static void
  1337 static void
  1348 glSDL_UnlockHWSurface (_THIS, SDL_Surface * surface)
  1338 glSDL_UnlockHWSurface(_THIS, SDL_Surface * surface)
  1349 {
  1339 {
  1350     private_hwdata *txi;
  1340     private_hwdata *txi;
  1351 
  1341 
  1352     if (!surface)
  1342     if (!surface)
  1353         return;
  1343         return;
  1354 
  1344 
  1355     /* upload this surface ONLY if this is a glSDL surface
  1345     /* upload this surface ONLY if this is a glSDL surface
  1356      * because sometimes (during displayformating for ex.) surfaces are unlocked that aren't glSDL
  1346      * because sometimes (during displayformating for ex.) surfaces are unlocked that aren't glSDL
  1357      */
  1347      */
  1358     if (!IS_GLSDL_SURFACE (surface))
  1348     if (!IS_GLSDL_SURFACE(surface))
  1359         return;
  1349         return;
  1360 
  1350 
  1361 #ifdef DEBUG_GLSDL
  1351 #ifdef DEBUG_GLSDL
  1362     fprintf (stderr, "glSDL: Unlock Surface.\n");
  1352     fprintf(stderr, "glSDL: Unlock Surface.\n");
  1363 #endif
  1353 #endif
  1364 
  1354 
  1365     txi = glSDL_UploadSurface (this, surface);
  1355     txi = glSDL_UploadSurface(this, surface);
  1366 
  1356 
  1367     if (!txi) {
  1357     if (!txi) {
  1368         GLERR ("glSDL_UnlockHWSurface() failed to upload surface!");
  1358         GLERR("glSDL_UnlockHWSurface() failed to upload surface!");
  1369         return;
  1359         return;
  1370     }
  1360     }
  1371     if (txi->temporary) {
  1361     if (txi->temporary) {
  1372         GLERR
  1362         GLERR
  1373             ("Weirdness... glSDL_UnlockHWSurface() got a temporary TexInfo.");
  1363             ("Weirdness... glSDL_UnlockHWSurface() got a temporary TexInfo.");
  1374         return;
  1364         return;
  1375     }
  1365     }
  1376     if (surface == SDL_VideoSurface)
  1366     if (surface == SDL_VideoSurface)
  1377         glSDL_BlitGL (this, SDL_VideoSurface, NULL, NULL);
  1367         glSDL_BlitGL(this, SDL_VideoSurface, NULL, NULL);
  1378 }
  1368 }
  1379 
  1369 
  1380 
  1370 
  1381 static int
  1371 static int
  1382 glSDL_SetHWColorKey (_THIS, SDL_Surface * surface, Uint32 key)
  1372 glSDL_SetHWColorKey(_THIS, SDL_Surface * surface, Uint32 key)
  1383 {
  1373 {
  1384     /*
  1374     /*
  1385      * If an application does this *after* SDL_DisplayFormat,
  1375      * If an application does this *after* SDL_DisplayFormat,
  1386      * we're basically screwed, unless we want to do an
  1376      * we're basically screwed, unless we want to do an
  1387      * in-place surface conversion hack here.
  1377      * in-place surface conversion hack here.
  1388      *
  1378      *
  1389      * What we do is just kill the glSDL texinfo... No big
  1379      * What we do is just kill the glSDL texinfo... No big
  1390      * deal in most cases, as glSDL only converts once anyway,
  1380      * deal in most cases, as glSDL only converts once anyway,
  1391      * *unless* you keep modifying the surface.
  1381      * *unless* you keep modifying the surface.
  1392      */
  1382      */
  1393     if (IS_GLSDL_SURFACE (surface))
  1383     if (IS_GLSDL_SURFACE(surface))
  1394         glSDL_RemoveTexInfo (this, surface);
  1384         glSDL_RemoveTexInfo(this, surface);
  1395     return 0;
  1385     return 0;
  1396 }
  1386 }
  1397 
  1387 
  1398 
  1388 
  1399 static int
  1389 static int
  1400 glSDL_SetHWAlpha (_THIS, SDL_Surface * surface, Uint8 alpha)
  1390 glSDL_SetHWAlpha(_THIS, SDL_Surface * surface, Uint8 alpha)
  1401 {
  1391 {
  1402     /*
  1392     /*
  1403      * If an application does this *after* SDL_DisplayFormat,
  1393      * If an application does this *after* SDL_DisplayFormat,
  1404      * we're basically screwed, unless we want to do an
  1394      * we're basically screwed, unless we want to do an
  1405      * in-place surface conversion hack here.
  1395      * in-place surface conversion hack here.
  1406      *
  1396      *
  1407      * What we do is just kill the glSDL texinfo... No big
  1397      * What we do is just kill the glSDL texinfo... No big
  1408      * deal in most cases, as glSDL only converts once anyway,
  1398      * deal in most cases, as glSDL only converts once anyway,
  1409      * *unless* you keep modifying the surface.
  1399      * *unless* you keep modifying the surface.
  1410      */
  1400      */
  1411     if (IS_GLSDL_SURFACE (surface))
  1401     if (IS_GLSDL_SURFACE(surface))
  1412         glSDL_RemoveTexInfo (this, surface);
  1402         glSDL_RemoveTexInfo(this, surface);
  1413     return 0;
  1403     return 0;
  1414 }
  1404 }
  1415 
  1405 
  1416 static SDL_bool
  1406 static SDL_bool
  1417 glSDL_SetClipRect (_THIS, SDL_Surface * surface, SDL_Rect * rect)
  1407 glSDL_SetClipRect(_THIS, SDL_Surface * surface, SDL_Rect * rect)
  1418 {
  1408 {
  1419     SDL_bool res;
  1409     SDL_bool res;
  1420     if (!surface)
  1410     if (!surface)
  1421         return SDL_FALSE;
  1411         return SDL_FALSE;
  1422 
  1412 
  1423     res = SDL_SetClipRect (surface, rect);
  1413     res = SDL_SetClipRect(surface, rect);
  1424     if (!res)
  1414     if (!res)
  1425         return SDL_FALSE;
  1415         return SDL_FALSE;
  1426 
  1416 
  1427     rect = &surface->clip_rect;
  1417     rect = &surface->clip_rect;
  1428 
  1418 
  1433 
  1423 
  1434         r.x = rect->x;
  1424         r.x = rect->x;
  1435         r.y = rect->y;
  1425         r.y = rect->y;
  1436         r.w = rect->w;
  1426         r.w = rect->w;
  1437         r.h = rect->h;
  1427         r.h = rect->h;
  1438         SDL_SetClipRect (surface, rect);
  1428         SDL_SetClipRect(surface, rect);
  1439 
  1429 
  1440         txi = glSDL_GetTexInfo (surface);
  1430         txi = glSDL_GetTexInfo(surface);
  1441         if (!txi)
  1431         if (!txi)
  1442             return GLERET ("SetClipRect(): Could not get TexInfo!");
  1432             return GLERET("SetClipRect(): Could not get TexInfo!");
  1443 
  1433 
  1444         this->glViewport (rect->x,
  1434         this->glViewport(rect->x,
  1445                           surface->h - (rect->y + rect->h), rect->w, rect->h);
  1435                          surface->h - (rect->y + rect->h), rect->w, rect->h);
  1446         /*
  1436         /*
  1447          * Note that this projection is upside down in
  1437          * Note that this projection is upside down in
  1448          * relation to the OpenGL coordinate system.
  1438          * relation to the OpenGL coordinate system.
  1449          */
  1439          */
  1450         this->glMatrixMode (GL_PROJECTION);
  1440         this->glMatrixMode(GL_PROJECTION);
  1451         this->glLoadIdentity ();
  1441         this->glLoadIdentity();
  1452         xscale = (float) txi->lw / (float) surface->w;
  1442         xscale = (float) txi->lw / (float) surface->w;
  1453         yscale = (float) txi->lh / (float) surface->h;
  1443         yscale = (float) txi->lh / (float) surface->h;
  1454         this->glOrtho (xscale * (float) rect->x,
  1444         this->glOrtho(xscale * (float) rect->x,
  1455                        xscale * (float) (rect->w + rect->x),
  1445                       xscale * (float) (rect->w + rect->x),
  1456                        yscale * (float) (rect->h + rect->y),
  1446                       yscale * (float) (rect->h + rect->y),
  1457                        yscale * (float) rect->y, -1.0, 1.0);
  1447                       yscale * (float) rect->y, -1.0, 1.0);
  1458         return SDL_TRUE;
  1448         return SDL_TRUE;
  1459     }
  1449     }
  1460     return res;
  1450     return res;
  1461 }
  1451 }
  1462 
  1452 
  1463 static int
  1453 static int
  1464 glSDL_BlitFromGL (_THIS, SDL_Rect * srcrect,
  1454 glSDL_BlitFromGL(_THIS, SDL_Rect * srcrect,
  1465                   SDL_Surface * dst, SDL_Rect * dstrect)
  1455                  SDL_Surface * dst, SDL_Rect * dstrect)
  1466 {
  1456 {
  1467     SDL_Rect sr, dr;
  1457     SDL_Rect sr, dr;
  1468 
  1458 
  1469     /* In case the destination has an OpenGL texture... */
  1459     /* In case the destination has an OpenGL texture... */
  1470     glSDL_Invalidate (dst, dstrect);
  1460     glSDL_Invalidate(dst, dstrect);
  1471 
  1461 
  1472     /* Abuse the fake screen buffer a little. */
  1462     /* Abuse the fake screen buffer a little. */
  1473     this->glPixelStorei (GL_UNPACK_ROW_LENGTH, SDL_VideoSurface->pitch /
  1463     this->glPixelStorei(GL_UNPACK_ROW_LENGTH, SDL_VideoSurface->pitch /
  1474                          SDL_VideoSurface->format->BytesPerPixel);
  1464                         SDL_VideoSurface->format->BytesPerPixel);
  1475     if (srcrect)
  1465     if (srcrect)
  1476         this->glReadPixels (srcrect->x,
  1466         this->glReadPixels(srcrect->x,
  1477                             OpenGL_Surface->h - (srcrect->y + srcrect->h - 1),
  1467                            OpenGL_Surface->h - (srcrect->y + srcrect->h - 1),
  1478                             srcrect->w, srcrect->h, GL_RGB, GL_UNSIGNED_BYTE,
  1468                            srcrect->w, srcrect->h, GL_RGB, GL_UNSIGNED_BYTE,
  1479                             OpenGL_Surface->pixels);
  1469                            OpenGL_Surface->pixels);
  1480     else
  1470     else
  1481         this->glReadPixels (0, 0, OpenGL_Surface->w, OpenGL_Surface->h,
  1471         this->glReadPixels(0, 0, OpenGL_Surface->w, OpenGL_Surface->h,
  1482                             GL_RGB, GL_UNSIGNED_BYTE, OpenGL_Surface->pixels);
  1472                            GL_RGB, GL_UNSIGNED_BYTE, OpenGL_Surface->pixels);
  1483     sr = *srcrect;
  1473     sr = *srcrect;
  1484     dr = *dstrect;
  1474     dr = *dstrect;
  1485     glSDL_SoftBlit (OpenGL_Surface, &sr, dst, &dr);
  1475     glSDL_SoftBlit(OpenGL_Surface, &sr, dst, &dr);
  1486     return 0;
  1476     return 0;
  1487 }
  1477 }
  1488 
  1478 
  1489 static __inline__ void
  1479 static __inline__ void
  1490 glSDL_BlitGL_single (_THIS, private_hwdata * txi,
  1480 glSDL_BlitGL_single(_THIS, private_hwdata * txi,
  1491                      float sx1, float sy1, SDL_Rect * dst,
  1481                     float sx1, float sy1, SDL_Rect * dst, unsigned char alpha)
  1492                      unsigned char alpha)
       
  1493 {
  1482 {
  1494     float sx2, sy2, texscale;
  1483     float sx2, sy2, texscale;
  1495     if (!txi->textures)
  1484     if (!txi->textures)
  1496         return;
  1485         return;
  1497     if (-1 == txi->texture[0])
  1486     if (-1 == txi->texture[0])
  1498         return;
  1487         return;
  1499     glSDL_texture (this, txi->texture[0]);
  1488     glSDL_texture(this, txi->texture[0]);
  1500 
  1489 
  1501     texscale = 1.0 / (float) txi->texsize;
  1490     texscale = 1.0 / (float) txi->texsize;
  1502     sx2 = (sx1 + (float) dst->w) * texscale;
  1491     sx2 = (sx1 + (float) dst->w) * texscale;
  1503     sy2 = (sy1 + (float) dst->h) * texscale;
  1492     sy2 = (sy1 + (float) dst->h) * texscale;
  1504     sx1 *= texscale;
  1493     sx1 *= texscale;
  1505     sy1 *= texscale;
  1494     sy1 *= texscale;
  1506 
  1495 
  1507 #ifdef GLSDL_GRAPHICAL_DEBUG
  1496 #ifdef GLSDL_GRAPHICAL_DEBUG
  1508     this->glDisable (GL_TEXTURE_2D);
  1497     this->glDisable(GL_TEXTURE_2D);
  1509     this->glBegin (GL_LINE_LOOP);
  1498     this->glBegin(GL_LINE_LOOP);
  1510     this->glColor4ub (0, 255, 0, 128);
  1499     this->glColor4ub(0, 255, 0, 128);
  1511     this->glVertex2i (dst->x, dst->y);
  1500     this->glVertex2i(dst->x, dst->y);
  1512     this->glVertex2i (dst->x + dst->w, dst->y);
  1501     this->glVertex2i(dst->x + dst->w, dst->y);
  1513     this->glVertex2i (dst->x + dst->w, dst->y + dst->h);
  1502     this->glVertex2i(dst->x + dst->w, dst->y + dst->h);
  1514     this->glVertex2i (dst->x, dst->y + dst->h);
  1503     this->glVertex2i(dst->x, dst->y + dst->h);
  1515     this->glEnd ();
  1504     this->glEnd();
  1516     this->glEnable (GL_TEXTURE_2D);
  1505     this->glEnable(GL_TEXTURE_2D);
  1517 #endif
  1506 #endif
  1518 
  1507 
  1519     this->glBegin (GL_TRIANGLE_FAN);
  1508     this->glBegin(GL_TRIANGLE_FAN);
  1520     this->glColor4ub (255, 255, 255, alpha);
  1509     this->glColor4ub(255, 255, 255, alpha);
  1521     this->glTexCoord2f (sx1, sy1);
  1510     this->glTexCoord2f(sx1, sy1);
  1522     this->glVertex2i (dst->x, dst->y);
  1511     this->glVertex2i(dst->x, dst->y);
  1523     this->glTexCoord2f (sx2, sy1);
  1512     this->glTexCoord2f(sx2, sy1);
  1524     this->glVertex2i (dst->x + dst->w, dst->y);
  1513     this->glVertex2i(dst->x + dst->w, dst->y);
  1525     this->glTexCoord2f (sx2, sy2);
  1514     this->glTexCoord2f(sx2, sy2);
  1526     this->glVertex2i (dst->x + dst->w, dst->y + dst->h);
  1515     this->glVertex2i(dst->x + dst->w, dst->y + dst->h);
  1527     this->glTexCoord2f (sx1, sy2);
  1516     this->glTexCoord2f(sx1, sy2);
  1528     this->glVertex2i (dst->x, dst->y + dst->h);
  1517     this->glVertex2i(dst->x, dst->y + dst->h);
  1529     this->glEnd ();
  1518     this->glEnd();
  1530 }
  1519 }
  1531 
  1520 
  1532 
  1521 
  1533 static void
  1522 static void
  1534 glSDL_BlitGL_htile (_THIS, private_hwdata * txi,
  1523 glSDL_BlitGL_htile(_THIS, private_hwdata * txi,
  1535                     float sx1, float sy1, SDL_Rect * dst, unsigned char alpha)
  1524                    float sx1, float sy1, SDL_Rect * dst, unsigned char alpha)
  1536 {
  1525 {
  1537     int tex;
  1526     int tex;
  1538     float tile, sx2, sy2, yo;
  1527     float tile, sx2, sy2, yo;
  1539     float texscale = 1.0 / (float) txi->texsize;
  1528     float texscale = 1.0 / (float) txi->texsize;
  1540     float tileh = (float) txi->tileh * texscale;
  1529     float tileh = (float) txi->tileh * texscale;
  1541     sx2 = (sx1 + (float) dst->w) * texscale;
  1530     sx2 = (sx1 + (float) dst->w) * texscale;
  1542     sy2 = (sy1 + (float) dst->h) * texscale;
  1531     sy2 = (sy1 + (float) dst->h) * texscale;
  1543     sx1 *= texscale;
  1532     sx1 *= texscale;
  1544     sy1 *= texscale;
  1533     sy1 *= texscale;
  1545     tile = floor (sx1);
  1534     tile = floor(sx1);
  1546     tex = (int) tile / txi->tilespertex;
  1535     tex = (int) tile / txi->tilespertex;
  1547     yo = ((int) tile % txi->tilespertex) * tileh;
  1536     yo = ((int) tile % txi->tilespertex) * tileh;
  1548 
  1537 
  1549     if (tex >= txi->textures)
  1538     if (tex >= txi->textures)
  1550         return;
  1539         return;
  1551     if (-1 == txi->texture[tex])
  1540     if (-1 == txi->texture[tex])
  1552         return;
  1541         return;
  1553     glSDL_texture (this, txi->texture[tex]);
  1542     glSDL_texture(this, txi->texture[tex]);
  1554 
  1543 
  1555     while (tile < sx2) {
  1544     while (tile < sx2) {
  1556         int tdx1 = dst->x;
  1545         int tdx1 = dst->x;
  1557         int tdx2 = dst->x + dst->w;
  1546         int tdx2 = dst->x + dst->w;
  1558         float tsx1 = sx1 - tile;
  1547         float tsx1 = sx1 - tile;
  1573             ++tex;
  1562             ++tex;
  1574             if (tex >= txi->textures)
  1563             if (tex >= txi->textures)
  1575                 return;
  1564                 return;
  1576             if (-1 == txi->texture[tex])
  1565             if (-1 == txi->texture[tex])
  1577                 return;
  1566                 return;
  1578             glSDL_texture (this, txi->texture[tex]);
  1567             glSDL_texture(this, txi->texture[tex]);
  1579             yo = 0.0;
  1568             yo = 0.0;
  1580         }
  1569         }
  1581 #ifdef GLSDL_GRAPHICAL_DEBUG
  1570 #ifdef GLSDL_GRAPHICAL_DEBUG
  1582         this->glDisable (GL_TEXTURE_2D);
  1571         this->glDisable(GL_TEXTURE_2D);
  1583         this->glBegin (GL_LINE_LOOP);
  1572         this->glBegin(GL_LINE_LOOP);
  1584         this->glColor4ub (0, 255, 0, 128);
  1573         this->glColor4ub(0, 255, 0, 128);
  1585         this->glVertex2i (tdx1, dst->y);
  1574         this->glVertex2i(tdx1, dst->y);
  1586         this->glVertex2i (tdx2, dst->y);
  1575         this->glVertex2i(tdx2, dst->y);
  1587         this->glVertex2i (tdx2, dst->y + dst->h);
  1576         this->glVertex2i(tdx2, dst->y + dst->h);
  1588         this->glVertex2i (tdx1, dst->y + dst->h);
  1577         this->glVertex2i(tdx1, dst->y + dst->h);
  1589         this->glEnd ();
  1578         this->glEnd();
  1590         this->glEnable (GL_TEXTURE_2D);
  1579         this->glEnable(GL_TEXTURE_2D);
  1591 #endif
  1580 #endif
  1592 
  1581 
  1593         this->glBegin (GL_TRIANGLE_FAN);
  1582         this->glBegin(GL_TRIANGLE_FAN);
  1594         this->glColor4ub (255, 255, 255, alpha);
  1583         this->glColor4ub(255, 255, 255, alpha);
  1595         this->glTexCoord2f (tsx1, yo + sy1);
  1584         this->glTexCoord2f(tsx1, yo + sy1);
  1596         this->glVertex2i (tdx1, dst->y);
  1585         this->glVertex2i(tdx1, dst->y);
  1597         this->glTexCoord2f (tsx2, yo + sy1);
  1586         this->glTexCoord2f(tsx2, yo + sy1);
  1598         this->glVertex2i (tdx2, dst->y);
  1587         this->glVertex2i(tdx2, dst->y);
  1599         this->glTexCoord2f (tsx2, yo + sy2);
  1588         this->glTexCoord2f(tsx2, yo + sy2);
  1600         this->glVertex2i (tdx2, dst->y + dst->h);
  1589         this->glVertex2i(tdx2, dst->y + dst->h);
  1601         this->glTexCoord2f (tsx1, yo + sy2);
  1590         this->glTexCoord2f(tsx1, yo + sy2);
  1602         this->glVertex2i (tdx1, dst->y + dst->h);
  1591         this->glVertex2i(tdx1, dst->y + dst->h);
  1603         this->glEnd ();
  1592         this->glEnd();
  1604         tile += 1.0;
  1593         tile += 1.0;
  1605         yo += tileh;
  1594         yo += tileh;
  1606     }
  1595     }
  1607 }
  1596 }
  1608 
  1597 
  1609 
  1598 
  1610 static void
  1599 static void
  1611 glSDL_BlitGL_vtile (_THIS, private_hwdata * txi,
  1600 glSDL_BlitGL_vtile(_THIS, private_hwdata * txi,
  1612                     float sx1, float sy1, SDL_Rect * dst, unsigned char alpha)
  1601                    float sx1, float sy1, SDL_Rect * dst, unsigned char alpha)
  1613 {
  1602 {
  1614     int tex;
  1603     int tex;
  1615     float tile, sx2, sy2, xo;
  1604     float tile, sx2, sy2, xo;
  1616     float texscale = 1.0 / (float) txi->texsize;
  1605     float texscale = 1.0 / (float) txi->texsize;
  1617     float tilew = (float) txi->tilew * texscale;
  1606     float tilew = (float) txi->tilew * texscale;
  1618     sx2 = (sx1 + (float) dst->w) * texscale;
  1607     sx2 = (sx1 + (float) dst->w) * texscale;
  1619     sy2 = (sy1 + (float) dst->h) * texscale;
  1608     sy2 = (sy1 + (float) dst->h) * texscale;
  1620     sx1 *= texscale;
  1609     sx1 *= texscale;
  1621     sy1 *= texscale;
  1610     sy1 *= texscale;
  1622     tile = floor (sy1);
  1611     tile = floor(sy1);
  1623     tex = (int) tile / txi->tilespertex;
  1612     tex = (int) tile / txi->tilespertex;
  1624     xo = ((int) tile % txi->tilespertex) * tilew;
  1613     xo = ((int) tile % txi->tilespertex) * tilew;
  1625 
  1614 
  1626     if (tex >= txi->textures)
  1615     if (tex >= txi->textures)
  1627         return;
  1616         return;
  1628     if (-1 == txi->texture[tex])
  1617     if (-1 == txi->texture[tex])
  1629         return;
  1618         return;
  1630     glSDL_texture (this, txi->texture[tex]);
  1619     glSDL_texture(this, txi->texture[tex]);
  1631 
  1620 
  1632     while (tile < sy2) {
  1621     while (tile < sy2) {
  1633         int tdy1 = dst->y;
  1622         int tdy1 = dst->y;
  1634         int tdy2 = dst->y + dst->h;
  1623         int tdy2 = dst->y + dst->h;
  1635         float tsy1 = sy1 - tile;
  1624         float tsy1 = sy1 - tile;
  1650             ++tex;
  1639             ++tex;
  1651             if (tex >= txi->textures)
  1640             if (tex >= txi->textures)
  1652                 return;
  1641                 return;
  1653             if (-1 == txi->texture[tex])
  1642             if (-1 == txi->texture[tex])
  1654                 return;
  1643                 return;
  1655             glSDL_texture (this, txi->texture[tex]);
  1644             glSDL_texture(this, txi->texture[tex]);
  1656             xo = 0.0;
  1645             xo = 0.0;
  1657         }
  1646         }
  1658 #ifdef GLSDL_GRAPHICAL_DEBUG
  1647 #ifdef GLSDL_GRAPHICAL_DEBUG
  1659         this->glDisable (GL_TEXTURE_2D);
  1648         this->glDisable(GL_TEXTURE_2D);
  1660         this->glBegin (GL_LINE_LOOP);
  1649         this->glBegin(GL_LINE_LOOP);
  1661         this->glColor4ub (0, 255, 0, 128);
  1650         this->glColor4ub(0, 255, 0, 128);
  1662         this->glVertex2i (dst->x, tdy1);
  1651         this->glVertex2i(dst->x, tdy1);
  1663         this->glVertex2i (dst->x + dst->w, tdy1);
  1652         this->glVertex2i(dst->x + dst->w, tdy1);
  1664         this->glVertex2i (dst->x + dst->w, tdy2);
  1653         this->glVertex2i(dst->x + dst->w, tdy2);
  1665         this->glVertex2i (dst->x, tdy2);
  1654         this->glVertex2i(dst->x, tdy2);
  1666         this->glEnd ();
  1655         this->glEnd();
  1667         this->glEnable (GL_TEXTURE_2D);
  1656         this->glEnable(GL_TEXTURE_2D);
  1668 #endif
  1657 #endif
  1669 
  1658 
  1670         this->glBegin (GL_TRIANGLE_FAN);
  1659         this->glBegin(GL_TRIANGLE_FAN);
  1671         this->glColor4ub (255, 255, 255, alpha);
  1660         this->glColor4ub(255, 255, 255, alpha);
  1672         this->glTexCoord2f (xo + sx1, tsy1);
  1661         this->glTexCoord2f(xo + sx1, tsy1);
  1673         this->glVertex2i (dst->x, tdy1);
  1662         this->glVertex2i(dst->x, tdy1);
  1674         this->glTexCoord2f (xo + sx2, tsy1);
  1663         this->glTexCoord2f(xo + sx2, tsy1);
  1675         this->glVertex2i (dst->x + dst->w, tdy1);
  1664         this->glVertex2i(dst->x + dst->w, tdy1);
  1676         this->glTexCoord2f (xo + sx2, tsy2);
  1665         this->glTexCoord2f(xo + sx2, tsy2);
  1677         this->glVertex2i (dst->x + dst->w, tdy2);
  1666         this->glVertex2i(dst->x + dst->w, tdy2);
  1678         this->glTexCoord2f (xo + sx1, tsy2);
  1667         this->glTexCoord2f(xo + sx1, tsy2);
  1679         this->glVertex2i (dst->x, tdy2);
  1668         this->glVertex2i(dst->x, tdy2);
  1680         this->glEnd ();
  1669         this->glEnd();
  1681 
  1670 
  1682         tile += 1.0;
  1671         tile += 1.0;
  1683         xo += tilew;
  1672         xo += tilew;
  1684     }
  1673     }
  1685 }
  1674 }
  1686 
  1675 
  1687 
  1676 
  1688 static void
  1677 static void
  1689 glSDL_BlitGL_hvtile (_THIS, SDL_Surface * src, private_hwdata * txi,
  1678 glSDL_BlitGL_hvtile(_THIS, SDL_Surface * src, private_hwdata * txi,
  1690                      float sx1, float sy1, SDL_Rect * dst,
  1679                     float sx1, float sy1, SDL_Rect * dst, unsigned char alpha)
  1691                      unsigned char alpha)
       
  1692 {
  1680 {
  1693     int x, y, last_tex, tex;
  1681     int x, y, last_tex, tex;
  1694     float sx2, sy2;
  1682     float sx2, sy2;
  1695     float texscale = 1.0 / (float) txi->texsize;
  1683     float texscale = 1.0 / (float) txi->texsize;
  1696     int tilesperrow = (src->w + txi->tilew - 1) / txi->tilew;
  1684     int tilesperrow = (src->w + txi->tilew - 1) / txi->tilew;
  1697     sx2 = (sx1 + (float) dst->w) * texscale;
  1685     sx2 = (sx1 + (float) dst->w) * texscale;
  1698     sy2 = (sy1 + (float) dst->h) * texscale;
  1686     sy2 = (sy1 + (float) dst->h) * texscale;
  1699     sx1 *= texscale;
  1687     sx1 *= texscale;
  1700     sy1 *= texscale;
  1688     sy1 *= texscale;
  1701 
  1689 
  1702     last_tex = tex = floor (sy1) * tilesperrow + floor (sx1);
  1690     last_tex = tex = floor(sy1) * tilesperrow + floor(sx1);
  1703     if (tex >= txi->textures)
  1691     if (tex >= txi->textures)
  1704         return;
  1692         return;
  1705     if (-1 == txi->texture[tex])
  1693     if (-1 == txi->texture[tex])
  1706         return;
  1694         return;
  1707     glSDL_texture (this, txi->texture[tex]);
  1695     glSDL_texture(this, txi->texture[tex]);
  1708 
  1696 
  1709     for (y = floor (sy1); y < sy2; ++y) {
  1697     for (y = floor(sy1); y < sy2; ++y) {
  1710         int tdy1 = dst->y;
  1698         int tdy1 = dst->y;
  1711         int tdy2 = dst->y + dst->h;
  1699         int tdy2 = dst->y + dst->h;
  1712         float tsy1 = sy1 - y;
  1700         float tsy1 = sy1 - y;
  1713         float tsy2 = sy2 - y;
  1701         float tsy2 = sy2 - y;
  1714 
  1702 
  1719         }
  1707         }
  1720         if (tsy2 > 1.0) {
  1708         if (tsy2 > 1.0) {
  1721             tdy2 -= (tsy2 - 1.0) * txi->texsize;
  1709             tdy2 -= (tsy2 - 1.0) * txi->texsize;
  1722             tsy2 = 1.0;
  1710             tsy2 = 1.0;
  1723         }
  1711         }
  1724         for (x = floor (sx1); x < sx2; ++x) {
  1712         for (x = floor(sx1); x < sx2; ++x) {
  1725             int tdx1 = dst->x;
  1713             int tdx1 = dst->x;
  1726             int tdx2 = dst->x + dst->w;
  1714             int tdx2 = dst->x + dst->w;
  1727             float tsx1 = sx1 - x;
  1715             float tsx1 = sx1 - x;
  1728             float tsx2 = sx2 - x;
  1716             float tsx2 = sx2 - x;
  1729 
  1717 
  1742             if (tex != last_tex) {
  1730             if (tex != last_tex) {
  1743                 if (tex >= txi->textures)
  1731                 if (tex >= txi->textures)
  1744                     return;
  1732                     return;
  1745                 if (-1 == txi->texture[tex])
  1733                 if (-1 == txi->texture[tex])
  1746                     return;
  1734                     return;
  1747                 glSDL_texture (this, txi->texture[tex]);
  1735                 glSDL_texture(this, txi->texture[tex]);
  1748                 last_tex = tex;
  1736                 last_tex = tex;
  1749             }
  1737             }
  1750 #ifdef GLSDL_GRAPHICAL_DEBUG
  1738 #ifdef GLSDL_GRAPHICAL_DEBUG
  1751             this->glDisable (GL_TEXTURE_2D);
  1739             this->glDisable(GL_TEXTURE_2D);
  1752             this->glBegin (GL_LINE_LOOP);
  1740             this->glBegin(GL_LINE_LOOP);
  1753             this->glColor4ub (0, 255, 0, 128);
  1741             this->glColor4ub(0, 255, 0, 128);
  1754             this->glVertex2i (tdx1, tdy1);
  1742             this->glVertex2i(tdx1, tdy1);
  1755             this->glVertex2i (tdx2, tdy1);
  1743             this->glVertex2i(tdx2, tdy1);
  1756             this->glVertex2i (tdx2, tdy2);
  1744             this->glVertex2i(tdx2, tdy2);
  1757             this->glVertex2i (tdx1, tdy2);
  1745             this->glVertex2i(tdx1, tdy2);
  1758             this->glEnd ();
  1746             this->glEnd();
  1759             this->glEnable (GL_TEXTURE_2D);
  1747             this->glEnable(GL_TEXTURE_2D);
  1760 #endif
  1748 #endif
  1761 
  1749 
  1762             this->glBegin (GL_TRIANGLE_FAN);
  1750             this->glBegin(GL_TRIANGLE_FAN);
  1763             this->glColor4ub (255, 255, 255, alpha);
  1751             this->glColor4ub(255, 255, 255, alpha);
  1764             this->glTexCoord2f (tsx1, tsy1);
  1752             this->glTexCoord2f(tsx1, tsy1);
  1765             this->glVertex2i (tdx1, tdy1);
  1753             this->glVertex2i(tdx1, tdy1);
  1766             this->glTexCoord2f (tsx2, tsy1);
  1754             this->glTexCoord2f(tsx2, tsy1);
  1767             this->glVertex2i (tdx2, tdy1);
  1755             this->glVertex2i(tdx2, tdy1);
  1768             this->glTexCoord2f (tsx2, tsy2);
  1756             this->glTexCoord2f(tsx2, tsy2);
  1769             this->glVertex2i (tdx2, tdy2);
  1757             this->glVertex2i(tdx2, tdy2);
  1770             this->glTexCoord2f (tsx1, tsy2);
  1758             this->glTexCoord2f(tsx1, tsy2);
  1771             this->glVertex2i (tdx1, tdy2);
  1759             this->glVertex2i(tdx1, tdy2);
  1772             this->glEnd ();
  1760             this->glEnd();
  1773         }
  1761         }
  1774     }
  1762     }
  1775 }
  1763 }
  1776 
  1764 
  1777 /*
  1765 /*
  1788  *	rect	destination rectangle
  1776  *	rect	destination rectangle
  1789  *
  1777  *
  1790  * Returns 1 if the result is visible, otherwise 0.
  1778  * Returns 1 if the result is visible, otherwise 0.
  1791  */
  1779  */
  1792 static __inline__ int
  1780 static __inline__ int
  1793 blitclip (SDL_Rect * rect, int w, int h, int *x, int *y, SDL_Rect * clip)
  1781 blitclip(SDL_Rect * rect, int w, int h, int *x, int *y, SDL_Rect * clip)
  1794 {
  1782 {
  1795     int sx1, sy1, sx2, sy2;
  1783     int sx1, sy1, sx2, sy2;
  1796     int dx1, dy1, dx2, dy2;
  1784     int dx1, dy1, dx2, dy2;
  1797 
  1785 
  1798     /* Get source and destination coordinates */
  1786     /* Get source and destination coordinates */
  1851     rect->h = dy2 - dy1;
  1839     rect->h = dy2 - dy1;
  1852     return 1;
  1840     return 1;
  1853 }
  1841 }
  1854 
  1842 
  1855 static int
  1843 static int
  1856 glSDL_BlitGL (_THIS, SDL_Surface * src,
  1844 glSDL_BlitGL(_THIS, SDL_Surface * src, SDL_Rect * srcrect, SDL_Rect * dstrect)
  1857               SDL_Rect * srcrect, SDL_Rect * dstrect)
       
  1858 {
  1845 {
  1859     private_hwdata *txi;
  1846     private_hwdata *txi;
  1860     float x1, y1;
  1847     float x1, y1;
  1861     unsigned char alpha;
  1848     unsigned char alpha;
  1862     SDL_Rect d;
  1849     SDL_Rect d;
  1863     int x, y;
  1850     int x, y;
  1864     SDL_Rect r;
  1851     SDL_Rect r;
  1865 
  1852 
  1866     if (!src)
  1853     if (!src)
  1867         return GLERET ("BlitGL(): No src surface!");
  1854         return GLERET("BlitGL(): No src surface!");
  1868 
  1855 
  1869     /* Get source and destination coordinates */
  1856     /* Get source and destination coordinates */
  1870     if (srcrect)
  1857     if (srcrect)
  1871         r = *srcrect;
  1858         r = *srcrect;
  1872     else {
  1859     else {
  1879         y = dstrect->y;
  1866         y = dstrect->y;
  1880     } else
  1867     } else
  1881         x = y = 0;
  1868         x = y = 0;
  1882 
  1869 
  1883     /* Clip! */
  1870     /* Clip! */
  1884     if (!blitclip (&r, src->w, src->h, &x, &y, &this->screen->clip_rect)) {
  1871     if (!blitclip(&r, src->w, src->h, &x, &y, &this->screen->clip_rect)) {
  1885         if (dstrect)
  1872         if (dstrect)
  1886             dstrect->w = dstrect->h = 0;
  1873             dstrect->w = dstrect->h = 0;
  1887         return 0;
  1874         return 0;
  1888     }
  1875     }
  1889 
  1876 
  1890     /* Write back the resulting cliprect */
  1877     /* Write back the resulting cliprect */
  1891     if (dstrect)
  1878     if (dstrect)
  1892         *dstrect = r;
  1879         *dstrect = r;
  1893 
  1880 
  1894     /* Make sure we have a source with a valid texture */
  1881     /* Make sure we have a source with a valid texture */
  1895     txi = glSDL_UploadSurface (this, src);
  1882     txi = glSDL_UploadSurface(this, src);
  1896     if (!txi)
  1883     if (!txi)
  1897         return GLERET ("BlitGL(): Could not get a TexInfo!");
  1884         return GLERET("BlitGL(): Could not get a TexInfo!");
  1898 
  1885 
  1899     /* Set up blending */
  1886     /* Set up blending */
  1900     if (src->flags & (SDL_SRCALPHA | SDL_SRCCOLORKEY)) {
  1887     if (src->flags & (SDL_SRCALPHA | SDL_SRCCOLORKEY)) {
  1901         glSDL_blendfunc (this, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  1888         glSDL_blendfunc(this, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  1902         glSDL_do_blend (this, 1);
  1889         glSDL_do_blend(this, 1);
  1903     } else
  1890     } else
  1904         glSDL_do_blend (this, 0);
  1891         glSDL_do_blend(this, 0);
  1905 
  1892 
  1906     /* Enable texturing */
  1893     /* Enable texturing */
  1907     glSDL_do_texture (this, 1);
  1894     glSDL_do_texture(this, 1);
  1908 
  1895 
  1909     /* Calculate texcoords */
  1896     /* Calculate texcoords */
  1910     if (!srcrect)
  1897     if (!srcrect)
  1911         srcrect = &txi->virt;
  1898         srcrect = &txi->virt;
  1912     x1 = (float) srcrect->x;
  1899     x1 = (float) srcrect->x;
  1935         alpha = src->format->alpha;
  1922         alpha = src->format->alpha;
  1936 
  1923 
  1937     /* Render! */
  1924     /* Render! */
  1938     switch (txi->tilemode) {
  1925     switch (txi->tilemode) {
  1939     case GLSDL_TM_SINGLE:
  1926     case GLSDL_TM_SINGLE:
  1940         glSDL_BlitGL_single (this, txi, x1, y1, &d, alpha);
  1927         glSDL_BlitGL_single(this, txi, x1, y1, &d, alpha);
  1941         break;
  1928         break;
  1942     case GLSDL_TM_HORIZONTAL:
  1929     case GLSDL_TM_HORIZONTAL:
  1943         glSDL_BlitGL_htile (this, txi, x1, y1, &d, alpha);
  1930         glSDL_BlitGL_htile(this, txi, x1, y1, &d, alpha);
  1944         break;
  1931         break;
  1945     case GLSDL_TM_VERTICAL:
  1932     case GLSDL_TM_VERTICAL:
  1946         glSDL_BlitGL_vtile (this, txi, x1, y1, &d, alpha);
  1933         glSDL_BlitGL_vtile(this, txi, x1, y1, &d, alpha);
  1947         break;
  1934         break;
  1948     case GLSDL_TM_HUGE:
  1935     case GLSDL_TM_HUGE:
  1949         glSDL_BlitGL_hvtile (this, src, txi, x1, y1, &d, alpha);
  1936         glSDL_BlitGL_hvtile(this, src, txi, x1, y1, &d, alpha);
  1950         break;
  1937         break;
  1951     }
  1938     }
  1952 
  1939 
  1953     if (txi->temporary)
  1940     if (txi->temporary)
  1954         glSDL_FreeTexInfo (this, txi);
  1941         glSDL_FreeTexInfo(this, txi);
  1955 
  1942 
  1956     return 0;
  1943     return 0;
  1957 }
  1944 }
  1958 
  1945 
  1959 
  1946 
  1960 static int
  1947 static int
  1961 glSDL_HWAccelBlit (SDL_Surface * src, SDL_Rect * srcrect,
  1948 glSDL_HWAccelBlit(SDL_Surface * src, SDL_Rect * srcrect,
  1962                    SDL_Surface * dst, SDL_Rect * dstrect)
  1949                   SDL_Surface * dst, SDL_Rect * dstrect)
  1963 {
  1950 {
  1964     SDL_Surface *vs;
  1951     SDL_Surface *vs;
  1965 
  1952 
  1966     if (!src)
  1953     if (!src)
  1967         return GLERET ("HWAccelBlit(): No src surface!");
  1954         return GLERET("HWAccelBlit(): No src surface!");
  1968     if (!dst)
  1955     if (!dst)
  1969         return GLERET ("HWAccelBlit(): No dst surface!");
  1956         return GLERET("HWAccelBlit(): No dst surface!");
  1970 
  1957 
  1971     /*
  1958     /*
  1972      * Figure out what to do:
  1959      * Figure out what to do:
  1973      *      screen->screen:         glSDL_BlitFromGL() + glSDL_BlitGL()
  1960      *      screen->screen:         glSDL_BlitFromGL() + glSDL_BlitGL()
  1974      *      surface->screen:        glSDL_BlitGL()
  1961      *      surface->screen:        glSDL_BlitGL()
  1979     if (src == vs) {
  1966     if (src == vs) {
  1980         if (dst == vs) {
  1967         if (dst == vs) {
  1981             /*
  1968             /*
  1982                FIXME: Try glCopyPixels() instead...
  1969                FIXME: Try glCopyPixels() instead...
  1983              */
  1970              */
  1984             glSDL_BlitFromGL (current_video, srcrect, vs, dstrect);
  1971             glSDL_BlitFromGL(current_video, srcrect, vs, dstrect);
  1985             return glSDL_BlitGL (current_video, vs, srcrect, dstrect);
  1972             return glSDL_BlitGL(current_video, vs, srcrect, dstrect);
  1986         } else {
  1973         } else {
  1987             return glSDL_BlitFromGL (current_video, srcrect, dst, dstrect);
  1974             return glSDL_BlitFromGL(current_video, srcrect, dst, dstrect);
  1988         }
  1975         }
  1989     } else {
  1976     } else {
  1990         if (dst == vs) {
  1977         if (dst == vs) {
  1991             return glSDL_BlitGL (current_video, src, srcrect, dstrect);
  1978             return glSDL_BlitGL(current_video, src, srcrect, dstrect);
  1992         } else {
  1979         } else {
  1993             glSDL_Invalidate (dst, dstrect);
  1980             glSDL_Invalidate(dst, dstrect);
  1994             glSDL_SoftBlit (src, srcrect, dst, dstrect);
  1981             glSDL_SoftBlit(src, srcrect, dst, dstrect);
  1995             return 0;
  1982             return 0;
  1996         }
  1983         }
  1997     }
  1984     }
  1998 }
  1985 }
  1999 
  1986 
  2000 
  1987 
  2001 static int
  1988 static int
  2002 glSDL_FillHWRect (_THIS, SDL_Surface * dst, SDL_Rect * dstrect, Uint32 color)
  1989 glSDL_FillHWRect(_THIS, SDL_Surface * dst, SDL_Rect * dstrect, Uint32 color)
  2003 {
  1990 {
  2004     SDL_Surface *vs = SDL_VideoSurface;
  1991     SDL_Surface *vs = SDL_VideoSurface;
  2005     int dx1, dy1, dx2, dy2;
  1992     int dx1, dy1, dx2, dy2;
  2006     Uint32 r, g, b;
  1993     Uint32 r, g, b;
  2007     Uint8 br, bg, bb;
  1994     Uint8 br, bg, bb;
  2013      * SDL_VideoSurface may differ!)
  2000      * SDL_VideoSurface may differ!)
  2014      */
  2001      */
  2015 
  2002 
  2016     if (dst->format->palette) {
  2003     if (dst->format->palette) {
  2017         /* this a paletted color */
  2004         /* this a paletted color */
  2018         SDL_GetRGB (color, dst->format, &br, &bg, &bb);
  2005         SDL_GetRGB(color, dst->format, &br, &bg, &bb);
  2019     } else {
  2006     } else {
  2020         /* this a RGB color */
  2007         /* this a RGB color */
  2021         r = color & dst->format->Rmask;
  2008         r = color & dst->format->Rmask;
  2022         r = r >> dst->format->Rshift;
  2009         r = r >> dst->format->Rshift;
  2023         r = r << dst->format->Rloss;
  2010         r = r << dst->format->Rloss;
  2034         bb = b;
  2021         bb = b;
  2035     }
  2022     }
  2036 
  2023 
  2037     if (vs != dst) {
  2024     if (vs != dst) {
  2038         /* draw a rect offscreen */
  2025         /* draw a rect offscreen */
  2039         glSDL_Invalidate (dst, dstrect);
  2026         glSDL_Invalidate(dst, dstrect);
  2040         /* software-fill the surface by faking it as a SW_SURFACE */
  2027         /* software-fill the surface by faking it as a SW_SURFACE */
  2041         dst->flags &= ~SDL_HWSURFACE;
  2028         dst->flags &= ~SDL_HWSURFACE;
  2042         SDL_FillRect (dst, dstrect, color);
  2029         SDL_FillRect(dst, dstrect, color);
  2043         dst->flags |= SDL_HWSURFACE;
  2030         dst->flags |= SDL_HWSURFACE;
  2044     } else {
  2031     } else {
  2045         /* draw a rect onscreen */
  2032         /* draw a rect onscreen */
  2046         glSDL_do_texture (this, 0);
  2033         glSDL_do_texture(this, 0);
  2047         glSDL_do_blend (this, 0);
  2034         glSDL_do_blend(this, 0);
  2048 
  2035 
  2049         dx1 = dstrect->x;
  2036         dx1 = dstrect->x;
  2050         dy1 = dstrect->y;
  2037         dy1 = dstrect->y;
  2051         dx2 = dx1 + dstrect->w;
  2038         dx2 = dx1 + dstrect->w;
  2052         dy2 = dy1 + dstrect->h;
  2039         dy2 = dy1 + dstrect->h;
  2053 
  2040 
  2054         this->glBegin (GL_TRIANGLE_FAN);
  2041         this->glBegin(GL_TRIANGLE_FAN);
  2055         this->glColor3ub (br, bg, bb);
  2042         this->glColor3ub(br, bg, bb);
  2056         this->glVertex2i (dx1, dy1);
  2043         this->glVertex2i(dx1, dy1);
  2057         this->glVertex2i (dx2, dy1);
  2044         this->glVertex2i(dx2, dy1);
  2058         this->glVertex2i (dx2, dy2);
  2045         this->glVertex2i(dx2, dy2);
  2059         this->glVertex2i (dx1, dy2);
  2046         this->glVertex2i(dx1, dy2);
  2060         this->glEnd ();
  2047         this->glEnd();
  2061     }
  2048     }
  2062     return 0;
  2049     return 0;
  2063 }
  2050 }
  2064 
  2051 
  2065 static int
  2052 static int
  2066 glSDL_CheckHWBlit (_THIS, SDL_Surface * src, SDL_Surface * dst)
  2053 glSDL_CheckHWBlit(_THIS, SDL_Surface * src, SDL_Surface * dst)
  2067 {
  2054 {
  2068     src->flags |= SDL_HWACCEL;
  2055     src->flags |= SDL_HWACCEL;
  2069     src->map->hw_blit = glSDL_HWAccelBlit;
  2056     src->map->hw_blit = glSDL_HWAccelBlit;
  2070     return 1;
  2057     return 1;
  2071 }
  2058 }
  2072 
  2059 
  2073 
  2060 
  2074 static SDL_Surface *
  2061 static SDL_Surface *
  2075 glSDL_DisplayFormat (SDL_Surface * surface)
  2062 glSDL_DisplayFormat(SDL_Surface * surface)
  2076 {
  2063 {
  2077     SDL_Surface *tmp;
  2064     SDL_Surface *tmp;
  2078     int use_rgba = (surface->flags & SDL_SRCCOLORKEY) ||
  2065     int use_rgba = (surface->flags & SDL_SRCCOLORKEY) ||
  2079         ((surface->flags & SDL_SRCALPHA) && surface->format->Amask);
  2066         ((surface->flags & SDL_SRCALPHA) && surface->format->Amask);
  2080 #ifdef DEBUG_GLSDL
  2067 #ifdef DEBUG_GLSDL
  2081     fprintf (stderr, "#### glSDL_DisplayFormat()\n");
  2068     fprintf(stderr, "#### glSDL_DisplayFormat()\n");
  2082 #endif
  2069 #endif
  2083     if (use_rgba)
  2070     if (use_rgba)
  2084         tmp = glSDL_ConvertSurface (surface, RGBAfmt, SDL_SWSURFACE);
  2071         tmp = glSDL_ConvertSurface(surface, RGBAfmt, SDL_SWSURFACE);
  2085     else
  2072     else
  2086         tmp = glSDL_ConvertSurface (surface, RGBfmt, SDL_SWSURFACE);
  2073         tmp = glSDL_ConvertSurface(surface, RGBfmt, SDL_SWSURFACE);
  2087     if (!tmp) {
  2074     if (!tmp) {
  2088         GLERR ("glSDL_DisplayFormat() could not convert surface!");
  2075         GLERR("glSDL_DisplayFormat() could not convert surface!");
  2089         return NULL;
  2076         return NULL;
  2090     }
  2077     }
  2091     SDL_SetAlpha (tmp, 0, 0);
  2078     SDL_SetAlpha(tmp, 0, 0);
  2092 
  2079 
  2093     if (surface->flags & SDL_SRCCOLORKEY) {
  2080     if (surface->flags & SDL_SRCCOLORKEY) {
  2094         /*
  2081         /*
  2095          * We drop colorkey data here, but we have to,
  2082          * We drop colorkey data here, but we have to,
  2096          * or we'll run into trouble when converting,
  2083          * or we'll run into trouble when converting,
  2097          * in particular from indexed color formats.
  2084          * in particular from indexed color formats.
  2098          */
  2085          */
  2099         SDL_SetColorKey (tmp, SDL_SRCCOLORKEY, surface->format->colorkey);
  2086         SDL_SetColorKey(tmp, SDL_SRCCOLORKEY, surface->format->colorkey);
  2100         glSDL_key2alpha (tmp);
  2087         glSDL_key2alpha(tmp);
  2101         SDL_SetColorKey (tmp, 0, 0);
  2088         SDL_SetColorKey(tmp, 0, 0);
  2102     }
  2089     }
  2103 
  2090 
  2104     return tmp;
  2091     return tmp;
  2105 }
  2092 }
  2106 
  2093 
  2107 
  2094 
  2108 static SDL_Surface *
  2095 static SDL_Surface *
  2109 glSDL_DisplayFormatAlpha (SDL_Surface * surface)
  2096 glSDL_DisplayFormatAlpha(SDL_Surface * surface)
  2110 {
  2097 {
  2111     SDL_Surface *s, *tmp;
  2098     SDL_Surface *s, *tmp;
  2112     tmp = glSDL_ConvertSurface (surface, RGBAfmt, SDL_SWSURFACE);
  2099     tmp = glSDL_ConvertSurface(surface, RGBAfmt, SDL_SWSURFACE);
  2113 #ifdef DEBUG_GLSDL
  2100 #ifdef DEBUG_GLSDL
  2114     fprintf (stderr, "#### glSDL_DisplayFormatAlpha()\n");
  2101     fprintf(stderr, "#### glSDL_DisplayFormatAlpha()\n");
  2115 #endif
  2102 #endif
  2116     if (!tmp)
  2103     if (!tmp)
  2117         return NULL;
  2104         return NULL;
  2118 
  2105 
  2119     SDL_SetAlpha (tmp, 0, 0);
  2106     SDL_SetAlpha(tmp, 0, 0);
  2120     SDL_SetColorKey (tmp, 0, 0);
  2107     SDL_SetColorKey(tmp, 0, 0);
  2121     s = glSDL_CreateRGBASurface (surface->w, surface->h);
  2108     s = glSDL_CreateRGBASurface(surface->w, surface->h);
  2122     if (!s) {
  2109     if (!s) {
  2123         SDL_FreeSurface (tmp);
  2110         SDL_FreeSurface(tmp);
  2124         return NULL;
  2111         return NULL;
  2125     }
  2112     }
  2126     glSDL_SoftBlit (tmp, NULL, s, NULL);
  2113     glSDL_SoftBlit(tmp, NULL, s, NULL);
  2127     SDL_FreeSurface (tmp);
  2114     SDL_FreeSurface(tmp);
  2128 
  2115 
  2129     if (surface->flags & SDL_SRCCOLORKEY) {
  2116     if (surface->flags & SDL_SRCCOLORKEY) {
  2130         SDL_SetColorKey (s, SDL_SRCCOLORKEY, surface->format->colorkey);
  2117         SDL_SetColorKey(s, SDL_SRCCOLORKEY, surface->format->colorkey);
  2131         glSDL_key2alpha (s);
  2118         glSDL_key2alpha(s);
  2132         SDL_SetColorKey (s, 0, 0);
  2119         SDL_SetColorKey(s, 0, 0);
  2133     }
  2120     }
  2134 
  2121 
  2135     if (surface->flags & SDL_SRCALPHA)
  2122     if (surface->flags & SDL_SRCALPHA)
  2136         SDL_SetAlpha (s, SDL_SRCALPHA, surface->format->alpha);
  2123         SDL_SetAlpha(s, SDL_SRCALPHA, surface->format->alpha);
  2137     return s;
  2124     return s;
  2138 }
  2125 }
  2139 
  2126 
  2140 
  2127 
  2141 /*----------------------------------------------------------
  2128 /*----------------------------------------------------------
  2142   glSDL specific API extensions
  2129   glSDL specific API extensions
  2143   ----------------------------------------------------------*/
  2130   ----------------------------------------------------------*/
  2144 
  2131 
  2145 static void
  2132 static void
  2146 glSDL_Invalidate (SDL_Surface * surface, SDL_Rect * area)
  2133 glSDL_Invalidate(SDL_Surface * surface, SDL_Rect * area)
  2147 {
  2134 {
  2148     private_hwdata *txi;
  2135     private_hwdata *txi;
  2149     if (!surface)
  2136     if (!surface)
  2150         return;
  2137         return;
  2151     txi = glSDL_GetTexInfo (surface);
  2138     txi = glSDL_GetTexInfo(surface);
  2152     if (!txi)
  2139     if (!txi)
  2153         return;
  2140         return;
  2154     if (!area) {
  2141     if (!area) {
  2155         txi->invalid_area.x = 0;
  2142         txi->invalid_area.x = 0;
  2156         txi->invalid_area.y = 0;
  2143         txi->invalid_area.y = 0;
  2161     txi->invalid_area = *area;
  2148     txi->invalid_area = *area;
  2162 }
  2149 }
  2163 
  2150 
  2164 
  2151 
  2165 static void
  2152 static void
  2166 glSDL_SetLogicSize (_THIS, SDL_Surface * surface, int w, int h)
  2153 glSDL_SetLogicSize(_THIS, SDL_Surface * surface, int w, int h)
  2167 {
  2154 {
  2168     SDL_Rect r;
  2155     SDL_Rect r;
  2169     private_hwdata *txi;
  2156     private_hwdata *txi;
  2170     if (!IS_GLSDL_SURFACE (surface))
  2157     if (!IS_GLSDL_SURFACE(surface))
  2171         return;
  2158         return;
  2172 
  2159 
  2173     txi = glSDL_GetTexInfo (surface);
  2160     txi = glSDL_GetTexInfo(surface);
  2174 
  2161 
  2175     txi->lw = w;
  2162     txi->lw = w;
  2176     txi->lh = h;
  2163     txi->lh = h;
  2177 
  2164 
  2178     if (SDL_VideoSurface != surface)
  2165     if (SDL_VideoSurface != surface)
  2179         return;
  2166         return;
  2180 
  2167 
  2181     r.x = r.y = 0;
  2168     r.x = r.y = 0;
  2182     r.w = w;
  2169     r.w = w;
  2183     r.h = h;
  2170     r.h = h;
  2184     glSDL_SetClipRect (this, surface, &r);
  2171     glSDL_SetClipRect(this, surface, &r);
  2185 
  2172 
  2186     this->glMatrixMode (GL_MODELVIEW);
  2173     this->glMatrixMode(GL_MODELVIEW);
  2187     this->glLoadIdentity ();
  2174     this->glLoadIdentity();
  2188     this->glTranslated (0.0f, 0.0f, 0.0f);
  2175     this->glTranslated(0.0f, 0.0f, 0.0f);
  2189 
  2176 
  2190     this->glDisable (GL_DEPTH_TEST);
  2177     this->glDisable(GL_DEPTH_TEST);
  2191     this->glDisable (GL_CULL_FACE);
  2178     this->glDisable(GL_CULL_FACE);
  2192 
  2179 
  2193     glSDL_reset ();
  2180     glSDL_reset();
  2194 }
  2181 }
  2195 
  2182 
  2196 static int
  2183 static int
  2197 glSDL_InitTexture (_THIS, SDL_Surface * datasurf, private_hwdata * txi,
  2184 glSDL_InitTexture(_THIS, SDL_Surface * datasurf, private_hwdata * txi,
  2198                    int tex)
  2185                   int tex)
  2199 {
  2186 {
  2200     this->glGenTextures (1, (GLuint *) & txi->texture[tex]);
  2187     this->glGenTextures(1, (GLuint *) & txi->texture[tex]);
  2201     this->glBindTexture (GL_TEXTURE_2D, txi->texture[tex]);
  2188     this->glBindTexture(GL_TEXTURE_2D, txi->texture[tex]);
  2202     glstate.texture = txi->texture[tex];
  2189     glstate.texture = txi->texture[tex];
  2203     this->glPixelStorei (GL_UNPACK_ROW_LENGTH, datasurf->pitch /
  2190     this->glPixelStorei(GL_UNPACK_ROW_LENGTH, datasurf->pitch /
  2204                          datasurf->format->BytesPerPixel);
  2191                         datasurf->format->BytesPerPixel);
  2205     this->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  2192     this->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  2206     this->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  2193     this->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  2207     this->glTexImage2D (GL_TEXTURE_2D, 0,
  2194     this->glTexImage2D(GL_TEXTURE_2D, 0,
  2208                         datasurf->format->Amask ? GL_RGBA8 : GL_RGB8,
  2195                        datasurf->format->Amask ? GL_RGBA8 : GL_RGB8,
  2209                         txi->texsize, txi->texsize, 0,
  2196                        txi->texsize, txi->texsize, 0,
  2210                         datasurf->format->Amask ? GL_RGBA : GL_RGB,
  2197                        datasurf->format->Amask ? GL_RGBA : GL_RGB,
  2211                         GL_UNSIGNED_BYTE, NULL);
  2198                        GL_UNSIGNED_BYTE, NULL);
  2212 #ifdef DEBUG_GLSDL
  2199 #ifdef DEBUG_GLSDL
  2213     glSDL_print_glerror (this, 1);
  2200     glSDL_print_glerror(this, 1);
  2214 #endif
  2201 #endif
  2215     return 0;
  2202     return 0;
  2216 }
  2203 }
  2217 
  2204 
  2218 
  2205 
  2219 /* Image tiled horizontally (wide surface), or not at all */
  2206 /* Image tiled horizontally (wide surface), or not at all */
  2220 static int
  2207 static int
  2221 glSDL_UploadHoriz (_THIS, SDL_Surface * datasurf, private_hwdata * txi)
  2208 glSDL_UploadHoriz(_THIS, SDL_Surface * datasurf, private_hwdata * txi)
  2222 {
  2209 {
  2223     int bpp = datasurf->format->BytesPerPixel;
  2210     int bpp = datasurf->format->BytesPerPixel;
  2224     int res;
  2211     int res;
  2225     int tex = 0;
  2212     int tex = 0;
  2226     int fromx = 0;
  2213     int fromx = 0;
  2231             thistw = txi->tilew;
  2218             thistw = txi->tilew;
  2232         else if (thistw <= 0)
  2219         else if (thistw <= 0)
  2233             break;
  2220             break;
  2234         if (toy + txi->tileh > txi->texsize) {
  2221         if (toy + txi->tileh > txi->texsize) {
  2235             toy = 0;
  2222             toy = 0;
  2236             res = glSDL_InitTexture (this, datasurf, txi, tex);
  2223             res = glSDL_InitTexture(this, datasurf, txi, tex);
  2237             if (res < 0)
  2224             if (res < 0)
  2238                 return res;
  2225                 return res;
  2239             ++tex;
  2226             ++tex;
  2240         }
  2227         }
  2241         this->glTexSubImage2D (GL_TEXTURE_2D, 0, 0, toy,
  2228         this->glTexSubImage2D(GL_TEXTURE_2D, 0, 0, toy,
  2242                                thistw, txi->tileh,
  2229                               thistw, txi->tileh,
  2243                                datasurf->format->Amask ? GL_RGBA : GL_RGB,
  2230                               datasurf->format->Amask ? GL_RGBA : GL_RGB,
  2244                                GL_UNSIGNED_BYTE,
  2231                               GL_UNSIGNED_BYTE,
  2245                                (char *) datasurf->pixels + bpp * fromx);
  2232                               (char *) datasurf->pixels + bpp * fromx);
  2246 #ifdef DEBUG_GLSDL
  2233 #ifdef DEBUG_GLSDL
  2247         glSDL_print_glerror (this, 2);
  2234         glSDL_print_glerror(this, 2);
  2248 #endif
  2235 #endif
  2249         fromx += txi->tilew;
  2236         fromx += txi->tilew;
  2250         toy += txi->tileh;
  2237         toy += txi->tileh;
  2251     }
  2238     }
  2252     return 0;
  2239     return 0;
  2253 }
  2240 }
  2254 
  2241 
  2255 
  2242 
  2256 /* Image tiled vertically (tall surface) */
  2243 /* Image tiled vertically (tall surface) */
  2257 static int
  2244 static int
  2258 glSDL_UploadVert (_THIS, SDL_Surface * datasurf, private_hwdata * txi)
  2245 glSDL_UploadVert(_THIS, SDL_Surface * datasurf, private_hwdata * txi)
  2259 {
  2246 {
  2260     int res;
  2247     int res;
  2261     int tex = 0;
  2248     int tex = 0;
  2262     int fromy = 0;
  2249     int fromy = 0;
  2263     int tox = txi->texsize;     /* To init first texture */
  2250     int tox = txi->texsize;     /* To init first texture */
  2267             thisth = txi->tileh;
  2254             thisth = txi->tileh;
  2268         else if (thisth <= 0)
  2255         else if (thisth <= 0)
  2269             break;
  2256             break;
  2270         if (tox + txi->tilew > txi->texsize) {
  2257         if (tox + txi->tilew > txi->texsize) {
  2271             tox = 0;
  2258             tox = 0;
  2272             res = glSDL_InitTexture (this, datasurf, txi, tex);
  2259             res = glSDL_InitTexture(this, datasurf, txi, tex);
  2273             if (res < 0)
  2260             if (res < 0)
  2274                 return res;
  2261                 return res;
  2275             ++tex;
  2262             ++tex;
  2276         }
  2263         }
  2277         this->glTexSubImage2D (GL_TEXTURE_2D, 0, tox, 0,
  2264         this->glTexSubImage2D(GL_TEXTURE_2D, 0, tox, 0,
  2278                                txi->tilew, thisth,
  2265                               txi->tilew, thisth,
  2279                                datasurf->format->Amask ? GL_RGBA : GL_RGB,
  2266                               datasurf->format->Amask ? GL_RGBA : GL_RGB,
  2280                                GL_UNSIGNED_BYTE,
  2267                               GL_UNSIGNED_BYTE,
  2281                                (char *) datasurf->pixels +
  2268                               (char *) datasurf->pixels +
  2282                                datasurf->pitch * fromy);
  2269                               datasurf->pitch * fromy);
  2283 #ifdef DEBUG_GLSDL
  2270 #ifdef DEBUG_GLSDL
  2284         glSDL_print_glerror (this, 3);
  2271         glSDL_print_glerror(this, 3);
  2285 #endif
  2272 #endif
  2286         fromy += txi->tileh;
  2273         fromy += txi->tileh;
  2287         tox += txi->tilew;
  2274         tox += txi->tilew;
  2288     }
  2275     }
  2289     return 0;
  2276     return 0;
  2290 }
  2277 }
  2291 
  2278 
  2292 
  2279 
  2293 /* Image tiled two-way (huge surface) */
  2280 /* Image tiled two-way (huge surface) */
  2294 static int
  2281 static int
  2295 glSDL_UploadHuge (_THIS, SDL_Surface * datasurf, private_hwdata * txi)
  2282 glSDL_UploadHuge(_THIS, SDL_Surface * datasurf, private_hwdata * txi)
  2296 {
  2283 {
  2297     int bpp = datasurf->format->BytesPerPixel;
  2284     int bpp = datasurf->format->BytesPerPixel;
  2298     int res;
  2285     int res;
  2299     int tex = 0;
  2286     int tex = 0;
  2300     int y = 0;
  2287     int y = 0;
  2306         x = 0;
  2293         x = 0;
  2307         while (x < datasurf->w) {
  2294         while (x < datasurf->w) {
  2308             int thistw = datasurf->w - x;
  2295             int thistw = datasurf->w - x;
  2309             if (thistw > txi->tilew)
  2296             if (thistw > txi->tilew)
  2310                 thistw = txi->tilew;
  2297                 thistw = txi->tilew;
  2311             res = glSDL_InitTexture (this, datasurf, txi, tex++);
  2298             res = glSDL_InitTexture(this, datasurf, txi, tex++);
  2312             if (res < 0)
  2299             if (res < 0)
  2313                 return res;
  2300                 return res;
  2314             this->glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0,
  2301             this->glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
  2315                                    thistw, thisth,
  2302                                   thistw, thisth,
  2316                                    datasurf->format->
  2303                                   datasurf->format->
  2317                                    Amask ? GL_RGBA : GL_RGB,
  2304                                   Amask ? GL_RGBA : GL_RGB,
  2318                                    GL_UNSIGNED_BYTE,
  2305                                   GL_UNSIGNED_BYTE,
  2319                                    (char *) datasurf->pixels +
  2306                                   (char *) datasurf->pixels +
  2320                                    datasurf->pitch * y + bpp * x);
  2307                                   datasurf->pitch * y + bpp * x);
  2321 #ifdef DEBUG_GLSDL
  2308 #ifdef DEBUG_GLSDL
  2322             fprintf (stderr,
  2309             fprintf(stderr,
  2323                      "glTexSubImage(x = %d, y = %d, w = %d, h = %d)\n", x,
  2310                     "glTexSubImage(x = %d, y = %d, w = %d, h = %d)\n", x,
  2324                      y, thistw, thisth);
  2311                     y, thistw, thisth);
  2325             glSDL_print_glerror (this, 4);
  2312             glSDL_print_glerror(this, 4);
  2326 #endif
  2313 #endif
  2327             x += txi->tilew;
  2314             x += txi->tilew;
  2328         }
  2315         }
  2329         y += txi->tileh;
  2316         y += txi->tileh;
  2330     }
  2317     }
  2332 }
  2319 }
  2333 
  2320 
  2334 
  2321 
  2335 /* Upload all textures for a surface. */
  2322 /* Upload all textures for a surface. */
  2336 static int
  2323 static int
  2337 glSDL_UploadTextures (_THIS, SDL_Surface * datasurf, private_hwdata * txi)
  2324 glSDL_UploadTextures(_THIS, SDL_Surface * datasurf, private_hwdata * txi)
  2338 {
  2325 {
  2339     switch (txi->tilemode) {
  2326     switch (txi->tilemode) {
  2340     case GLSDL_TM_SINGLE:
  2327     case GLSDL_TM_SINGLE:
  2341     case GLSDL_TM_HORIZONTAL:
  2328     case GLSDL_TM_HORIZONTAL:
  2342         glSDL_UploadHoriz (this, datasurf, txi);
  2329         glSDL_UploadHoriz(this, datasurf, txi);
  2343         break;
  2330         break;
  2344     case GLSDL_TM_VERTICAL:
  2331     case GLSDL_TM_VERTICAL:
  2345         glSDL_UploadVert (this, datasurf, txi);
  2332         glSDL_UploadVert(this, datasurf, txi);
  2346         break;
  2333         break;
  2347     case GLSDL_TM_HUGE:
  2334     case GLSDL_TM_HUGE:
  2348         glSDL_UploadHuge (this, datasurf, txi);
  2335         glSDL_UploadHuge(this, datasurf, txi);
  2349         break;
  2336         break;
  2350     }
  2337     }
  2351     return 0;
  2338     return 0;
  2352 }
  2339 }
  2353 
  2340 
  2362  *	TexInfo must be used only once and then thrown away,
  2349  *	TexInfo must be used only once and then thrown away,
  2363  *	since it means that glSDL cannot track changes in
  2350  *	since it means that glSDL cannot track changes in
  2364  *	the pixel data of 'texture'.
  2351  *	the pixel data of 'texture'.
  2365  */
  2352  */
  2366 static private_hwdata *
  2353 static private_hwdata *
  2367 glSDL_UploadSurface (_THIS, SDL_Surface * surface)
  2354 glSDL_UploadSurface(_THIS, SDL_Surface * surface)
  2368 {
  2355 {
  2369     int i;
  2356     int i;
  2370     int converted = 0;
  2357     int converted = 0;
  2371     private_hwdata *txi = glSDL_GetTexInfo (surface);
  2358     private_hwdata *txi = glSDL_GetTexInfo(surface);
  2372 
  2359 
  2373     if (IS_GLSDL_SURFACE (surface)) {
  2360     if (IS_GLSDL_SURFACE(surface)) {
  2374         /*
  2361         /*
  2375          * Ok, this is a glSDL surface, and it *might* be
  2362          * Ok, this is a glSDL surface, and it *might* be
  2376          * in texture memory already. If so, it may need
  2363          * in texture memory already. If so, it may need
  2377          * an update.
  2364          * an update.
  2378          */
  2365          */
  2379         if (txi->invalid_area.w) {
  2366         if (txi->invalid_area.w) {
  2380             glSDL_UnloadTexture (this, txi);
  2367             glSDL_UnloadTexture(this, txi);
  2381         } else {
  2368         } else {
  2382             int missing = 0;
  2369             int missing = 0;
  2383             if (txi->textures) {
  2370             if (txi->textures) {
  2384                 for (i = 0; i < txi->textures; ++i)
  2371                 for (i = 0; i < txi->textures; ++i)
  2385                     if (GLSDL_NOTEX == txi->texture[i]) {
  2372                     if (GLSDL_NOTEX == txi->texture[i]) {
  2395          * Nope, this isn't (yet) a glSDL surface. Let's
  2382          * Nope, this isn't (yet) a glSDL surface. Let's
  2396          * try to either make it one, or set up a temporary
  2383          * try to either make it one, or set up a temporary
  2397          * TexInfo for it, valid for only one blit.
  2384          * TexInfo for it, valid for only one blit.
  2398          */
  2385          */
  2399         if ((surface->flags & SDL_HWSURFACE) == SDL_HWSURFACE) {
  2386         if ((surface->flags & SDL_HWSURFACE) == SDL_HWSURFACE) {
  2400             txi = glSDL_AddTexInfo (this, surface);
  2387             txi = glSDL_AddTexInfo(this, surface);
  2401             if (!txi) {
  2388             if (!txi) {
  2402                 GLERR ("UploadSurface(): Could not add TexInfo!");
  2389                 GLERR("UploadSurface(): Could not add TexInfo!");
  2403                 return NULL;
  2390                 return NULL;
  2404             }
  2391             }
  2405             surface->flags |= SDL_HWSURFACE;
  2392             surface->flags |= SDL_HWSURFACE;
  2406             surface->flags |= SDL_HWACCEL;
  2393             surface->flags |= SDL_HWACCEL;
  2407         } else {
  2394         } else {
  2409              * FIXME
  2396              * FIXME
  2410              * here if the surface is small enough, it's a good 
  2397              * here if the surface is small enough, it's a good 
  2411              * candidate for a blit using glDrawPixels instead 
  2398              * candidate for a blit using glDrawPixels instead 
  2412              * of a texture blit
  2399              * of a texture blit
  2413              */
  2400              */
  2414             txi = glSDL_CreateTempTexInfo (this, surface);
  2401             txi = glSDL_CreateTempTexInfo(this, surface);
  2415             if (!txi) {
  2402             if (!txi) {
  2416                 GLERR ("UploadSurface(): Could not create temp TexInfo!");
  2403                 GLERR("UploadSurface(): Could not create temp TexInfo!");
  2417                 return NULL;
  2404                 return NULL;
  2418             }
  2405             }
  2419         }
  2406         }
  2420     }
  2407     }
  2421 
  2408 
  2422     if (txi->texsize > maxtexsize) {
  2409     if (txi->texsize > maxtexsize) {
  2423         /* This surface wasn't tiled properly... */
  2410         /* This surface wasn't tiled properly... */
  2424         if (txi->temporary)
  2411         if (txi->temporary)
  2425             glSDL_FreeTexInfo (this, txi);
  2412             glSDL_FreeTexInfo(this, txi);
  2426         GLERR ("UploadSurface(): Too large texture!");
  2413         GLERR("UploadSurface(): Too large texture!");
  2427         return NULL;
  2414         return NULL;
  2428     }
  2415     }
  2429 
  2416 
  2430     /*
  2417     /*
  2431      * Kludge: Convert if not of preferred RGB or RGBA format.
  2418      * Kludge: Convert if not of preferred RGB or RGBA format.
  2436      *
  2423      *
  2437      *      Besides, any surface that's been SDL_DisplayFormat()ed
  2424      *      Besides, any surface that's been SDL_DisplayFormat()ed
  2438      *      should already be in the best known OpenGL format -
  2425      *      should already be in the best known OpenGL format -
  2439      *      preferably one that makes DMA w/o conversion possible.
  2426      *      preferably one that makes DMA w/o conversion possible.
  2440      */
  2427      */
  2441     if (!glSDL_FormatIsOk (surface)) {
  2428     if (!glSDL_FormatIsOk(surface)) {
  2442 #ifdef DEBUG_GLSDL
  2429 #ifdef DEBUG_GLSDL
  2443         fprintf (stderr,
  2430         fprintf(stderr, "glSDL: WARNING: On-the-fly conversion performed!\n");
  2444                  "glSDL: WARNING: On-the-fly conversion performed!\n");
       
  2445 #endif
  2431 #endif
  2446         converted = 1;
  2432         converted = 1;
  2447         /* NOTE: We forget about the original surface here. */
  2433         /* NOTE: We forget about the original surface here. */
  2448         if (surface->format->Amask)
  2434         if (surface->format->Amask)
  2449             surface = glSDL_DisplayFormatAlpha (surface);
  2435             surface = glSDL_DisplayFormatAlpha(surface);
  2450         else
  2436         else
  2451             surface = glSDL_DisplayFormat (surface);
  2437             surface = glSDL_DisplayFormat(surface);
  2452         if (!surface) {
  2438         if (!surface) {
  2453             GLERR ("UploadSurface(): Could not convert surface!");
  2439             GLERR("UploadSurface(): Could not convert surface!");
  2454             if (txi->temporary)
  2440             if (txi->temporary)
  2455                 glSDL_FreeTexInfo (this, txi);
  2441                 glSDL_FreeTexInfo(this, txi);
  2456             return NULL;
  2442             return NULL;
  2457         }
  2443         }
  2458     }
  2444     }
  2459 
  2445 
  2460     glSDL_UploadTextures (this, surface, txi);
  2446     glSDL_UploadTextures(this, surface, txi);
  2461 
  2447 
  2462     if (converted)
  2448     if (converted)
  2463         SDL_FreeSurface (surface);
  2449         SDL_FreeSurface(surface);
  2464 
  2450 
  2465     return txi;
  2451     return txi;
  2466 }
  2452 }
  2467 
  2453 
  2468 
  2454 
  2469 static void
  2455 static void
  2470 glSDL_UnloadTexture (_THIS, private_hwdata * txi)
  2456 glSDL_UnloadTexture(_THIS, private_hwdata * txi)
  2471 {
  2457 {
  2472     int i;
  2458     int i;
  2473     for (i = 0; i < txi->textures; ++i)
  2459     for (i = 0; i < txi->textures; ++i)
  2474         if (txi->texture[i] != GLSDL_NOTEX)
  2460         if (txi->texture[i] != GLSDL_NOTEX)
  2475             this->glDeleteTextures (1, &txi->texture[i]);
  2461             this->glDeleteTextures(1, &txi->texture[i]);
  2476     SDL_memset (&txi->invalid_area, 0, sizeof (txi->invalid_area));
  2462     SDL_memset(&txi->invalid_area, 0, sizeof(txi->invalid_area));
  2477 }
  2463 }
  2478 
  2464 
  2479 /* vi: set ts=4 sw=4 expandtab: */
  2465 /* vi: set ts=4 sw=4 expandtab: */