src/video/glesrenderer/SDL_renderer_gles.c
author Holmes Futrell
Fri, 15 Aug 2008 00:44:07 +0000
branchgsoc2008_iphone
changeset 2426 aad0c5ccf6bb
parent 2359 b70b96e615d2
child 2456 da4fd7cf8745
permissions -rw-r--r--
Removed log messages
hfutrell@2359
     1
/*
hfutrell@2359
     2
    SDL - Simple DirectMedia Layer
hfutrell@2359
     3
    Copyright (C) 1997-2006 Sam Lantinga
hfutrell@2359
     4
hfutrell@2359
     5
    This library is free software; you can redistribute it and/or
hfutrell@2359
     6
    modify it under the terms of the GNU Lesser General Public
hfutrell@2359
     7
    License as published by the Free Software Foundation; either
hfutrell@2359
     8
    version 2.1 of the License, or (at your option) any later version.
hfutrell@2359
     9
hfutrell@2359
    10
    This library is distributed in the hope that it will be useful,
hfutrell@2359
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
hfutrell@2359
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
hfutrell@2359
    13
    Lesser General Public License for more details.
hfutrell@2359
    14
hfutrell@2359
    15
    You should have received a copy of the GNU Lesser General Public
hfutrell@2359
    16
    License along with this library; if not, write to the Free Software
hfutrell@2359
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
hfutrell@2359
    18
hfutrell@2359
    19
    Sam Lantinga
hfutrell@2359
    20
    slouken@libsdl.org
hfutrell@2359
    21
*/
hfutrell@2359
    22
#include "SDL_config.h"
hfutrell@2359
    23
hfutrell@2359
    24
#if SDL_VIDEO_RENDER_OGL_ES
hfutrell@2359
    25
hfutrell@2359
    26
#include "SDL_video.h"
hfutrell@2359
    27
#include "SDL_opengles.h"
hfutrell@2359
    28
#include "SDL_sysvideo.h"
hfutrell@2359
    29
#include "SDL_pixels_c.h"
hfutrell@2359
    30
#include "SDL_rect_c.h"
hfutrell@2359
    31
#include "SDL_yuv_sw_c.h"
hfutrell@2359
    32
hfutrell@2359
    33
/* OpenGL ES 1.1 renderer implementation */
hfutrell@2359
    34
hfutrell@2359
    35
static const float inv255f = 1.0f / 255.0f;
hfutrell@2359
    36
hfutrell@2359
    37
static SDL_Renderer *GL_CreateRenderer(SDL_Window * window, Uint32 flags);
hfutrell@2359
    38
static int GL_ActivateRenderer(SDL_Renderer * renderer);
hfutrell@2359
    39
static int GL_DisplayModeChanged(SDL_Renderer * renderer);
hfutrell@2359
    40
static int GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
hfutrell@2359
    41
static int GL_QueryTexturePixels(SDL_Renderer * renderer,
hfutrell@2359
    42
                                 SDL_Texture * texture, void **pixels,
hfutrell@2359
    43
                                 int *pitch);
hfutrell@2359
    44
static int GL_SetTexturePalette(SDL_Renderer * renderer,
hfutrell@2359
    45
                                SDL_Texture * texture,
hfutrell@2359
    46
                                const SDL_Color * colors, int firstcolor,
hfutrell@2359
    47
                                int ncolors);
hfutrell@2359
    48
static int GL_GetTexturePalette(SDL_Renderer * renderer,
hfutrell@2359
    49
                                SDL_Texture * texture, SDL_Color * colors,
hfutrell@2359
    50
                                int firstcolor, int ncolors);
hfutrell@2359
    51
static int GL_SetTextureColorMod(SDL_Renderer * renderer,
hfutrell@2359
    52
                                 SDL_Texture * texture);
hfutrell@2359
    53
static int GL_SetTextureAlphaMod(SDL_Renderer * renderer,
hfutrell@2359
    54
                                 SDL_Texture * texture);
hfutrell@2359
    55
static int GL_SetTextureBlendMode(SDL_Renderer * renderer,
hfutrell@2359
    56
                                  SDL_Texture * texture);
hfutrell@2359
    57
static int GL_SetTextureScaleMode(SDL_Renderer * renderer,
hfutrell@2359
    58
                                  SDL_Texture * texture);
hfutrell@2359
    59
static int GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
hfutrell@2359
    60
                            const SDL_Rect * rect, const void *pixels,
hfutrell@2359
    61
                            int pitch);
hfutrell@2359
    62
static int GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
hfutrell@2359
    63
                          const SDL_Rect * rect, int markDirty, void **pixels,
hfutrell@2359
    64
                          int *pitch);
hfutrell@2359
    65
static void GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
hfutrell@2359
    66
static void GL_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
hfutrell@2359
    67
                            int numrects, const SDL_Rect * rects);
hfutrell@2359
    68
static int GL_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b,
hfutrell@2359
    69
                         Uint8 a, const SDL_Rect * rect);
hfutrell@2359
    70
static int GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
hfutrell@2359
    71
                         const SDL_Rect * srcrect, const SDL_Rect * dstrect);
hfutrell@2359
    72
static void GL_RenderPresent(SDL_Renderer * renderer);
hfutrell@2359
    73
static void GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
hfutrell@2359
    74
static void GL_DestroyRenderer(SDL_Renderer * renderer);
hfutrell@2359
    75
hfutrell@2359
    76
hfutrell@2359
    77
SDL_RenderDriver GL_ES_RenderDriver = {
hfutrell@2359
    78
    GL_CreateRenderer,
hfutrell@2359
    79
    {
hfutrell@2359
    80
     "opengl_es",
hfutrell@2359
    81
     (SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTDISCARD |
hfutrell@2359
    82
      SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED),
hfutrell@2359
    83
     (SDL_TEXTUREMODULATE_NONE | SDL_TEXTUREMODULATE_COLOR |
hfutrell@2359
    84
      SDL_TEXTUREMODULATE_ALPHA),
hfutrell@2359
    85
     (SDL_TEXTUREBLENDMODE_NONE | SDL_TEXTUREBLENDMODE_MASK |
hfutrell@2359
    86
      SDL_TEXTUREBLENDMODE_BLEND | SDL_TEXTUREBLENDMODE_ADD |
hfutrell@2359
    87
      SDL_TEXTUREBLENDMODE_MOD),
hfutrell@2359
    88
     (SDL_TEXTURESCALEMODE_NONE | SDL_TEXTURESCALEMODE_FAST |
hfutrell@2359
    89
      SDL_TEXTURESCALEMODE_SLOW),
hfutrell@2359
    90
#ifdef GL_IMG_texture_format_BGRA8888     
hfutrell@2359
    91
		3,
hfutrell@2359
    92
#else
hfutrell@2359
    93
		2,
hfutrell@2359
    94
#endif
hfutrell@2359
    95
     {
hfutrell@2359
    96
#ifdef GL_IMG_texture_format_BGRA8888
hfutrell@2359
    97
		SDL_PIXELFORMAT_ARGB8888,
hfutrell@2359
    98
#endif
hfutrell@2359
    99
		SDL_PIXELFORMAT_RGB24,
hfutrell@2359
   100
		SDL_PIXELFORMAT_ABGR8888,
hfutrell@2359
   101
		},
hfutrell@2359
   102
	 0,
hfutrell@2359
   103
     0}
hfutrell@2359
   104
};
hfutrell@2359
   105
hfutrell@2359
   106
typedef struct
hfutrell@2359
   107
{
hfutrell@2359
   108
    SDL_GLContext context;
hfutrell@2359
   109
    SDL_bool updateSize;
hfutrell@2359
   110
    int blendMode;
hfutrell@2359
   111
    int scaleMode;
hfutrell@2359
   112
	
hfutrell@2359
   113
#ifndef APIENTRY
hfutrell@2359
   114
#define APIENTRY
hfutrell@2359
   115
#endif
hfutrell@2359
   116
	
hfutrell@2359
   117
	SDL_bool useDrawTexture;
hfutrell@2359
   118
	SDL_bool GL_OES_draw_texture_supported;
hfutrell@2359
   119
	
hfutrell@2359
   120
	/* OpenGL ES functions */
hfutrell@2359
   121
	#define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
hfutrell@2359
   122
	#include "SDL_glesfuncs.h"
hfutrell@2359
   123
	#undef SDL_PROC
hfutrell@2359
   124
hfutrell@2359
   125
} GL_RenderData;
hfutrell@2359
   126
hfutrell@2359
   127
typedef struct
hfutrell@2359
   128
{
hfutrell@2359
   129
    GLuint texture;
hfutrell@2359
   130
    GLenum type;
hfutrell@2359
   131
    GLfloat texw;
hfutrell@2359
   132
    GLfloat texh;
hfutrell@2359
   133
    GLenum format;
hfutrell@2359
   134
    GLenum formattype;
hfutrell@2359
   135
    void *pixels;
hfutrell@2359
   136
    int pitch;
hfutrell@2359
   137
    SDL_DirtyRectList dirty;
hfutrell@2359
   138
} GL_TextureData;
hfutrell@2359
   139
hfutrell@2359
   140
static void
hfutrell@2359
   141
GL_SetError(const char *prefix, GLenum result)
hfutrell@2359
   142
{
hfutrell@2359
   143
    const char *error;
hfutrell@2359
   144
hfutrell@2359
   145
    switch (result) {
hfutrell@2359
   146
    case GL_NO_ERROR:
hfutrell@2359
   147
        error = "GL_NO_ERROR";
hfutrell@2359
   148
        break;
hfutrell@2359
   149
    case GL_INVALID_ENUM:
hfutrell@2359
   150
        error = "GL_INVALID_ENUM";
hfutrell@2359
   151
        break;
hfutrell@2359
   152
    case GL_INVALID_VALUE:
hfutrell@2359
   153
        error = "GL_INVALID_VALUE";
hfutrell@2359
   154
        break;
hfutrell@2359
   155
    case GL_INVALID_OPERATION:
hfutrell@2359
   156
        error = "GL_INVALID_OPERATION";
hfutrell@2359
   157
        break;
hfutrell@2359
   158
    case GL_STACK_OVERFLOW:
hfutrell@2359
   159
        error = "GL_STACK_OVERFLOW";
hfutrell@2359
   160
        break;
hfutrell@2359
   161
    case GL_STACK_UNDERFLOW:
hfutrell@2359
   162
        error = "GL_STACK_UNDERFLOW";
hfutrell@2359
   163
        break;
hfutrell@2359
   164
    case GL_OUT_OF_MEMORY:
hfutrell@2359
   165
        error = "GL_OUT_OF_MEMORY";
hfutrell@2359
   166
        break;
hfutrell@2359
   167
    default:
hfutrell@2359
   168
        error = "UNKNOWN";
hfutrell@2359
   169
        break;
hfutrell@2359
   170
    }
hfutrell@2359
   171
    SDL_SetError("%s: %s", prefix, error);
hfutrell@2359
   172
}
hfutrell@2359
   173
hfutrell@2359
   174
static int
hfutrell@2359
   175
GL_LoadFunctions(GL_RenderData * data)
hfutrell@2359
   176
{
hfutrell@2359
   177
	
hfutrell@2359
   178
	#define SDL_PROC(ret,func,params) \
hfutrell@2359
   179
	data->func = func;
hfutrell@2359
   180
	#include "SDL_glesfuncs.h"
hfutrell@2359
   181
	#undef SDL_PROC
hfutrell@2359
   182
	
hfutrell@2359
   183
    return 0;
hfutrell@2359
   184
}
hfutrell@2359
   185
hfutrell@2359
   186
void
hfutrell@2359
   187
GL_AddRenderDriver(_THIS)
hfutrell@2359
   188
{
hfutrell@2359
   189
    if (_this->GL_CreateContext) {
hfutrell@2359
   190
        SDL_AddRenderDriver(0, &GL_ES_RenderDriver);
hfutrell@2359
   191
    }
hfutrell@2359
   192
}
hfutrell@2359
   193
hfutrell@2359
   194
SDL_Renderer *
hfutrell@2359
   195
GL_CreateRenderer(SDL_Window * window, Uint32 flags)
hfutrell@2359
   196
{
hfutrell@2359
   197
	
hfutrell@2359
   198
	printf("gl(es) create renderer ...\n");
hfutrell@2359
   199
	
hfutrell@2359
   200
    SDL_Renderer *renderer;
hfutrell@2359
   201
    GL_RenderData *data;
hfutrell@2359
   202
    GLint value;
hfutrell@2359
   203
    int doublebuffer;
hfutrell@2359
   204
hfutrell@2359
   205
    if (!(window->flags & SDL_WINDOW_OPENGL)) {
hfutrell@2359
   206
        if (SDL_RecreateWindow(window, window->flags | SDL_WINDOW_OPENGL) < 0) {
hfutrell@2359
   207
            return NULL;
hfutrell@2359
   208
        }
hfutrell@2359
   209
    }
hfutrell@2359
   210
hfutrell@2359
   211
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
hfutrell@2359
   212
    if (!renderer) {
hfutrell@2359
   213
        SDL_OutOfMemory();
hfutrell@2359
   214
        return NULL;
hfutrell@2359
   215
    }
hfutrell@2359
   216
hfutrell@2359
   217
    data = (GL_RenderData *) SDL_calloc(1, sizeof(*data));
hfutrell@2359
   218
    if (!data) {
hfutrell@2359
   219
        GL_DestroyRenderer(renderer);
hfutrell@2359
   220
        SDL_OutOfMemory();
hfutrell@2359
   221
        return NULL;
hfutrell@2359
   222
    }
hfutrell@2359
   223
hfutrell@2359
   224
    renderer->ActivateRenderer = GL_ActivateRenderer;
hfutrell@2359
   225
    renderer->DisplayModeChanged = GL_DisplayModeChanged;
hfutrell@2359
   226
    renderer->CreateTexture = GL_CreateTexture;
hfutrell@2359
   227
    renderer->QueryTexturePixels = GL_QueryTexturePixels;
hfutrell@2359
   228
    renderer->SetTexturePalette = GL_SetTexturePalette;
hfutrell@2359
   229
    renderer->GetTexturePalette = GL_GetTexturePalette;
hfutrell@2359
   230
    renderer->SetTextureColorMod = GL_SetTextureColorMod;
hfutrell@2359
   231
    renderer->SetTextureAlphaMod = GL_SetTextureAlphaMod;
hfutrell@2359
   232
    renderer->SetTextureBlendMode = GL_SetTextureBlendMode;
hfutrell@2359
   233
    renderer->SetTextureScaleMode = GL_SetTextureScaleMode;
hfutrell@2359
   234
    renderer->UpdateTexture = GL_UpdateTexture;
hfutrell@2359
   235
    renderer->LockTexture = GL_LockTexture;
hfutrell@2359
   236
    renderer->UnlockTexture = GL_UnlockTexture;
hfutrell@2359
   237
    renderer->DirtyTexture = GL_DirtyTexture;
hfutrell@2359
   238
    renderer->RenderFill = GL_RenderFill;
hfutrell@2359
   239
    renderer->RenderCopy = GL_RenderCopy;
hfutrell@2359
   240
    renderer->RenderPresent = GL_RenderPresent;
hfutrell@2359
   241
    renderer->DestroyTexture = GL_DestroyTexture;
hfutrell@2359
   242
    renderer->DestroyRenderer = GL_DestroyRenderer;
hfutrell@2359
   243
    renderer->info = GL_ES_RenderDriver.info;
hfutrell@2359
   244
    renderer->window = window->id;
hfutrell@2359
   245
    renderer->driverdata = data;
hfutrell@2359
   246
	
hfutrell@2359
   247
	
hfutrell@2359
   248
    renderer->info.flags = (SDL_RENDERER_PRESENTDISCARD | SDL_RENDERER_ACCELERATED);
hfutrell@2359
   249
hfutrell@2359
   250
    if (GL_LoadFunctions(data) < 0) {
hfutrell@2359
   251
        GL_DestroyRenderer(renderer);
hfutrell@2359
   252
        return NULL;
hfutrell@2359
   253
    }
hfutrell@2359
   254
hfutrell@2359
   255
    data->context = SDL_GL_CreateContext(window->id);
hfutrell@2359
   256
    if (!data->context) {
hfutrell@2359
   257
        GL_DestroyRenderer(renderer);
hfutrell@2359
   258
        return NULL;
hfutrell@2359
   259
    }
hfutrell@2359
   260
    if (SDL_GL_MakeCurrent(window->id, data->context) < 0) {
hfutrell@2359
   261
        GL_DestroyRenderer(renderer);
hfutrell@2359
   262
        return NULL;
hfutrell@2359
   263
    }
hfutrell@2359
   264
hfutrell@2359
   265
    if (flags & SDL_RENDERER_PRESENTVSYNC) {
hfutrell@2359
   266
        SDL_GL_SetSwapInterval(1);
hfutrell@2359
   267
    } else {
hfutrell@2359
   268
        SDL_GL_SetSwapInterval(0);
hfutrell@2359
   269
    }
hfutrell@2359
   270
    if (SDL_GL_GetSwapInterval() > 0) {
hfutrell@2359
   271
        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
hfutrell@2359
   272
    }
hfutrell@2359
   273
hfutrell@2359
   274
    if (SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &doublebuffer) == 0) {
hfutrell@2359
   275
        if (!doublebuffer) {
hfutrell@2359
   276
            renderer->info.flags |= SDL_RENDERER_SINGLEBUFFER;
hfutrell@2359
   277
        }
hfutrell@2359
   278
    }
hfutrell@2359
   279
	
hfutrell@2359
   280
	if (SDL_GL_ExtensionSupported("GL_OES_draw_texture")) {
hfutrell@2359
   281
		data->GL_OES_draw_texture_supported = SDL_TRUE;
hfutrell@2359
   282
		data->useDrawTexture = SDL_TRUE;
hfutrell@2359
   283
	}
hfutrell@2359
   284
	else {
hfutrell@2359
   285
		data->GL_OES_draw_texture_supported = SDL_FALSE;
hfutrell@2359
   286
		data->useDrawTexture = SDL_FALSE;
hfutrell@2359
   287
	}	
hfutrell@2359
   288
hfutrell@2359
   289
    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
hfutrell@2359
   290
    renderer->info.max_texture_width = value;
hfutrell@2359
   291
    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
hfutrell@2359
   292
    renderer->info.max_texture_height = value;
hfutrell@2359
   293
hfutrell@2359
   294
    /* Set up parameters for rendering */
hfutrell@2359
   295
    data->blendMode = -1;
hfutrell@2359
   296
    data->scaleMode = -1;
hfutrell@2359
   297
    data->glDisable(GL_DEPTH_TEST);
hfutrell@2359
   298
    data->glDisable(GL_CULL_FACE);
hfutrell@2359
   299
	data->glEnable(GL_TEXTURE_2D);
hfutrell@2359
   300
    data->updateSize = SDL_TRUE;
hfutrell@2359
   301
hfutrell@2359
   302
    return renderer;
hfutrell@2359
   303
}
hfutrell@2359
   304
hfutrell@2359
   305
static int GL_ActivateRenderer(SDL_Renderer * renderer)
hfutrell@2359
   306
{
hfutrell@2359
   307
		
hfutrell@2359
   308
    GL_RenderData *data = (GL_RenderData *)renderer->driverdata;
hfutrell@2359
   309
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
hfutrell@2426
   310
				
hfutrell@2359
   311
    if (SDL_GL_MakeCurrent(window->id, data->context) < 0) {
hfutrell@2359
   312
        return -1;
hfutrell@2359
   313
    }
hfutrell@2359
   314
    if (data->updateSize) {
hfutrell@2359
   315
        data->glMatrixMode(GL_PROJECTION);
hfutrell@2359
   316
        data->glLoadIdentity();
hfutrell@2359
   317
        data->glMatrixMode(GL_MODELVIEW);
hfutrell@2359
   318
        data->glLoadIdentity();
hfutrell@2359
   319
        data->glViewport(0, 0, window->w, window->h);
hfutrell@2359
   320
        data->glOrthof(0.0, (GLfloat)window->w, (GLfloat)window->h, 0.0,
hfutrell@2359
   321
                      0.0, 1.0);
hfutrell@2359
   322
        data->updateSize = SDL_FALSE;
hfutrell@2359
   323
    }
hfutrell@2359
   324
    return 0;
hfutrell@2359
   325
}
hfutrell@2359
   326
hfutrell@2359
   327
static int
hfutrell@2359
   328
GL_DisplayModeChanged(SDL_Renderer * renderer)
hfutrell@2359
   329
{
hfutrell@2359
   330
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
hfutrell@2359
   331
hfutrell@2359
   332
    data->updateSize = SDL_TRUE;
hfutrell@2359
   333
    return 0;
hfutrell@2359
   334
}
hfutrell@2359
   335
hfutrell@2359
   336
static __inline__ int
hfutrell@2359
   337
power_of_2(int input)
hfutrell@2359
   338
{
hfutrell@2359
   339
    int value = 1;
hfutrell@2359
   340
hfutrell@2359
   341
    while (value < input) {
hfutrell@2359
   342
        value <<= 1;
hfutrell@2359
   343
    }
hfutrell@2359
   344
    return value;
hfutrell@2359
   345
}
hfutrell@2359
   346
hfutrell@2359
   347
static int
hfutrell@2359
   348
GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
hfutrell@2359
   349
{
hfutrell@2359
   350
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
hfutrell@2359
   351
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
hfutrell@2359
   352
    GL_TextureData *data;
hfutrell@2359
   353
    GLint internalFormat;
hfutrell@2359
   354
    GLenum format, type;
hfutrell@2359
   355
    int texture_w, texture_h;
hfutrell@2359
   356
    GLenum result;
hfutrell@2359
   357
	switch (texture->format) {
hfutrell@2359
   358
		case SDL_PIXELFORMAT_INDEX1LSB:
hfutrell@2359
   359
		case SDL_PIXELFORMAT_INDEX1MSB:
hfutrell@2359
   360
		case SDL_PIXELFORMAT_INDEX8:
hfutrell@2359
   361
		case SDL_PIXELFORMAT_RGB332:
hfutrell@2359
   362
		case SDL_PIXELFORMAT_RGB444:
hfutrell@2359
   363
		case SDL_PIXELFORMAT_RGB555:
hfutrell@2359
   364
		case SDL_PIXELFORMAT_ARGB4444:
hfutrell@2359
   365
		case SDL_PIXELFORMAT_ARGB1555:
hfutrell@2359
   366
		case SDL_PIXELFORMAT_BGR24:
hfutrell@2359
   367
		case SDL_PIXELFORMAT_BGR888: /* can't convert 32 bit format to 24 */
hfutrell@2359
   368
		case SDL_PIXELFORMAT_RGB888:
hfutrell@2359
   369
		case SDL_PIXELFORMAT_RGBA8888:
hfutrell@2359
   370
		case SDL_PIXELFORMAT_ARGB2101010:
hfutrell@2359
   371
			SDL_SetError("Unsupported format");
hfutrell@2359
   372
			return -1;
hfutrell@2359
   373
		case SDL_PIXELFORMAT_ARGB8888:
hfutrell@2359
   374
		#ifdef GL_IMG_texture_format_BGRA8888
hfutrell@2359
   375
			internalFormat = GL_RGBA;
hfutrell@2359
   376
			format = GL_RGBA;
hfutrell@2359
   377
			type = GL_UNSIGNED_BYTE;
hfutrell@2359
   378
			break;
hfutrell@2359
   379
		#else
hfutrell@2359
   380
			SDL_SetError("Unsupported format");
hfutrell@2359
   381
			return -1;
hfutrell@2359
   382
		#endif
hfutrell@2359
   383
		case SDL_PIXELFORMAT_RGB24:
hfutrell@2359
   384
			internalFormat = GL_RGB;
hfutrell@2359
   385
			format = GL_RGB;
hfutrell@2359
   386
			type = GL_UNSIGNED_BYTE;
hfutrell@2359
   387
			break;
hfutrell@2359
   388
		case SDL_PIXELFORMAT_ABGR8888:
hfutrell@2359
   389
			internalFormat = GL_RGBA;
hfutrell@2359
   390
			format = GL_RGBA;
hfutrell@2359
   391
			type = GL_UNSIGNED_BYTE;			
hfutrell@2359
   392
			break;			
hfutrell@2359
   393
		/*
hfutrell@2359
   394
			These formats would be supported if SDL had the necessary pixel formats
hfutrell@2359
   395
		case SDL_PIXELFORMAT_BGR565:
hfutrell@2359
   396
			internalFormat = GL_RGB;
hfutrell@2359
   397
			format = GL_RGB;
hfutrell@2359
   398
			type = GL_UNSIGNED_SHORT_5_6_5;
hfutrell@2359
   399
			break;			
hfutrell@2359
   400
		case SDL_PIXELFORMAT_ABGR5551:
hfutrell@2359
   401
			internalFormat = GL_RGBA;
hfutrell@2359
   402
			format = GL_RGBA;
hfutrell@2359
   403
			type = GL_UNSIGNED_SHORT_5_5_5_1;
hfutrell@2359
   404
			break;
hfutrell@2359
   405
		case SDL_PIXELFORMAT_ABGR4444:
hfutrell@2359
   406
			internalFormat = GL_RGBA;
hfutrell@2359
   407
			format = GL_RGBA;
hfutrell@2359
   408
			type = GL_UNSIGNED_SHORT_4_4_4_4;
hfutrell@2359
   409
			break;
hfutrell@2359
   410
		*/
hfutrell@2359
   411
		default:
hfutrell@2359
   412
			SDL_SetError("Unsupported texture format");
hfutrell@2359
   413
			return -1;
hfutrell@2359
   414
    }
hfutrell@2359
   415
	data = (GL_TextureData *) SDL_calloc(1, sizeof(*data));
hfutrell@2359
   416
    if (!data) {
hfutrell@2359
   417
        SDL_OutOfMemory();
hfutrell@2359
   418
        return -1;
hfutrell@2359
   419
    }
hfutrell@2359
   420
hfutrell@2359
   421
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
hfutrell@2359
   422
        data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
hfutrell@2359
   423
        data->pixels = SDL_malloc(texture->h * data->pitch);
hfutrell@2359
   424
        if (!data->pixels) {
hfutrell@2359
   425
            SDL_OutOfMemory();
hfutrell@2359
   426
            SDL_free(data);
hfutrell@2359
   427
            return -1;
hfutrell@2359
   428
        }
hfutrell@2359
   429
    }
hfutrell@2359
   430
hfutrell@2359
   431
    texture->driverdata = data;
hfutrell@2359
   432
hfutrell@2359
   433
    renderdata->glGetError();
hfutrell@2359
   434
    renderdata->glGenTextures(1, &data->texture);
hfutrell@2359
   435
    
hfutrell@2359
   436
	data->type = GL_TEXTURE_2D;
hfutrell@2359
   437
	texture_w = power_of_2(texture->w);
hfutrell@2359
   438
	texture_h = power_of_2(texture->h);
hfutrell@2359
   439
	data->texw = (GLfloat) texture->w / texture_w;
hfutrell@2359
   440
	data->texh = (GLfloat) texture->h / texture_h;
hfutrell@2359
   441
	
hfutrell@2359
   442
    data->format = format;
hfutrell@2359
   443
    data->formattype = type;
hfutrell@2359
   444
    renderdata->glBindTexture(data->type, data->texture);
hfutrell@2359
   445
    renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
hfutrell@2359
   446
                                GL_NEAREST);
hfutrell@2359
   447
    renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
hfutrell@2359
   448
                                GL_NEAREST);
hfutrell@2359
   449
    renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
hfutrell@2359
   450
                                GL_CLAMP_TO_EDGE);
hfutrell@2359
   451
    renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
hfutrell@2359
   452
                                GL_CLAMP_TO_EDGE);
hfutrell@2359
   453
    
hfutrell@2359
   454
	renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
hfutrell@2359
   455
							 texture_h, 0, format, type, NULL);
hfutrell@2359
   456
hfutrell@2359
   457
    result = renderdata->glGetError();
hfutrell@2359
   458
    if (result != GL_NO_ERROR) {
hfutrell@2359
   459
        GL_SetError("glTexImage2D()", result);
hfutrell@2359
   460
        return -1;
hfutrell@2359
   461
    }
hfutrell@2359
   462
    return 0;
hfutrell@2359
   463
}
hfutrell@2359
   464
hfutrell@2359
   465
static int
hfutrell@2359
   466
GL_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
hfutrell@2359
   467
                      void **pixels, int *pitch)
hfutrell@2359
   468
{
hfutrell@2359
   469
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
hfutrell@2359
   470
hfutrell@2359
   471
    *pixels = data->pixels;
hfutrell@2359
   472
    *pitch = data->pitch;
hfutrell@2359
   473
    return 0;
hfutrell@2359
   474
}
hfutrell@2359
   475
hfutrell@2359
   476
static int
hfutrell@2359
   477
GL_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
hfutrell@2359
   478
                     const SDL_Color * colors, int firstcolor, int ncolors)
hfutrell@2359
   479
{
hfutrell@2359
   480
	SDL_SetError("OpenGL ES does not support paletted textures");
hfutrell@2359
   481
    return -1;
hfutrell@2359
   482
}
hfutrell@2359
   483
hfutrell@2359
   484
static int
hfutrell@2359
   485
GL_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
hfutrell@2359
   486
                     SDL_Color * colors, int firstcolor, int ncolors)
hfutrell@2359
   487
{
hfutrell@2359
   488
	SDL_SetError("OpenGL ES does not support paletted textures");
hfutrell@2359
   489
    return -1;
hfutrell@2359
   490
}
hfutrell@2359
   491
hfutrell@2359
   492
static void
hfutrell@2359
   493
SetupTextureUpdate(GL_RenderData * renderdata, SDL_Texture * texture,
hfutrell@2359
   494
                   int pitch)
hfutrell@2359
   495
{
hfutrell@2359
   496
	
hfutrell@2359
   497
	
hfutrell@2359
   498
	GL_TextureData *data = (GL_TextureData *) texture->driverdata;
hfutrell@2359
   499
	renderdata->glBindTexture(data->type, data->texture);
hfutrell@2359
   500
    renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
hfutrell@2359
   501
}
hfutrell@2359
   502
hfutrell@2359
   503
static int
hfutrell@2359
   504
GL_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
hfutrell@2359
   505
{
hfutrell@2359
   506
    return 0;
hfutrell@2359
   507
}
hfutrell@2359
   508
hfutrell@2359
   509
static int
hfutrell@2359
   510
GL_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
hfutrell@2359
   511
{
hfutrell@2359
   512
    return 0;
hfutrell@2359
   513
}
hfutrell@2359
   514
hfutrell@2359
   515
static int
hfutrell@2359
   516
GL_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
hfutrell@2359
   517
{
hfutrell@2359
   518
    switch (texture->blendMode) {
hfutrell@2359
   519
    case SDL_TEXTUREBLENDMODE_NONE:
hfutrell@2359
   520
    case SDL_TEXTUREBLENDMODE_MASK:
hfutrell@2359
   521
    case SDL_TEXTUREBLENDMODE_BLEND:
hfutrell@2359
   522
    case SDL_TEXTUREBLENDMODE_ADD:
hfutrell@2359
   523
    case SDL_TEXTUREBLENDMODE_MOD:
hfutrell@2359
   524
        return 0;
hfutrell@2359
   525
    default:
hfutrell@2359
   526
        SDL_Unsupported();
hfutrell@2359
   527
        texture->blendMode = SDL_TEXTUREBLENDMODE_NONE;
hfutrell@2359
   528
        return -1;
hfutrell@2359
   529
    }
hfutrell@2359
   530
}
hfutrell@2359
   531
hfutrell@2359
   532
static int
hfutrell@2359
   533
GL_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture)
hfutrell@2359
   534
{
hfutrell@2359
   535
    switch (texture->scaleMode) {
hfutrell@2359
   536
    case SDL_TEXTURESCALEMODE_NONE:
hfutrell@2359
   537
    case SDL_TEXTURESCALEMODE_FAST:
hfutrell@2359
   538
    case SDL_TEXTURESCALEMODE_SLOW:
hfutrell@2359
   539
        return 0;
hfutrell@2359
   540
    case SDL_TEXTURESCALEMODE_BEST:
hfutrell@2359
   541
        SDL_Unsupported();
hfutrell@2359
   542
		texture->scaleMode = SDL_TEXTURESCALEMODE_SLOW;
hfutrell@2359
   543
        return -1;
hfutrell@2359
   544
    default:
hfutrell@2359
   545
        SDL_Unsupported();
hfutrell@2359
   546
        texture->scaleMode = SDL_TEXTURESCALEMODE_NONE;
hfutrell@2359
   547
        return -1;
hfutrell@2359
   548
    }
hfutrell@2359
   549
}
hfutrell@2359
   550
hfutrell@2359
   551
static int
hfutrell@2359
   552
GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
hfutrell@2359
   553
                 const SDL_Rect * rect, const void *pixels, int pitch)
hfutrell@2359
   554
{
hfutrell@2359
   555
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
hfutrell@2359
   556
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
hfutrell@2359
   557
    GLenum result;
hfutrell@2359
   558
hfutrell@2359
   559
    SetupTextureUpdate(renderdata, texture, pitch);
hfutrell@2359
   560
	renderdata->glGetError();
hfutrell@2359
   561
	renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
hfutrell@2359
   562
                                rect->h, data->format, data->formattype,
hfutrell@2359
   563
                                pixels);
hfutrell@2359
   564
    result = renderdata->glGetError();
hfutrell@2359
   565
    if (result != GL_NO_ERROR) {
hfutrell@2359
   566
        GL_SetError("glTexSubImage2D()", result);
hfutrell@2359
   567
        return -1;
hfutrell@2359
   568
    }
hfutrell@2359
   569
    return 0;
hfutrell@2359
   570
}
hfutrell@2359
   571
hfutrell@2359
   572
static int
hfutrell@2359
   573
GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
hfutrell@2359
   574
               const SDL_Rect * rect, int markDirty, void **pixels,
hfutrell@2359
   575
               int *pitch)
hfutrell@2359
   576
{
hfutrell@2359
   577
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
hfutrell@2359
   578
hfutrell@2359
   579
    if (markDirty) {
hfutrell@2359
   580
        SDL_AddDirtyRect(&data->dirty, rect);
hfutrell@2359
   581
    }
hfutrell@2359
   582
hfutrell@2359
   583
    *pixels =
hfutrell@2359
   584
        (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
hfutrell@2359
   585
                  rect->x * SDL_BYTESPERPIXEL(texture->format));
hfutrell@2359
   586
    *pitch = data->pitch;
hfutrell@2359
   587
    return 0;
hfutrell@2359
   588
}
hfutrell@2359
   589
hfutrell@2359
   590
static void
hfutrell@2359
   591
GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
hfutrell@2359
   592
{
hfutrell@2359
   593
}
hfutrell@2359
   594
hfutrell@2359
   595
static void
hfutrell@2359
   596
GL_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture, int numrects,
hfutrell@2359
   597
                const SDL_Rect * rects)
hfutrell@2359
   598
{
hfutrell@2359
   599
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
hfutrell@2359
   600
    int i;
hfutrell@2359
   601
hfutrell@2359
   602
    for (i = 0; i < numrects; ++i) {
hfutrell@2359
   603
        SDL_AddDirtyRect(&data->dirty, &rects[i]);
hfutrell@2359
   604
    }
hfutrell@2359
   605
}
hfutrell@2359
   606
hfutrell@2359
   607
static int
hfutrell@2359
   608
GL_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b, Uint8 a,
hfutrell@2359
   609
              const SDL_Rect * rect)
hfutrell@2359
   610
{
hfutrell@2359
   611
 	
hfutrell@2359
   612
	GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
hfutrell@2359
   613
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
hfutrell@2359
   614
	
hfutrell@2359
   615
	/* set proper drawing color */
hfutrell@2359
   616
	GLfloat oldClearColor[4];
hfutrell@2359
   617
	
hfutrell@2359
   618
	data->glGetFloatv(GL_COLOR_CLEAR_VALUE, oldClearColor);
hfutrell@2359
   619
	
hfutrell@2359
   620
	data->glClearColor((GLclampf) r * inv255f, (GLclampf) g * inv255f,
hfutrell@2359
   621
			  (GLclampf) b * inv255f, (GLclampf) a * inv255f);
hfutrell@2359
   622
	
hfutrell@2359
   623
	data->glScissor(rect->x, window->h - rect->y - rect->h, rect->w, rect->h);
hfutrell@2359
   624
	data->glEnable(GL_SCISSOR_TEST);
hfutrell@2359
   625
	data->glClear(GL_COLOR_BUFFER_BIT);
hfutrell@2359
   626
	data->glDisable(GL_SCISSOR_TEST);
hfutrell@2359
   627
	
hfutrell@2359
   628
	/* reset clear color */
hfutrell@2359
   629
	data->glClearColor(oldClearColor[0], oldClearColor[1], oldClearColor[2], oldClearColor[2]);
hfutrell@2359
   630
	
hfutrell@2359
   631
	return 0;
hfutrell@2359
   632
}
hfutrell@2359
   633
hfutrell@2359
   634
static int
hfutrell@2359
   635
GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
hfutrell@2359
   636
              const SDL_Rect * srcrect, const SDL_Rect * dstrect)
hfutrell@2359
   637
{
hfutrell@2359
   638
		
hfutrell@2359
   639
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
hfutrell@2359
   640
    GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
hfutrell@2359
   641
    int minx, miny, maxx, maxy;
hfutrell@2359
   642
    GLfloat minu, maxu, minv, maxv;
hfutrell@2359
   643
	int i;
hfutrell@2359
   644
	void *temp_buffer; /* used for reformatting dirty rect pixels */
hfutrell@2359
   645
	void *temp_ptr;
hfutrell@2359
   646
	
hfutrell@2359
   647
    if (texturedata->dirty.list) {
hfutrell@2359
   648
        SDL_DirtyRect *dirty;
hfutrell@2359
   649
        void *pixels;
hfutrell@2359
   650
        int bpp = SDL_BYTESPERPIXEL(texture->format);
hfutrell@2359
   651
        int pitch = texturedata->pitch;
hfutrell@2359
   652
hfutrell@2359
   653
        SetupTextureUpdate(data, texture, pitch);
hfutrell@2359
   654
		
hfutrell@2359
   655
        data->glBindTexture(texturedata->type, texturedata->texture);
hfutrell@2359
   656
        for (dirty = texturedata->dirty.list; dirty; dirty = dirty->next) {
hfutrell@2359
   657
            SDL_Rect *rect = &dirty->rect;
hfutrell@2359
   658
            pixels = (void *) ((Uint8 *) texturedata->pixels + rect->y * pitch + rect->x * bpp);
hfutrell@2359
   659
			/*	There is no GL_UNPACK_ROW_LENGTH in OpenGLES 
hfutrell@2359
   660
				we must do this reformatting ourselves(!)
hfutrell@2359
   661
			 
hfutrell@2359
   662
				maybe it'd be a good idea to keep a temp buffer around
hfutrell@2359
   663
				for this purpose rather than allocating it each time
hfutrell@2359
   664
			*/
hfutrell@2359
   665
			temp_buffer = SDL_malloc(rect->w * rect->h * bpp);
hfutrell@2359
   666
			temp_ptr = temp_buffer;
hfutrell@2359
   667
			for (i=0; i<rect->h; i++) {
hfutrell@2359
   668
				SDL_memcpy(temp_ptr, pixels, rect->w * bpp);
hfutrell@2359
   669
				temp_ptr += rect->w * bpp;
hfutrell@2359
   670
				pixels	 += pitch;
hfutrell@2359
   671
			}
hfutrell@2359
   672
			
hfutrell@2359
   673
            data->glTexSubImage2D(texturedata->type, 0, rect->x, rect->y,
hfutrell@2359
   674
                                  rect->w, rect->h, texturedata->format,
hfutrell@2359
   675
                                  texturedata->formattype, temp_buffer);
hfutrell@2359
   676
			
hfutrell@2359
   677
			SDL_free(temp_buffer);
hfutrell@2359
   678
			
hfutrell@2359
   679
        }
hfutrell@2359
   680
        SDL_ClearDirtyRects(&texturedata->dirty);
hfutrell@2359
   681
    }
hfutrell@2359
   682
	
hfutrell@2359
   683
    data->glBindTexture(texturedata->type, texturedata->texture);
hfutrell@2359
   684
	data->glEnable(GL_TEXTURE_2D);
hfutrell@2359
   685
	
hfutrell@2359
   686
    if (texture->modMode) {
hfutrell@2359
   687
        data->glColor4f((GLfloat) texture->r * inv255f,
hfutrell@2359
   688
                        (GLfloat) texture->g * inv255f,
hfutrell@2359
   689
                        (GLfloat) texture->b * inv255f,
hfutrell@2359
   690
                        (GLfloat) texture->a * inv255f);
hfutrell@2359
   691
    } else {
hfutrell@2359
   692
        data->glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
hfutrell@2359
   693
    }
hfutrell@2359
   694
hfutrell@2359
   695
    //if (texture->blendMode != data->blendMode) {
hfutrell@2359
   696
        switch (texture->blendMode) {
hfutrell@2359
   697
        case SDL_TEXTUREBLENDMODE_NONE:
hfutrell@2359
   698
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
hfutrell@2359
   699
            data->glDisable(GL_BLEND);
hfutrell@2359
   700
            break;
hfutrell@2359
   701
        case SDL_TEXTUREBLENDMODE_MASK:
hfutrell@2359
   702
        case SDL_TEXTUREBLENDMODE_BLEND:
hfutrell@2359
   703
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
hfutrell@2359
   704
            data->glEnable(GL_BLEND);
hfutrell@2359
   705
            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
hfutrell@2359
   706
            break;
hfutrell@2359
   707
        case SDL_TEXTUREBLENDMODE_ADD:
hfutrell@2359
   708
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
hfutrell@2359
   709
            data->glEnable(GL_BLEND);
hfutrell@2359
   710
            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE);
hfutrell@2359
   711
            break;
hfutrell@2359
   712
        case SDL_TEXTUREBLENDMODE_MOD:
hfutrell@2359
   713
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
hfutrell@2359
   714
            data->glEnable(GL_BLEND);
hfutrell@2359
   715
            data->glBlendFunc(GL_ZERO, GL_SRC_COLOR);
hfutrell@2359
   716
            break;
hfutrell@2359
   717
        }
hfutrell@2359
   718
        data->blendMode = texture->blendMode;
hfutrell@2359
   719
   // }
hfutrell@2359
   720
hfutrell@2359
   721
   // if (texture->scaleMode != data->scaleMode) {
hfutrell@2359
   722
        switch (texture->scaleMode) {
hfutrell@2359
   723
        case SDL_TEXTURESCALEMODE_NONE:
hfutrell@2359
   724
        case SDL_TEXTURESCALEMODE_FAST:
hfutrell@2359
   725
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
hfutrell@2359
   726
                                  GL_NEAREST);
hfutrell@2359
   727
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
hfutrell@2359
   728
                                  GL_NEAREST);
hfutrell@2359
   729
            break;
hfutrell@2359
   730
        case SDL_TEXTURESCALEMODE_SLOW:
hfutrell@2359
   731
        case SDL_TEXTURESCALEMODE_BEST:
hfutrell@2359
   732
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
hfutrell@2359
   733
                                  GL_LINEAR);
hfutrell@2359
   734
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
hfutrell@2359
   735
                                  GL_LINEAR);
hfutrell@2359
   736
            break;
hfutrell@2359
   737
        }
hfutrell@2359
   738
        data->scaleMode = texture->scaleMode;
hfutrell@2359
   739
    //}
hfutrell@2359
   740
hfutrell@2359
   741
	if (data->GL_OES_draw_texture_supported && data->useDrawTexture) {
hfutrell@2359
   742
		/* this code is a little funny because the viewport is upside down vs SDL's coordinate system */
hfutrell@2359
   743
		SDL_Window *window = SDL_GetWindowFromID(renderer->window);
hfutrell@2359
   744
		GLint cropRect[4];
hfutrell@2359
   745
		cropRect[0] = srcrect->x;
hfutrell@2359
   746
		cropRect[1] = srcrect->y + srcrect->h;
hfutrell@2359
   747
		cropRect[2] = srcrect->w;
hfutrell@2359
   748
		cropRect[3] = -srcrect->h;
hfutrell@2359
   749
		data->glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect);
hfutrell@2359
   750
		data->glDrawTexiOES(dstrect->x, window->h - dstrect->y - dstrect->h, 0, dstrect->w, dstrect->h);
hfutrell@2359
   751
	}
hfutrell@2359
   752
	else {
hfutrell@2359
   753
		
hfutrell@2359
   754
		minx = dstrect->x;
hfutrell@2359
   755
		miny = dstrect->y;
hfutrell@2359
   756
		maxx = dstrect->x + dstrect->w;
hfutrell@2359
   757
		maxy = dstrect->y + dstrect->h;
hfutrell@2359
   758
		
hfutrell@2359
   759
		minu = (GLfloat) srcrect->x / texture->w;
hfutrell@2359
   760
		minu *= texturedata->texw;
hfutrell@2359
   761
		maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
hfutrell@2359
   762
		maxu *= texturedata->texw;
hfutrell@2359
   763
		minv = (GLfloat) srcrect->y / texture->h;
hfutrell@2359
   764
		minv *= texturedata->texh;
hfutrell@2359
   765
		maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
hfutrell@2359
   766
		maxv *= texturedata->texh;			
hfutrell@2359
   767
		
hfutrell@2359
   768
		GLshort vertices[8];
hfutrell@2359
   769
		GLfloat texCoords[8];
hfutrell@2359
   770
		
hfutrell@2359
   771
		vertices[0] = minx; vertices[1] = miny;
hfutrell@2359
   772
		vertices[2] = maxx; vertices[3] = miny;
hfutrell@2359
   773
		vertices[4]	= minx; vertices[5] = maxy;
hfutrell@2359
   774
		vertices[6] = maxx; vertices[7] = maxy;	
hfutrell@2359
   775
		
hfutrell@2359
   776
		texCoords[0] = minu; texCoords[1] = minv;
hfutrell@2359
   777
		texCoords[2] = maxu; texCoords[3] = minv;
hfutrell@2359
   778
		texCoords[4] = minu; texCoords[5] = maxv;
hfutrell@2359
   779
		texCoords[6] = maxu; texCoords[7] = maxv;	
hfutrell@2359
   780
		
hfutrell@2359
   781
		data->glVertexPointer(2, GL_SHORT, 0, vertices);
hfutrell@2359
   782
		data->glEnableClientState(GL_VERTEX_ARRAY);
hfutrell@2359
   783
		data->glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
hfutrell@2359
   784
		data->glEnableClientState(GL_TEXTURE_COORD_ARRAY);
hfutrell@2359
   785
		data->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
hfutrell@2359
   786
		
hfutrell@2359
   787
	}
hfutrell@2359
   788
		
hfutrell@2359
   789
    return 0;
hfutrell@2359
   790
}
hfutrell@2359
   791
hfutrell@2359
   792
static void
hfutrell@2359
   793
GL_RenderPresent(SDL_Renderer * renderer)
hfutrell@2359
   794
{
hfutrell@2359
   795
    SDL_GL_SwapWindow(renderer->window);
hfutrell@2359
   796
}
hfutrell@2359
   797
hfutrell@2359
   798
static void
hfutrell@2359
   799
GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
hfutrell@2359
   800
{
hfutrell@2359
   801
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
hfutrell@2359
   802
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
hfutrell@2359
   803
hfutrell@2359
   804
    if (!data) {
hfutrell@2359
   805
        return;
hfutrell@2359
   806
    }
hfutrell@2359
   807
    if (data->texture) {
hfutrell@2359
   808
		glDeleteTextures(1, &data->texture);
hfutrell@2359
   809
    }
hfutrell@2359
   810
    if (data->pixels) {
hfutrell@2359
   811
        SDL_free(data->pixels);
hfutrell@2359
   812
    }
hfutrell@2359
   813
    SDL_FreeDirtyRects(&data->dirty);
hfutrell@2359
   814
    SDL_free(data);
hfutrell@2359
   815
    texture->driverdata = NULL;
hfutrell@2359
   816
}
hfutrell@2359
   817
hfutrell@2359
   818
static void
hfutrell@2359
   819
GL_DestroyRenderer(SDL_Renderer * renderer)
hfutrell@2359
   820
{
hfutrell@2359
   821
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
hfutrell@2359
   822
hfutrell@2359
   823
    if (data) {
hfutrell@2359
   824
        if (data->context) {
hfutrell@2359
   825
            /* SDL_GL_MakeCurrent(0, NULL); *//* doesn't do anything */
hfutrell@2359
   826
            SDL_GL_DeleteContext(data->context);
hfutrell@2359
   827
        }
hfutrell@2359
   828
        SDL_free(data);
hfutrell@2359
   829
    }
hfutrell@2359
   830
    SDL_free(renderer);
hfutrell@2359
   831
}
hfutrell@2359
   832
hfutrell@2359
   833
#endif /* SDL_VIDEO_RENDER_OGL */
hfutrell@2359
   834
hfutrell@2359
   835
/* vi: set ts=4 sw=4 expandtab: */