These files are the OpenGL ES render driver. You should be able to use them on any platform that supports OpenGL ES -- not just iPhone. The driver is based off the OpenGL render driver. gsoc2008_iphone
authorHolmes Futrell <hfutrell@umail.ucsb.edu>
Thu, 17 Jul 2008 23:31:42 +0000
branchgsoc2008_iphone
changeset 2359b70b96e615d2
parent 2358 da266ae53b98
child 2360 96411dcd2267
These files are the OpenGL ES render driver. You should be able to use them on any platform that supports OpenGL ES -- not just iPhone. The driver is based off the OpenGL render driver.

There are a few differences between OpenGL and OpenGL ES that present difficulties for this driver:
- OpenGL ES does NOT support many pixel formats (for example, no GL_BGR). Also, when using texture functions format and internalFormat must be the same -- this means 32 bit packed formats like SDL_PIXELFORMAT_BGR888 cannot be automatically converted to GL_RGB (which is 24bpp).
- OpenGL ES doesn't have GL_PACK_ROW_LENGTH, which means data must be reformatted before uploading changes to dirty rects. This change has been added.
- OpenGL ES doesn't support paletted textures, though there is an extension. I'm looking into this.

Some other notable differences:
- OpenGL ES has an extension called GL_OES_draw_texture which allows for quicker 2D sprite-type drawing. I use this in GL_RenderCopy when it is available. The iPhone supports the extension, but the iPhone Simulator does not (presently).
- No glBegin() / glEnd() and no GL_QUADS! I'm using glDrawArrays with GL_TRIANGLE_STRIP instead!
src/video/glesrenderer/SDL_renderer_gles.c
src/video/glesrenderer/SDL_renderer_gles.h
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/video/glesrenderer/SDL_renderer_gles.c	Thu Jul 17 23:31:42 2008 +0000
     1.3 @@ -0,0 +1,841 @@
     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 */
    1.37 +
    1.38 +static const float inv255f = 1.0f / 255.0f;
    1.39 +
    1.40 +static SDL_Renderer *GL_CreateRenderer(SDL_Window * window, Uint32 flags);
    1.41 +static int GL_ActivateRenderer(SDL_Renderer * renderer);
    1.42 +static int GL_DisplayModeChanged(SDL_Renderer * renderer);
    1.43 +static int GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
    1.44 +static int GL_QueryTexturePixels(SDL_Renderer * renderer,
    1.45 +                                 SDL_Texture * texture, void **pixels,
    1.46 +                                 int *pitch);
    1.47 +static int GL_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 GL_GetTexturePalette(SDL_Renderer * renderer,
    1.52 +                                SDL_Texture * texture, SDL_Color * colors,
    1.53 +                                int firstcolor, int ncolors);
    1.54 +static int GL_SetTextureColorMod(SDL_Renderer * renderer,
    1.55 +                                 SDL_Texture * texture);
    1.56 +static int GL_SetTextureAlphaMod(SDL_Renderer * renderer,
    1.57 +                                 SDL_Texture * texture);
    1.58 +static int GL_SetTextureBlendMode(SDL_Renderer * renderer,
    1.59 +                                  SDL_Texture * texture);
    1.60 +static int GL_SetTextureScaleMode(SDL_Renderer * renderer,
    1.61 +                                  SDL_Texture * texture);
    1.62 +static int GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
    1.63 +                            const SDL_Rect * rect, const void *pixels,
    1.64 +                            int pitch);
    1.65 +static int GL_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 GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
    1.69 +static void GL_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
    1.70 +                            int numrects, const SDL_Rect * rects);
    1.71 +static int GL_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b,
    1.72 +                         Uint8 a, const SDL_Rect * rect);
    1.73 +static int GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
    1.74 +                         const SDL_Rect * srcrect, const SDL_Rect * dstrect);
    1.75 +static void GL_RenderPresent(SDL_Renderer * renderer);
    1.76 +static void GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
    1.77 +static void GL_DestroyRenderer(SDL_Renderer * renderer);
    1.78 +
    1.79 +
    1.80 +SDL_RenderDriver GL_ES_RenderDriver = {
    1.81 +    GL_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),
    1.93 +#ifdef GL_IMG_texture_format_BGRA8888     
    1.94 +		3,
    1.95 +#else
    1.96 +		2,
    1.97 +#endif
    1.98 +     {
    1.99 +#ifdef GL_IMG_texture_format_BGRA8888
   1.100 +		SDL_PIXELFORMAT_ARGB8888,
   1.101 +#endif
   1.102 +		SDL_PIXELFORMAT_RGB24,
   1.103 +		SDL_PIXELFORMAT_ABGR8888,
   1.104 +		},
   1.105 +	 0,
   1.106 +     0}
   1.107 +};
   1.108 +
   1.109 +typedef struct
   1.110 +{
   1.111 +    SDL_GLContext context;
   1.112 +    SDL_bool updateSize;
   1.113 +    int blendMode;
   1.114 +    int scaleMode;
   1.115 +	
   1.116 +#ifndef APIENTRY
   1.117 +#define APIENTRY
   1.118 +#endif
   1.119 +	
   1.120 +	SDL_bool useDrawTexture;
   1.121 +	SDL_bool GL_OES_draw_texture_supported;
   1.122 +	
   1.123 +	/* OpenGL ES functions */
   1.124 +	#define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
   1.125 +	#include "SDL_glesfuncs.h"
   1.126 +	#undef SDL_PROC
   1.127 +
   1.128 +} GL_RenderData;
   1.129 +
   1.130 +typedef struct
   1.131 +{
   1.132 +    GLuint texture;
   1.133 +    GLenum type;
   1.134 +    GLfloat texw;
   1.135 +    GLfloat texh;
   1.136 +    GLenum format;
   1.137 +    GLenum formattype;
   1.138 +    void *pixels;
   1.139 +    int pitch;
   1.140 +    SDL_DirtyRectList dirty;
   1.141 +} GL_TextureData;
   1.142 +
   1.143 +static void
   1.144 +GL_SetError(const char *prefix, GLenum result)
   1.145 +{
   1.146 +    const char *error;
   1.147 +
   1.148 +    switch (result) {
   1.149 +    case GL_NO_ERROR:
   1.150 +        error = "GL_NO_ERROR";
   1.151 +        break;
   1.152 +    case GL_INVALID_ENUM:
   1.153 +        error = "GL_INVALID_ENUM";
   1.154 +        break;
   1.155 +    case GL_INVALID_VALUE:
   1.156 +        error = "GL_INVALID_VALUE";
   1.157 +        break;
   1.158 +    case GL_INVALID_OPERATION:
   1.159 +        error = "GL_INVALID_OPERATION";
   1.160 +        break;
   1.161 +    case GL_STACK_OVERFLOW:
   1.162 +        error = "GL_STACK_OVERFLOW";
   1.163 +        break;
   1.164 +    case GL_STACK_UNDERFLOW:
   1.165 +        error = "GL_STACK_UNDERFLOW";
   1.166 +        break;
   1.167 +    case GL_OUT_OF_MEMORY:
   1.168 +        error = "GL_OUT_OF_MEMORY";
   1.169 +        break;
   1.170 +    default:
   1.171 +        error = "UNKNOWN";
   1.172 +        break;
   1.173 +    }
   1.174 +    SDL_SetError("%s: %s", prefix, error);
   1.175 +}
   1.176 +
   1.177 +static int
   1.178 +GL_LoadFunctions(GL_RenderData * data)
   1.179 +{
   1.180 +	
   1.181 +	#define SDL_PROC(ret,func,params) \
   1.182 +	data->func = func;
   1.183 +	#include "SDL_glesfuncs.h"
   1.184 +	#undef SDL_PROC
   1.185 +	
   1.186 +    return 0;
   1.187 +}
   1.188 +
   1.189 +void
   1.190 +GL_AddRenderDriver(_THIS)
   1.191 +{
   1.192 +    if (_this->GL_CreateContext) {
   1.193 +        SDL_AddRenderDriver(0, &GL_ES_RenderDriver);
   1.194 +    }
   1.195 +}
   1.196 +
   1.197 +SDL_Renderer *
   1.198 +GL_CreateRenderer(SDL_Window * window, Uint32 flags)
   1.199 +{
   1.200 +	
   1.201 +	printf("gl(es) create renderer ...\n");
   1.202 +	
   1.203 +    SDL_Renderer *renderer;
   1.204 +    GL_RenderData *data;
   1.205 +    GLint value;
   1.206 +    int doublebuffer;
   1.207 +
   1.208 +    if (!(window->flags & SDL_WINDOW_OPENGL)) {
   1.209 +        if (SDL_RecreateWindow(window, window->flags | SDL_WINDOW_OPENGL) < 0) {
   1.210 +            return NULL;
   1.211 +        }
   1.212 +    }
   1.213 +
   1.214 +    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
   1.215 +    if (!renderer) {
   1.216 +        SDL_OutOfMemory();
   1.217 +        return NULL;
   1.218 +    }
   1.219 +
   1.220 +    data = (GL_RenderData *) SDL_calloc(1, sizeof(*data));
   1.221 +    if (!data) {
   1.222 +        GL_DestroyRenderer(renderer);
   1.223 +        SDL_OutOfMemory();
   1.224 +        return NULL;
   1.225 +    }
   1.226 +
   1.227 +    renderer->ActivateRenderer = GL_ActivateRenderer;
   1.228 +    renderer->DisplayModeChanged = GL_DisplayModeChanged;
   1.229 +    renderer->CreateTexture = GL_CreateTexture;
   1.230 +    renderer->QueryTexturePixels = GL_QueryTexturePixels;
   1.231 +    renderer->SetTexturePalette = GL_SetTexturePalette;
   1.232 +    renderer->GetTexturePalette = GL_GetTexturePalette;
   1.233 +    renderer->SetTextureColorMod = GL_SetTextureColorMod;
   1.234 +    renderer->SetTextureAlphaMod = GL_SetTextureAlphaMod;
   1.235 +    renderer->SetTextureBlendMode = GL_SetTextureBlendMode;
   1.236 +    renderer->SetTextureScaleMode = GL_SetTextureScaleMode;
   1.237 +    renderer->UpdateTexture = GL_UpdateTexture;
   1.238 +    renderer->LockTexture = GL_LockTexture;
   1.239 +    renderer->UnlockTexture = GL_UnlockTexture;
   1.240 +    renderer->DirtyTexture = GL_DirtyTexture;
   1.241 +    renderer->RenderFill = GL_RenderFill;
   1.242 +    renderer->RenderCopy = GL_RenderCopy;
   1.243 +    renderer->RenderPresent = GL_RenderPresent;
   1.244 +    renderer->DestroyTexture = GL_DestroyTexture;
   1.245 +    renderer->DestroyRenderer = GL_DestroyRenderer;
   1.246 +    renderer->info = GL_ES_RenderDriver.info;
   1.247 +    renderer->window = window->id;
   1.248 +    renderer->driverdata = data;
   1.249 +	
   1.250 +	
   1.251 +    renderer->info.flags = (SDL_RENDERER_PRESENTDISCARD | SDL_RENDERER_ACCELERATED);
   1.252 +
   1.253 +    if (GL_LoadFunctions(data) < 0) {
   1.254 +        GL_DestroyRenderer(renderer);
   1.255 +        return NULL;
   1.256 +    }
   1.257 +
   1.258 +    data->context = SDL_GL_CreateContext(window->id);
   1.259 +    if (!data->context) {
   1.260 +        GL_DestroyRenderer(renderer);
   1.261 +        return NULL;
   1.262 +    }
   1.263 +    if (SDL_GL_MakeCurrent(window->id, data->context) < 0) {
   1.264 +        GL_DestroyRenderer(renderer);
   1.265 +        return NULL;
   1.266 +    }
   1.267 +
   1.268 +    if (flags & SDL_RENDERER_PRESENTVSYNC) {
   1.269 +        SDL_GL_SetSwapInterval(1);
   1.270 +    } else {
   1.271 +        SDL_GL_SetSwapInterval(0);
   1.272 +    }
   1.273 +    if (SDL_GL_GetSwapInterval() > 0) {
   1.274 +        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
   1.275 +    }
   1.276 +
   1.277 +    if (SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &doublebuffer) == 0) {
   1.278 +        if (!doublebuffer) {
   1.279 +            renderer->info.flags |= SDL_RENDERER_SINGLEBUFFER;
   1.280 +        }
   1.281 +    }
   1.282 +	
   1.283 +	if (SDL_GL_ExtensionSupported("GL_OES_draw_texture")) {
   1.284 +		printf("using draw texture\n");
   1.285 +		data->GL_OES_draw_texture_supported = SDL_TRUE;
   1.286 +		data->useDrawTexture = SDL_TRUE;
   1.287 +	}
   1.288 +	else {
   1.289 +		printf("not using draw texture\n");
   1.290 +		data->GL_OES_draw_texture_supported = SDL_FALSE;
   1.291 +		data->useDrawTexture = SDL_FALSE;
   1.292 +	}	
   1.293 +
   1.294 +    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
   1.295 +    renderer->info.max_texture_width = value;
   1.296 +    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
   1.297 +    renderer->info.max_texture_height = value;
   1.298 +
   1.299 +    /* Set up parameters for rendering */
   1.300 +    data->blendMode = -1;
   1.301 +    data->scaleMode = -1;
   1.302 +    data->glDisable(GL_DEPTH_TEST);
   1.303 +    data->glDisable(GL_CULL_FACE);
   1.304 +	data->glEnable(GL_TEXTURE_2D);
   1.305 +    data->updateSize = SDL_TRUE;
   1.306 +
   1.307 +    return renderer;
   1.308 +}
   1.309 +
   1.310 +static int GL_ActivateRenderer(SDL_Renderer * renderer)
   1.311 +{
   1.312 +		
   1.313 +    GL_RenderData *data = (GL_RenderData *)renderer->driverdata;
   1.314 +    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
   1.315 +		
   1.316 +	if (data->context == NULL) {
   1.317 +		printf("Context is null, dude\n");
   1.318 +	}
   1.319 +		
   1.320 +    if (SDL_GL_MakeCurrent(window->id, data->context) < 0) {
   1.321 +        return -1;
   1.322 +    }
   1.323 +    if (data->updateSize) {
   1.324 +        data->glMatrixMode(GL_PROJECTION);
   1.325 +        data->glLoadIdentity();
   1.326 +        data->glMatrixMode(GL_MODELVIEW);
   1.327 +        data->glLoadIdentity();
   1.328 +        data->glViewport(0, 0, window->w, window->h);
   1.329 +        data->glOrthof(0.0, (GLfloat)window->w, (GLfloat)window->h, 0.0,
   1.330 +                      0.0, 1.0);
   1.331 +        data->updateSize = SDL_FALSE;
   1.332 +    }
   1.333 +    return 0;
   1.334 +}
   1.335 +
   1.336 +static int
   1.337 +GL_DisplayModeChanged(SDL_Renderer * renderer)
   1.338 +{
   1.339 +    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
   1.340 +
   1.341 +    data->updateSize = SDL_TRUE;
   1.342 +    return 0;
   1.343 +}
   1.344 +
   1.345 +static __inline__ int
   1.346 +power_of_2(int input)
   1.347 +{
   1.348 +    int value = 1;
   1.349 +
   1.350 +    while (value < input) {
   1.351 +        value <<= 1;
   1.352 +    }
   1.353 +    return value;
   1.354 +}
   1.355 +
   1.356 +static int
   1.357 +GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   1.358 +{
   1.359 +    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
   1.360 +    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
   1.361 +    GL_TextureData *data;
   1.362 +    GLint internalFormat;
   1.363 +    GLenum format, type;
   1.364 +    int texture_w, texture_h;
   1.365 +    GLenum result;
   1.366 +	switch (texture->format) {
   1.367 +		case SDL_PIXELFORMAT_INDEX1LSB:
   1.368 +		case SDL_PIXELFORMAT_INDEX1MSB:
   1.369 +		case SDL_PIXELFORMAT_INDEX8:
   1.370 +		case SDL_PIXELFORMAT_RGB332:
   1.371 +		case SDL_PIXELFORMAT_RGB444:
   1.372 +		case SDL_PIXELFORMAT_RGB555:
   1.373 +		case SDL_PIXELFORMAT_ARGB4444:
   1.374 +		case SDL_PIXELFORMAT_ARGB1555:
   1.375 +		case SDL_PIXELFORMAT_BGR24:
   1.376 +		case SDL_PIXELFORMAT_BGR888: /* can't convert 32 bit format to 24 */
   1.377 +		case SDL_PIXELFORMAT_RGB888:
   1.378 +		case SDL_PIXELFORMAT_RGBA8888:
   1.379 +		case SDL_PIXELFORMAT_ARGB2101010:
   1.380 +			SDL_SetError("Unsupported format");
   1.381 +			return -1;
   1.382 +		case SDL_PIXELFORMAT_ARGB8888:
   1.383 +		#ifdef GL_IMG_texture_format_BGRA8888
   1.384 +			internalFormat = GL_RGBA;
   1.385 +			format = GL_RGBA;
   1.386 +			type = GL_UNSIGNED_BYTE;
   1.387 +			break;
   1.388 +		#else
   1.389 +			SDL_SetError("Unsupported format");
   1.390 +			return -1;
   1.391 +		#endif
   1.392 +		case SDL_PIXELFORMAT_RGB24:
   1.393 +			internalFormat = GL_RGB;
   1.394 +			format = GL_RGB;
   1.395 +			type = GL_UNSIGNED_BYTE;
   1.396 +			break;
   1.397 +		case SDL_PIXELFORMAT_ABGR8888:
   1.398 +			internalFormat = GL_RGBA;
   1.399 +			format = GL_RGBA;
   1.400 +			type = GL_UNSIGNED_BYTE;			
   1.401 +			break;			
   1.402 +		/*
   1.403 +			These formats would be supported if SDL had the necessary pixel formats
   1.404 +		case SDL_PIXELFORMAT_BGR565:
   1.405 +			internalFormat = GL_RGB;
   1.406 +			format = GL_RGB;
   1.407 +			type = GL_UNSIGNED_SHORT_5_6_5;
   1.408 +			break;			
   1.409 +		case SDL_PIXELFORMAT_ABGR5551:
   1.410 +			internalFormat = GL_RGBA;
   1.411 +			format = GL_RGBA;
   1.412 +			type = GL_UNSIGNED_SHORT_5_5_5_1;
   1.413 +			break;
   1.414 +		case SDL_PIXELFORMAT_ABGR4444:
   1.415 +			internalFormat = GL_RGBA;
   1.416 +			format = GL_RGBA;
   1.417 +			type = GL_UNSIGNED_SHORT_4_4_4_4;
   1.418 +			break;
   1.419 +		*/
   1.420 +		default:
   1.421 +			SDL_SetError("Unsupported texture format");
   1.422 +			return -1;
   1.423 +    }
   1.424 +	data = (GL_TextureData *) SDL_calloc(1, sizeof(*data));
   1.425 +    if (!data) {
   1.426 +        SDL_OutOfMemory();
   1.427 +        return -1;
   1.428 +    }
   1.429 +
   1.430 +    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
   1.431 +        data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
   1.432 +        data->pixels = SDL_malloc(texture->h * data->pitch);
   1.433 +        if (!data->pixels) {
   1.434 +            SDL_OutOfMemory();
   1.435 +            SDL_free(data);
   1.436 +            return -1;
   1.437 +        }
   1.438 +    }
   1.439 +
   1.440 +    texture->driverdata = data;
   1.441 +
   1.442 +    renderdata->glGetError();
   1.443 +    renderdata->glGenTextures(1, &data->texture);
   1.444 +    
   1.445 +	data->type = GL_TEXTURE_2D;
   1.446 +	texture_w = power_of_2(texture->w);
   1.447 +	texture_h = power_of_2(texture->h);
   1.448 +	data->texw = (GLfloat) texture->w / texture_w;
   1.449 +	data->texh = (GLfloat) texture->h / texture_h;
   1.450 +	
   1.451 +    data->format = format;
   1.452 +    data->formattype = type;
   1.453 +    renderdata->glBindTexture(data->type, data->texture);
   1.454 +    renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
   1.455 +                                GL_NEAREST);
   1.456 +    renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
   1.457 +                                GL_NEAREST);
   1.458 +    renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
   1.459 +                                GL_CLAMP_TO_EDGE);
   1.460 +    renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
   1.461 +                                GL_CLAMP_TO_EDGE);
   1.462 +    
   1.463 +	renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
   1.464 +							 texture_h, 0, format, type, NULL);
   1.465 +
   1.466 +    result = renderdata->glGetError();
   1.467 +    if (result != GL_NO_ERROR) {
   1.468 +        GL_SetError("glTexImage2D()", result);
   1.469 +        return -1;
   1.470 +    }
   1.471 +    return 0;
   1.472 +}
   1.473 +
   1.474 +static int
   1.475 +GL_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
   1.476 +                      void **pixels, int *pitch)
   1.477 +{
   1.478 +    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
   1.479 +
   1.480 +    *pixels = data->pixels;
   1.481 +    *pitch = data->pitch;
   1.482 +    return 0;
   1.483 +}
   1.484 +
   1.485 +static int
   1.486 +GL_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   1.487 +                     const SDL_Color * colors, int firstcolor, int ncolors)
   1.488 +{
   1.489 +	SDL_SetError("OpenGL ES does not support paletted textures");
   1.490 +    return -1;
   1.491 +}
   1.492 +
   1.493 +static int
   1.494 +GL_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   1.495 +                     SDL_Color * colors, int firstcolor, int ncolors)
   1.496 +{
   1.497 +	SDL_SetError("OpenGL ES does not support paletted textures");
   1.498 +    return -1;
   1.499 +}
   1.500 +
   1.501 +static void
   1.502 +SetupTextureUpdate(GL_RenderData * renderdata, SDL_Texture * texture,
   1.503 +                   int pitch)
   1.504 +{
   1.505 +	
   1.506 +	
   1.507 +	GL_TextureData *data = (GL_TextureData *) texture->driverdata;
   1.508 +	renderdata->glBindTexture(data->type, data->texture);
   1.509 +    renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
   1.510 +}
   1.511 +
   1.512 +static int
   1.513 +GL_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
   1.514 +{
   1.515 +    return 0;
   1.516 +}
   1.517 +
   1.518 +static int
   1.519 +GL_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
   1.520 +{
   1.521 +    return 0;
   1.522 +}
   1.523 +
   1.524 +static int
   1.525 +GL_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
   1.526 +{
   1.527 +    switch (texture->blendMode) {
   1.528 +    case SDL_TEXTUREBLENDMODE_NONE:
   1.529 +    case SDL_TEXTUREBLENDMODE_MASK:
   1.530 +    case SDL_TEXTUREBLENDMODE_BLEND:
   1.531 +    case SDL_TEXTUREBLENDMODE_ADD:
   1.532 +    case SDL_TEXTUREBLENDMODE_MOD:
   1.533 +        return 0;
   1.534 +    default:
   1.535 +        SDL_Unsupported();
   1.536 +        texture->blendMode = SDL_TEXTUREBLENDMODE_NONE;
   1.537 +        return -1;
   1.538 +    }
   1.539 +}
   1.540 +
   1.541 +static int
   1.542 +GL_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture)
   1.543 +{
   1.544 +    switch (texture->scaleMode) {
   1.545 +    case SDL_TEXTURESCALEMODE_NONE:
   1.546 +    case SDL_TEXTURESCALEMODE_FAST:
   1.547 +    case SDL_TEXTURESCALEMODE_SLOW:
   1.548 +        return 0;
   1.549 +    case SDL_TEXTURESCALEMODE_BEST:
   1.550 +        SDL_Unsupported();
   1.551 +		texture->scaleMode = SDL_TEXTURESCALEMODE_SLOW;
   1.552 +        return -1;
   1.553 +    default:
   1.554 +        SDL_Unsupported();
   1.555 +        texture->scaleMode = SDL_TEXTURESCALEMODE_NONE;
   1.556 +        return -1;
   1.557 +    }
   1.558 +}
   1.559 +
   1.560 +static int
   1.561 +GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   1.562 +                 const SDL_Rect * rect, const void *pixels, int pitch)
   1.563 +{
   1.564 +    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
   1.565 +    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
   1.566 +    GLenum result;
   1.567 +
   1.568 +    SetupTextureUpdate(renderdata, texture, pitch);
   1.569 +	renderdata->glGetError();
   1.570 +	renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
   1.571 +                                rect->h, data->format, data->formattype,
   1.572 +                                pixels);
   1.573 +    result = renderdata->glGetError();
   1.574 +    if (result != GL_NO_ERROR) {
   1.575 +        GL_SetError("glTexSubImage2D()", result);
   1.576 +        return -1;
   1.577 +    }
   1.578 +    return 0;
   1.579 +}
   1.580 +
   1.581 +static int
   1.582 +GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   1.583 +               const SDL_Rect * rect, int markDirty, void **pixels,
   1.584 +               int *pitch)
   1.585 +{
   1.586 +    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
   1.587 +
   1.588 +    if (markDirty) {
   1.589 +        SDL_AddDirtyRect(&data->dirty, rect);
   1.590 +    }
   1.591 +
   1.592 +    *pixels =
   1.593 +        (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
   1.594 +                  rect->x * SDL_BYTESPERPIXEL(texture->format));
   1.595 +    *pitch = data->pitch;
   1.596 +    return 0;
   1.597 +}
   1.598 +
   1.599 +static void
   1.600 +GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   1.601 +{
   1.602 +}
   1.603 +
   1.604 +static void
   1.605 +GL_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture, int numrects,
   1.606 +                const SDL_Rect * rects)
   1.607 +{
   1.608 +    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
   1.609 +    int i;
   1.610 +
   1.611 +    for (i = 0; i < numrects; ++i) {
   1.612 +        SDL_AddDirtyRect(&data->dirty, &rects[i]);
   1.613 +    }
   1.614 +}
   1.615 +
   1.616 +static int
   1.617 +GL_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b, Uint8 a,
   1.618 +              const SDL_Rect * rect)
   1.619 +{
   1.620 + 	
   1.621 +	GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
   1.622 +    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
   1.623 +	
   1.624 +	/* set proper drawing color */
   1.625 +	GLfloat oldClearColor[4];
   1.626 +	
   1.627 +	data->glGetFloatv(GL_COLOR_CLEAR_VALUE, oldClearColor);
   1.628 +	
   1.629 +	data->glClearColor((GLclampf) r * inv255f, (GLclampf) g * inv255f,
   1.630 +			  (GLclampf) b * inv255f, (GLclampf) a * inv255f);
   1.631 +	
   1.632 +	data->glScissor(rect->x, window->h - rect->y - rect->h, rect->w, rect->h);
   1.633 +	data->glEnable(GL_SCISSOR_TEST);
   1.634 +	data->glClear(GL_COLOR_BUFFER_BIT);
   1.635 +	data->glDisable(GL_SCISSOR_TEST);
   1.636 +	
   1.637 +	/* reset clear color */
   1.638 +	data->glClearColor(oldClearColor[0], oldClearColor[1], oldClearColor[2], oldClearColor[2]);
   1.639 +	
   1.640 +	return 0;
   1.641 +}
   1.642 +
   1.643 +static int
   1.644 +GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
   1.645 +              const SDL_Rect * srcrect, const SDL_Rect * dstrect)
   1.646 +{
   1.647 +		
   1.648 +    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
   1.649 +    GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
   1.650 +    int minx, miny, maxx, maxy;
   1.651 +    GLfloat minu, maxu, minv, maxv;
   1.652 +	int i;
   1.653 +	void *temp_buffer; /* used for reformatting dirty rect pixels */
   1.654 +	void *temp_ptr;
   1.655 +	
   1.656 +    if (texturedata->dirty.list) {
   1.657 +        SDL_DirtyRect *dirty;
   1.658 +        void *pixels;
   1.659 +        int bpp = SDL_BYTESPERPIXEL(texture->format);
   1.660 +        int pitch = texturedata->pitch;
   1.661 +
   1.662 +        SetupTextureUpdate(data, texture, pitch);
   1.663 +		
   1.664 +        data->glBindTexture(texturedata->type, texturedata->texture);
   1.665 +        for (dirty = texturedata->dirty.list; dirty; dirty = dirty->next) {
   1.666 +            SDL_Rect *rect = &dirty->rect;
   1.667 +            pixels = (void *) ((Uint8 *) texturedata->pixels + rect->y * pitch + rect->x * bpp);
   1.668 +			/*	There is no GL_UNPACK_ROW_LENGTH in OpenGLES 
   1.669 +				we must do this reformatting ourselves(!)
   1.670 +			 
   1.671 +				maybe it'd be a good idea to keep a temp buffer around
   1.672 +				for this purpose rather than allocating it each time
   1.673 +			*/
   1.674 +			temp_buffer = SDL_malloc(rect->w * rect->h * bpp);
   1.675 +			temp_ptr = temp_buffer;
   1.676 +			for (i=0; i<rect->h; i++) {
   1.677 +				SDL_memcpy(temp_ptr, pixels, rect->w * bpp);
   1.678 +				temp_ptr += rect->w * bpp;
   1.679 +				pixels	 += pitch;
   1.680 +			}
   1.681 +			
   1.682 +            data->glTexSubImage2D(texturedata->type, 0, rect->x, rect->y,
   1.683 +                                  rect->w, rect->h, texturedata->format,
   1.684 +                                  texturedata->formattype, temp_buffer);
   1.685 +			
   1.686 +			SDL_free(temp_buffer);
   1.687 +			
   1.688 +        }
   1.689 +        SDL_ClearDirtyRects(&texturedata->dirty);
   1.690 +    }
   1.691 +	
   1.692 +    data->glBindTexture(texturedata->type, texturedata->texture);
   1.693 +	data->glEnable(GL_TEXTURE_2D);
   1.694 +	
   1.695 +    if (texture->modMode) {
   1.696 +        data->glColor4f((GLfloat) texture->r * inv255f,
   1.697 +                        (GLfloat) texture->g * inv255f,
   1.698 +                        (GLfloat) texture->b * inv255f,
   1.699 +                        (GLfloat) texture->a * inv255f);
   1.700 +    } else {
   1.701 +        data->glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
   1.702 +    }
   1.703 +
   1.704 +    //if (texture->blendMode != data->blendMode) {
   1.705 +        switch (texture->blendMode) {
   1.706 +        case SDL_TEXTUREBLENDMODE_NONE:
   1.707 +            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
   1.708 +            data->glDisable(GL_BLEND);
   1.709 +            break;
   1.710 +        case SDL_TEXTUREBLENDMODE_MASK:
   1.711 +        case SDL_TEXTUREBLENDMODE_BLEND:
   1.712 +            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   1.713 +            data->glEnable(GL_BLEND);
   1.714 +            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   1.715 +            break;
   1.716 +        case SDL_TEXTUREBLENDMODE_ADD:
   1.717 +            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   1.718 +            data->glEnable(GL_BLEND);
   1.719 +            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE);
   1.720 +            break;
   1.721 +        case SDL_TEXTUREBLENDMODE_MOD:
   1.722 +            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   1.723 +            data->glEnable(GL_BLEND);
   1.724 +            data->glBlendFunc(GL_ZERO, GL_SRC_COLOR);
   1.725 +            break;
   1.726 +        }
   1.727 +        data->blendMode = texture->blendMode;
   1.728 +   // }
   1.729 +
   1.730 +   // if (texture->scaleMode != data->scaleMode) {
   1.731 +        switch (texture->scaleMode) {
   1.732 +        case SDL_TEXTURESCALEMODE_NONE:
   1.733 +        case SDL_TEXTURESCALEMODE_FAST:
   1.734 +            data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
   1.735 +                                  GL_NEAREST);
   1.736 +            data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
   1.737 +                                  GL_NEAREST);
   1.738 +            break;
   1.739 +        case SDL_TEXTURESCALEMODE_SLOW:
   1.740 +        case SDL_TEXTURESCALEMODE_BEST:
   1.741 +            data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
   1.742 +                                  GL_LINEAR);
   1.743 +            data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
   1.744 +                                  GL_LINEAR);
   1.745 +            break;
   1.746 +        }
   1.747 +        data->scaleMode = texture->scaleMode;
   1.748 +    //}
   1.749 +
   1.750 +	if (data->GL_OES_draw_texture_supported && data->useDrawTexture) {
   1.751 +		/* this code is a little funny because the viewport is upside down vs SDL's coordinate system */
   1.752 +		SDL_Window *window = SDL_GetWindowFromID(renderer->window);
   1.753 +		GLint cropRect[4];
   1.754 +		cropRect[0] = srcrect->x;
   1.755 +		cropRect[1] = srcrect->y + srcrect->h;
   1.756 +		cropRect[2] = srcrect->w;
   1.757 +		cropRect[3] = -srcrect->h;
   1.758 +		data->glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect);
   1.759 +		data->glDrawTexiOES(dstrect->x, window->h - dstrect->y - dstrect->h, 0, dstrect->w, dstrect->h);
   1.760 +	}
   1.761 +	else {
   1.762 +		
   1.763 +		minx = dstrect->x;
   1.764 +		miny = dstrect->y;
   1.765 +		maxx = dstrect->x + dstrect->w;
   1.766 +		maxy = dstrect->y + dstrect->h;
   1.767 +		
   1.768 +		minu = (GLfloat) srcrect->x / texture->w;
   1.769 +		minu *= texturedata->texw;
   1.770 +		maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
   1.771 +		maxu *= texturedata->texw;
   1.772 +		minv = (GLfloat) srcrect->y / texture->h;
   1.773 +		minv *= texturedata->texh;
   1.774 +		maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
   1.775 +		maxv *= texturedata->texh;			
   1.776 +		
   1.777 +		GLshort vertices[8];
   1.778 +		GLfloat texCoords[8];
   1.779 +		
   1.780 +		vertices[0] = minx; vertices[1] = miny;
   1.781 +		vertices[2] = maxx; vertices[3] = miny;
   1.782 +		vertices[4]	= minx; vertices[5] = maxy;
   1.783 +		vertices[6] = maxx; vertices[7] = maxy;	
   1.784 +		
   1.785 +		texCoords[0] = minu; texCoords[1] = minv;
   1.786 +		texCoords[2] = maxu; texCoords[3] = minv;
   1.787 +		texCoords[4] = minu; texCoords[5] = maxv;
   1.788 +		texCoords[6] = maxu; texCoords[7] = maxv;	
   1.789 +		
   1.790 +		data->glVertexPointer(2, GL_SHORT, 0, vertices);
   1.791 +		data->glEnableClientState(GL_VERTEX_ARRAY);
   1.792 +		data->glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
   1.793 +		data->glEnableClientState(GL_TEXTURE_COORD_ARRAY);
   1.794 +		data->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
   1.795 +		
   1.796 +	}
   1.797 +		
   1.798 +    return 0;
   1.799 +}
   1.800 +
   1.801 +static void
   1.802 +GL_RenderPresent(SDL_Renderer * renderer)
   1.803 +{
   1.804 +    SDL_GL_SwapWindow(renderer->window);
   1.805 +}
   1.806 +
   1.807 +static void
   1.808 +GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   1.809 +{
   1.810 +    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
   1.811 +    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
   1.812 +
   1.813 +    if (!data) {
   1.814 +        return;
   1.815 +    }
   1.816 +    if (data->texture) {
   1.817 +		glDeleteTextures(1, &data->texture);
   1.818 +    }
   1.819 +    if (data->pixels) {
   1.820 +        SDL_free(data->pixels);
   1.821 +    }
   1.822 +    SDL_FreeDirtyRects(&data->dirty);
   1.823 +    SDL_free(data);
   1.824 +    texture->driverdata = NULL;
   1.825 +}
   1.826 +
   1.827 +static void
   1.828 +GL_DestroyRenderer(SDL_Renderer * renderer)
   1.829 +{
   1.830 +    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
   1.831 +
   1.832 +    if (data) {
   1.833 +        if (data->context) {
   1.834 +            /* SDL_GL_MakeCurrent(0, NULL); *//* doesn't do anything */
   1.835 +            SDL_GL_DeleteContext(data->context);
   1.836 +        }
   1.837 +        SDL_free(data);
   1.838 +    }
   1.839 +    SDL_free(renderer);
   1.840 +}
   1.841 +
   1.842 +#endif /* SDL_VIDEO_RENDER_OGL */
   1.843 +
   1.844 +/* vi: set ts=4 sw=4 expandtab: */
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/video/glesrenderer/SDL_renderer_gles.h	Thu Jul 17 23:31:42 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: */