SDL_RenderTexture() and SDL_RenderTextureRotated() take floating point source coordinates

See the discussion at https://discourse.libsdl.org/t/sdl-rendercopyf-uses-ints/36732/8
main
Sam Lantinga 2023-03-02 08:56:54 -08:00
parent 199a7af296
commit bd2e2ee7aa
9 changed files with 82 additions and 75 deletions

View File

@ -1282,7 +1282,7 @@ extern DECLSPEC int SDLCALL SDL_RenderFillRects(SDL_Renderer *renderer, const SD
*
* \since This function is available since SDL 3.0.0.
*/
extern DECLSPEC int SDLCALL SDL_RenderTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *srcrect, const SDL_FRect *dstrect);
extern DECLSPEC int SDLCALL SDL_RenderTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_FRect *srcrect, const SDL_FRect *dstrect);
/**
* Copy a portion of the source texture to the current rendering target, with
@ -1307,7 +1307,7 @@ extern DECLSPEC int SDLCALL SDL_RenderTexture(SDL_Renderer *renderer, SDL_Textur
* \since This function is available since SDL 3.0.0.
*/
extern DECLSPEC int SDLCALL SDL_RenderTextureRotated(SDL_Renderer *renderer, SDL_Texture *texture,
const SDL_Rect *srcrect, const SDL_FRect *dstrect,
const SDL_FRect *srcrect, const SDL_FRect *dstrect,
const double angle, const SDL_FPoint *center,
const SDL_RendererFlip flip);

View File

@ -611,8 +611,8 @@ SDL_DYNAPI_PROC(int,SDL_RenderPresent,(SDL_Renderer *a),(a),return)
SDL_DYNAPI_PROC(int,SDL_RenderReadPixels,(SDL_Renderer *a, const SDL_Rect *b, Uint32 c, void *d, int e),(a,b,c,d,e),return)
SDL_DYNAPI_PROC(int,SDL_RenderRect,(SDL_Renderer *a, const SDL_FRect *b),(a,b),return)
SDL_DYNAPI_PROC(int,SDL_RenderRects,(SDL_Renderer *a, const SDL_FRect *b, int c),(a,b,c),return)
SDL_DYNAPI_PROC(int,SDL_RenderTexture,(SDL_Renderer *a, SDL_Texture *b, const SDL_Rect *c, const SDL_FRect *d),(a,b,c,d),return)
SDL_DYNAPI_PROC(int,SDL_RenderTextureRotated,(SDL_Renderer *a, SDL_Texture *b, const SDL_Rect *c, const SDL_FRect *d, const double e, const SDL_FPoint *f, const SDL_RendererFlip g),(a,b,c,d,e,f,g),return)
SDL_DYNAPI_PROC(int,SDL_RenderTexture,(SDL_Renderer *a, SDL_Texture *b, const SDL_FRect *c, const SDL_FRect *d),(a,b,c,d),return)
SDL_DYNAPI_PROC(int,SDL_RenderTextureRotated,(SDL_Renderer *a, SDL_Texture *b, const SDL_FRect *c, const SDL_FRect *d, const double e, const SDL_FPoint *f, const SDL_RendererFlip g),(a,b,c,d,e,f,g),return)
SDL_DYNAPI_PROC(int,SDL_RenderCoordinatesFromWindow,(SDL_Renderer *a, float b, float c, float *d, float *e),(a,b,c,d,e),return)
SDL_DYNAPI_PROC(SDL_AssertState,SDL_ReportAssertion,(SDL_AssertData *a, const char *b, const char *c, int d),(a,b,c,d),return)
SDL_DYNAPI_PROC(void,SDL_ResetAssertionReport,(void),(),)

View File

@ -599,7 +599,7 @@ static int QueueCmdFillRects(SDL_Renderer *renderer, const SDL_FRect *rects, con
return retval;
}
static int QueueCmdCopy(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *srcrect, const SDL_FRect *dstrect)
static int QueueCmdCopy(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_FRect *srcrect, const SDL_FRect *dstrect)
{
SDL_RenderCommand *cmd = PrepQueueCmdDraw(renderer, SDL_RENDERCMD_COPY, texture);
int retval = -1;
@ -613,7 +613,7 @@ static int QueueCmdCopy(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_
}
static int QueueCmdCopyEx(SDL_Renderer *renderer, SDL_Texture *texture,
const SDL_Rect *srcquad, const SDL_FRect *dstrect,
const SDL_FRect *srcquad, const SDL_FRect *dstrect,
const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip, float scale_x, float scale_y)
{
SDL_RenderCommand *cmd = PrepQueueCmdDraw(renderer, SDL_RENDERCMD_COPY_EX, texture);
@ -2150,10 +2150,10 @@ static int UpdateLogicalPresentation(SDL_Renderer *renderer)
real_aspect = (float)output_w / output_h;
}
renderer->logical_src_rect.x = 0;
renderer->logical_src_rect.y = 0;
renderer->logical_src_rect.w = logical_w;
renderer->logical_src_rect.h = logical_h;
renderer->logical_src_rect.x = 0.0f;
renderer->logical_src_rect.y = 0.0f;
renderer->logical_src_rect.w = (float)logical_w;
renderer->logical_src_rect.h = (float)logical_h;
if (renderer->logical_presentation_mode == SDL_LOGICAL_PRESENTATION_INTEGER_SCALE) {
if (want_aspect > real_aspect) {
@ -2309,7 +2309,7 @@ int SDL_RenderCoordinatesFromWindow(SDL_Renderer *renderer, float window_x, floa
/* Convert from pixels within the window to pixels within the view */
if (renderer->logical_target) {
const SDL_Rect *src = &renderer->logical_src_rect;
const SDL_FRect *src = &renderer->logical_src_rect;
const SDL_FRect *dst = &renderer->logical_dst_rect;
render_x = ((render_x - dst->x) * src->w) / dst->w;
render_y = ((render_y - dst->y) * src->h) / dst->h;
@ -2350,7 +2350,7 @@ int SDL_RenderCoordinatesToWindow(SDL_Renderer *renderer, float x, float y, floa
/* Convert from pixels within the view to pixels within the window */
if (renderer->logical_target) {
const SDL_Rect *src = &renderer->logical_src_rect;
const SDL_FRect *src = &renderer->logical_src_rect;
const SDL_FRect *dst = &renderer->logical_dst_rect;
x = dst->x + ((x * dst->w) / src->w);
y = dst->y + ((y * dst->h) / src->h);
@ -2386,7 +2386,7 @@ int SDL_ConvertEventToRenderCoordinates(SDL_Renderer *renderer, SDL_Event *event
/* Convert from pixels within the window to pixels within the view */
if (renderer->logical_target) {
const SDL_Rect *src = &renderer->logical_src_rect;
const SDL_FRect *src = &renderer->logical_src_rect;
const SDL_FRect *dst = &renderer->logical_dst_rect;
scale = (scale * src->w) / dst->w;
}
@ -2409,7 +2409,7 @@ int SDL_ConvertEventToRenderCoordinates(SDL_Renderer *renderer, SDL_Event *event
/* Convert from pixels within the window to pixels within the view */
if (renderer->logical_target) {
const SDL_Rect *src = &renderer->logical_src_rect;
const SDL_FRect *src = &renderer->logical_src_rect;
const SDL_FRect *dst = &renderer->logical_dst_rect;
scale = (scale * src->h) / dst->h;
}
@ -3129,9 +3129,9 @@ int SDL_RenderFillRects(SDL_Renderer *renderer, const SDL_FRect *rects, int coun
return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
}
int SDL_RenderTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *srcrect, const SDL_FRect *dstrect)
int SDL_RenderTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_FRect *srcrect, const SDL_FRect *dstrect)
{
SDL_Rect real_srcrect;
SDL_FRect real_srcrect;
SDL_FRect real_dstrect;
int retval;
int use_rendergeometry;
@ -3152,12 +3152,12 @@ int SDL_RenderTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Re
use_rendergeometry = (renderer->QueueCopy == NULL);
real_srcrect.x = 0;
real_srcrect.y = 0;
real_srcrect.w = texture->w;
real_srcrect.h = texture->h;
real_srcrect.x = 0.0f;
real_srcrect.y = 0.0f;
real_srcrect.w = (float)texture->w;
real_srcrect.h = (float)texture->h;
if (srcrect) {
if (!SDL_GetRectIntersection(srcrect, &real_srcrect, &real_srcrect)) {
if (!SDL_GetRectIntersectionFloat(srcrect, &real_srcrect, &real_srcrect)) {
return 0;
}
}
@ -3188,10 +3188,10 @@ int SDL_RenderTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Re
float minu, minv, maxu, maxv;
float minx, miny, maxx, maxy;
minu = (float)(real_srcrect.x) / (float)texture->w;
minv = (float)(real_srcrect.y) / (float)texture->h;
maxu = (float)(real_srcrect.x + real_srcrect.w) / (float)texture->w;
maxv = (float)(real_srcrect.y + real_srcrect.h) / (float)texture->h;
minu = real_srcrect.x / texture->w;
minv = real_srcrect.y / texture->h;
maxu = (real_srcrect.x + real_srcrect.w) / texture->w;
maxv = (real_srcrect.y + real_srcrect.h) / texture->h;
minx = real_dstrect.x;
miny = real_dstrect.y;
@ -3235,10 +3235,10 @@ int SDL_RenderTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Re
}
int SDL_RenderTextureRotated(SDL_Renderer *renderer, SDL_Texture *texture,
const SDL_Rect *srcrect, const SDL_FRect *dstrect,
const SDL_FRect *srcrect, const SDL_FRect *dstrect,
const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip)
{
SDL_Rect real_srcrect;
SDL_FRect real_srcrect;
SDL_FRect real_dstrect;
SDL_FPoint real_center;
int retval;
@ -3267,12 +3267,12 @@ int SDL_RenderTextureRotated(SDL_Renderer *renderer, SDL_Texture *texture,
use_rendergeometry = (renderer->QueueCopyEx == NULL);
real_srcrect.x = 0;
real_srcrect.y = 0;
real_srcrect.w = texture->w;
real_srcrect.h = texture->h;
real_srcrect.x = 0.0f;
real_srcrect.y = 0.0f;
real_srcrect.w = (float)texture->w;
real_srcrect.h = (float)texture->h;
if (srcrect) {
if (!SDL_GetRectIntersection(srcrect, &real_srcrect, &real_srcrect)) {
if (!SDL_GetRectIntersectionFloat(srcrect, &real_srcrect, &real_srcrect)) {
return 0;
}
}
@ -3317,10 +3317,10 @@ int SDL_RenderTextureRotated(SDL_Renderer *renderer, SDL_Texture *texture,
const float s = SDL_sinf(radian_angle);
const float c = SDL_cosf(radian_angle);
minu = (float)(real_srcrect.x) / (float)texture->w;
minv = (float)(real_srcrect.y) / (float)texture->h;
maxu = (float)(real_srcrect.x + real_srcrect.w) / (float)texture->w;
maxv = (float)(real_srcrect.y + real_srcrect.h) / (float)texture->h;
minu = real_srcrect.x / texture->w;
minv = real_srcrect.y / texture->h;
maxu = (real_srcrect.x + real_srcrect.w) / texture->w;
maxv = (real_srcrect.y + real_srcrect.h) / texture->h;
centerx = real_center.x + real_dstrect.x;
centery = real_center.y + real_dstrect.y;
@ -3667,7 +3667,7 @@ static int SDLCALL SDL_SW_RenderGeometryRaw(SDL_Renderer *renderer,
/* Start rendering rect */
if (is_quad) {
SDL_Rect s;
SDL_FRect s;
SDL_FRect d;
const float *xy0_, *xy1_, *uv0_, *uv1_;
SDL_Color col0_ = *(const SDL_Color *)((const char *)color + k0 * color_stride);
@ -3678,10 +3678,10 @@ static int SDLCALL SDL_SW_RenderGeometryRaw(SDL_Renderer *renderer,
if (texture) {
uv0_ = (const float *)((const char *)uv + A * uv_stride);
uv1_ = (const float *)((const char *)uv + B * uv_stride);
s.x = (int)(uv0_[0] * texw);
s.y = (int)(uv0_[1] * texh);
s.w = (int)(uv1_[0] * texw - s.x);
s.h = (int)(uv1_[1] * texh - s.y);
s.x = uv0_[0] * texw;
s.y = uv0_[1] * texh;
s.w = uv1_[0] * texw - s.x;
s.h = uv1_[1] * texh - s.y;
}
d.x = xy0_[0];

View File

@ -168,9 +168,9 @@ struct SDL_Renderer
int (*QueueFillRects)(SDL_Renderer *renderer, SDL_RenderCommand *cmd, const SDL_FRect *rects,
int count);
int (*QueueCopy)(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *texture,
const SDL_Rect *srcrect, const SDL_FRect *dstrect);
const SDL_FRect *srcrect, const SDL_FRect *dstrect);
int (*QueueCopyEx)(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *texture,
const SDL_Rect *srcquad, const SDL_FRect *dstrect,
const SDL_FRect *srcquad, const SDL_FRect *dstrect,
const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip, float scale_x, float scale_y);
int (*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,
@ -229,7 +229,7 @@ struct SDL_Renderer
SDL_Texture *logical_target;
SDL_RendererLogicalPresentation logical_presentation_mode;
SDL_ScaleMode logical_scale_mode;
SDL_Rect logical_src_rect;
SDL_FRect logical_src_rect;
SDL_FRect logical_dst_rect;
SDL_RenderViewState *view;

View File

@ -757,7 +757,7 @@ static int PSP_QueueFillRects(SDL_Renderer *renderer, SDL_RenderCommand *cmd, co
}
static int PSP_QueueCopy(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *texture,
const SDL_Rect *srcrect, const SDL_FRect *dstrect)
const SDL_FRect *srcrect, const SDL_FRect *dstrect)
{
VertTV *verts;
const float x = dstrect->x;
@ -841,7 +841,7 @@ static int PSP_QueueCopy(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Tex
}
static int PSP_QueueCopyEx(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *texture,
const SDL_Rect *srcrect, const SDL_FRect *dstrect,
const SDL_FRect *srcrect, const SDL_FRect *dstrect,
const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip, float scale_x, float scale_y)
{
VertTV *verts = (VertTV *)SDL_AllocateRenderVertices(renderer, 4 * sizeof(VertTV), 4, &cmd->data.draw.first);

View File

@ -227,7 +227,7 @@ static int SW_QueueFillRects(SDL_Renderer *renderer, SDL_RenderCommand *cmd, con
}
static int SW_QueueCopy(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *texture,
const SDL_Rect *srcrect, const SDL_FRect *dstrect)
const SDL_FRect *srcrect, const SDL_FRect *dstrect)
{
SDL_Rect *verts = (SDL_Rect *)SDL_AllocateRenderVertices(renderer, 2 * sizeof(SDL_Rect), 0, &cmd->data.draw.first);
@ -237,7 +237,10 @@ static int SW_QueueCopy(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Text
cmd->data.draw.count = 1;
SDL_copyp(verts, srcrect);
verts->x = (int)srcrect->x;
verts->y = (int)srcrect->y;
verts->w = (int)srcrect->w;
verts->h = (int)srcrect->h;
verts++;
verts->x = (int)dstrect->x;
@ -260,7 +263,7 @@ typedef struct CopyExData
} CopyExData;
static int SW_QueueCopyEx(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *texture,
const SDL_Rect *srcrect, const SDL_FRect *dstrect,
const SDL_FRect *srcrect, const SDL_FRect *dstrect,
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);
@ -271,8 +274,10 @@ static int SW_QueueCopyEx(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Te
cmd->data.draw.count = 1;
SDL_copyp(&verts->srcrect, srcrect);
verts->srcrect.x = (int)srcrect->x;
verts->srcrect.y = (int)srcrect->y;
verts->srcrect.w = (int)srcrect->w;
verts->srcrect.h = (int)srcrect->h;
verts->dstrect.x = (int)dstrect->x;
verts->dstrect.y = (int)dstrect->y;
verts->dstrect.w = (int)dstrect->w;

View File

@ -3131,7 +3131,7 @@ int SDLTest_DrawCharacter(SDL_Renderer *renderer, float x, float y, Uint32 c)
const Uint32 charWidth = FONT_CHARACTER_SIZE;
const Uint32 charHeight = FONT_CHARACTER_SIZE;
const Uint32 charSize = FONT_CHARACTER_SIZE;
SDL_Rect srect;
SDL_FRect srect;
SDL_FRect drect;
int result;
Uint32 ix, iy;
@ -3147,10 +3147,10 @@ int SDLTest_DrawCharacter(SDL_Renderer *renderer, float x, float y, Uint32 c)
/*
* Setup source rectangle
*/
srect.x = 0;
srect.y = 0;
srect.w = charWidth;
srect.h = charHeight;
srect.x = 0.0f;
srect.y = 0.0f;
srect.w = (float)charWidth;
srect.h = (float)charHeight;
/*
* Setup destination rectangle

View File

@ -330,8 +330,8 @@ static Sint32 unifont_draw_glyph(Uint32 codepoint, int rendererID, SDL_FRect *ds
{
SDL_Texture *texture;
const Uint32 textureID = codepoint / UNIFONT_GLYPHS_IN_TEXTURE;
SDL_Rect srcrect;
srcrect.w = srcrect.h = 16;
SDL_FRect srcrect;
srcrect.w = srcrect.h = 16.0f;
if (codepoint > UNIFONT_MAX_CODEPOINT) {
return 0;
}
@ -343,8 +343,8 @@ static Sint32 unifont_draw_glyph(Uint32 codepoint, int rendererID, SDL_FRect *ds
texture = unifontTexture[UNIFONT_NUM_TEXTURES * rendererID + textureID];
if (texture != NULL) {
const Uint32 cInTex = codepoint % UNIFONT_GLYPHS_IN_TEXTURE;
srcrect.x = cInTex % UNIFONT_GLYPHS_IN_ROW * 16;
srcrect.y = cInTex / UNIFONT_GLYPHS_IN_ROW * 16;
srcrect.x = (float)(cInTex % UNIFONT_GLYPHS_IN_ROW * 16);
srcrect.y = (float)(cInTex / UNIFONT_GLYPHS_IN_ROW * 16);
SDL_RenderTexture(state->renderers[rendererID], texture, &srcrect, dst);
}
return unifontGlyph[codepoint].width;

View File

@ -26,7 +26,7 @@ typedef struct LoadedPicture
const char *name;
} LoadedPicture;
void render(SDL_Renderer *renderer, SDL_Texture *texture, SDL_Rect texture_dimensions)
void render(SDL_Renderer *renderer, SDL_Texture *texture, SDL_FRect texture_dimensions)
{
SDL_FRect dst;
@ -35,10 +35,10 @@ void render(SDL_Renderer *renderer, SDL_Texture *texture, SDL_Rect texture_dimen
SDL_RenderClear(renderer);
/* Render the texture. */
dst.x = (float)texture_dimensions.x;
dst.y = (float)texture_dimensions.y;
dst.w = (float)texture_dimensions.w;
dst.h = (float)texture_dimensions.h;
dst.x = texture_dimensions.x;
dst.y = texture_dimensions.y;
dst.w = texture_dimensions.w;
dst.h = texture_dimensions.h;
SDL_RenderTexture(renderer, texture, &texture_dimensions, &dst);
SDL_RenderPresent(renderer);
@ -58,8 +58,8 @@ int main(int argc, char **argv)
unsigned int current_picture;
int button_down;
Uint32 pixelFormat = 0;
int access = 0;
SDL_Rect texture_dimensions;
int w, h, access = 0;
SDL_FRect texture_dimensions;
/* Enable standard application logging */
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
@ -158,13 +158,13 @@ int main(int argc, char **argv)
should_exit = 0;
current_picture = 0;
button_down = 0;
texture_dimensions.h = 0;
texture_dimensions.w = 0;
texture_dimensions.x = 0;
texture_dimensions.y = 0;
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Changing to shaped bmp: %s", pictures[current_picture].name);
SDL_QueryTexture(pictures[current_picture].texture, &pixelFormat, &access, &texture_dimensions.w, &texture_dimensions.h);
SDL_SetWindowSize(window, texture_dimensions.w, texture_dimensions.h);
SDL_QueryTexture(pictures[current_picture].texture, &pixelFormat, &access, &w, &h);
texture_dimensions.x = 0.0f;
texture_dimensions.y = 0.0f;
texture_dimensions.h = (float)w;
texture_dimensions.w = (float)h;
SDL_SetWindowSize(window, w, h);
SDL_SetWindowShape(window, pictures[current_picture].surface, &pictures[current_picture].mode);
while (should_exit == 0) {
while (SDL_PollEvent(&event)) {
@ -182,8 +182,10 @@ int main(int argc, char **argv)
current_picture = 0;
}
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Changing to shaped bmp: %s", pictures[current_picture].name);
SDL_QueryTexture(pictures[current_picture].texture, &pixelFormat, &access, &texture_dimensions.w, &texture_dimensions.h);
SDL_SetWindowSize(window, texture_dimensions.w, texture_dimensions.h);
SDL_QueryTexture(pictures[current_picture].texture, &pixelFormat, &access, &w, &h);
texture_dimensions.h = (float)w;
texture_dimensions.w = (float)h;
SDL_SetWindowSize(window, w, h);
SDL_SetWindowShape(window, pictures[current_picture].surface, &pictures[current_picture].mode);
}
if (event.type == SDL_EVENT_QUIT) {