Added SDL_RenderSetScale() and SDL_RenderGetScale()
This commit is contained in:
parent
0b35b7b0bf
commit
f366b098d4
10 changed files with 603 additions and 353 deletions
|
@ -56,16 +56,16 @@ static int SW_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture);
|
|||
static int SW_UpdateViewport(SDL_Renderer * renderer);
|
||||
static int SW_RenderClear(SDL_Renderer * renderer);
|
||||
static int SW_RenderDrawPoints(SDL_Renderer * renderer,
|
||||
const SDL_Point * points, int count);
|
||||
const SDL_FPoint * points, int count);
|
||||
static int SW_RenderDrawLines(SDL_Renderer * renderer,
|
||||
const SDL_Point * points, int count);
|
||||
const SDL_FPoint * points, int count);
|
||||
static int SW_RenderFillRects(SDL_Renderer * renderer,
|
||||
const SDL_Rect * rects, int count);
|
||||
const SDL_FRect * rects, int count);
|
||||
static int SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
|
||||
const SDL_Rect * srcrect, const SDL_Rect * dstrect);
|
||||
const SDL_Rect * srcrect, const SDL_FRect * dstrect);
|
||||
static int SW_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
|
||||
const SDL_Rect * srcrect, const SDL_Rect * dstrect,
|
||||
const double angle, const SDL_Point * center, const SDL_RendererFlip flip);
|
||||
const SDL_Rect * srcrect, const SDL_FRect * dstrect,
|
||||
const double angle, const SDL_FPoint * center, const SDL_RendererFlip flip);
|
||||
static int SW_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
|
||||
Uint32 format, void * pixels, int pitch);
|
||||
static void SW_RenderPresent(SDL_Renderer * renderer);
|
||||
|
@ -344,28 +344,35 @@ SW_RenderClear(SDL_Renderer * renderer)
|
|||
}
|
||||
|
||||
static int
|
||||
SW_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
|
||||
SW_RenderDrawPoints(SDL_Renderer * renderer, const SDL_FPoint * points,
|
||||
int count)
|
||||
{
|
||||
SDL_Surface *surface = SW_ActivateRenderer(renderer);
|
||||
SDL_Point *temp = NULL;
|
||||
int status;
|
||||
SDL_Point *final_points;
|
||||
int i, status;
|
||||
|
||||
if (!surface) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
final_points = SDL_stack_alloc(SDL_Point, count);
|
||||
if (!final_points) {
|
||||
SDL_OutOfMemory();
|
||||
return -1;
|
||||
}
|
||||
if (renderer->viewport.x || renderer->viewport.y) {
|
||||
int i;
|
||||
int x = renderer->viewport.x;
|
||||
int y = renderer->viewport.y;
|
||||
float x = renderer->viewport.x * renderer->scale.x;
|
||||
float y = renderer->viewport.y * renderer->scale.y;
|
||||
|
||||
temp = SDL_stack_alloc(SDL_Point, count);
|
||||
for (i = 0; i < count; ++i) {
|
||||
temp[i].x = x + points[i].x;
|
||||
temp[i].y = y + points[i].x;
|
||||
final_points[i].x = (int)(x + points[i].x);
|
||||
final_points[i].y = (int)(y + points[i].y);
|
||||
}
|
||||
} else {
|
||||
for (i = 0; i < count; ++i) {
|
||||
final_points[i].x = (int)points[i].x;
|
||||
final_points[i].y = (int)points[i].y;
|
||||
}
|
||||
points = temp;
|
||||
}
|
||||
|
||||
/* Draw the points! */
|
||||
|
@ -374,43 +381,48 @@ SW_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
|
|||
renderer->r, renderer->g, renderer->b,
|
||||
renderer->a);
|
||||
|
||||
status = SDL_DrawPoints(surface, points, count, color);
|
||||
status = SDL_DrawPoints(surface, final_points, count, color);
|
||||
} else {
|
||||
status = SDL_BlendPoints(surface, points, count,
|
||||
status = SDL_BlendPoints(surface, final_points, count,
|
||||
renderer->blendMode,
|
||||
renderer->r, renderer->g, renderer->b,
|
||||
renderer->a);
|
||||
}
|
||||
SDL_stack_free(final_points);
|
||||
|
||||
if (temp) {
|
||||
SDL_stack_free(temp);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
static int
|
||||
SW_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
|
||||
SW_RenderDrawLines(SDL_Renderer * renderer, const SDL_FPoint * points,
|
||||
int count)
|
||||
{
|
||||
SDL_Surface *surface = SW_ActivateRenderer(renderer);
|
||||
SDL_Point *temp = NULL;
|
||||
int status;
|
||||
SDL_Point *final_points;
|
||||
int i, status;
|
||||
|
||||
if (!surface) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
final_points = SDL_stack_alloc(SDL_Point, count);
|
||||
if (!final_points) {
|
||||
SDL_OutOfMemory();
|
||||
return -1;
|
||||
}
|
||||
if (renderer->viewport.x || renderer->viewport.y) {
|
||||
int i;
|
||||
int x = renderer->viewport.x;
|
||||
int y = renderer->viewport.y;
|
||||
float x = renderer->viewport.x * renderer->scale.x;
|
||||
float y = renderer->viewport.y * renderer->scale.y;
|
||||
|
||||
temp = SDL_stack_alloc(SDL_Point, count);
|
||||
for (i = 0; i < count; ++i) {
|
||||
temp[i].x = x + points[i].x;
|
||||
temp[i].y = y + points[i].y;
|
||||
final_points[i].x = (int)(x + points[i].x);
|
||||
final_points[i].y = (int)(y + points[i].y);
|
||||
}
|
||||
} else {
|
||||
for (i = 0; i < count; ++i) {
|
||||
final_points[i].x = (int)points[i].x;
|
||||
final_points[i].y = (int)points[i].y;
|
||||
}
|
||||
points = temp;
|
||||
}
|
||||
|
||||
/* Draw the lines! */
|
||||
|
@ -419,80 +431,91 @@ SW_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
|
|||
renderer->r, renderer->g, renderer->b,
|
||||
renderer->a);
|
||||
|
||||
status = SDL_DrawLines(surface, points, count, color);
|
||||
status = SDL_DrawLines(surface, final_points, count, color);
|
||||
} else {
|
||||
status = SDL_BlendLines(surface, points, count,
|
||||
status = SDL_BlendLines(surface, final_points, count,
|
||||
renderer->blendMode,
|
||||
renderer->r, renderer->g, renderer->b,
|
||||
renderer->a);
|
||||
}
|
||||
SDL_stack_free(final_points);
|
||||
|
||||
if (temp) {
|
||||
SDL_stack_free(temp);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
static int
|
||||
SW_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect * rects, int count)
|
||||
SW_RenderFillRects(SDL_Renderer * renderer, const SDL_FRect * rects, int count)
|
||||
{
|
||||
SDL_Surface *surface = SW_ActivateRenderer(renderer);
|
||||
SDL_Rect *temp = NULL;
|
||||
int status;
|
||||
SDL_Rect *final_rects;
|
||||
int i, status;
|
||||
|
||||
if (!surface) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
final_rects = SDL_stack_alloc(SDL_Rect, count);
|
||||
if (!final_rects) {
|
||||
SDL_OutOfMemory();
|
||||
return -1;
|
||||
}
|
||||
if (renderer->viewport.x || renderer->viewport.y) {
|
||||
int i;
|
||||
int x = renderer->viewport.x;
|
||||
int y = renderer->viewport.y;
|
||||
float x = renderer->viewport.x * renderer->scale.x;
|
||||
float y = renderer->viewport.y * renderer->scale.y;
|
||||
|
||||
temp = SDL_stack_alloc(SDL_Rect, count);
|
||||
for (i = 0; i < count; ++i) {
|
||||
temp[i].x = x + rects[i].x;
|
||||
temp[i].y = y + rects[i].y;
|
||||
temp[i].w = rects[i].w;
|
||||
temp[i].h = rects[i].h;
|
||||
final_rects[i].x = (int)(x + rects[i].x);
|
||||
final_rects[i].y = (int)(y + rects[i].y);
|
||||
final_rects[i].w = SDL_max((int)rects[i].w, 1);
|
||||
final_rects[i].h = SDL_max((int)rects[i].h, 1);
|
||||
}
|
||||
} else {
|
||||
for (i = 0; i < count; ++i) {
|
||||
final_rects[i].x = (int)rects[i].x;
|
||||
final_rects[i].y = (int)rects[i].y;
|
||||
final_rects[i].w = SDL_max((int)rects[i].w, 1);
|
||||
final_rects[i].h = SDL_max((int)rects[i].h, 1);
|
||||
}
|
||||
rects = temp;
|
||||
}
|
||||
|
||||
if (renderer->blendMode == SDL_BLENDMODE_NONE) {
|
||||
Uint32 color = SDL_MapRGBA(surface->format,
|
||||
renderer->r, renderer->g, renderer->b,
|
||||
renderer->a);
|
||||
status = SDL_FillRects(surface, rects, count, color);
|
||||
status = SDL_FillRects(surface, final_rects, count, color);
|
||||
} else {
|
||||
status = SDL_BlendFillRects(surface, rects, count,
|
||||
status = SDL_BlendFillRects(surface, final_rects, count,
|
||||
renderer->blendMode,
|
||||
renderer->r, renderer->g, renderer->b,
|
||||
renderer->a);
|
||||
}
|
||||
SDL_stack_free(final_rects);
|
||||
|
||||
if (temp) {
|
||||
SDL_stack_free(temp);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
static int
|
||||
SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
|
||||
const SDL_Rect * srcrect, const SDL_Rect * dstrect)
|
||||
const SDL_Rect * srcrect, const SDL_FRect * dstrect)
|
||||
{
|
||||
SDL_Surface *surface = SW_ActivateRenderer(renderer);
|
||||
SDL_Surface *src = (SDL_Surface *) texture->driverdata;
|
||||
SDL_Rect final_rect = *dstrect;
|
||||
SDL_Rect final_rect;
|
||||
|
||||
if (!surface) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (renderer->viewport.x || renderer->viewport.y) {
|
||||
final_rect.x += renderer->viewport.x;
|
||||
final_rect.y += renderer->viewport.y;
|
||||
final_rect.x = (int)((renderer->viewport.x * renderer->scale.x) + dstrect->x);
|
||||
final_rect.y = (int)((renderer->viewport.y * renderer->scale.y) + dstrect->y);
|
||||
} else {
|
||||
final_rect.x = (int)dstrect->x;
|
||||
final_rect.y = (int)dstrect->y;
|
||||
}
|
||||
final_rect.w = (int)dstrect->w;
|
||||
final_rect.h = (int)dstrect->h;
|
||||
|
||||
if ( srcrect->w == final_rect.w && srcrect->h == final_rect.h ) {
|
||||
return SDL_BlitSurface(src, srcrect, surface, &final_rect);
|
||||
} else {
|
||||
|
@ -514,12 +537,12 @@ GetScaleQuality(void)
|
|||
|
||||
static int
|
||||
SW_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
|
||||
const SDL_Rect * srcrect, const SDL_Rect * dstrect,
|
||||
const double angle, const SDL_Point * center, const SDL_RendererFlip flip)
|
||||
const SDL_Rect * srcrect, const SDL_FRect * dstrect,
|
||||
const double angle, const SDL_FPoint * center, const SDL_RendererFlip flip)
|
||||
{
|
||||
SDL_Surface *surface = SW_ActivateRenderer(renderer);
|
||||
SDL_Surface *src = (SDL_Surface *) texture->driverdata;
|
||||
SDL_Rect final_rect = *dstrect, tmp_rect;
|
||||
SDL_Rect final_rect, tmp_rect;
|
||||
SDL_Surface *surface_rotated, *surface_scaled;
|
||||
Uint32 colorkey;
|
||||
int retval, dstwidth, dstheight, abscenterx, abscentery;
|
||||
|
@ -530,27 +553,33 @@ SW_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
|
|||
}
|
||||
|
||||
if (renderer->viewport.x || renderer->viewport.y) {
|
||||
final_rect.x += renderer->viewport.x;
|
||||
final_rect.y += renderer->viewport.y;
|
||||
final_rect.x = (int)((renderer->viewport.x * renderer->scale.x) + dstrect->x);
|
||||
final_rect.y = (int)((renderer->viewport.y * renderer->scale.y) + dstrect->y);
|
||||
} else {
|
||||
final_rect.x = (int)dstrect->x;
|
||||
final_rect.y = (int)dstrect->y;
|
||||
}
|
||||
final_rect.w = (int)dstrect->w;
|
||||
final_rect.h = (int)dstrect->h;
|
||||
|
||||
surface_scaled = SDL_CreateRGBSurface(SDL_SWSURFACE, final_rect.w, final_rect.h, src->format->BitsPerPixel,
|
||||
src->format->Rmask, src->format->Gmask,
|
||||
src->format->Bmask, src->format->Amask );
|
||||
SDL_GetColorKey(src, &colorkey);
|
||||
SDL_SetColorKey(surface_scaled, SDL_TRUE, colorkey);
|
||||
tmp_rect = final_rect;
|
||||
tmp_rect.x = 0;
|
||||
tmp_rect.y = 0;
|
||||
if (surface_scaled) {
|
||||
SDL_GetColorKey(src, &colorkey);
|
||||
SDL_SetColorKey(surface_scaled, SDL_TRUE, colorkey);
|
||||
tmp_rect = final_rect;
|
||||
tmp_rect.x = 0;
|
||||
tmp_rect.y = 0;
|
||||
|
||||
retval = SDL_BlitScaled(src, srcrect, surface_scaled, &tmp_rect);
|
||||
if (!retval) {
|
||||
_rotozoomSurfaceSizeTrig(tmp_rect.w, tmp_rect.h, -angle, &dstwidth, &dstheight, &cangle, &sangle);
|
||||
surface_rotated = _rotateSurface(surface_scaled, -angle, dstwidth/2, dstheight/2, GetScaleQuality(), flip & SDL_FLIP_HORIZONTAL, flip & SDL_FLIP_VERTICAL, dstwidth, dstheight, cangle, sangle);
|
||||
if(surface_rotated) {
|
||||
/* Find out where the new origin is by rotating the four final_rect points around the center and then taking the extremes */
|
||||
abscenterx = final_rect.x + center->x;
|
||||
abscentery = final_rect.y + center->y;
|
||||
abscenterx = final_rect.x + (int)center->x;
|
||||
abscentery = final_rect.y + (int)center->y;
|
||||
/* Compensate the angle inversion to match the behaviour of the other backends */
|
||||
sangle = -sangle;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue