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