These files are similar in purpose and structure as SDL_renderer_gl.c and SDL_renderer_gl.h, except they use OpenGL ES 1.1 for rendering.
authorHolmes Futrell <hfutrell@umail.ucsb.edu>
Tue, 02 Sep 2008 00:37:04 +0000
changeset 273968862734a5fd
parent 2738 79c1bd651f04
child 2740 22c96d28449f
These files are similar in purpose and structure as SDL_renderer_gl.c and SDL_renderer_gl.h, except they use OpenGL ES 1.1 for rendering.
src/video/SDL_renderer_gles.c
src/video/SDL_renderer_gles.h
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/video/SDL_renderer_gles.c	Tue Sep 02 00:37:04 2008 +0000
     1.3 @@ -0,0 +1,811 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997-2006 Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Lesser General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2.1 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Lesser General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Lesser General Public
    1.19 +    License along with this library; if not, write to the Free Software
    1.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@libsdl.org
    1.24 +*/
    1.25 +#include "SDL_config.h"
    1.26 +
    1.27 +#if SDL_VIDEO_RENDER_OGL_ES
    1.28 +
    1.29 +#include "SDL_video.h"
    1.30 +#include "SDL_opengles.h"
    1.31 +#include "SDL_sysvideo.h"
    1.32 +#include "SDL_pixels_c.h"
    1.33 +#include "SDL_rect_c.h"
    1.34 +#include "SDL_yuv_sw_c.h"
    1.35 +
    1.36 +/* OpenGL ES 1.1 renderer implementation, based on the OpenGL renderer */
    1.37 +
    1.38 +static const float inv255f = 1.0f / 255.0f;
    1.39 +
    1.40 +static SDL_Renderer *GLES_CreateRenderer(SDL_Window * window, Uint32 flags);
    1.41 +static int GLES_ActivateRenderer(SDL_Renderer * renderer);
    1.42 +static int GLES_DisplayModeChanged(SDL_Renderer * renderer);
    1.43 +static int GLES_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
    1.44 +static int GLES_QueryTexturePixels(SDL_Renderer * renderer,
    1.45 +                                 SDL_Texture * texture, void **pixels,
    1.46 +                                 int *pitch);
    1.47 +static int GLES_SetTexturePalette(SDL_Renderer * renderer,
    1.48 +                                SDL_Texture * texture,
    1.49 +                                const SDL_Color * colors, int firstcolor,
    1.50 +                                int ncolors);
    1.51 +static int GLES_GetTexturePalette(SDL_Renderer * renderer,
    1.52 +                                SDL_Texture * texture, SDL_Color * colors,
    1.53 +                                int firstcolor, int ncolors);
    1.54 +static int GLES_SetTextureColorMod(SDL_Renderer * renderer,
    1.55 +                                 SDL_Texture * texture);
    1.56 +static int GLES_SetTextureAlphaMod(SDL_Renderer * renderer,
    1.57 +                                 SDL_Texture * texture);
    1.58 +static int GLES_SetTextureBlendMode(SDL_Renderer * renderer,
    1.59 +                                  SDL_Texture * texture);
    1.60 +static int GLES_SetTextureScaleMode(SDL_Renderer * renderer,
    1.61 +                                  SDL_Texture * texture);
    1.62 +static int GLES_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
    1.63 +                            const SDL_Rect * rect, const void *pixels,
    1.64 +                            int pitch);
    1.65 +static int GLES_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
    1.66 +                          const SDL_Rect * rect, int markDirty, void **pixels,
    1.67 +                          int *pitch);
    1.68 +static void GLES_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
    1.69 +static void GLES_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
    1.70 +                            int numrects, const SDL_Rect * rects);
    1.71 +static int GLES_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b,
    1.72 +                         Uint8 a, const SDL_Rect * rect);
    1.73 +static int GLES_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
    1.74 +                         const SDL_Rect * srcrect, const SDL_Rect * dstrect);
    1.75 +static void GLES_RenderPresent(SDL_Renderer * renderer);
    1.76 +static void GLES_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
    1.77 +static void GLES_DestroyRenderer(SDL_Renderer * renderer);
    1.78 +
    1.79 +
    1.80 +SDL_RenderDriver GL_ES_RenderDriver = {
    1.81 +    GLES_CreateRenderer,
    1.82 +    {
    1.83 +     "opengl_es",
    1.84 +     (SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTDISCARD |
    1.85 +      SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED),
    1.86 +     (SDL_TEXTUREMODULATE_NONE | SDL_TEXTUREMODULATE_COLOR |
    1.87 +      SDL_TEXTUREMODULATE_ALPHA),
    1.88 +     (SDL_TEXTUREBLENDMODE_NONE | SDL_TEXTUREBLENDMODE_MASK |
    1.89 +      SDL_TEXTUREBLENDMODE_BLEND | SDL_TEXTUREBLENDMODE_ADD |
    1.90 +      SDL_TEXTUREBLENDMODE_MOD),
    1.91 +     (SDL_TEXTURESCALEMODE_NONE | SDL_TEXTURESCALEMODE_FAST |
    1.92 +      SDL_TEXTURESCALEMODE_SLOW), 2,
    1.93 +		{
    1.94 +		SDL_PIXELFORMAT_RGB24,
    1.95 +		SDL_PIXELFORMAT_ABGR8888,
    1.96 +		},
    1.97 +	 0,
    1.98 +     0}
    1.99 +};
   1.100 +
   1.101 +typedef struct
   1.102 +{
   1.103 +    SDL_GLContext context;
   1.104 +    SDL_bool updateSize;
   1.105 +    int blendMode;
   1.106 +	
   1.107 +#ifndef APIENTRY
   1.108 +#define APIENTRY
   1.109 +#endif
   1.110 +	
   1.111 +	SDL_bool useDrawTexture;
   1.112 +	SDL_bool GL_OES_draw_texture_supported;
   1.113 +	
   1.114 +	/* OpenGL ES functions */
   1.115 +	#define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
   1.116 +	#include "SDL_glesfuncs.h"
   1.117 +	#undef SDL_PROC
   1.118 +
   1.119 +} GLES_RenderData;
   1.120 +
   1.121 +typedef struct
   1.122 +{
   1.123 +    GLuint texture;
   1.124 +    GLenum type;
   1.125 +    GLfloat texw;
   1.126 +    GLfloat texh;
   1.127 +    GLenum format;
   1.128 +    GLenum formattype;
   1.129 +    void *pixels;
   1.130 +    int pitch;
   1.131 +    SDL_DirtyRectList dirty;
   1.132 +} GLES_TextureData;
   1.133 +
   1.134 +static void
   1.135 +GLES_SetError(const char *prefix, GLenum result)
   1.136 +{
   1.137 +    const char *error;
   1.138 +
   1.139 +    switch (result) {
   1.140 +    case GL_NO_ERROR:
   1.141 +        error = "GL_NO_ERROR";
   1.142 +        break;
   1.143 +    case GL_INVALID_ENUM:
   1.144 +        error = "GL_INVALID_ENUM";
   1.145 +        break;
   1.146 +    case GL_INVALID_VALUE:
   1.147 +        error = "GL_INVALID_VALUE";
   1.148 +        break;
   1.149 +    case GL_INVALID_OPERATION:
   1.150 +        error = "GL_INVALID_OPERATION";
   1.151 +        break;
   1.152 +    case GL_STACK_OVERFLOW:
   1.153 +        error = "GL_STACK_OVERFLOW";
   1.154 +        break;
   1.155 +    case GL_STACK_UNDERFLOW:
   1.156 +        error = "GL_STACK_UNDERFLOW";
   1.157 +        break;
   1.158 +    case GL_OUT_OF_MEMORY:
   1.159 +        error = "GL_OUT_OF_MEMORY";
   1.160 +        break;
   1.161 +    default:
   1.162 +        error = "UNKNOWN";
   1.163 +        break;
   1.164 +    }
   1.165 +    SDL_SetError("%s: %s", prefix, error);
   1.166 +}
   1.167 +
   1.168 +static int
   1.169 +GLES_LoadFunctions(GLES_RenderData * data)
   1.170 +{
   1.171 +	
   1.172 +	#define SDL_PROC(ret,func,params) \
   1.173 +	data->func = func;
   1.174 +	#include "SDL_glesfuncs.h"
   1.175 +	#undef SDL_PROC
   1.176 +	
   1.177 +    return 0;
   1.178 +}
   1.179 +
   1.180 +void
   1.181 +GLES_AddRenderDriver(_THIS)
   1.182 +{
   1.183 +    if (_this->GL_CreateContext) {
   1.184 +        SDL_AddRenderDriver(0, &GL_ES_RenderDriver);
   1.185 +    }
   1.186 +}
   1.187 +
   1.188 +SDL_Renderer *
   1.189 +GLES_CreateRenderer(SDL_Window * window, Uint32 flags)
   1.190 +{
   1.191 +		
   1.192 +    SDL_Renderer *renderer;
   1.193 +    GLES_RenderData *data;
   1.194 +    GLint value;
   1.195 +    int doublebuffer;
   1.196 +
   1.197 +    if (!(window->flags & SDL_WINDOW_OPENGL)) {
   1.198 +        if (SDL_RecreateWindow(window, window->flags | SDL_WINDOW_OPENGL) < 0) {
   1.199 +            return NULL;
   1.200 +        }
   1.201 +    }
   1.202 +
   1.203 +    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
   1.204 +    if (!renderer) {
   1.205 +        SDL_OutOfMemory();
   1.206 +        return NULL;
   1.207 +    }
   1.208 +
   1.209 +    data = (GLES_RenderData *) SDL_calloc(1, sizeof(*data));
   1.210 +    if (!data) {
   1.211 +        GLES_DestroyRenderer(renderer);
   1.212 +        SDL_OutOfMemory();
   1.213 +        return NULL;
   1.214 +    }
   1.215 +
   1.216 +    renderer->ActivateRenderer = GLES_ActivateRenderer;
   1.217 +    renderer->DisplayModeChanged = GLES_DisplayModeChanged;
   1.218 +    renderer->CreateTexture = GLES_CreateTexture;
   1.219 +    renderer->QueryTexturePixels = GLES_QueryTexturePixels;
   1.220 +    renderer->SetTexturePalette = GLES_SetTexturePalette;
   1.221 +    renderer->GetTexturePalette = GLES_GetTexturePalette;
   1.222 +    renderer->SetTextureColorMod = GLES_SetTextureColorMod;
   1.223 +    renderer->SetTextureAlphaMod = GLES_SetTextureAlphaMod;
   1.224 +    renderer->SetTextureBlendMode = GLES_SetTextureBlendMode;
   1.225 +    renderer->SetTextureScaleMode = GLES_SetTextureScaleMode;
   1.226 +    renderer->UpdateTexture = GLES_UpdateTexture;
   1.227 +    renderer->LockTexture = GLES_LockTexture;
   1.228 +    renderer->UnlockTexture = GLES_UnlockTexture;
   1.229 +    renderer->DirtyTexture = GLES_DirtyTexture;
   1.230 +    renderer->RenderFill = GLES_RenderFill;
   1.231 +    renderer->RenderCopy = GLES_RenderCopy;
   1.232 +    renderer->RenderPresent = GLES_RenderPresent;
   1.233 +    renderer->DestroyTexture = GLES_DestroyTexture;
   1.234 +    renderer->DestroyRenderer = GLES_DestroyRenderer;
   1.235 +    renderer->info = GL_ES_RenderDriver.info;
   1.236 +    renderer->window = window->id;
   1.237 +    renderer->driverdata = data;
   1.238 +	
   1.239 +	
   1.240 +    renderer->info.flags = (SDL_RENDERER_PRESENTDISCARD | SDL_RENDERER_ACCELERATED);
   1.241 +
   1.242 +    if (GLES_LoadFunctions(data) < 0) {
   1.243 +        GLES_DestroyRenderer(renderer);
   1.244 +        return NULL;
   1.245 +    }
   1.246 +
   1.247 +    data->context = SDL_GL_CreateContext(window->id);
   1.248 +    if (!data->context) {
   1.249 +        GLES_DestroyRenderer(renderer);
   1.250 +        return NULL;
   1.251 +    }
   1.252 +    if (SDL_GL_MakeCurrent(window->id, data->context) < 0) {
   1.253 +        GLES_DestroyRenderer(renderer);
   1.254 +        return NULL;
   1.255 +    }
   1.256 +
   1.257 +    if (flags & SDL_RENDERER_PRESENTVSYNC) {
   1.258 +        SDL_GL_SetSwapInterval(1);
   1.259 +    } else {
   1.260 +        SDL_GL_SetSwapInterval(0);
   1.261 +    }
   1.262 +    if (SDL_GL_GetSwapInterval() > 0) {
   1.263 +        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
   1.264 +    }
   1.265 +
   1.266 +    if (SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &doublebuffer) == 0) {
   1.267 +        if (!doublebuffer) {
   1.268 +            renderer->info.flags |= SDL_RENDERER_SINGLEBUFFER;
   1.269 +        }
   1.270 +    }
   1.271 +	
   1.272 +	if (SDL_GL_ExtensionSupported("GL_OES_draw_texture")) {
   1.273 +		data->GL_OES_draw_texture_supported = SDL_TRUE;
   1.274 +		data->useDrawTexture = SDL_TRUE;
   1.275 +	}
   1.276 +	else {
   1.277 +		data->GL_OES_draw_texture_supported = SDL_FALSE;
   1.278 +		data->useDrawTexture = SDL_FALSE;
   1.279 +	}	
   1.280 +
   1.281 +    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
   1.282 +    renderer->info.max_texture_width = value;
   1.283 +    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
   1.284 +    renderer->info.max_texture_height = value;
   1.285 +
   1.286 +    /* Set up parameters for rendering */
   1.287 +    data->blendMode = -1;
   1.288 +    data->glDisable(GL_DEPTH_TEST);
   1.289 +    data->glDisable(GL_CULL_FACE);
   1.290 +	data->glEnable(GL_TEXTURE_2D);
   1.291 +    data->updateSize = SDL_TRUE;
   1.292 +
   1.293 +    return renderer;
   1.294 +}
   1.295 +
   1.296 +static int GLES_ActivateRenderer(SDL_Renderer * renderer)
   1.297 +{
   1.298 +		
   1.299 +    GLES_RenderData *data = (GLES_RenderData *)renderer->driverdata;
   1.300 +    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
   1.301 +				
   1.302 +    if (SDL_GL_MakeCurrent(window->id, data->context) < 0) {
   1.303 +        return -1;
   1.304 +    }
   1.305 +    if (data->updateSize) {
   1.306 +        data->glMatrixMode(GL_PROJECTION);
   1.307 +        data->glLoadIdentity();
   1.308 +        data->glMatrixMode(GL_MODELVIEW);
   1.309 +        data->glLoadIdentity();
   1.310 +        data->glViewport(0, 0, window->w, window->h);
   1.311 +        data->glOrthof(0.0, (GLfloat)window->w, (GLfloat)window->h, 0.0,
   1.312 +                      0.0, 1.0);
   1.313 +        data->updateSize = SDL_FALSE;
   1.314 +    }
   1.315 +    return 0;
   1.316 +}
   1.317 +
   1.318 +static int
   1.319 +GLES_DisplayModeChanged(SDL_Renderer * renderer)
   1.320 +{
   1.321 +    GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   1.322 +
   1.323 +    data->updateSize = SDL_TRUE;
   1.324 +    return 0;
   1.325 +}
   1.326 +
   1.327 +static __inline__ int
   1.328 +power_of_2(int input)
   1.329 +{
   1.330 +    int value = 1;
   1.331 +
   1.332 +    while (value < input) {
   1.333 +        value <<= 1;
   1.334 +    }
   1.335 +    return value;
   1.336 +}
   1.337 +
   1.338 +static int
   1.339 +GLES_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   1.340 +{
   1.341 +    GLES_RenderData *renderdata = (GLES_RenderData *) renderer->driverdata;
   1.342 +    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
   1.343 +    GLES_TextureData *data;
   1.344 +    GLint internalFormat;
   1.345 +    GLenum format, type;
   1.346 +    int texture_w, texture_h;
   1.347 +    GLenum result;
   1.348 +	switch (texture->format) {
   1.349 +		case SDL_PIXELFORMAT_INDEX1LSB:
   1.350 +		case SDL_PIXELFORMAT_INDEX1MSB:
   1.351 +		case SDL_PIXELFORMAT_INDEX8:
   1.352 +		case SDL_PIXELFORMAT_RGB332:
   1.353 +		case SDL_PIXELFORMAT_RGB444:
   1.354 +		case SDL_PIXELFORMAT_RGB555:
   1.355 +		case SDL_PIXELFORMAT_ARGB4444:
   1.356 +		case SDL_PIXELFORMAT_ARGB1555:
   1.357 +		case SDL_PIXELFORMAT_BGR24:
   1.358 +		case SDL_PIXELFORMAT_BGR888:
   1.359 +		case SDL_PIXELFORMAT_RGB888:
   1.360 +		case SDL_PIXELFORMAT_RGBA8888:
   1.361 +		case SDL_PIXELFORMAT_ARGB2101010:
   1.362 +		case SDL_PIXELFORMAT_ARGB8888:
   1.363 +		case SDL_PIXELFORMAT_RGB24:
   1.364 +			internalFormat = GL_RGB;
   1.365 +			format = GL_RGB;
   1.366 +			type = GL_UNSIGNED_BYTE;
   1.367 +			break;
   1.368 +		case SDL_PIXELFORMAT_ABGR8888:
   1.369 +			internalFormat = GL_RGBA;
   1.370 +			format = GL_RGBA;
   1.371 +			type = GL_UNSIGNED_BYTE;			
   1.372 +			break;			
   1.373 +		/*
   1.374 +			These formats would be supported if SDL had the necessary pixel formats
   1.375 +		case SDL_PIXELFORMAT_BGR565:
   1.376 +			internalFormat = GL_RGB;
   1.377 +			format = GL_RGB;
   1.378 +			type = GL_UNSIGNED_SHORT_5_6_5;
   1.379 +			break;			
   1.380 +		case SDL_PIXELFORMAT_ABGR5551:
   1.381 +			internalFormat = GL_RGBA;
   1.382 +			format = GL_RGBA;
   1.383 +			type = GL_UNSIGNED_SHORT_5_5_5_1;
   1.384 +			break;
   1.385 +		case SDL_PIXELFORMAT_ABGR4444:
   1.386 +			internalFormat = GL_RGBA;
   1.387 +			format = GL_RGBA;
   1.388 +			type = GL_UNSIGNED_SHORT_4_4_4_4;
   1.389 +			break;
   1.390 +		*/
   1.391 +		default:
   1.392 +			SDL_SetError("Unsupported texture format");
   1.393 +			return -1;
   1.394 +    }
   1.395 +	
   1.396 +	data = (GLES_TextureData *) SDL_calloc(1, sizeof(*data));
   1.397 +    if (!data) {
   1.398 +        SDL_OutOfMemory();
   1.399 +        return -1;
   1.400 +    }
   1.401 +
   1.402 +    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
   1.403 +        data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
   1.404 +        data->pixels = SDL_malloc(texture->h * data->pitch);
   1.405 +        if (!data->pixels) {
   1.406 +            SDL_OutOfMemory();
   1.407 +            SDL_free(data);
   1.408 +            return -1;
   1.409 +        }
   1.410 +    }
   1.411 +
   1.412 +    texture->driverdata = data;
   1.413 +
   1.414 +    renderdata->glGetError();
   1.415 +    renderdata->glGenTextures(1, &data->texture);
   1.416 +    
   1.417 +	data->type = GL_TEXTURE_2D;
   1.418 +	/* no NPOV textures allowed in OpenGL ES (yet) */
   1.419 +	texture_w = power_of_2(texture->w);
   1.420 +	texture_h = power_of_2(texture->h);
   1.421 +	data->texw = (GLfloat) texture->w / texture_w;
   1.422 +	data->texh = (GLfloat) texture->h / texture_h;
   1.423 +	
   1.424 +    data->format = format;
   1.425 +    data->formattype = type;
   1.426 +    renderdata->glBindTexture(data->type, data->texture);
   1.427 +    renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
   1.428 +                                GL_NEAREST);
   1.429 +    renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
   1.430 +                                GL_NEAREST);
   1.431 +    renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
   1.432 +                                GL_CLAMP_TO_EDGE);
   1.433 +    renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
   1.434 +                                GL_CLAMP_TO_EDGE);
   1.435 +    
   1.436 +	renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
   1.437 +							 texture_h, 0, format, type, NULL);
   1.438 +
   1.439 +    result = renderdata->glGetError();
   1.440 +    if (result != GL_NO_ERROR) {
   1.441 +        GLES_SetError("glTexImage2D()", result);
   1.442 +        return -1;
   1.443 +    }
   1.444 +    return 0;
   1.445 +}
   1.446 +
   1.447 +static int
   1.448 +GLES_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
   1.449 +                      void **pixels, int *pitch)
   1.450 +{
   1.451 +    GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
   1.452 +
   1.453 +    *pixels = data->pixels;
   1.454 +    *pitch = data->pitch;
   1.455 +    return 0;
   1.456 +}
   1.457 +
   1.458 +static int
   1.459 +GLES_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   1.460 +                     const SDL_Color * colors, int firstcolor, int ncolors)
   1.461 +{
   1.462 +	SDL_SetError("OpenGL ES does not support paletted textures");
   1.463 +    return -1;
   1.464 +}
   1.465 +
   1.466 +static int
   1.467 +GLES_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   1.468 +                     SDL_Color * colors, int firstcolor, int ncolors)
   1.469 +{
   1.470 +	SDL_SetError("OpenGL ES does not support paletted textures");
   1.471 +    return -1;
   1.472 +}
   1.473 +
   1.474 +static void
   1.475 +SetupTextureUpdate(GLES_RenderData * renderdata, SDL_Texture * texture,
   1.476 +                   int pitch)
   1.477 +{
   1.478 +	
   1.479 +	
   1.480 +	GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
   1.481 +	renderdata->glBindTexture(data->type, data->texture);
   1.482 +    renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
   1.483 +}
   1.484 +
   1.485 +static int
   1.486 +GLES_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
   1.487 +{
   1.488 +    return 0;
   1.489 +}
   1.490 +
   1.491 +static int
   1.492 +GLES_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
   1.493 +{
   1.494 +    return 0;
   1.495 +}
   1.496 +
   1.497 +static int
   1.498 +GLES_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
   1.499 +{
   1.500 +    switch (texture->blendMode) {
   1.501 +    case SDL_TEXTUREBLENDMODE_NONE:
   1.502 +    case SDL_TEXTUREBLENDMODE_MASK:
   1.503 +    case SDL_TEXTUREBLENDMODE_BLEND:
   1.504 +    case SDL_TEXTUREBLENDMODE_ADD:
   1.505 +    case SDL_TEXTUREBLENDMODE_MOD:
   1.506 +        return 0;
   1.507 +    default:
   1.508 +        SDL_Unsupported();
   1.509 +        texture->blendMode = SDL_TEXTUREBLENDMODE_NONE;
   1.510 +        return -1;
   1.511 +    }
   1.512 +}
   1.513 +
   1.514 +static int
   1.515 +GLES_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture)
   1.516 +{
   1.517 +    switch (texture->scaleMode) {
   1.518 +    case SDL_TEXTURESCALEMODE_NONE:
   1.519 +    case SDL_TEXTURESCALEMODE_FAST:
   1.520 +    case SDL_TEXTURESCALEMODE_SLOW:
   1.521 +        return 0;
   1.522 +    case SDL_TEXTURESCALEMODE_BEST:
   1.523 +        SDL_Unsupported();
   1.524 +		texture->scaleMode = SDL_TEXTURESCALEMODE_SLOW;
   1.525 +        return -1;
   1.526 +    default:
   1.527 +        SDL_Unsupported();
   1.528 +        texture->scaleMode = SDL_TEXTURESCALEMODE_NONE;
   1.529 +        return -1;
   1.530 +    }
   1.531 +}
   1.532 +
   1.533 +static int
   1.534 +GLES_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   1.535 +                 const SDL_Rect * rect, const void *pixels, int pitch)
   1.536 +{
   1.537 +    GLES_RenderData *renderdata = (GLES_RenderData *) renderer->driverdata;
   1.538 +    GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
   1.539 +    GLenum result;
   1.540 +
   1.541 +    SetupTextureUpdate(renderdata, texture, pitch);
   1.542 +	renderdata->glGetError();
   1.543 +	renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
   1.544 +                                rect->h, data->format, data->formattype,
   1.545 +                                pixels);
   1.546 +    result = renderdata->glGetError();
   1.547 +    if (result != GL_NO_ERROR) {
   1.548 +        GLES_SetError("glTexSubImage2D()", result);
   1.549 +        return -1;
   1.550 +    }
   1.551 +    return 0;
   1.552 +}
   1.553 +
   1.554 +static int
   1.555 +GLES_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   1.556 +               const SDL_Rect * rect, int markDirty, void **pixels,
   1.557 +               int *pitch)
   1.558 +{
   1.559 +    GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
   1.560 +
   1.561 +    if (markDirty) {
   1.562 +        SDL_AddDirtyRect(&data->dirty, rect);
   1.563 +    }
   1.564 +
   1.565 +    *pixels =
   1.566 +        (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
   1.567 +                  rect->x * SDL_BYTESPERPIXEL(texture->format));
   1.568 +    *pitch = data->pitch;
   1.569 +    return 0;
   1.570 +}
   1.571 +
   1.572 +static void
   1.573 +GLES_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   1.574 +{
   1.575 +}
   1.576 +
   1.577 +static void
   1.578 +GLES_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture, int numrects,
   1.579 +                const SDL_Rect * rects)
   1.580 +{
   1.581 +    GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
   1.582 +    int i;
   1.583 +
   1.584 +    for (i = 0; i < numrects; ++i) {
   1.585 +        SDL_AddDirtyRect(&data->dirty, &rects[i]);
   1.586 +    }
   1.587 +}
   1.588 +
   1.589 +static int
   1.590 +GLES_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b, Uint8 a,
   1.591 +              const SDL_Rect * rect)
   1.592 +{
   1.593 + 	
   1.594 +	GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   1.595 +    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
   1.596 +	
   1.597 +	/* set proper drawing color */
   1.598 +	GLfloat oldClearColor[4];
   1.599 +	
   1.600 +	data->glGetFloatv(GL_COLOR_CLEAR_VALUE, oldClearColor);
   1.601 +	
   1.602 +	data->glClearColor((GLclampf) r * inv255f, (GLclampf) g * inv255f,
   1.603 +			  (GLclampf) b * inv255f, (GLclampf) a * inv255f);
   1.604 +	
   1.605 +	data->glScissor(rect->x, window->h - rect->y - rect->h, rect->w, rect->h);
   1.606 +	data->glEnable(GL_SCISSOR_TEST);
   1.607 +	data->glClear(GL_COLOR_BUFFER_BIT);
   1.608 +	data->glDisable(GL_SCISSOR_TEST);
   1.609 +	
   1.610 +	/* reset clear color */
   1.611 +	data->glClearColor(oldClearColor[0], oldClearColor[1], oldClearColor[2], oldClearColor[2]);
   1.612 +	
   1.613 +	return 0;
   1.614 +}
   1.615 +
   1.616 +static int
   1.617 +GLES_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
   1.618 +              const SDL_Rect * srcrect, const SDL_Rect * dstrect)
   1.619 +{
   1.620 +		
   1.621 +    GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   1.622 +    GLES_TextureData *texturedata = (GLES_TextureData *) texture->driverdata;
   1.623 +    int minx, miny, maxx, maxy;
   1.624 +    GLfloat minu, maxu, minv, maxv;
   1.625 +	int i;
   1.626 +	void *temp_buffer; /* used for reformatting dirty rect pixels */
   1.627 +	void *temp_ptr;
   1.628 +	
   1.629 +    if (texturedata->dirty.list) {
   1.630 +        SDL_DirtyRect *dirty;
   1.631 +        void *pixels;
   1.632 +        int bpp = SDL_BYTESPERPIXEL(texture->format);
   1.633 +        int pitch = texturedata->pitch;
   1.634 +
   1.635 +        SetupTextureUpdate(data, texture, pitch);
   1.636 +		
   1.637 +        data->glBindTexture(texturedata->type, texturedata->texture);
   1.638 +        for (dirty = texturedata->dirty.list; dirty; dirty = dirty->next) {
   1.639 +            SDL_Rect *rect = &dirty->rect;
   1.640 +            pixels = (void *) ((Uint8 *) texturedata->pixels + rect->y * pitch + rect->x * bpp);
   1.641 +			/*	There is no GL_UNPACK_ROW_LENGTH in OpenGLES 
   1.642 +				we must do this reformatting ourselves(!)
   1.643 +			 
   1.644 +				maybe it'd be a good idea to keep a temp buffer around
   1.645 +				for this purpose rather than allocating it each time
   1.646 +			*/
   1.647 +			temp_buffer = SDL_malloc(rect->w * rect->h * bpp);
   1.648 +			temp_ptr = temp_buffer;
   1.649 +			for (i=0; i<rect->h; i++) {
   1.650 +				SDL_memcpy(temp_ptr, pixels, rect->w * bpp);
   1.651 +				temp_ptr += rect->w * bpp;
   1.652 +				pixels	 += pitch;
   1.653 +			}
   1.654 +			
   1.655 +            data->glTexSubImage2D(texturedata->type, 0, rect->x, rect->y,
   1.656 +                                  rect->w, rect->h, texturedata->format,
   1.657 +                                  texturedata->formattype, temp_buffer);
   1.658 +			
   1.659 +			SDL_free(temp_buffer);
   1.660 +			
   1.661 +        }
   1.662 +        SDL_ClearDirtyRects(&texturedata->dirty);
   1.663 +    }
   1.664 +	
   1.665 +    data->glBindTexture(texturedata->type, texturedata->texture);
   1.666 +	data->glEnable(GL_TEXTURE_2D);
   1.667 +	
   1.668 +    if (texture->modMode) {
   1.669 +        data->glColor4f((GLfloat) texture->r * inv255f,
   1.670 +                        (GLfloat) texture->g * inv255f,
   1.671 +                        (GLfloat) texture->b * inv255f,
   1.672 +                        (GLfloat) texture->a * inv255f);
   1.673 +    } else {
   1.674 +        data->glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
   1.675 +    }
   1.676 +
   1.677 +    if (texture->blendMode != data->blendMode) {
   1.678 +        switch (texture->blendMode) {
   1.679 +        case SDL_TEXTUREBLENDMODE_NONE:
   1.680 +            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
   1.681 +            data->glDisable(GL_BLEND);
   1.682 +            break;
   1.683 +        case SDL_TEXTUREBLENDMODE_MASK:
   1.684 +        case SDL_TEXTUREBLENDMODE_BLEND:
   1.685 +            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   1.686 +            data->glEnable(GL_BLEND);
   1.687 +            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   1.688 +            break;
   1.689 +        case SDL_TEXTUREBLENDMODE_ADD:
   1.690 +            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   1.691 +            data->glEnable(GL_BLEND);
   1.692 +            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE);
   1.693 +            break;
   1.694 +        case SDL_TEXTUREBLENDMODE_MOD:
   1.695 +            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   1.696 +            data->glEnable(GL_BLEND);
   1.697 +            data->glBlendFunc(GL_ZERO, GL_SRC_COLOR);
   1.698 +            break;
   1.699 +        }
   1.700 +        data->blendMode = texture->blendMode;
   1.701 +    }
   1.702 +
   1.703 +	switch (texture->scaleMode) {
   1.704 +		case SDL_TEXTURESCALEMODE_NONE:
   1.705 +		case SDL_TEXTURESCALEMODE_FAST:
   1.706 +			data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
   1.707 +								  GL_NEAREST);
   1.708 +			data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
   1.709 +								  GL_NEAREST);
   1.710 +			break;
   1.711 +		case SDL_TEXTURESCALEMODE_SLOW:
   1.712 +		case SDL_TEXTURESCALEMODE_BEST:
   1.713 +			data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
   1.714 +								  GL_LINEAR);
   1.715 +			data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
   1.716 +								  GL_LINEAR);
   1.717 +			break;
   1.718 +	}
   1.719 +
   1.720 +	if (data->GL_OES_draw_texture_supported && data->useDrawTexture) {
   1.721 +		/* this code is a little funny because the viewport is upside down vs SDL's coordinate system */
   1.722 +		SDL_Window *window = SDL_GetWindowFromID(renderer->window);
   1.723 +		GLint cropRect[4];
   1.724 +		cropRect[0] = srcrect->x;
   1.725 +		cropRect[1] = srcrect->y + srcrect->h;
   1.726 +		cropRect[2] = srcrect->w;
   1.727 +		cropRect[3] = -srcrect->h;
   1.728 +		data->glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect);
   1.729 +		data->glDrawTexiOES(dstrect->x, window->h - dstrect->y - dstrect->h, 0, dstrect->w, dstrect->h);
   1.730 +	}
   1.731 +	else {
   1.732 +		
   1.733 +		minx = dstrect->x;
   1.734 +		miny = dstrect->y;
   1.735 +		maxx = dstrect->x + dstrect->w;
   1.736 +		maxy = dstrect->y + dstrect->h;
   1.737 +		
   1.738 +		minu = (GLfloat) srcrect->x / texture->w;
   1.739 +		minu *= texturedata->texw;
   1.740 +		maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
   1.741 +		maxu *= texturedata->texw;
   1.742 +		minv = (GLfloat) srcrect->y / texture->h;
   1.743 +		minv *= texturedata->texh;
   1.744 +		maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
   1.745 +		maxv *= texturedata->texh;			
   1.746 +		
   1.747 +		GLshort vertices[8];
   1.748 +		GLfloat texCoords[8];
   1.749 +		
   1.750 +		vertices[0] = minx; vertices[1] = miny;
   1.751 +		vertices[2] = maxx; vertices[3] = miny;
   1.752 +		vertices[4]	= minx; vertices[5] = maxy;
   1.753 +		vertices[6] = maxx; vertices[7] = maxy;	
   1.754 +		
   1.755 +		texCoords[0] = minu; texCoords[1] = minv;
   1.756 +		texCoords[2] = maxu; texCoords[3] = minv;
   1.757 +		texCoords[4] = minu; texCoords[5] = maxv;
   1.758 +		texCoords[6] = maxu; texCoords[7] = maxv;	
   1.759 +		
   1.760 +		data->glVertexPointer(2, GL_SHORT, 0, vertices);
   1.761 +		data->glEnableClientState(GL_VERTEX_ARRAY);
   1.762 +		data->glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
   1.763 +		data->glEnableClientState(GL_TEXTURE_COORD_ARRAY);
   1.764 +		data->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
   1.765 +		
   1.766 +	}
   1.767 +		
   1.768 +    return 0;
   1.769 +}
   1.770 +
   1.771 +static void
   1.772 +GLES_RenderPresent(SDL_Renderer * renderer)
   1.773 +{
   1.774 +    SDL_GL_SwapWindow(renderer->window);
   1.775 +}
   1.776 +
   1.777 +static void
   1.778 +GLES_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   1.779 +{
   1.780 +    GLES_RenderData *renderdata = (GLES_RenderData *) renderer->driverdata;
   1.781 +    GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
   1.782 +
   1.783 +    if (!data) {
   1.784 +        return;
   1.785 +    }
   1.786 +    if (data->texture) {
   1.787 +		glDeleteTextures(1, &data->texture);
   1.788 +    }
   1.789 +    if (data->pixels) {
   1.790 +        SDL_free(data->pixels);
   1.791 +    }
   1.792 +    SDL_FreeDirtyRects(&data->dirty);
   1.793 +    SDL_free(data);
   1.794 +    texture->driverdata = NULL;
   1.795 +}
   1.796 +
   1.797 +static void
   1.798 +GLES_DestroyRenderer(SDL_Renderer * renderer)
   1.799 +{
   1.800 +    GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   1.801 +
   1.802 +    if (data) {
   1.803 +        if (data->context) {
   1.804 +            /* SDL_GL_MakeCurrent(0, NULL); *//* doesn't do anything */
   1.805 +            SDL_GL_DeleteContext(data->context);
   1.806 +        }
   1.807 +        SDL_free(data);
   1.808 +    }
   1.809 +    SDL_free(renderer);
   1.810 +}
   1.811 +
   1.812 +#endif /* SDL_VIDEO_RENDER_OGL */
   1.813 +
   1.814 +/* vi: set ts=4 sw=4 expandtab: */
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/video/SDL_renderer_gles.h	Tue Sep 02 00:37:04 2008 +0000
     2.3 @@ -0,0 +1,28 @@
     2.4 +/*
     2.5 +    SDL - Simple DirectMedia Layer
     2.6 +    Copyright (C) 1997-2006 Sam Lantinga
     2.7 +
     2.8 +    This library is free software; you can redistribute it and/or
     2.9 +    modify it under the terms of the GNU Lesser General Public
    2.10 +    License as published by the Free Software Foundation; either
    2.11 +    version 2.1 of the License, or (at your option) any later version.
    2.12 +
    2.13 +    This library is distributed in the hope that it will be useful,
    2.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    2.16 +    Lesser General Public License for more details.
    2.17 +
    2.18 +    You should have received a copy of the GNU Lesser General Public
    2.19 +    License along with this library; if not, write to the Free Software
    2.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    2.21 +
    2.22 +    Sam Lantinga
    2.23 +    slouken@libsdl.org
    2.24 +*/
    2.25 +#include "SDL_config.h"
    2.26 +
    2.27 +/* OpenGL renderer implementation */
    2.28 +
    2.29 +extern SDL_RenderDriver GL_ES_RenderDriver;
    2.30 +
    2.31 +/* vi: set ts=4 sw=4 expandtab: */