mirror of
https://github.com/TorqueGameEngines/Torque3D.git
synced 2026-07-12 15:14:35 +00:00
update sdl to https://github.com/libsdl-org/SDL 22March 2022
This commit is contained in:
parent
ee4253c982
commit
2614274639
1225 changed files with 148950 additions and 51674 deletions
|
|
@ -1,6 +1,6 @@
|
|||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
|
||||
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
|
|
@ -220,7 +220,7 @@ SDL_BlendFillRect(SDL_Surface * dst, const SDL_Rect * rect,
|
|||
SDL_Rect clipped;
|
||||
|
||||
if (!dst) {
|
||||
return SDL_SetError("Passed NULL destination surface");
|
||||
return SDL_InvalidParamError("SDL_BlendFillRect(): dst");
|
||||
}
|
||||
|
||||
/* This function doesn't work on surfaces < 8 bpp */
|
||||
|
|
@ -291,7 +291,7 @@ SDL_BlendFillRects(SDL_Surface * dst, const SDL_Rect * rects, int count,
|
|||
int status = 0;
|
||||
|
||||
if (!dst) {
|
||||
return SDL_SetError("Passed NULL destination surface");
|
||||
return SDL_InvalidParamError("SDL_BlendFillRects(): dst");
|
||||
}
|
||||
|
||||
/* This function doesn't work on surfaces < 8 bpp */
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
|
||||
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
|
||||
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
|
|
@ -809,7 +809,7 @@ SDL_BlendLine(SDL_Surface * dst, int x1, int y1, int x2, int y2,
|
|||
BlendLineFunc func;
|
||||
|
||||
if (!dst) {
|
||||
return SDL_SetError("SDL_BlendLine(): Passed NULL destination surface");
|
||||
return SDL_InvalidParamError("SDL_BlendLine(): dst");
|
||||
}
|
||||
|
||||
func = SDL_CalculateBlendLineFunc(dst->format);
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
|
||||
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
|
||||
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
|
|
@ -218,7 +218,7 @@ SDL_BlendPoint(SDL_Surface * dst, int x, int y, SDL_BlendMode blendMode, Uint8 r
|
|||
Uint8 g, Uint8 b, Uint8 a)
|
||||
{
|
||||
if (!dst) {
|
||||
return SDL_SetError("Passed NULL destination surface");
|
||||
return SDL_InvalidParamError("SDL_BlendPoint(): dst");
|
||||
}
|
||||
|
||||
/* This function doesn't work on surfaces < 8 bpp */
|
||||
|
|
@ -287,7 +287,7 @@ SDL_BlendPoints(SDL_Surface * dst, const SDL_Point * points, int count,
|
|||
int status = 0;
|
||||
|
||||
if (!dst) {
|
||||
return SDL_SetError("Passed NULL destination surface");
|
||||
return SDL_InvalidParamError("SDL_BlendPoints(): dst");
|
||||
}
|
||||
|
||||
/* This function doesn't work on surfaces < 8 bpp */
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
|
||||
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
|
||||
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
|
|
@ -618,10 +618,10 @@ do { \
|
|||
while (height--) { \
|
||||
{ int n = (width+3)/4; \
|
||||
switch (width & 3) { \
|
||||
case 0: do { op; pixel++; /* fallthrough */ \
|
||||
case 3: op; pixel++; /* fallthrough */ \
|
||||
case 2: op; pixel++; /* fallthrough */ \
|
||||
case 1: op; pixel++; /* fallthrough */ \
|
||||
case 0: do { op; pixel++; SDL_FALLTHROUGH; \
|
||||
case 3: op; pixel++; SDL_FALLTHROUGH; \
|
||||
case 2: op; pixel++; SDL_FALLTHROUGH; \
|
||||
case 1: op; pixel++; \
|
||||
} while ( --n > 0 ); \
|
||||
} \
|
||||
} \
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
|
||||
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
|
|
@ -144,7 +144,7 @@ SDL_DrawLine(SDL_Surface * dst, int x1, int y1, int x2, int y2, Uint32 color)
|
|||
DrawLineFunc func;
|
||||
|
||||
if (!dst) {
|
||||
return SDL_SetError("SDL_DrawLine(): Passed NULL destination surface");
|
||||
return SDL_InvalidParamError("SDL_DrawLine(): dst");
|
||||
}
|
||||
|
||||
func = SDL_CalculateDrawLineFunc(dst->format);
|
||||
|
|
@ -173,7 +173,7 @@ SDL_DrawLines(SDL_Surface * dst, const SDL_Point * points, int count,
|
|||
DrawLineFunc func;
|
||||
|
||||
if (!dst) {
|
||||
return SDL_SetError("SDL_DrawLines(): Passed NULL destination surface");
|
||||
return SDL_InvalidParamError("SDL_DrawLines(): dst");
|
||||
}
|
||||
|
||||
func = SDL_CalculateDrawLineFunc(dst->format);
|
||||
|
|
@ -193,8 +193,8 @@ SDL_DrawLines(SDL_Surface * dst, const SDL_Point * points, int count,
|
|||
continue;
|
||||
}
|
||||
|
||||
/* Draw the end if it was clipped */
|
||||
draw_end = (x2 != points[i].x || y2 != points[i].y);
|
||||
/* Draw the end if the whole line is a single point or it was clipped */
|
||||
draw_end = ((x1 == x2) && (y1 == y2)) || (x2 != points[i].x || y2 != points[i].y);
|
||||
|
||||
func(dst, x1, y1, x2, y2, color, draw_end);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
|
||||
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
|
||||
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
|
|
@ -30,7 +30,7 @@ int
|
|||
SDL_DrawPoint(SDL_Surface * dst, int x, int y, Uint32 color)
|
||||
{
|
||||
if (!dst) {
|
||||
return SDL_SetError("Passed NULL destination surface");
|
||||
return SDL_InvalidParamError("SDL_DrawPoint(): dst");
|
||||
}
|
||||
|
||||
/* This function doesn't work on surfaces < 8 bpp */
|
||||
|
|
@ -71,7 +71,7 @@ SDL_DrawPoints(SDL_Surface * dst, const SDL_Point * points, int count,
|
|||
int x, y;
|
||||
|
||||
if (!dst) {
|
||||
return SDL_SetError("Passed NULL destination surface");
|
||||
return SDL_InvalidParamError("SDL_DrawPoints(): dst");
|
||||
}
|
||||
|
||||
/* This function doesn't work on surfaces < 8 bpp */
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
|
||||
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
|
||||
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
|
|
@ -33,6 +33,7 @@
|
|||
#include "SDL_drawline.h"
|
||||
#include "SDL_drawpoint.h"
|
||||
#include "SDL_rotate.h"
|
||||
#include "SDL_triangle.h"
|
||||
|
||||
/* SDL surface based renderer implementation */
|
||||
|
||||
|
|
@ -98,8 +99,7 @@ SW_GetOutputSize(SDL_Renderer * renderer, int *w, int *h)
|
|||
return 0;
|
||||
}
|
||||
|
||||
SDL_SetError("Software renderer doesn't have an output surface");
|
||||
return -1;
|
||||
return SDL_SetError("Software renderer doesn't have an output surface");
|
||||
}
|
||||
|
||||
static int
|
||||
|
|
@ -116,9 +116,8 @@ SW_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
|
|||
texture->driverdata =
|
||||
SDL_CreateRGBSurface(0, texture->w, texture->h, bpp, Rmask, Gmask,
|
||||
Bmask, Amask);
|
||||
SDL_SetSurfaceColorMod(texture->driverdata, texture->r, texture->g,
|
||||
texture->b);
|
||||
SDL_SetSurfaceAlphaMod(texture->driverdata, texture->a);
|
||||
SDL_SetSurfaceColorMod(texture->driverdata, texture->color.r, texture->color.g, texture->color.b);
|
||||
SDL_SetSurfaceAlphaMod(texture->driverdata, texture->color.a);
|
||||
SDL_SetSurfaceBlendMode(texture->driverdata, texture->blendMode);
|
||||
|
||||
/* Only RLE encode textures without an alpha channel since the RLE coder
|
||||
|
|
@ -214,18 +213,9 @@ SW_QueueDrawPoints(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FP
|
|||
|
||||
cmd->data.draw.count = count;
|
||||
|
||||
if (renderer->viewport.x || renderer->viewport.y) {
|
||||
const int x = renderer->viewport.x;
|
||||
const int y = renderer->viewport.y;
|
||||
for (i = 0; i < count; i++, verts++, points++) {
|
||||
verts->x = (int)(x + points->x);
|
||||
verts->y = (int)(y + points->y);
|
||||
}
|
||||
} else {
|
||||
for (i = 0; i < count; i++, verts++, points++) {
|
||||
verts->x = (int)points->x;
|
||||
verts->y = (int)points->y;
|
||||
}
|
||||
for (i = 0; i < count; i++, verts++, points++) {
|
||||
verts->x = (int)points->x;
|
||||
verts->y = (int)points->y;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
@ -243,23 +233,11 @@ SW_QueueFillRects(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FRe
|
|||
|
||||
cmd->data.draw.count = count;
|
||||
|
||||
if (renderer->viewport.x || renderer->viewport.y) {
|
||||
const int x = renderer->viewport.x;
|
||||
const int y = renderer->viewport.y;
|
||||
|
||||
for (i = 0; i < count; i++, verts++, rects++) {
|
||||
verts->x = (int)(x + rects->x);
|
||||
verts->y = (int)(y + rects->y);
|
||||
verts->w = SDL_max((int)rects->w, 1);
|
||||
verts->h = SDL_max((int)rects->h, 1);
|
||||
}
|
||||
} else {
|
||||
for (i = 0; i < count; i++, verts++, rects++) {
|
||||
verts->x = (int)rects->x;
|
||||
verts->y = (int)rects->y;
|
||||
verts->w = SDL_max((int)rects->w, 1);
|
||||
verts->h = SDL_max((int)rects->h, 1);
|
||||
}
|
||||
for (i = 0; i < count; i++, verts++, rects++) {
|
||||
verts->x = (int)rects->x;
|
||||
verts->y = (int)rects->y;
|
||||
verts->w = SDL_max((int)rects->w, 1);
|
||||
verts->h = SDL_max((int)rects->h, 1);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
@ -280,13 +258,8 @@ SW_QueueCopy(SDL_Renderer * renderer, SDL_RenderCommand *cmd, SDL_Texture * text
|
|||
SDL_memcpy(verts, srcrect, sizeof (SDL_Rect));
|
||||
verts++;
|
||||
|
||||
if (renderer->viewport.x || renderer->viewport.y) {
|
||||
verts->x = (int)(renderer->viewport.x + dstrect->x);
|
||||
verts->y = (int)(renderer->viewport.y + dstrect->y);
|
||||
} else {
|
||||
verts->x = (int)dstrect->x;
|
||||
verts->y = (int)dstrect->y;
|
||||
}
|
||||
verts->x = (int)dstrect->x;
|
||||
verts->y = (int)dstrect->y;
|
||||
verts->w = (int)dstrect->w;
|
||||
verts->h = (int)dstrect->h;
|
||||
|
||||
|
|
@ -300,12 +273,14 @@ typedef struct CopyExData
|
|||
double angle;
|
||||
SDL_FPoint center;
|
||||
SDL_RendererFlip flip;
|
||||
float scale_x;
|
||||
float scale_y;
|
||||
} CopyExData;
|
||||
|
||||
static int
|
||||
SW_QueueCopyEx(SDL_Renderer * renderer, SDL_RenderCommand *cmd, SDL_Texture * texture,
|
||||
const SDL_Rect * srcrect, const SDL_FRect * dstrect,
|
||||
const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip)
|
||||
const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip, float scale_x, float scale_y)
|
||||
{
|
||||
CopyExData *verts = (CopyExData *) SDL_AllocateRenderVertices(renderer, sizeof (CopyExData), 0, &cmd->data.draw.first);
|
||||
|
||||
|
|
@ -317,33 +292,48 @@ SW_QueueCopyEx(SDL_Renderer * renderer, SDL_RenderCommand *cmd, SDL_Texture * te
|
|||
|
||||
SDL_memcpy(&verts->srcrect, srcrect, sizeof (SDL_Rect));
|
||||
|
||||
if (renderer->viewport.x || renderer->viewport.y) {
|
||||
verts->dstrect.x = (int)(renderer->viewport.x + dstrect->x);
|
||||
verts->dstrect.y = (int)(renderer->viewport.y + dstrect->y);
|
||||
} else {
|
||||
verts->dstrect.x = (int)dstrect->x;
|
||||
verts->dstrect.y = (int)dstrect->y;
|
||||
}
|
||||
verts->dstrect.x = (int)dstrect->x;
|
||||
verts->dstrect.y = (int)dstrect->y;
|
||||
verts->dstrect.w = (int)dstrect->w;
|
||||
verts->dstrect.h = (int)dstrect->h;
|
||||
verts->angle = angle;
|
||||
SDL_memcpy(&verts->center, center, sizeof (SDL_FPoint));
|
||||
verts->flip = flip;
|
||||
verts->scale_x = scale_x;
|
||||
verts->scale_y = scale_y;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
Blit_to_Screen(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *surface, SDL_Rect *dstrect,
|
||||
float scale_x, float scale_y, SDL_ScaleMode scaleMode)
|
||||
{
|
||||
int retval;
|
||||
/* Renderer scaling, if needed */
|
||||
if (scale_x != 1.0f || scale_y != 1.0f) {
|
||||
SDL_Rect r;
|
||||
r.x = (int)((float) dstrect->x * scale_x);
|
||||
r.y = (int)((float) dstrect->y * scale_y);
|
||||
r.w = (int)((float) dstrect->w * scale_x);
|
||||
r.h = (int)((float) dstrect->h * scale_y);
|
||||
retval = SDL_PrivateUpperBlitScaled(src, srcrect, surface, &r, scaleMode);
|
||||
} else {
|
||||
retval = SDL_BlitSurface(src, srcrect, surface, dstrect);
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
static int
|
||||
SW_RenderCopyEx(SDL_Renderer * renderer, SDL_Surface *surface, SDL_Texture * texture,
|
||||
const SDL_Rect * srcrect, const SDL_Rect * final_rect,
|
||||
const double angle, const SDL_FPoint * center, const SDL_RendererFlip flip)
|
||||
const double angle, const SDL_FPoint * center, const SDL_RendererFlip flip, float scale_x, float scale_y)
|
||||
{
|
||||
SDL_Surface *src = (SDL_Surface *) texture->driverdata;
|
||||
SDL_Rect tmp_rect;
|
||||
SDL_Surface *src_clone, *src_rotated, *src_scaled;
|
||||
SDL_Surface *mask = NULL, *mask_rotated = NULL;
|
||||
int retval = 0, dstwidth, dstheight, abscenterx, abscentery;
|
||||
double cangle, sangle, px, py, p1x, p1y, p2x, p2y, p3x, p3y, p4x, p4y;
|
||||
int retval = 0;
|
||||
SDL_BlendMode blendmode;
|
||||
Uint8 alphaMod, rMod, gMod, bMod;
|
||||
int applyModulation = SDL_FALSE;
|
||||
|
|
@ -434,7 +424,7 @@ SW_RenderCopyEx(SDL_Renderer * renderer, SDL_Surface *surface, SDL_Texture * tex
|
|||
retval = -1;
|
||||
} else {
|
||||
SDL_SetSurfaceBlendMode(src_clone, SDL_BLENDMODE_NONE);
|
||||
retval = SDL_BlitScaled(src_clone, srcrect, src_scaled, &scale_rect);
|
||||
retval = SDL_PrivateUpperBlitScaled(src_clone, srcrect, src_scaled, &scale_rect, texture->scaleMode);
|
||||
SDL_FreeSurface(src_clone);
|
||||
src_clone = src_scaled;
|
||||
src_scaled = NULL;
|
||||
|
|
@ -445,53 +435,32 @@ SW_RenderCopyEx(SDL_Renderer * renderer, SDL_Surface *surface, SDL_Texture * tex
|
|||
SDL_SetSurfaceBlendMode(src_clone, blendmode);
|
||||
|
||||
if (!retval) {
|
||||
SDLgfx_rotozoomSurfaceSizeTrig(tmp_rect.w, tmp_rect.h, angle, &dstwidth, &dstheight, &cangle, &sangle);
|
||||
src_rotated = SDLgfx_rotateSurface(src_clone, angle, dstwidth/2, dstheight/2, (texture->scaleMode == SDL_ScaleModeNearest) ? 0 : 1, flip & SDL_FLIP_HORIZONTAL, flip & SDL_FLIP_VERTICAL, dstwidth, dstheight, cangle, sangle);
|
||||
SDL_Rect rect_dest;
|
||||
double cangle, sangle;
|
||||
|
||||
SDLgfx_rotozoomSurfaceSizeTrig(tmp_rect.w, tmp_rect.h, angle, center,
|
||||
&rect_dest, &cangle, &sangle);
|
||||
src_rotated = SDLgfx_rotateSurface(src_clone, angle,
|
||||
(texture->scaleMode == SDL_ScaleModeNearest) ? 0 : 1, flip & SDL_FLIP_HORIZONTAL, flip & SDL_FLIP_VERTICAL,
|
||||
&rect_dest, cangle, sangle, center);
|
||||
if (src_rotated == NULL) {
|
||||
retval = -1;
|
||||
}
|
||||
if (!retval && mask != NULL) {
|
||||
/* The mask needed for the NONE blend mode gets rotated with the same parameters. */
|
||||
mask_rotated = SDLgfx_rotateSurface(mask, angle, dstwidth/2, dstheight/2, SDL_FALSE, 0, 0, dstwidth, dstheight, cangle, sangle);
|
||||
mask_rotated = SDLgfx_rotateSurface(mask, angle,
|
||||
SDL_FALSE, 0, 0,
|
||||
&rect_dest, cangle, sangle, center);
|
||||
if (mask_rotated == NULL) {
|
||||
retval = -1;
|
||||
}
|
||||
}
|
||||
if (!retval) {
|
||||
/* 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 + (int)center->x;
|
||||
abscentery = final_rect->y + (int)center->y;
|
||||
/* Compensate the angle inversion to match the behaviour of the other backends */
|
||||
sangle = -sangle;
|
||||
|
||||
/* Top Left */
|
||||
px = final_rect->x - abscenterx;
|
||||
py = final_rect->y - abscentery;
|
||||
p1x = px * cangle - py * sangle + abscenterx;
|
||||
p1y = px * sangle + py * cangle + abscentery;
|
||||
|
||||
/* Top Right */
|
||||
px = final_rect->x + final_rect->w - abscenterx;
|
||||
py = final_rect->y - abscentery;
|
||||
p2x = px * cangle - py * sangle + abscenterx;
|
||||
p2y = px * sangle + py * cangle + abscentery;
|
||||
|
||||
/* Bottom Left */
|
||||
px = final_rect->x - abscenterx;
|
||||
py = final_rect->y + final_rect->h - abscentery;
|
||||
p3x = px * cangle - py * sangle + abscenterx;
|
||||
p3y = px * sangle + py * cangle + abscentery;
|
||||
|
||||
/* Bottom Right */
|
||||
px = final_rect->x + final_rect->w - abscenterx;
|
||||
py = final_rect->y + final_rect->h - abscentery;
|
||||
p4x = px * cangle - py * sangle + abscenterx;
|
||||
p4y = px * sangle + py * cangle + abscentery;
|
||||
|
||||
tmp_rect.x = (int)MIN(MIN(p1x, p2x), MIN(p3x, p4x));
|
||||
tmp_rect.y = (int)MIN(MIN(p1y, p2y), MIN(p3y, p4y));
|
||||
tmp_rect.w = dstwidth;
|
||||
tmp_rect.h = dstheight;
|
||||
tmp_rect.x = final_rect->x + rect_dest.x;
|
||||
tmp_rect.y = final_rect->y + rect_dest.y;
|
||||
tmp_rect.w = rect_dest.w;
|
||||
tmp_rect.h = rect_dest.h;
|
||||
|
||||
/* The NONE blend mode needs some special care with non-opaque surfaces.
|
||||
* Other blend modes or opaque surfaces can be blitted directly.
|
||||
|
|
@ -502,7 +471,8 @@ SW_RenderCopyEx(SDL_Renderer * renderer, SDL_Surface *surface, SDL_Texture * tex
|
|||
SDL_SetSurfaceAlphaMod(src_rotated, alphaMod);
|
||||
SDL_SetSurfaceColorMod(src_rotated, rMod, gMod, bMod);
|
||||
}
|
||||
retval = SDL_BlitSurface(src_rotated, NULL, surface, &tmp_rect);
|
||||
/* Renderer scaling, if needed */
|
||||
retval = Blit_to_Screen(src_rotated, NULL, surface, &tmp_rect, scale_x, scale_y, texture->scaleMode);
|
||||
} else {
|
||||
/* The NONE blend mode requires three steps to get the pixels onto the destination surface.
|
||||
* First, the area where the rotated pixels will be blitted to get set to zero.
|
||||
|
|
@ -511,7 +481,8 @@ SW_RenderCopyEx(SDL_Renderer * renderer, SDL_Surface *surface, SDL_Texture * tex
|
|||
*/
|
||||
SDL_Rect mask_rect = tmp_rect;
|
||||
SDL_SetSurfaceBlendMode(mask_rotated, SDL_BLENDMODE_NONE);
|
||||
retval = SDL_BlitSurface(mask_rotated, NULL, surface, &mask_rect);
|
||||
/* Renderer scaling, if needed */
|
||||
retval = Blit_to_Screen(mask_rotated, NULL, surface, &mask_rect, scale_x, scale_y, texture->scaleMode);
|
||||
if (!retval) {
|
||||
/* The next step copies the alpha value. This is done with the BLEND blend mode and
|
||||
* by modulating the source colors with 0. Since the destination is all zeros, this
|
||||
|
|
@ -519,7 +490,8 @@ SW_RenderCopyEx(SDL_Renderer * renderer, SDL_Surface *surface, SDL_Texture * tex
|
|||
*/
|
||||
SDL_SetSurfaceColorMod(src_rotated, 0, 0, 0);
|
||||
mask_rect = tmp_rect;
|
||||
retval = SDL_BlitSurface(src_rotated, NULL, surface, &mask_rect);
|
||||
/* Renderer scaling, if needed */
|
||||
retval = Blit_to_Screen(src_rotated, NULL, surface, &mask_rect, scale_x, scale_y, texture->scaleMode);
|
||||
if (!retval) {
|
||||
/* The last step gets the color values in place. The ADD blend mode simply adds them to
|
||||
* the destination (where the color values are all zero). However, because the ADD blend
|
||||
|
|
@ -535,7 +507,8 @@ SW_RenderCopyEx(SDL_Renderer * renderer, SDL_Surface *surface, SDL_Texture * tex
|
|||
retval = -1;
|
||||
} else {
|
||||
SDL_SetSurfaceBlendMode(src_rotated_rgb, SDL_BLENDMODE_ADD);
|
||||
retval = SDL_BlitSurface(src_rotated_rgb, NULL, surface, &tmp_rect);
|
||||
/* Renderer scaling, if needed */
|
||||
retval = Blit_to_Screen(src_rotated_rgb, NULL, surface, &tmp_rect, scale_x, scale_y, texture->scaleMode);
|
||||
SDL_FreeSurface(src_rotated_rgb);
|
||||
}
|
||||
}
|
||||
|
|
@ -560,6 +533,104 @@ SW_RenderCopyEx(SDL_Renderer * renderer, SDL_Surface *surface, SDL_Texture * tex
|
|||
return retval;
|
||||
}
|
||||
|
||||
|
||||
typedef struct GeometryFillData
|
||||
{
|
||||
SDL_Point dst;
|
||||
SDL_Color color;
|
||||
} GeometryFillData;
|
||||
|
||||
typedef struct GeometryCopyData
|
||||
{
|
||||
SDL_Point src;
|
||||
SDL_Point dst;
|
||||
SDL_Color color;
|
||||
} GeometryCopyData;
|
||||
|
||||
static int
|
||||
SW_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *texture,
|
||||
const float *xy, int xy_stride, const SDL_Color *color, int color_stride, const float *uv, int uv_stride,
|
||||
int num_vertices, const void *indices, int num_indices, int size_indices,
|
||||
float scale_x, float scale_y)
|
||||
{
|
||||
int i;
|
||||
int count = indices ? num_indices : num_vertices;
|
||||
void *verts;
|
||||
int sz = texture ? sizeof (GeometryCopyData) : sizeof (GeometryFillData);
|
||||
|
||||
verts = (void *) SDL_AllocateRenderVertices(renderer, count * sz, 0, &cmd->data.draw.first);
|
||||
if (!verts) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
cmd->data.draw.count = count;
|
||||
size_indices = indices ? size_indices : 0;
|
||||
|
||||
if (texture) {
|
||||
GeometryCopyData *ptr = (GeometryCopyData *) verts;
|
||||
for (i = 0; i < count; i++) {
|
||||
int j;
|
||||
float *xy_;
|
||||
SDL_Color col_;
|
||||
float *uv_;
|
||||
if (size_indices == 4) {
|
||||
j = ((const Uint32 *)indices)[i];
|
||||
} else if (size_indices == 2) {
|
||||
j = ((const Uint16 *)indices)[i];
|
||||
} else if (size_indices == 1) {
|
||||
j = ((const Uint8 *)indices)[i];
|
||||
} else {
|
||||
j = i;
|
||||
}
|
||||
|
||||
xy_ = (float *)((char*)xy + j * xy_stride);
|
||||
col_ = *(SDL_Color *)((char*)color + j * color_stride);
|
||||
|
||||
uv_ = (float *)((char*)uv + j * uv_stride);
|
||||
|
||||
ptr->src.x = (int)(uv_[0] * texture->w);
|
||||
ptr->src.y = (int)(uv_[1] * texture->h);
|
||||
|
||||
ptr->dst.x = (int)(xy_[0] * scale_x);
|
||||
ptr->dst.y = (int)(xy_[1] * scale_y);
|
||||
trianglepoint_2_fixedpoint(&ptr->dst);
|
||||
|
||||
ptr->color = col_;
|
||||
|
||||
ptr++;
|
||||
}
|
||||
} else {
|
||||
GeometryFillData *ptr = (GeometryFillData *) verts;
|
||||
|
||||
for (i = 0; i < count; i++) {
|
||||
int j;
|
||||
float *xy_;
|
||||
SDL_Color col_;
|
||||
if (size_indices == 4) {
|
||||
j = ((const Uint32 *)indices)[i];
|
||||
} else if (size_indices == 2) {
|
||||
j = ((const Uint16 *)indices)[i];
|
||||
} else if (size_indices == 1) {
|
||||
j = ((const Uint8 *)indices)[i];
|
||||
} else {
|
||||
j = i;
|
||||
}
|
||||
|
||||
xy_ = (float *)((char*)xy + j * xy_stride);
|
||||
col_ = *(SDL_Color *)((char*)color + j * color_stride);
|
||||
|
||||
ptr->dst.x = (int)(xy_[0] * scale_x);
|
||||
ptr->dst.y = (int)(xy_[1] * scale_y);
|
||||
trianglepoint_2_fixedpoint(&ptr->dst);
|
||||
|
||||
ptr->color = col_;
|
||||
|
||||
ptr++;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
PrepTextureForCopy(const SDL_RenderCommand *cmd)
|
||||
{
|
||||
|
|
@ -634,7 +705,7 @@ SW_RunCommandQueue(SDL_Renderer * renderer, SDL_RenderCommand *cmd, void *vertic
|
|||
}
|
||||
|
||||
case SDL_RENDERCMD_SETCLIPRECT: {
|
||||
drawstate.cliprect = cmd->data.cliprect.enabled ? &cmd->data.cliprect.rect : NULL;
|
||||
drawstate.cliprect = cmd->data.cliprect.enabled ? &cmd->data.cliprect.rect : NULL;
|
||||
drawstate.surface_cliprect_dirty = SDL_TRUE;
|
||||
break;
|
||||
}
|
||||
|
|
@ -657,9 +728,19 @@ SW_RunCommandQueue(SDL_Renderer * renderer, SDL_RenderCommand *cmd, void *vertic
|
|||
const Uint8 b = cmd->data.draw.b;
|
||||
const Uint8 a = cmd->data.draw.a;
|
||||
const int count = (int) cmd->data.draw.count;
|
||||
const SDL_Point *verts = (SDL_Point *) (((Uint8 *) vertices) + cmd->data.draw.first);
|
||||
SDL_Point *verts = (SDL_Point *) (((Uint8 *) vertices) + cmd->data.draw.first);
|
||||
const SDL_BlendMode blend = cmd->data.draw.blend;
|
||||
SetDrawState(surface, &drawstate);
|
||||
|
||||
/* Apply viewport */
|
||||
if (drawstate.viewport->x || drawstate.viewport->y) {
|
||||
int i;
|
||||
for (i = 0; i < count; i++) {
|
||||
verts[i].x += drawstate.viewport->x;
|
||||
verts[i].y += drawstate.viewport->y;
|
||||
}
|
||||
}
|
||||
|
||||
if (blend == SDL_BLENDMODE_NONE) {
|
||||
SDL_DrawPoints(surface, verts, count, SDL_MapRGBA(surface->format, r, g, b, a));
|
||||
} else {
|
||||
|
|
@ -674,9 +755,19 @@ SW_RunCommandQueue(SDL_Renderer * renderer, SDL_RenderCommand *cmd, void *vertic
|
|||
const Uint8 b = cmd->data.draw.b;
|
||||
const Uint8 a = cmd->data.draw.a;
|
||||
const int count = (int) cmd->data.draw.count;
|
||||
const SDL_Point *verts = (SDL_Point *) (((Uint8 *) vertices) + cmd->data.draw.first);
|
||||
SDL_Point *verts = (SDL_Point *) (((Uint8 *) vertices) + cmd->data.draw.first);
|
||||
const SDL_BlendMode blend = cmd->data.draw.blend;
|
||||
SetDrawState(surface, &drawstate);
|
||||
|
||||
/* Apply viewport */
|
||||
if (drawstate.viewport->x || drawstate.viewport->y) {
|
||||
int i;
|
||||
for (i = 0; i < count; i++) {
|
||||
verts[i].x += drawstate.viewport->x;
|
||||
verts[i].y += drawstate.viewport->y;
|
||||
}
|
||||
}
|
||||
|
||||
if (blend == SDL_BLENDMODE_NONE) {
|
||||
SDL_DrawLines(surface, verts, count, SDL_MapRGBA(surface->format, r, g, b, a));
|
||||
} else {
|
||||
|
|
@ -691,9 +782,19 @@ SW_RunCommandQueue(SDL_Renderer * renderer, SDL_RenderCommand *cmd, void *vertic
|
|||
const Uint8 b = cmd->data.draw.b;
|
||||
const Uint8 a = cmd->data.draw.a;
|
||||
const int count = (int) cmd->data.draw.count;
|
||||
const SDL_Rect *verts = (SDL_Rect *) (((Uint8 *) vertices) + cmd->data.draw.first);
|
||||
SDL_Rect *verts = (SDL_Rect *) (((Uint8 *) vertices) + cmd->data.draw.first);
|
||||
const SDL_BlendMode blend = cmd->data.draw.blend;
|
||||
SetDrawState(surface, &drawstate);
|
||||
|
||||
/* Apply viewport */
|
||||
if (drawstate.viewport->x || drawstate.viewport->y) {
|
||||
int i;
|
||||
for (i = 0; i < count; i++) {
|
||||
verts[i].x += drawstate.viewport->x;
|
||||
verts[i].y += drawstate.viewport->y;
|
||||
}
|
||||
}
|
||||
|
||||
if (blend == SDL_BLENDMODE_NONE) {
|
||||
SDL_FillRects(surface, verts, count, SDL_MapRGBA(surface->format, r, g, b, a));
|
||||
} else {
|
||||
|
|
@ -713,6 +814,12 @@ SW_RunCommandQueue(SDL_Renderer * renderer, SDL_RenderCommand *cmd, void *vertic
|
|||
|
||||
PrepTextureForCopy(cmd);
|
||||
|
||||
/* Apply viewport */
|
||||
if (drawstate.viewport->x || drawstate.viewport->y) {
|
||||
dstrect->x += drawstate.viewport->x;
|
||||
dstrect->y += drawstate.viewport->y;
|
||||
}
|
||||
|
||||
if ( srcrect->w == dstrect->w && srcrect->h == dstrect->h ) {
|
||||
SDL_BlitSurface(src, srcrect, surface, dstrect);
|
||||
} else {
|
||||
|
|
@ -720,17 +827,118 @@ SW_RunCommandQueue(SDL_Renderer * renderer, SDL_RenderCommand *cmd, void *vertic
|
|||
* to avoid potentially frequent RLE encoding/decoding.
|
||||
*/
|
||||
SDL_SetSurfaceRLE(surface, 0);
|
||||
SDL_BlitScaled(src, srcrect, surface, dstrect);
|
||||
|
||||
/* Prevent to do scaling + clipping on viewport boundaries as it may lose proportion */
|
||||
if (dstrect->x < 0 || dstrect->y < 0 || dstrect->x + dstrect->w > surface->w || dstrect->y + dstrect->h > surface->h) {
|
||||
SDL_Surface *tmp = SDL_CreateRGBSurfaceWithFormat(0, dstrect->w, dstrect->h, 0, src->format->format);
|
||||
/* Scale to an intermediate surface, then blit */
|
||||
if (tmp) {
|
||||
SDL_Rect r;
|
||||
SDL_BlendMode blendmode;
|
||||
Uint8 alphaMod, rMod, gMod, bMod;
|
||||
|
||||
SDL_GetSurfaceBlendMode(src, &blendmode);
|
||||
SDL_GetSurfaceAlphaMod(src, &alphaMod);
|
||||
SDL_GetSurfaceColorMod(src, &rMod, &gMod, &bMod);
|
||||
|
||||
r.x = 0;
|
||||
r.y = 0;
|
||||
r.w = dstrect->w;
|
||||
r.h = dstrect->h;
|
||||
|
||||
SDL_SetSurfaceBlendMode(src, SDL_BLENDMODE_NONE);
|
||||
SDL_SetSurfaceColorMod(src, 255, 255, 255);
|
||||
SDL_SetSurfaceAlphaMod(src, 255);
|
||||
|
||||
SDL_PrivateUpperBlitScaled(src, srcrect, tmp, &r, texture->scaleMode);
|
||||
|
||||
SDL_SetSurfaceColorMod(tmp, rMod, gMod, bMod);
|
||||
SDL_SetSurfaceAlphaMod(tmp, alphaMod);
|
||||
SDL_SetSurfaceBlendMode(tmp, blendmode);
|
||||
|
||||
SDL_BlitSurface(tmp, NULL, surface, dstrect);
|
||||
SDL_FreeSurface(tmp);
|
||||
/* No need to set back r/g/b/a/blendmode to 'src' since it's done in PrepTextureForCopy() */
|
||||
}
|
||||
} else{
|
||||
SDL_PrivateUpperBlitScaled(src, srcrect, surface, dstrect, texture->scaleMode);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case SDL_RENDERCMD_COPY_EX: {
|
||||
const CopyExData *copydata = (CopyExData *) (((Uint8 *) vertices) + cmd->data.draw.first);
|
||||
CopyExData *copydata = (CopyExData *) (((Uint8 *) vertices) + cmd->data.draw.first);
|
||||
SetDrawState(surface, &drawstate);
|
||||
PrepTextureForCopy(cmd);
|
||||
|
||||
/* Apply viewport */
|
||||
if (drawstate.viewport->x || drawstate.viewport->y) {
|
||||
copydata->dstrect.x += drawstate.viewport->x;
|
||||
copydata->dstrect.y += drawstate.viewport->y;
|
||||
}
|
||||
|
||||
SW_RenderCopyEx(renderer, surface, cmd->data.draw.texture, ©data->srcrect,
|
||||
©data->dstrect, copydata->angle, ©data->center, copydata->flip);
|
||||
©data->dstrect, copydata->angle, ©data->center, copydata->flip,
|
||||
copydata->scale_x, copydata->scale_y);
|
||||
break;
|
||||
}
|
||||
|
||||
case SDL_RENDERCMD_GEOMETRY: {
|
||||
int i;
|
||||
SDL_Rect *verts = (SDL_Rect *) (((Uint8 *) vertices) + cmd->data.draw.first);
|
||||
const int count = (int) cmd->data.draw.count;
|
||||
SDL_Texture *texture = cmd->data.draw.texture;
|
||||
const SDL_BlendMode blend = cmd->data.draw.blend;
|
||||
|
||||
SetDrawState(surface, &drawstate);
|
||||
|
||||
if (texture) {
|
||||
SDL_Surface *src = (SDL_Surface *) texture->driverdata;
|
||||
|
||||
GeometryCopyData *ptr = (GeometryCopyData *) verts;
|
||||
|
||||
PrepTextureForCopy(cmd);
|
||||
|
||||
/* Apply viewport */
|
||||
if (drawstate.viewport->x || drawstate.viewport->y) {
|
||||
SDL_Point vp;
|
||||
vp.x = drawstate.viewport->x;
|
||||
vp.y = drawstate.viewport->y;
|
||||
trianglepoint_2_fixedpoint(&vp);
|
||||
for (i = 0; i < count; i++) {
|
||||
ptr[i].dst.x += vp.x;
|
||||
ptr[i].dst.y += vp.y;
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < count; i += 3, ptr += 3) {
|
||||
SDL_SW_BlitTriangle(
|
||||
src,
|
||||
&(ptr[0].src), &(ptr[1].src), &(ptr[2].src),
|
||||
surface,
|
||||
&(ptr[0].dst), &(ptr[1].dst), &(ptr[2].dst),
|
||||
ptr[0].color, ptr[1].color, ptr[2].color);
|
||||
}
|
||||
} else {
|
||||
GeometryFillData *ptr = (GeometryFillData *) verts;
|
||||
|
||||
/* Apply viewport */
|
||||
if (drawstate.viewport->x || drawstate.viewport->y) {
|
||||
SDL_Point vp;
|
||||
vp.x = drawstate.viewport->x;
|
||||
vp.y = drawstate.viewport->y;
|
||||
trianglepoint_2_fixedpoint(&vp);
|
||||
for (i = 0; i < count; i++) {
|
||||
ptr[i].dst.x += vp.x;
|
||||
ptr[i].dst.y += vp.y;
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < count; i += 3, ptr += 3) {
|
||||
SDL_SW_FillTriangle(surface, &(ptr[0].dst), &(ptr[1].dst), &(ptr[2].dst), blend, ptr[0].color, ptr[1].color, ptr[2].color);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
@ -809,7 +1017,7 @@ SW_CreateRendererForSurface(SDL_Surface * surface)
|
|||
SW_RenderData *data;
|
||||
|
||||
if (!surface) {
|
||||
SDL_SetError("Can't create renderer for NULL surface");
|
||||
SDL_InvalidParamError("surface");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
|
@ -843,6 +1051,7 @@ SW_CreateRendererForSurface(SDL_Surface * surface)
|
|||
renderer->QueueFillRects = SW_QueueFillRects;
|
||||
renderer->QueueCopy = SW_QueueCopy;
|
||||
renderer->QueueCopyEx = SW_QueueCopyEx;
|
||||
renderer->QueueGeometry = SW_QueueGeometry;
|
||||
renderer->RunCommandQueue = SW_RunCommandQueue;
|
||||
renderer->RenderReadPixels = SW_RenderReadPixels;
|
||||
renderer->RenderPresent = SW_RenderPresent;
|
||||
|
|
@ -859,9 +1068,29 @@ SW_CreateRendererForSurface(SDL_Surface * surface)
|
|||
static SDL_Renderer *
|
||||
SW_CreateRenderer(SDL_Window * window, Uint32 flags)
|
||||
{
|
||||
const char *hint;
|
||||
SDL_Surface *surface;
|
||||
SDL_bool no_hint_set;
|
||||
|
||||
/* Set the vsync hint based on our flags, if it's not already set */
|
||||
hint = SDL_GetHint(SDL_HINT_RENDER_VSYNC);
|
||||
if (!hint || !*hint) {
|
||||
no_hint_set = SDL_TRUE;
|
||||
} else {
|
||||
no_hint_set = SDL_FALSE;
|
||||
}
|
||||
|
||||
if (no_hint_set) {
|
||||
SDL_SetHint(SDL_HINT_RENDER_VSYNC, (flags & SDL_RENDERER_PRESENTVSYNC) ? "1" : "0");
|
||||
}
|
||||
|
||||
surface = SDL_GetWindowSurface(window);
|
||||
|
||||
/* Reset the vsync hint if we set it above */
|
||||
if (no_hint_set) {
|
||||
SDL_SetHint(SDL_HINT_RENDER_VSYNC, "");
|
||||
}
|
||||
|
||||
if (!surface) {
|
||||
return NULL;
|
||||
}
|
||||
|
|
@ -872,7 +1101,7 @@ SDL_RenderDriver SW_RenderDriver = {
|
|||
SW_CreateRenderer,
|
||||
{
|
||||
"software",
|
||||
SDL_RENDERER_SOFTWARE | SDL_RENDERER_TARGETTEXTURE,
|
||||
SDL_RENDERER_SOFTWARE | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE,
|
||||
8,
|
||||
{
|
||||
SDL_PIXELFORMAT_ARGB8888,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
|
||||
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
|
|
|
|||
|
|
@ -82,7 +82,7 @@ to a situation where the program can segfault.
|
|||
\brief Returns colorkey info for a surface
|
||||
*/
|
||||
static Uint32
|
||||
_colorkey(SDL_Surface *src)
|
||||
get_colorkey(SDL_Surface *src)
|
||||
{
|
||||
Uint32 key = 0;
|
||||
if (SDL_HasColorKey(src)) {
|
||||
|
|
@ -91,6 +91,18 @@ _colorkey(SDL_Surface *src)
|
|||
return key;
|
||||
}
|
||||
|
||||
/* rotate (sx, sy) by (angle, center) into (dx, dy) */
|
||||
static void
|
||||
rotate(double sx, double sy, double sinangle, double cosangle, const SDL_FPoint *center, double *dx, double *dy) {
|
||||
sx -= center->x;
|
||||
sy -= center->y;
|
||||
|
||||
*dx = cosangle * sx - sinangle * sy;
|
||||
*dy = sinangle * sx + cosangle * sy;
|
||||
|
||||
*dx += center->x;
|
||||
*dy += center->y;
|
||||
}
|
||||
|
||||
/* !
|
||||
\brief Internal target surface sizing function for rotations with trig result return.
|
||||
|
|
@ -105,49 +117,61 @@ _colorkey(SDL_Surface *src)
|
|||
|
||||
*/
|
||||
void
|
||||
SDLgfx_rotozoomSurfaceSizeTrig(int width, int height, double angle,
|
||||
int *dstwidth, int *dstheight,
|
||||
double *cangle, double *sangle)
|
||||
SDLgfx_rotozoomSurfaceSizeTrig(int width, int height, double angle, const SDL_FPoint *center,
|
||||
SDL_Rect *rect_dest, double *cangle, double *sangle)
|
||||
{
|
||||
/* The trig code below gets the wrong size (due to FP inaccuracy?) when angle is a multiple of 90 degrees */
|
||||
int angle90 = (int)(angle/90);
|
||||
if(angle90 == angle/90) { /* if the angle is a multiple of 90 degrees */
|
||||
angle90 %= 4;
|
||||
if(angle90 < 0) angle90 += 4; /* 0:0 deg, 1:90 deg, 2:180 deg, 3:270 deg */
|
||||
if(angle90 & 1) {
|
||||
*dstwidth = height;
|
||||
*dstheight = width;
|
||||
*cangle = 0;
|
||||
*sangle = angle90 == 1 ? -1 : 1; /* reversed because our rotations are clockwise */
|
||||
} else {
|
||||
*dstwidth = width;
|
||||
*dstheight = height;
|
||||
*cangle = angle90 == 0 ? 1 : -1;
|
||||
*sangle = 0;
|
||||
}
|
||||
} else {
|
||||
double x, y, cx, cy, sx, sy;
|
||||
double radangle;
|
||||
int dstwidthhalf, dstheighthalf;
|
||||
/*
|
||||
* Determine destination width and height by rotating a centered source box
|
||||
*/
|
||||
radangle = angle * (M_PI / -180.0); /* reverse the angle because our rotations are clockwise */
|
||||
*sangle = SDL_sin(radangle);
|
||||
*cangle = SDL_cos(radangle);
|
||||
x = (double)(width / 2);
|
||||
y = (double)(height / 2);
|
||||
cx = *cangle * x;
|
||||
cy = *cangle * y;
|
||||
sx = *sangle * x;
|
||||
sy = *sangle * y;
|
||||
int minx, maxx, miny, maxy;
|
||||
double radangle;
|
||||
double x0, x1, x2, x3;
|
||||
double y0, y1, y2, y3;
|
||||
double sinangle;
|
||||
double cosangle;
|
||||
|
||||
dstwidthhalf = MAX((int)
|
||||
SDL_ceil(MAX(MAX(MAX(SDL_fabs(cx + sy), SDL_fabs(cx - sy)), SDL_fabs(-cx + sy)), SDL_fabs(-cx - sy))), 1);
|
||||
dstheighthalf = MAX((int)
|
||||
SDL_ceil(MAX(MAX(MAX(SDL_fabs(sx + cy), SDL_fabs(sx - cy)), SDL_fabs(-sx + cy)), SDL_fabs(-sx - cy))), 1);
|
||||
*dstwidth = 2 * dstwidthhalf;
|
||||
*dstheight = 2 * dstheighthalf;
|
||||
radangle = angle * (M_PI / 180.0);
|
||||
sinangle = SDL_sin(radangle);
|
||||
cosangle = SDL_cos(radangle);
|
||||
|
||||
/*
|
||||
* Determine destination width and height by rotating a source box, at pixel center
|
||||
*/
|
||||
rotate(0.5, 0.5, sinangle, cosangle, center, &x0, &y0);
|
||||
rotate(width - 0.5, 0.5, sinangle, cosangle, center, &x1, &y1);
|
||||
rotate(0.5, height - 0.5, sinangle, cosangle, center, &x2, &y2);
|
||||
rotate(width - 0.5, height - 0.5, sinangle, cosangle, center, &x3, &y3);
|
||||
|
||||
minx = (int)SDL_floor( SDL_min( SDL_min(x0, x1), SDL_min(x2, x3) ) );
|
||||
maxx = (int)SDL_ceil( SDL_max( SDL_max(x0, x1), SDL_max(x2, x3) ) );
|
||||
|
||||
miny = (int)SDL_floor( SDL_min( SDL_min(y0, y1), SDL_min(y2, y3) ) );
|
||||
maxy = (int)SDL_ceil( SDL_max( SDL_max(y0, y1), SDL_max(y2, y3) ) );
|
||||
|
||||
rect_dest->w = maxx - minx;
|
||||
rect_dest->h = maxy - miny;
|
||||
rect_dest->x = minx;
|
||||
rect_dest->y = miny;
|
||||
|
||||
/* reverse the angle because our rotations are clockwise */
|
||||
*sangle = -sinangle;
|
||||
*cangle = cosangle;
|
||||
|
||||
{
|
||||
/* The trig code below gets the wrong size (due to FP inaccuracy?) when angle is a multiple of 90 degrees */
|
||||
int angle90 = (int)(angle/90);
|
||||
if(angle90 == angle/90) { /* if the angle is a multiple of 90 degrees */
|
||||
angle90 %= 4;
|
||||
if(angle90 < 0) angle90 += 4; /* 0:0 deg, 1:90 deg, 2:180 deg, 3:270 deg */
|
||||
if(angle90 & 1) {
|
||||
rect_dest->w = height;
|
||||
rect_dest->h = width;
|
||||
*cangle = 0;
|
||||
*sangle = angle90 == 1 ? -1 : 1; /* reversed because our rotations are clockwise */
|
||||
} else {
|
||||
rect_dest->w = width;
|
||||
rect_dest->h = height;
|
||||
*cangle = angle90 == 0 ? 1 : -1;
|
||||
*sangle = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -184,7 +208,7 @@ computeSourceIncrements90(SDL_Surface * src, int bpp, int angle, int flipx, int
|
|||
if (signy < 0) sp += (src->h-1)*src->pitch; \
|
||||
\
|
||||
for (dy = 0; dy < dst->h; sp += sincy, dp += dincy, dy++) { \
|
||||
if (sincx == sizeof(pixelType)) { /* if advancing src and dest equally, use memcpy */ \
|
||||
if (sincx == sizeof(pixelType)) { /* if advancing src and dest equally, use SDL_memcpy */ \
|
||||
SDL_memcpy(dp, sp, dst->w*sizeof(pixelType)); \
|
||||
sp += dst->w*sizeof(pixelType); \
|
||||
dp += dst->w*sizeof(pixelType); \
|
||||
|
|
@ -220,48 +244,56 @@ Assumes dst surface was allocated with the correct dimensions.
|
|||
|
||||
\param src Source surface.
|
||||
\param dst Destination surface.
|
||||
\param cx Horizontal center coordinate.
|
||||
\param cy Vertical center coordinate.
|
||||
\param isin Integer version of sine of angle.
|
||||
\param icos Integer version of cosine of angle.
|
||||
\param flipx Flag indicating horizontal mirroring should be applied.
|
||||
\param flipy Flag indicating vertical mirroring should be applied.
|
||||
\param smooth Flag indicating anti-aliasing should be used.
|
||||
\param dst_rect destination coordinates
|
||||
\param center true center.
|
||||
*/
|
||||
static void
|
||||
_transformSurfaceRGBA(SDL_Surface * src, SDL_Surface * dst, int cx, int cy, int isin, int icos, int flipx, int flipy, int smooth)
|
||||
transformSurfaceRGBA(SDL_Surface * src, SDL_Surface * dst, int isin, int icos,
|
||||
int flipx, int flipy, int smooth,
|
||||
const SDL_Rect *rect_dest,
|
||||
const SDL_FPoint *center)
|
||||
{
|
||||
int x, y, t1, t2, dx, dy, xd, yd, sdx, sdy, ax, ay, ex, ey, sw, sh;
|
||||
int sw, sh;
|
||||
int cx, cy;
|
||||
tColorRGBA c00, c01, c10, c11, cswap;
|
||||
tColorRGBA *pc, *sp;
|
||||
int gap;
|
||||
const int fp_half = (1<<15);
|
||||
|
||||
/*
|
||||
* Variable setup
|
||||
*/
|
||||
xd = ((src->w - dst->w) << 15);
|
||||
yd = ((src->h - dst->h) << 15);
|
||||
ax = (cx << 16) - (icos * cx);
|
||||
ay = (cy << 16) - (isin * cx);
|
||||
sw = src->w - 1;
|
||||
sh = src->h - 1;
|
||||
pc = (tColorRGBA*) dst->pixels;
|
||||
gap = dst->pitch - dst->w * 4;
|
||||
cx = (int)(center->x * 65536.0);
|
||||
cy = (int)(center->y * 65536.0);
|
||||
|
||||
/*
|
||||
* Switch between interpolating and non-interpolating code
|
||||
*/
|
||||
if (smooth) {
|
||||
int y;
|
||||
for (y = 0; y < dst->h; y++) {
|
||||
dy = cy - y;
|
||||
sdx = (ax + (isin * dy)) + xd;
|
||||
sdy = (ay - (icos * dy)) + yd;
|
||||
int x;
|
||||
double src_x = (rect_dest->x + 0 + 0.5 - center->x);
|
||||
double src_y = (rect_dest->y + y + 0.5 - center->y);
|
||||
int sdx = (int)((icos * src_x - isin * src_y) + cx - fp_half);
|
||||
int sdy = (int)((isin * src_x + icos * src_y) + cy - fp_half);
|
||||
for (x = 0; x < dst->w; x++) {
|
||||
dx = (sdx >> 16);
|
||||
dy = (sdy >> 16);
|
||||
int dx = (sdx >> 16);
|
||||
int dy = (sdy >> 16);
|
||||
if (flipx) dx = sw - dx;
|
||||
if (flipy) dy = sh - dy;
|
||||
if ((dx > -1) && (dy > -1) && (dx < (src->w-1)) && (dy < (src->h-1))) {
|
||||
int ex, ey;
|
||||
int t1, t2;
|
||||
sp = (tColorRGBA *) ((Uint8 *) src->pixels + src->pitch * dy) + dx;
|
||||
c00 = *sp;
|
||||
sp += 1;
|
||||
|
|
@ -303,13 +335,16 @@ _transformSurfaceRGBA(SDL_Surface * src, SDL_Surface * dst, int cx, int cy, int
|
|||
pc = (tColorRGBA *) ((Uint8 *) pc + gap);
|
||||
}
|
||||
} else {
|
||||
int y;
|
||||
for (y = 0; y < dst->h; y++) {
|
||||
dy = cy - y;
|
||||
sdx = (ax + (isin * dy)) + xd;
|
||||
sdy = (ay - (icos * dy)) + yd;
|
||||
int x;
|
||||
double src_x = (rect_dest->x + 0 + 0.5 - center->x);
|
||||
double src_y = (rect_dest->y + y + 0.5 - center->y);
|
||||
int sdx = (int)((icos * src_x - isin * src_y) + cx - fp_half);
|
||||
int sdy = (int)((isin * src_x + icos * src_y) + cy - fp_half);
|
||||
for (x = 0; x < dst->w; x++) {
|
||||
dx = (sdx >> 16);
|
||||
dy = (sdy >> 16);
|
||||
int dx = (sdx >> 16);
|
||||
int dy = (sdy >> 16);
|
||||
if ((unsigned)dx < (unsigned)src->w && (unsigned)dy < (unsigned)src->h) {
|
||||
if(flipx) dx = sw - dx;
|
||||
if(flipy) dy = sh - dy;
|
||||
|
|
@ -335,46 +370,54 @@ Assumes dst surface was allocated with the correct dimensions.
|
|||
|
||||
\param src Source surface.
|
||||
\param dst Destination surface.
|
||||
\param cx Horizontal center coordinate.
|
||||
\param cy Vertical center coordinate.
|
||||
\param isin Integer version of sine of angle.
|
||||
\param icos Integer version of cosine of angle.
|
||||
\param flipx Flag indicating horizontal mirroring should be applied.
|
||||
\param flipy Flag indicating vertical mirroring should be applied.
|
||||
\param dst_rect destination coordinates
|
||||
\param center true center.
|
||||
*/
|
||||
static void
|
||||
transformSurfaceY(SDL_Surface * src, SDL_Surface * dst, int cx, int cy, int isin, int icos, int flipx, int flipy)
|
||||
transformSurfaceY(SDL_Surface * src, SDL_Surface * dst, int isin, int icos, int flipx, int flipy,
|
||||
const SDL_Rect *rect_dest,
|
||||
const SDL_FPoint *center)
|
||||
{
|
||||
int x, y, dx, dy, xd, yd, sdx, sdy, ax, ay;
|
||||
int sw, sh;
|
||||
int cx, cy;
|
||||
tColorY *pc;
|
||||
int gap;
|
||||
const int fp_half = (1<<15);
|
||||
int y;
|
||||
|
||||
/*
|
||||
* Variable setup
|
||||
*/
|
||||
xd = ((src->w - dst->w) << 15);
|
||||
yd = ((src->h - dst->h) << 15);
|
||||
ax = (cx << 16) - (icos * cx);
|
||||
ay = (cy << 16) - (isin * cx);
|
||||
sw = src->w - 1;
|
||||
sh = src->h - 1;
|
||||
pc = (tColorY*) dst->pixels;
|
||||
gap = dst->pitch - dst->w;
|
||||
cx = (int)(center->x * 65536.0);
|
||||
cy = (int)(center->y * 65536.0);
|
||||
|
||||
/*
|
||||
* Clear surface to colorkey
|
||||
*/
|
||||
SDL_memset(pc, (int)(_colorkey(src) & 0xff), dst->pitch * dst->h);
|
||||
SDL_memset(pc, (int)(get_colorkey(src) & 0xff), dst->pitch * dst->h);
|
||||
/*
|
||||
* Iterate through destination surface
|
||||
*/
|
||||
for (y = 0; y < dst->h; y++) {
|
||||
dy = cy - y;
|
||||
sdx = (ax + (isin * dy)) + xd;
|
||||
sdy = (ay - (icos * dy)) + yd;
|
||||
int x;
|
||||
double src_x = (rect_dest->x + 0 + 0.5 - center->x);
|
||||
double src_y = (rect_dest->y + y + 0.5 - center->y);
|
||||
int sdx = (int)((icos * src_x - isin * src_y) + cx - fp_half);
|
||||
int sdy = (int)((isin * src_x + icos * src_y) + cy - fp_half);
|
||||
for (x = 0; x < dst->w; x++) {
|
||||
dx = (sdx >> 16);
|
||||
dy = (sdy >> 16);
|
||||
int dx = (sdx >> 16);
|
||||
int dy = (sdy >> 16);
|
||||
if ((unsigned)dx < (unsigned)src->w && (unsigned)dy < (unsigned)src->h) {
|
||||
if (flipx) dx = (src->w-1)-dx;
|
||||
if (flipy) dy = (src->h-1)-dy;
|
||||
if (flipx) dx = sw - dx;
|
||||
if (flipy) dy = sh- dy;
|
||||
*pc = *((tColorY *)src->pixels + src->pitch * dy + dx);
|
||||
}
|
||||
sdx += icos;
|
||||
|
|
@ -390,7 +433,7 @@ transformSurfaceY(SDL_Surface * src, SDL_Surface * dst, int cx, int cy, int isin
|
|||
\brief Rotates and zooms a surface with different horizontal and vertival scaling factors and optional anti-aliasing.
|
||||
|
||||
Rotates a 32-bit or 8-bit 'src' surface to newly created 'dst' surface.
|
||||
'angle' is the rotation in degrees, 'centerx' and 'centery' the rotation center. If 'smooth' is set
|
||||
'angle' is the rotation in degrees, 'center' the rotation center. If 'smooth' is set
|
||||
then the destination 32-bit surface is anti-aliased. 8-bit surfaces must have a colorkey. 32-bit
|
||||
surfaces must have a 8888 layout with red, green, blue and alpha masks (any ordering goes).
|
||||
The blend mode of the 'src' surface has some effects on generation of the 'dst' surface: The NONE
|
||||
|
|
@ -400,21 +443,21 @@ When using the NONE and MOD modes, color and alpha modulation must be applied be
|
|||
|
||||
\param src The surface to rotozoom.
|
||||
\param angle The angle to rotate in degrees.
|
||||
\param centerx The horizontal coordinate of the center of rotation
|
||||
\param zoomy The vertical coordinate of the center of rotation
|
||||
\param smooth Antialiasing flag; set to SMOOTHING_ON to enable.
|
||||
\param flipx Set to 1 to flip the image horizontally
|
||||
\param flipy Set to 1 to flip the image vertically
|
||||
\param dstwidth The destination surface width
|
||||
\param dstheight The destination surface height
|
||||
\param rect_dest The destination rect bounding box
|
||||
\param cangle The angle cosine
|
||||
\param sangle The angle sine
|
||||
\param center The true coordinate of the center of rotation
|
||||
\return The new rotated surface.
|
||||
|
||||
*/
|
||||
|
||||
SDL_Surface *
|
||||
SDLgfx_rotateSurface(SDL_Surface * src, double angle, int centerx, int centery, int smooth, int flipx, int flipy, int dstwidth, int dstheight, double cangle, double sangle)
|
||||
SDLgfx_rotateSurface(SDL_Surface * src, double angle, int smooth, int flipx, int flipy,
|
||||
const SDL_Rect *rect_dest, double cangle, double sangle, const SDL_FPoint *center)
|
||||
{
|
||||
SDL_Surface *rz_dst;
|
||||
int is8bit, angle90;
|
||||
|
|
@ -433,13 +476,12 @@ SDLgfx_rotateSurface(SDL_Surface * src, double angle, int centerx, int centery,
|
|||
colorKeyAvailable = SDL_TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
/* This function requires a 32-bit surface or 8-bit surface with a colorkey */
|
||||
is8bit = src->format->BitsPerPixel == 8 && colorKeyAvailable;
|
||||
if (!(is8bit || (src->format->BitsPerPixel == 32 && src->format->Amask)))
|
||||
return NULL;
|
||||
|
||||
/* Calculate target factors from sin/cos and zoom */
|
||||
/* Calculate target factors from sine/cosine and zoom */
|
||||
sangleinv = sangle*65536.0;
|
||||
cangleinv = cangle*65536.0;
|
||||
|
||||
|
|
@ -447,16 +489,18 @@ SDLgfx_rotateSurface(SDL_Surface * src, double angle, int centerx, int centery,
|
|||
rz_dst = NULL;
|
||||
if (is8bit) {
|
||||
/* Target surface is 8 bit */
|
||||
rz_dst = SDL_CreateRGBSurface(0, dstwidth, dstheight + GUARD_ROWS, 8, 0, 0, 0, 0);
|
||||
rz_dst = SDL_CreateRGBSurfaceWithFormat(0, rect_dest->w, rect_dest->h + GUARD_ROWS, 8, src->format->format);
|
||||
if (rz_dst != NULL) {
|
||||
for (i = 0; i < src->format->palette->ncolors; i++) {
|
||||
rz_dst->format->palette->colors[i] = src->format->palette->colors[i];
|
||||
if (src->format->palette) {
|
||||
for (i = 0; i < src->format->palette->ncolors; i++) {
|
||||
rz_dst->format->palette->colors[i] = src->format->palette->colors[i];
|
||||
}
|
||||
rz_dst->format->palette->ncolors = src->format->palette->ncolors;
|
||||
}
|
||||
rz_dst->format->palette->ncolors = src->format->palette->ncolors;
|
||||
}
|
||||
} else {
|
||||
/* Target surface is 32 bit with source RGBA ordering */
|
||||
rz_dst = SDL_CreateRGBSurface(0, dstwidth, dstheight + GUARD_ROWS, 32,
|
||||
rz_dst = SDL_CreateRGBSurface(0, rect_dest->w, rect_dest->h + GUARD_ROWS, 32,
|
||||
src->format->Rmask, src->format->Gmask,
|
||||
src->format->Bmask, src->format->Amask);
|
||||
}
|
||||
|
|
@ -466,7 +510,7 @@ SDLgfx_rotateSurface(SDL_Surface * src, double angle, int centerx, int centery,
|
|||
return NULL;
|
||||
|
||||
/* Adjust for guard rows */
|
||||
rz_dst->h = dstheight;
|
||||
rz_dst->h = rect_dest->h;
|
||||
|
||||
SDL_GetSurfaceBlendMode(src, &blendmode);
|
||||
|
||||
|
|
@ -497,7 +541,7 @@ SDLgfx_rotateSurface(SDL_Surface * src, double angle, int centerx, int centery,
|
|||
}
|
||||
|
||||
/* check if the rotation is a multiple of 90 degrees so we can take a fast path and also somewhat reduce
|
||||
* the off-by-one problem in _transformSurfaceRGBA that expresses itself when the rotation is near
|
||||
* the off-by-one problem in transformSurfaceRGBA that expresses itself when the rotation is near
|
||||
* multiples of 90 degrees.
|
||||
*/
|
||||
angle90 = (int)(angle/90);
|
||||
|
|
@ -513,16 +557,16 @@ SDLgfx_rotateSurface(SDL_Surface * src, double angle, int centerx, int centery,
|
|||
if(angle90 >= 0) {
|
||||
transformSurfaceY90(src, rz_dst, angle90, flipx, flipy);
|
||||
} else {
|
||||
transformSurfaceY(src, rz_dst, centerx, centery, (int)sangleinv, (int)cangleinv,
|
||||
flipx, flipy);
|
||||
transformSurfaceY(src, rz_dst, (int)sangleinv, (int)cangleinv,
|
||||
flipx, flipy, rect_dest, center);
|
||||
}
|
||||
} else {
|
||||
/* Call the 32-bit transformation routine to do the rotation */
|
||||
if (angle90 >= 0) {
|
||||
transformSurfaceRGBA90(src, rz_dst, angle90, flipx, flipy);
|
||||
} else {
|
||||
_transformSurfaceRGBA(src, rz_dst, centerx, centery, (int)sangleinv, (int)cangleinv,
|
||||
flipx, flipy, smooth);
|
||||
transformSurfaceRGBA(src, rz_dst, (int)sangleinv, (int)cangleinv,
|
||||
flipx, flipy, smooth, rect_dest, center);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
|
||||
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
|
|
@ -26,7 +26,9 @@
|
|||
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
|
||||
#endif
|
||||
|
||||
extern SDL_Surface *SDLgfx_rotateSurface(SDL_Surface * src, double angle, int centerx, int centery, int smooth, int flipx, int flipy, int dstwidth, int dstheight, double cangle, double sangle);
|
||||
extern void SDLgfx_rotozoomSurfaceSizeTrig(int width, int height, double angle, int *dstwidth, int *dstheight, double *cangle, double *sangle);
|
||||
extern SDL_Surface *SDLgfx_rotateSurface(SDL_Surface * src, double angle, int smooth, int flipx, int flipy,
|
||||
const SDL_Rect *rect_dest, double cangle, double sangle, const SDL_FPoint *center);
|
||||
extern void SDLgfx_rotozoomSurfaceSizeTrig(int width, int height, double angle, const SDL_FPoint *center,
|
||||
SDL_Rect *rect_dest, double *cangle, double *sangle);
|
||||
|
||||
#endif /* SDL_rotate_h_ */
|
||||
|
|
|
|||
888
Engine/lib/sdl/src/render/software/SDL_triangle.c
Normal file
888
Engine/lib/sdl/src/render/software/SDL_triangle.c
Normal file
|
|
@ -0,0 +1,888 @@
|
|||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
#include "../../SDL_internal.h"
|
||||
|
||||
#if SDL_VIDEO_RENDER_SW && !SDL_RENDER_DISABLED
|
||||
|
||||
#include "SDL_surface.h"
|
||||
#include "SDL_triangle.h"
|
||||
|
||||
#include "../../video/SDL_blit.h"
|
||||
|
||||
/* fixed points bits precision
|
||||
* Set to 1, so that it can start rendering wth middle of a pixel precision.
|
||||
* It doesn't need to be increased.
|
||||
* But, if increased too much, it overflows (srcx, srcy) coordinates used for filling with texture.
|
||||
* (which could be turned to int64).
|
||||
*/
|
||||
#define FP_BITS 1
|
||||
|
||||
|
||||
#define COLOR_EQ(c1, c2) ((c1).r == (c2).r && (c1).g == (c2).g && (c1).b == (c2).b && (c1).a == (c2).a)
|
||||
|
||||
static void SDL_BlitTriangle_Slow(SDL_BlitInfo * info,
|
||||
SDL_Point s2_x_area, SDL_Rect dstrect, int area, int bias_w0, int bias_w1, int bias_w2,
|
||||
int d2d1_y, int d1d2_x, int d0d2_y, int d2d0_x, int d1d0_y, int d0d1_x,
|
||||
int s2s0_x, int s2s1_x, int s2s0_y, int s2s1_y, int w0_row, int w1_row, int w2_row,
|
||||
SDL_Color c0, SDL_Color c1, SDL_Color c2, int is_uniform);
|
||||
|
||||
#if 0
|
||||
int SDL_BlitTriangle(SDL_Surface *src, const SDL_Point srcpoints[3], SDL_Surface *dst, const SDL_Point dstpoints[3])
|
||||
{
|
||||
int i;
|
||||
SDL_Point points[6];
|
||||
|
||||
if (src == NULL || dst == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (i = 0; i < 3; i++) {
|
||||
if (srcpoints[i].x < 0 || srcpoints[i].y < 0 || srcpoints[i].x >= src->w || srcpoints[i].y >= src->h) {
|
||||
return SDL_SetError("Values of 'srcpoints' out of bounds");
|
||||
}
|
||||
}
|
||||
|
||||
points[0] = srcpoints[0];
|
||||
points[1] = dstpoints[0];
|
||||
points[2] = srcpoints[1];
|
||||
points[3] = dstpoints[1];
|
||||
points[4] = srcpoints[2];
|
||||
points[5] = dstpoints[2];
|
||||
for (i = 0; i < 3; i++) {
|
||||
trianglepoint_2_fixedpoint(&points[2 * i + 1]);
|
||||
}
|
||||
return SDL_SW_BlitTriangle(src, dst, points);
|
||||
}
|
||||
|
||||
int SDL_FillTriangle(SDL_Surface *dst, const SDL_Point points[3], Uint32 color)
|
||||
{
|
||||
int i;
|
||||
SDL_Point points_tmp[3];
|
||||
if (dst == NULL) {
|
||||
return -1;
|
||||
}
|
||||
for (i = 0; i < 3; i++) {
|
||||
points_tmp[i] = points[i];
|
||||
trianglepoint_2_fixedpoint(&points_tmp[i]);
|
||||
}
|
||||
return SDL_SW_FillTriangle(dst, points_tmp, SDL_BLENDMODE_NONE, color);
|
||||
}
|
||||
#endif
|
||||
|
||||
/* cross product AB x AC */
|
||||
static int cross_product(const SDL_Point *a, const SDL_Point *b, int c_x, int c_y)
|
||||
{
|
||||
return (b->x - a->x) * (c_y - a->y) - (b->y - a->y) * (c_x - a->x);
|
||||
}
|
||||
|
||||
/* check for top left rules */
|
||||
static int is_top_left(const SDL_Point *a, const SDL_Point *b, int is_clockwise)
|
||||
{
|
||||
if (is_clockwise) {
|
||||
if (a->y == b->y && a->x < b->x) {
|
||||
return 1;
|
||||
}
|
||||
if (b->y < a->y) {
|
||||
return 1;
|
||||
}
|
||||
} else {
|
||||
if (a->y == b->y && b->x < a->x) {
|
||||
return 1;
|
||||
}
|
||||
if (a->y < b->y) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void trianglepoint_2_fixedpoint(SDL_Point *a) {
|
||||
a->x <<= FP_BITS;
|
||||
a->y <<= FP_BITS;
|
||||
}
|
||||
|
||||
/* bounding rect of three points (in fixed point) */
|
||||
static void bounding_rect_fixedpoint(const SDL_Point *a, const SDL_Point *b, const SDL_Point *c, SDL_Rect *r)
|
||||
{
|
||||
int min_x = SDL_min(a->x, SDL_min(b->x, c->x));
|
||||
int max_x = SDL_max(a->x, SDL_max(b->x, c->x));
|
||||
int min_y = SDL_min(a->y, SDL_min(b->y, c->y));
|
||||
int max_y = SDL_max(a->y, SDL_max(b->y, c->y));
|
||||
/* points are in fixed point, shift back */
|
||||
r->x = min_x >> FP_BITS;
|
||||
r->y = min_y >> FP_BITS;
|
||||
r->w = (max_x - min_x) >> FP_BITS;
|
||||
r->h = (max_y - min_y) >> FP_BITS;
|
||||
}
|
||||
|
||||
/* bounding rect of three points */
|
||||
static void bounding_rect(const SDL_Point *a, const SDL_Point *b, const SDL_Point *c, SDL_Rect *r)
|
||||
{
|
||||
int min_x = SDL_min(a->x, SDL_min(b->x, c->x));
|
||||
int max_x = SDL_max(a->x, SDL_max(b->x, c->x));
|
||||
int min_y = SDL_min(a->y, SDL_min(b->y, c->y));
|
||||
int max_y = SDL_max(a->y, SDL_max(b->y, c->y));
|
||||
r->x = min_x;
|
||||
r->y = min_y;
|
||||
r->w = (max_x - min_x);
|
||||
r->h = (max_y - min_y);
|
||||
}
|
||||
|
||||
|
||||
/* Triangle rendering, using Barycentric coordinates (w0, w1, w2)
|
||||
*
|
||||
* The cross product isn't computed from scratch at each iteration,
|
||||
* but optimized using constant step increments
|
||||
*
|
||||
*/
|
||||
|
||||
#define TRIANGLE_BEGIN_LOOP \
|
||||
{ \
|
||||
int x, y; \
|
||||
for (y = 0; y < dstrect.h; y++) { \
|
||||
/* y start */ \
|
||||
int w0 = w0_row; \
|
||||
int w1 = w1_row; \
|
||||
int w2 = w2_row; \
|
||||
for (x = 0; x < dstrect.w; x++) { \
|
||||
/* In triangle */ \
|
||||
if (w0 + bias_w0 >= 0 && w1 + bias_w1 >= 0 && w2 + bias_w2 >= 0) { \
|
||||
Uint8 *dptr = (Uint8 *) dst_ptr + x * dstbpp; \
|
||||
|
||||
|
||||
/* Use 64 bits precision to prevent overflow when interpolating color / texture with wide triangles */
|
||||
#define TRIANGLE_GET_TEXTCOORD \
|
||||
int srcx = (int)(((Sint64)w0 * s2s0_x + (Sint64)w1 * s2s1_x + s2_x_area.x) / area); \
|
||||
int srcy = (int)(((Sint64)w0 * s2s0_y + (Sint64)w1 * s2s1_y + s2_x_area.y) / area); \
|
||||
|
||||
#define TRIANGLE_GET_MAPPED_COLOR \
|
||||
int r = (int)(((Sint64)w0 * c0.r + (Sint64)w1 * c1.r + (Sint64)w2 * c2.r) / area); \
|
||||
int g = (int)(((Sint64)w0 * c0.g + (Sint64)w1 * c1.g + (Sint64)w2 * c2.g) / area); \
|
||||
int b = (int)(((Sint64)w0 * c0.b + (Sint64)w1 * c1.b + (Sint64)w2 * c2.b) / area); \
|
||||
int a = (int)(((Sint64)w0 * c0.a + (Sint64)w1 * c1.a + (Sint64)w2 * c2.a) / area); \
|
||||
int color = SDL_MapRGBA(format, r, g, b, a); \
|
||||
|
||||
#define TRIANGLE_GET_COLOR \
|
||||
int r = (int)(((Sint64)w0 * c0.r + (Sint64)w1 * c1.r + (Sint64)w2 * c2.r) / area); \
|
||||
int g = (int)(((Sint64)w0 * c0.g + (Sint64)w1 * c1.g + (Sint64)w2 * c2.g) / area); \
|
||||
int b = (int)(((Sint64)w0 * c0.b + (Sint64)w1 * c1.b + (Sint64)w2 * c2.b) / area); \
|
||||
int a = (int)(((Sint64)w0 * c0.a + (Sint64)w1 * c1.a + (Sint64)w2 * c2.a) / area); \
|
||||
|
||||
|
||||
#define TRIANGLE_END_LOOP \
|
||||
} \
|
||||
/* x += 1 */ \
|
||||
w0 += d2d1_y; \
|
||||
w1 += d0d2_y; \
|
||||
w2 += d1d0_y; \
|
||||
} \
|
||||
/* y += 1 */ \
|
||||
w0_row += d1d2_x; \
|
||||
w1_row += d2d0_x; \
|
||||
w2_row += d0d1_x; \
|
||||
dst_ptr += dst_pitch; \
|
||||
} \
|
||||
} \
|
||||
|
||||
int SDL_SW_FillTriangle(SDL_Surface *dst, SDL_Point *d0, SDL_Point *d1, SDL_Point *d2, SDL_BlendMode blend, SDL_Color c0, SDL_Color c1, SDL_Color c2)
|
||||
{
|
||||
int ret = 0;
|
||||
int dst_locked = 0;
|
||||
|
||||
SDL_Rect dstrect;
|
||||
|
||||
int dstbpp;
|
||||
Uint8 *dst_ptr;
|
||||
int dst_pitch;
|
||||
|
||||
int area, is_clockwise;
|
||||
|
||||
int d2d1_y, d1d2_x, d0d2_y, d2d0_x, d1d0_y, d0d1_x;
|
||||
int w0_row, w1_row, w2_row;
|
||||
int bias_w0, bias_w1, bias_w2;
|
||||
|
||||
int is_uniform;
|
||||
|
||||
SDL_Surface *tmp = NULL;
|
||||
|
||||
if (dst == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
area = cross_product(d0, d1, d2->x, d2->y);
|
||||
|
||||
is_uniform = COLOR_EQ(c0, c1) && COLOR_EQ(c1, c2);
|
||||
|
||||
/* Flat triangle */
|
||||
if (area == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Lock the destination, if needed */
|
||||
if (SDL_MUSTLOCK(dst)) {
|
||||
if (SDL_LockSurface(dst) < 0) {
|
||||
ret = -1;
|
||||
goto end;
|
||||
} else {
|
||||
dst_locked = 1;
|
||||
}
|
||||
}
|
||||
|
||||
bounding_rect_fixedpoint(d0, d1, d2, &dstrect);
|
||||
|
||||
{
|
||||
/* Clip triangle rect with surface rect */
|
||||
SDL_Rect rect;
|
||||
rect.x = 0;
|
||||
rect.y = 0;
|
||||
rect.w = dst->w;
|
||||
rect.h = dst->h;
|
||||
SDL_IntersectRect(&dstrect, &rect, &dstrect);
|
||||
}
|
||||
|
||||
{
|
||||
/* Clip triangle with surface clip rect */
|
||||
SDL_Rect rect;
|
||||
SDL_GetClipRect(dst, &rect);
|
||||
SDL_IntersectRect(&dstrect, &rect, &dstrect);
|
||||
}
|
||||
|
||||
|
||||
if (blend != SDL_BLENDMODE_NONE) {
|
||||
int format = dst->format->format;
|
||||
|
||||
/* need an alpha format */
|
||||
if (! dst->format->Amask) {
|
||||
format = SDL_PIXELFORMAT_ARGB8888;
|
||||
}
|
||||
|
||||
/* Use an intermediate surface */
|
||||
tmp = SDL_CreateRGBSurfaceWithFormat(0, dstrect.w, dstrect.h, 0, format);
|
||||
if (tmp == NULL) {
|
||||
ret = -1;
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (blend == SDL_BLENDMODE_MOD) {
|
||||
Uint32 c = SDL_MapRGBA(tmp->format, 255, 255, 255, 255);
|
||||
SDL_FillRect(tmp, NULL, c);
|
||||
}
|
||||
|
||||
SDL_SetSurfaceBlendMode(tmp, blend);
|
||||
|
||||
dstbpp = tmp->format->BytesPerPixel;
|
||||
dst_ptr = tmp->pixels;
|
||||
dst_pitch = tmp->pitch;
|
||||
|
||||
} else {
|
||||
/* Write directly to destination surface */
|
||||
dstbpp = dst->format->BytesPerPixel;
|
||||
dst_ptr = (Uint8 *)dst->pixels + dstrect.x * dstbpp + dstrect.y * dst->pitch;
|
||||
dst_pitch = dst->pitch;
|
||||
}
|
||||
|
||||
is_clockwise = area > 0;
|
||||
area = SDL_abs(area);
|
||||
|
||||
d2d1_y = (d1->y - d2->y) << FP_BITS;
|
||||
d0d2_y = (d2->y - d0->y) << FP_BITS;
|
||||
d1d0_y = (d0->y - d1->y) << FP_BITS;
|
||||
d1d2_x = (d2->x - d1->x) << FP_BITS;
|
||||
d2d0_x = (d0->x - d2->x) << FP_BITS;
|
||||
d0d1_x = (d1->x - d0->x) << FP_BITS;
|
||||
|
||||
/* Starting point for rendering, at the middle of a pixel */
|
||||
{
|
||||
SDL_Point p;
|
||||
p.x = dstrect.x;
|
||||
p.y = dstrect.y;
|
||||
trianglepoint_2_fixedpoint(&p);
|
||||
p.x += (1 << FP_BITS) / 2;
|
||||
p.y += (1 << FP_BITS) / 2;
|
||||
w0_row = cross_product(d1, d2, p.x, p.y);
|
||||
w1_row = cross_product(d2, d0, p.x, p.y);
|
||||
w2_row = cross_product(d0, d1, p.x, p.y);
|
||||
}
|
||||
|
||||
/* Handle anti-clockwise triangles */
|
||||
if (! is_clockwise) {
|
||||
d2d1_y *= -1;
|
||||
d0d2_y *= -1;
|
||||
d1d0_y *= -1;
|
||||
d1d2_x *= -1;
|
||||
d2d0_x *= -1;
|
||||
d0d1_x *= -1;
|
||||
w0_row *= -1;
|
||||
w1_row *= -1;
|
||||
w2_row *= -1;
|
||||
}
|
||||
|
||||
/* Add a bias to respect top-left rasterization rule */
|
||||
bias_w0 = (is_top_left(d1, d2, is_clockwise) ? 0 : -1);
|
||||
bias_w1 = (is_top_left(d2, d0, is_clockwise) ? 0 : -1);
|
||||
bias_w2 = (is_top_left(d0, d1, is_clockwise) ? 0 : -1);
|
||||
|
||||
if (is_uniform) {
|
||||
Uint32 color;
|
||||
if (tmp) {
|
||||
if (dst->format->Amask) {
|
||||
color = SDL_MapRGBA(tmp->format, c0.r, c0.g, c0.b, c0.a);
|
||||
} else {
|
||||
//color = SDL_MapRGB(tmp->format, c0.r, c0.g, c0.b);
|
||||
color = SDL_MapRGBA(tmp->format, c0.r, c0.g, c0.b, c0.a);
|
||||
}
|
||||
} else {
|
||||
color = SDL_MapRGBA(dst->format, c0.r, c0.g, c0.b, c0.a);
|
||||
}
|
||||
|
||||
if (dstbpp == 4) {
|
||||
TRIANGLE_BEGIN_LOOP
|
||||
{
|
||||
*(Uint32 *)dptr = color;
|
||||
}
|
||||
TRIANGLE_END_LOOP
|
||||
} else if (dstbpp == 3) {
|
||||
TRIANGLE_BEGIN_LOOP
|
||||
{
|
||||
Uint8 *s = (Uint8*)&color;
|
||||
dptr[0] = s[0];
|
||||
dptr[1] = s[1];
|
||||
dptr[2] = s[2];
|
||||
}
|
||||
TRIANGLE_END_LOOP
|
||||
} else if (dstbpp == 2) {
|
||||
TRIANGLE_BEGIN_LOOP
|
||||
{
|
||||
*(Uint16 *)dptr = color;
|
||||
}
|
||||
TRIANGLE_END_LOOP
|
||||
} else if (dstbpp == 1) {
|
||||
TRIANGLE_BEGIN_LOOP
|
||||
{
|
||||
*dptr = color;
|
||||
}
|
||||
TRIANGLE_END_LOOP
|
||||
}
|
||||
} else {
|
||||
SDL_PixelFormat *format = dst->format;
|
||||
if (tmp) {
|
||||
format = tmp->format;
|
||||
}
|
||||
if (dstbpp == 4) {
|
||||
TRIANGLE_BEGIN_LOOP
|
||||
{
|
||||
TRIANGLE_GET_MAPPED_COLOR
|
||||
*(Uint32 *)dptr = color;
|
||||
}
|
||||
TRIANGLE_END_LOOP
|
||||
} else if (dstbpp == 3) {
|
||||
TRIANGLE_BEGIN_LOOP
|
||||
{
|
||||
TRIANGLE_GET_MAPPED_COLOR
|
||||
Uint8 *s = (Uint8*)&color;
|
||||
dptr[0] = s[0];
|
||||
dptr[1] = s[1];
|
||||
dptr[2] = s[2];
|
||||
}
|
||||
TRIANGLE_END_LOOP
|
||||
} else if (dstbpp == 2) {
|
||||
TRIANGLE_BEGIN_LOOP
|
||||
{
|
||||
TRIANGLE_GET_MAPPED_COLOR
|
||||
*(Uint16 *)dptr = color;
|
||||
}
|
||||
TRIANGLE_END_LOOP
|
||||
} else if (dstbpp == 1) {
|
||||
TRIANGLE_BEGIN_LOOP
|
||||
{
|
||||
TRIANGLE_GET_MAPPED_COLOR
|
||||
*dptr = color;
|
||||
}
|
||||
TRIANGLE_END_LOOP
|
||||
}
|
||||
}
|
||||
|
||||
if (tmp) {
|
||||
SDL_BlitSurface(tmp, NULL, dst, &dstrect);
|
||||
SDL_FreeSurface(tmp);
|
||||
}
|
||||
|
||||
end:
|
||||
if (dst_locked) {
|
||||
SDL_UnlockSurface(dst);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
int SDL_SW_BlitTriangle(
|
||||
SDL_Surface *src,
|
||||
SDL_Point *s0, SDL_Point *s1, SDL_Point *s2,
|
||||
SDL_Surface *dst,
|
||||
SDL_Point *d0, SDL_Point *d1, SDL_Point *d2,
|
||||
SDL_Color c0, SDL_Color c1, SDL_Color c2)
|
||||
{
|
||||
int ret = 0;
|
||||
int src_locked = 0;
|
||||
int dst_locked = 0;
|
||||
|
||||
SDL_BlendMode blend;
|
||||
|
||||
SDL_Rect dstrect;
|
||||
|
||||
SDL_Point s2_x_area;
|
||||
|
||||
int dstbpp;
|
||||
Uint8 *dst_ptr;
|
||||
int dst_pitch;
|
||||
|
||||
int *src_ptr;
|
||||
int src_pitch;
|
||||
|
||||
int area, is_clockwise;
|
||||
|
||||
int d2d1_y, d1d2_x, d0d2_y, d2d0_x, d1d0_y, d0d1_x;
|
||||
int s2s0_x, s2s1_x, s2s0_y, s2s1_y;
|
||||
|
||||
int w0_row, w1_row, w2_row;
|
||||
int bias_w0, bias_w1, bias_w2;
|
||||
|
||||
int is_uniform;
|
||||
|
||||
int has_modulation;
|
||||
|
||||
if (src == NULL || dst == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
area = cross_product(d0, d1, d2->x, d2->y);
|
||||
|
||||
/* Flat triangle */
|
||||
if (area == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Lock the destination, if needed */
|
||||
if (SDL_MUSTLOCK(dst)) {
|
||||
if (SDL_LockSurface(dst) < 0) {
|
||||
ret = -1;
|
||||
goto end;
|
||||
} else {
|
||||
dst_locked = 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Lock the source, if needed */
|
||||
if (SDL_MUSTLOCK(src)) {
|
||||
if (SDL_LockSurface(src) < 0) {
|
||||
ret = -1;
|
||||
goto end;
|
||||
} else {
|
||||
src_locked = 1;
|
||||
}
|
||||
}
|
||||
|
||||
is_uniform = COLOR_EQ(c0, c1) && COLOR_EQ(c1, c2);
|
||||
|
||||
bounding_rect_fixedpoint(d0, d1, d2, &dstrect);
|
||||
|
||||
SDL_GetSurfaceBlendMode(src, &blend);
|
||||
|
||||
/* TRIANGLE_GET_TEXTCOORD interpolates up to the max values included, so reduce by 1 */
|
||||
{
|
||||
SDL_Rect srcrect;
|
||||
int maxx, maxy;
|
||||
bounding_rect(s0, s1, s2, &srcrect);
|
||||
maxx = srcrect.x + srcrect.w;
|
||||
maxy = srcrect.y + srcrect.h;
|
||||
if (srcrect.w > 0) {
|
||||
if (s0->x == maxx) s0->x--;
|
||||
if (s1->x == maxx) s1->x--;
|
||||
if (s2->x == maxx) s2->x--;
|
||||
}
|
||||
if (srcrect.h > 0) {
|
||||
if (s0->y == maxy) s0->y--;
|
||||
if (s1->y == maxy) s1->y--;
|
||||
if (s2->y == maxy) s2->y--;
|
||||
}
|
||||
}
|
||||
|
||||
if (is_uniform) {
|
||||
// SDL_GetSurfaceColorMod(src, &r, &g, &b);
|
||||
has_modulation = c0.r != 255 || c0.g != 255 || c0.b != 255 || c0.a != 255;;
|
||||
} else {
|
||||
has_modulation = SDL_TRUE;
|
||||
}
|
||||
|
||||
{
|
||||
/* Clip triangle rect with surface rect */
|
||||
SDL_Rect rect;
|
||||
rect.x = 0;
|
||||
rect.y = 0;
|
||||
rect.w = dst->w;
|
||||
rect.h = dst->h;
|
||||
|
||||
SDL_IntersectRect(&dstrect, &rect, &dstrect);
|
||||
}
|
||||
|
||||
{
|
||||
/* Clip triangle with surface clip rect */
|
||||
SDL_Rect rect;
|
||||
SDL_GetClipRect(dst, &rect);
|
||||
SDL_IntersectRect(&dstrect, &rect, &dstrect);
|
||||
}
|
||||
|
||||
/* Set destination pointer */
|
||||
dstbpp = dst->format->BytesPerPixel;
|
||||
dst_ptr = (Uint8 *)dst->pixels + dstrect.x * dstbpp + dstrect.y * dst->pitch;
|
||||
dst_pitch = dst->pitch;
|
||||
|
||||
/* Set source pointer */
|
||||
src_ptr = src->pixels;
|
||||
src_pitch = src->pitch;
|
||||
|
||||
is_clockwise = area > 0;
|
||||
area = SDL_abs(area);
|
||||
|
||||
d2d1_y = (d1->y - d2->y) << FP_BITS;
|
||||
d0d2_y = (d2->y - d0->y) << FP_BITS;
|
||||
d1d0_y = (d0->y - d1->y) << FP_BITS;
|
||||
|
||||
|
||||
d1d2_x = (d2->x - d1->x) << FP_BITS;
|
||||
d2d0_x = (d0->x - d2->x) << FP_BITS;
|
||||
d0d1_x = (d1->x - d0->x) << FP_BITS;
|
||||
|
||||
s2s0_x = s0->x - s2->x;
|
||||
s2s1_x = s1->x - s2->x;
|
||||
s2s0_y = s0->y - s2->y;
|
||||
s2s1_y = s1->y - s2->y;
|
||||
|
||||
/* Starting point for rendering, at the middle of a pixel */
|
||||
{
|
||||
SDL_Point p;
|
||||
p.x = dstrect.x;
|
||||
p.y = dstrect.y;
|
||||
trianglepoint_2_fixedpoint(&p);
|
||||
p.x += (1 << FP_BITS) / 2;
|
||||
p.y += (1 << FP_BITS) / 2;
|
||||
w0_row = cross_product(d1, d2, p.x, p.y);
|
||||
w1_row = cross_product(d2, d0, p.x, p.y);
|
||||
w2_row = cross_product(d0, d1, p.x, p.y);
|
||||
}
|
||||
|
||||
/* Handle anti-clockwise triangles */
|
||||
if (! is_clockwise) {
|
||||
d2d1_y *= -1;
|
||||
d0d2_y *= -1;
|
||||
d1d0_y *= -1;
|
||||
d1d2_x *= -1;
|
||||
d2d0_x *= -1;
|
||||
d0d1_x *= -1;
|
||||
w0_row *= -1;
|
||||
w1_row *= -1;
|
||||
w2_row *= -1;
|
||||
}
|
||||
|
||||
/* Add a bias to respect top-left rasterization rule */
|
||||
bias_w0 = (is_top_left(d1, d2, is_clockwise) ? 0 : -1);
|
||||
bias_w1 = (is_top_left(d2, d0, is_clockwise) ? 0 : -1);
|
||||
bias_w2 = (is_top_left(d0, d1, is_clockwise) ? 0 : -1);
|
||||
|
||||
/* precompute constant 's2->x * area' used in TRIANGLE_GET_TEXTCOORD */
|
||||
s2_x_area.x = s2->x * area;
|
||||
s2_x_area.y = s2->y * area;
|
||||
|
||||
if (blend != SDL_BLENDMODE_NONE || src->format->format != dst->format->format || has_modulation || ! is_uniform) {
|
||||
/* Use SDL_BlitTriangle_Slow */
|
||||
|
||||
SDL_BlitInfo *info = &src->map->info;
|
||||
SDL_BlitInfo tmp_info;
|
||||
|
||||
SDL_zero(tmp_info);
|
||||
|
||||
tmp_info.src_fmt = src->format;
|
||||
tmp_info.dst_fmt = dst->format;
|
||||
tmp_info.flags = info->flags;
|
||||
/*
|
||||
tmp_info.r = info->r;
|
||||
tmp_info.g = info->g;
|
||||
tmp_info.b = info->b;
|
||||
tmp_info.a = info->a;
|
||||
*/
|
||||
tmp_info.r = c0.r;
|
||||
tmp_info.g = c0.g;
|
||||
tmp_info.b = c0.b;
|
||||
tmp_info.a = c0.a;
|
||||
|
||||
|
||||
tmp_info.flags &= ~(SDL_COPY_MODULATE_COLOR | SDL_COPY_MODULATE_ALPHA);
|
||||
|
||||
if (c0.r != 255 || c1.r != 255 || c2.r != 255 ||
|
||||
c0.g != 255 || c1.g != 255 || c2.g != 255 ||
|
||||
c0.b != 255 || c1.b != 255 || c2.b != 255) {
|
||||
tmp_info.flags |= SDL_COPY_MODULATE_COLOR;
|
||||
}
|
||||
|
||||
if (c0.a != 255 || c1.a != 255 || c2.a != 255) {
|
||||
tmp_info.flags |= SDL_COPY_MODULATE_ALPHA;
|
||||
}
|
||||
|
||||
tmp_info.colorkey = info->colorkey;
|
||||
|
||||
/* src */
|
||||
tmp_info.src = (Uint8 *) src_ptr;
|
||||
tmp_info.src_pitch = src_pitch;
|
||||
|
||||
/* dst */
|
||||
tmp_info.dst = (Uint8 *) dst_ptr;
|
||||
tmp_info.dst_pitch = dst_pitch;
|
||||
|
||||
SDL_BlitTriangle_Slow(&tmp_info, s2_x_area, dstrect, area, bias_w0, bias_w1, bias_w2,
|
||||
d2d1_y, d1d2_x, d0d2_y, d2d0_x, d1d0_y, d0d1_x,
|
||||
s2s0_x, s2s1_x, s2s0_y, s2s1_y, w0_row, w1_row, w2_row,
|
||||
c0, c1, c2, is_uniform);
|
||||
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (dstbpp == 4) {
|
||||
TRIANGLE_BEGIN_LOOP
|
||||
{
|
||||
TRIANGLE_GET_TEXTCOORD
|
||||
Uint32 *sptr = (Uint32 *)((Uint8 *) src_ptr + srcy * src_pitch);
|
||||
*(Uint32 *)dptr = sptr[srcx];
|
||||
}
|
||||
TRIANGLE_END_LOOP
|
||||
} else if (dstbpp == 3) {
|
||||
TRIANGLE_BEGIN_LOOP
|
||||
{
|
||||
TRIANGLE_GET_TEXTCOORD
|
||||
Uint8 *sptr = (Uint8 *)((Uint8 *) src_ptr + srcy * src_pitch);
|
||||
dptr[0] = sptr[3 * srcx];
|
||||
dptr[1] = sptr[3 * srcx + 1];
|
||||
dptr[2] = sptr[3 * srcx + 2];
|
||||
}
|
||||
TRIANGLE_END_LOOP
|
||||
} else if (dstbpp == 2) {
|
||||
TRIANGLE_BEGIN_LOOP
|
||||
{
|
||||
TRIANGLE_GET_TEXTCOORD
|
||||
Uint16 *sptr = (Uint16 *)((Uint8 *) src_ptr + srcy * src_pitch);
|
||||
*(Uint16 *)dptr = sptr[srcx];
|
||||
}
|
||||
TRIANGLE_END_LOOP
|
||||
} else if (dstbpp == 1) {
|
||||
TRIANGLE_BEGIN_LOOP
|
||||
{
|
||||
TRIANGLE_GET_TEXTCOORD
|
||||
Uint8 *sptr = (Uint8 *)((Uint8 *) src_ptr + srcy * src_pitch);
|
||||
*dptr = sptr[srcx];
|
||||
}
|
||||
TRIANGLE_END_LOOP
|
||||
}
|
||||
|
||||
end:
|
||||
if (dst_locked) {
|
||||
SDL_UnlockSurface(dst);
|
||||
}
|
||||
if (src_locked) {
|
||||
SDL_UnlockSurface(src);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
#define FORMAT_ALPHA 0
|
||||
#define FORMAT_NO_ALPHA -1
|
||||
#define FORMAT_2101010 1
|
||||
#define FORMAT_HAS_ALPHA(format) format == 0
|
||||
#define FORMAT_HAS_NO_ALPHA(format) format < 0
|
||||
static int SDL_INLINE detect_format(SDL_PixelFormat *pf) {
|
||||
if (pf->format == SDL_PIXELFORMAT_ARGB2101010) {
|
||||
return FORMAT_2101010;
|
||||
} else if (pf->Amask) {
|
||||
return FORMAT_ALPHA;
|
||||
} else {
|
||||
return FORMAT_NO_ALPHA;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
SDL_BlitTriangle_Slow(SDL_BlitInfo *info,
|
||||
SDL_Point s2_x_area, SDL_Rect dstrect, int area, int bias_w0, int bias_w1, int bias_w2,
|
||||
int d2d1_y, int d1d2_x, int d0d2_y, int d2d0_x, int d1d0_y, int d0d1_x,
|
||||
int s2s0_x, int s2s1_x, int s2s0_y, int s2s1_y, int w0_row, int w1_row, int w2_row,
|
||||
SDL_Color c0, SDL_Color c1, SDL_Color c2, int is_uniform)
|
||||
{
|
||||
const int flags = info->flags;
|
||||
Uint32 modulateR = info->r;
|
||||
Uint32 modulateG = info->g;
|
||||
Uint32 modulateB = info->b;
|
||||
Uint32 modulateA = info->a;
|
||||
Uint32 srcpixel;
|
||||
Uint32 srcR, srcG, srcB, srcA;
|
||||
Uint32 dstpixel;
|
||||
Uint32 dstR, dstG, dstB, dstA;
|
||||
SDL_PixelFormat *src_fmt = info->src_fmt;
|
||||
SDL_PixelFormat *dst_fmt = info->dst_fmt;
|
||||
int srcbpp = src_fmt->BytesPerPixel;
|
||||
int dstbpp = dst_fmt->BytesPerPixel;
|
||||
int srcfmt_val;
|
||||
int dstfmt_val;
|
||||
Uint32 rgbmask = ~src_fmt->Amask;
|
||||
Uint32 ckey = info->colorkey & rgbmask;
|
||||
|
||||
Uint8 *dst_ptr = info->dst;
|
||||
int dst_pitch = info->dst_pitch;;
|
||||
|
||||
srcfmt_val = detect_format(src_fmt);
|
||||
dstfmt_val = detect_format(dst_fmt);
|
||||
|
||||
TRIANGLE_BEGIN_LOOP
|
||||
{
|
||||
Uint8 *src;
|
||||
Uint8 *dst = dptr;
|
||||
TRIANGLE_GET_TEXTCOORD
|
||||
src = (info->src + (srcy * info->src_pitch) + (srcx * srcbpp));
|
||||
if (FORMAT_HAS_ALPHA(srcfmt_val)) {
|
||||
DISEMBLE_RGBA(src, srcbpp, src_fmt, srcpixel, srcR, srcG, srcB, srcA);
|
||||
} else if (FORMAT_HAS_NO_ALPHA(srcfmt_val)) {
|
||||
DISEMBLE_RGB(src, srcbpp, src_fmt, srcpixel, srcR, srcG, srcB);
|
||||
srcA = 0xFF;
|
||||
} else {
|
||||
/* SDL_PIXELFORMAT_ARGB2101010 */
|
||||
srcpixel = *((Uint32 *)(src));
|
||||
RGBA_FROM_ARGB2101010(srcpixel, srcR, srcG, srcB, srcA);
|
||||
}
|
||||
if (flags & SDL_COPY_COLORKEY) {
|
||||
/* srcpixel isn't set for 24 bpp */
|
||||
if (srcbpp == 3) {
|
||||
srcpixel = (srcR << src_fmt->Rshift) |
|
||||
(srcG << src_fmt->Gshift) | (srcB << src_fmt->Bshift);
|
||||
}
|
||||
if ((srcpixel & rgbmask) == ckey) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (FORMAT_HAS_ALPHA(dstfmt_val)) {
|
||||
DISEMBLE_RGBA(dst, dstbpp, dst_fmt, dstpixel, dstR, dstG, dstB, dstA);
|
||||
} else if (FORMAT_HAS_NO_ALPHA(dstfmt_val)) {
|
||||
DISEMBLE_RGB(dst, dstbpp, dst_fmt, dstpixel, dstR, dstG, dstB);
|
||||
dstA = 0xFF;
|
||||
} else {
|
||||
/* SDL_PIXELFORMAT_ARGB2101010 */
|
||||
dstpixel = *((Uint32 *)(dst));
|
||||
RGBA_FROM_ARGB2101010(dstpixel, dstR, dstG, dstB, dstA);
|
||||
}
|
||||
|
||||
if (! is_uniform) {
|
||||
TRIANGLE_GET_COLOR
|
||||
modulateR = r;
|
||||
modulateG = g;
|
||||
modulateB = b;
|
||||
modulateA = a;
|
||||
}
|
||||
|
||||
if (flags & SDL_COPY_MODULATE_COLOR) {
|
||||
srcR = (srcR * modulateR) / 255;
|
||||
srcG = (srcG * modulateG) / 255;
|
||||
srcB = (srcB * modulateB) / 255;
|
||||
}
|
||||
if (flags & SDL_COPY_MODULATE_ALPHA) {
|
||||
srcA = (srcA * modulateA) / 255;
|
||||
}
|
||||
if (flags & (SDL_COPY_BLEND | SDL_COPY_ADD)) {
|
||||
/* This goes away if we ever use premultiplied alpha */
|
||||
if (srcA < 255) {
|
||||
srcR = (srcR * srcA) / 255;
|
||||
srcG = (srcG * srcA) / 255;
|
||||
srcB = (srcB * srcA) / 255;
|
||||
}
|
||||
}
|
||||
switch (flags & (SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD | SDL_COPY_MUL)) {
|
||||
case 0:
|
||||
dstR = srcR;
|
||||
dstG = srcG;
|
||||
dstB = srcB;
|
||||
dstA = srcA;
|
||||
break;
|
||||
case SDL_COPY_BLEND:
|
||||
dstR = srcR + ((255 - srcA) * dstR) / 255;
|
||||
dstG = srcG + ((255 - srcA) * dstG) / 255;
|
||||
dstB = srcB + ((255 - srcA) * dstB) / 255;
|
||||
dstA = srcA + ((255 - srcA) * dstA) / 255;
|
||||
break;
|
||||
case SDL_COPY_ADD:
|
||||
dstR = srcR + dstR;
|
||||
if (dstR > 255)
|
||||
dstR = 255;
|
||||
dstG = srcG + dstG;
|
||||
if (dstG > 255)
|
||||
dstG = 255;
|
||||
dstB = srcB + dstB;
|
||||
if (dstB > 255)
|
||||
dstB = 255;
|
||||
break;
|
||||
case SDL_COPY_MOD:
|
||||
dstR = (srcR * dstR) / 255;
|
||||
dstG = (srcG * dstG) / 255;
|
||||
dstB = (srcB * dstB) / 255;
|
||||
break;
|
||||
case SDL_COPY_MUL:
|
||||
dstR = ((srcR * dstR) + (dstR * (255 - srcA))) / 255;
|
||||
if (dstR > 255)
|
||||
dstR = 255;
|
||||
dstG = ((srcG * dstG) + (dstG * (255 - srcA))) / 255;
|
||||
if (dstG > 255)
|
||||
dstG = 255;
|
||||
dstB = ((srcB * dstB) + (dstB * (255 - srcA))) / 255;
|
||||
if (dstB > 255)
|
||||
dstB = 255;
|
||||
dstA = ((srcA * dstA) + (dstA * (255 - srcA))) / 255;
|
||||
if (dstA > 255)
|
||||
dstA = 255;
|
||||
break;
|
||||
}
|
||||
if (FORMAT_HAS_ALPHA(dstfmt_val)) {
|
||||
ASSEMBLE_RGBA(dst, dstbpp, dst_fmt, dstR, dstG, dstB, dstA);
|
||||
} else if (FORMAT_HAS_NO_ALPHA(dstfmt_val)) {
|
||||
ASSEMBLE_RGB(dst, dstbpp, dst_fmt, dstR, dstG, dstB);
|
||||
} else {
|
||||
/* SDL_PIXELFORMAT_ARGB2101010 */
|
||||
Uint32 pixel;
|
||||
ARGB2101010_FROM_RGBA(pixel, dstR, dstG, dstB, dstA);
|
||||
*(Uint32 *)dst = pixel;
|
||||
}
|
||||
}
|
||||
TRIANGLE_END_LOOP
|
||||
}
|
||||
|
||||
#endif /* SDL_VIDEO_RENDER_SW && !SDL_RENDER_DISABLED */
|
||||
|
||||
/* vi: set ts=4 sw=4 expandtab: */
|
||||
42
Engine/lib/sdl/src/render/software/SDL_triangle.h
Normal file
42
Engine/lib/sdl/src/render/software/SDL_triangle.h
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef SDL_triangle_h_
|
||||
#define SDL_triangle_h_
|
||||
|
||||
#include "../../SDL_internal.h"
|
||||
|
||||
extern int SDL_SW_FillTriangle(SDL_Surface *dst,
|
||||
SDL_Point *d0, SDL_Point *d1, SDL_Point *d2,
|
||||
SDL_BlendMode blend, SDL_Color c0, SDL_Color c1, SDL_Color c2);
|
||||
|
||||
extern int SDL_SW_BlitTriangle(
|
||||
SDL_Surface *src,
|
||||
SDL_Point *s0, SDL_Point *s1, SDL_Point *s2,
|
||||
SDL_Surface *dst,
|
||||
SDL_Point *d0, SDL_Point *d1, SDL_Point *d2,
|
||||
SDL_Color c0, SDL_Color c1, SDL_Color c2);
|
||||
|
||||
extern void trianglepoint_2_fixedpoint(SDL_Point *a);
|
||||
|
||||
#endif /* SDL_triangle_h_ */
|
||||
|
||||
/* vi: set ts=4 sw=4 expandtab: */
|
||||
Loading…
Add table
Add a link
Reference in a new issue