src/video/ataricommon/SDL_atarigl.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 06 Jan 2006 13:20:10 +0000
changeset 1234 73676c1f56ee
parent 1005 2ab1cb02a44e
child 1238 1901fea5edac
permissions -rw-r--r--
For sanity's sake, removed the '&' when passing copy_row array to asm.
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@1005
   428
	gl_upsidedown = SDL_FALSE;
patmandin@991
   429
patmandin@991
   430
	pixel_format = current->format;
patmandin@991
   431
	redmask = pixel_format->Rmask;
patmandin@991
   432
	switch (pixel_format->BitsPerPixel) {
patmandin@991
   433
		case 15:
patmandin@991
   434
			/* 1555, big and little endian, unsupported */
patmandin@991
   435
			gl_pixelsize = 2;
patmandin@991
   436
			osmesa_format = OSMESA_RGB_565;
patmandin@991
   437
			if (redmask == 31<<10) {
patmandin@991
   438
				gl_convert = Convert565To555be;
patmandin@991
   439
			} else {
patmandin@991
   440
				gl_convert = Convert565To555le;
patmandin@991
   441
			}
patmandin@991
   442
			break;
patmandin@991
   443
		case 16:
patmandin@991
   444
			gl_pixelsize = 2;
patmandin@991
   445
			if (redmask == 31<<11) {
patmandin@991
   446
				osmesa_format = OSMESA_RGB_565;
patmandin@991
   447
			} else {
patmandin@991
   448
				/* 565, little endian, unsupported */
patmandin@991
   449
				osmesa_format = OSMESA_RGB_565;
patmandin@991
   450
				gl_convert = Convert565le;
patmandin@991
   451
			}
patmandin@991
   452
			break;
patmandin@991
   453
		case 24:
patmandin@991
   454
			gl_pixelsize = 3;
patmandin@991
   455
			if (redmask == 255<<16) {
patmandin@991
   456
				osmesa_format = OSMESA_RGB;
patmandin@991
   457
			} else {
patmandin@991
   458
				osmesa_format = OSMESA_BGR;
patmandin@991
   459
			}
patmandin@991
   460
			break;
patmandin@991
   461
		case 32:
patmandin@991
   462
			gl_pixelsize = 4;
patmandin@991
   463
			if (redmask == 255<<16) {
patmandin@991
   464
				osmesa_format = OSMESA_ARGB;
patmandin@991
   465
			} else if (redmask == 255<<8) {
patmandin@991
   466
				osmesa_format = OSMESA_BGRA;
patmandin@991
   467
			} else if (redmask == 255<<24) {
patmandin@991
   468
				osmesa_format = OSMESA_RGBA;
patmandin@991
   469
			} else {
patmandin@991
   470
				/* ABGR format unsupported */
patmandin@991
   471
				osmesa_format = OSMESA_BGRA;
patmandin@991
   472
				gl_convert = ConvertBGRAToABGR;
patmandin@991
   473
			}
patmandin@991
   474
			break;
patmandin@991
   475
		default:
patmandin@991
   476
			gl_pixelsize = 1;
patmandin@991
   477
			osmesa_format = OSMESA_COLOR_INDEX;
patmandin@991
   478
			break;
patmandin@991
   479
	}
patmandin@991
   480
patmandin@992
   481
	/* Try to keep current context if possible */
patmandin@992
   482
	newaccumsize =
patmandin@992
   483
		this->gl_config.accum_red_size +
patmandin@992
   484
		this->gl_config.accum_green_size +
patmandin@992
   485
		this->gl_config.accum_blue_size +
patmandin@992
   486
		this->gl_config.accum_alpha_size;
patmandin@992
   487
	recreatecontext=1;
patmandin@992
   488
	if (gl_ctx &&
patmandin@992
   489
		(gl_curformat == osmesa_format) &&
patmandin@992
   490
		(gl_curdepth == this->gl_config.depth_size) &&
patmandin@992
   491
		(gl_curstencil == this->gl_config.stencil_size) &&
patmandin@992
   492
		(gl_curaccum == newaccumsize)) {
patmandin@992
   493
		recreatecontext = 0;
patmandin@992
   494
	}
patmandin@992
   495
	if (recreatecontext) {
patmandin@992
   496
		SDL_AtariGL_Quit(this, SDL_FALSE);
patmandin@992
   497
patmandin@992
   498
		gl_ctx = this->gl_data->OSMesaCreateContextExt(
patmandin@992
   499
			osmesa_format, this->gl_config.depth_size,
patmandin@992
   500
			this->gl_config.stencil_size, newaccumsize, NULL );
patmandin@992
   501
patmandin@992
   502
		if (gl_ctx) {
patmandin@992
   503
			gl_curformat = osmesa_format;
patmandin@992
   504
			gl_curdepth = this->gl_config.depth_size;
patmandin@992
   505
			gl_curstencil = this->gl_config.stencil_size;
patmandin@992
   506
			gl_curaccum = newaccumsize;
patmandin@992
   507
		} else {
patmandin@992
   508
			gl_curformat = 0;
patmandin@992
   509
			gl_curdepth = 0;
patmandin@992
   510
			gl_curstencil = 0;
patmandin@992
   511
			gl_curaccum = 0;
patmandin@992
   512
		}
patmandin@992
   513
	}
patmandin@991
   514
patmandin@991
   515
	return (gl_ctx != NULL);
patmandin@991
   516
}
patmandin@991
   517
patmandin@1000
   518
patmandin@991
   519
static int InitOld(_THIS, SDL_Surface *current)
patmandin@991
   520
{
patmandin@991
   521
	GLenum osmesa_format;
patmandin@991
   522
	SDL_PixelFormat *pixel_format;
patmandin@991
   523
	Uint32	redmask;
patmandin@1000
   524
	int recreatecontext, tinygl_present;
patmandin@991
   525
patmandin@991
   526
	if (this->gl_config.dll_handle) {
patmandin@991
   527
		if (this->gl_data->OSMesaCreateLDG == NULL) {
patmandin@991
   528
			return 0;
patmandin@991
   529
		}
patmandin@991
   530
	}
patmandin@991
   531
patmandin@1000
   532
	/* TinyGL only supports VDI_RGB (OSMESA_RGB) */
patmandin@1000
   533
	tinygl_present=0;
patmandin@1000
   534
	if (this->gl_config.dll_handle) {
patmandin@1000
   535
		if (this->gl_data->glFinish == NULL) {
patmandin@1000
   536
			tinygl_present=1;
patmandin@1000
   537
		}
patmandin@1000
   538
	}
patmandin@1000
   539
patmandin@991
   540
	/* Init OpenGL context using OSMesa */
patmandin@991
   541
	gl_convert = ConvertNull;
patmandin@991
   542
	gl_copyshadow = CopyShadowNull;
patmandin@1005
   543
	gl_upsidedown = SDL_FALSE;
patmandin@991
   544
patmandin@991
   545
	pixel_format = current->format;
patmandin@991
   546
	redmask = pixel_format->Rmask;
patmandin@991
   547
	switch (pixel_format->BitsPerPixel) {
patmandin@991
   548
		case 15:
patmandin@991
   549
			/* 15 bits unsupported */
patmandin@1000
   550
			if (tinygl_present) {
patmandin@1002
   551
				gl_pixelsize = 3;
patmandin@1000
   552
				osmesa_format = VDI_RGB;
patmandin@1000
   553
				if (redmask == 31<<10) {
patmandin@1000
   554
					gl_copyshadow = CopyShadowRGBTo555;
patmandin@1000
   555
				} else {
patmandin@1000
   556
					gl_copyshadow = CopyShadowRGBTo565;
patmandin@1000
   557
					gl_convert = Convert565To555le;
patmandin@1000
   558
				}
patmandin@991
   559
			} else {
patmandin@1002
   560
				gl_pixelsize = 4;
patmandin@1005
   561
				gl_upsidedown = SDL_TRUE;
patmandin@1000
   562
				osmesa_format = OSMESA_ARGB;
patmandin@1000
   563
				if (redmask == 31<<10) {
patmandin@1000
   564
					gl_copyshadow = CopyShadow8888To555;
patmandin@1000
   565
				} else {
patmandin@1000
   566
					gl_copyshadow = CopyShadow8888To565;
patmandin@1000
   567
					gl_convert = Convert565To555le;
patmandin@1000
   568
				}
patmandin@991
   569
			}
patmandin@991
   570
			break;
patmandin@991
   571
		case 16:
patmandin@991
   572
			/* 16 bits unsupported */
patmandin@1000
   573
			if (tinygl_present) {
patmandin@1002
   574
				gl_pixelsize = 3;
patmandin@1000
   575
				osmesa_format = VDI_RGB;
patmandin@1000
   576
				gl_copyshadow = CopyShadowRGBTo565;
patmandin@1000
   577
				if (redmask != 31<<11) {
patmandin@1000
   578
					/* 565, little endian, unsupported */
patmandin@1000
   579
					gl_convert = Convert565le;
patmandin@1000
   580
				}
patmandin@1000
   581
			} else {
patmandin@1002
   582
				gl_pixelsize = 4;
patmandin@1005
   583
				gl_upsidedown = SDL_TRUE;
patmandin@1000
   584
				osmesa_format = OSMESA_ARGB;
patmandin@1000
   585
				gl_copyshadow = CopyShadow8888To565;
patmandin@1000
   586
				if (redmask != 31<<11) {
patmandin@1000
   587
					/* 565, little endian, unsupported */
patmandin@1000
   588
					gl_convert = Convert565le;
patmandin@1000
   589
				}
patmandin@991
   590
			}
patmandin@991
   591
			break;
patmandin@991
   592
		case 24:
patmandin@991
   593
			gl_pixelsize = 3;
patmandin@1000
   594
			if (tinygl_present) {
patmandin@1000
   595
				osmesa_format = VDI_RGB;
patmandin@1000
   596
				gl_copyshadow = CopyShadowDirect;
patmandin@1000
   597
				if (redmask != 255<<16) {
patmandin@1000
   598
					gl_copyshadow = CopyShadowRGBSwap;
patmandin@1000
   599
				}
patmandin@991
   600
			} else {
patmandin@1000
   601
				gl_copyshadow = CopyShadowDirect;
patmandin@1005
   602
				gl_upsidedown = SDL_TRUE;
patmandin@1000
   603
				if (redmask == 255<<16) {
patmandin@1000
   604
					osmesa_format = OSMESA_RGB;
patmandin@1000
   605
				} else {
patmandin@1000
   606
					osmesa_format = OSMESA_BGR;
patmandin@1000
   607
				}
patmandin@991
   608
			}
patmandin@991
   609
			break;
patmandin@991
   610
		case 32:
patmandin@1000
   611
			if (tinygl_present) {
patmandin@1002
   612
				gl_pixelsize = 3;
patmandin@1000
   613
				osmesa_format = VDI_RGB;
patmandin@1000
   614
				gl_copyshadow = CopyShadowRGBToARGB;
patmandin@1000
   615
				if (redmask == 255) {
patmandin@1000
   616
					gl_convert = CopyShadowRGBToABGR;
patmandin@1000
   617
				} else if (redmask == 255<<8) {
patmandin@1000
   618
					gl_convert = CopyShadowRGBToBGRA;
patmandin@1000
   619
				} else if (redmask == 255<<24) {
patmandin@1000
   620
					gl_convert = CopyShadowRGBToRGBA;
patmandin@1000
   621
				}
patmandin@991
   622
			} else {
patmandin@1002
   623
				gl_pixelsize = 4;
patmandin@1005
   624
				gl_upsidedown = SDL_TRUE;
patmandin@1000
   625
				gl_copyshadow = CopyShadowDirect;
patmandin@1000
   626
				if (redmask == 255<<16) {
patmandin@1000
   627
					osmesa_format = OSMESA_ARGB;
patmandin@1000
   628
				} else if (redmask == 255<<8) {
patmandin@1000
   629
					osmesa_format = OSMESA_BGRA;
patmandin@1000
   630
				} else if (redmask == 255<<24) {
patmandin@1000
   631
					osmesa_format = OSMESA_RGBA;
patmandin@1000
   632
				} else {
patmandin@1000
   633
					/* ABGR format unsupported */
patmandin@1000
   634
					osmesa_format = OSMESA_BGRA;
patmandin@1000
   635
					gl_convert = ConvertBGRAToABGR;
patmandin@1000
   636
				}
patmandin@991
   637
			}
patmandin@991
   638
			break;
patmandin@991
   639
		default:
patmandin@1000
   640
			if (tinygl_present) {
patmandin@1000
   641
				SDL_AtariGL_Quit(this, SDL_FALSE);
patmandin@1000
   642
				return 0;
patmandin@1000
   643
			}
patmandin@991
   644
			gl_pixelsize = 1;
patmandin@991
   645
			gl_copyshadow = CopyShadowDirect;
patmandin@991
   646
			osmesa_format = OSMESA_COLOR_INDEX;
patmandin@991
   647
			break;
patmandin@991
   648
	}
patmandin@991
   649
patmandin@992
   650
	/* Try to keep current context if possible */
patmandin@992
   651
	recreatecontext=1;
patmandin@992
   652
	if (gl_shadow &&
patmandin@992
   653
		(gl_curformat == osmesa_format) &&
patmandin@992
   654
		(gl_curwidth == current->w) &&
patmandin@992
   655
		(gl_curheight == current->h)) {
patmandin@992
   656
		recreatecontext = 0;
patmandin@992
   657
	}
patmandin@992
   658
	if (recreatecontext) {
patmandin@992
   659
		SDL_AtariGL_Quit(this, SDL_FALSE);
patmandin@992
   660
patmandin@992
   661
		gl_shadow = this->gl_data->OSMesaCreateLDG(
patmandin@992
   662
			osmesa_format, GL_UNSIGNED_BYTE, current->w, current->h
patmandin@992
   663
		);
patmandin@992
   664
patmandin@992
   665
		if (gl_shadow) {
patmandin@992
   666
			gl_curformat = osmesa_format;
patmandin@992
   667
			gl_curwidth = current->w;
patmandin@992
   668
			gl_curheight = current->h;
patmandin@992
   669
		} else {
patmandin@992
   670
			gl_curformat = 0;
patmandin@992
   671
			gl_curwidth = 0;
patmandin@992
   672
			gl_curheight = 0;
patmandin@992
   673
		}
patmandin@992
   674
	}
patmandin@991
   675
patmandin@991
   676
	return (gl_shadow != NULL);
patmandin@991
   677
}
patmandin@991
   678
patmandin@991
   679
/*--- Conversions routines from shadow buffer to the screen ---*/
patmandin@991
   680
patmandin@991
   681
static void CopyShadowNull(_THIS, SDL_Surface *surface)
patmandin@989
   682
{
patmandin@989
   683
}
patmandin@989
   684
patmandin@991
   685
static void CopyShadowDirect(_THIS, SDL_Surface *surface)
patmandin@991
   686
{
patmandin@991
   687
	int y, srcpitch, dstpitch;
patmandin@991
   688
	Uint8 *srcline, *dstline;
patmandin@991
   689
patmandin@991
   690
	srcline = gl_shadow;
patmandin@991
   691
	srcpitch = surface->w * gl_pixelsize;
patmandin@991
   692
	dstline = surface->pixels;
patmandin@991
   693
	dstpitch = surface->pitch;
patmandin@1005
   694
	if (gl_upsidedown) {
patmandin@1005
   695
		srcline += (surface->h-1)*srcpitch;
patmandin@1005
   696
		srcpitch = -srcpitch;
patmandin@1005
   697
	}
patmandin@991
   698
patmandin@991
   699
	for (y=0; y<surface->h; y++) {
patmandin@991
   700
		memcpy(dstline, srcline, srcpitch);
patmandin@991
   701
patmandin@991
   702
		srcline += srcpitch;
patmandin@991
   703
		dstline += dstpitch;
patmandin@991
   704
	}
patmandin@991
   705
}
patmandin@991
   706
patmandin@1000
   707
static void CopyShadowRGBTo555(_THIS, SDL_Surface *surface)
patmandin@1000
   708
{
patmandin@1000
   709
	int x,y, srcpitch, dstpitch;
patmandin@1000
   710
	Uint16 *dstline, *dstcol;
patmandin@1000
   711
	Uint8 *srcline, *srccol;
patmandin@1000
   712
patmandin@1000
   713
	srcline = (Uint8 *)gl_shadow;
patmandin@1002
   714
	srcpitch = surface->w * gl_pixelsize;
patmandin@1000
   715
	dstline = surface->pixels;
patmandin@1000
   716
	dstpitch = surface->pitch >>1;
patmandin@1005
   717
	if (gl_upsidedown) {
patmandin@1005
   718
		srcline += (surface->h-1)*srcpitch;
patmandin@1005
   719
		srcpitch = -srcpitch;
patmandin@1005
   720
	}
patmandin@1000
   721
patmandin@1000
   722
	for (y=0; y<surface->h; y++) {
patmandin@1000
   723
		srccol = srcline;
patmandin@1000
   724
		dstcol = dstline;
patmandin@1000
   725
		for (x=0; x<surface->w; x++) {
patmandin@1000
   726
			Uint16 dstcolor;
patmandin@1000
   727
			
patmandin@1003
   728
			dstcolor = ((*srccol++)<<7) & (31<<10);
patmandin@1003
   729
			dstcolor |= ((*srccol++)<<2) & (31<<5);
patmandin@1000
   730
			dstcolor |= ((*srccol++)>>3) & 31;
patmandin@1000
   731
			*dstcol++ = dstcolor;
patmandin@1000
   732
		}
patmandin@1000
   733
patmandin@1000
   734
		srcline += srcpitch;
patmandin@1000
   735
		dstline += dstpitch;
patmandin@1000
   736
	}
patmandin@1000
   737
}
patmandin@1000
   738
patmandin@1000
   739
static void CopyShadowRGBTo565(_THIS, SDL_Surface *surface)
patmandin@1000
   740
{
patmandin@1000
   741
	int x,y, srcpitch, dstpitch;
patmandin@1000
   742
	Uint16 *dstline, *dstcol;
patmandin@1000
   743
	Uint8 *srcline, *srccol;
patmandin@1000
   744
patmandin@1000
   745
	srcline = (Uint8 *)gl_shadow;
patmandin@1002
   746
	srcpitch = surface->w * gl_pixelsize;
patmandin@1000
   747
	dstline = surface->pixels;
patmandin@1000
   748
	dstpitch = surface->pitch >>1;
patmandin@1005
   749
	if (gl_upsidedown) {
patmandin@1005
   750
		srcline += (surface->h-1)*srcpitch;
patmandin@1005
   751
		srcpitch = -srcpitch;
patmandin@1005
   752
	}
patmandin@1000
   753
patmandin@1000
   754
	for (y=0; y<surface->h; y++) {
patmandin@1000
   755
		srccol = srcline;
patmandin@1000
   756
		dstcol = dstline;
patmandin@1000
   757
patmandin@1000
   758
		for (x=0; x<surface->w; x++) {
patmandin@1000
   759
			Uint16 dstcolor;
patmandin@1000
   760
			
patmandin@1003
   761
			dstcolor = ((*srccol++)<<8) & (31<<11);
patmandin@1003
   762
			dstcolor |= ((*srccol++)<<3) & (63<<5);
patmandin@1000
   763
			dstcolor |= ((*srccol++)>>3) & 31;
patmandin@1000
   764
			*dstcol++ = dstcolor;
patmandin@1000
   765
		}
patmandin@1000
   766
patmandin@1000
   767
		srcline += srcpitch;
patmandin@1000
   768
		dstline += dstpitch;
patmandin@1000
   769
	}
patmandin@1000
   770
}
patmandin@1000
   771
patmandin@1000
   772
static void CopyShadowRGBSwap(_THIS, SDL_Surface *surface)
patmandin@1000
   773
{
patmandin@1000
   774
	int x,y, srcpitch, dstpitch;
patmandin@1000
   775
	Uint8 *dstline, *dstcol;
patmandin@1000
   776
	Uint8 *srcline, *srccol;
patmandin@1000
   777
patmandin@1000
   778
	srcline = (Uint8 *)gl_shadow;
patmandin@1002
   779
	srcpitch = surface->w * gl_pixelsize;
patmandin@1000
   780
	dstline = surface->pixels;
patmandin@1000
   781
	dstpitch = surface->pitch;
patmandin@1005
   782
	if (gl_upsidedown) {
patmandin@1005
   783
		srcline += (surface->h-1)*srcpitch;
patmandin@1005
   784
		srcpitch = -srcpitch;
patmandin@1005
   785
	}
patmandin@1000
   786
patmandin@1000
   787
	for (y=0; y<surface->h; y++) {
patmandin@1000
   788
		srccol = srcline;
patmandin@1000
   789
		dstcol = dstline;
patmandin@1000
   790
patmandin@1000
   791
		for (x=0; x<surface->w; x++) {
patmandin@1000
   792
			*dstcol++ = srccol[2];
patmandin@1000
   793
			*dstcol++ = srccol[1];
patmandin@1000
   794
			*dstcol++ = srccol[0];
patmandin@1000
   795
			srccol += 3;
patmandin@1000
   796
		}
patmandin@1000
   797
patmandin@1000
   798
		srcline += srcpitch;
patmandin@1000
   799
		dstline += dstpitch;
patmandin@1000
   800
	}
patmandin@1000
   801
}
patmandin@1000
   802
patmandin@1000
   803
static void CopyShadowRGBToARGB(_THIS, SDL_Surface *surface)
patmandin@1000
   804
{
patmandin@1000
   805
	int x,y, srcpitch, dstpitch;
patmandin@1000
   806
	Uint32 *dstline, *dstcol;
patmandin@1000
   807
	Uint8 *srcline, *srccol;
patmandin@1000
   808
patmandin@1000
   809
	srcline = (Uint8 *)gl_shadow;
patmandin@1002
   810
	srcpitch = surface->w * gl_pixelsize;
patmandin@1000
   811
	dstline = surface->pixels;
patmandin@1000
   812
	dstpitch = surface->pitch >>2;
patmandin@1005
   813
	if (gl_upsidedown) {
patmandin@1005
   814
		srcline += (surface->h-1)*srcpitch;
patmandin@1005
   815
		srcpitch = -srcpitch;
patmandin@1005
   816
	}
patmandin@1000
   817
patmandin@1000
   818
	for (y=0; y<surface->h; y++) {
patmandin@1000
   819
		srccol = srcline;
patmandin@1000
   820
		dstcol = dstline;
patmandin@1000
   821
patmandin@1000
   822
		for (x=0; x<surface->w; x++) {
patmandin@1000
   823
			Uint32	dstcolor;
patmandin@1000
   824
patmandin@1000
   825
			dstcolor = (*srccol++)<<16;
patmandin@1000
   826
			dstcolor |= (*srccol++)<<8;
patmandin@1000
   827
			dstcolor |= *srccol++;
patmandin@1000
   828
patmandin@1000
   829
			*dstcol++ = dstcolor;
patmandin@1000
   830
		}
patmandin@1000
   831
patmandin@1000
   832
		srcline += srcpitch;
patmandin@1000
   833
		dstline += dstpitch;
patmandin@1000
   834
	}
patmandin@1000
   835
}
patmandin@1000
   836
patmandin@1000
   837
static void CopyShadowRGBToABGR(_THIS, SDL_Surface *surface)
patmandin@1000
   838
{
patmandin@1000
   839
	int x,y, srcpitch, dstpitch;
patmandin@1000
   840
	Uint32 *dstline, *dstcol;
patmandin@1000
   841
	Uint8 *srcline, *srccol;
patmandin@1000
   842
patmandin@1000
   843
	srcline = (Uint8 *)gl_shadow;
patmandin@1002
   844
	srcpitch = surface->w * gl_pixelsize;
patmandin@1000
   845
	dstline = surface->pixels;
patmandin@1000
   846
	dstpitch = surface->pitch >>2;
patmandin@1005
   847
	if (gl_upsidedown) {
patmandin@1005
   848
		srcline += (surface->h-1)*srcpitch;
patmandin@1005
   849
		srcpitch = -srcpitch;
patmandin@1005
   850
	}
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++;
patmandin@1000
   860
			dstcolor |= (*srccol++)<<8;
patmandin@1000
   861
			dstcolor |= (*srccol++)<<16;
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 CopyShadowRGBToBGRA(_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@1005
   881
	if (gl_upsidedown) {
patmandin@1005
   882
		srcline += (surface->h-1)*srcpitch;
patmandin@1005
   883
		srcpitch = -srcpitch;
patmandin@1005
   884
	}
patmandin@1000
   885
patmandin@1000
   886
	for (y=0; y<surface->h; y++) {
patmandin@1000
   887
		srccol = srcline;
patmandin@1000
   888
		dstcol = dstline;
patmandin@1000
   889
patmandin@1000
   890
		for (x=0; x<surface->w; x++) {
patmandin@1000
   891
			Uint32	dstcolor;
patmandin@1000
   892
patmandin@1000
   893
			dstcolor = (*srccol++)<<8;
patmandin@1000
   894
			dstcolor |= (*srccol++)<<16;
patmandin@1000
   895
			dstcolor |= (*srccol++)<<24;
patmandin@1000
   896
patmandin@1000
   897
			*dstcol++ = dstcolor;
patmandin@1000
   898
		}
patmandin@1000
   899
patmandin@1000
   900
		srcline += srcpitch;
patmandin@1000
   901
		dstline += dstpitch;
patmandin@1000
   902
	}
patmandin@1000
   903
}
patmandin@1000
   904
patmandin@1000
   905
static void CopyShadowRGBToRGBA(_THIS, SDL_Surface *surface)
patmandin@1000
   906
{
patmandin@1000
   907
	int x,y, srcpitch, dstpitch;
patmandin@1000
   908
	Uint32 *dstline, *dstcol;
patmandin@1000
   909
	Uint8 *srcline, *srccol;
patmandin@1000
   910
patmandin@1000
   911
	srcline = (Uint8 *)gl_shadow;
patmandin@1002
   912
	srcpitch = surface->w * gl_pixelsize;
patmandin@1000
   913
	dstline = surface->pixels;
patmandin@1000
   914
	dstpitch = surface->pitch >>2;
patmandin@1005
   915
	if (gl_upsidedown) {
patmandin@1005
   916
		srcline += (surface->h-1)*srcpitch;
patmandin@1005
   917
		srcpitch = -srcpitch;
patmandin@1005
   918
	}
patmandin@1000
   919
patmandin@1000
   920
	for (y=0; y<surface->h; y++) {
patmandin@1000
   921
		srccol = srcline;
patmandin@1000
   922
		dstcol = dstline;
patmandin@1000
   923
patmandin@1000
   924
		for (x=0; x<surface->w; x++) {
patmandin@1000
   925
			Uint32	dstcolor;
patmandin@1000
   926
patmandin@1000
   927
			dstcolor = (*srccol++)<<24;
patmandin@1000
   928
			dstcolor |= (*srccol++)<<16;
patmandin@1000
   929
			dstcolor |= (*srccol++)<<8;
patmandin@1000
   930
patmandin@1000
   931
			*dstcol++ = dstcolor;
patmandin@1000
   932
		}
patmandin@1000
   933
patmandin@1000
   934
		srcline += srcpitch;
patmandin@1000
   935
		dstline += dstpitch;
patmandin@1000
   936
	}
patmandin@1000
   937
}
patmandin@1000
   938
patmandin@991
   939
static void CopyShadow8888To555(_THIS, SDL_Surface *surface)
patmandin@991
   940
{
patmandin@991
   941
	int x,y, srcpitch, dstpitch;
patmandin@991
   942
	Uint16 *dstline, *dstcol;
patmandin@991
   943
	Uint32 *srcline, *srccol;
patmandin@991
   944
patmandin@991
   945
	srcline = (Uint32 *)gl_shadow;
patmandin@1002
   946
	srcpitch = (surface->w * gl_pixelsize) >>2;
patmandin@991
   947
	dstline = surface->pixels;
patmandin@991
   948
	dstpitch = surface->pitch >>1;
patmandin@1005
   949
	if (gl_upsidedown) {
patmandin@1005
   950
		srcline += (surface->h-1)*srcpitch;
patmandin@1005
   951
		srcpitch = -srcpitch;
patmandin@1005
   952
	}
patmandin@991
   953
patmandin@991
   954
	for (y=0; y<surface->h; y++) {
patmandin@991
   955
		srccol = srcline;
patmandin@991
   956
		dstcol = dstline;
patmandin@991
   957
		for (x=0; x<surface->w; x++) {
patmandin@991
   958
			Uint32 srccolor;
patmandin@991
   959
			Uint16 dstcolor;
patmandin@991
   960
			
patmandin@991
   961
			srccolor = *srccol++;
patmandin@991
   962
			dstcolor = (srccolor>>9) & (31<<10);
patmandin@991
   963
			dstcolor |= (srccolor>>6) & (31<<5);
patmandin@991
   964
			dstcolor |= (srccolor>>3) & 31;
patmandin@991
   965
			*dstcol++ = dstcolor;
patmandin@991
   966
		}
patmandin@991
   967
patmandin@991
   968
		srcline += srcpitch;
patmandin@991
   969
		dstline += dstpitch;
patmandin@991
   970
	}
patmandin@991
   971
}
patmandin@991
   972
patmandin@991
   973
static void CopyShadow8888To565(_THIS, SDL_Surface *surface)
patmandin@991
   974
{
patmandin@991
   975
	int x,y, srcpitch, dstpitch;
patmandin@991
   976
	Uint16 *dstline, *dstcol;
patmandin@991
   977
	Uint32 *srcline, *srccol;
patmandin@991
   978
patmandin@991
   979
	srcline = (Uint32 *)gl_shadow;
patmandin@1002
   980
	srcpitch = (surface->w * gl_pixelsize) >> 2;
patmandin@991
   981
	dstline = surface->pixels;
patmandin@991
   982
	dstpitch = surface->pitch >>1;
patmandin@1005
   983
	if (gl_upsidedown) {
patmandin@1005
   984
		srcline += (surface->h-1)*srcpitch;
patmandin@1005
   985
		srcpitch = -srcpitch;
patmandin@1005
   986
	}
patmandin@991
   987
patmandin@991
   988
	for (y=0; y<surface->h; y++) {
patmandin@991
   989
		srccol = srcline;
patmandin@991
   990
		dstcol = dstline;
patmandin@991
   991
patmandin@991
   992
		for (x=0; x<surface->w; x++) {
patmandin@991
   993
			Uint32 srccolor;
patmandin@991
   994
			Uint16 dstcolor;
patmandin@991
   995
			
patmandin@991
   996
			srccolor = *srccol++;
patmandin@991
   997
			dstcolor = (srccolor>>8) & (31<<11);
patmandin@991
   998
			dstcolor |= (srccolor>>5) & (63<<5);
patmandin@991
   999
			dstcolor |= (srccolor>>3) & 31;
patmandin@991
  1000
			*dstcol++ = dstcolor;
patmandin@991
  1001
		}
patmandin@991
  1002
patmandin@991
  1003
		srcline += srcpitch;
patmandin@991
  1004
		dstline += dstpitch;
patmandin@991
  1005
	}
patmandin@991
  1006
}
patmandin@991
  1007
patmandin@991
  1008
/*--- Conversions routines in the screen ---*/
patmandin@991
  1009
patmandin@991
  1010
static void ConvertNull(_THIS, SDL_Surface *surface)
patmandin@991
  1011
{
patmandin@991
  1012
}
patmandin@991
  1013
patmandin@991
  1014
static void Convert565To555be(_THIS, SDL_Surface *surface)
patmandin@989
  1015
{
patmandin@989
  1016
	int x,y, pitch;
patmandin@989
  1017
	unsigned short *line, *pixel;
patmandin@989
  1018
patmandin@989
  1019
	line = surface->pixels;
patmandin@989
  1020
	pitch = surface->pitch >> 1;
patmandin@989
  1021
	for (y=0; y<surface->h; y++) {
patmandin@989
  1022
		pixel = line;
patmandin@989
  1023
		for (x=0; x<surface->w; x++) {
patmandin@989
  1024
			unsigned short color = *pixel;
patmandin@989
  1025
patmandin@989
  1026
			*pixel++ = (color & 0x1f)|((color>>1) & 0xffe0);
patmandin@989
  1027
		}
patmandin@989
  1028
patmandin@989
  1029
		line += pitch;
patmandin@989
  1030
	}
patmandin@989
  1031
}
patmandin@989
  1032
patmandin@991
  1033
static void Convert565To555le(_THIS, SDL_Surface *surface)
patmandin@989
  1034
{
patmandin@989
  1035
	int x,y, pitch;
patmandin@989
  1036
	unsigned short *line, *pixel;
patmandin@989
  1037
patmandin@989
  1038
	line = surface->pixels;
patmandin@989
  1039
	pitch = surface->pitch >>1;
patmandin@989
  1040
	for (y=0; y<surface->h; y++) {
patmandin@989
  1041
		pixel = line;
patmandin@989
  1042
		for (x=0; x<surface->w; x++) {
patmandin@989
  1043
			unsigned short color = *pixel;
patmandin@989
  1044
patmandin@989
  1045
			color = (color & 0x1f)|((color>>1) & 0xffe0);
patmandin@989
  1046
			*pixel++ = SDL_Swap16(color);
patmandin@989
  1047
		}
patmandin@989
  1048
patmandin@989
  1049
		line += pitch;
patmandin@989
  1050
	}
patmandin@989
  1051
}
patmandin@989
  1052
patmandin@991
  1053
static void Convert565le(_THIS, SDL_Surface *surface)
patmandin@989
  1054
{
patmandin@989
  1055
	int x,y, pitch;
patmandin@989
  1056
	unsigned short *line, *pixel;
patmandin@989
  1057
patmandin@989
  1058
	line = surface->pixels;
patmandin@989
  1059
	pitch = surface->pitch >>1;
patmandin@989
  1060
	for (y=0; y<surface->h; y++) {
patmandin@989
  1061
		pixel = line;
patmandin@989
  1062
		for (x=0; x<surface->w; x++) {
patmandin@989
  1063
			unsigned short color = *pixel;
patmandin@989
  1064
patmandin@989
  1065
			*pixel++ = SDL_Swap16(color);
patmandin@989
  1066
		}
patmandin@989
  1067
patmandin@989
  1068
		line += pitch;
patmandin@989
  1069
	}
patmandin@989
  1070
}
patmandin@989
  1071
patmandin@991
  1072
static void ConvertBGRAToABGR(_THIS, SDL_Surface *surface)
patmandin@989
  1073
{
patmandin@989
  1074
	int x,y, pitch;
patmandin@989
  1075
	unsigned long *line, *pixel;
patmandin@989
  1076
patmandin@989
  1077
	line = surface->pixels;
patmandin@989
  1078
	pitch = surface->pitch >>2;
patmandin@989
  1079
	for (y=0; y<surface->h; y++) {
patmandin@989
  1080
		pixel = line;
patmandin@989
  1081
		for (x=0; x<surface->w; x++) {
patmandin@989
  1082
			unsigned long color = *pixel;
patmandin@989
  1083
patmandin@989
  1084
			*pixel++ = (color<<24)|(color>>8);
patmandin@989
  1085
		}
patmandin@989
  1086
patmandin@989
  1087
		line += pitch;
patmandin@989
  1088
	}
patmandin@989
  1089
}
patmandin@1000
  1090
patmandin@1000
  1091
#endif /* HAVE_OPENGL */