src/video/ataricommon/SDL_atarigl.c
author Patrice Mandin
Thu, 02 Dec 2004 23:00:28 +0000
changeset 1003 ece5ab8acd1c
parent 1002 13aa96c76e86
child 1005 2ab1cb02a44e
permissions -rw-r--r--
Wrong RGB24 to RGB16 conversion
patmandin@989
     1
/*
patmandin@989
     2
    SDL - Simple DirectMedia Layer
patmandin@989
     3
    Copyright (C) 1997-2004 Sam Lantinga
patmandin@989
     4
patmandin@989
     5
    This library is free software; you can redistribute it and/or
patmandin@989
     6
    modify it under the terms of the GNU Library General Public
patmandin@989
     7
    License as published by the Free Software Foundation; either
patmandin@989
     8
    version 2 of the License, or (at your option) any later version.
patmandin@989
     9
patmandin@989
    10
    This library is distributed in the hope that it will be useful,
patmandin@989
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
patmandin@989
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
patmandin@989
    13
    Library General Public License for more details.
patmandin@989
    14
patmandin@989
    15
    You should have received a copy of the GNU Library General Public
patmandin@989
    16
    License along with this library; if not, write to the Free
patmandin@989
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
patmandin@989
    18
patmandin@989
    19
    Sam Lantinga
patmandin@989
    20
    slouken@libsdl.org
patmandin@989
    21
*/
patmandin@989
    22
patmandin@989
    23
/* Atari OSMesa.ldg implementation of SDL OpenGL support */
patmandin@989
    24
patmandin@989
    25
/*--- Includes ---*/
patmandin@989
    26
patmandin@991
    27
#include <stdio.h>
patmandin@991
    28
#include <stdlib.h>
patmandin@991
    29
#include <string.h>
patmandin@989
    30
#ifdef HAVE_OPENGL
patmandin@989
    31
#include <GL/osmesa.h>
patmandin@989
    32
#endif
patmandin@989
    33
patmandin@991
    34
#include <mint/osbind.h>
patmandin@991
    35
patmandin@989
    36
#include "SDL_video.h"
patmandin@989
    37
#include "SDL_error.h"
patmandin@989
    38
#include "SDL_endian.h"
patmandin@989
    39
#include "SDL_atarigl_c.h"
patmandin@991
    40
#ifdef ENABLE_OSMESA_SHARED
patmandin@991
    41
#include "SDL_loadso.h"
patmandin@991
    42
#endif
patmandin@989
    43
patmandin@991
    44
/*--- Defines ---*/
patmandin@991
    45
patmandin@991
    46
#define PATH_OSMESA_LDG	"osmesa.ldg"
patmandin@991
    47
#define PATH_MESAGL_LDG	"mesa_gl.ldg"
patmandin@991
    48
#define PATH_TINYGL_LDG	"tiny_gl.ldg"
patmandin@989
    49
patmandin@1000
    50
#define VDI_RGB	0xf
patmandin@1000
    51
patmandin@989
    52
/*--- Functions prototypes ---*/
patmandin@989
    53
patmandin@991
    54
static void SDL_AtariGL_UnloadLibrary(_THIS);
patmandin@991
    55
patmandin@1000
    56
#ifdef HAVE_OPENGL
patmandin@991
    57
static void CopyShadowNull(_THIS, SDL_Surface *surface);
patmandin@991
    58
static void CopyShadowDirect(_THIS, SDL_Surface *surface);
patmandin@1000
    59
static void CopyShadowRGBTo555(_THIS, SDL_Surface *surface);
patmandin@1000
    60
static void CopyShadowRGBTo565(_THIS, SDL_Surface *surface);
patmandin@1000
    61
static void CopyShadowRGBSwap(_THIS, SDL_Surface *surface);
patmandin@1000
    62
static void CopyShadowRGBToARGB(_THIS, SDL_Surface *surface);
patmandin@1000
    63
static void CopyShadowRGBToABGR(_THIS, SDL_Surface *surface);
patmandin@1000
    64
static void CopyShadowRGBToBGRA(_THIS, SDL_Surface *surface);
patmandin@1000
    65
static void CopyShadowRGBToRGBA(_THIS, SDL_Surface *surface);
patmandin@991
    66
static void CopyShadow8888To555(_THIS, SDL_Surface *surface);
patmandin@991
    67
static void CopyShadow8888To565(_THIS, SDL_Surface *surface);
patmandin@991
    68
patmandin@991
    69
static void ConvertNull(_THIS, SDL_Surface *surface);
patmandin@991
    70
static void Convert565To555be(_THIS, SDL_Surface *surface);
patmandin@991
    71
static void Convert565To555le(_THIS, SDL_Surface *surface);
patmandin@991
    72
static void Convert565le(_THIS, SDL_Surface *surface);
patmandin@991
    73
static void ConvertBGRAToABGR(_THIS, SDL_Surface *surface);
patmandin@991
    74
patmandin@991
    75
static int InitNew(_THIS, SDL_Surface *current);
patmandin@991
    76
static int InitOld(_THIS, SDL_Surface *current);
patmandin@1000
    77
#endif
patmandin@989
    78
patmandin@989
    79
/*--- Public functions ---*/
patmandin@989
    80
patmandin@989
    81
int SDL_AtariGL_Init(_THIS, SDL_Surface *current)
patmandin@989
    82
{
patmandin@989
    83
#ifdef HAVE_OPENGL
patmandin@991
    84
	if (gl_oldmesa) {
patmandin@991
    85
		gl_active = InitOld(this, current);		
patmandin@991
    86
	} else {
patmandin@991
    87
		gl_active = InitNew(this, current);		
patmandin@991
    88
	}
patmandin@991
    89
#endif
patmandin@989
    90
patmandin@989
    91
	return (gl_active);
patmandin@989
    92
}
patmandin@989
    93
patmandin@992
    94
void SDL_AtariGL_Quit(_THIS, SDL_bool unload)
patmandin@989
    95
{
patmandin@989
    96
#ifdef HAVE_OPENGL
patmandin@991
    97
	if (gl_oldmesa) {
patmandin@991
    98
		/* Old mesa implementations */
patmandin@991
    99
		if (this->gl_data->OSMesaDestroyLDG) {
patmandin@991
   100
			this->gl_data->OSMesaDestroyLDG();
patmandin@991
   101
		}
patmandin@991
   102
		if (gl_shadow) {
patmandin@991
   103
			Mfree(gl_shadow);
patmandin@991
   104
			gl_shadow = NULL;
patmandin@991
   105
		}
patmandin@991
   106
	} else {
patmandin@991
   107
		/* New mesa implementation */
patmandin@991
   108
		if (gl_ctx) {
patmandin@991
   109
			if (this->gl_data->OSMesaDestroyContext) {
patmandin@991
   110
				this->gl_data->OSMesaDestroyContext(gl_ctx);
patmandin@991
   111
			}
patmandin@991
   112
			gl_ctx = NULL;
patmandin@991
   113
		}
patmandin@991
   114
	}
patmandin@991
   115
patmandin@992
   116
	if (unload) {
patmandin@992
   117
		SDL_AtariGL_UnloadLibrary(this);
patmandin@992
   118
	}
patmandin@991
   119
patmandin@991
   120
#endif /* HAVE_OPENGL */
patmandin@989
   121
	gl_active = 0;
patmandin@989
   122
}
patmandin@989
   123
patmandin@989
   124
int SDL_AtariGL_LoadLibrary(_THIS, const char *path)
patmandin@989
   125
{
patmandin@989
   126
#ifdef HAVE_OPENGL
patmandin@991
   127
patmandin@991
   128
#ifdef ENABLE_OSMESA_SHARED
patmandin@991
   129
	void *handle;
patmandin@995
   130
	SDL_bool cancel_load;
patmandin@991
   131
patmandin@991
   132
	if (gl_active) {
patmandin@991
   133
		SDL_SetError("OpenGL context already created");
patmandin@991
   134
		return -1;
patmandin@991
   135
	}
patmandin@991
   136
patmandin@991
   137
	/* Unload previous driver */
patmandin@991
   138
	SDL_AtariGL_UnloadLibrary(this);
patmandin@991
   139
patmandin@991
   140
	/* Load library given by path */
patmandin@991
   141
	handle = SDL_LoadObject(path);
patmandin@991
   142
	if (handle == NULL) {
patmandin@991
   143
		/* Try to load another one */
patmandin@991
   144
		path = getenv("SDL_VIDEO_GL_DRIVER");
patmandin@991
   145
		if ( path != NULL ) {
patmandin@991
   146
			handle = SDL_LoadObject(path);
patmandin@991
   147
		}
patmandin@991
   148
patmandin@991
   149
		/* If it does not work, try some other */
patmandin@991
   150
		if (handle == NULL) {
patmandin@991
   151
			path = PATH_OSMESA_LDG;
patmandin@991
   152
			handle = SDL_LoadObject(path);
patmandin@991
   153
		}
patmandin@991
   154
patmandin@991
   155
		if (handle == NULL) {
patmandin@991
   156
			path = PATH_MESAGL_LDG;
patmandin@991
   157
			handle = SDL_LoadObject(path);
patmandin@991
   158
		}
patmandin@991
   159
patmandin@991
   160
		if (handle == NULL) {
patmandin@991
   161
			path = PATH_TINYGL_LDG;
patmandin@991
   162
			handle = SDL_LoadObject(path);
patmandin@991
   163
		}
patmandin@991
   164
	}
patmandin@991
   165
patmandin@991
   166
	if (handle == NULL) {
patmandin@991
   167
		SDL_SetError("Could not load OpenGL library");
patmandin@991
   168
		return -1;
patmandin@991
   169
	}
patmandin@991
   170
patmandin@993
   171
	this->gl_data->glGetIntegerv = SDL_LoadFunction(handle, "glGetIntegerv");
patmandin@993
   172
	this->gl_data->glFinish = SDL_LoadFunction(handle, "glFinish");
patmandin@995
   173
	this->gl_data->glFlush = SDL_LoadFunction(handle, "glFlush");
patmandin@993
   174
patmandin@995
   175
	cancel_load = SDL_FALSE;
patmandin@995
   176
	if (this->gl_data->glGetIntegerv == NULL) {
patmandin@995
   177
		cancel_load = SDL_TRUE;
patmandin@995
   178
	} else {
patmandin@995
   179
		/* We need either glFinish (OSMesa) or glFlush (TinyGL) */
patmandin@995
   180
		if ((this->gl_data->glFinish == NULL) &&
patmandin@995
   181
			(this->gl_data->glFlush == NULL)) {
patmandin@995
   182
				cancel_load = SDL_TRUE;
patmandin@995
   183
		}
patmandin@995
   184
	}
patmandin@995
   185
	if (cancel_load) {
patmandin@993
   186
		SDL_SetError("Could not retrieve OpenGL functions");
patmandin@993
   187
		SDL_UnloadObject(handle);
patmandin@993
   188
		/* Restore pointers to static library */
patmandin@993
   189
		SDL_AtariGL_InitPointers(this);
patmandin@993
   190
		return -1;
patmandin@993
   191
	}
patmandin@993
   192
patmandin@991
   193
	/* Load functions pointers (osmesa.ldg) */
patmandin@991
   194
	this->gl_data->OSMesaCreateContextExt = SDL_LoadFunction(handle, "OSMesaCreateContextExt");
patmandin@991
   195
	this->gl_data->OSMesaDestroyContext = SDL_LoadFunction(handle, "OSMesaDestroyContext");
patmandin@991
   196
	this->gl_data->OSMesaMakeCurrent = SDL_LoadFunction(handle, "OSMesaMakeCurrent");
patmandin@991
   197
	this->gl_data->OSMesaPixelStore = SDL_LoadFunction(handle, "OSMesaPixelStore");
patmandin@991
   198
	this->gl_data->OSMesaGetProcAddress = SDL_LoadFunction(handle, "OSMesaGetProcAddress");
patmandin@991
   199
patmandin@991
   200
	/* Load old functions pointers (mesa_gl.ldg, tiny_gl.ldg) */
patmandin@991
   201
	this->gl_data->OSMesaCreateLDG = SDL_LoadFunction(handle, "OSMesaCreateLDG");
patmandin@991
   202
	this->gl_data->OSMesaDestroyLDG = SDL_LoadFunction(handle, "OSMesaDestroyLDG");
patmandin@991
   203
patmandin@991
   204
	gl_oldmesa = 0;
patmandin@991
   205
patmandin@991
   206
	if ( (this->gl_data->OSMesaCreateContextExt == NULL) || 
patmandin@991
   207
	     (this->gl_data->OSMesaDestroyContext == NULL) ||
patmandin@991
   208
	     (this->gl_data->OSMesaMakeCurrent == NULL) ||
patmandin@991
   209
	     (this->gl_data->OSMesaPixelStore == NULL) ||
patmandin@991
   210
	     (this->gl_data->OSMesaGetProcAddress == NULL)) {
patmandin@991
   211
		/* Hum, maybe old library ? */
patmandin@991
   212
		if ( (this->gl_data->OSMesaCreateLDG == NULL) || 
patmandin@991
   213
		     (this->gl_data->OSMesaDestroyLDG == NULL)) {
patmandin@993
   214
			SDL_SetError("Could not retrieve OSMesa functions");
patmandin@993
   215
			SDL_UnloadObject(handle);
patmandin@993
   216
			/* Restore pointers to static library */
patmandin@993
   217
			SDL_AtariGL_InitPointers(this);
patmandin@991
   218
			return -1;
patmandin@991
   219
		} else {
patmandin@991
   220
			gl_oldmesa = 1;
patmandin@991
   221
		}
patmandin@991
   222
	}
patmandin@991
   223
patmandin@991
   224
	this->gl_config.dll_handle = handle;
patmandin@991
   225
	if ( path ) {
patmandin@991
   226
		strncpy(this->gl_config.driver_path, path,
patmandin@991
   227
			sizeof(this->gl_config.driver_path)-1);
patmandin@991
   228
	} else {
patmandin@991
   229
		strcpy(this->gl_config.driver_path, "");
patmandin@991
   230
	}
patmandin@991
   231
patmandin@991
   232
#endif
patmandin@989
   233
	this->gl_config.driver_loaded = 1;
patmandin@991
   234
patmandin@991
   235
	return 0;
patmandin@991
   236
#else
patmandin@991
   237
	return -1;
patmandin@989
   238
#endif
patmandin@989
   239
}
patmandin@989
   240
patmandin@989
   241
void *SDL_AtariGL_GetProcAddress(_THIS, const char *proc)
patmandin@989
   242
{
patmandin@989
   243
	void *func = NULL;
patmandin@989
   244
#ifdef HAVE_OPENGL
patmandin@991
   245
patmandin@991
   246
	if (this->gl_config.dll_handle) {
patmandin@991
   247
		func = SDL_LoadFunction(this->gl_config.dll_handle, (void *)proc);
patmandin@991
   248
	} else if (this->gl_data->OSMesaGetProcAddress) {
patmandin@991
   249
		func = this->gl_data->OSMesaGetProcAddress(proc);
patmandin@989
   250
	}
patmandin@991
   251
patmandin@989
   252
#endif
patmandin@989
   253
	return func;
patmandin@989
   254
}
patmandin@989
   255
patmandin@989
   256
int SDL_AtariGL_GetAttribute(_THIS, SDL_GLattr attrib, int* value)
patmandin@989
   257
{
patmandin@989
   258
#ifdef HAVE_OPENGL
patmandin@989
   259
	GLenum mesa_attrib;
patmandin@989
   260
	SDL_Surface *surface;
patmandin@989
   261
patmandin@991
   262
	if (!gl_active) {
patmandin@989
   263
		return -1;
patmandin@989
   264
	}
patmandin@989
   265
patmandin@989
   266
	switch(attrib) {
patmandin@989
   267
		case SDL_GL_RED_SIZE:
patmandin@989
   268
			mesa_attrib = GL_RED_BITS;
patmandin@989
   269
			break;
patmandin@989
   270
		case SDL_GL_GREEN_SIZE:
patmandin@989
   271
			mesa_attrib = GL_GREEN_BITS;
patmandin@989
   272
			break;
patmandin@989
   273
		case SDL_GL_BLUE_SIZE:
patmandin@989
   274
			mesa_attrib = GL_BLUE_BITS;
patmandin@989
   275
			break;
patmandin@989
   276
		case SDL_GL_ALPHA_SIZE:
patmandin@989
   277
			mesa_attrib = GL_ALPHA_BITS;
patmandin@989
   278
			break;
patmandin@989
   279
		case SDL_GL_DOUBLEBUFFER:
patmandin@989
   280
			surface = this->screen;
patmandin@989
   281
			*value = ((surface->flags & SDL_DOUBLEBUF)==SDL_DOUBLEBUF);
patmandin@989
   282
			return 0;
patmandin@989
   283
		case SDL_GL_DEPTH_SIZE:
patmandin@989
   284
			mesa_attrib = GL_DEPTH_BITS;
patmandin@989
   285
			break;
patmandin@989
   286
		case SDL_GL_STENCIL_SIZE:
patmandin@989
   287
			mesa_attrib = GL_STENCIL_BITS;
patmandin@989
   288
			break;
patmandin@989
   289
		case SDL_GL_ACCUM_RED_SIZE:
patmandin@989
   290
			mesa_attrib = GL_ACCUM_RED_BITS;
patmandin@989
   291
			break;
patmandin@989
   292
		case SDL_GL_ACCUM_GREEN_SIZE:
patmandin@989
   293
			mesa_attrib = GL_ACCUM_GREEN_BITS;
patmandin@989
   294
			break;
patmandin@989
   295
		case SDL_GL_ACCUM_BLUE_SIZE:
patmandin@989
   296
			mesa_attrib = GL_ACCUM_BLUE_BITS;
patmandin@989
   297
			break;
patmandin@989
   298
		case SDL_GL_ACCUM_ALPHA_SIZE:
patmandin@989
   299
			mesa_attrib = GL_ACCUM_ALPHA_BITS;
patmandin@989
   300
			break;
patmandin@989
   301
		default :
patmandin@989
   302
			return -1;
patmandin@989
   303
	}
patmandin@989
   304
patmandin@991
   305
	this->gl_data->glGetIntegerv(mesa_attrib, value);
patmandin@989
   306
	return 0;
patmandin@989
   307
#else
patmandin@989
   308
	return -1;
patmandin@989
   309
#endif
patmandin@989
   310
}
patmandin@989
   311
patmandin@989
   312
int SDL_AtariGL_MakeCurrent(_THIS)
patmandin@989
   313
{
patmandin@989
   314
#ifdef HAVE_OPENGL
patmandin@989
   315
	SDL_Surface *surface;
patmandin@989
   316
	GLenum type;
patmandin@989
   317
patmandin@991
   318
	if (gl_oldmesa && gl_active) {
patmandin@991
   319
		return 0;
patmandin@991
   320
	}
patmandin@991
   321
patmandin@991
   322
	if (this->gl_config.dll_handle) {
patmandin@991
   323
		if ((this->gl_data->OSMesaMakeCurrent == NULL) ||
patmandin@991
   324
			(this->gl_data->OSMesaPixelStore == NULL)) {
patmandin@991
   325
			return -1;
patmandin@991
   326
		}
patmandin@991
   327
	}
patmandin@991
   328
patmandin@991
   329
	if (!gl_active) {
patmandin@989
   330
		SDL_SetError("Invalid OpenGL context");
patmandin@989
   331
		return -1;
patmandin@989
   332
	}
patmandin@989
   333
patmandin@989
   334
	surface = this->screen;
patmandin@989
   335
	
patmandin@989
   336
	if ((surface->format->BitsPerPixel == 15) || (surface->format->BitsPerPixel == 16)) {
patmandin@989
   337
		type = GL_UNSIGNED_SHORT_5_6_5;
patmandin@989
   338
	} else {
patmandin@989
   339
		type = GL_UNSIGNED_BYTE;
patmandin@989
   340
	}
patmandin@989
   341
patmandin@991
   342
	if (!(this->gl_data->OSMesaMakeCurrent(gl_ctx, surface->pixels, type, surface->w, surface->h))) {
patmandin@989
   343
		SDL_SetError("Can not make OpenGL context current");
patmandin@989
   344
		return -1;
patmandin@989
   345
	}
patmandin@989
   346
patmandin@989
   347
	/* OSMesa draws upside down */
patmandin@991
   348
	this->gl_data->OSMesaPixelStore(OSMESA_Y_UP, 0);
patmandin@989
   349
patmandin@989
   350
	return 0;
patmandin@989
   351
#else
patmandin@989
   352
	return -1;
patmandin@989
   353
#endif
patmandin@989
   354
}
patmandin@989
   355
patmandin@989
   356
void SDL_AtariGL_SwapBuffers(_THIS)
patmandin@989
   357
{
patmandin@989
   358
#ifdef HAVE_OPENGL
patmandin@991
   359
	if (gl_active) {
patmandin@993
   360
		if (this->gl_config.dll_handle) {
patmandin@993
   361
			if (this->gl_data->glFinish) {
patmandin@993
   362
				this->gl_data->glFinish();
patmandin@995
   363
			} else if (this->gl_data->glFlush) {
patmandin@995
   364
				this->gl_data->glFlush();
patmandin@993
   365
			}
patmandin@993
   366
		} else {
patmandin@993
   367
			this->gl_data->glFinish();
patmandin@993
   368
		}
patmandin@991
   369
		gl_copyshadow(this, this->screen);
patmandin@991
   370
		gl_convert(this, this->screen);
patmandin@989
   371
	}
patmandin@991
   372
#endif
patmandin@991
   373
}
patmandin@989
   374
patmandin@991
   375
void SDL_AtariGL_InitPointers(_THIS)
patmandin@991
   376
{
patmandin@991
   377
#if defined(HAVE_OPENGL)
patmandin@991
   378
	this->gl_data->OSMesaCreateContextExt = OSMesaCreateContextExt;
patmandin@991
   379
	this->gl_data->OSMesaDestroyContext = OSMesaDestroyContext;
patmandin@991
   380
	this->gl_data->OSMesaMakeCurrent = OSMesaMakeCurrent;
patmandin@991
   381
	this->gl_data->OSMesaPixelStore = OSMesaPixelStore;
patmandin@991
   382
	this->gl_data->OSMesaGetProcAddress = OSMesaGetProcAddress;
patmandin@993
   383
patmandin@991
   384
	this->gl_data->glGetIntegerv = glGetIntegerv;
patmandin@993
   385
	this->gl_data->glFinish = glFinish;
patmandin@995
   386
	this->gl_data->glFlush = glFlush;
patmandin@993
   387
patmandin@993
   388
	this->gl_data->OSMesaCreateLDG = NULL;
patmandin@993
   389
	this->gl_data->OSMesaDestroyLDG = NULL;
patmandin@989
   390
#endif
patmandin@989
   391
}
patmandin@989
   392
patmandin@989
   393
/*--- Private functions ---*/
patmandin@989
   394
patmandin@991
   395
static void SDL_AtariGL_UnloadLibrary(_THIS)
patmandin@991
   396
{
patmandin@991
   397
#if defined(HAVE_OPENGL)
patmandin@991
   398
	if (this->gl_config.dll_handle) {
patmandin@991
   399
		SDL_UnloadObject(this->gl_config.dll_handle);
patmandin@991
   400
		this->gl_config.dll_handle = NULL;
patmandin@991
   401
patmandin@991
   402
		/* Restore pointers to static library */
patmandin@993
   403
		SDL_AtariGL_InitPointers(this);
patmandin@991
   404
	}
patmandin@991
   405
#endif
patmandin@991
   406
}
patmandin@991
   407
patmandin@991
   408
/*--- Creation of an OpenGL context using new/old functions ---*/
patmandin@991
   409
patmandin@1000
   410
#ifdef HAVE_OPENGL
patmandin@991
   411
static int InitNew(_THIS, SDL_Surface *current)
patmandin@991
   412
{
patmandin@991
   413
	GLenum osmesa_format;
patmandin@991
   414
	SDL_PixelFormat *pixel_format;
patmandin@991
   415
	Uint32	redmask;
patmandin@992
   416
	int recreatecontext;
patmandin@992
   417
	GLint newaccumsize;
patmandin@991
   418
patmandin@991
   419
	if (this->gl_config.dll_handle) {
patmandin@991
   420
		if (this->gl_data->OSMesaCreateContextExt == NULL) {
patmandin@991
   421
			return 0;
patmandin@991
   422
		}
patmandin@991
   423
	}
patmandin@991
   424
patmandin@991
   425
	/* Init OpenGL context using OSMesa */
patmandin@991
   426
	gl_convert = ConvertNull;
patmandin@991
   427
	gl_copyshadow = CopyShadowNull;
patmandin@991
   428
patmandin@991
   429
	pixel_format = current->format;
patmandin@991
   430
	redmask = pixel_format->Rmask;
patmandin@991
   431
	switch (pixel_format->BitsPerPixel) {
patmandin@991
   432
		case 15:
patmandin@991
   433
			/* 1555, big and little endian, unsupported */
patmandin@991
   434
			gl_pixelsize = 2;
patmandin@991
   435
			osmesa_format = OSMESA_RGB_565;
patmandin@991
   436
			if (redmask == 31<<10) {
patmandin@991
   437
				gl_convert = Convert565To555be;
patmandin@991
   438
			} else {
patmandin@991
   439
				gl_convert = Convert565To555le;
patmandin@991
   440
			}
patmandin@991
   441
			break;
patmandin@991
   442
		case 16:
patmandin@991
   443
			gl_pixelsize = 2;
patmandin@991
   444
			if (redmask == 31<<11) {
patmandin@991
   445
				osmesa_format = OSMESA_RGB_565;
patmandin@991
   446
			} else {
patmandin@991
   447
				/* 565, little endian, unsupported */
patmandin@991
   448
				osmesa_format = OSMESA_RGB_565;
patmandin@991
   449
				gl_convert = Convert565le;
patmandin@991
   450
			}
patmandin@991
   451
			break;
patmandin@991
   452
		case 24:
patmandin@991
   453
			gl_pixelsize = 3;
patmandin@991
   454
			if (redmask == 255<<16) {
patmandin@991
   455
				osmesa_format = OSMESA_RGB;
patmandin@991
   456
			} else {
patmandin@991
   457
				osmesa_format = OSMESA_BGR;
patmandin@991
   458
			}
patmandin@991
   459
			break;
patmandin@991
   460
		case 32:
patmandin@991
   461
			gl_pixelsize = 4;
patmandin@991
   462
			if (redmask == 255<<16) {
patmandin@991
   463
				osmesa_format = OSMESA_ARGB;
patmandin@991
   464
			} else if (redmask == 255<<8) {
patmandin@991
   465
				osmesa_format = OSMESA_BGRA;
patmandin@991
   466
			} else if (redmask == 255<<24) {
patmandin@991
   467
				osmesa_format = OSMESA_RGBA;
patmandin@991
   468
			} else {
patmandin@991
   469
				/* ABGR format unsupported */
patmandin@991
   470
				osmesa_format = OSMESA_BGRA;
patmandin@991
   471
				gl_convert = ConvertBGRAToABGR;
patmandin@991
   472
			}
patmandin@991
   473
			break;
patmandin@991
   474
		default:
patmandin@991
   475
			gl_pixelsize = 1;
patmandin@991
   476
			osmesa_format = OSMESA_COLOR_INDEX;
patmandin@991
   477
			break;
patmandin@991
   478
	}
patmandin@991
   479
patmandin@992
   480
	/* Try to keep current context if possible */
patmandin@992
   481
	newaccumsize =
patmandin@992
   482
		this->gl_config.accum_red_size +
patmandin@992
   483
		this->gl_config.accum_green_size +
patmandin@992
   484
		this->gl_config.accum_blue_size +
patmandin@992
   485
		this->gl_config.accum_alpha_size;
patmandin@992
   486
	recreatecontext=1;
patmandin@992
   487
	if (gl_ctx &&
patmandin@992
   488
		(gl_curformat == osmesa_format) &&
patmandin@992
   489
		(gl_curdepth == this->gl_config.depth_size) &&
patmandin@992
   490
		(gl_curstencil == this->gl_config.stencil_size) &&
patmandin@992
   491
		(gl_curaccum == newaccumsize)) {
patmandin@992
   492
		recreatecontext = 0;
patmandin@992
   493
	}
patmandin@992
   494
	if (recreatecontext) {
patmandin@992
   495
		SDL_AtariGL_Quit(this, SDL_FALSE);
patmandin@992
   496
patmandin@992
   497
		gl_ctx = this->gl_data->OSMesaCreateContextExt(
patmandin@992
   498
			osmesa_format, this->gl_config.depth_size,
patmandin@992
   499
			this->gl_config.stencil_size, newaccumsize, NULL );
patmandin@992
   500
patmandin@992
   501
		if (gl_ctx) {
patmandin@992
   502
			gl_curformat = osmesa_format;
patmandin@992
   503
			gl_curdepth = this->gl_config.depth_size;
patmandin@992
   504
			gl_curstencil = this->gl_config.stencil_size;
patmandin@992
   505
			gl_curaccum = newaccumsize;
patmandin@992
   506
		} else {
patmandin@992
   507
			gl_curformat = 0;
patmandin@992
   508
			gl_curdepth = 0;
patmandin@992
   509
			gl_curstencil = 0;
patmandin@992
   510
			gl_curaccum = 0;
patmandin@992
   511
		}
patmandin@992
   512
	}
patmandin@991
   513
patmandin@991
   514
	return (gl_ctx != NULL);
patmandin@991
   515
}
patmandin@991
   516
patmandin@1000
   517
patmandin@991
   518
static int InitOld(_THIS, SDL_Surface *current)
patmandin@991
   519
{
patmandin@991
   520
	GLenum osmesa_format;
patmandin@991
   521
	SDL_PixelFormat *pixel_format;
patmandin@991
   522
	Uint32	redmask;
patmandin@1000
   523
	int recreatecontext, tinygl_present;
patmandin@991
   524
patmandin@991
   525
	if (this->gl_config.dll_handle) {
patmandin@991
   526
		if (this->gl_data->OSMesaCreateLDG == NULL) {
patmandin@991
   527
			return 0;
patmandin@991
   528
		}
patmandin@991
   529
	}
patmandin@991
   530
patmandin@1000
   531
	/* TinyGL only supports VDI_RGB (OSMESA_RGB) */
patmandin@1000
   532
	tinygl_present=0;
patmandin@1000
   533
	if (this->gl_config.dll_handle) {
patmandin@1000
   534
		if (this->gl_data->glFinish == NULL) {
patmandin@1000
   535
			tinygl_present=1;
patmandin@1000
   536
		}
patmandin@1000
   537
	}
patmandin@1000
   538
patmandin@991
   539
	/* Init OpenGL context using OSMesa */
patmandin@991
   540
	gl_convert = ConvertNull;
patmandin@991
   541
	gl_copyshadow = CopyShadowNull;
patmandin@991
   542
patmandin@991
   543
	pixel_format = current->format;
patmandin@991
   544
	redmask = pixel_format->Rmask;
patmandin@991
   545
	switch (pixel_format->BitsPerPixel) {
patmandin@991
   546
		case 15:
patmandin@991
   547
			/* 15 bits unsupported */
patmandin@1000
   548
			if (tinygl_present) {
patmandin@1002
   549
				gl_pixelsize = 3;
patmandin@1000
   550
				osmesa_format = VDI_RGB;
patmandin@1000
   551
				if (redmask == 31<<10) {
patmandin@1000
   552
					gl_copyshadow = CopyShadowRGBTo555;
patmandin@1000
   553
				} else {
patmandin@1000
   554
					gl_copyshadow = CopyShadowRGBTo565;
patmandin@1000
   555
					gl_convert = Convert565To555le;
patmandin@1000
   556
				}
patmandin@991
   557
			} else {
patmandin@1002
   558
				gl_pixelsize = 4;
patmandin@1000
   559
				osmesa_format = OSMESA_ARGB;
patmandin@1000
   560
				if (redmask == 31<<10) {
patmandin@1000
   561
					gl_copyshadow = CopyShadow8888To555;
patmandin@1000
   562
				} else {
patmandin@1000
   563
					gl_copyshadow = CopyShadow8888To565;
patmandin@1000
   564
					gl_convert = Convert565To555le;
patmandin@1000
   565
				}
patmandin@991
   566
			}
patmandin@991
   567
			break;
patmandin@991
   568
		case 16:
patmandin@991
   569
			/* 16 bits unsupported */
patmandin@1000
   570
			if (tinygl_present) {
patmandin@1002
   571
				gl_pixelsize = 3;
patmandin@1000
   572
				osmesa_format = VDI_RGB;
patmandin@1000
   573
				gl_copyshadow = CopyShadowRGBTo565;
patmandin@1000
   574
				if (redmask != 31<<11) {
patmandin@1000
   575
					/* 565, little endian, unsupported */
patmandin@1000
   576
					gl_convert = Convert565le;
patmandin@1000
   577
				}
patmandin@1000
   578
			} else {
patmandin@1002
   579
				gl_pixelsize = 4;
patmandin@1000
   580
				osmesa_format = OSMESA_ARGB;
patmandin@1000
   581
				gl_copyshadow = CopyShadow8888To565;
patmandin@1000
   582
				if (redmask != 31<<11) {
patmandin@1000
   583
					/* 565, little endian, unsupported */
patmandin@1000
   584
					gl_convert = Convert565le;
patmandin@1000
   585
				}
patmandin@991
   586
			}
patmandin@991
   587
			break;
patmandin@991
   588
		case 24:
patmandin@991
   589
			gl_pixelsize = 3;
patmandin@1000
   590
			if (tinygl_present) {
patmandin@1000
   591
				osmesa_format = VDI_RGB;
patmandin@1000
   592
				gl_copyshadow = CopyShadowDirect;
patmandin@1000
   593
				if (redmask != 255<<16) {
patmandin@1000
   594
					gl_copyshadow = CopyShadowRGBSwap;
patmandin@1000
   595
				}
patmandin@991
   596
			} else {
patmandin@1000
   597
				gl_copyshadow = CopyShadowDirect;
patmandin@1000
   598
				if (redmask == 255<<16) {
patmandin@1000
   599
					osmesa_format = OSMESA_RGB;
patmandin@1000
   600
				} else {
patmandin@1000
   601
					osmesa_format = OSMESA_BGR;
patmandin@1000
   602
				}
patmandin@991
   603
			}
patmandin@991
   604
			break;
patmandin@991
   605
		case 32:
patmandin@1000
   606
			if (tinygl_present) {
patmandin@1002
   607
				gl_pixelsize = 3;
patmandin@1000
   608
				osmesa_format = VDI_RGB;
patmandin@1000
   609
				gl_copyshadow = CopyShadowRGBToARGB;
patmandin@1000
   610
				if (redmask == 255) {
patmandin@1000
   611
					gl_convert = CopyShadowRGBToABGR;
patmandin@1000
   612
				} else if (redmask == 255<<8) {
patmandin@1000
   613
					gl_convert = CopyShadowRGBToBGRA;
patmandin@1000
   614
				} else if (redmask == 255<<24) {
patmandin@1000
   615
					gl_convert = CopyShadowRGBToRGBA;
patmandin@1000
   616
				}
patmandin@991
   617
			} else {
patmandin@1002
   618
				gl_pixelsize = 4;
patmandin@1000
   619
				gl_copyshadow = CopyShadowDirect;
patmandin@1000
   620
				if (redmask == 255<<16) {
patmandin@1000
   621
					osmesa_format = OSMESA_ARGB;
patmandin@1000
   622
				} else if (redmask == 255<<8) {
patmandin@1000
   623
					osmesa_format = OSMESA_BGRA;
patmandin@1000
   624
				} else if (redmask == 255<<24) {
patmandin@1000
   625
					osmesa_format = OSMESA_RGBA;
patmandin@1000
   626
				} else {
patmandin@1000
   627
					/* ABGR format unsupported */
patmandin@1000
   628
					osmesa_format = OSMESA_BGRA;
patmandin@1000
   629
					gl_convert = ConvertBGRAToABGR;
patmandin@1000
   630
				}
patmandin@991
   631
			}
patmandin@991
   632
			break;
patmandin@991
   633
		default:
patmandin@1000
   634
			if (tinygl_present) {
patmandin@1000
   635
				SDL_AtariGL_Quit(this, SDL_FALSE);
patmandin@1000
   636
				return 0;
patmandin@1000
   637
			}
patmandin@991
   638
			gl_pixelsize = 1;
patmandin@991
   639
			gl_copyshadow = CopyShadowDirect;
patmandin@991
   640
			osmesa_format = OSMESA_COLOR_INDEX;
patmandin@991
   641
			break;
patmandin@991
   642
	}
patmandin@991
   643
patmandin@992
   644
	/* Try to keep current context if possible */
patmandin@992
   645
	recreatecontext=1;
patmandin@992
   646
	if (gl_shadow &&
patmandin@992
   647
		(gl_curformat == osmesa_format) &&
patmandin@992
   648
		(gl_curwidth == current->w) &&
patmandin@992
   649
		(gl_curheight == current->h)) {
patmandin@992
   650
		recreatecontext = 0;
patmandin@992
   651
	}
patmandin@992
   652
	if (recreatecontext) {
patmandin@992
   653
		SDL_AtariGL_Quit(this, SDL_FALSE);
patmandin@992
   654
patmandin@992
   655
		gl_shadow = this->gl_data->OSMesaCreateLDG(
patmandin@992
   656
			osmesa_format, GL_UNSIGNED_BYTE, current->w, current->h
patmandin@992
   657
		);
patmandin@992
   658
patmandin@992
   659
		if (gl_shadow) {
patmandin@992
   660
			gl_curformat = osmesa_format;
patmandin@992
   661
			gl_curwidth = current->w;
patmandin@992
   662
			gl_curheight = current->h;
patmandin@992
   663
		} else {
patmandin@992
   664
			gl_curformat = 0;
patmandin@992
   665
			gl_curwidth = 0;
patmandin@992
   666
			gl_curheight = 0;
patmandin@992
   667
		}
patmandin@992
   668
	}
patmandin@991
   669
patmandin@991
   670
	return (gl_shadow != NULL);
patmandin@991
   671
}
patmandin@991
   672
patmandin@991
   673
/*--- Conversions routines from shadow buffer to the screen ---*/
patmandin@991
   674
patmandin@991
   675
static void CopyShadowNull(_THIS, SDL_Surface *surface)
patmandin@989
   676
{
patmandin@989
   677
}
patmandin@989
   678
patmandin@991
   679
static void CopyShadowDirect(_THIS, SDL_Surface *surface)
patmandin@991
   680
{
patmandin@991
   681
	int y, srcpitch, dstpitch;
patmandin@991
   682
	Uint8 *srcline, *dstline;
patmandin@991
   683
patmandin@991
   684
	srcline = gl_shadow;
patmandin@991
   685
	srcpitch = surface->w * gl_pixelsize;
patmandin@991
   686
	dstline = surface->pixels;
patmandin@991
   687
	dstpitch = surface->pitch;
patmandin@991
   688
patmandin@991
   689
	for (y=0; y<surface->h; y++) {
patmandin@991
   690
		memcpy(dstline, srcline, srcpitch);
patmandin@991
   691
patmandin@991
   692
		srcline += srcpitch;
patmandin@991
   693
		dstline += dstpitch;
patmandin@991
   694
	}
patmandin@991
   695
}
patmandin@991
   696
patmandin@1000
   697
static void CopyShadowRGBTo555(_THIS, SDL_Surface *surface)
patmandin@1000
   698
{
patmandin@1000
   699
	int x,y, srcpitch, dstpitch;
patmandin@1000
   700
	Uint16 *dstline, *dstcol;
patmandin@1000
   701
	Uint8 *srcline, *srccol;
patmandin@1000
   702
patmandin@1000
   703
	srcline = (Uint8 *)gl_shadow;
patmandin@1002
   704
	srcpitch = surface->w * gl_pixelsize;
patmandin@1000
   705
	dstline = surface->pixels;
patmandin@1000
   706
	dstpitch = surface->pitch >>1;
patmandin@1000
   707
patmandin@1000
   708
	for (y=0; y<surface->h; y++) {
patmandin@1000
   709
		srccol = srcline;
patmandin@1000
   710
		dstcol = dstline;
patmandin@1000
   711
		for (x=0; x<surface->w; x++) {
patmandin@1000
   712
			Uint16 dstcolor;
patmandin@1000
   713
			
patmandin@1003
   714
			dstcolor = ((*srccol++)<<7) & (31<<10);
patmandin@1003
   715
			dstcolor |= ((*srccol++)<<2) & (31<<5);
patmandin@1000
   716
			dstcolor |= ((*srccol++)>>3) & 31;
patmandin@1000
   717
			*dstcol++ = dstcolor;
patmandin@1000
   718
		}
patmandin@1000
   719
patmandin@1000
   720
		srcline += srcpitch;
patmandin@1000
   721
		dstline += dstpitch;
patmandin@1000
   722
	}
patmandin@1000
   723
}
patmandin@1000
   724
patmandin@1000
   725
static void CopyShadowRGBTo565(_THIS, SDL_Surface *surface)
patmandin@1000
   726
{
patmandin@1000
   727
	int x,y, srcpitch, dstpitch;
patmandin@1000
   728
	Uint16 *dstline, *dstcol;
patmandin@1000
   729
	Uint8 *srcline, *srccol;
patmandin@1000
   730
patmandin@1000
   731
	srcline = (Uint8 *)gl_shadow;
patmandin@1002
   732
	srcpitch = surface->w * gl_pixelsize;
patmandin@1000
   733
	dstline = surface->pixels;
patmandin@1000
   734
	dstpitch = surface->pitch >>1;
patmandin@1000
   735
patmandin@1000
   736
	for (y=0; y<surface->h; y++) {
patmandin@1000
   737
		srccol = srcline;
patmandin@1000
   738
		dstcol = dstline;
patmandin@1000
   739
patmandin@1000
   740
		for (x=0; x<surface->w; x++) {
patmandin@1000
   741
			Uint16 dstcolor;
patmandin@1000
   742
			
patmandin@1003
   743
			dstcolor = ((*srccol++)<<8) & (31<<11);
patmandin@1003
   744
			dstcolor |= ((*srccol++)<<3) & (63<<5);
patmandin@1000
   745
			dstcolor |= ((*srccol++)>>3) & 31;
patmandin@1000
   746
			*dstcol++ = dstcolor;
patmandin@1000
   747
		}
patmandin@1000
   748
patmandin@1000
   749
		srcline += srcpitch;
patmandin@1000
   750
		dstline += dstpitch;
patmandin@1000
   751
	}
patmandin@1000
   752
}
patmandin@1000
   753
patmandin@1000
   754
static void CopyShadowRGBSwap(_THIS, SDL_Surface *surface)
patmandin@1000
   755
{
patmandin@1000
   756
	int x,y, srcpitch, dstpitch;
patmandin@1000
   757
	Uint8 *dstline, *dstcol;
patmandin@1000
   758
	Uint8 *srcline, *srccol;
patmandin@1000
   759
patmandin@1000
   760
	srcline = (Uint8 *)gl_shadow;
patmandin@1002
   761
	srcpitch = surface->w * gl_pixelsize;
patmandin@1000
   762
	dstline = surface->pixels;
patmandin@1000
   763
	dstpitch = surface->pitch;
patmandin@1000
   764
patmandin@1000
   765
	for (y=0; y<surface->h; y++) {
patmandin@1000
   766
		srccol = srcline;
patmandin@1000
   767
		dstcol = dstline;
patmandin@1000
   768
patmandin@1000
   769
		for (x=0; x<surface->w; x++) {
patmandin@1000
   770
			*dstcol++ = srccol[2];
patmandin@1000
   771
			*dstcol++ = srccol[1];
patmandin@1000
   772
			*dstcol++ = srccol[0];
patmandin@1000
   773
			srccol += 3;
patmandin@1000
   774
		}
patmandin@1000
   775
patmandin@1000
   776
		srcline += srcpitch;
patmandin@1000
   777
		dstline += dstpitch;
patmandin@1000
   778
	}
patmandin@1000
   779
}
patmandin@1000
   780
patmandin@1000
   781
static void CopyShadowRGBToARGB(_THIS, SDL_Surface *surface)
patmandin@1000
   782
{
patmandin@1000
   783
	int x,y, srcpitch, dstpitch;
patmandin@1000
   784
	Uint32 *dstline, *dstcol;
patmandin@1000
   785
	Uint8 *srcline, *srccol;
patmandin@1000
   786
patmandin@1000
   787
	srcline = (Uint8 *)gl_shadow;
patmandin@1002
   788
	srcpitch = surface->w * gl_pixelsize;
patmandin@1000
   789
	dstline = surface->pixels;
patmandin@1000
   790
	dstpitch = surface->pitch >>2;
patmandin@1000
   791
patmandin@1000
   792
	for (y=0; y<surface->h; y++) {
patmandin@1000
   793
		srccol = srcline;
patmandin@1000
   794
		dstcol = dstline;
patmandin@1000
   795
patmandin@1000
   796
		for (x=0; x<surface->w; x++) {
patmandin@1000
   797
			Uint32	dstcolor;
patmandin@1000
   798
patmandin@1000
   799
			dstcolor = (*srccol++)<<16;
patmandin@1000
   800
			dstcolor |= (*srccol++)<<8;
patmandin@1000
   801
			dstcolor |= *srccol++;
patmandin@1000
   802
patmandin@1000
   803
			*dstcol++ = dstcolor;
patmandin@1000
   804
		}
patmandin@1000
   805
patmandin@1000
   806
		srcline += srcpitch;
patmandin@1000
   807
		dstline += dstpitch;
patmandin@1000
   808
	}
patmandin@1000
   809
}
patmandin@1000
   810
patmandin@1000
   811
static void CopyShadowRGBToABGR(_THIS, SDL_Surface *surface)
patmandin@1000
   812
{
patmandin@1000
   813
	int x,y, srcpitch, dstpitch;
patmandin@1000
   814
	Uint32 *dstline, *dstcol;
patmandin@1000
   815
	Uint8 *srcline, *srccol;
patmandin@1000
   816
patmandin@1000
   817
	srcline = (Uint8 *)gl_shadow;
patmandin@1002
   818
	srcpitch = surface->w * gl_pixelsize;
patmandin@1000
   819
	dstline = surface->pixels;
patmandin@1000
   820
	dstpitch = surface->pitch >>2;
patmandin@1000
   821
patmandin@1000
   822
	for (y=0; y<surface->h; y++) {
patmandin@1000
   823
		srccol = srcline;
patmandin@1000
   824
		dstcol = dstline;
patmandin@1000
   825
patmandin@1000
   826
		for (x=0; x<surface->w; x++) {
patmandin@1000
   827
			Uint32	dstcolor;
patmandin@1000
   828
patmandin@1000
   829
			dstcolor = *srccol++;
patmandin@1000
   830
			dstcolor |= (*srccol++)<<8;
patmandin@1000
   831
			dstcolor |= (*srccol++)<<16;
patmandin@1000
   832
patmandin@1000
   833
			*dstcol++ = dstcolor;
patmandin@1000
   834
		}
patmandin@1000
   835
patmandin@1000
   836
		srcline += srcpitch;
patmandin@1000
   837
		dstline += dstpitch;
patmandin@1000
   838
	}
patmandin@1000
   839
}
patmandin@1000
   840
patmandin@1000
   841
static void CopyShadowRGBToBGRA(_THIS, SDL_Surface *surface)
patmandin@1000
   842
{
patmandin@1000
   843
	int x,y, srcpitch, dstpitch;
patmandin@1000
   844
	Uint32 *dstline, *dstcol;
patmandin@1000
   845
	Uint8 *srcline, *srccol;
patmandin@1000
   846
patmandin@1000
   847
	srcline = (Uint8 *)gl_shadow;
patmandin@1002
   848
	srcpitch = surface->w * gl_pixelsize;
patmandin@1000
   849
	dstline = surface->pixels;
patmandin@1000
   850
	dstpitch = surface->pitch >>2;
patmandin@1000
   851
patmandin@1000
   852
	for (y=0; y<surface->h; y++) {
patmandin@1000
   853
		srccol = srcline;
patmandin@1000
   854
		dstcol = dstline;
patmandin@1000
   855
patmandin@1000
   856
		for (x=0; x<surface->w; x++) {
patmandin@1000
   857
			Uint32	dstcolor;
patmandin@1000
   858
patmandin@1000
   859
			dstcolor = (*srccol++)<<8;
patmandin@1000
   860
			dstcolor |= (*srccol++)<<16;
patmandin@1000
   861
			dstcolor |= (*srccol++)<<24;
patmandin@1000
   862
patmandin@1000
   863
			*dstcol++ = dstcolor;
patmandin@1000
   864
		}
patmandin@1000
   865
patmandin@1000
   866
		srcline += srcpitch;
patmandin@1000
   867
		dstline += dstpitch;
patmandin@1000
   868
	}
patmandin@1000
   869
}
patmandin@1000
   870
patmandin@1000
   871
static void CopyShadowRGBToRGBA(_THIS, SDL_Surface *surface)
patmandin@1000
   872
{
patmandin@1000
   873
	int x,y, srcpitch, dstpitch;
patmandin@1000
   874
	Uint32 *dstline, *dstcol;
patmandin@1000
   875
	Uint8 *srcline, *srccol;
patmandin@1000
   876
patmandin@1000
   877
	srcline = (Uint8 *)gl_shadow;
patmandin@1002
   878
	srcpitch = surface->w * gl_pixelsize;
patmandin@1000
   879
	dstline = surface->pixels;
patmandin@1000
   880
	dstpitch = surface->pitch >>2;
patmandin@1000
   881
patmandin@1000
   882
	for (y=0; y<surface->h; y++) {
patmandin@1000
   883
		srccol = srcline;
patmandin@1000
   884
		dstcol = dstline;
patmandin@1000
   885
patmandin@1000
   886
		for (x=0; x<surface->w; x++) {
patmandin@1000
   887
			Uint32	dstcolor;
patmandin@1000
   888
patmandin@1000
   889
			dstcolor = (*srccol++)<<24;
patmandin@1000
   890
			dstcolor |= (*srccol++)<<16;
patmandin@1000
   891
			dstcolor |= (*srccol++)<<8;
patmandin@1000
   892
patmandin@1000
   893
			*dstcol++ = dstcolor;
patmandin@1000
   894
		}
patmandin@1000
   895
patmandin@1000
   896
		srcline += srcpitch;
patmandin@1000
   897
		dstline += dstpitch;
patmandin@1000
   898
	}
patmandin@1000
   899
}
patmandin@1000
   900
patmandin@991
   901
static void CopyShadow8888To555(_THIS, SDL_Surface *surface)
patmandin@991
   902
{
patmandin@991
   903
	int x,y, srcpitch, dstpitch;
patmandin@991
   904
	Uint16 *dstline, *dstcol;
patmandin@991
   905
	Uint32 *srcline, *srccol;
patmandin@991
   906
patmandin@991
   907
	srcline = (Uint32 *)gl_shadow;
patmandin@1002
   908
	srcpitch = (surface->w * gl_pixelsize) >>2;
patmandin@991
   909
	dstline = surface->pixels;
patmandin@991
   910
	dstpitch = surface->pitch >>1;
patmandin@991
   911
patmandin@991
   912
	for (y=0; y<surface->h; y++) {
patmandin@991
   913
		srccol = srcline;
patmandin@991
   914
		dstcol = dstline;
patmandin@991
   915
		for (x=0; x<surface->w; x++) {
patmandin@991
   916
			Uint32 srccolor;
patmandin@991
   917
			Uint16 dstcolor;
patmandin@991
   918
			
patmandin@991
   919
			srccolor = *srccol++;
patmandin@991
   920
			dstcolor = (srccolor>>9) & (31<<10);
patmandin@991
   921
			dstcolor |= (srccolor>>6) & (31<<5);
patmandin@991
   922
			dstcolor |= (srccolor>>3) & 31;
patmandin@991
   923
			*dstcol++ = dstcolor;
patmandin@991
   924
		}
patmandin@991
   925
patmandin@991
   926
		srcline += srcpitch;
patmandin@991
   927
		dstline += dstpitch;
patmandin@991
   928
	}
patmandin@991
   929
}
patmandin@991
   930
patmandin@991
   931
static void CopyShadow8888To565(_THIS, SDL_Surface *surface)
patmandin@991
   932
{
patmandin@991
   933
	int x,y, srcpitch, dstpitch;
patmandin@991
   934
	Uint16 *dstline, *dstcol;
patmandin@991
   935
	Uint32 *srcline, *srccol;
patmandin@991
   936
patmandin@991
   937
	srcline = (Uint32 *)gl_shadow;
patmandin@1002
   938
	srcpitch = (surface->w * gl_pixelsize) >> 2;
patmandin@991
   939
	dstline = surface->pixels;
patmandin@991
   940
	dstpitch = surface->pitch >>1;
patmandin@991
   941
patmandin@991
   942
	for (y=0; y<surface->h; y++) {
patmandin@991
   943
		srccol = srcline;
patmandin@991
   944
		dstcol = dstline;
patmandin@991
   945
patmandin@991
   946
		for (x=0; x<surface->w; x++) {
patmandin@991
   947
			Uint32 srccolor;
patmandin@991
   948
			Uint16 dstcolor;
patmandin@991
   949
			
patmandin@991
   950
			srccolor = *srccol++;
patmandin@991
   951
			dstcolor = (srccolor>>8) & (31<<11);
patmandin@991
   952
			dstcolor |= (srccolor>>5) & (63<<5);
patmandin@991
   953
			dstcolor |= (srccolor>>3) & 31;
patmandin@991
   954
			*dstcol++ = dstcolor;
patmandin@991
   955
		}
patmandin@991
   956
patmandin@991
   957
		srcline += srcpitch;
patmandin@991
   958
		dstline += dstpitch;
patmandin@991
   959
	}
patmandin@991
   960
}
patmandin@991
   961
patmandin@991
   962
/*--- Conversions routines in the screen ---*/
patmandin@991
   963
patmandin@991
   964
static void ConvertNull(_THIS, SDL_Surface *surface)
patmandin@991
   965
{
patmandin@991
   966
}
patmandin@991
   967
patmandin@991
   968
static void Convert565To555be(_THIS, SDL_Surface *surface)
patmandin@989
   969
{
patmandin@989
   970
	int x,y, pitch;
patmandin@989
   971
	unsigned short *line, *pixel;
patmandin@989
   972
patmandin@989
   973
	line = surface->pixels;
patmandin@989
   974
	pitch = surface->pitch >> 1;
patmandin@989
   975
	for (y=0; y<surface->h; y++) {
patmandin@989
   976
		pixel = line;
patmandin@989
   977
		for (x=0; x<surface->w; x++) {
patmandin@989
   978
			unsigned short color = *pixel;
patmandin@989
   979
patmandin@989
   980
			*pixel++ = (color & 0x1f)|((color>>1) & 0xffe0);
patmandin@989
   981
		}
patmandin@989
   982
patmandin@989
   983
		line += pitch;
patmandin@989
   984
	}
patmandin@989
   985
}
patmandin@989
   986
patmandin@991
   987
static void Convert565To555le(_THIS, SDL_Surface *surface)
patmandin@989
   988
{
patmandin@989
   989
	int x,y, pitch;
patmandin@989
   990
	unsigned short *line, *pixel;
patmandin@989
   991
patmandin@989
   992
	line = surface->pixels;
patmandin@989
   993
	pitch = surface->pitch >>1;
patmandin@989
   994
	for (y=0; y<surface->h; y++) {
patmandin@989
   995
		pixel = line;
patmandin@989
   996
		for (x=0; x<surface->w; x++) {
patmandin@989
   997
			unsigned short color = *pixel;
patmandin@989
   998
patmandin@989
   999
			color = (color & 0x1f)|((color>>1) & 0xffe0);
patmandin@989
  1000
			*pixel++ = SDL_Swap16(color);
patmandin@989
  1001
		}
patmandin@989
  1002
patmandin@989
  1003
		line += pitch;
patmandin@989
  1004
	}
patmandin@989
  1005
}
patmandin@989
  1006
patmandin@991
  1007
static void Convert565le(_THIS, SDL_Surface *surface)
patmandin@989
  1008
{
patmandin@989
  1009
	int x,y, pitch;
patmandin@989
  1010
	unsigned short *line, *pixel;
patmandin@989
  1011
patmandin@989
  1012
	line = surface->pixels;
patmandin@989
  1013
	pitch = surface->pitch >>1;
patmandin@989
  1014
	for (y=0; y<surface->h; y++) {
patmandin@989
  1015
		pixel = line;
patmandin@989
  1016
		for (x=0; x<surface->w; x++) {
patmandin@989
  1017
			unsigned short color = *pixel;
patmandin@989
  1018
patmandin@989
  1019
			*pixel++ = SDL_Swap16(color);
patmandin@989
  1020
		}
patmandin@989
  1021
patmandin@989
  1022
		line += pitch;
patmandin@989
  1023
	}
patmandin@989
  1024
}
patmandin@989
  1025
patmandin@991
  1026
static void ConvertBGRAToABGR(_THIS, SDL_Surface *surface)
patmandin@989
  1027
{
patmandin@989
  1028
	int x,y, pitch;
patmandin@989
  1029
	unsigned long *line, *pixel;
patmandin@989
  1030
patmandin@989
  1031
	line = surface->pixels;
patmandin@989
  1032
	pitch = surface->pitch >>2;
patmandin@989
  1033
	for (y=0; y<surface->h; y++) {
patmandin@989
  1034
		pixel = line;
patmandin@989
  1035
		for (x=0; x<surface->w; x++) {
patmandin@989
  1036
			unsigned long color = *pixel;
patmandin@989
  1037
patmandin@989
  1038
			*pixel++ = (color<<24)|(color>>8);
patmandin@989
  1039
		}
patmandin@989
  1040
patmandin@989
  1041
		line += pitch;
patmandin@989
  1042
	}
patmandin@989
  1043
}
patmandin@1000
  1044
patmandin@1000
  1045
#endif /* HAVE_OPENGL */