src/video/SDL_video.c
author Holmes Futrell <hfutrell@umail.ucsb.edu>
Tue, 02 Sep 2008 20:19:45 +0000
changeset 2745 587d2b5fb805
parent 2744 1aede15771d0
child 2753 0969758c8809
permissions -rw-r--r--
Added support for OpenGL ES renderer
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
/* The high-level video driver subsystem */
slouken@0
    25
slouken@0
    26
#include "SDL.h"
slouken@0
    27
#include "SDL_sysvideo.h"
slouken@0
    28
#include "SDL_blit.h"
slouken@0
    29
#include "SDL_pixels_c.h"
slouken@1918
    30
#include "SDL_renderer_gl.h"
hfutrell@2745
    31
#include "SDL_renderer_gles.h"
slouken@1895
    32
#include "SDL_renderer_sw.h"
slouken@1361
    33
#include "../events/SDL_sysevents.h"
slouken@1361
    34
#include "../events/SDL_events_c.h"
slouken@0
    35
hfutrell@2745
    36
#if SDL_VIDEO_OPENGL_ES
hfutrell@2745
    37
#include "SDL_opengles.h"
hfutrell@2745
    38
#endif				/* SDL_VIDEO_OPENGL_ES */
hfutrell@2745
    39
slouken@1926
    40
#if SDL_VIDEO_OPENGL
slouken@1926
    41
#include "SDL_opengl.h"
slouken@1926
    42
slouken@1926
    43
/* On Windows, windows.h defines CreateWindow */
slouken@1926
    44
#ifdef CreateWindow
slouken@1926
    45
#undef CreateWindow
slouken@1926
    46
#endif
hfutrell@2744
    47
#endif				/* SDL_VIDEO_OPENGL */
slouken@1926
    48
slouken@0
    49
/* Available video drivers */
slouken@0
    50
static VideoBootStrap *bootstrap[] = {
slouken@1931
    51
#if SDL_VIDEO_DRIVER_COCOA
hfutrell@2744
    52
	&COCOA_bootstrap,
icculus@1133
    53
#endif
slouken@1361
    54
#if SDL_VIDEO_DRIVER_X11
hfutrell@2744
    55
	&X11_bootstrap,
slouken@0
    56
#endif
slouken@1361
    57
#if SDL_VIDEO_DRIVER_NANOX
hfutrell@2744
    58
	&NX_bootstrap,
slouken@30
    59
#endif
slouken@1361
    60
#if SDL_VIDEO_DRIVER_IPOD
hfutrell@2744
    61
	&iPod_bootstrap,
icculus@1140
    62
#endif
slouken@1361
    63
#if SDL_VIDEO_DRIVER_WSCONS
hfutrell@2744
    64
	&WSCONS_bootstrap,
icculus@1187
    65
#endif
slouken@1361
    66
#if SDL_VIDEO_DRIVER_FBCON
hfutrell@2744
    67
	&FBCON_bootstrap,
slouken@0
    68
#endif
slouken@1361
    69
#if SDL_VIDEO_DRIVER_DIRECTFB
hfutrell@2744
    70
	&DirectFB_bootstrap,
slouken@167
    71
#endif
slouken@1361
    72
#if SDL_VIDEO_DRIVER_PS2GS
hfutrell@2744
    73
	&PS2GS_bootstrap,
slouken@0
    74
#endif
slouken@1361
    75
#if SDL_VIDEO_DRIVER_VGL
hfutrell@2744
    76
	&VGL_bootstrap,
slouken@75
    77
#endif
slouken@1361
    78
#if SDL_VIDEO_DRIVER_SVGALIB
hfutrell@2744
    79
	&SVGALIB_bootstrap,
slouken@0
    80
#endif
slouken@1361
    81
#if SDL_VIDEO_DRIVER_GAPI
hfutrell@2744
    82
	&GAPI_bootstrap,
slouken@1361
    83
#endif
slouken@1895
    84
#if SDL_VIDEO_DRIVER_WIN32
hfutrell@2744
    85
	&WIN32_bootstrap,
slouken@1361
    86
#endif
slouken@1361
    87
#if SDL_VIDEO_DRIVER_BWINDOW
hfutrell@2744
    88
	&BWINDOW_bootstrap,
slouken@1361
    89
#endif
slouken@1361
    90
#if SDL_VIDEO_DRIVER_PHOTON
hfutrell@2744
    91
	&ph_bootstrap,
slouken@1361
    92
#endif
slouken@1361
    93
#if SDL_VIDEO_DRIVER_EPOC
hfutrell@2744
    94
	&EPOC_bootstrap,
slouken@1361
    95
#endif
slouken@1361
    96
#if SDL_VIDEO_DRIVER_XBIOS
hfutrell@2744
    97
	&XBIOS_bootstrap,
slouken@1361
    98
#endif
slouken@1361
    99
#if SDL_VIDEO_DRIVER_GEM
hfutrell@2744
   100
	&GEM_bootstrap,
slouken@1361
   101
#endif
slouken@1361
   102
#if SDL_VIDEO_DRIVER_DC
hfutrell@2744
   103
	&DC_bootstrap,
slouken@1361
   104
#endif
slouken@1361
   105
#if SDL_VIDEO_DRIVER_RISCOS
hfutrell@2744
   106
	&RISCOS_bootstrap,
slouken@1361
   107
#endif
slouken@1361
   108
#if SDL_VIDEO_DRIVER_OS2FS
hfutrell@2744
   109
	&OS2FSLib_bootstrap,
slouken@1361
   110
#endif
hfutrell@2743
   111
#if SDL_VIDEO_DRIVER_NDS
hfutrell@2744
   112
	&NDS_bootstrap,
slouken@2735
   113
#endif
hfutrell@2745
   114
#if SDL_VIDEO_DRIVER_UIKIT
hfutrell@2745
   115
	&UIKIT_bootstrap,
hfutrell@2745
   116
#endif
slouken@1361
   117
#if SDL_VIDEO_DRIVER_DUMMY
hfutrell@2744
   118
	&DUMMY_bootstrap,
slouken@610
   119
#endif
hfutrell@2744
   120
	NULL
slouken@0
   121
};
slouken@173
   122
slouken@1895
   123
static SDL_VideoDevice *_this = NULL;
slouken@0
   124
slouken@0
   125
/* Various local functions */
hfutrell@2744
   126
int             SDL_VideoInit(const char *driver_name, Uint32 flags);
hfutrell@2744
   127
void            SDL_VideoQuit(void);
slouken@0
   128
slouken@1895
   129
static int
slouken@1895
   130
cmpmodes(const void *A, const void *B)
slouken@1895
   131
{
hfutrell@2744
   132
	SDL_DisplayMode a = *(const SDL_DisplayMode *) A;
hfutrell@2744
   133
	SDL_DisplayMode b = *(const SDL_DisplayMode *) B;
slouken@0
   134
hfutrell@2744
   135
	if (a.w != b.w) {
hfutrell@2744
   136
		return b.w - a.w;
hfutrell@2744
   137
	}
hfutrell@2744
   138
	if (a.h != b.h) {
hfutrell@2744
   139
		return b.h - a.h;
hfutrell@2744
   140
	}
hfutrell@2744
   141
	if (SDL_BITSPERPIXEL(a.format) != SDL_BITSPERPIXEL(b.format)) {
hfutrell@2744
   142
		return SDL_BITSPERPIXEL(b.format) - SDL_BITSPERPIXEL(a.format);
hfutrell@2744
   143
	}
hfutrell@2744
   144
	if (a.refresh_rate != b.refresh_rate) {
hfutrell@2744
   145
		return b.refresh_rate - a.refresh_rate;
hfutrell@2744
   146
	}
hfutrell@2744
   147
	return 0;
slouken@1895
   148
}
slouken@1895
   149
slouken@1912
   150
static void
slouken@1912
   151
SDL_UninitializedVideo()
slouken@1912
   152
{
hfutrell@2744
   153
	SDL_SetError("Video subsystem has not been initialized");
slouken@1912
   154
}
slouken@1912
   155
slouken@1895
   156
int
slouken@1895
   157
SDL_GetNumVideoDrivers(void)
slouken@1895
   158
{
hfutrell@2744
   159
	return SDL_arraysize(bootstrap) - 1;
slouken@1895
   160
}
slouken@1895
   161
hfutrell@2744
   162
const char     *
slouken@1895
   163
SDL_GetVideoDriver(int index)
slouken@1895
   164
{
hfutrell@2744
   165
	if (index >= 0 && index < SDL_GetNumVideoDrivers()) {
hfutrell@2744
   166
		return bootstrap[index]->name;
hfutrell@2744
   167
	}
hfutrell@2744
   168
	return NULL;
slouken@1895
   169
}
slouken@0
   170
slouken@0
   171
/*
slouken@0
   172
 * Initialize the video and event subsystems -- determine native pixel format
slouken@0
   173
 */
slouken@1895
   174
int
slouken@1895
   175
SDL_VideoInit(const char *driver_name, Uint32 flags)
slouken@0
   176
{
hfutrell@2744
   177
	SDL_VideoDevice *video;
hfutrell@2744
   178
	int             index;
hfutrell@2744
   179
	int             i;
slouken@0
   180
hfutrell@2744
   181
	/* Toggle the event thread flags, based on OS requirements */
slouken@0
   182
#if defined(MUST_THREAD_EVENTS)
hfutrell@2744
   183
	flags |= SDL_INIT_EVENTTHREAD;
slouken@0
   184
#elif defined(CANT_THREAD_EVENTS)
hfutrell@2744
   185
	if ((flags & SDL_INIT_EVENTTHREAD) == SDL_INIT_EVENTTHREAD) {
hfutrell@2744
   186
		SDL_SetError("OS doesn't support threaded events");
hfutrell@2744
   187
		return -1;
hfutrell@2744
   188
	}
slouken@0
   189
#endif
slouken@0
   190
hfutrell@2744
   191
	/* Start the event loop */
hfutrell@2744
   192
	if (SDL_StartEventLoop(flags) < 0) {
hfutrell@2744
   193
		return -1;
hfutrell@2744
   194
	}
hfutrell@2744
   195
	/* Check to make sure we don't overwrite '_this' */
hfutrell@2744
   196
	if (_this != NULL) {
hfutrell@2744
   197
		SDL_VideoQuit();
hfutrell@2744
   198
	}
hfutrell@2744
   199
	/* Select the proper video driver */
hfutrell@2744
   200
	index = 0;
hfutrell@2744
   201
	video = NULL;
hfutrell@2744
   202
	if (driver_name == NULL) {
hfutrell@2744
   203
		driver_name = SDL_getenv("SDL_VIDEODRIVER");
hfutrell@2744
   204
	}
hfutrell@2744
   205
	if (driver_name != NULL) {
hfutrell@2744
   206
		for (i = 0; bootstrap[i]; ++i) {
hfutrell@2744
   207
			if (SDL_strcasecmp(bootstrap[i]->name, driver_name) == 0) {
hfutrell@2744
   208
				if (bootstrap[i]->available()) {
hfutrell@2744
   209
					video = bootstrap[i]->create(index);
hfutrell@2744
   210
				}
hfutrell@2744
   211
				break;
hfutrell@2744
   212
			}
hfutrell@2744
   213
		}
hfutrell@2744
   214
	} else {
hfutrell@2744
   215
		for (i = 0; bootstrap[i]; ++i) {
hfutrell@2744
   216
			if (bootstrap[i]->available()) {
hfutrell@2744
   217
				video = bootstrap[i]->create(index);
hfutrell@2744
   218
				if (video != NULL) {
hfutrell@2744
   219
					break;
hfutrell@2744
   220
				}
hfutrell@2744
   221
			}
hfutrell@2744
   222
		}
hfutrell@2744
   223
	}
hfutrell@2744
   224
	if (video == NULL) {
hfutrell@2744
   225
		if (driver_name) {
hfutrell@2744
   226
			SDL_SetError("%s not available", driver_name);
hfutrell@2744
   227
		} else {
hfutrell@2744
   228
			SDL_SetError("No available video device");
hfutrell@2744
   229
		}
hfutrell@2744
   230
		return -1;
hfutrell@2744
   231
	}
hfutrell@2744
   232
	_this = video;
hfutrell@2744
   233
	_this->name = bootstrap[i]->name;
hfutrell@2744
   234
	_this->next_object_id = 1;
slouken@0
   235
slouken@0
   236
hfutrell@2744
   237
	/* Set some very sane GL defaults */
hfutrell@2744
   238
	_this->gl_config.driver_loaded = 0;
hfutrell@2744
   239
	_this->gl_config.dll_handle = NULL;
hfutrell@2744
   240
	_this->gl_config.red_size = 3;
hfutrell@2744
   241
	_this->gl_config.green_size = 3;
hfutrell@2744
   242
	_this->gl_config.blue_size = 2;
hfutrell@2744
   243
	_this->gl_config.alpha_size = 0;
hfutrell@2744
   244
	_this->gl_config.buffer_size = 0;
hfutrell@2744
   245
	_this->gl_config.depth_size = 16;
hfutrell@2744
   246
	_this->gl_config.stencil_size = 0;
hfutrell@2744
   247
	_this->gl_config.double_buffer = 1;
hfutrell@2744
   248
	_this->gl_config.accum_red_size = 0;
hfutrell@2744
   249
	_this->gl_config.accum_green_size = 0;
hfutrell@2744
   250
	_this->gl_config.accum_blue_size = 0;
hfutrell@2744
   251
	_this->gl_config.accum_alpha_size = 0;
hfutrell@2744
   252
	_this->gl_config.stereo = 0;
hfutrell@2744
   253
	_this->gl_config.multisamplebuffers = 0;
hfutrell@2744
   254
	_this->gl_config.multisamplesamples = 0;
hfutrell@2745
   255
	_this->gl_config.retained_backing = 1;
hfutrell@2744
   256
	_this->gl_config.accelerated = -1;	/* not known, don't set */
slouken@0
   257
hfutrell@2744
   258
	/* Initialize the video subsystem */
hfutrell@2744
   259
	if (_this->VideoInit(_this) < 0) {
hfutrell@2744
   260
		SDL_VideoQuit();
hfutrell@2744
   261
		return -1;
hfutrell@2744
   262
	}
hfutrell@2744
   263
	/* Make sure some displays were added */
hfutrell@2744
   264
	if (_this->num_displays == 0) {
hfutrell@2744
   265
		SDL_SetError("The video driver did not add any displays");
hfutrell@2744
   266
		SDL_VideoQuit();
hfutrell@2744
   267
		return (-1);
hfutrell@2744
   268
	}
hfutrell@2744
   269
	/* The software renderer is always available */
hfutrell@2744
   270
	for (i = 0; i < _this->num_displays; ++i) {
hfutrell@2744
   271
#if SDL_VIDEO_RENDER_OGL
hfutrell@2744
   272
		SDL_AddRenderDriver(i, &GL_RenderDriver);
hfutrell@2744
   273
#endif
hfutrell@2745
   274
hfutrell@2745
   275
#if SDL_VIDEO_RENDER_OGL_ES
hfutrell@2745
   276
		SDL_AddRenderDriver(i, &GL_ES_RenderDriver);
hfutrell@2745
   277
#endif
hfutrell@2744
   278
		if (_this->displays[i].num_render_drivers > 0) {
hfutrell@2744
   279
			SDL_AddRenderDriver(i, &SW_RenderDriver);
hfutrell@2744
   280
		}
hfutrell@2744
   281
	}
slouken@0
   282
hfutrell@2744
   283
	/* We're ready to go! */
hfutrell@2744
   284
	return 0;
slouken@0
   285
}
slouken@0
   286
hfutrell@2744
   287
const char     *
slouken@1895
   288
SDL_GetCurrentVideoDriver()
slouken@0
   289
{
hfutrell@2744
   290
	if (!_this) {
hfutrell@2744
   291
		SDL_UninitializedVideo();
hfutrell@2744
   292
		return NULL;
hfutrell@2744
   293
	}
hfutrell@2744
   294
	return _this->name;
slouken@0
   295
}
slouken@0
   296
slouken@1895
   297
SDL_VideoDevice *
slouken@1895
   298
SDL_GetVideoDevice()
slouken@0
   299
{
hfutrell@2744
   300
	return _this;
slouken@0
   301
}
slouken@0
   302
slouken@1895
   303
int
slouken@1895
   304
SDL_AddBasicVideoDisplay(const SDL_DisplayMode * desktop_mode)
slouken@0
   305
{
hfutrell@2744
   306
	SDL_VideoDisplay display;
slouken@0
   307
hfutrell@2744
   308
	SDL_zero(display);
hfutrell@2744
   309
	if (desktop_mode) {
hfutrell@2744
   310
		display.desktop_mode = *desktop_mode;
hfutrell@2744
   311
	}
hfutrell@2744
   312
	display.current_mode = display.desktop_mode;
slouken@1895
   313
hfutrell@2744
   314
	return SDL_AddVideoDisplay(&display);
slouken@0
   315
}
slouken@0
   316
slouken@1895
   317
int
slouken@1895
   318
SDL_AddVideoDisplay(const SDL_VideoDisplay * display)
slouken@0
   319
{
hfutrell@2744
   320
	SDL_VideoDisplay *displays;
hfutrell@2744
   321
	int             index = -1;
slouken@0
   322
hfutrell@2744
   323
	displays =
hfutrell@2744
   324
		SDL_realloc(_this->displays,
hfutrell@2744
   325
			    (_this->num_displays + 1) * sizeof(*displays));
hfutrell@2744
   326
	if (displays) {
hfutrell@2744
   327
		index = _this->num_displays++;
hfutrell@2744
   328
		displays[index] = *display;
hfutrell@2744
   329
		displays[index].device = _this;
hfutrell@2744
   330
		_this->displays = displays;
hfutrell@2744
   331
	} else {
hfutrell@2744
   332
		SDL_OutOfMemory();
hfutrell@2744
   333
	}
hfutrell@2744
   334
	return index;
slouken@0
   335
}
slouken@0
   336
slouken@1895
   337
int
slouken@1895
   338
SDL_GetNumVideoDisplays(void)
slouken@0
   339
{
hfutrell@2744
   340
	if (!_this) {
hfutrell@2744
   341
		SDL_UninitializedVideo();
hfutrell@2744
   342
		return 0;
hfutrell@2744
   343
	}
hfutrell@2744
   344
	return _this->num_displays;
slouken@0
   345
}
slouken@0
   346
slouken@1895
   347
int
slouken@1895
   348
SDL_SelectVideoDisplay(int index)
slouken@0
   349
{
hfutrell@2744
   350
	if (!_this) {
hfutrell@2744
   351
		SDL_UninitializedVideo();
hfutrell@2744
   352
		return (-1);
hfutrell@2744
   353
	}
hfutrell@2744
   354
	if (index < 0 || index >= _this->num_displays) {
hfutrell@2744
   355
		SDL_SetError("index must be in the range 0 - %d",
hfutrell@2744
   356
			     _this->num_displays - 1);
hfutrell@2744
   357
		return -1;
hfutrell@2744
   358
	}
hfutrell@2744
   359
	_this->current_display = index;
hfutrell@2744
   360
	return 0;
slouken@1963
   361
}
slouken@1963
   362
slouken@1963
   363
int
slouken@1963
   364
SDL_GetCurrentVideoDisplay(void)
slouken@1963
   365
{
hfutrell@2744
   366
	if (!_this) {
hfutrell@2744
   367
		SDL_UninitializedVideo();
hfutrell@2744
   368
		return (-1);
hfutrell@2744
   369
	}
hfutrell@2744
   370
	return _this->current_display;
slouken@0
   371
}
slouken@0
   372
slouken@1895
   373
SDL_bool
slouken@1895
   374
SDL_AddDisplayMode(int displayIndex, const SDL_DisplayMode * mode)
slouken@0
   375
{
hfutrell@2744
   376
	SDL_VideoDisplay *display = &_this->displays[displayIndex];
hfutrell@2744
   377
	SDL_DisplayMode *modes;
hfutrell@2744
   378
	int             i, nmodes;
slouken@0
   379
hfutrell@2744
   380
	/* Make sure we don't already have the mode in the list */
hfutrell@2744
   381
	modes = display->display_modes;
hfutrell@2744
   382
	nmodes = display->num_display_modes;
hfutrell@2744
   383
	for (i = nmodes; i--;) {
hfutrell@2744
   384
		if (SDL_memcmp(mode, &modes[i], sizeof(*mode)) == 0) {
hfutrell@2744
   385
			return SDL_FALSE;
hfutrell@2744
   386
		}
hfutrell@2744
   387
	}
slouken@1895
   388
hfutrell@2744
   389
	/* Go ahead and add the new mode */
hfutrell@2744
   390
	if (nmodes == display->max_display_modes) {
hfutrell@2744
   391
		modes =
hfutrell@2744
   392
			SDL_realloc(modes,
hfutrell@2744
   393
			(display->max_display_modes + 32) * sizeof(*modes));
hfutrell@2744
   394
		if (!modes) {
hfutrell@2744
   395
			return SDL_FALSE;
hfutrell@2744
   396
		}
hfutrell@2744
   397
		display->display_modes = modes;
hfutrell@2744
   398
		display->max_display_modes += 32;
hfutrell@2744
   399
	}
hfutrell@2744
   400
	modes[nmodes] = *mode;
hfutrell@2744
   401
	display->num_display_modes++;
slouken@1895
   402
hfutrell@2744
   403
	return SDL_TRUE;
slouken@0
   404
}
slouken@0
   405
slouken@1895
   406
int
slouken@1895
   407
SDL_GetNumDisplayModes()
slouken@0
   408
{
hfutrell@2744
   409
	if (_this) {
hfutrell@2744
   410
		SDL_VideoDisplay *display = &SDL_CurrentDisplay;
hfutrell@2744
   411
		if (!display->num_display_modes && _this->GetDisplayModes) {
hfutrell@2744
   412
			_this->GetDisplayModes(_this);
hfutrell@2744
   413
			SDL_qsort(display->display_modes, display->num_display_modes,
hfutrell@2744
   414
				  sizeof(SDL_DisplayMode), cmpmodes);
hfutrell@2744
   415
		}
hfutrell@2744
   416
		return display->num_display_modes;
hfutrell@2744
   417
	}
hfutrell@2744
   418
	return 0;
slouken@0
   419
}
slouken@0
   420
slouken@1967
   421
int
slouken@1967
   422
SDL_GetDisplayMode(int index, SDL_DisplayMode * mode)
slouken@0
   423
{
hfutrell@2744
   424
	if (index < 0 || index >= SDL_GetNumDisplayModes()) {
hfutrell@2744
   425
		SDL_SetError("index must be in the range of 0 - %d",
hfutrell@2744
   426
			     SDL_GetNumDisplayModes() - 1);
hfutrell@2744
   427
		return -1;
hfutrell@2744
   428
	}
hfutrell@2744
   429
	if (mode) {
hfutrell@2744
   430
		*mode = SDL_CurrentDisplay.display_modes[index];
hfutrell@2744
   431
	}
hfutrell@2744
   432
	return 0;
slouken@0
   433
}
slouken@0
   434
slouken@1967
   435
int
slouken@1967
   436
SDL_GetDesktopDisplayMode(SDL_DisplayMode * mode)
slouken@0
   437
{
hfutrell@2744
   438
	if (!_this) {
hfutrell@2744
   439
		SDL_UninitializedVideo();
hfutrell@2744
   440
		return -1;
hfutrell@2744
   441
	}
hfutrell@2744
   442
	if (mode) {
hfutrell@2744
   443
		*mode = SDL_CurrentDisplay.desktop_mode;
hfutrell@2744
   444
	}
hfutrell@2744
   445
	return 0;
slouken@0
   446
}
slouken@0
   447
slouken@1967
   448
int
slouken@1967
   449
SDL_GetCurrentDisplayMode(SDL_DisplayMode * mode)
slouken@0
   450
{
hfutrell@2744
   451
	if (!_this) {
hfutrell@2744
   452
		SDL_UninitializedVideo();
hfutrell@2744
   453
		return -1;
hfutrell@2744
   454
	}
hfutrell@2744
   455
	if (mode) {
hfutrell@2744
   456
		*mode = SDL_CurrentDisplay.current_mode;
hfutrell@2744
   457
	}
hfutrell@2744
   458
	return 0;
slouken@0
   459
}
slouken@0
   460
slouken@1895
   461
SDL_DisplayMode *
slouken@1895
   462
SDL_GetClosestDisplayMode(const SDL_DisplayMode * mode,
hfutrell@2744
   463
			  SDL_DisplayMode * closest)
slouken@0
   464
{
hfutrell@2744
   465
	Uint32          target_format;
hfutrell@2744
   466
	int             target_refresh_rate;
hfutrell@2744
   467
	int             i;
hfutrell@2744
   468
	SDL_DisplayMode *current, *match;
slouken@0
   469
hfutrell@2744
   470
	if (!_this || !mode || !closest) {
hfutrell@2744
   471
		return NULL;
hfutrell@2744
   472
	}
hfutrell@2744
   473
	/* Default to the desktop format */
hfutrell@2744
   474
	if (mode->format) {
hfutrell@2744
   475
		target_format = mode->format;
hfutrell@2744
   476
	} else {
hfutrell@2744
   477
		target_format = SDL_CurrentDisplay.desktop_mode.format;
hfutrell@2744
   478
	}
slouken@0
   479
hfutrell@2744
   480
	/* Default to the desktop refresh rate */
hfutrell@2744
   481
	if (mode->refresh_rate) {
hfutrell@2744
   482
		target_refresh_rate = mode->refresh_rate;
hfutrell@2744
   483
	} else {
hfutrell@2744
   484
		target_refresh_rate = SDL_CurrentDisplay.desktop_mode.refresh_rate;
hfutrell@2744
   485
	}
slouken@0
   486
hfutrell@2744
   487
	match = NULL;
hfutrell@2744
   488
	for (i = 0; i < SDL_GetNumDisplayModes(); ++i) {
hfutrell@2744
   489
		current = &SDL_CurrentDisplay.display_modes[i];
slouken@0
   490
hfutrell@2744
   491
		if ((current->w && current->h) &&
hfutrell@2744
   492
		    (current->w < mode->w || current->h < mode->h)) {
hfutrell@2744
   493
			/* Out of sorted modes large enough here */
hfutrell@2744
   494
			break;
hfutrell@2744
   495
		}
hfutrell@2744
   496
		if (!match || current->w < match->w || current->h < match->h) {
hfutrell@2744
   497
			match = current;
hfutrell@2744
   498
			continue;
hfutrell@2744
   499
		}
hfutrell@2744
   500
		if (current->format != match->format) {
hfutrell@2744
   501
			/* Sorted highest depth to lowest */
hfutrell@2744
   502
			if (current->format == target_format ||
hfutrell@2744
   503
			    (SDL_BITSPERPIXEL(current->format) >=
hfutrell@2744
   504
			     SDL_BITSPERPIXEL(target_format)
hfutrell@2744
   505
			     && SDL_PIXELTYPE(current->format) ==
hfutrell@2744
   506
			     SDL_PIXELTYPE(target_format))) {
hfutrell@2744
   507
				match = current;
hfutrell@2744
   508
			}
hfutrell@2744
   509
			continue;
hfutrell@2744
   510
		}
hfutrell@2744
   511
		if (current->refresh_rate != match->refresh_rate) {
hfutrell@2744
   512
			/* Sorted highest refresh to lowest */
hfutrell@2744
   513
			if (current->refresh_rate >= target_refresh_rate) {
hfutrell@2744
   514
				match = current;
hfutrell@2744
   515
			}
hfutrell@2744
   516
		}
hfutrell@2744
   517
	}
hfutrell@2744
   518
	if (match) {
hfutrell@2744
   519
		if (match->format) {
hfutrell@2744
   520
			closest->format = match->format;
hfutrell@2744
   521
		} else {
hfutrell@2744
   522
			closest->format = mode->format;
hfutrell@2744
   523
		}
hfutrell@2744
   524
		if (match->w && match->h) {
hfutrell@2744
   525
			closest->w = match->w;
hfutrell@2744
   526
			closest->h = match->h;
hfutrell@2744
   527
		} else {
hfutrell@2744
   528
			closest->w = mode->w;
hfutrell@2744
   529
			closest->h = mode->h;
hfutrell@2744
   530
		}
hfutrell@2744
   531
		if (match->refresh_rate) {
hfutrell@2744
   532
			closest->refresh_rate = match->refresh_rate;
hfutrell@2744
   533
		} else {
hfutrell@2744
   534
			closest->refresh_rate = mode->refresh_rate;
hfutrell@2744
   535
		}
hfutrell@2744
   536
		closest->driverdata = match->driverdata;
slouken@1895
   537
hfutrell@2744
   538
		/*
hfutrell@2744
   539
		 * Pick some reasonable defaults if the app and driver don't
hfutrell@2744
   540
		 * care
hfutrell@2744
   541
		 */
hfutrell@2744
   542
		if (!closest->format) {
hfutrell@2744
   543
			closest->format = SDL_PIXELFORMAT_RGB888;
hfutrell@2744
   544
		}
hfutrell@2744
   545
		if (!closest->w) {
hfutrell@2744
   546
			closest->w = 640;
hfutrell@2744
   547
		}
hfutrell@2744
   548
		if (!closest->h) {
hfutrell@2744
   549
			closest->h = 480;
hfutrell@2744
   550
		}
hfutrell@2744
   551
		return closest;
hfutrell@2744
   552
	}
hfutrell@2744
   553
	return NULL;
slouken@0
   554
}
slouken@0
   555
slouken@1895
   556
int
slouken@1895
   557
SDL_SetDisplayMode(const SDL_DisplayMode * mode)
slouken@0
   558
{
hfutrell@2744
   559
	SDL_VideoDisplay *display;
hfutrell@2744
   560
	SDL_DisplayMode display_mode;
hfutrell@2744
   561
	SDL_DisplayMode current_mode;
hfutrell@2744
   562
	int             i, ncolors;
slouken@0
   563
hfutrell@2744
   564
	if (!_this) {
hfutrell@2744
   565
		SDL_UninitializedVideo();
hfutrell@2744
   566
		return -1;
hfutrell@2744
   567
	}
hfutrell@2744
   568
	display = &SDL_CurrentDisplay;
hfutrell@2744
   569
	if (!mode) {
hfutrell@2744
   570
		mode = &display->desktop_mode;
hfutrell@2744
   571
	}
hfutrell@2744
   572
	display_mode = *mode;
slouken@0
   573
hfutrell@2744
   574
	/* Default to the current mode */
hfutrell@2744
   575
	if (!display_mode.format) {
hfutrell@2744
   576
		display_mode.format = display->current_mode.format;
hfutrell@2744
   577
	}
hfutrell@2744
   578
	if (!display_mode.w) {
hfutrell@2744
   579
		display_mode.w = display->current_mode.w;
hfutrell@2744
   580
	}
hfutrell@2744
   581
	if (!display_mode.h) {
hfutrell@2744
   582
		display_mode.h = display->current_mode.h;
hfutrell@2744
   583
	}
hfutrell@2744
   584
	if (!display_mode.refresh_rate) {
hfutrell@2744
   585
		display_mode.refresh_rate = display->current_mode.refresh_rate;
hfutrell@2744
   586
	}
hfutrell@2744
   587
	/* Get a good video mode, the closest one possible */
hfutrell@2744
   588
	if (!SDL_GetClosestDisplayMode(&display_mode, &display_mode)) {
hfutrell@2744
   589
		SDL_SetError("No video mode large enough for %dx%d",
hfutrell@2744
   590
			     display_mode.w, display_mode.h);
hfutrell@2744
   591
		return -1;
hfutrell@2744
   592
	}
hfutrell@2744
   593
	/* See if there's anything left to do */
hfutrell@2744
   594
	SDL_GetCurrentDisplayMode(&current_mode);
hfutrell@2744
   595
	if (SDL_memcmp(&display_mode, &current_mode, sizeof(display_mode)) == 0) {
hfutrell@2744
   596
		return 0;
hfutrell@2744
   597
	}
hfutrell@2744
   598
	/* Actually change the display mode */
hfutrell@2744
   599
	if (_this->SetDisplayMode(_this, &display_mode) < 0) {
hfutrell@2744
   600
		return -1;
hfutrell@2744
   601
	}
hfutrell@2744
   602
	display->current_mode = display_mode;
slouken@1895
   603
hfutrell@2744
   604
	/* Set up a palette, if necessary */
hfutrell@2744
   605
	if (SDL_ISPIXELFORMAT_INDEXED(display_mode.format)) {
hfutrell@2744
   606
		ncolors = (1 << SDL_BITSPERPIXEL(display_mode.format));
hfutrell@2744
   607
	} else {
hfutrell@2744
   608
		ncolors = 0;
hfutrell@2744
   609
	}
hfutrell@2744
   610
	if ((!ncolors && display->palette) || (ncolors && !display->palette)
hfutrell@2744
   611
	    || (ncolors && ncolors != display->palette->ncolors)) {
hfutrell@2744
   612
		if (display->palette) {
hfutrell@2744
   613
			SDL_FreePalette(display->palette);
hfutrell@2744
   614
			display->palette = NULL;
hfutrell@2744
   615
		}
hfutrell@2744
   616
		if (ncolors) {
hfutrell@2744
   617
			display->palette = SDL_AllocPalette(ncolors);
hfutrell@2744
   618
			if (!display->palette) {
hfutrell@2744
   619
				return -1;
hfutrell@2744
   620
			}
hfutrell@2744
   621
			SDL_DitherColors(display->palette->colors,
hfutrell@2744
   622
				     SDL_BITSPERPIXEL(display_mode.format));
hfutrell@2744
   623
		}
hfutrell@2744
   624
	}
hfutrell@2744
   625
	/* Move any fullscreen windows into position */
hfutrell@2744
   626
	for (i = 0; i < display->num_windows; ++i) {
hfutrell@2744
   627
		SDL_Window     *window = &display->windows[i];
hfutrell@2744
   628
		if (FULLSCREEN_VISIBLE(window)) {
hfutrell@2744
   629
			SDL_SetWindowPosition(window->id, SDL_WINDOWPOS_CENTERED,
hfutrell@2744
   630
					      SDL_WINDOWPOS_CENTERED);
hfutrell@2744
   631
		}
hfutrell@2744
   632
	}
slouken@1895
   633
hfutrell@2744
   634
	return 0;
slouken@0
   635
}
slouken@0
   636
slouken@1895
   637
int
slouken@1895
   638
SDL_SetFullscreenDisplayMode(const SDL_DisplayMode * mode)
slouken@0
   639
{
hfutrell@2744
   640
	SDL_VideoDisplay *display;
hfutrell@2744
   641
	SDL_DisplayMode fullscreen_mode;
hfutrell@2744
   642
	int             i;
slouken@0
   643
hfutrell@2744
   644
	if (!_this) {
hfutrell@2744
   645
		SDL_UninitializedVideo();
hfutrell@2744
   646
		return -1;
hfutrell@2744
   647
	}
hfutrell@2744
   648
	display = &SDL_CurrentDisplay;
hfutrell@2744
   649
	if (!mode) {
hfutrell@2744
   650
		mode = &display->desktop_mode;
hfutrell@2744
   651
	}
hfutrell@2744
   652
	SDL_GetClosestDisplayMode(mode, &fullscreen_mode);
hfutrell@2744
   653
	if (SDL_memcmp
hfutrell@2744
   654
	    (&fullscreen_mode, &display->fullscreen_mode,
hfutrell@2744
   655
	     sizeof(fullscreen_mode)) == 0) {
hfutrell@2744
   656
		/* Nothing to do... */
hfutrell@2744
   657
		return 0;
hfutrell@2744
   658
	}
hfutrell@2744
   659
	display->fullscreen_mode = fullscreen_mode;
slouken@0
   660
hfutrell@2744
   661
	/* Actually set the mode if we have a fullscreen window visible */
hfutrell@2744
   662
	for (i = 0; i < display->num_windows; ++i) {
hfutrell@2744
   663
		SDL_Window     *window = &display->windows[i];
hfutrell@2744
   664
		if (FULLSCREEN_VISIBLE(window)) {
hfutrell@2744
   665
			if (SDL_SetDisplayMode(&display->fullscreen_mode) < 0) {
hfutrell@2744
   666
				return -1;
hfutrell@2744
   667
			}
hfutrell@2744
   668
		}
hfutrell@2744
   669
		if (window->flags & SDL_WINDOW_FULLSCREEN) {
hfutrell@2744
   670
			SDL_OnWindowResized(window);
hfutrell@2744
   671
		}
hfutrell@2744
   672
	}
hfutrell@2744
   673
	return 0;
slouken@0
   674
}
slouken@0
   675
slouken@1967
   676
int
slouken@1967
   677
SDL_GetFullscreenDisplayMode(SDL_DisplayMode * mode)
slouken@0
   678
{
hfutrell@2744
   679
	if (!_this) {
hfutrell@2744
   680
		SDL_UninitializedVideo();
hfutrell@2744
   681
		return -1;
hfutrell@2744
   682
	}
hfutrell@2744
   683
	if (mode) {
hfutrell@2744
   684
		*mode = SDL_CurrentDisplay.fullscreen_mode;
hfutrell@2744
   685
	}
hfutrell@2744
   686
	return 0;
slouken@0
   687
}
slouken@0
   688
slouken@1895
   689
int
slouken@1895
   690
SDL_SetDisplayPalette(const SDL_Color * colors, int firstcolor, int ncolors)
slouken@0
   691
{
hfutrell@2744
   692
	SDL_Palette    *palette;
hfutrell@2744
   693
	int             status = 0;
slouken@0
   694
hfutrell@2744
   695
	if (!_this) {
hfutrell@2744
   696
		SDL_UninitializedVideo();
hfutrell@2744
   697
		return -1;
hfutrell@2744
   698
	}
hfutrell@2744
   699
	palette = SDL_CurrentDisplay.palette;
hfutrell@2744
   700
	if (!palette) {
hfutrell@2744
   701
		SDL_SetError("Display mode does not have a palette");
hfutrell@2744
   702
		return -1;
hfutrell@2744
   703
	}
hfutrell@2744
   704
	status = SDL_SetPaletteColors(palette, colors, firstcolor, ncolors);
slouken@0
   705
hfutrell@2744
   706
	if (_this->SetDisplayPalette) {
hfutrell@2744
   707
		if (_this->SetDisplayPalette(_this, palette) < 0) {
hfutrell@2744
   708
			status = -1;
hfutrell@2744
   709
		}
hfutrell@2744
   710
	}
hfutrell@2744
   711
	return status;
slouken@0
   712
}
slouken@0
   713
slouken@1895
   714
int
slouken@1895
   715
SDL_GetDisplayPalette(SDL_Color * colors, int firstcolor, int ncolors)
slouken@0
   716
{
hfutrell@2744
   717
	SDL_Palette    *palette;
slouken@1895
   718
hfutrell@2744
   719
	if (!_this) {
hfutrell@2744
   720
		SDL_UninitializedVideo();
hfutrell@2744
   721
		return -1;
hfutrell@2744
   722
	}
hfutrell@2744
   723
	palette = SDL_CurrentDisplay.palette;
hfutrell@2744
   724
	if (!palette->ncolors) {
hfutrell@2744
   725
		SDL_SetError("Display mode does not have a palette");
hfutrell@2744
   726
		return -1;
hfutrell@2744
   727
	}
hfutrell@2744
   728
	if (firstcolor < 0 || (firstcolor + ncolors) > palette->ncolors) {
hfutrell@2744
   729
		SDL_SetError("Palette indices are out of range");
hfutrell@2744
   730
		return -1;
hfutrell@2744
   731
	}
hfutrell@2744
   732
	SDL_memcpy(colors, &palette->colors[firstcolor],
hfutrell@2744
   733
		   ncolors * sizeof(*colors));
hfutrell@2744
   734
	return 0;
slouken@0
   735
}
slouken@0
   736
slouken@1895
   737
SDL_WindowID
slouken@1895
   738
SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
slouken@0
   739
{
hfutrell@2744
   740
	const Uint32    allowed_flags = (SDL_WINDOW_FULLSCREEN |
hfutrell@2744
   741
					 SDL_WINDOW_OPENGL |
hfutrell@2744
   742
					 SDL_WINDOW_BORDERLESS |
hfutrell@2744
   743
					 SDL_WINDOW_RESIZABLE);
hfutrell@2744
   744
	SDL_VideoDisplay *display;
hfutrell@2744
   745
	SDL_Window      window;
hfutrell@2744
   746
	int             num_windows;
hfutrell@2744
   747
	SDL_Window     *windows;
slouken@0
   748
hfutrell@2744
   749
	if (!_this) {
hfutrell@2744
   750
		SDL_UninitializedVideo();
hfutrell@2744
   751
		return 0;
hfutrell@2744
   752
	}
hfutrell@2744
   753
	if ((flags & SDL_WINDOW_OPENGL) && !_this->GL_CreateContext) {
hfutrell@2744
   754
		SDL_SetError("No OpenGL support in video driver");
hfutrell@2744
   755
		return 0;
hfutrell@2744
   756
	}
hfutrell@2744
   757
	/* Fullscreen windows don't have any window decorations */
hfutrell@2744
   758
	if (flags & SDL_WINDOW_FULLSCREEN) {
hfutrell@2744
   759
		flags |= SDL_WINDOW_BORDERLESS;
hfutrell@2744
   760
		flags &= ~SDL_WINDOW_RESIZABLE;
hfutrell@2744
   761
	}
hfutrell@2744
   762
	SDL_zero(window);
hfutrell@2744
   763
	window.id = _this->next_object_id++;
hfutrell@2744
   764
	window.x = x;
hfutrell@2744
   765
	window.y = y;
hfutrell@2744
   766
	window.w = w;
hfutrell@2744
   767
	window.h = h;
hfutrell@2744
   768
	window.flags = (flags & allowed_flags);
hfutrell@2744
   769
	window.display = _this->current_display;
slouken@1912
   770
hfutrell@2744
   771
	if (_this->CreateWindow && _this->CreateWindow(_this, &window) < 0) {
hfutrell@2744
   772
		return 0;
hfutrell@2744
   773
	}
hfutrell@2744
   774
	display = &SDL_CurrentDisplay;
hfutrell@2744
   775
	num_windows = display->num_windows;
hfutrell@2744
   776
	windows =
hfutrell@2744
   777
		SDL_realloc(display->windows, (num_windows + 1) * sizeof(*windows));
hfutrell@2744
   778
	if (!windows) {
hfutrell@2744
   779
		if (_this->DestroyWindow) {
hfutrell@2744
   780
			_this->DestroyWindow(_this, &window);
hfutrell@2744
   781
		}
hfutrell@2744
   782
		return 0;
hfutrell@2744
   783
	}
hfutrell@2744
   784
	windows[num_windows] = window;
hfutrell@2744
   785
	display->windows = windows;
hfutrell@2744
   786
	display->num_windows++;
slouken@0
   787
hfutrell@2744
   788
	if (title) {
hfutrell@2744
   789
		SDL_SetWindowTitle(window.id, title);
hfutrell@2744
   790
	}
hfutrell@2744
   791
	if (flags & SDL_WINDOW_MAXIMIZED) {
hfutrell@2744
   792
		SDL_MaximizeWindow(window.id);
hfutrell@2744
   793
	}
hfutrell@2744
   794
	if (flags & SDL_WINDOW_MINIMIZED) {
hfutrell@2744
   795
		SDL_MinimizeWindow(window.id);
hfutrell@2744
   796
	}
hfutrell@2744
   797
	if (flags & SDL_WINDOW_SHOWN) {
hfutrell@2744
   798
		SDL_ShowWindow(window.id);
hfutrell@2744
   799
	}
hfutrell@2744
   800
	if (flags & SDL_WINDOW_INPUT_GRABBED) {
hfutrell@2744
   801
		SDL_SetWindowGrab(window.id, 1);
hfutrell@2744
   802
	}
hfutrell@2744
   803
	return window.id;
slouken@1895
   804
}
slouken@0
   805
slouken@1895
   806
SDL_WindowID
slouken@1895
   807
SDL_CreateWindowFrom(const void *data)
slouken@1895
   808
{
hfutrell@2744
   809
	SDL_VideoDisplay *display;
hfutrell@2744
   810
	SDL_Window      window;
hfutrell@2744
   811
	int             num_windows;
hfutrell@2744
   812
	SDL_Window     *windows;
slouken@0
   813
hfutrell@2744
   814
	if (!_this) {
hfutrell@2744
   815
		SDL_UninitializedVideo();
hfutrell@2744
   816
		return (0);
hfutrell@2744
   817
	}
hfutrell@2744
   818
	SDL_zero(window);
hfutrell@2744
   819
	window.id = _this->next_object_id++;
hfutrell@2744
   820
	window.display = _this->current_display;
slouken@1895
   821
hfutrell@2744
   822
	if (!_this->CreateWindowFrom ||
hfutrell@2744
   823
	    _this->CreateWindowFrom(_this, &window, data) < 0) {
hfutrell@2744
   824
		return 0;
hfutrell@2744
   825
	}
hfutrell@2744
   826
	display = &SDL_CurrentDisplay;
hfutrell@2744
   827
	num_windows = display->num_windows;
hfutrell@2744
   828
	windows =
hfutrell@2744
   829
		SDL_realloc(display->windows, (num_windows + 1) * sizeof(*windows));
hfutrell@2744
   830
	if (!windows) {
hfutrell@2744
   831
		if (_this->DestroyWindow) {
hfutrell@2744
   832
			_this->DestroyWindow(_this, &window);
hfutrell@2744
   833
		}
hfutrell@2744
   834
		if (window.title) {
hfutrell@2744
   835
			SDL_free(window.title);
hfutrell@2744
   836
		}
hfutrell@2744
   837
		return 0;
hfutrell@2744
   838
	}
hfutrell@2744
   839
	windows[num_windows] = window;
hfutrell@2744
   840
	display->windows = windows;
hfutrell@2744
   841
	display->num_windows++;
slouken@1895
   842
hfutrell@2744
   843
	return window.id;
slouken@1895
   844
}
slouken@1895
   845
slouken@1924
   846
int
slouken@1928
   847
SDL_RecreateWindow(SDL_Window * window, Uint32 flags)
slouken@1924
   848
{
hfutrell@2744
   849
	char           *title = window->title;
slouken@1956
   850
hfutrell@2744
   851
	if ((flags & SDL_WINDOW_OPENGL) && !_this->GL_CreateContext) {
hfutrell@2744
   852
		SDL_SetError("No OpenGL support in video driver");
hfutrell@2744
   853
		return -1;
hfutrell@2744
   854
	}
hfutrell@2744
   855
	if (_this->DestroyWindow) {
hfutrell@2744
   856
		_this->DestroyWindow(_this, window);
hfutrell@2744
   857
	}
hfutrell@2744
   858
	window->title = NULL;
hfutrell@2744
   859
	window->flags =
hfutrell@2744
   860
		(flags &
hfutrell@2744
   861
	  ~(SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED | SDL_WINDOW_SHOWN |
hfutrell@2744
   862
	    SDL_WINDOW_INPUT_GRABBED));
slouken@1956
   863
hfutrell@2744
   864
	if (_this->CreateWindow && _this->CreateWindow(_this, window) < 0) {
hfutrell@2744
   865
		return -1;
hfutrell@2744
   866
	}
hfutrell@2744
   867
	if (title) {
hfutrell@2744
   868
		SDL_SetWindowTitle(window->id, title);
hfutrell@2744
   869
		SDL_free(title);
hfutrell@2744
   870
	}
hfutrell@2744
   871
	if (flags & SDL_WINDOW_MAXIMIZED) {
hfutrell@2744
   872
		SDL_MaximizeWindow(window->id);
hfutrell@2744
   873
	}
hfutrell@2744
   874
	if (flags & SDL_WINDOW_MINIMIZED) {
hfutrell@2744
   875
		SDL_MinimizeWindow(window->id);
hfutrell@2744
   876
	}
hfutrell@2744
   877
	if (flags & SDL_WINDOW_SHOWN) {
hfutrell@2744
   878
		SDL_ShowWindow(window->id);
hfutrell@2744
   879
	}
hfutrell@2744
   880
	if (flags & SDL_WINDOW_INPUT_GRABBED) {
hfutrell@2744
   881
		SDL_SetWindowGrab(window->id, 1);
hfutrell@2744
   882
	}
hfutrell@2744
   883
	return 0;
slouken@1924
   884
}
slouken@1924
   885
hfutrell@2744
   886
SDL_Window     *
slouken@1895
   887
SDL_GetWindowFromID(SDL_WindowID windowID)
slouken@1895
   888
{
hfutrell@2744
   889
	int             i, j;
slouken@1895
   890
hfutrell@2744
   891
	if (!_this) {
hfutrell@2744
   892
		SDL_UninitializedVideo();
hfutrell@2744
   893
		return NULL;
hfutrell@2744
   894
	}
hfutrell@2744
   895
	for (i = 0; i < _this->num_displays; ++i) {
hfutrell@2744
   896
		SDL_VideoDisplay *display = &_this->displays[i];
hfutrell@2744
   897
		for (j = 0; j < display->num_windows; ++j) {
hfutrell@2744
   898
			SDL_Window     *window = &display->windows[j];
hfutrell@2744
   899
			if (window->id == windowID) {
hfutrell@2744
   900
				return window;
hfutrell@2744
   901
			}
hfutrell@2744
   902
		}
hfutrell@2744
   903
	}
hfutrell@2744
   904
	return NULL;
slouken@1895
   905
}
slouken@1895
   906
slouken@1895
   907
SDL_VideoDisplay *
slouken@1895
   908
SDL_GetDisplayFromWindow(SDL_Window * window)
slouken@1895
   909
{
hfutrell@2744
   910
	if (!_this) {
hfutrell@2744
   911
		SDL_UninitializedVideo();
hfutrell@2744
   912
		return NULL;
hfutrell@2744
   913
	}
hfutrell@2744
   914
	if (!window) {
hfutrell@2744
   915
		return NULL;
hfutrell@2744
   916
	}
hfutrell@2744
   917
	return &_this->displays[window->display];
slouken@1895
   918
}
slouken@1895
   919
slouken@1895
   920
Uint32
slouken@1895
   921
SDL_GetWindowFlags(SDL_WindowID windowID)
slouken@1895
   922
{
hfutrell@2744
   923
	SDL_Window     *window = SDL_GetWindowFromID(windowID);
slouken@1895
   924
hfutrell@2744
   925
	if (!window) {
hfutrell@2744
   926
		return 0;
hfutrell@2744
   927
	}
hfutrell@2744
   928
	return window->flags;
slouken@1895
   929
}
slouken@1895
   930
slouken@1895
   931
void
slouken@1895
   932
SDL_SetWindowTitle(SDL_WindowID windowID, const char *title)
slouken@1895
   933
{
hfutrell@2744
   934
	SDL_Window     *window = SDL_GetWindowFromID(windowID);
slouken@1895
   935
hfutrell@2744
   936
	if (!window || title == window->title) {
hfutrell@2744
   937
		return;
hfutrell@2744
   938
	}
hfutrell@2744
   939
	if (window->title) {
hfutrell@2744
   940
		SDL_free(window->title);
hfutrell@2744
   941
	}
hfutrell@2744
   942
	if (title) {
hfutrell@2744
   943
		window->title = SDL_strdup(title);
hfutrell@2744
   944
	} else {
hfutrell@2744
   945
		window->title = NULL;
hfutrell@2744
   946
	}
slouken@1956
   947
hfutrell@2744
   948
	if (_this->SetWindowTitle) {
hfutrell@2744
   949
		_this->SetWindowTitle(_this, window);
hfutrell@2744
   950
	}
slouken@1895
   951
}
slouken@1895
   952
hfutrell@2744
   953
const char     *
slouken@1895
   954
SDL_GetWindowTitle(SDL_WindowID windowID)
slouken@1895
   955
{
hfutrell@2744
   956
	SDL_Window     *window = SDL_GetWindowFromID(windowID);
slouken@1895
   957
hfutrell@2744
   958
	if (!window) {
hfutrell@2744
   959
		return NULL;
hfutrell@2744
   960
	}
hfutrell@2744
   961
	return window->title;
slouken@1895
   962
}
slouken@1895
   963
slouken@1895
   964
void
slouken@1895
   965
SDL_SetWindowData(SDL_WindowID windowID, void *userdata)
slouken@1895
   966
{
hfutrell@2744
   967
	SDL_Window     *window = SDL_GetWindowFromID(windowID);
slouken@1895
   968
hfutrell@2744
   969
	if (!window) {
hfutrell@2744
   970
		return;
hfutrell@2744
   971
	}
hfutrell@2744
   972
	window->userdata = userdata;
slouken@1895
   973
}
slouken@1895
   974
hfutrell@2744
   975
void           *
slouken@1895
   976
SDL_GetWindowData(SDL_WindowID windowID)
slouken@1895
   977
{
hfutrell@2744
   978
	SDL_Window     *window = SDL_GetWindowFromID(windowID);
slouken@1895
   979
hfutrell@2744
   980
	if (!window) {
hfutrell@2744
   981
		return NULL;
hfutrell@2744
   982
	}
hfutrell@2744
   983
	return window->userdata;
slouken@1895
   984
}
slouken@1895
   985
slouken@1895
   986
void
slouken@1895
   987
SDL_SetWindowPosition(SDL_WindowID windowID, int x, int y)
slouken@1895
   988
{
hfutrell@2744
   989
	SDL_Window     *window = SDL_GetWindowFromID(windowID);
hfutrell@2744
   990
	SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@1895
   991
hfutrell@2744
   992
	if (!window) {
hfutrell@2744
   993
		return;
hfutrell@2744
   994
	}
hfutrell@2744
   995
	if (x == SDL_WINDOWPOS_CENTERED) {
hfutrell@2744
   996
		window->x = (display->current_mode.w - window->w) / 2;
hfutrell@2744
   997
	} else if (x != SDL_WINDOWPOS_UNDEFINED) {
hfutrell@2744
   998
		window->x = x;
hfutrell@2744
   999
	}
hfutrell@2744
  1000
	if (y == SDL_WINDOWPOS_CENTERED) {
hfutrell@2744
  1001
		window->y = (display->current_mode.h - window->h) / 2;
hfutrell@2744
  1002
	} else if (y != SDL_WINDOWPOS_UNDEFINED) {
hfutrell@2744
  1003
		window->y = y;
hfutrell@2744
  1004
	}
hfutrell@2744
  1005
	if (_this->SetWindowPosition) {
hfutrell@2744
  1006
		_this->SetWindowPosition(_this, window);
hfutrell@2744
  1007
	}
slouken@1895
  1008
}
slouken@1895
  1009
slouken@1895
  1010
void
slouken@1895
  1011
SDL_GetWindowPosition(SDL_WindowID windowID, int *x, int *y)
slouken@1895
  1012
{
hfutrell@2744
  1013
	SDL_Window     *window = SDL_GetWindowFromID(windowID);
slouken@1895
  1014
hfutrell@2744
  1015
	if (!window) {
hfutrell@2744
  1016
		return;
hfutrell@2744
  1017
	}
hfutrell@2744
  1018
	if (x) {
hfutrell@2744
  1019
		*x = window->x;
hfutrell@2744
  1020
	}
hfutrell@2744
  1021
	if (y) {
hfutrell@2744
  1022
		*y = window->y;
hfutrell@2744
  1023
	}
slouken@1895
  1024
}
slouken@1895
  1025
slouken@1895
  1026
void
slouken@1895
  1027
SDL_SetWindowSize(SDL_WindowID windowID, int w, int h)
slouken@1895
  1028
{
hfutrell@2744
  1029
	SDL_Window     *window = SDL_GetWindowFromID(windowID);
slouken@1895
  1030
hfutrell@2744
  1031
	if (!window) {
hfutrell@2744
  1032
		return;
hfutrell@2744
  1033
	}
hfutrell@2744
  1034
	window->w = w;
hfutrell@2744
  1035
	window->h = h;
slouken@1895
  1036
hfutrell@2744
  1037
	if (_this->SetWindowSize) {
hfutrell@2744
  1038
		_this->SetWindowSize(_this, window);
hfutrell@2744
  1039
	}
slouken@1895
  1040
}
slouken@1895
  1041
slouken@1895
  1042
void
slouken@1895
  1043
SDL_GetWindowSize(SDL_WindowID windowID, int *w, int *h)
slouken@1895
  1044
{
hfutrell@2744
  1045
	SDL_Window     *window = SDL_GetWindowFromID(windowID);
slouken@1895
  1046
hfutrell@2744
  1047
	if (!window) {
hfutrell@2744
  1048
		return;
hfutrell@2744
  1049
	}
hfutrell@2744
  1050
	if (w) {
hfutrell@2744
  1051
		*w = window->w;
hfutrell@2744
  1052
	}
hfutrell@2744
  1053
	if (h) {
hfutrell@2744
  1054
		*h = window->h;
hfutrell@2744
  1055
	}
slouken@1895
  1056
}
slouken@1895
  1057
slouken@1895
  1058
void
slouken@1895
  1059
SDL_ShowWindow(SDL_WindowID windowID)
slouken@1895
  1060
{
hfutrell@2744
  1061
	SDL_Window     *window = SDL_GetWindowFromID(windowID);
slouken@1895
  1062
hfutrell@2744
  1063
	if (!window || (window->flags & SDL_WINDOW_SHOWN)) {
hfutrell@2744
  1064
		return;
hfutrell@2744
  1065
	}
hfutrell@2744
  1066
	SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_SHOWN, 0, 0);
slouken@1895
  1067
hfutrell@2744
  1068
	if (_this->ShowWindow) {
hfutrell@2744
  1069
		_this->ShowWindow(_this, window);
hfutrell@2744
  1070
	}
slouken@1895
  1071
}
slouken@1895
  1072
slouken@1895
  1073
void
slouken@1895
  1074
SDL_HideWindow(SDL_WindowID windowID)
slouken@1895
  1075
{
hfutrell@2744
  1076
	SDL_Window     *window = SDL_GetWindowFromID(windowID);
slouken@1895
  1077
hfutrell@2744
  1078
	if (!window || !(window->flags & SDL_WINDOW_SHOWN)) {
hfutrell@2744
  1079
		return;
hfutrell@2744
  1080
	}
hfutrell@2744
  1081
	SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_HIDDEN, 0, 0);
slouken@1895
  1082
hfutrell@2744
  1083
	if (_this->HideWindow) {
hfutrell@2744
  1084
		_this->HideWindow(_this, window);
hfutrell@2744
  1085
	}
slouken@1895
  1086
}
slouken@1895
  1087
slouken@1895
  1088
void
slouken@1895
  1089
SDL_RaiseWindow(SDL_WindowID windowID)
slouken@1895
  1090
{
hfutrell@2744
  1091
	SDL_Window     *window = SDL_GetWindowFromID(windowID);
slouken@1895
  1092
hfutrell@2744
  1093
	if (!window || !(window->flags & SDL_WINDOW_SHOWN)) {
hfutrell@2744
  1094
		return;
hfutrell@2744
  1095
	}
hfutrell@2744
  1096
	if (_this->RaiseWindow) {
hfutrell@2744
  1097
		_this->RaiseWindow(_this, window);
hfutrell@2744
  1098
	}
slouken@1895
  1099
}
slouken@1895
  1100
slouken@1895
  1101
void
slouken@1895
  1102
SDL_MaximizeWindow(SDL_WindowID windowID)
slouken@1895
  1103
{
hfutrell@2744
  1104
	SDL_Window     *window = SDL_GetWindowFromID(windowID);
slouken@1895
  1105
hfutrell@2744
  1106
	if (!window || (window->flags & SDL_WINDOW_MAXIMIZED)) {
hfutrell@2744
  1107
		return;
hfutrell@2744
  1108
	}
hfutrell@2744
  1109
	SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_MAXIMIZED, 0, 0);
slouken@1895
  1110
hfutrell@2744
  1111
	if (_this->MaximizeWindow) {
hfutrell@2744
  1112
		_this->MaximizeWindow(_this, window);
hfutrell@2744
  1113
	}
slouken@1895
  1114
}
slouken@1895
  1115
slouken@1895
  1116
void
slouken@1895
  1117
SDL_MinimizeWindow(SDL_WindowID windowID)
slouken@1895
  1118
{
hfutrell@2744
  1119
	SDL_Window     *window = SDL_GetWindowFromID(windowID);
slouken@1895
  1120
hfutrell@2744
  1121
	if (!window || (window->flags & SDL_WINDOW_MINIMIZED)) {
hfutrell@2744
  1122
		return;
hfutrell@2744
  1123
	}
hfutrell@2744
  1124
	SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
slouken@1895
  1125
hfutrell@2744
  1126
	if (_this->MinimizeWindow) {
hfutrell@2744
  1127
		_this->MinimizeWindow(_this, window);
hfutrell@2744
  1128
	}
slouken@1895
  1129
}
slouken@1895
  1130
slouken@1895
  1131
void
slouken@1895
  1132
SDL_RestoreWindow(SDL_WindowID windowID)
slouken@1895
  1133
{
hfutrell@2744
  1134
	SDL_Window     *window = SDL_GetWindowFromID(windowID);
slouken@1895
  1135
hfutrell@2744
  1136
	if (!window
hfutrell@2744
  1137
	|| (window->flags & (SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED))) {
hfutrell@2744
  1138
		return;
hfutrell@2744
  1139
	}
hfutrell@2744
  1140
	SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_RESTORED, 0, 0);
slouken@1895
  1141
hfutrell@2744
  1142
	if (_this->RestoreWindow) {
hfutrell@2744
  1143
		_this->RestoreWindow(_this, window);
hfutrell@2744
  1144
	}
slouken@1895
  1145
}
slouken@1895
  1146
slouken@1895
  1147
int
slouken@1895
  1148
SDL_SetWindowFullscreen(SDL_WindowID windowID, int fullscreen)
slouken@1895
  1149
{
hfutrell@2744
  1150
	SDL_Window     *window = SDL_GetWindowFromID(windowID);
slouken@1895
  1151
hfutrell@2744
  1152
	if (!window) {
hfutrell@2744
  1153
		return -1;
hfutrell@2744
  1154
	}
hfutrell@2744
  1155
	if (fullscreen) {
hfutrell@2744
  1156
		fullscreen = SDL_WINDOW_FULLSCREEN;
hfutrell@2744
  1157
	}
hfutrell@2744
  1158
	if ((window->flags & SDL_WINDOW_FULLSCREEN) == fullscreen) {
hfutrell@2744
  1159
		return 0;
hfutrell@2744
  1160
	}
hfutrell@2744
  1161
	if (fullscreen) {
hfutrell@2744
  1162
		window->flags |= SDL_WINDOW_FULLSCREEN;
slouken@1895
  1163
hfutrell@2744
  1164
		if (FULLSCREEN_VISIBLE(window)) {
hfutrell@2744
  1165
			SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@1895
  1166
hfutrell@2744
  1167
			/* Hide any other fullscreen windows */
hfutrell@2744
  1168
			int             i;
hfutrell@2744
  1169
			for (i = 0; i < display->num_windows; ++i) {
hfutrell@2744
  1170
				SDL_Window     *other = &display->windows[i];
hfutrell@2744
  1171
				if (other->id != windowID && FULLSCREEN_VISIBLE(other)) {
hfutrell@2744
  1172
					SDL_MinimizeWindow(other->id);
hfutrell@2744
  1173
				}
hfutrell@2744
  1174
			}
slouken@1895
  1175
hfutrell@2744
  1176
			SDL_SetDisplayMode(&display->fullscreen_mode);
hfutrell@2744
  1177
		}
hfutrell@2744
  1178
	} else {
hfutrell@2744
  1179
		window->flags &= ~SDL_WINDOW_FULLSCREEN;
slouken@1895
  1180
hfutrell@2744
  1181
		if (FULLSCREEN_VISIBLE(window)) {
hfutrell@2744
  1182
			SDL_SetDisplayMode(NULL);
hfutrell@2744
  1183
		}
hfutrell@2744
  1184
	}
hfutrell@2744
  1185
	return 0;
slouken@1895
  1186
}
slouken@1895
  1187
slouken@1895
  1188
void
slouken@1895
  1189
SDL_SetWindowGrab(SDL_WindowID windowID, int mode)
slouken@1895
  1190
{
hfutrell@2744
  1191
	SDL_Window     *window = SDL_GetWindowFromID(windowID);
slouken@1895
  1192
hfutrell@2744
  1193
	if (!window || (!!mode == !!(window->flags & SDL_WINDOW_INPUT_GRABBED))) {
hfutrell@2744
  1194
		return;
hfutrell@2744
  1195
	}
hfutrell@2744
  1196
	if (mode) {
hfutrell@2744
  1197
		window->flags |= SDL_WINDOW_INPUT_GRABBED;
hfutrell@2744
  1198
	} else {
hfutrell@2744
  1199
		window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
hfutrell@2744
  1200
	}
slouken@1895
  1201
hfutrell@2744
  1202
	if ((window->flags & SDL_WINDOW_INPUT_FOCUS) && _this->SetWindowGrab) {
hfutrell@2744
  1203
		_this->SetWindowGrab(_this, window);
hfutrell@2744
  1204
	}
slouken@1895
  1205
}
slouken@1895
  1206
slouken@1895
  1207
int
slouken@1895
  1208
SDL_GetWindowGrab(SDL_WindowID windowID)
slouken@1895
  1209
{
hfutrell@2744
  1210
	SDL_Window     *window = SDL_GetWindowFromID(windowID);
slouken@1895
  1211
hfutrell@2744
  1212
	if (!window) {
hfutrell@2744
  1213
		return 0;
hfutrell@2744
  1214
	}
hfutrell@2744
  1215
	return ((window->flags & SDL_WINDOW_INPUT_GRABBED) != 0);
slouken@1895
  1216
}
slouken@1895
  1217
slouken@1895
  1218
void
slouken@1895
  1219
SDL_OnWindowShown(SDL_Window * window)
slouken@1895
  1220
{
slouken@1895
  1221
}
slouken@1895
  1222
slouken@1895
  1223
void
slouken@1895
  1224
SDL_OnWindowHidden(SDL_Window * window)
slouken@1895
  1225
{
slouken@1895
  1226
}
slouken@1895
  1227
slouken@1895
  1228
void
slouken@1970
  1229
SDL_OnWindowResized(SDL_Window * window)
slouken@1970
  1230
{
hfutrell@2744
  1231
	SDL_Renderer   *renderer = window->renderer;
slouken@1970
  1232
hfutrell@2744
  1233
	if (renderer && renderer->DisplayModeChanged) {
hfutrell@2744
  1234
		renderer->DisplayModeChanged(renderer);
hfutrell@2744
  1235
	}
slouken@1970
  1236
}
slouken@1970
  1237
slouken@1970
  1238
void
slouken@1895
  1239
SDL_OnWindowFocusGained(SDL_Window * window)
slouken@1895
  1240
{
hfutrell@2744
  1241
	SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@1895
  1242
hfutrell@2744
  1243
	if (window->flags & SDL_WINDOW_FULLSCREEN) {
hfutrell@2744
  1244
		SDL_SetDisplayMode(&display->fullscreen_mode);
hfutrell@2744
  1245
	}
hfutrell@2744
  1246
	if (display->gamma && _this->SetDisplayGammaRamp) {
hfutrell@2744
  1247
		_this->SetDisplayGammaRamp(_this, display->gamma);
hfutrell@2744
  1248
	}
hfutrell@2744
  1249
	if ((window->flags & SDL_WINDOW_INPUT_GRABBED) && _this->SetWindowGrab) {
hfutrell@2744
  1250
		_this->SetWindowGrab(_this, window);
hfutrell@2744
  1251
	}
slouken@1895
  1252
}
slouken@1895
  1253
slouken@1895
  1254
void
slouken@1895
  1255
SDL_OnWindowFocusLost(SDL_Window * window)
slouken@1895
  1256
{
hfutrell@2744
  1257
	SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@1895
  1258
hfutrell@2744
  1259
	if (window->flags & SDL_WINDOW_FULLSCREEN) {
hfutrell@2744
  1260
		SDL_MinimizeWindow(window->id);
hfutrell@2744
  1261
		SDL_SetDisplayMode(NULL);
hfutrell@2744
  1262
	}
hfutrell@2744
  1263
	if (display->gamma && _this->SetDisplayGammaRamp) {
hfutrell@2744
  1264
		_this->SetDisplayGammaRamp(_this, display->saved_gamma);
hfutrell@2744
  1265
	}
hfutrell@2744
  1266
	if ((window->flags & SDL_WINDOW_INPUT_GRABBED) && _this->SetWindowGrab) {
hfutrell@2744
  1267
		_this->SetWindowGrab(_this, window);
hfutrell@2744
  1268
	}
slouken@1895
  1269
}
slouken@1895
  1270
slouken@1895
  1271
SDL_WindowID
slouken@1895
  1272
SDL_GetFocusWindow(void)
slouken@1895
  1273
{
hfutrell@2744
  1274
	SDL_VideoDisplay *display;
hfutrell@2744
  1275
	int             i;
slouken@1895
  1276
hfutrell@2744
  1277
	if (!_this) {
hfutrell@2744
  1278
		return 0;
hfutrell@2744
  1279
	}
hfutrell@2744
  1280
	display = &SDL_CurrentDisplay;
hfutrell@2744
  1281
	for (i = 0; i < display->num_windows; ++i) {
hfutrell@2744
  1282
		SDL_Window     *window = &display->windows[i];
slouken@1895
  1283
hfutrell@2744
  1284
		if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
hfutrell@2744
  1285
			return window->id;
hfutrell@2744
  1286
		}
hfutrell@2744
  1287
	}
hfutrell@2744
  1288
	return 0;
slouken@1895
  1289
}
slouken@1895
  1290
slouken@1895
  1291
void
slouken@1895
  1292
SDL_DestroyWindow(SDL_WindowID windowID)
slouken@1895
  1293
{
hfutrell@2744
  1294
	int             i, j;
slouken@1895
  1295
hfutrell@2744
  1296
	if (!_this) {
hfutrell@2744
  1297
		return;
hfutrell@2744
  1298
	}
hfutrell@2744
  1299
	/* Restore video mode, etc. */
hfutrell@2744
  1300
	SDL_SendWindowEvent(windowID, SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
slouken@1895
  1301
hfutrell@2744
  1302
	for (i = 0; i < _this->num_displays; ++i) {
hfutrell@2744
  1303
		SDL_VideoDisplay *display = &_this->displays[i];
hfutrell@2744
  1304
		for (j = 0; j < display->num_windows; ++j) {
hfutrell@2744
  1305
			SDL_Window     *window = &display->windows[j];
hfutrell@2744
  1306
			if (window->id != windowID) {
hfutrell@2744
  1307
				continue;
hfutrell@2744
  1308
			}
hfutrell@2744
  1309
			if (window->title) {
hfutrell@2744
  1310
				SDL_free(window->title);
hfutrell@2744
  1311
				window->title = NULL;
hfutrell@2744
  1312
			}
hfutrell@2744
  1313
			if (window->renderer) {
hfutrell@2744
  1314
				SDL_DestroyRenderer(window->id);
hfutrell@2744
  1315
				window->renderer = NULL;
hfutrell@2744
  1316
			}
hfutrell@2744
  1317
			if (_this->DestroyWindow) {
hfutrell@2744
  1318
				_this->DestroyWindow(_this, window);
hfutrell@2744
  1319
			}
hfutrell@2744
  1320
			if (j != display->num_windows - 1) {
hfutrell@2744
  1321
				SDL_memcpy(&display->windows[i],
hfutrell@2744
  1322
					   &display->windows[i + 1],
hfutrell@2744
  1323
					   (display->num_windows - i - 1) * sizeof(*window));
hfutrell@2744
  1324
			}
hfutrell@2744
  1325
			--display->num_windows;
hfutrell@2744
  1326
			return;
hfutrell@2744
  1327
		}
hfutrell@2744
  1328
	}
slouken@1895
  1329
}
slouken@1895
  1330
slouken@1895
  1331
void
slouken@1895
  1332
SDL_AddRenderDriver(int displayIndex, const SDL_RenderDriver * driver)
slouken@1895
  1333
{
hfutrell@2744
  1334
	SDL_VideoDisplay *display;
hfutrell@2744
  1335
	SDL_RenderDriver *render_drivers;
slouken@1895
  1336
hfutrell@2744
  1337
	if (displayIndex >= _this->num_displays) {
hfutrell@2744
  1338
		return;
hfutrell@2744
  1339
	}
hfutrell@2744
  1340
	display = &_this->displays[displayIndex];
slouken@2119
  1341
hfutrell@2744
  1342
	render_drivers =
hfutrell@2744
  1343
		SDL_realloc(display->render_drivers,
hfutrell@2744
  1344
			    (display->num_render_drivers +
hfutrell@2744
  1345
			     1) * sizeof(*render_drivers));
hfutrell@2744
  1346
	if (render_drivers) {
hfutrell@2744
  1347
		render_drivers[display->num_render_drivers] = *driver;
hfutrell@2744
  1348
		display->render_drivers = render_drivers;
hfutrell@2744
  1349
		display->num_render_drivers++;
hfutrell@2744
  1350
	}
slouken@1895
  1351
}
slouken@1895
  1352
slouken@1895
  1353
int
slouken@1969
  1354
SDL_GetNumRenderDrivers(void)
slouken@1895
  1355
{
hfutrell@2744
  1356
	if (_this) {
hfutrell@2744
  1357
		return SDL_CurrentDisplay.num_render_drivers;
hfutrell@2744
  1358
	}
hfutrell@2744
  1359
	return 0;
slouken@1895
  1360
}
slouken@1895
  1361
slouken@1895
  1362
int
slouken@1969
  1363
SDL_GetRenderDriverInfo(int index, SDL_RendererInfo * info)
slouken@1895
  1364
{
hfutrell@2744
  1365
	if (!_this) {
hfutrell@2744
  1366
		SDL_UninitializedVideo();
hfutrell@2744
  1367
		return -1;
hfutrell@2744
  1368
	}
hfutrell@2744
  1369
	if (index < 0 || index >= SDL_GetNumRenderDrivers()) {
hfutrell@2744
  1370
		SDL_SetError("index must be in the range of 0 - %d",
hfutrell@2744
  1371
			     SDL_GetNumRenderDrivers() - 1);
hfutrell@2744
  1372
		return -1;
hfutrell@2744
  1373
	}
hfutrell@2744
  1374
	*info = SDL_CurrentDisplay.render_drivers[index].info;
hfutrell@2744
  1375
	return 0;
slouken@1895
  1376
}
slouken@1895
  1377
slouken@1895
  1378
int
slouken@1895
  1379
SDL_CreateRenderer(SDL_WindowID windowID, int index, Uint32 flags)
slouken@1895
  1380
{
hfutrell@2744
  1381
	SDL_Window     *window = SDL_GetWindowFromID(windowID);
slouken@1895
  1382
hfutrell@2744
  1383
	if (!window) {
hfutrell@2744
  1384
		return 0;
hfutrell@2744
  1385
	}
hfutrell@2744
  1386
	if (index < 0) {
hfutrell@2744
  1387
		const char     *override = SDL_getenv("SDL_VIDEO_RENDERER");
hfutrell@2744
  1388
		int             n = SDL_GetNumRenderDrivers();
hfutrell@2744
  1389
		for (index = 0; index < n; ++index) {
hfutrell@2744
  1390
			SDL_RenderDriver *driver =
hfutrell@2744
  1391
			&SDL_CurrentDisplay.render_drivers[index];
slouken@1895
  1392
hfutrell@2744
  1393
			if (override) {
hfutrell@2744
  1394
				if (SDL_strcasecmp(override, driver->info.name) == 0) {
hfutrell@2744
  1395
					break;
hfutrell@2744
  1396
				}
hfutrell@2744
  1397
			} else {
hfutrell@2744
  1398
				if ((driver->info.flags & flags) == flags) {
hfutrell@2744
  1399
					break;
hfutrell@2744
  1400
				}
hfutrell@2744
  1401
			}
hfutrell@2744
  1402
		}
hfutrell@2744
  1403
		if (index == n) {
hfutrell@2744
  1404
			SDL_SetError("Couldn't find matching render driver");
hfutrell@2744
  1405
			return -1;
hfutrell@2744
  1406
		}
hfutrell@2744
  1407
	}
hfutrell@2744
  1408
	if (index >= SDL_GetNumRenderDrivers()) {
hfutrell@2744
  1409
		SDL_SetError("index must be -1 or in the range of 0 - %d",
hfutrell@2744
  1410
			     SDL_GetNumRenderDrivers() - 1);
hfutrell@2744
  1411
		return -1;
hfutrell@2744
  1412
	}
hfutrell@2744
  1413
	/* Free any existing renderer */
hfutrell@2744
  1414
	SDL_DestroyRenderer(windowID);
slouken@1895
  1415
hfutrell@2744
  1416
	/* Create a new renderer instance */
hfutrell@2744
  1417
	window->renderer = SDL_CurrentDisplay.render_drivers[index]
hfutrell@2744
  1418
		.CreateRenderer(window, flags);
hfutrell@2744
  1419
	SDL_SelectRenderer(window->id);
slouken@1895
  1420
hfutrell@2744
  1421
	return 0;
slouken@1895
  1422
}
slouken@1895
  1423
slouken@1895
  1424
int
slouken@1895
  1425
SDL_SelectRenderer(SDL_WindowID windowID)
slouken@1895
  1426
{
hfutrell@2744
  1427
	SDL_Window     *window = SDL_GetWindowFromID(windowID);
hfutrell@2744
  1428
	SDL_Renderer   *renderer;
slouken@1895
  1429
hfutrell@2744
  1430
	if (!window || !window->renderer) {
hfutrell@2744
  1431
		return -1;
hfutrell@2744
  1432
	}
hfutrell@2744
  1433
	renderer = window->renderer;
hfutrell@2744
  1434
	if (renderer && renderer->ActivateRenderer) {
hfutrell@2744
  1435
		if (renderer->ActivateRenderer(renderer) < 0) {
hfutrell@2744
  1436
			return -1;
hfutrell@2744
  1437
		}
hfutrell@2744
  1438
	}
hfutrell@2744
  1439
	SDL_CurrentDisplay.current_renderer = renderer;
hfutrell@2744
  1440
	return 0;
slouken@1895
  1441
}
slouken@1895
  1442
slouken@1969
  1443
int
slouken@1969
  1444
SDL_GetRendererInfo(SDL_RendererInfo * info)
slouken@1969
  1445
{
hfutrell@2744
  1446
	if (!_this) {
hfutrell@2744
  1447
		SDL_UninitializedVideo();
hfutrell@2744
  1448
		return -1;
hfutrell@2744
  1449
	}
hfutrell@2744
  1450
	if (!SDL_CurrentDisplay.current_renderer) {
hfutrell@2744
  1451
		SDL_SetError("There is no current renderer");
hfutrell@2744
  1452
		return -1;
hfutrell@2744
  1453
	}
hfutrell@2744
  1454
	*info = SDL_CurrentDisplay.current_renderer->info;
hfutrell@2744
  1455
	return 0;
slouken@1969
  1456
}
slouken@1969
  1457
slouken@1895
  1458
SDL_TextureID
slouken@1895
  1459
SDL_CreateTexture(Uint32 format, int access, int w, int h)
slouken@1895
  1460
{
hfutrell@2744
  1461
	int             hash;
hfutrell@2744
  1462
	SDL_Renderer   *renderer;
hfutrell@2744
  1463
	SDL_Texture    *texture;
slouken@1895
  1464
hfutrell@2744
  1465
	if (!_this) {
hfutrell@2744
  1466
		SDL_UninitializedVideo();
hfutrell@2744
  1467
		return 0;
hfutrell@2744
  1468
	}
hfutrell@2744
  1469
	renderer = SDL_CurrentDisplay.current_renderer;
hfutrell@2744
  1470
	if (!renderer || !renderer->CreateTexture) {
hfutrell@2744
  1471
		return 0;
hfutrell@2744
  1472
	}
hfutrell@2744
  1473
	texture = (SDL_Texture *) SDL_calloc(1, sizeof(*texture));
hfutrell@2744
  1474
	if (!texture) {
hfutrell@2744
  1475
		SDL_OutOfMemory();
hfutrell@2744
  1476
		return 0;
hfutrell@2744
  1477
	}
hfutrell@2744
  1478
	texture->id = _this->next_object_id++;
hfutrell@2744
  1479
	texture->format = format;
hfutrell@2744
  1480
	texture->access = access;
hfutrell@2744
  1481
	texture->w = w;
hfutrell@2744
  1482
	texture->h = h;
hfutrell@2744
  1483
	texture->r = 255;
hfutrell@2744
  1484
	texture->g = 255;
hfutrell@2744
  1485
	texture->b = 255;
hfutrell@2744
  1486
	texture->a = 255;
hfutrell@2744
  1487
	texture->renderer = renderer;
slouken@1895
  1488
hfutrell@2744
  1489
	if (renderer->CreateTexture(renderer, texture) < 0) {
hfutrell@2744
  1490
		if (renderer->DestroyTexture) {
hfutrell@2744
  1491
			renderer->DestroyTexture(renderer, texture);
hfutrell@2744
  1492
		}
hfutrell@2744
  1493
		SDL_free(texture);
hfutrell@2744
  1494
		return 0;
hfutrell@2744
  1495
	}
hfutrell@2744
  1496
	hash = (texture->id % SDL_arraysize(SDL_CurrentDisplay.textures));
hfutrell@2744
  1497
	texture->next = SDL_CurrentDisplay.textures[hash];
hfutrell@2744
  1498
	SDL_CurrentDisplay.textures[hash] = texture;
slouken@1895
  1499
hfutrell@2744
  1500
	return texture->id;
slouken@1895
  1501
}
slouken@1895
  1502
slouken@1895
  1503
SDL_TextureID
slouken@2222
  1504
SDL_CreateTextureFromSurface(Uint32 format, SDL_Surface * surface)
slouken@1895
  1505
{
hfutrell@2744
  1506
	SDL_TextureID   textureID;
hfutrell@2744
  1507
	SDL_PixelFormat *fmt;
hfutrell@2744
  1508
	int             bpp;
hfutrell@2744
  1509
	Uint32          Rmask, Gmask, Bmask, Amask;
slouken@1895
  1510
hfutrell@2744
  1511
	if (!surface) {
hfutrell@2744
  1512
		SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
hfutrell@2744
  1513
		return 0;
hfutrell@2744
  1514
	}
hfutrell@2744
  1515
	fmt = surface->format;
slouken@1895
  1516
hfutrell@2744
  1517
	if (format) {
hfutrell@2744
  1518
		if (!SDL_PixelFormatEnumToMasks
hfutrell@2744
  1519
		    (format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
hfutrell@2744
  1520
			SDL_SetError("Unknown pixel format");
hfutrell@2744
  1521
			return 0;
hfutrell@2744
  1522
		}
hfutrell@2744
  1523
	} else {
hfutrell@2744
  1524
		if (surface->format->Amask
hfutrell@2744
  1525
		    || !(surface->map->info.flags &
hfutrell@2744
  1526
		    (SDL_COPY_COLORKEY | SDL_COPY_MASK | SDL_COPY_BLEND))) {
hfutrell@2744
  1527
			bpp = fmt->BitsPerPixel;
hfutrell@2744
  1528
			Rmask = fmt->Rmask;
hfutrell@2744
  1529
			Gmask = fmt->Gmask;
hfutrell@2744
  1530
			Bmask = fmt->Bmask;
hfutrell@2744
  1531
			Amask = fmt->Amask;
hfutrell@2744
  1532
		} else {
hfutrell@2744
  1533
			/* Need a format with alpha */
hfutrell@2744
  1534
			bpp = 32;
hfutrell@2744
  1535
			Rmask = 0x00FF0000;
hfutrell@2744
  1536
			Gmask = 0x0000FF00;
hfutrell@2744
  1537
			Bmask = 0x000000FF;
hfutrell@2744
  1538
			Amask = 0xFF000000;
hfutrell@2744
  1539
		}
hfutrell@2744
  1540
		format = SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
hfutrell@2744
  1541
		if (!format) {
hfutrell@2744
  1542
			SDL_SetError("Unknown pixel format");
hfutrell@2744
  1543
			return 0;
hfutrell@2744
  1544
		}
hfutrell@2744
  1545
	}
slouken@1895
  1546
hfutrell@2744
  1547
	textureID =
hfutrell@2744
  1548
		SDL_CreateTexture(format, SDL_TEXTUREACCESS_STATIC, surface->w,
hfutrell@2744
  1549
				  surface->h);
hfutrell@2744
  1550
	if (!textureID) {
hfutrell@2744
  1551
		return 0;
hfutrell@2744
  1552
	}
hfutrell@2744
  1553
	if (bpp == fmt->BitsPerPixel && Rmask == fmt->Rmask && Gmask == fmt->Gmask
hfutrell@2744
  1554
	    && Bmask == fmt->Bmask && Amask == fmt->Amask) {
hfutrell@2744
  1555
		if (SDL_MUSTLOCK(surface)) {
hfutrell@2744
  1556
			if (SDL_LockSurface(surface) < 0) {
hfutrell@2744
  1557
				SDL_DestroyTexture(textureID);
hfutrell@2744
  1558
				return 0;
hfutrell@2744
  1559
			}
hfutrell@2744
  1560
			SDL_UpdateTexture(textureID, NULL, surface->pixels,
hfutrell@2744
  1561
					  surface->pitch);
hfutrell@2744
  1562
			SDL_UnlockSurface(surface);
hfutrell@2744
  1563
		} else {
hfutrell@2744
  1564
			SDL_UpdateTexture(textureID, NULL, surface->pixels,
hfutrell@2744
  1565
					  surface->pitch);
hfutrell@2744
  1566
		}
hfutrell@2744
  1567
	} else {
hfutrell@2744
  1568
		SDL_PixelFormat *dst_fmt;
hfutrell@2744
  1569
		SDL_Surface    *dst = NULL;
slouken@1895
  1570
hfutrell@2744
  1571
		/* Set up a destination surface for the texture update */
hfutrell@2744
  1572
		dst_fmt = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
hfutrell@2744
  1573
		if (dst_fmt) {
hfutrell@2744
  1574
			if (SDL_ISPIXELFORMAT_INDEXED(format)) {
hfutrell@2744
  1575
				dst_fmt->palette =
hfutrell@2744
  1576
					SDL_AllocPalette((1 << SDL_BITSPERPIXEL(format)));
hfutrell@2744
  1577
				if (dst_fmt->palette) {
hfutrell@2744
  1578
					/*
hfutrell@2744
  1579
					 * FIXME: Should we try to copy
hfutrell@2744
  1580
					 * fmt->palette?
hfutrell@2744
  1581
					 */
hfutrell@2744
  1582
					SDL_DitherColors(dst_fmt->palette->colors,
hfutrell@2744
  1583
						  SDL_BITSPERPIXEL(format));
hfutrell@2744
  1584
				}
hfutrell@2744
  1585
			}
hfutrell@2744
  1586
			dst = SDL_ConvertSurface(surface, dst_fmt, 0);
hfutrell@2744
  1587
			if (dst) {
hfutrell@2744
  1588
				SDL_UpdateTexture(textureID, NULL, dst->pixels, dst->pitch);
hfutrell@2744
  1589
				SDL_FreeSurface(dst);
hfutrell@2744
  1590
			}
hfutrell@2744
  1591
			if (dst_fmt->palette) {
hfutrell@2744
  1592
				SDL_FreePalette(dst_fmt->palette);
hfutrell@2744
  1593
			}
hfutrell@2744
  1594
			SDL_FreeFormat(dst_fmt);
hfutrell@2744
  1595
		}
hfutrell@2744
  1596
		if (!dst) {
hfutrell@2744
  1597
			SDL_DestroyTexture(textureID);
hfutrell@2744
  1598
			return 0;
hfutrell@2744
  1599
		}
hfutrell@2744
  1600
	}
slouken@2222
  1601
hfutrell@2744
  1602
	if (SDL_ISPIXELFORMAT_INDEXED(format) && fmt->palette) {
hfutrell@2744
  1603
		SDL_SetTexturePalette(textureID, fmt->palette->colors, 0,
hfutrell@2744
  1604
				      fmt->palette->ncolors);
hfutrell@2744
  1605
	}
hfutrell@2744
  1606
	return textureID;
slouken@1895
  1607
}
slouken@1895
  1608
slouken@1895
  1609
static __inline__ SDL_Texture *
slouken@1895
  1610
SDL_GetTextureFromID(SDL_TextureID textureID)
slouken@1895
  1611
{
hfutrell@2744
  1612
	int             hash;
hfutrell@2744
  1613
	SDL_Texture    *texture;
slouken@1895
  1614
hfutrell@2744
  1615
	if (!_this) {
hfutrell@2744
  1616
		return NULL;
hfutrell@2744
  1617
	}
hfutrell@2744
  1618
	hash = (textureID % SDL_arraysize(SDL_CurrentDisplay.textures));
hfutrell@2744
  1619
	for (texture = SDL_CurrentDisplay.textures[hash]; texture;
hfutrell@2744
  1620
	     texture = texture->next) {
hfutrell@2744
  1621
		if (texture->id == textureID) {
hfutrell@2744
  1622
			return texture;
hfutrell@2744
  1623
		}
hfutrell@2744
  1624
	}
hfutrell@2744
  1625
	return NULL;
slouken@1895
  1626
}
slouken@1895
  1627
slouken@1895
  1628
int
slouken@1895
  1629
SDL_QueryTexture(SDL_TextureID textureID, Uint32 * format, int *access,
hfutrell@2744
  1630
		 int *w, int *h)
slouken@1895
  1631
{
hfutrell@2744
  1632
	SDL_Texture    *texture = SDL_GetTextureFromID(textureID);
slouken@1895
  1633
hfutrell@2744
  1634
	if (!texture) {
hfutrell@2744
  1635
		return -1;
hfutrell@2744
  1636
	}
hfutrell@2744
  1637
	if (format) {
hfutrell@2744
  1638
		*format = texture->format;
hfutrell@2744
  1639
	}
hfutrell@2744
  1640
	if (access) {
hfutrell@2744
  1641
		*access = texture->access;
hfutrell@2744
  1642
	}
hfutrell@2744
  1643
	if (w) {
hfutrell@2744
  1644
		*w = texture->w;
hfutrell@2744
  1645
	}
hfutrell@2744
  1646
	if (h) {
hfutrell@2744
  1647
		*h = texture->h;
hfutrell@2744
  1648
	}
hfutrell@2744
  1649
	return 0;
slouken@1895
  1650
}
slouken@1895
  1651
slouken@1895
  1652
int
slouken@1895
  1653
SDL_QueryTexturePixels(SDL_TextureID textureID, void **pixels, int *pitch)
slouken@1895
  1654
{
hfutrell@2744
  1655
	SDL_Texture    *texture = SDL_GetTextureFromID(textureID);
hfutrell@2744
  1656
	SDL_Renderer   *renderer;
slouken@1895
  1657
hfutrell@2744
  1658
	if (!texture) {
hfutrell@2744
  1659
		return -1;
hfutrell@2744
  1660
	}
hfutrell@2744
  1661
	renderer = texture->renderer;
hfutrell@2744
  1662
	if (!renderer->QueryTexturePixels) {
hfutrell@2744
  1663
		return -1;
hfutrell@2744
  1664
	}
hfutrell@2744
  1665
	return renderer->QueryTexturePixels(renderer, texture, pixels, pitch);
slouken@1895
  1666
}
slouken@1895
  1667
slouken@1895
  1668
int
slouken@1895
  1669
SDL_SetTexturePalette(SDL_TextureID textureID, const SDL_Color * colors,
hfutrell@2744
  1670
		      int firstcolor, int ncolors)
slouken@1895
  1671
{
hfutrell@2744
  1672
	SDL_Texture    *texture = SDL_GetTextureFromID(textureID);
hfutrell@2744
  1673
	SDL_Renderer   *renderer;
slouken@1895
  1674
hfutrell@2744
  1675
	if (!texture) {
hfutrell@2744
  1676
		return -1;
hfutrell@2744
  1677
	}
hfutrell@2744
  1678
	renderer = texture->renderer;
hfutrell@2744
  1679
	if (!renderer->SetTexturePalette) {
hfutrell@2744
  1680
		return -1;
hfutrell@2744
  1681
	}
hfutrell@2744
  1682
	return renderer->SetTexturePalette(renderer, texture, colors, firstcolor,
hfutrell@2744
  1683
					   ncolors);
slouken@1895
  1684
}
slouken@1895
  1685
slouken@1895
  1686
int
slouken@1895
  1687
SDL_GetTexturePalette(SDL_TextureID textureID, SDL_Color * colors,
hfutrell@2744
  1688
		      int firstcolor, int ncolors)
slouken@1895
  1689
{
hfutrell@2744
  1690
	SDL_Texture    *texture = SDL_GetTextureFromID(textureID);
hfutrell@2744
  1691
	SDL_Renderer   *renderer;
slouken@1895
  1692
hfutrell@2744
  1693
	if (!texture) {
hfutrell@2744
  1694
		return -1;
hfutrell@2744
  1695
	}
hfutrell@2744
  1696
	renderer = texture->renderer;
hfutrell@2744
  1697
	if (!renderer->GetTexturePalette) {
hfutrell@2744
  1698
		return -1;
hfutrell@2744
  1699
	}
hfutrell@2744
  1700
	return renderer->GetTexturePalette(renderer, texture, colors, firstcolor,
hfutrell@2744
  1701
					   ncolors);
slouken@1895
  1702
}
slouken@1895
  1703
slouken@1895
  1704
int
slouken@1985
  1705
SDL_SetTextureColorMod(SDL_TextureID textureID, Uint8 r, Uint8 g, Uint8 b)
slouken@1985
  1706
{
hfutrell@2744
  1707
	SDL_Texture    *texture = SDL_GetTextureFromID(textureID);
hfutrell@2744
  1708
	SDL_Renderer   *renderer;
slouken@1985
  1709
hfutrell@2744
  1710
	if (!texture) {
hfutrell@2744
  1711
		return -1;
hfutrell@2744
  1712
	}
hfutrell@2744
  1713
	renderer = texture->renderer;
hfutrell@2744
  1714
	if (!renderer->SetTextureColorMod) {
hfutrell@2744
  1715
		return -1;
hfutrell@2744
  1716
	}
hfutrell@2744
  1717
	if (r < 255 || g < 255 || b < 255) {
hfutrell@2744
  1718
		texture->modMode |= SDL_TEXTUREMODULATE_COLOR;
hfutrell@2744
  1719
	} else {
hfutrell@2744
  1720
		texture->modMode &= ~SDL_TEXTUREMODULATE_COLOR;
hfutrell@2744
  1721
	}
hfutrell@2744
  1722
	texture->r = r;
hfutrell@2744
  1723
	texture->g = g;
hfutrell@2744
  1724
	texture->b = b;
hfutrell@2744
  1725
	return renderer->SetTextureColorMod(renderer, texture);
slouken@1985
  1726
}
slouken@1985
  1727
slouken@1985
  1728
int
slouken@1985
  1729
SDL_GetTextureColorMod(SDL_TextureID textureID, Uint8 * r, Uint8 * g,
hfutrell@2744
  1730
		       Uint8 * b)
slouken@1985
  1731
{
hfutrell@2744
  1732
	SDL_Texture    *texture = SDL_GetTextureFromID(textureID);
hfutrell@2744
  1733
	SDL_Renderer   *renderer;
slouken@1985
  1734
hfutrell@2744
  1735
	if (!texture) {
hfutrell@2744
  1736
		return -1;
hfutrell@2744
  1737
	}
hfutrell@2744
  1738
	renderer = texture->renderer;
hfutrell@2744
  1739
	if (r) {
hfutrell@2744
  1740
		*r = texture->r;
hfutrell@2744
  1741
	}
hfutrell@2744
  1742
	if (g) {
hfutrell@2744
  1743
		*g = texture->g;
hfutrell@2744
  1744
	}
hfutrell@2744
  1745
	if (b) {
hfutrell@2744
  1746
		*b = texture->b;
hfutrell@2744
  1747
	}
hfutrell@2744
  1748
	return 0;
slouken@1985
  1749
}
slouken@1985
  1750
slouken@1985
  1751
int
slouken@1985
  1752
SDL_SetTextureAlphaMod(SDL_TextureID textureID, Uint8 alpha)
slouken@1985
  1753
{
hfutrell@2744
  1754
	SDL_Texture    *texture = SDL_GetTextureFromID(textureID);
hfutrell@2744
  1755
	SDL_Renderer   *renderer;
slouken@1985
  1756
hfutrell@2744
  1757
	if (!texture) {
hfutrell@2744
  1758
		return -1;
hfutrell@2744
  1759
	}
hfutrell@2744
  1760
	renderer = texture->renderer;
hfutrell@2744
  1761
	if (!renderer->SetTextureAlphaMod) {
hfutrell@2744
  1762
		return -1;
hfutrell@2744
  1763
	}
hfutrell@2744
  1764
	if (alpha < 255) {
hfutrell@2744
  1765
		texture->modMode |= SDL_TEXTUREMODULATE_ALPHA;
hfutrell@2744
  1766
	} else {
hfutrell@2744
  1767
		texture->modMode &= ~SDL_TEXTUREMODULATE_ALPHA;
hfutrell@2744
  1768
	}
hfutrell@2744
  1769
	texture->a = alpha;
hfutrell@2744
  1770
	return renderer->SetTextureAlphaMod(renderer, texture);
slouken@1985
  1771
}
slouken@1985
  1772
slouken@1985
  1773
int
slouken@1985
  1774
SDL_GetTextureAlphaMod(SDL_TextureID textureID, Uint8 * alpha)
slouken@1985
  1775
{
hfutrell@2744
  1776
	SDL_Texture    *texture = SDL_GetTextureFromID(textureID);
slouken@1985
  1777
hfutrell@2744
  1778
	if (!texture) {
hfutrell@2744
  1779
		return -1;
hfutrell@2744
  1780
	}
hfutrell@2744
  1781
	if (alpha) {
hfutrell@2744
  1782
		*alpha = texture->a;
hfutrell@2744
  1783
	}
hfutrell@2744
  1784
	return 0;
slouken@1985
  1785
}
slouken@1985
  1786
slouken@1985
  1787
int
slouken@1985
  1788
SDL_SetTextureBlendMode(SDL_TextureID textureID, int blendMode)
slouken@1985
  1789
{
hfutrell@2744
  1790
	SDL_Texture    *texture = SDL_GetTextureFromID(textureID);
hfutrell@2744
  1791
	SDL_Renderer   *renderer;
slouken@1985
  1792
hfutrell@2744
  1793
	if (!texture) {
hfutrell@2744
  1794
		return -1;
hfutrell@2744
  1795
	}
hfutrell@2744
  1796
	renderer = texture->renderer;
hfutrell@2744
  1797
	if (!renderer->SetTextureBlendMode) {
hfutrell@2744
  1798
		return -1;
hfutrell@2744
  1799
	}
hfutrell@2744
  1800
	texture->blendMode = blendMode;
hfutrell@2744
  1801
	return renderer->SetTextureBlendMode(renderer, texture);
slouken@1985
  1802
}
slouken@1985
  1803
slouken@1985
  1804
int
slouken@1985
  1805
SDL_GetTextureBlendMode(SDL_TextureID textureID, int *blendMode)
slouken@1985
  1806
{
hfutrell@2744
  1807
	SDL_Texture    *texture = SDL_GetTextureFromID(textureID);
slouken@1985
  1808
hfutrell@2744
  1809
	if (!texture) {
hfutrell@2744
  1810
		return -1;
hfutrell@2744
  1811
	}
hfutrell@2744
  1812
	if (blendMode) {
hfutrell@2744
  1813
		*blendMode = texture->blendMode;
hfutrell@2744
  1814
	}
hfutrell@2744
  1815
	return 0;
slouken@1985
  1816
}
slouken@1985
  1817
slouken@1985
  1818
int
slouken@1985
  1819
SDL_SetTextureScaleMode(SDL_TextureID textureID, int scaleMode)
slouken@1985
  1820
{
hfutrell@2744
  1821
	SDL_Texture    *texture = SDL_GetTextureFromID(textureID);
hfutrell@2744
  1822
	SDL_Renderer   *renderer;
slouken@1985
  1823
hfutrell@2744
  1824
	if (!texture) {
hfutrell@2744
  1825
		return -1;
hfutrell@2744
  1826
	}
hfutrell@2744
  1827
	renderer = texture->renderer;
hfutrell@2744
  1828
	if (!renderer->SetTextureScaleMode) {
hfutrell@2744
  1829
		return -1;
hfutrell@2744
  1830
	}
hfutrell@2744
  1831
	texture->scaleMode = scaleMode;
hfutrell@2744
  1832
	return renderer->SetTextureScaleMode(renderer, texture);
slouken@1985
  1833
}
slouken@1985
  1834
slouken@1985
  1835
int
slouken@1985
  1836
SDL_GetTextureScaleMode(SDL_TextureID textureID, int *scaleMode)
slouken@1985
  1837
{
hfutrell@2744
  1838
	SDL_Texture    *texture = SDL_GetTextureFromID(textureID);
slouken@1985
  1839
hfutrell@2744
  1840
	if (!texture) {
hfutrell@2744
  1841
		return -1;
hfutrell@2744
  1842
	}
hfutrell@2744
  1843
	if (scaleMode) {
hfutrell@2744
  1844
		*scaleMode = texture->scaleMode;
hfutrell@2744
  1845
	}
hfutrell@2744
  1846
	return 0;
slouken@1985
  1847
}
slouken@1985
  1848
slouken@1985
  1849
int
slouken@1895
  1850
SDL_UpdateTexture(SDL_TextureID textureID, const SDL_Rect * rect,
hfutrell@2744
  1851
		  const void *pixels, int pitch)
slouken@1895
  1852
{
hfutrell@2744
  1853
	SDL_Texture    *texture = SDL_GetTextureFromID(textureID);
hfutrell@2744
  1854
	SDL_Renderer   *renderer;
hfutrell@2744
  1855
	SDL_Rect        full_rect;
slouken@1895
  1856
hfutrell@2744
  1857
	if (!texture) {
hfutrell@2744
  1858
		return -1;
hfutrell@2744
  1859
	}
hfutrell@2744
  1860
	renderer = texture->renderer;
hfutrell@2744
  1861
	if (!renderer->UpdateTexture) {
hfutrell@2744
  1862
		return -1;
hfutrell@2744
  1863
	}
hfutrell@2744
  1864
	if (!rect) {
hfutrell@2744
  1865
		full_rect.x = 0;
hfutrell@2744
  1866
		full_rect.y = 0;
hfutrell@2744
  1867
		full_rect.w = texture->w;
hfutrell@2744
  1868
		full_rect.h = texture->h;
hfutrell@2744
  1869
		rect = &full_rect;
hfutrell@2744
  1870
	}
hfutrell@2744
  1871
	return renderer->UpdateTexture(renderer, texture, rect, pixels, pitch);
slouken@1895
  1872
}
slouken@1895
  1873
slouken@1895
  1874
int
slouken@1895
  1875
SDL_LockTexture(SDL_TextureID textureID, const SDL_Rect * rect, int markDirty,
hfutrell@2744
  1876
		void **pixels, int *pitch)
slouken@1895
  1877
{
hfutrell@2744
  1878
	SDL_Texture    *texture = SDL_GetTextureFromID(textureID);
hfutrell@2744
  1879
	SDL_Renderer   *renderer;
hfutrell@2744
  1880
	SDL_Rect        full_rect;
slouken@1895
  1881
hfutrell@2744
  1882
	if (!texture) {
hfutrell@2744
  1883
		return -1;
hfutrell@2744
  1884
	}
hfutrell@2744
  1885
	if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
hfutrell@2744
  1886
		SDL_SetError("SDL_LockTexture(): texture must be streaming");
hfutrell@2744
  1887
		return -1;
hfutrell@2744
  1888
	}
hfutrell@2744
  1889
	renderer = texture->renderer;
hfutrell@2744
  1890
	if (!renderer->LockTexture) {
hfutrell@2744
  1891
		return -1;
hfutrell@2744
  1892
	}
hfutrell@2744
  1893
	if (!rect) {
hfutrell@2744
  1894
		full_rect.x = 0;
hfutrell@2744
  1895
		full_rect.y = 0;
hfutrell@2744
  1896
		full_rect.w = texture->w;
hfutrell@2744
  1897
		full_rect.h = texture->h;
hfutrell@2744
  1898
		rect = &full_rect;
hfutrell@2744
  1899
	}
hfutrell@2744
  1900
	return renderer->LockTexture(renderer, texture, rect, markDirty, pixels,
hfutrell@2744
  1901
				     pitch);
slouken@1895
  1902
}
slouken@1895
  1903
slouken@1895
  1904
void
slouken@1895
  1905
SDL_UnlockTexture(SDL_TextureID textureID)
slouken@1895
  1906
{
hfutrell@2744
  1907
	SDL_Texture    *texture = SDL_GetTextureFromID(textureID);
hfutrell@2744
  1908
	SDL_Renderer   *renderer;
slouken@1895
  1909
hfutrell@2744
  1910
	if (!texture) {
hfutrell@2744
  1911
		return;
hfutrell@2744
  1912
	}
hfutrell@2744
  1913
	if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
hfutrell@2744
  1914
		return;
hfutrell@2744
  1915
	}
hfutrell@2744
  1916
	renderer = texture->renderer;
hfutrell@2744
  1917
	if (!renderer->UnlockTexture) {
hfutrell@2744
  1918
		return;
hfutrell@2744
  1919
	}
hfutrell@2744
  1920
	renderer->UnlockTexture(renderer, texture);
slouken@1895
  1921
}
slouken@1895
  1922
slouken@1895
  1923
void
slouken@1895
  1924
SDL_DirtyTexture(SDL_TextureID textureID, int numrects,
hfutrell@2744
  1925
		 const SDL_Rect * rects)
slouken@1895
  1926
{
hfutrell@2744
  1927
	SDL_Texture    *texture = SDL_GetTextureFromID(textureID);
hfutrell@2744
  1928
	SDL_Renderer   *renderer;
slouken@1895
  1929
hfutrell@2744
  1930
	if (!texture) {
hfutrell@2744
  1931
		return;
hfutrell@2744
  1932
	}
hfutrell@2744
  1933
	if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
hfutrell@2744
  1934
		return;
hfutrell@2744
  1935
	}
hfutrell@2744
  1936
	renderer = texture->renderer;
hfutrell@2744
  1937
	if (!renderer->DirtyTexture) {
hfutrell@2744
  1938
		return;
hfutrell@2744
  1939
	}
hfutrell@2744
  1940
	renderer->DirtyTexture(renderer, texture, numrects, rects);
slouken@1895
  1941
}
slouken@1895
  1942
slouken@1895
  1943
int
slouken@1985
  1944
SDL_RenderFill(Uint8 r, Uint8 g, Uint8 b, Uint8 a, const SDL_Rect * rect)
slouken@1895
  1945
{
hfutrell@2744
  1946
	SDL_Renderer   *renderer;
hfutrell@2744
  1947
	SDL_Window     *window;
hfutrell@2744
  1948
	SDL_Rect        real_rect;
slouken@1895
  1949
hfutrell@2744
  1950
	if (!_this) {
hfutrell@2744
  1951
		SDL_UninitializedVideo();
hfutrell@2744
  1952
		return -1;
hfutrell@2744
  1953
	}
hfutrell@2744
  1954
	renderer = SDL_CurrentDisplay.current_renderer;
hfutrell@2744
  1955
	if (!renderer || !renderer->RenderFill) {
hfutrell@2744
  1956
		return -1;
hfutrell@2744
  1957
	}
hfutrell@2744
  1958
	window = SDL_GetWindowFromID(renderer->window);
hfutrell@2744
  1959
	real_rect.x = 0;
hfutrell@2744
  1960
	real_rect.y = 0;
hfutrell@2744
  1961
	real_rect.w = window->w;
hfutrell@2744
  1962
	real_rect.h = window->h;
hfutrell@2744
  1963
	if (rect) {
hfutrell@2744
  1964
		if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
hfutrell@2744
  1965
			return 0;
hfutrell@2744
  1966
		}
hfutrell@2744
  1967
	}
hfutrell@2744
  1968
	return renderer->RenderFill(renderer, r, g, b, a, &real_rect);
slouken@1895
  1969
}
slouken@1895
  1970
slouken@1895
  1971
int
slouken@1895
  1972
SDL_RenderCopy(SDL_TextureID textureID, const SDL_Rect * srcrect,
hfutrell@2744
  1973
	       const SDL_Rect * dstrect)
slouken@1895
  1974
{
hfutrell@2744
  1975
	SDL_Texture    *texture = SDL_GetTextureFromID(textureID);
hfutrell@2744
  1976
	SDL_Renderer   *renderer;
hfutrell@2744
  1977
	SDL_Window     *window;
hfutrell@2744
  1978
	SDL_Rect        real_srcrect;
hfutrell@2744
  1979
	SDL_Rect        real_dstrect;
slouken@1895
  1980
hfutrell@2744
  1981
	if (!texture || texture->renderer != SDL_CurrentDisplay.current_renderer) {
hfutrell@2744
  1982
		return -1;
hfutrell@2744
  1983
	}
hfutrell@2744
  1984
	renderer = SDL_CurrentDisplay.current_renderer;
hfutrell@2744
  1985
	if (!renderer || !renderer->RenderCopy) {
hfutrell@2744
  1986
		return -1;
hfutrell@2744
  1987
	}
hfutrell@2744
  1988
	window = SDL_GetWindowFromID(renderer->window);
hfutrell@2744
  1989
	if (srcrect) {
hfutrell@2744
  1990
		real_srcrect = *srcrect;
hfutrell@2744
  1991
	} else {
hfutrell@2744
  1992
		real_srcrect.x = 0;
hfutrell@2744
  1993
		real_srcrect.y = 0;
hfutrell@2744
  1994
		real_srcrect.w = texture->w;
hfutrell@2744
  1995
		real_srcrect.h = texture->h;
hfutrell@2744
  1996
	}
hfutrell@2744
  1997
	if (dstrect) {
hfutrell@2744
  1998
		real_dstrect = *dstrect;
hfutrell@2744
  1999
	} else {
hfutrell@2744
  2000
		real_dstrect.x = 0;
hfutrell@2744
  2001
		real_dstrect.y = 0;
hfutrell@2744
  2002
		real_dstrect.w = window->w;
hfutrell@2744
  2003
		real_dstrect.h = window->h;
hfutrell@2744
  2004
	}
slouken@1895
  2005
hfutrell@2744
  2006
	return renderer->RenderCopy(renderer, texture, &real_srcrect,
hfutrell@2744
  2007
				    &real_dstrect);
slouken@1895
  2008
}
slouken@1895
  2009
slouken@1895
  2010
void
slouken@1895
  2011
SDL_RenderPresent(void)
slouken@1895
  2012
{
hfutrell@2744
  2013
	SDL_Renderer   *renderer;
slouken@1895
  2014
hfutrell@2744
  2015
	if (!_this) {
hfutrell@2744
  2016
		SDL_UninitializedVideo();
hfutrell@2744
  2017
		return;
hfutrell@2744
  2018
	}
hfutrell@2744
  2019
	renderer = SDL_CurrentDisplay.current_renderer;
hfutrell@2744
  2020
	if (!renderer || !renderer->RenderPresent) {
hfutrell@2744
  2021
		return;
hfutrell@2744
  2022
	}
hfutrell@2744
  2023
	renderer->RenderPresent(renderer);
slouken@1895
  2024
}
slouken@1895
  2025
slouken@1895
  2026
void
slouken@1895
  2027
SDL_DestroyTexture(SDL_TextureID textureID)
slouken@1895
  2028
{
hfutrell@2744
  2029
	int             hash;
hfutrell@2744
  2030
	SDL_Texture    *prev, *texture;
hfutrell@2744
  2031
	SDL_Renderer   *renderer;
slouken@1895
  2032
hfutrell@2744
  2033
	if (!_this) {
hfutrell@2744
  2034
		SDL_UninitializedVideo();
hfutrell@2744
  2035
		return;
hfutrell@2744
  2036
	}
hfutrell@2744
  2037
	/* Look up the texture in the hash table */
hfutrell@2744
  2038
	hash = (textureID % SDL_arraysize(SDL_CurrentDisplay.textures));
hfutrell@2744
  2039
	prev = NULL;
hfutrell@2744
  2040
	for (texture = SDL_CurrentDisplay.textures[hash]; texture;
hfutrell@2744
  2041
	     prev = texture, texture = texture->next) {
hfutrell@2744
  2042
		if (texture->id == textureID) {
hfutrell@2744
  2043
			break;
hfutrell@2744
  2044
		}
hfutrell@2744
  2045
	}
hfutrell@2744
  2046
	if (!texture) {
hfutrell@2744
  2047
		return;
hfutrell@2744
  2048
	}
hfutrell@2744
  2049
	/* Unlink the texture from the list */
hfutrell@2744
  2050
	if (prev) {
hfutrell@2744
  2051
		prev->next = texture->next;
hfutrell@2744
  2052
	} else {
hfutrell@2744
  2053
		SDL_CurrentDisplay.textures[hash] = texture->next;
hfutrell@2744
  2054
	}
slouken@1895
  2055
hfutrell@2744
  2056
	/* Free the texture */
hfutrell@2744
  2057
	renderer = texture->renderer;
hfutrell@2744
  2058
	renderer->DestroyTexture(renderer, texture);
hfutrell@2744
  2059
	SDL_free(texture);
slouken@1895
  2060
}
slouken@1895
  2061
slouken@1895
  2062
void
slouken@1895
  2063
SDL_DestroyRenderer(SDL_WindowID windowID)
slouken@1895
  2064
{
hfutrell@2744
  2065
	SDL_Window     *window = SDL_GetWindowFromID(windowID);
hfutrell@2744
  2066
	SDL_Renderer   *renderer;
hfutrell@2744
  2067
	int             i;
slouken@1895
  2068
hfutrell@2744
  2069
	if (!window) {
hfutrell@2744
  2070
		return;
hfutrell@2744
  2071
	}
hfutrell@2744
  2072
	renderer = window->renderer;
hfutrell@2744
  2073
	if (!renderer) {
hfutrell@2744
  2074
		return;
hfutrell@2744
  2075
	}
hfutrell@2744
  2076
	/* Free existing textures for this renderer */
hfutrell@2744
  2077
	for (i = 0; i < SDL_arraysize(SDL_CurrentDisplay.textures); ++i) {
hfutrell@2744
  2078
		SDL_Texture    *texture;
hfutrell@2744
  2079
		SDL_Texture    *prev = NULL;
hfutrell@2744
  2080
		SDL_Texture    *next;
hfutrell@2744
  2081
		for (texture = SDL_CurrentDisplay.textures[i]; texture;
hfutrell@2744
  2082
		     texture = next) {
hfutrell@2744
  2083
			next = texture->next;
hfutrell@2744
  2084
			if (texture->renderer == renderer) {
hfutrell@2744
  2085
				if (prev) {
hfutrell@2744
  2086
					prev->next = next;
hfutrell@2744
  2087
				} else {
hfutrell@2744
  2088
					SDL_CurrentDisplay.textures[i] = next;
hfutrell@2744
  2089
				}
hfutrell@2744
  2090
				renderer->DestroyTexture(renderer, texture);
hfutrell@2744
  2091
				SDL_free(texture);
hfutrell@2744
  2092
			} else {
hfutrell@2744
  2093
				prev = texture;
hfutrell@2744
  2094
			}
hfutrell@2744
  2095
		}
hfutrell@2744
  2096
	}
slouken@1895
  2097
hfutrell@2744
  2098
	/* Free the renderer instance */
hfutrell@2744
  2099
	renderer->DestroyRenderer(renderer);
slouken@1895
  2100
hfutrell@2744
  2101
	/* Clear references */
hfutrell@2744
  2102
	window->renderer = NULL;
hfutrell@2744
  2103
	if (SDL_CurrentDisplay.current_renderer == renderer) {
hfutrell@2744
  2104
		SDL_CurrentDisplay.current_renderer = NULL;
hfutrell@2744
  2105
	}
slouken@1895
  2106
}
slouken@1895
  2107
slouken@1895
  2108
void
slouken@1895
  2109
SDL_VideoQuit(void)
slouken@1895
  2110
{
hfutrell@2744
  2111
	int             i, j;
slouken@1895
  2112
hfutrell@2744
  2113
	if (!_this) {
hfutrell@2744
  2114
		return;
hfutrell@2744
  2115
	}
hfutrell@2744
  2116
	/* Halt event processing before doing anything else */
hfutrell@2744
  2117
	SDL_StopEventLoop();
slouken@1895
  2118
hfutrell@2744
  2119
	/* Clean up the system video */
hfutrell@2744
  2120
	for (i = _this->num_displays; i--;) {
hfutrell@2744
  2121
		SDL_VideoDisplay *display = &_this->displays[i];
hfutrell@2744
  2122
		for (j = display->num_windows; j--;) {
hfutrell@2744
  2123
			SDL_DestroyWindow(display->windows[i].id);
hfutrell@2744
  2124
		}
hfutrell@2744
  2125
		if (display->windows) {
hfutrell@2744
  2126
			SDL_free(display->windows);
hfutrell@2744
  2127
			display->windows = NULL;
hfutrell@2744
  2128
		}
hfutrell@2744
  2129
		display->num_windows = 0;
hfutrell@2744
  2130
		if (display->render_drivers) {
hfutrell@2744
  2131
			SDL_free(display->render_drivers);
hfutrell@2744
  2132
			display->render_drivers = NULL;
hfutrell@2744
  2133
		}
hfutrell@2744
  2134
		display->num_render_drivers = 0;
hfutrell@2744
  2135
	}
hfutrell@2744
  2136
	_this->VideoQuit(_this);
slouken@1895
  2137
hfutrell@2744
  2138
	for (i = _this->num_displays; i--;) {
hfutrell@2744
  2139
		SDL_VideoDisplay *display = &_this->displays[i];
hfutrell@2744
  2140
		for (j = display->num_display_modes; j--;) {
hfutrell@2744
  2141
			if (display->display_modes[j].driverdata) {
hfutrell@2744
  2142
				SDL_free(display->display_modes[j].driverdata);
hfutrell@2744
  2143
				display->display_modes[j].driverdata = NULL;
hfutrell@2744
  2144
			}
hfutrell@2744
  2145
		}
hfutrell@2744
  2146
		if (display->display_modes) {
hfutrell@2744
  2147
			SDL_free(display->display_modes);
hfutrell@2744
  2148
			display->display_modes = NULL;
hfutrell@2744
  2149
		}
hfutrell@2744
  2150
		if (display->desktop_mode.driverdata) {
hfutrell@2744
  2151
			SDL_free(display->desktop_mode.driverdata);
hfutrell@2744
  2152
			display->desktop_mode.driverdata = NULL;
hfutrell@2744
  2153
		}
hfutrell@2744
  2154
		if (display->palette) {
hfutrell@2744
  2155
			SDL_FreePalette(display->palette);
hfutrell@2744
  2156
			display->palette = NULL;
hfutrell@2744
  2157
		}
hfutrell@2744
  2158
		if (display->gamma) {
hfutrell@2744
  2159
			SDL_free(display->gamma);
hfutrell@2744
  2160
			display->gamma = NULL;
hfutrell@2744
  2161
		}
hfutrell@2744
  2162
		if (display->driverdata) {
hfutrell@2744
  2163
			SDL_free(display->driverdata);
hfutrell@2744
  2164
			display->driverdata = NULL;
hfutrell@2744
  2165
		}
hfutrell@2744
  2166
	}
hfutrell@2744
  2167
	if (_this->displays) {
hfutrell@2744
  2168
		SDL_free(_this->displays);
hfutrell@2744
  2169
		_this->displays = NULL;
hfutrell@2744
  2170
	}
hfutrell@2744
  2171
	_this->free(_this);
hfutrell@2744
  2172
	_this = NULL;
slouken@0
  2173
}
slouken@0
  2174
slouken@1895
  2175
int
slouken@1895
  2176
SDL_GL_LoadLibrary(const char *path)
slouken@0
  2177
{
hfutrell@2744
  2178
	int             retval;
slouken@0
  2179
hfutrell@2744
  2180
	if (!_this) {
hfutrell@2744
  2181
		SDL_UninitializedVideo();
hfutrell@2744
  2182
		return -1;
hfutrell@2744
  2183
	}
hfutrell@2744
  2184
	if (_this->GL_LoadLibrary) {
hfutrell@2744
  2185
		retval = _this->GL_LoadLibrary(_this, path);
hfutrell@2744
  2186
	} else {
hfutrell@2744
  2187
		SDL_SetError("No dynamic GL support in video driver");
hfutrell@2744
  2188
		retval = -1;
hfutrell@2744
  2189
	}
hfutrell@2744
  2190
	return (retval);
slouken@0
  2191
}
slouken@0
  2192
hfutrell@2744
  2193
void           *
slouken@1895
  2194
SDL_GL_GetProcAddress(const char *proc)
slouken@0
  2195
{
hfutrell@2744
  2196
	void           *func;
slouken@0
  2197
hfutrell@2744
  2198
	if (!_this) {
hfutrell@2744
  2199
		SDL_UninitializedVideo();
hfutrell@2744
  2200
		return NULL;
hfutrell@2744
  2201
	}
hfutrell@2744
  2202
	func = NULL;
hfutrell@2744
  2203
	if (_this->GL_GetProcAddress) {
hfutrell@2744
  2204
		if (_this->gl_config.driver_loaded) {
hfutrell@2744
  2205
			func = _this->GL_GetProcAddress(_this, proc);
hfutrell@2744
  2206
		} else {
hfutrell@2744
  2207
			SDL_SetError("No GL driver has been loaded");
hfutrell@2744
  2208
		}
hfutrell@2744
  2209
	} else {
hfutrell@2744
  2210
		SDL_SetError("No dynamic GL support in video driver");
hfutrell@2744
  2211
	}
hfutrell@2744
  2212
	return func;
slouken@0
  2213
}
slouken@0
  2214
slouken@1926
  2215
SDL_bool
slouken@1926
  2216
SDL_GL_ExtensionSupported(const char *extension)
slouken@1926
  2217
{
hfutrell@2745
  2218
#if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
hfutrell@2744
  2219
	const GLubyte  *(APIENTRY * glGetStringFunc) (GLenum);
hfutrell@2744
  2220
	const char     *extensions;
hfutrell@2744
  2221
	const char     *start;
hfutrell@2744
  2222
	const char     *where, *terminator;
slouken@1926
  2223
hfutrell@2744
  2224
	/* Extension names should not have spaces. */
hfutrell@2744
  2225
	where = SDL_strchr(extension, ' ');
hfutrell@2744
  2226
	if (where || *extension == '\0') {
hfutrell@2744
  2227
		return SDL_FALSE;
hfutrell@2744
  2228
	}
hfutrell@2744
  2229
	/* See if there's an environment variable override */
hfutrell@2744
  2230
	start = SDL_getenv(extension);
hfutrell@2744
  2231
	if (start && *start == '0') {
hfutrell@2744
  2232
		return SDL_FALSE;
hfutrell@2744
  2233
	}
hfutrell@2744
  2234
	/* Lookup the available extensions */
hfutrell@2744
  2235
	glGetStringFunc = SDL_GL_GetProcAddress("glGetString");
hfutrell@2744
  2236
	if (glGetStringFunc) {
hfutrell@2744
  2237
		extensions = (const char *) glGetStringFunc(GL_EXTENSIONS);
hfutrell@2744
  2238
	} else {
hfutrell@2744
  2239
		extensions = NULL;
hfutrell@2744
  2240
	}
hfutrell@2744
  2241
	if (!extensions) {
hfutrell@2744
  2242
		return SDL_FALSE;
hfutrell@2744
  2243
	}
hfutrell@2744
  2244
	/*
hfutrell@2744
  2245
	 * It takes a bit of care to be fool-proof about parsing the OpenGL
hfutrell@2744
  2246
	 * extensions string. Don't be fooled by sub-strings, etc.
hfutrell@2744
  2247
	 */
slouken@1926
  2248
hfutrell@2744
  2249
	start = extensions;
slouken@1926
  2250
hfutrell@2744
  2251
	for (;;) {
hfutrell@2744
  2252
		where = SDL_strstr(start, extension);
hfutrell@2744
  2253
		if (!where)
hfutrell@2744
  2254
			break;
slouken@1926
  2255
hfutrell@2744
  2256
		terminator = where + SDL_strlen(extension);
hfutrell@2744
  2257
		if (where == start || *(where - 1) == ' ')
hfutrell@2744
  2258
			if (*terminator == ' ' || *terminator == '\0')
hfutrell@2744
  2259
				return SDL_TRUE;
slouken@1926
  2260
hfutrell@2744
  2261
		start = terminator;
hfutrell@2744
  2262
	}
hfutrell@2744
  2263
	return SDL_FALSE;
slouken@1926
  2264
#else
hfutrell@2744
  2265
	return SDL_FALSE;
slouken@1926
  2266
#endif
slouken@1926
  2267
}
slouken@1926
  2268
slouken@1895
  2269
int
slouken@1895
  2270
SDL_GL_SetAttribute(SDL_GLattr attr, int value)
slouken@0
  2271
{
hfutrell@2745
  2272
#if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
hfutrell@2744
  2273
	int             retval;
slouken@0
  2274
hfutrell@2744
  2275
	if (!_this) {
hfutrell@2744
  2276
		SDL_UninitializedVideo();
hfutrell@2744
  2277
		return -1;
hfutrell@2744
  2278
	}
hfutrell@2744
  2279
	retval = 0;
hfutrell@2744
  2280
	switch (attr) {
hfutrell@2744
  2281
	case SDL_GL_RED_SIZE:
hfutrell@2744
  2282
		_this->gl_config.red_size = value;
hfutrell@2744
  2283
		break;
hfutrell@2744
  2284
	case SDL_GL_GREEN_SIZE:
hfutrell@2744
  2285
		_this->gl_config.green_size = value;
hfutrell@2744
  2286
		break;
hfutrell@2744
  2287
	case SDL_GL_BLUE_SIZE:
hfutrell@2744
  2288
		_this->gl_config.blue_size = value;
hfutrell@2744
  2289
		break;
hfutrell@2744
  2290
	case SDL_GL_ALPHA_SIZE:
hfutrell@2744
  2291
		_this->gl_config.alpha_size = value;
hfutrell@2744
  2292
		break;
hfutrell@2744
  2293
	case SDL_GL_DOUBLEBUFFER:
hfutrell@2744
  2294
		_this->gl_config.double_buffer = value;
hfutrell@2744
  2295
		break;
hfutrell@2744
  2296
	case SDL_GL_BUFFER_SIZE:
hfutrell@2744
  2297
		_this->gl_config.buffer_size = value;
hfutrell@2744
  2298
		break;
hfutrell@2744
  2299
	case SDL_GL_DEPTH_SIZE:
hfutrell@2744
  2300
		_this->gl_config.depth_size = value;
hfutrell@2744
  2301
		break;
hfutrell@2744
  2302
	case SDL_GL_STENCIL_SIZE:
hfutrell@2744
  2303
		_this->gl_config.stencil_size = value;
hfutrell@2744
  2304
		break;
hfutrell@2744
  2305
	case SDL_GL_ACCUM_RED_SIZE:
hfutrell@2744
  2306
		_this->gl_config.accum_red_size = value;
hfutrell@2744
  2307
		break;
hfutrell@2744
  2308
	case SDL_GL_ACCUM_GREEN_SIZE:
hfutrell@2744
  2309
		_this->gl_config.accum_green_size = value;
hfutrell@2744
  2310
		break;
hfutrell@2744
  2311
	case SDL_GL_ACCUM_BLUE_SIZE:
hfutrell@2744
  2312
		_this->gl_config.accum_blue_size = value;
hfutrell@2744
  2313
		break;
hfutrell@2744
  2314
	case SDL_GL_ACCUM_ALPHA_SIZE:
hfutrell@2744
  2315
		_this->gl_config.accum_alpha_size = value;
hfutrell@2744
  2316
		break;
hfutrell@2744
  2317
	case SDL_GL_STEREO:
hfutrell@2744
  2318
		_this->gl_config.stereo = value;
hfutrell@2744
  2319
		break;
hfutrell@2744
  2320
	case SDL_GL_MULTISAMPLEBUFFERS:
hfutrell@2744
  2321
		_this->gl_config.multisamplebuffers = value;
hfutrell@2744
  2322
		break;
hfutrell@2744
  2323
	case SDL_GL_MULTISAMPLESAMPLES:
hfutrell@2744
  2324
		_this->gl_config.multisamplesamples = value;
hfutrell@2744
  2325
		break;
hfutrell@2744
  2326
	case SDL_GL_ACCELERATED_VISUAL:
hfutrell@2744
  2327
		_this->gl_config.accelerated = value;
hfutrell@2744
  2328
		break;
hfutrell@2745
  2329
	case SDL_GL_RETAINED_BACKING:
hfutrell@2745
  2330
		_this->gl_config.retained_backing = value;
hfutrell@2745
  2331
		break;
hfutrell@2744
  2332
	default:
hfutrell@2744
  2333
		SDL_SetError("Unknown OpenGL attribute");
hfutrell@2744
  2334
		retval = -1;
hfutrell@2744
  2335
		break;
hfutrell@2744
  2336
	}
hfutrell@2744
  2337
	return retval;
slouken@1936
  2338
#else
hfutrell@2744
  2339
	SDL_Unsupported();
hfutrell@2744
  2340
	return -1;
hfutrell@2744
  2341
#endif				/* SDL_VIDEO_OPENGL */
slouken@0
  2342
}
slouken@0
  2343
slouken@1895
  2344
int
slouken@1936
  2345
SDL_GL_GetAttribute(SDL_GLattr attr, int *value)
slouken@0
  2346
{
hfutrell@2745
  2347
#if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
hfutrell@2744
  2348
	void            (APIENTRY * glGetIntegervFunc) (GLenum pname, GLint * params);
hfutrell@2744
  2349
	GLenum          attrib = 0;
slouken@1912
  2350
hfutrell@2744
  2351
	glGetIntegervFunc = SDL_GL_GetProcAddress("glGetIntegerv");
hfutrell@2744
  2352
	if (!glGetIntegervFunc) {
hfutrell@2744
  2353
		return -1;
hfutrell@2744
  2354
	}
hfutrell@2744
  2355
	switch (attr) {
hfutrell@2745
  2356
	case SDL_GL_RETAINED_BACKING:
hfutrell@2745
  2357
		*value = _this->gl_config.retained_backing;
hfutrell@2745
  2358
		return 0;
hfutrell@2744
  2359
	case SDL_GL_RED_SIZE:
hfutrell@2744
  2360
		attrib = GL_RED_BITS;
hfutrell@2744
  2361
		break;
hfutrell@2744
  2362
	case SDL_GL_BLUE_SIZE:
hfutrell@2744
  2363
		attrib = GL_BLUE_BITS;
hfutrell@2744
  2364
		break;
hfutrell@2744
  2365
	case SDL_GL_GREEN_SIZE:
hfutrell@2744
  2366
		attrib = GL_GREEN_BITS;
hfutrell@2744
  2367
		break;
hfutrell@2744
  2368
	case SDL_GL_ALPHA_SIZE:
hfutrell@2744
  2369
		attrib = GL_ALPHA_BITS;
hfutrell@2744
  2370
		break;
hfutrell@2744
  2371
	case SDL_GL_DOUBLEBUFFER:
hfutrell@2745
  2372
#ifndef SDL_VIDEO_OPENGL_ES
hfutrell@2744
  2373
		attrib = GL_DOUBLEBUFFER;
hfutrell@2744
  2374
		break;
hfutrell@2745
  2375
#else
hfutrell@2745
  2376
		/*
hfutrell@2745
  2377
		 * I believe double buffering is the only option in OpenGL ES
hfutrell@2745
  2378
		 * -- in any case, GL_DOUBLEBUFFER doesn't exist
hfutrell@2745
  2379
		 */
hfutrell@2745
  2380
		*value = 1;
hfutrell@2745
  2381
		return 0;
hfutrell@2745
  2382
#endif
hfutrell@2744
  2383
	case SDL_GL_DEPTH_SIZE:
hfutrell@2744
  2384
		attrib = GL_DEPTH_BITS;
hfutrell@2744
  2385
		break;
hfutrell@2744
  2386
	case SDL_GL_STENCIL_SIZE:
hfutrell@2744
  2387
		attrib = GL_STENCIL_BITS;
hfutrell@2744
  2388
		break;
hfutrell@2745
  2389
#ifndef SDL_VIDEO_OPENGL_ES
hfutrell@2744
  2390
	case SDL_GL_ACCUM_RED_SIZE:
hfutrell@2744
  2391
		attrib = GL_ACCUM_RED_BITS;
hfutrell@2744
  2392
		break;
hfutrell@2744
  2393
	case SDL_GL_ACCUM_GREEN_SIZE:
hfutrell@2744
  2394
		attrib = GL_ACCUM_GREEN_BITS;
hfutrell@2744
  2395
		break;
hfutrell@2744
  2396
	case SDL_GL_ACCUM_BLUE_SIZE:
hfutrell@2744
  2397
		attrib = GL_ACCUM_BLUE_BITS;
hfutrell@2744
  2398
		break;
hfutrell@2744
  2399
	case SDL_GL_ACCUM_ALPHA_SIZE:
hfutrell@2744
  2400
		attrib = GL_ACCUM_ALPHA_BITS;
hfutrell@2744
  2401
		break;
hfutrell@2744
  2402
	case SDL_GL_STEREO:
hfutrell@2744
  2403
		attrib = GL_STEREO;
hfutrell@2744
  2404
		break;
hfutrell@2745
  2405
#else
hfutrell@2745
  2406
	case SDL_GL_ACCUM_RED_SIZE:
hfutrell@2745
  2407
	case SDL_GL_ACCUM_GREEN_SIZE:
hfutrell@2745
  2408
	case SDL_GL_ACCUM_BLUE_SIZE:
hfutrell@2745
  2409
	case SDL_GL_ACCUM_ALPHA_SIZE:
hfutrell@2745
  2410
	case SDL_GL_STEREO:
hfutrell@2745
  2411
		/* none of these are supported in OpenGL ES */
hfutrell@2745
  2412
		*value = 0;
hfutrell@2745
  2413
		return 0;
hfutrell@2745
  2414
#endif
hfutrell@2744
  2415
	case SDL_GL_MULTISAMPLEBUFFERS:
hfutrell@2745
  2416
#ifndef SDL_VIDEO_OPENGL_ES
hfutrell@2744
  2417
		attrib = GL_SAMPLE_BUFFERS_ARB;
hfutrell@2745
  2418
#else
hfutrell@2745
  2419
		attrib = GL_SAMPLE_BUFFERS;
hfutrell@2745
  2420
#endif
hfutrell@2744
  2421
		break;
hfutrell@2744
  2422
	case SDL_GL_MULTISAMPLESAMPLES:
hfutrell@2745
  2423
#ifndef SDL_VIDEO_OPENGL_ES
hfutrell@2744
  2424
		attrib = GL_SAMPLES_ARB;
hfutrell@2745
  2425
#else
hfutrell@2745
  2426
		attrib = GL_SAMPLES;
hfutrell@2745
  2427
#endif
hfutrell@2744
  2428
		break;
hfutrell@2744
  2429
	case SDL_GL_BUFFER_SIZE:
hfutrell@2744
  2430
		{
hfutrell@2744
  2431
			GLint           bits = 0;
hfutrell@2744
  2432
			GLint           component;
slouken@1936
  2433
hfutrell@2744
  2434
			/*
hfutrell@2744
  2435
			 * there doesn't seem to be a single flag in OpenGL
hfutrell@2744
  2436
			 * for this!
hfutrell@2744
  2437
			 */
hfutrell@2744
  2438
			glGetIntegervFunc(GL_RED_BITS, &component);
hfutrell@2744
  2439
			bits += component;
hfutrell@2744
  2440
			glGetIntegervFunc(GL_GREEN_BITS, &component);
hfutrell@2744
  2441
			bits += component;
hfutrell@2744
  2442
			glGetIntegervFunc(GL_BLUE_BITS, &component);
hfutrell@2744
  2443
			bits += component;
hfutrell@2744
  2444
			glGetIntegervFunc(GL_ALPHA_BITS, &component);
hfutrell@2744
  2445
			bits += component;
slouken@1936
  2446
hfutrell@2744
  2447
			*value = bits;
hfutrell@2744
  2448
			return 0;
hfutrell@2744
  2449
		}
hfutrell@2744
  2450
	case SDL_GL_ACCELERATED_VISUAL:
hfutrell@2744
  2451
		{
hfutrell@2744
  2452
			/* FIXME: How do we get this information? */
hfutrell@2744
  2453
			*value = (_this->gl_config.accelerated != 0);
hfutrell@2744
  2454
			return 0;
hfutrell@2744
  2455
		}
hfutrell@2744
  2456
	default:
hfutrell@2744
  2457
		SDL_SetError("Unknown OpenGL attribute");
hfutrell@2744
  2458
		return -1;
hfutrell@2744
  2459
	}
slouken@0
  2460
hfutrell@2744
  2461
	glGetIntegervFunc(attrib, (GLint *) value);
hfutrell@2744
  2462
	return 0;
slouken@1936
  2463
#else
hfutrell@2744
  2464
	SDL_Unsupported();
hfutrell@2744
  2465
	return -1;
hfutrell@2744
  2466
#endif				/* SDL_VIDEO_OPENGL */
slouken@0
  2467
}
slouken@0
  2468
slouken@1912
  2469
SDL_GLContext
slouken@1912
  2470
SDL_GL_CreateContext(SDL_WindowID windowID)
slouken@1912
  2471
{
hfutrell@2744
  2472
	SDL_Window     *window = SDL_GetWindowFromID(windowID);
slouken@1912
  2473
hfutrell@2744
  2474
	if (!window) {
hfutrell@2744
  2475
		return NULL;
hfutrell@2744
  2476
	}
hfutrell@2744
  2477
	if (!(window->flags & SDL_WINDOW_OPENGL)) {
hfutrell@2744
  2478
		SDL_SetError("The specified window isn't an OpenGL window");
hfutrell@2744
  2479
		return NULL;
hfutrell@2744
  2480
	}
hfutrell@2744
  2481
	return _this->GL_CreateContext(_this, window);
slouken@1912
  2482
}
slouken@1912
  2483
slouken@1912
  2484
int
slouken@1912
  2485
SDL_GL_MakeCurrent(SDL_WindowID windowID, SDL_GLContext context)
slouken@1912
  2486
{
hfutrell@2744
  2487
	SDL_Window     *window = SDL_GetWindowFromID(windowID);
slouken@1912
  2488
hfutrell@2744
  2489
	if (window && !(window->flags & SDL_WINDOW_OPENGL)) {
hfutrell@2744
  2490
		SDL_SetError("The specified window isn't an OpenGL window");
hfutrell@2744
  2491
		return -1;
hfutrell@2744
  2492
	}
hfutrell@2744
  2493
	if (!context) {
hfutrell@2744
  2494
		window = NULL;
hfutrell@2744
  2495
	}
hfutrell@2744
  2496
	return _this->GL_MakeCurrent(_this, window, context);
slouken@1912
  2497
}
slouken@1912
  2498
slouken@1912
  2499
int
slouken@1912
  2500
SDL_GL_SetSwapInterval(int interval)
slouken@1912
  2501
{
hfutrell@2744
  2502
	if (!_this) {
hfutrell@2744
  2503
		SDL_UninitializedVideo();
hfutrell@2744
  2504
		return -1;
hfutrell@2744
  2505
	}
hfutrell@2744
  2506
	if (_this->GL_SetSwapInterval) {
hfutrell@2744
  2507
		return _this->GL_SetSwapInterval(_this, interval);
hfutrell@2744
  2508
	} else {
hfutrell@2744
  2509
		SDL_SetError("Setting the swap interval is not supported");
hfutrell@2744
  2510
		return -1;
hfutrell@2744
  2511
	}
slouken@1912
  2512
}
slouken@1912
  2513
slouken@1912
  2514
int
slouken@1912
  2515
SDL_GL_GetSwapInterval(void)
slouken@1912
  2516
{
hfutrell@2744
  2517
	if (!_this) {
hfutrell@2744
  2518
		SDL_UninitializedVideo();
hfutrell@2744
  2519
		return -1;
hfutrell@2744
  2520
	}
hfutrell@2744
  2521
	if (_this->GL_GetSwapInterval) {
hfutrell@2744
  2522
		return _this->GL_GetSwapInterval(_this);
hfutrell@2744
  2523
	} else {
hfutrell@2744
  2524
		SDL_SetError("Getting the swap interval is not supported");
hfutrell@2744
  2525
		return -1;
hfutrell@2744
  2526
	}
slouken@1912
  2527
}
slouken@1912
  2528
slouken@1895
  2529
void
slouken@1912
  2530
SDL_GL_SwapWindow(SDL_WindowID windowID)
slouken@0
  2531
{
hfutrell@2744
  2532
	SDL_Window     *window = SDL_GetWindowFromID(windowID);
slouken@1912
  2533
hfutrell@2744
  2534
	if (!window) {
hfutrell@2744
  2535
		return;
hfutrell@2744
  2536
	}
hfutrell@2744
  2537
	if (!(window->flags & SDL_WINDOW_OPENGL)) {
hfutrell@2744
  2538
		SDL_SetError("The specified window isn't an OpenGL window");
hfutrell@2744
  2539
		return;
hfutrell@2744
  2540
	}
hfutrell@2744
  2541
	_this->GL_SwapWindow(_this, window);
slouken@1912
  2542
}
slouken@1912
  2543
slouken@1912
  2544
void
slouken@1912
  2545
SDL_GL_DeleteContext(SDL_GLContext context)
slouken@1912
  2546
{
hfutrell@2744
  2547
	if (!_this || !context) {
hfutrell@2744
  2548
		return;
hfutrell@2744
  2549
	}
hfutrell@2744
  2550
	_this->GL_MakeCurrent(_this, NULL, NULL);
hfutrell@2744
  2551
	_this->GL_DeleteContext(_this, context);
slouken@0
  2552
}
slouken@0
  2553
slouken@1895
  2554
#if 0                           // FIXME
hfutrell@2744
  2555
/*
hfutrell@2744
  2556
 * Utility function used by SDL_WM_SetIcon(); flags & 1 for color key, flags
hfutrell@2744
  2557
 * & 2 for alpha channel.
hfutrell@2744
  2558
 */
slouken@1895
  2559
static void
slouken@1895
  2560
CreateMaskFromColorKeyOrAlpha(SDL_Surface * icon, Uint8 * mask, int flags)
slouken@0
  2561
{
hfutrell@2744
  2562
	int             x, y;
hfutrell@2744
  2563
	Uint32          colorkey;
slouken@0
  2564
#define SET_MASKBIT(icon, x, y, mask) \
slouken@0
  2565
	mask[(y*((icon->w+7)/8))+(x/8)] &= ~(0x01<<(7-(x%8)))
slouken@0
  2566
hfutrell@2744
  2567
	colorkey = icon->format->colorkey;
hfutrell@2744
  2568
	switch (icon->format->BytesPerPixel) {
hfutrell@2744
  2569
	case 1:
hfutrell@2744
  2570
		{
hfutrell@2744
  2571
			Uint8          *pixels;
hfutrell@2744
  2572
			for (y = 0; y < icon->h; ++y) {
hfutrell@2744
  2573
				pixels = (Uint8 *) icon->pixels + y * icon->pitch;
hfutrell@2744
  2574
				for (x = 0; x < icon->w; ++x) {
hfutrell@2744
  2575
					if (*pixels++ == colorkey) {
hfutrell@2744
  2576
						SET_MASKBIT(icon, x, y, mask);
hfutrell@2744
  2577
					}
hfutrell@2744
  2578
				}
hfutrell@2744
  2579
			}
hfutrell@2744
  2580
		}
hfutrell@2744
  2581
		break;
slouken@0
  2582
hfutrell@2744
  2583
	case 2:
hfutrell@2744
  2584
		{
hfutrell@2744
  2585
			Uint16         *pixels;
hfutrell@2744
  2586
			for (y = 0; y < icon->h; ++y) {
hfutrell@2744
  2587
				pixels = (Uint16 *) icon->pixels + y * icon->pitch / 2;
hfutrell@2744
  2588
				for (x = 0; x < icon->w; ++x) {
hfutrell@2744
  2589
					if ((flags & 1) && *pixels == colorkey) {
hfutrell@2744
  2590
						SET_MASKBIT(icon, x, y, mask);
hfutrell@2744
  2591
					} else if ((flags & 2)
hfutrell@2744
  2592
						   && (*pixels & icon->format->Amask) == 0) {
hfutrell@2744
  2593
						SET_MASKBIT(icon, x, y, mask);
hfutrell@2744
  2594
					}
hfutrell@2744
  2595
					pixels++;
hfutrell@2744
  2596
				}
hfutrell@2744
  2597
			}
hfutrell@2744
  2598
		}
hfutrell@2744
  2599
		break;
slouken@0
  2600
hfutrell@2744
  2601
	case 4:
hfutrell@2744
  2602
		{
hfutrell@2744
  2603
			Uint32         *pixels;
hfutrell@2744
  2604
			for (y = 0; y < icon->h; ++y) {
hfutrell@2744
  2605
				pixels = (Uint32 *) icon->pixels + y * icon->pitch / 4;
hfutrell@2744
  2606
				for (x = 0; x < icon->w; ++x) {
hfutrell@2744
  2607
					if ((flags & 1) && *pixels == colorkey) {
hfutrell@2744
  2608
						SET_MASKBIT(icon, x, y, mask);
hfutrell@2744
  2609
					} else if ((flags & 2)
hfutrell@2744
  2610
						   && (*pixels & icon->format->Amask) == 0) {
hfutrell@2744
  2611
						SET_MASKBIT(icon, x, y, mask);
hfutrell@2744
  2612
					}
hfutrell@2744
  2613
					pixels++;
hfutrell@2744
  2614
				}
hfutrell@2744
  2615
			}
hfutrell@2744
  2616
		}
hfutrell@2744
  2617
		break;
hfutrell@2744
  2618
	}
slouken@0
  2619
}
slouken@0
  2620
slouken@0
  2621
/*
slouken@0
  2622
 * Sets the window manager icon for the display window.
slouken@0
  2623
 */
slouken@1895
  2624
void
slouken@1895
  2625
SDL_WM_SetIcon(SDL_Surface * icon, Uint8 * mask)
slouken@0
  2626
{
hfutrell@2744
  2627
	if (icon && _this->SetIcon) {
hfutrell@2744
  2628
		/* Generate a mask if necessary, and create the icon! */
hfutrell@2744
  2629
		if (mask == NULL) {
hfutrell@2744
  2630
			int             mask_len = icon->h * (icon->w + 7) / 8;
hfutrell@2744
  2631
			int             flags = 0;
hfutrell@2744
  2632
			mask = (Uint8 *) SDL_malloc(mask_len);
hfutrell@2744
  2633
			if (mask == NULL) {
hfutrell@2744
  2634
				return;
hfutrell@2744
  2635
			}
hfutrell@2744
  2636
			SDL_memset(mask, ~0, mask_len);
hfutrell@2744
  2637
			if (icon->flags & SDL_SRCCOLORKEY)
hfutrell@2744
  2638
				flags |= 1;
hfutrell@2744
  2639
			if (icon->flags & SDL_SRCALPHA)
hfutrell@2744
  2640
				flags |= 2;
hfutrell@2744
  2641
			if (flags) {
hfutrell@2744
  2642
				CreateMaskFromColorKeyOrAlpha(icon, mask, flags);
hfutrell@2744
  2643
			}
hfutrell@2744
  2644
			_this->SetIcon(_this, icon, mask);
hfutrell@2744
  2645
			SDL_free(mask);
hfutrell@2744
  2646
		} else {
hfutrell@2744
  2647
			_this->SetIcon(_this, icon, mask);
hfutrell@2744
  2648
		}
hfutrell@2744
  2649
	}
slouken@1895
  2650
}
slouken@1895
  2651
#endif
slouken@0
  2652
slouken@1895
  2653
SDL_bool
hfutrell@2744
  2654
SDL_GetWindowWMInfo(SDL_WindowID windowID, struct SDL_SysWMinfo * info)
slouken@1895
  2655
{
hfutrell@2744
  2656
	SDL_Window     *window = SDL_GetWindowFromID(windowID);
slouken@1895
  2657
hfutrell@2744
  2658
	if (!window || !_this->GetWindowWMInfo) {
hfutrell@2744
  2659
		return SDL_FALSE;
hfutrell@2744
  2660
	}
hfutrell@2744
  2661
	return (_this->GetWindowWMInfo(_this, window, info));
slouken@0
  2662
}
slouken@0
  2663
slouken@1895
  2664
/* vi: set ts=4 sw=4 expandtab: */