src/video/ataricommon/SDL_atarigl.c
author Patrice Mandin
Sun, 08 Jan 2006 10:22:27 +0000
changeset 1238 1901fea5edac
parent 1005 2ab1cb02a44e
child 1239 cbdb2c156aca
permissions -rw-r--r--
Load default OpenGL library if needed at driver initialization
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@1238
   137
	/* Driver already loaded ? */
patmandin@1238
   138
	if (this->gl_config.driver_loaded) {
patmandin@1238
   139
		/* Default path ? don't change anything */
patmandin@1238
   140
		if (path==NULL) {
patmandin@1238
   141
			return 0;
patmandin@1238
   142
		}
patmandin@1238
   143
		/* Unload previous driver */
patmandin@1238
   144
		SDL_AtariGL_UnloadLibrary(this);
patmandin@1238
   145
	}
patmandin@991
   146
patmandin@991
   147
	/* Load library given by path */
patmandin@1238
   148
	handle = NULL
patmandin@1238
   149
	if (path != NULL) {
patmandin@1238
   150
		handle = SDL_LoadObject(path);
patmandin@1238
   151
	}
patmandin@991
   152
	if (handle == NULL) {
patmandin@991
   153
		/* Try to load another one */
patmandin@991
   154
		path = getenv("SDL_VIDEO_GL_DRIVER");
patmandin@991
   155
		if ( path != NULL ) {
patmandin@991
   156
			handle = SDL_LoadObject(path);
patmandin@991
   157
		}
patmandin@991
   158
patmandin@991
   159
		/* If it does not work, try some other */
patmandin@991
   160
		if (handle == NULL) {
patmandin@991
   161
			path = PATH_OSMESA_LDG;
patmandin@991
   162
			handle = SDL_LoadObject(path);
patmandin@991
   163
		}
patmandin@991
   164
patmandin@991
   165
		if (handle == NULL) {
patmandin@991
   166
			path = PATH_MESAGL_LDG;
patmandin@991
   167
			handle = SDL_LoadObject(path);
patmandin@991
   168
		}
patmandin@991
   169
patmandin@991
   170
		if (handle == NULL) {
patmandin@991
   171
			path = PATH_TINYGL_LDG;
patmandin@991
   172
			handle = SDL_LoadObject(path);
patmandin@991
   173
		}
patmandin@991
   174
	}
patmandin@991
   175
patmandin@991
   176
	if (handle == NULL) {
patmandin@991
   177
		SDL_SetError("Could not load OpenGL library");
patmandin@991
   178
		return -1;
patmandin@991
   179
	}
patmandin@991
   180
patmandin@993
   181
	this->gl_data->glGetIntegerv = SDL_LoadFunction(handle, "glGetIntegerv");
patmandin@993
   182
	this->gl_data->glFinish = SDL_LoadFunction(handle, "glFinish");
patmandin@995
   183
	this->gl_data->glFlush = SDL_LoadFunction(handle, "glFlush");
patmandin@993
   184
patmandin@995
   185
	cancel_load = SDL_FALSE;
patmandin@995
   186
	if (this->gl_data->glGetIntegerv == NULL) {
patmandin@995
   187
		cancel_load = SDL_TRUE;
patmandin@995
   188
	} else {
patmandin@995
   189
		/* We need either glFinish (OSMesa) or glFlush (TinyGL) */
patmandin@995
   190
		if ((this->gl_data->glFinish == NULL) &&
patmandin@995
   191
			(this->gl_data->glFlush == NULL)) {
patmandin@995
   192
				cancel_load = SDL_TRUE;
patmandin@995
   193
		}
patmandin@995
   194
	}
patmandin@995
   195
	if (cancel_load) {
patmandin@993
   196
		SDL_SetError("Could not retrieve OpenGL functions");
patmandin@993
   197
		SDL_UnloadObject(handle);
patmandin@993
   198
		/* Restore pointers to static library */
patmandin@993
   199
		SDL_AtariGL_InitPointers(this);
patmandin@993
   200
		return -1;
patmandin@993
   201
	}
patmandin@993
   202
patmandin@991
   203
	/* Load functions pointers (osmesa.ldg) */
patmandin@991
   204
	this->gl_data->OSMesaCreateContextExt = SDL_LoadFunction(handle, "OSMesaCreateContextExt");
patmandin@991
   205
	this->gl_data->OSMesaDestroyContext = SDL_LoadFunction(handle, "OSMesaDestroyContext");
patmandin@991
   206
	this->gl_data->OSMesaMakeCurrent = SDL_LoadFunction(handle, "OSMesaMakeCurrent");
patmandin@991
   207
	this->gl_data->OSMesaPixelStore = SDL_LoadFunction(handle, "OSMesaPixelStore");
patmandin@991
   208
	this->gl_data->OSMesaGetProcAddress = SDL_LoadFunction(handle, "OSMesaGetProcAddress");
patmandin@991
   209
patmandin@991
   210
	/* Load old functions pointers (mesa_gl.ldg, tiny_gl.ldg) */
patmandin@991
   211
	this->gl_data->OSMesaCreateLDG = SDL_LoadFunction(handle, "OSMesaCreateLDG");
patmandin@991
   212
	this->gl_data->OSMesaDestroyLDG = SDL_LoadFunction(handle, "OSMesaDestroyLDG");
patmandin@991
   213
patmandin@991
   214
	gl_oldmesa = 0;
patmandin@991
   215
patmandin@991
   216
	if ( (this->gl_data->OSMesaCreateContextExt == NULL) || 
patmandin@991
   217
	     (this->gl_data->OSMesaDestroyContext == NULL) ||
patmandin@991
   218
	     (this->gl_data->OSMesaMakeCurrent == NULL) ||
patmandin@991
   219
	     (this->gl_data->OSMesaPixelStore == NULL) ||
patmandin@991
   220
	     (this->gl_data->OSMesaGetProcAddress == NULL)) {
patmandin@991
   221
		/* Hum, maybe old library ? */
patmandin@991
   222
		if ( (this->gl_data->OSMesaCreateLDG == NULL) || 
patmandin@991
   223
		     (this->gl_data->OSMesaDestroyLDG == NULL)) {
patmandin@993
   224
			SDL_SetError("Could not retrieve OSMesa functions");
patmandin@993
   225
			SDL_UnloadObject(handle);
patmandin@993
   226
			/* Restore pointers to static library */
patmandin@993
   227
			SDL_AtariGL_InitPointers(this);
patmandin@991
   228
			return -1;
patmandin@991
   229
		} else {
patmandin@991
   230
			gl_oldmesa = 1;
patmandin@991
   231
		}
patmandin@991
   232
	}
patmandin@991
   233
patmandin@991
   234
	this->gl_config.dll_handle = handle;
patmandin@991
   235
	if ( path ) {
patmandin@991
   236
		strncpy(this->gl_config.driver_path, path,
patmandin@991
   237
			sizeof(this->gl_config.driver_path)-1);
patmandin@991
   238
	} else {
patmandin@991
   239
		strcpy(this->gl_config.driver_path, "");
patmandin@991
   240
	}
patmandin@991
   241
patmandin@991
   242
#endif
patmandin@989
   243
	this->gl_config.driver_loaded = 1;
patmandin@991
   244
patmandin@991
   245
	return 0;
patmandin@991
   246
#else
patmandin@991
   247
	return -1;
patmandin@989
   248
#endif
patmandin@989
   249
}
patmandin@989
   250
patmandin@989
   251
void *SDL_AtariGL_GetProcAddress(_THIS, const char *proc)
patmandin@989
   252
{
patmandin@989
   253
	void *func = NULL;
patmandin@989
   254
#ifdef HAVE_OPENGL
patmandin@991
   255
patmandin@991
   256
	if (this->gl_config.dll_handle) {
patmandin@991
   257
		func = SDL_LoadFunction(this->gl_config.dll_handle, (void *)proc);
patmandin@991
   258
	} else if (this->gl_data->OSMesaGetProcAddress) {
patmandin@991
   259
		func = this->gl_data->OSMesaGetProcAddress(proc);
patmandin@989
   260
	}
patmandin@991
   261
patmandin@989
   262
#endif
patmandin@989
   263
	return func;
patmandin@989
   264
}
patmandin@989
   265
patmandin@989
   266
int SDL_AtariGL_GetAttribute(_THIS, SDL_GLattr attrib, int* value)
patmandin@989
   267
{
patmandin@989
   268
#ifdef HAVE_OPENGL
patmandin@989
   269
	GLenum mesa_attrib;
patmandin@989
   270
	SDL_Surface *surface;
patmandin@989
   271
patmandin@991
   272
	if (!gl_active) {
patmandin@989
   273
		return -1;
patmandin@989
   274
	}
patmandin@989
   275
patmandin@989
   276
	switch(attrib) {
patmandin@989
   277
		case SDL_GL_RED_SIZE:
patmandin@989
   278
			mesa_attrib = GL_RED_BITS;
patmandin@989
   279
			break;
patmandin@989
   280
		case SDL_GL_GREEN_SIZE:
patmandin@989
   281
			mesa_attrib = GL_GREEN_BITS;
patmandin@989
   282
			break;
patmandin@989
   283
		case SDL_GL_BLUE_SIZE:
patmandin@989
   284
			mesa_attrib = GL_BLUE_BITS;
patmandin@989
   285
			break;
patmandin@989
   286
		case SDL_GL_ALPHA_SIZE:
patmandin@989
   287
			mesa_attrib = GL_ALPHA_BITS;
patmandin@989
   288
			break;
patmandin@989
   289
		case SDL_GL_DOUBLEBUFFER:
patmandin@989
   290
			surface = this->screen;
patmandin@989
   291
			*value = ((surface->flags & SDL_DOUBLEBUF)==SDL_DOUBLEBUF);
patmandin@989
   292
			return 0;
patmandin@989
   293
		case SDL_GL_DEPTH_SIZE:
patmandin@989
   294
			mesa_attrib = GL_DEPTH_BITS;
patmandin@989
   295
			break;
patmandin@989
   296
		case SDL_GL_STENCIL_SIZE:
patmandin@989
   297
			mesa_attrib = GL_STENCIL_BITS;
patmandin@989
   298
			break;
patmandin@989
   299
		case SDL_GL_ACCUM_RED_SIZE:
patmandin@989
   300
			mesa_attrib = GL_ACCUM_RED_BITS;
patmandin@989
   301
			break;
patmandin@989
   302
		case SDL_GL_ACCUM_GREEN_SIZE:
patmandin@989
   303
			mesa_attrib = GL_ACCUM_GREEN_BITS;
patmandin@989
   304
			break;
patmandin@989
   305
		case SDL_GL_ACCUM_BLUE_SIZE:
patmandin@989
   306
			mesa_attrib = GL_ACCUM_BLUE_BITS;
patmandin@989
   307
			break;
patmandin@989
   308
		case SDL_GL_ACCUM_ALPHA_SIZE:
patmandin@989
   309
			mesa_attrib = GL_ACCUM_ALPHA_BITS;
patmandin@989
   310
			break;
patmandin@989
   311
		default :
patmandin@989
   312
			return -1;
patmandin@989
   313
	}
patmandin@989
   314
patmandin@991
   315
	this->gl_data->glGetIntegerv(mesa_attrib, value);
patmandin@989
   316
	return 0;
patmandin@989
   317
#else
patmandin@989
   318
	return -1;
patmandin@989
   319
#endif
patmandin@989
   320
}
patmandin@989
   321
patmandin@989
   322
int SDL_AtariGL_MakeCurrent(_THIS)
patmandin@989
   323
{
patmandin@989
   324
#ifdef HAVE_OPENGL
patmandin@989
   325
	SDL_Surface *surface;
patmandin@989
   326
	GLenum type;
patmandin@989
   327
patmandin@991
   328
	if (gl_oldmesa && gl_active) {
patmandin@991
   329
		return 0;
patmandin@991
   330
	}
patmandin@991
   331
patmandin@991
   332
	if (this->gl_config.dll_handle) {
patmandin@991
   333
		if ((this->gl_data->OSMesaMakeCurrent == NULL) ||
patmandin@991
   334
			(this->gl_data->OSMesaPixelStore == NULL)) {
patmandin@991
   335
			return -1;
patmandin@991
   336
		}
patmandin@991
   337
	}
patmandin@991
   338
patmandin@991
   339
	if (!gl_active) {
patmandin@989
   340
		SDL_SetError("Invalid OpenGL context");
patmandin@989
   341
		return -1;
patmandin@989
   342
	}
patmandin@989
   343
patmandin@989
   344
	surface = this->screen;
patmandin@989
   345
	
patmandin@989
   346
	if ((surface->format->BitsPerPixel == 15) || (surface->format->BitsPerPixel == 16)) {
patmandin@989
   347
		type = GL_UNSIGNED_SHORT_5_6_5;
patmandin@989
   348
	} else {
patmandin@989
   349
		type = GL_UNSIGNED_BYTE;
patmandin@989
   350
	}
patmandin@989
   351
patmandin@991
   352
	if (!(this->gl_data->OSMesaMakeCurrent(gl_ctx, surface->pixels, type, surface->w, surface->h))) {
patmandin@989
   353
		SDL_SetError("Can not make OpenGL context current");
patmandin@989
   354
		return -1;
patmandin@989
   355
	}
patmandin@989
   356
patmandin@989
   357
	/* OSMesa draws upside down */
patmandin@991
   358
	this->gl_data->OSMesaPixelStore(OSMESA_Y_UP, 0);
patmandin@989
   359
patmandin@989
   360
	return 0;
patmandin@989
   361
#else
patmandin@989
   362
	return -1;
patmandin@989
   363
#endif
patmandin@989
   364
}
patmandin@989
   365
patmandin@989
   366
void SDL_AtariGL_SwapBuffers(_THIS)
patmandin@989
   367
{
patmandin@989
   368
#ifdef HAVE_OPENGL
patmandin@991
   369
	if (gl_active) {
patmandin@993
   370
		if (this->gl_config.dll_handle) {
patmandin@993
   371
			if (this->gl_data->glFinish) {
patmandin@993
   372
				this->gl_data->glFinish();
patmandin@995
   373
			} else if (this->gl_data->glFlush) {
patmandin@995
   374
				this->gl_data->glFlush();
patmandin@993
   375
			}
patmandin@993
   376
		} else {
patmandin@993
   377
			this->gl_data->glFinish();
patmandin@993
   378
		}
patmandin@991
   379
		gl_copyshadow(this, this->screen);
patmandin@991
   380
		gl_convert(this, this->screen);
patmandin@989
   381
	}
patmandin@991
   382
#endif
patmandin@991
   383
}
patmandin@989
   384
patmandin@991
   385
void SDL_AtariGL_InitPointers(_THIS)
patmandin@991
   386
{
patmandin@991
   387
#if defined(HAVE_OPENGL)
patmandin@991
   388
	this->gl_data->OSMesaCreateContextExt = OSMesaCreateContextExt;
patmandin@991
   389
	this->gl_data->OSMesaDestroyContext = OSMesaDestroyContext;
patmandin@991
   390
	this->gl_data->OSMesaMakeCurrent = OSMesaMakeCurrent;
patmandin@991
   391
	this->gl_data->OSMesaPixelStore = OSMesaPixelStore;
patmandin@991
   392
	this->gl_data->OSMesaGetProcAddress = OSMesaGetProcAddress;
patmandin@993
   393
patmandin@991
   394
	this->gl_data->glGetIntegerv = glGetIntegerv;
patmandin@993
   395
	this->gl_data->glFinish = glFinish;
patmandin@995
   396
	this->gl_data->glFlush = glFlush;
patmandin@993
   397
patmandin@993
   398
	this->gl_data->OSMesaCreateLDG = NULL;
patmandin@993
   399
	this->gl_data->OSMesaDestroyLDG = NULL;
patmandin@989
   400
#endif
patmandin@989
   401
}
patmandin@989
   402
patmandin@989
   403
/*--- Private functions ---*/
patmandin@989
   404
patmandin@991
   405
static void SDL_AtariGL_UnloadLibrary(_THIS)
patmandin@991
   406
{
patmandin@991
   407
#if defined(HAVE_OPENGL)
patmandin@991
   408
	if (this->gl_config.dll_handle) {
patmandin@991
   409
		SDL_UnloadObject(this->gl_config.dll_handle);
patmandin@991
   410
		this->gl_config.dll_handle = NULL;
patmandin@1238
   411
		this->gl_config.driver_loaded = 0;
patmandin@991
   412
patmandin@991
   413
		/* Restore pointers to static library */
patmandin@993
   414
		SDL_AtariGL_InitPointers(this);
patmandin@991
   415
	}
patmandin@991
   416
#endif
patmandin@991
   417
}
patmandin@991
   418
patmandin@991
   419
/*--- Creation of an OpenGL context using new/old functions ---*/
patmandin@991
   420
patmandin@1000
   421
#ifdef HAVE_OPENGL
patmandin@991
   422
static int InitNew(_THIS, SDL_Surface *current)
patmandin@991
   423
{
patmandin@991
   424
	GLenum osmesa_format;
patmandin@991
   425
	SDL_PixelFormat *pixel_format;
patmandin@991
   426
	Uint32	redmask;
patmandin@992
   427
	int recreatecontext;
patmandin@992
   428
	GLint newaccumsize;
patmandin@991
   429
patmandin@991
   430
	if (this->gl_config.dll_handle) {
patmandin@991
   431
		if (this->gl_data->OSMesaCreateContextExt == NULL) {
patmandin@991
   432
			return 0;
patmandin@991
   433
		}
patmandin@991
   434
	}
patmandin@991
   435
patmandin@991
   436
	/* Init OpenGL context using OSMesa */
patmandin@991
   437
	gl_convert = ConvertNull;
patmandin@991
   438
	gl_copyshadow = CopyShadowNull;
patmandin@1005
   439
	gl_upsidedown = SDL_FALSE;
patmandin@991
   440
patmandin@991
   441
	pixel_format = current->format;
patmandin@991
   442
	redmask = pixel_format->Rmask;
patmandin@991
   443
	switch (pixel_format->BitsPerPixel) {
patmandin@991
   444
		case 15:
patmandin@991
   445
			/* 1555, big and little endian, unsupported */
patmandin@991
   446
			gl_pixelsize = 2;
patmandin@991
   447
			osmesa_format = OSMESA_RGB_565;
patmandin@991
   448
			if (redmask == 31<<10) {
patmandin@991
   449
				gl_convert = Convert565To555be;
patmandin@991
   450
			} else {
patmandin@991
   451
				gl_convert = Convert565To555le;
patmandin@991
   452
			}
patmandin@991
   453
			break;
patmandin@991
   454
		case 16:
patmandin@991
   455
			gl_pixelsize = 2;
patmandin@991
   456
			if (redmask == 31<<11) {
patmandin@991
   457
				osmesa_format = OSMESA_RGB_565;
patmandin@991
   458
			} else {
patmandin@991
   459
				/* 565, little endian, unsupported */
patmandin@991
   460
				osmesa_format = OSMESA_RGB_565;
patmandin@991
   461
				gl_convert = Convert565le;
patmandin@991
   462
			}
patmandin@991
   463
			break;
patmandin@991
   464
		case 24:
patmandin@991
   465
			gl_pixelsize = 3;
patmandin@991
   466
			if (redmask == 255<<16) {
patmandin@991
   467
				osmesa_format = OSMESA_RGB;
patmandin@991
   468
			} else {
patmandin@991
   469
				osmesa_format = OSMESA_BGR;
patmandin@991
   470
			}
patmandin@991
   471
			break;
patmandin@991
   472
		case 32:
patmandin@991
   473
			gl_pixelsize = 4;
patmandin@991
   474
			if (redmask == 255<<16) {
patmandin@991
   475
				osmesa_format = OSMESA_ARGB;
patmandin@991
   476
			} else if (redmask == 255<<8) {
patmandin@991
   477
				osmesa_format = OSMESA_BGRA;
patmandin@991
   478
			} else if (redmask == 255<<24) {
patmandin@991
   479
				osmesa_format = OSMESA_RGBA;
patmandin@991
   480
			} else {
patmandin@991
   481
				/* ABGR format unsupported */
patmandin@991
   482
				osmesa_format = OSMESA_BGRA;
patmandin@991
   483
				gl_convert = ConvertBGRAToABGR;
patmandin@991
   484
			}
patmandin@991
   485
			break;
patmandin@991
   486
		default:
patmandin@991
   487
			gl_pixelsize = 1;
patmandin@991
   488
			osmesa_format = OSMESA_COLOR_INDEX;
patmandin@991
   489
			break;
patmandin@991
   490
	}
patmandin@991
   491
patmandin@992
   492
	/* Try to keep current context if possible */
patmandin@992
   493
	newaccumsize =
patmandin@992
   494
		this->gl_config.accum_red_size +
patmandin@992
   495
		this->gl_config.accum_green_size +
patmandin@992
   496
		this->gl_config.accum_blue_size +
patmandin@992
   497
		this->gl_config.accum_alpha_size;
patmandin@992
   498
	recreatecontext=1;
patmandin@992
   499
	if (gl_ctx &&
patmandin@992
   500
		(gl_curformat == osmesa_format) &&
patmandin@992
   501
		(gl_curdepth == this->gl_config.depth_size) &&
patmandin@992
   502
		(gl_curstencil == this->gl_config.stencil_size) &&
patmandin@992
   503
		(gl_curaccum == newaccumsize)) {
patmandin@992
   504
		recreatecontext = 0;
patmandin@992
   505
	}
patmandin@992
   506
	if (recreatecontext) {
patmandin@992
   507
		SDL_AtariGL_Quit(this, SDL_FALSE);
patmandin@992
   508
patmandin@992
   509
		gl_ctx = this->gl_data->OSMesaCreateContextExt(
patmandin@992
   510
			osmesa_format, this->gl_config.depth_size,
patmandin@992
   511
			this->gl_config.stencil_size, newaccumsize, NULL );
patmandin@992
   512
patmandin@992
   513
		if (gl_ctx) {
patmandin@992
   514
			gl_curformat = osmesa_format;
patmandin@992
   515
			gl_curdepth = this->gl_config.depth_size;
patmandin@992
   516
			gl_curstencil = this->gl_config.stencil_size;
patmandin@992
   517
			gl_curaccum = newaccumsize;
patmandin@992
   518
		} else {
patmandin@992
   519
			gl_curformat = 0;
patmandin@992
   520
			gl_curdepth = 0;
patmandin@992
   521
			gl_curstencil = 0;
patmandin@992
   522
			gl_curaccum = 0;
patmandin@992
   523
		}
patmandin@992
   524
	}
patmandin@991
   525
patmandin@991
   526
	return (gl_ctx != NULL);
patmandin@991
   527
}
patmandin@991
   528
patmandin@1000
   529
patmandin@991
   530
static int InitOld(_THIS, SDL_Surface *current)
patmandin@991
   531
{
patmandin@991
   532
	GLenum osmesa_format;
patmandin@991
   533
	SDL_PixelFormat *pixel_format;
patmandin@991
   534
	Uint32	redmask;
patmandin@1000
   535
	int recreatecontext, tinygl_present;
patmandin@991
   536
patmandin@991
   537
	if (this->gl_config.dll_handle) {
patmandin@991
   538
		if (this->gl_data->OSMesaCreateLDG == NULL) {
patmandin@991
   539
			return 0;
patmandin@991
   540
		}
patmandin@991
   541
	}
patmandin@991
   542
patmandin@1000
   543
	/* TinyGL only supports VDI_RGB (OSMESA_RGB) */
patmandin@1000
   544
	tinygl_present=0;
patmandin@1000
   545
	if (this->gl_config.dll_handle) {
patmandin@1000
   546
		if (this->gl_data->glFinish == NULL) {
patmandin@1000
   547
			tinygl_present=1;
patmandin@1000
   548
		}
patmandin@1000
   549
	}
patmandin@1000
   550
patmandin@991
   551
	/* Init OpenGL context using OSMesa */
patmandin@991
   552
	gl_convert = ConvertNull;
patmandin@991
   553
	gl_copyshadow = CopyShadowNull;
patmandin@1005
   554
	gl_upsidedown = SDL_FALSE;
patmandin@991
   555
patmandin@991
   556
	pixel_format = current->format;
patmandin@991
   557
	redmask = pixel_format->Rmask;
patmandin@991
   558
	switch (pixel_format->BitsPerPixel) {
patmandin@991
   559
		case 15:
patmandin@991
   560
			/* 15 bits unsupported */
patmandin@1000
   561
			if (tinygl_present) {
patmandin@1002
   562
				gl_pixelsize = 3;
patmandin@1000
   563
				osmesa_format = VDI_RGB;
patmandin@1000
   564
				if (redmask == 31<<10) {
patmandin@1000
   565
					gl_copyshadow = CopyShadowRGBTo555;
patmandin@1000
   566
				} else {
patmandin@1000
   567
					gl_copyshadow = CopyShadowRGBTo565;
patmandin@1000
   568
					gl_convert = Convert565To555le;
patmandin@1000
   569
				}
patmandin@991
   570
			} else {
patmandin@1002
   571
				gl_pixelsize = 4;
patmandin@1005
   572
				gl_upsidedown = SDL_TRUE;
patmandin@1000
   573
				osmesa_format = OSMESA_ARGB;
patmandin@1000
   574
				if (redmask == 31<<10) {
patmandin@1000
   575
					gl_copyshadow = CopyShadow8888To555;
patmandin@1000
   576
				} else {
patmandin@1000
   577
					gl_copyshadow = CopyShadow8888To565;
patmandin@1000
   578
					gl_convert = Convert565To555le;
patmandin@1000
   579
				}
patmandin@991
   580
			}
patmandin@991
   581
			break;
patmandin@991
   582
		case 16:
patmandin@991
   583
			/* 16 bits unsupported */
patmandin@1000
   584
			if (tinygl_present) {
patmandin@1002
   585
				gl_pixelsize = 3;
patmandin@1000
   586
				osmesa_format = VDI_RGB;
patmandin@1000
   587
				gl_copyshadow = CopyShadowRGBTo565;
patmandin@1000
   588
				if (redmask != 31<<11) {
patmandin@1000
   589
					/* 565, little endian, unsupported */
patmandin@1000
   590
					gl_convert = Convert565le;
patmandin@1000
   591
				}
patmandin@1000
   592
			} else {
patmandin@1002
   593
				gl_pixelsize = 4;
patmandin@1005
   594
				gl_upsidedown = SDL_TRUE;
patmandin@1000
   595
				osmesa_format = OSMESA_ARGB;
patmandin@1000
   596
				gl_copyshadow = CopyShadow8888To565;
patmandin@1000
   597
				if (redmask != 31<<11) {
patmandin@1000
   598
					/* 565, little endian, unsupported */
patmandin@1000
   599
					gl_convert = Convert565le;
patmandin@1000
   600
				}
patmandin@991
   601
			}
patmandin@991
   602
			break;
patmandin@991
   603
		case 24:
patmandin@991
   604
			gl_pixelsize = 3;
patmandin@1000
   605
			if (tinygl_present) {
patmandin@1000
   606
				osmesa_format = VDI_RGB;
patmandin@1000
   607
				gl_copyshadow = CopyShadowDirect;
patmandin@1000
   608
				if (redmask != 255<<16) {
patmandin@1000
   609
					gl_copyshadow = CopyShadowRGBSwap;
patmandin@1000
   610
				}
patmandin@991
   611
			} else {
patmandin@1000
   612
				gl_copyshadow = CopyShadowDirect;
patmandin@1005
   613
				gl_upsidedown = SDL_TRUE;
patmandin@1000
   614
				if (redmask == 255<<16) {
patmandin@1000
   615
					osmesa_format = OSMESA_RGB;
patmandin@1000
   616
				} else {
patmandin@1000
   617
					osmesa_format = OSMESA_BGR;
patmandin@1000
   618
				}
patmandin@991
   619
			}
patmandin@991
   620
			break;
patmandin@991
   621
		case 32:
patmandin@1000
   622
			if (tinygl_present) {
patmandin@1002
   623
				gl_pixelsize = 3;
patmandin@1000
   624
				osmesa_format = VDI_RGB;
patmandin@1000
   625
				gl_copyshadow = CopyShadowRGBToARGB;
patmandin@1000
   626
				if (redmask == 255) {
patmandin@1000
   627
					gl_convert = CopyShadowRGBToABGR;
patmandin@1000
   628
				} else if (redmask == 255<<8) {
patmandin@1000
   629
					gl_convert = CopyShadowRGBToBGRA;
patmandin@1000
   630
				} else if (redmask == 255<<24) {
patmandin@1000
   631
					gl_convert = CopyShadowRGBToRGBA;
patmandin@1000
   632
				}
patmandin@991
   633
			} else {
patmandin@1002
   634
				gl_pixelsize = 4;
patmandin@1005
   635
				gl_upsidedown = SDL_TRUE;
patmandin@1000
   636
				gl_copyshadow = CopyShadowDirect;
patmandin@1000
   637
				if (redmask == 255<<16) {
patmandin@1000
   638
					osmesa_format = OSMESA_ARGB;
patmandin@1000
   639
				} else if (redmask == 255<<8) {
patmandin@1000
   640
					osmesa_format = OSMESA_BGRA;
patmandin@1000
   641
				} else if (redmask == 255<<24) {
patmandin@1000
   642
					osmesa_format = OSMESA_RGBA;
patmandin@1000
   643
				} else {
patmandin@1000
   644
					/* ABGR format unsupported */
patmandin@1000
   645
					osmesa_format = OSMESA_BGRA;
patmandin@1000
   646
					gl_convert = ConvertBGRAToABGR;
patmandin@1000
   647
				}
patmandin@991
   648
			}
patmandin@991
   649
			break;
patmandin@991
   650
		default:
patmandin@1000
   651
			if (tinygl_present) {
patmandin@1000
   652
				SDL_AtariGL_Quit(this, SDL_FALSE);
patmandin@1000
   653
				return 0;
patmandin@1000
   654
			}
patmandin@991
   655
			gl_pixelsize = 1;
patmandin@991
   656
			gl_copyshadow = CopyShadowDirect;
patmandin@991
   657
			osmesa_format = OSMESA_COLOR_INDEX;
patmandin@991
   658
			break;
patmandin@991
   659
	}
patmandin@991
   660
patmandin@992
   661
	/* Try to keep current context if possible */
patmandin@992
   662
	recreatecontext=1;
patmandin@992
   663
	if (gl_shadow &&
patmandin@992
   664
		(gl_curformat == osmesa_format) &&
patmandin@992
   665
		(gl_curwidth == current->w) &&
patmandin@992
   666
		(gl_curheight == current->h)) {
patmandin@992
   667
		recreatecontext = 0;
patmandin@992
   668
	}
patmandin@992
   669
	if (recreatecontext) {
patmandin@992
   670
		SDL_AtariGL_Quit(this, SDL_FALSE);
patmandin@992
   671
patmandin@992
   672
		gl_shadow = this->gl_data->OSMesaCreateLDG(
patmandin@992
   673
			osmesa_format, GL_UNSIGNED_BYTE, current->w, current->h
patmandin@992
   674
		);
patmandin@992
   675
patmandin@992
   676
		if (gl_shadow) {
patmandin@992
   677
			gl_curformat = osmesa_format;
patmandin@992
   678
			gl_curwidth = current->w;
patmandin@992
   679
			gl_curheight = current->h;
patmandin@992
   680
		} else {
patmandin@992
   681
			gl_curformat = 0;
patmandin@992
   682
			gl_curwidth = 0;
patmandin@992
   683
			gl_curheight = 0;
patmandin@992
   684
		}
patmandin@992
   685
	}
patmandin@991
   686
patmandin@991
   687
	return (gl_shadow != NULL);
patmandin@991
   688
}
patmandin@991
   689
patmandin@991
   690
/*--- Conversions routines from shadow buffer to the screen ---*/
patmandin@991
   691
patmandin@991
   692
static void CopyShadowNull(_THIS, SDL_Surface *surface)
patmandin@989
   693
{
patmandin@989
   694
}
patmandin@989
   695
patmandin@991
   696
static void CopyShadowDirect(_THIS, SDL_Surface *surface)
patmandin@991
   697
{
patmandin@991
   698
	int y, srcpitch, dstpitch;
patmandin@991
   699
	Uint8 *srcline, *dstline;
patmandin@991
   700
patmandin@991
   701
	srcline = gl_shadow;
patmandin@991
   702
	srcpitch = surface->w * gl_pixelsize;
patmandin@991
   703
	dstline = surface->pixels;
patmandin@991
   704
	dstpitch = surface->pitch;
patmandin@1005
   705
	if (gl_upsidedown) {
patmandin@1005
   706
		srcline += (surface->h-1)*srcpitch;
patmandin@1005
   707
		srcpitch = -srcpitch;
patmandin@1005
   708
	}
patmandin@991
   709
patmandin@991
   710
	for (y=0; y<surface->h; y++) {
patmandin@991
   711
		memcpy(dstline, srcline, srcpitch);
patmandin@991
   712
patmandin@991
   713
		srcline += srcpitch;
patmandin@991
   714
		dstline += dstpitch;
patmandin@991
   715
	}
patmandin@991
   716
}
patmandin@991
   717
patmandin@1000
   718
static void CopyShadowRGBTo555(_THIS, SDL_Surface *surface)
patmandin@1000
   719
{
patmandin@1000
   720
	int x,y, srcpitch, dstpitch;
patmandin@1000
   721
	Uint16 *dstline, *dstcol;
patmandin@1000
   722
	Uint8 *srcline, *srccol;
patmandin@1000
   723
patmandin@1000
   724
	srcline = (Uint8 *)gl_shadow;
patmandin@1002
   725
	srcpitch = surface->w * gl_pixelsize;
patmandin@1000
   726
	dstline = surface->pixels;
patmandin@1000
   727
	dstpitch = surface->pitch >>1;
patmandin@1005
   728
	if (gl_upsidedown) {
patmandin@1005
   729
		srcline += (surface->h-1)*srcpitch;
patmandin@1005
   730
		srcpitch = -srcpitch;
patmandin@1005
   731
	}
patmandin@1000
   732
patmandin@1000
   733
	for (y=0; y<surface->h; y++) {
patmandin@1000
   734
		srccol = srcline;
patmandin@1000
   735
		dstcol = dstline;
patmandin@1000
   736
		for (x=0; x<surface->w; x++) {
patmandin@1000
   737
			Uint16 dstcolor;
patmandin@1000
   738
			
patmandin@1003
   739
			dstcolor = ((*srccol++)<<7) & (31<<10);
patmandin@1003
   740
			dstcolor |= ((*srccol++)<<2) & (31<<5);
patmandin@1000
   741
			dstcolor |= ((*srccol++)>>3) & 31;
patmandin@1000
   742
			*dstcol++ = dstcolor;
patmandin@1000
   743
		}
patmandin@1000
   744
patmandin@1000
   745
		srcline += srcpitch;
patmandin@1000
   746
		dstline += dstpitch;
patmandin@1000
   747
	}
patmandin@1000
   748
}
patmandin@1000
   749
patmandin@1000
   750
static void CopyShadowRGBTo565(_THIS, SDL_Surface *surface)
patmandin@1000
   751
{
patmandin@1000
   752
	int x,y, srcpitch, dstpitch;
patmandin@1000
   753
	Uint16 *dstline, *dstcol;
patmandin@1000
   754
	Uint8 *srcline, *srccol;
patmandin@1000
   755
patmandin@1000
   756
	srcline = (Uint8 *)gl_shadow;
patmandin@1002
   757
	srcpitch = surface->w * gl_pixelsize;
patmandin@1000
   758
	dstline = surface->pixels;
patmandin@1000
   759
	dstpitch = surface->pitch >>1;
patmandin@1005
   760
	if (gl_upsidedown) {
patmandin@1005
   761
		srcline += (surface->h-1)*srcpitch;
patmandin@1005
   762
		srcpitch = -srcpitch;
patmandin@1005
   763
	}
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
			Uint16 dstcolor;
patmandin@1000
   771
			
patmandin@1003
   772
			dstcolor = ((*srccol++)<<8) & (31<<11);
patmandin@1003
   773
			dstcolor |= ((*srccol++)<<3) & (63<<5);
patmandin@1000
   774
			dstcolor |= ((*srccol++)>>3) & 31;
patmandin@1000
   775
			*dstcol++ = dstcolor;
patmandin@1000
   776
		}
patmandin@1000
   777
patmandin@1000
   778
		srcline += srcpitch;
patmandin@1000
   779
		dstline += dstpitch;
patmandin@1000
   780
	}
patmandin@1000
   781
}
patmandin@1000
   782
patmandin@1000
   783
static void CopyShadowRGBSwap(_THIS, SDL_Surface *surface)
patmandin@1000
   784
{
patmandin@1000
   785
	int x,y, srcpitch, dstpitch;
patmandin@1000
   786
	Uint8 *dstline, *dstcol;
patmandin@1000
   787
	Uint8 *srcline, *srccol;
patmandin@1000
   788
patmandin@1000
   789
	srcline = (Uint8 *)gl_shadow;
patmandin@1002
   790
	srcpitch = surface->w * gl_pixelsize;
patmandin@1000
   791
	dstline = surface->pixels;
patmandin@1000
   792
	dstpitch = surface->pitch;
patmandin@1005
   793
	if (gl_upsidedown) {
patmandin@1005
   794
		srcline += (surface->h-1)*srcpitch;
patmandin@1005
   795
		srcpitch = -srcpitch;
patmandin@1005
   796
	}
patmandin@1000
   797
patmandin@1000
   798
	for (y=0; y<surface->h; y++) {
patmandin@1000
   799
		srccol = srcline;
patmandin@1000
   800
		dstcol = dstline;
patmandin@1000
   801
patmandin@1000
   802
		for (x=0; x<surface->w; x++) {
patmandin@1000
   803
			*dstcol++ = srccol[2];
patmandin@1000
   804
			*dstcol++ = srccol[1];
patmandin@1000
   805
			*dstcol++ = srccol[0];
patmandin@1000
   806
			srccol += 3;
patmandin@1000
   807
		}
patmandin@1000
   808
patmandin@1000
   809
		srcline += srcpitch;
patmandin@1000
   810
		dstline += dstpitch;
patmandin@1000
   811
	}
patmandin@1000
   812
}
patmandin@1000
   813
patmandin@1000
   814
static void CopyShadowRGBToARGB(_THIS, SDL_Surface *surface)
patmandin@1000
   815
{
patmandin@1000
   816
	int x,y, srcpitch, dstpitch;
patmandin@1000
   817
	Uint32 *dstline, *dstcol;
patmandin@1000
   818
	Uint8 *srcline, *srccol;
patmandin@1000
   819
patmandin@1000
   820
	srcline = (Uint8 *)gl_shadow;
patmandin@1002
   821
	srcpitch = surface->w * gl_pixelsize;
patmandin@1000
   822
	dstline = surface->pixels;
patmandin@1000
   823
	dstpitch = surface->pitch >>2;
patmandin@1005
   824
	if (gl_upsidedown) {
patmandin@1005
   825
		srcline += (surface->h-1)*srcpitch;
patmandin@1005
   826
		srcpitch = -srcpitch;
patmandin@1005
   827
	}
patmandin@1000
   828
patmandin@1000
   829
	for (y=0; y<surface->h; y++) {
patmandin@1000
   830
		srccol = srcline;
patmandin@1000
   831
		dstcol = dstline;
patmandin@1000
   832
patmandin@1000
   833
		for (x=0; x<surface->w; x++) {
patmandin@1000
   834
			Uint32	dstcolor;
patmandin@1000
   835
patmandin@1000
   836
			dstcolor = (*srccol++)<<16;
patmandin@1000
   837
			dstcolor |= (*srccol++)<<8;
patmandin@1000
   838
			dstcolor |= *srccol++;
patmandin@1000
   839
patmandin@1000
   840
			*dstcol++ = dstcolor;
patmandin@1000
   841
		}
patmandin@1000
   842
patmandin@1000
   843
		srcline += srcpitch;
patmandin@1000
   844
		dstline += dstpitch;
patmandin@1000
   845
	}
patmandin@1000
   846
}
patmandin@1000
   847
patmandin@1000
   848
static void CopyShadowRGBToABGR(_THIS, SDL_Surface *surface)
patmandin@1000
   849
{
patmandin@1000
   850
	int x,y, srcpitch, dstpitch;
patmandin@1000
   851
	Uint32 *dstline, *dstcol;
patmandin@1000
   852
	Uint8 *srcline, *srccol;
patmandin@1000
   853
patmandin@1000
   854
	srcline = (Uint8 *)gl_shadow;
patmandin@1002
   855
	srcpitch = surface->w * gl_pixelsize;
patmandin@1000
   856
	dstline = surface->pixels;
patmandin@1000
   857
	dstpitch = surface->pitch >>2;
patmandin@1005
   858
	if (gl_upsidedown) {
patmandin@1005
   859
		srcline += (surface->h-1)*srcpitch;
patmandin@1005
   860
		srcpitch = -srcpitch;
patmandin@1005
   861
	}
patmandin@1000
   862
patmandin@1000
   863
	for (y=0; y<surface->h; y++) {
patmandin@1000
   864
		srccol = srcline;
patmandin@1000
   865
		dstcol = dstline;
patmandin@1000
   866
patmandin@1000
   867
		for (x=0; x<surface->w; x++) {
patmandin@1000
   868
			Uint32	dstcolor;
patmandin@1000
   869
patmandin@1000
   870
			dstcolor = *srccol++;
patmandin@1000
   871
			dstcolor |= (*srccol++)<<8;
patmandin@1000
   872
			dstcolor |= (*srccol++)<<16;
patmandin@1000
   873
patmandin@1000
   874
			*dstcol++ = dstcolor;
patmandin@1000
   875
		}
patmandin@1000
   876
patmandin@1000
   877
		srcline += srcpitch;
patmandin@1000
   878
		dstline += dstpitch;
patmandin@1000
   879
	}
patmandin@1000
   880
}
patmandin@1000
   881
patmandin@1000
   882
static void CopyShadowRGBToBGRA(_THIS, SDL_Surface *surface)
patmandin@1000
   883
{
patmandin@1000
   884
	int x,y, srcpitch, dstpitch;
patmandin@1000
   885
	Uint32 *dstline, *dstcol;
patmandin@1000
   886
	Uint8 *srcline, *srccol;
patmandin@1000
   887
patmandin@1000
   888
	srcline = (Uint8 *)gl_shadow;
patmandin@1002
   889
	srcpitch = surface->w * gl_pixelsize;
patmandin@1000
   890
	dstline = surface->pixels;
patmandin@1000
   891
	dstpitch = surface->pitch >>2;
patmandin@1005
   892
	if (gl_upsidedown) {
patmandin@1005
   893
		srcline += (surface->h-1)*srcpitch;
patmandin@1005
   894
		srcpitch = -srcpitch;
patmandin@1005
   895
	}
patmandin@1000
   896
patmandin@1000
   897
	for (y=0; y<surface->h; y++) {
patmandin@1000
   898
		srccol = srcline;
patmandin@1000
   899
		dstcol = dstline;
patmandin@1000
   900
patmandin@1000
   901
		for (x=0; x<surface->w; x++) {
patmandin@1000
   902
			Uint32	dstcolor;
patmandin@1000
   903
patmandin@1000
   904
			dstcolor = (*srccol++)<<8;
patmandin@1000
   905
			dstcolor |= (*srccol++)<<16;
patmandin@1000
   906
			dstcolor |= (*srccol++)<<24;
patmandin@1000
   907
patmandin@1000
   908
			*dstcol++ = dstcolor;
patmandin@1000
   909
		}
patmandin@1000
   910
patmandin@1000
   911
		srcline += srcpitch;
patmandin@1000
   912
		dstline += dstpitch;
patmandin@1000
   913
	}
patmandin@1000
   914
}
patmandin@1000
   915
patmandin@1000
   916
static void CopyShadowRGBToRGBA(_THIS, SDL_Surface *surface)
patmandin@1000
   917
{
patmandin@1000
   918
	int x,y, srcpitch, dstpitch;
patmandin@1000
   919
	Uint32 *dstline, *dstcol;
patmandin@1000
   920
	Uint8 *srcline, *srccol;
patmandin@1000
   921
patmandin@1000
   922
	srcline = (Uint8 *)gl_shadow;
patmandin@1002
   923
	srcpitch = surface->w * gl_pixelsize;
patmandin@1000
   924
	dstline = surface->pixels;
patmandin@1000
   925
	dstpitch = surface->pitch >>2;
patmandin@1005
   926
	if (gl_upsidedown) {
patmandin@1005
   927
		srcline += (surface->h-1)*srcpitch;
patmandin@1005
   928
		srcpitch = -srcpitch;
patmandin@1005
   929
	}
patmandin@1000
   930
patmandin@1000
   931
	for (y=0; y<surface->h; y++) {
patmandin@1000
   932
		srccol = srcline;
patmandin@1000
   933
		dstcol = dstline;
patmandin@1000
   934
patmandin@1000
   935
		for (x=0; x<surface->w; x++) {
patmandin@1000
   936
			Uint32	dstcolor;
patmandin@1000
   937
patmandin@1000
   938
			dstcolor = (*srccol++)<<24;
patmandin@1000
   939
			dstcolor |= (*srccol++)<<16;
patmandin@1000
   940
			dstcolor |= (*srccol++)<<8;
patmandin@1000
   941
patmandin@1000
   942
			*dstcol++ = dstcolor;
patmandin@1000
   943
		}
patmandin@1000
   944
patmandin@1000
   945
		srcline += srcpitch;
patmandin@1000
   946
		dstline += dstpitch;
patmandin@1000
   947
	}
patmandin@1000
   948
}
patmandin@1000
   949
patmandin@991
   950
static void CopyShadow8888To555(_THIS, SDL_Surface *surface)
patmandin@991
   951
{
patmandin@991
   952
	int x,y, srcpitch, dstpitch;
patmandin@991
   953
	Uint16 *dstline, *dstcol;
patmandin@991
   954
	Uint32 *srcline, *srccol;
patmandin@991
   955
patmandin@991
   956
	srcline = (Uint32 *)gl_shadow;
patmandin@1002
   957
	srcpitch = (surface->w * gl_pixelsize) >>2;
patmandin@991
   958
	dstline = surface->pixels;
patmandin@991
   959
	dstpitch = surface->pitch >>1;
patmandin@1005
   960
	if (gl_upsidedown) {
patmandin@1005
   961
		srcline += (surface->h-1)*srcpitch;
patmandin@1005
   962
		srcpitch = -srcpitch;
patmandin@1005
   963
	}
patmandin@991
   964
patmandin@991
   965
	for (y=0; y<surface->h; y++) {
patmandin@991
   966
		srccol = srcline;
patmandin@991
   967
		dstcol = dstline;
patmandin@991
   968
		for (x=0; x<surface->w; x++) {
patmandin@991
   969
			Uint32 srccolor;
patmandin@991
   970
			Uint16 dstcolor;
patmandin@991
   971
			
patmandin@991
   972
			srccolor = *srccol++;
patmandin@991
   973
			dstcolor = (srccolor>>9) & (31<<10);
patmandin@991
   974
			dstcolor |= (srccolor>>6) & (31<<5);
patmandin@991
   975
			dstcolor |= (srccolor>>3) & 31;
patmandin@991
   976
			*dstcol++ = dstcolor;
patmandin@991
   977
		}
patmandin@991
   978
patmandin@991
   979
		srcline += srcpitch;
patmandin@991
   980
		dstline += dstpitch;
patmandin@991
   981
	}
patmandin@991
   982
}
patmandin@991
   983
patmandin@991
   984
static void CopyShadow8888To565(_THIS, SDL_Surface *surface)
patmandin@991
   985
{
patmandin@991
   986
	int x,y, srcpitch, dstpitch;
patmandin@991
   987
	Uint16 *dstline, *dstcol;
patmandin@991
   988
	Uint32 *srcline, *srccol;
patmandin@991
   989
patmandin@991
   990
	srcline = (Uint32 *)gl_shadow;
patmandin@1002
   991
	srcpitch = (surface->w * gl_pixelsize) >> 2;
patmandin@991
   992
	dstline = surface->pixels;
patmandin@991
   993
	dstpitch = surface->pitch >>1;
patmandin@1005
   994
	if (gl_upsidedown) {
patmandin@1005
   995
		srcline += (surface->h-1)*srcpitch;
patmandin@1005
   996
		srcpitch = -srcpitch;
patmandin@1005
   997
	}
patmandin@991
   998
patmandin@991
   999
	for (y=0; y<surface->h; y++) {
patmandin@991
  1000
		srccol = srcline;
patmandin@991
  1001
		dstcol = dstline;
patmandin@991
  1002
patmandin@991
  1003
		for (x=0; x<surface->w; x++) {
patmandin@991
  1004
			Uint32 srccolor;
patmandin@991
  1005
			Uint16 dstcolor;
patmandin@991
  1006
			
patmandin@991
  1007
			srccolor = *srccol++;
patmandin@991
  1008
			dstcolor = (srccolor>>8) & (31<<11);
patmandin@991
  1009
			dstcolor |= (srccolor>>5) & (63<<5);
patmandin@991
  1010
			dstcolor |= (srccolor>>3) & 31;
patmandin@991
  1011
			*dstcol++ = dstcolor;
patmandin@991
  1012
		}
patmandin@991
  1013
patmandin@991
  1014
		srcline += srcpitch;
patmandin@991
  1015
		dstline += dstpitch;
patmandin@991
  1016
	}
patmandin@991
  1017
}
patmandin@991
  1018
patmandin@991
  1019
/*--- Conversions routines in the screen ---*/
patmandin@991
  1020
patmandin@991
  1021
static void ConvertNull(_THIS, SDL_Surface *surface)
patmandin@991
  1022
{
patmandin@991
  1023
}
patmandin@991
  1024
patmandin@991
  1025
static void Convert565To555be(_THIS, SDL_Surface *surface)
patmandin@989
  1026
{
patmandin@989
  1027
	int x,y, pitch;
patmandin@989
  1028
	unsigned short *line, *pixel;
patmandin@989
  1029
patmandin@989
  1030
	line = surface->pixels;
patmandin@989
  1031
	pitch = surface->pitch >> 1;
patmandin@989
  1032
	for (y=0; y<surface->h; y++) {
patmandin@989
  1033
		pixel = line;
patmandin@989
  1034
		for (x=0; x<surface->w; x++) {
patmandin@989
  1035
			unsigned short color = *pixel;
patmandin@989
  1036
patmandin@989
  1037
			*pixel++ = (color & 0x1f)|((color>>1) & 0xffe0);
patmandin@989
  1038
		}
patmandin@989
  1039
patmandin@989
  1040
		line += pitch;
patmandin@989
  1041
	}
patmandin@989
  1042
}
patmandin@989
  1043
patmandin@991
  1044
static void Convert565To555le(_THIS, SDL_Surface *surface)
patmandin@989
  1045
{
patmandin@989
  1046
	int x,y, pitch;
patmandin@989
  1047
	unsigned short *line, *pixel;
patmandin@989
  1048
patmandin@989
  1049
	line = surface->pixels;
patmandin@989
  1050
	pitch = surface->pitch >>1;
patmandin@989
  1051
	for (y=0; y<surface->h; y++) {
patmandin@989
  1052
		pixel = line;
patmandin@989
  1053
		for (x=0; x<surface->w; x++) {
patmandin@989
  1054
			unsigned short color = *pixel;
patmandin@989
  1055
patmandin@989
  1056
			color = (color & 0x1f)|((color>>1) & 0xffe0);
patmandin@989
  1057
			*pixel++ = SDL_Swap16(color);
patmandin@989
  1058
		}
patmandin@989
  1059
patmandin@989
  1060
		line += pitch;
patmandin@989
  1061
	}
patmandin@989
  1062
}
patmandin@989
  1063
patmandin@991
  1064
static void Convert565le(_THIS, SDL_Surface *surface)
patmandin@989
  1065
{
patmandin@989
  1066
	int x,y, pitch;
patmandin@989
  1067
	unsigned short *line, *pixel;
patmandin@989
  1068
patmandin@989
  1069
	line = surface->pixels;
patmandin@989
  1070
	pitch = surface->pitch >>1;
patmandin@989
  1071
	for (y=0; y<surface->h; y++) {
patmandin@989
  1072
		pixel = line;
patmandin@989
  1073
		for (x=0; x<surface->w; x++) {
patmandin@989
  1074
			unsigned short color = *pixel;
patmandin@989
  1075
patmandin@989
  1076
			*pixel++ = SDL_Swap16(color);
patmandin@989
  1077
		}
patmandin@989
  1078
patmandin@989
  1079
		line += pitch;
patmandin@989
  1080
	}
patmandin@989
  1081
}
patmandin@989
  1082
patmandin@991
  1083
static void ConvertBGRAToABGR(_THIS, SDL_Surface *surface)
patmandin@989
  1084
{
patmandin@989
  1085
	int x,y, pitch;
patmandin@989
  1086
	unsigned long *line, *pixel;
patmandin@989
  1087
patmandin@989
  1088
	line = surface->pixels;
patmandin@989
  1089
	pitch = surface->pitch >>2;
patmandin@989
  1090
	for (y=0; y<surface->h; y++) {
patmandin@989
  1091
		pixel = line;
patmandin@989
  1092
		for (x=0; x<surface->w; x++) {
patmandin@989
  1093
			unsigned long color = *pixel;
patmandin@989
  1094
patmandin@989
  1095
			*pixel++ = (color<<24)|(color>>8);
patmandin@989
  1096
		}
patmandin@989
  1097
patmandin@989
  1098
		line += pitch;
patmandin@989
  1099
	}
patmandin@989
  1100
}
patmandin@1000
  1101
patmandin@1000
  1102
#endif /* HAVE_OPENGL */