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