static void GLES_RenderPresent(SDL_Renderer * renderer) { GLES_ActivateRenderer(renderer); SDL_GL_SwapWindow(renderer->window); }
static int GLES_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture) { GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata; GLES_TextureData *texturedata = NULL; GLenum status; GLES_ActivateRenderer(renderer); if (texture == NULL) { data->glBindFramebufferOES(GL_FRAMEBUFFER_OES, data->window_framebuffer); return 0; } texturedata = (GLES_TextureData *) texture->driverdata; data->glBindFramebufferOES(GL_FRAMEBUFFER_OES, texturedata->fbo->FBO); /* TODO: check if texture pixel format allows this operation */ data->glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, texturedata->type, texturedata->texture, 0); /* Check FBO status */ status = data->glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES); if (status != GL_FRAMEBUFFER_COMPLETE_OES) { SDL_SetError("glFramebufferTexture2DOES() failed"); return -1; } return 0; }
/* This is called if we need to invalidate all of the SDL OpenGL state */ static void GLES_ResetState(SDL_Renderer *renderer) { GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata; if (SDL_CurrentContext == data->context) { GLES_UpdateViewport(renderer); } else { GLES_ActivateRenderer(renderer); } data->current.color = 0; data->current.blendMode = -1; data->current.scaleMode = 0; data->current.tex_coords = SDL_FALSE; glDisable(GL_DEPTH_TEST); glDisable(GL_CULL_FACE); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glEnableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); }
static int GLES_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture, const SDL_Rect * rect, const void *pixels, int pitch) { GLES_RenderData *renderdata = (GLES_RenderData *) renderer->driverdata; GLES_TextureData *data = (GLES_TextureData *) texture->driverdata; Uint8 *blob = NULL; Uint8 *src; int srcPitch; int y; GLES_ActivateRenderer(renderer); /* Bail out if we're supposed to update an empty rectangle */ if (rect->w <= 0 || rect->h <= 0) return 0; /* Reformat the texture data into a tightly packed array */ srcPitch = rect->w * SDL_BYTESPERPIXEL(texture->format); src = (Uint8 *)pixels; if (pitch != srcPitch) { blob = (Uint8 *)SDL_malloc(srcPitch * rect->h); if (!blob) { return SDL_OutOfMemory(); } src = blob; for (y = 0; y < rect->h; ++y) { SDL_memcpy(src, pixels, srcPitch); src += srcPitch; pixels = (Uint8 *)pixels + pitch; } src = blob; } /* Create a texture subimage with the supplied data */ renderdata->glGetError(); renderdata->glEnable(data->type); renderdata->glBindTexture(data->type, data->texture); renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1); renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w, rect->h, data->format, data->formattype, src); if (blob) { SDL_free(blob); } if (renderdata->glGetError() != GL_NO_ERROR) { return SDL_SetError("Failed to update texture"); } return 0; }
static int GLES_UnbindTexture (SDL_Renderer * renderer, SDL_Texture *texture) { GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata; GLES_TextureData *texturedata = (GLES_TextureData *) texture->driverdata; GLES_ActivateRenderer(renderer); data->glDisable(texturedata->type); return 0; }
static int GLES_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect, Uint32 pixel_format, void * pixels, int pitch) { GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata; Uint32 temp_format = renderer->target ? renderer->target->format : SDL_PIXELFORMAT_ABGR8888; void *temp_pixels; int temp_pitch; Uint8 *src, *dst, *tmp; int w, h, length, rows; int status; GLES_ActivateRenderer(renderer); temp_pitch = rect->w * SDL_BYTESPERPIXEL(temp_format); temp_pixels = SDL_malloc(rect->h * temp_pitch); if (!temp_pixels) { return SDL_OutOfMemory(); } SDL_GetRendererOutputSize(renderer, &w, &h); data->glPixelStorei(GL_PACK_ALIGNMENT, 1); data->glReadPixels(rect->x, renderer->target ? rect->y : (h-rect->y)-rect->h, rect->w, rect->h, GL_RGBA, GL_UNSIGNED_BYTE, temp_pixels); /* Flip the rows to be top-down if necessary */ if (!renderer->target) { length = rect->w * SDL_BYTESPERPIXEL(temp_format); src = (Uint8*)temp_pixels + (rect->h-1)*temp_pitch; dst = (Uint8*)temp_pixels; tmp = SDL_stack_alloc(Uint8, length); rows = rect->h / 2; while (rows--) { SDL_memcpy(tmp, dst, length); SDL_memcpy(dst, src, length); SDL_memcpy(src, tmp, length); dst += temp_pitch; src -= temp_pitch; } SDL_stack_free(tmp); } status = SDL_ConvertPixels(rect->w, rect->h, temp_format, temp_pixels, temp_pitch, pixel_format, pixels, pitch); SDL_free(temp_pixels); return status; }
static int GLES_RenderClear(SDL_Renderer * renderer) { GLES_ActivateRenderer(renderer); glClearColor((GLfloat) renderer->r * inv255f, (GLfloat) renderer->g * inv255f, (GLfloat) renderer->b * inv255f, (GLfloat) renderer->a * inv255f); glClear(GL_COLOR_BUFFER_BIT); return 0; }
static int GLES_BindTexture (SDL_Renderer * renderer, SDL_Texture *texture, float *texw, float *texh) { GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata; GLES_TextureData *texturedata = (GLES_TextureData *) texture->driverdata; GLES_ActivateRenderer(renderer); data->glEnable(GL_TEXTURE_2D); data->glBindTexture(texturedata->type, texturedata->texture); if(texw) *texw = (float)texturedata->texw; if(texh) *texh = (float)texturedata->texh; return 0; }
static void GLES_SetDrawingState(SDL_Renderer * renderer) { GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata; GLES_ActivateRenderer(renderer); GLES_SetColor(data, (GLfloat) renderer->r, (GLfloat) renderer->g, (GLfloat) renderer->b, (GLfloat) renderer->a); GLES_SetBlendMode(data, renderer->blendMode); GLES_SetTexCoords(data, SDL_FALSE); }
static void GLES_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture) { GLES_RenderData *renderdata = (GLES_RenderData *) renderer->driverdata; GLES_TextureData *data = (GLES_TextureData *) texture->driverdata; GLES_ActivateRenderer(renderer); if (!data) { return; } if (data->texture) { renderdata->glDeleteTextures(1, &data->texture); } SDL_free(data->pixels); SDL_free(data); texture->driverdata = NULL; }
static int GLES_RenderClear(SDL_Renderer * renderer) { GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata; GLES_ActivateRenderer(renderer); data->glClearColor((GLfloat) renderer->r * inv255f, (GLfloat) renderer->g * inv255f, (GLfloat) renderer->b * inv255f, (GLfloat) renderer->a * inv255f); if (renderer->clipping_enabled) { data->glDisable(GL_SCISSOR_TEST); } data->glClear(GL_COLOR_BUFFER_BIT); if (renderer->clipping_enabled) { data->glEnable(GL_SCISSOR_TEST); } return 0; }
static int GLES_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture) { GLES_RenderData *renderdata = (GLES_RenderData *) renderer->driverdata; GLES_TextureData *data; GLint internalFormat; GLenum format, type; int texture_w, texture_h; GLenum scaleMode; GLenum result; GLES_ActivateRenderer(renderer); switch (texture->format) { case SDL_PIXELFORMAT_ABGR8888: internalFormat = GL_RGBA; format = GL_RGBA; type = GL_UNSIGNED_BYTE; break; default: return SDL_SetError("Texture format not supported"); } data = (GLES_TextureData *) SDL_calloc(1, sizeof(*data)); if (!data) { return SDL_OutOfMemory(); } if (texture->access == SDL_TEXTUREACCESS_STREAMING) { data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format); data->pixels = SDL_calloc(1, texture->h * data->pitch); if (!data->pixels) { SDL_free(data); return SDL_OutOfMemory(); } } if (texture->access == SDL_TEXTUREACCESS_TARGET) { if (!renderdata->GL_OES_framebuffer_object_supported) { SDL_free(data); return SDL_SetError("GL_OES_framebuffer_object not supported"); } data->fbo = GLES_GetFBO(renderer->driverdata, texture->w, texture->h); } else { data->fbo = NULL; } renderdata->glGetError(); renderdata->glEnable(GL_TEXTURE_2D); renderdata->glGenTextures(1, &data->texture); result = renderdata->glGetError(); if (result != GL_NO_ERROR) { SDL_free(data); return GLES_SetError("glGenTextures()", result); } data->type = GL_TEXTURE_2D; /* no NPOV textures allowed in OpenGL ES (yet) */ texture_w = power_of_2(texture->w); texture_h = power_of_2(texture->h); data->texw = (GLfloat) texture->w / texture_w; data->texh = (GLfloat) texture->h / texture_h; data->format = format; data->formattype = type; scaleMode = GetScaleQuality(); renderdata->glBindTexture(data->type, data->texture); renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER, scaleMode); renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER, scaleMode); renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w, texture_h, 0, format, type, NULL); renderdata->glDisable(GL_TEXTURE_2D); result = renderdata->glGetError(); if (result != GL_NO_ERROR) { SDL_free(data); return GLES_SetError("glTexImage2D()", result); } texture->driverdata = data; return 0; }
static int GLES_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture, const SDL_Rect * srcrect, const SDL_FRect * dstrect, const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip) { GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata; GLES_TextureData *texturedata = (GLES_TextureData *) texture->driverdata; GLfloat minx, miny, maxx, maxy; GLfloat minu, maxu, minv, maxv; GLfloat centerx, centery; GLfloat vertices[8]; GLfloat texCoords[8]; GLES_ActivateRenderer(renderer); data->glEnable(GL_TEXTURE_2D); data->glBindTexture(texturedata->type, texturedata->texture); if (texture->modMode) { GLES_SetColor(data, texture->r, texture->g, texture->b, texture->a); } else { GLES_SetColor(data, 255, 255, 255, 255); } GLES_SetBlendMode(data, texture->blendMode); GLES_SetTexCoords(data, SDL_TRUE); centerx = center->x; centery = center->y; /* Rotate and translate */ data->glPushMatrix(); data->glTranslatef(dstrect->x + centerx, dstrect->y + centery, 0.0f); data->glRotatef((GLfloat)angle, 0.0f, 0.0f, 1.0f); if (flip & SDL_FLIP_HORIZONTAL) { minx = dstrect->w - centerx; maxx = -centerx; } else { minx = -centerx; maxx = dstrect->w - centerx; } if (flip & SDL_FLIP_VERTICAL) { miny = dstrect->h - centery; maxy = -centery; } else { miny = -centery; maxy = dstrect->h - centery; } minu = (GLfloat) srcrect->x / texture->w; minu *= texturedata->texw; maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w; maxu *= texturedata->texw; minv = (GLfloat) srcrect->y / texture->h; minv *= texturedata->texh; maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h; maxv *= texturedata->texh; vertices[0] = minx; vertices[1] = miny; vertices[2] = maxx; vertices[3] = miny; vertices[4] = minx; vertices[5] = maxy; vertices[6] = maxx; vertices[7] = maxy; texCoords[0] = minu; texCoords[1] = minv; texCoords[2] = maxu; texCoords[3] = minv; texCoords[4] = minu; texCoords[5] = maxv; texCoords[6] = maxu; texCoords[7] = maxv; data->glVertexPointer(2, GL_FLOAT, 0, vertices); data->glTexCoordPointer(2, GL_FLOAT, 0, texCoords); data->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); data->glPopMatrix(); data->glDisable(GL_TEXTURE_2D); return 0; }
static int GLES_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture, const SDL_Rect * srcrect, const SDL_FRect * dstrect) { GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata; GLES_TextureData *texturedata = (GLES_TextureData *) texture->driverdata; GLfloat minx, miny, maxx, maxy; GLfloat minu, maxu, minv, maxv; GLfloat vertices[8]; GLfloat texCoords[8]; GLES_ActivateRenderer(renderer); data->glEnable(GL_TEXTURE_2D); data->glBindTexture(texturedata->type, texturedata->texture); if (texture->modMode) { GLES_SetColor(data, texture->r, texture->g, texture->b, texture->a); } else { GLES_SetColor(data, 255, 255, 255, 255); } GLES_SetBlendMode(data, texture->blendMode); GLES_SetTexCoords(data, SDL_TRUE); minx = dstrect->x; miny = dstrect->y; maxx = dstrect->x + dstrect->w; maxy = dstrect->y + dstrect->h; minu = (GLfloat) srcrect->x / texture->w; minu *= texturedata->texw; maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w; maxu *= texturedata->texw; minv = (GLfloat) srcrect->y / texture->h; minv *= texturedata->texh; maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h; maxv *= texturedata->texh; vertices[0] = minx; vertices[1] = miny; vertices[2] = maxx; vertices[3] = miny; vertices[4] = minx; vertices[5] = maxy; vertices[6] = maxx; vertices[7] = maxy; texCoords[0] = minu; texCoords[1] = minv; texCoords[2] = maxu; texCoords[3] = minv; texCoords[4] = minu; texCoords[5] = maxv; texCoords[6] = maxu; texCoords[7] = maxv; data->glVertexPointer(2, GL_FLOAT, 0, vertices); data->glTexCoordPointer(2, GL_FLOAT, 0, texCoords); data->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); data->glDisable(GL_TEXTURE_2D); return 0; }
static int GLES_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture, const SDL_Rect * srcrect, const SDL_Rect * dstrect) { GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata; GLES_TextureData *texturedata = (GLES_TextureData *) texture->driverdata; int minx, miny, maxx, maxy; GLfloat minu, maxu, minv, maxv; GLES_ActivateRenderer(renderer); data->glEnable(GL_TEXTURE_2D); data->glBindTexture(texturedata->type, texturedata->texture); if (texture->modMode) { GLES_SetColor(data, texture->r, texture->g, texture->b, texture->a); } else { GLES_SetColor(data, 255, 255, 255, 255); } GLES_SetBlendMode(data, texture->blendMode); GLES_SetTexCoords(data, SDL_TRUE); if (data->GL_OES_draw_texture_supported && data->useDrawTexture) { /* this code is a little funny because the viewport is upside down vs SDL's coordinate system */ GLint cropRect[4]; int w, h; SDL_Window *window = renderer->window; SDL_GetWindowSize(window, &w, &h); if (renderer->target) { cropRect[0] = srcrect->x; cropRect[1] = srcrect->y; cropRect[2] = srcrect->w; cropRect[3] = srcrect->h; data->glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect); data->glDrawTexiOES(renderer->viewport.x + dstrect->x, renderer->viewport.y + dstrect->y, 0, dstrect->w, dstrect->h); } else { cropRect[0] = srcrect->x; cropRect[1] = srcrect->y + srcrect->h; cropRect[2] = srcrect->w; cropRect[3] = -srcrect->h; data->glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect); data->glDrawTexiOES(renderer->viewport.x + dstrect->x, h - (renderer->viewport.y + dstrect->y) - dstrect->h, 0, dstrect->w, dstrect->h); } } else { minx = dstrect->x; miny = dstrect->y; maxx = dstrect->x + dstrect->w; maxy = dstrect->y + dstrect->h; minu = (GLfloat) srcrect->x / texture->w; minu *= texturedata->texw; maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w; maxu *= texturedata->texw; minv = (GLfloat) srcrect->y / texture->h; minv *= texturedata->texh; maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h; maxv *= texturedata->texh; GLshort vertices[8]; GLfloat texCoords[8]; vertices[0] = minx; vertices[1] = miny; vertices[2] = maxx; vertices[3] = miny; vertices[4] = minx; vertices[5] = maxy; vertices[6] = maxx; vertices[7] = maxy; texCoords[0] = minu; texCoords[1] = minv; texCoords[2] = maxu; texCoords[3] = minv; texCoords[4] = minu; texCoords[5] = maxv; texCoords[6] = maxu; texCoords[7] = maxv; data->glVertexPointer(2, GL_SHORT, 0, vertices); data->glTexCoordPointer(2, GL_FLOAT, 0, texCoords); data->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); } data->glDisable(GL_TEXTURE_2D); return 0; }