From 9674e72d7657a8399448325ca76898b3f119d51d Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Thu, 20 Sep 2018 15:46:02 -0400 Subject: render: Move to a batching system for rendering (work in progress). --HG-- branch : SDL-ryan-batching-renderer --- include/SDL_hints.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'include') diff --git a/include/SDL_hints.h b/include/SDL_hints.h index 07a9113389..b3c0c154fb 100644 --- a/include/SDL_hints.h +++ b/include/SDL_hints.h @@ -1029,6 +1029,31 @@ extern "C" { */ #define SDL_HINT_AUDIO_CATEGORY "SDL_AUDIO_CATEGORY" +/** + * \brief A variable controlling whether the 2D render API is compatible or efficient. + * + * This variable can be set to the following values: + * + * "0" - Don't use batching to make rendering more efficient. + * "1" - Use batching, but might cause problems if app makes its own direct OpenGL calls. + * + * Up to SDL 2.0.9, the render API would draw immediately when requested. Now + * it batches up draw requests and sends them all to the GPU only when forced + * to (during SDL_RenderPresent, when changing render targets, by updating a + * texture that the batch needs, etc). This is significantly more efficient, + * but it can cause problems for apps that expect to render on top of the + * render API's output. As such, SDL will disable batching if a specific + * render backend is requested (since this might indicate that the app is + * planning to use the underlying graphics API directly). This hint can + * be used to explicitly request batching in this instance. It is a contract + * that you will either never use the underlying graphics API directly, or + * if you do, you will call SDL_RenderFlush() before you do so any current + * batch goes to the GPU before your work begins. Not following this contract + * will result in undefined behavior. + */ +#define SDL_HINT_RENDER_BATCHING "SDL_RENDER_BATCHING" + + /** * \brief An enumeration of hint priorities */ -- cgit v1.2.3 From eae900576aea6c94e174bd7cf16839b08d78877f Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Thu, 4 Oct 2018 16:34:44 -0400 Subject: render: Added SDL_RenderFlush(). --HG-- branch : SDL-ryan-batching-renderer --- include/SDL_render.h | 25 +++++++++++++++++++++++++ src/dynapi/SDL_dynapi_overrides.h | 1 + src/dynapi/SDL_dynapi_procs.h | 1 + src/render/SDL_render.c | 6 ++++++ 4 files changed, 33 insertions(+) (limited to 'include') diff --git a/include/SDL_render.h b/include/SDL_render.h index d336192974..850e908ecd 100644 --- a/include/SDL_render.h +++ b/include/SDL_render.h @@ -876,6 +876,31 @@ extern DECLSPEC void SDLCALL SDL_DestroyTexture(SDL_Texture * texture); */ extern DECLSPEC void SDLCALL SDL_DestroyRenderer(SDL_Renderer * renderer); +/** + * \brief Force the rendering context to flush any pending commands to the + * underlying rendering API. + * + * You do not need to (and in fact, shouldn't) call this function unless + * you are planning to call into OpenGL/Direct3D/Metal/whatever directly + * in addition to using an SDL_Renderer. + * + * This is for a very-specific case: if you are using SDL's render API, + * you asked for a specific renderer backend (OpenGL, Direct3D, etc), + * you set SDL_HINT_RENDER_BATCHING to "1", and you plan to make + * OpenGL/D3D/whatever calls in addition to SDL render API calls. If all of + * this applies, you should call SDL_RenderFlush() between calls to SDL's + * render API and the low-level API you're using in cooperation. + * + * In all other cases, you can ignore this function. This is only here to + * get maximum performance out of a specific situation. In all other cases, + * SDL will do the right thing, perhaps at a performance loss. + * + * This function is first available in SDL 2.0.10, and is not needed in + * 2.0.9 and earlier, as earlier versions did not queue rendering commands + * at all, instead flushing them to the OS immediately. + */ +extern DECLSPEC int SDLCALL SDL_RenderFlush(SDL_Renderer * renderer); + /** * \brief Bind the texture to the current OpenGL/ES/ES2 context for use with diff --git a/src/dynapi/SDL_dynapi_overrides.h b/src/dynapi/SDL_dynapi_overrides.h index b1f029f370..0006041680 100644 --- a/src/dynapi/SDL_dynapi_overrides.h +++ b/src/dynapi/SDL_dynapi_overrides.h @@ -696,3 +696,4 @@ #define SDL_SensorUpdate SDL_SensorUpdate_REAL #define SDL_IsTablet SDL_IsTablet_REAL #define SDL_GetDisplayOrientation SDL_GetDisplayOrientation_REAL +#define SDL_RenderFlush SDL_RenderFlush_REAL diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h index 6619f882a8..4461d55197 100644 --- a/src/dynapi/SDL_dynapi_procs.h +++ b/src/dynapi/SDL_dynapi_procs.h @@ -738,3 +738,4 @@ SDL_DYNAPI_PROC(void,SDL_SensorClose,(SDL_Sensor *a),(a),) SDL_DYNAPI_PROC(void,SDL_SensorUpdate,(void),(),) SDL_DYNAPI_PROC(SDL_bool,SDL_IsTablet,(void),(),return) SDL_DYNAPI_PROC(SDL_DisplayOrientation,SDL_GetDisplayOrientation,(int a),(a),return) +SDL_DYNAPI_PROC(int,SDL_RenderFlush,(SDL_Renderer *a),(a),return) diff --git a/src/render/SDL_render.c b/src/render/SDL_render.c index 5c7e36da06..5bbcd790bd 100644 --- a/src/render/SDL_render.c +++ b/src/render/SDL_render.c @@ -254,6 +254,12 @@ FlushRenderCommandsIfNotBatching(SDL_Renderer *renderer) return renderer->batching ? 0 : FlushRenderCommands(renderer); } +int +SDL_RenderFlush(SDL_Renderer * renderer) +{ + return FlushRenderCommands(renderer); +} + static SDL_AllocVertGap * AllocateVertexGap(SDL_Renderer *renderer) { -- cgit v1.2.3 From 747fc8f88b62c788cb41ae25d9de62fd97107353 Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Tue, 23 Oct 2018 01:34:03 -0400 Subject: render: Add floating point versions of various draw APIs. --HG-- branch : SDL-ryan-batching-renderer --- include/SDL_rect.h | 30 ++- include/SDL_render.h | 142 +++++++++++ src/dynapi/SDL_dynapi_overrides.h | 10 + src/dynapi/SDL_dynapi_procs.h | 10 + src/render/SDL_render.c | 513 +++++++++++++++++++++++++++++++++----- src/render/SDL_sysrender.h | 14 -- 6 files changed, 643 insertions(+), 76 deletions(-) (limited to 'include') diff --git a/include/SDL_rect.h b/include/SDL_rect.h index 543bb61869..986764cd69 100644 --- a/include/SDL_rect.h +++ b/include/SDL_rect.h @@ -40,7 +40,7 @@ extern "C" { #endif /** - * \brief The structure that defines a point + * \brief The structure that defines a point (integer) * * \sa SDL_EnclosePoints * \sa SDL_PointInRect @@ -52,7 +52,20 @@ typedef struct SDL_Point } SDL_Point; /** - * \brief A rectangle, with the origin at the upper left. + * \brief The structure that defines a point (floating point) + * + * \sa SDL_EnclosePoints + * \sa SDL_PointInRect + */ +typedef struct SDL_FPoint +{ + float x; + float y; +} SDL_FPoint; + + +/** + * \brief A rectangle, with the origin at the upper left (integer). * * \sa SDL_RectEmpty * \sa SDL_RectEquals @@ -67,6 +80,19 @@ typedef struct SDL_Rect int w, h; } SDL_Rect; + +/** + * \brief A rectangle, with the origin at the upper left (floating point). + */ +typedef struct SDL_FRect +{ + float x; + float y; + float w; + float h; +} SDL_FRect; + + /** * \brief Returns true if point resides inside a rectangle. */ diff --git a/include/SDL_render.h b/include/SDL_render.h index 850e908ecd..738b7392a6 100644 --- a/include/SDL_render.h +++ b/include/SDL_render.h @@ -835,6 +835,148 @@ extern DECLSPEC int SDLCALL SDL_RenderCopyEx(SDL_Renderer * renderer, const SDL_Point *center, const SDL_RendererFlip flip); + +/** + * \brief Draw a point on the current rendering target. + * + * \param renderer The renderer which should draw a point. + * \param x The x coordinate of the point. + * \param y The y coordinate of the point. + * + * \return 0 on success, or -1 on error + */ +extern DECLSPEC int SDLCALL SDL_RenderDrawPointF(SDL_Renderer * renderer, + float x, float y); + +/** + * \brief Draw multiple points on the current rendering target. + * + * \param renderer The renderer which should draw multiple points. + * \param points The points to draw + * \param count The number of points to draw + * + * \return 0 on success, or -1 on error + */ +extern DECLSPEC int SDLCALL SDL_RenderDrawPointsF(SDL_Renderer * renderer, + const SDL_FPoint * points, + int count); + +/** + * \brief Draw a line on the current rendering target. + * + * \param renderer The renderer which should draw a line. + * \param x1 The x coordinate of the start point. + * \param y1 The y coordinate of the start point. + * \param x2 The x coordinate of the end point. + * \param y2 The y coordinate of the end point. + * + * \return 0 on success, or -1 on error + */ +extern DECLSPEC int SDLCALL SDL_RenderDrawLineF(SDL_Renderer * renderer, + float x1, float y1, float x2, float y2); + +/** + * \brief Draw a series of connected lines on the current rendering target. + * + * \param renderer The renderer which should draw multiple lines. + * \param points The points along the lines + * \param count The number of points, drawing count-1 lines + * + * \return 0 on success, or -1 on error + */ +extern DECLSPEC int SDLCALL SDL_RenderDrawLinesF(SDL_Renderer * renderer, + const SDL_FPoint * points, + int count); + +/** + * \brief Draw a rectangle on the current rendering target. + * + * \param renderer The renderer which should draw a rectangle. + * \param rect A pointer to the destination rectangle, or NULL to outline the entire rendering target. + * + * \return 0 on success, or -1 on error + */ +extern DECLSPEC int SDLCALL SDL_RenderDrawRectF(SDL_Renderer * renderer, + const SDL_FRect * rect); + +/** + * \brief Draw some number of rectangles on the current rendering target. + * + * \param renderer The renderer which should draw multiple rectangles. + * \param rects A pointer to an array of destination rectangles. + * \param count The number of rectangles. + * + * \return 0 on success, or -1 on error + */ +extern DECLSPEC int SDLCALL SDL_RenderDrawRectsF(SDL_Renderer * renderer, + const SDL_FRect * rects, + int count); + +/** + * \brief Fill a rectangle on the current rendering target with the drawing color. + * + * \param renderer The renderer which should fill a rectangle. + * \param rect A pointer to the destination rectangle, or NULL for the entire + * rendering target. + * + * \return 0 on success, or -1 on error + */ +extern DECLSPEC int SDLCALL SDL_RenderFillRectF(SDL_Renderer * renderer, + const SDL_FRect * rect); + +/** + * \brief Fill some number of rectangles on the current rendering target with the drawing color. + * + * \param renderer The renderer which should fill multiple rectangles. + * \param rects A pointer to an array of destination rectangles. + * \param count The number of rectangles. + * + * \return 0 on success, or -1 on error + */ +extern DECLSPEC int SDLCALL SDL_RenderFillRectsF(SDL_Renderer * renderer, + const SDL_FRect * rects, + int count); + +/** + * \brief Copy a portion of the texture to the current rendering target. + * + * \param renderer The renderer which should copy parts of a texture. + * \param texture The source texture. + * \param srcrect A pointer to the source rectangle, or NULL for the entire + * texture. + * \param dstrect A pointer to the destination rectangle, or NULL for the + * entire rendering target. + * + * \return 0 on success, or -1 on error + */ +extern DECLSPEC int SDLCALL SDL_RenderCopyF(SDL_Renderer * renderer, + SDL_Texture * texture, + const SDL_Rect * srcrect, + const SDL_FRect * dstrect); + +/** + * \brief Copy a portion of the source texture to the current rendering target, rotating it by angle around the given center + * + * \param renderer The renderer which should copy parts of a texture. + * \param texture The source texture. + * \param srcrect A pointer to the source rectangle, or NULL for the entire + * texture. + * \param dstrect A pointer to the destination rectangle, or NULL for the + * entire rendering target. + * \param angle An angle in degrees that indicates the rotation that will be applied to dstrect, rotating it in a clockwise direction + * \param center A pointer to a point indicating the point around which dstrect will be rotated (if NULL, rotation will be done around dstrect.w/2, dstrect.h/2). + * \param flip An SDL_RendererFlip value stating which flipping actions should be performed on the texture + * + * \return 0 on success, or -1 on error + */ +extern DECLSPEC int SDLCALL SDL_RenderCopyExF(SDL_Renderer * renderer, + SDL_Texture * texture, + const SDL_Rect * srcrect, + const SDL_FRect * dstrect, + const double angle, + const SDL_FPoint *center, + const SDL_RendererFlip flip); + /** * \brief Read pixels from the current rendering target. * diff --git a/src/dynapi/SDL_dynapi_overrides.h b/src/dynapi/SDL_dynapi_overrides.h index 0006041680..0bc6f44f05 100644 --- a/src/dynapi/SDL_dynapi_overrides.h +++ b/src/dynapi/SDL_dynapi_overrides.h @@ -697,3 +697,13 @@ #define SDL_IsTablet SDL_IsTablet_REAL #define SDL_GetDisplayOrientation SDL_GetDisplayOrientation_REAL #define SDL_RenderFlush SDL_RenderFlush_REAL +#define SDL_RenderDrawPointF SDL_RenderDrawPointF_REAL +#define SDL_RenderDrawPointsF SDL_RenderDrawPointsF_REAL +#define SDL_RenderDrawLineF SDL_RenderDrawLineF_REAL +#define SDL_RenderDrawLinesF SDL_RenderDrawLinesF_REAL +#define SDL_RenderDrawRectF SDL_RenderDrawRectF_REAL +#define SDL_RenderDrawRectsF SDL_RenderDrawRectsF_REAL +#define SDL_RenderFillRectF SDL_RenderFillRectF_REAL +#define SDL_RenderFillRectsF SDL_RenderFillRectsF_REAL +#define SDL_RenderCopyF SDL_RenderCopyF_REAL +#define SDL_RenderCopyExF SDL_RenderCopyExF_REAL diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h index 4461d55197..ce88ad9c02 100644 --- a/src/dynapi/SDL_dynapi_procs.h +++ b/src/dynapi/SDL_dynapi_procs.h @@ -739,3 +739,13 @@ SDL_DYNAPI_PROC(void,SDL_SensorUpdate,(void),(),) SDL_DYNAPI_PROC(SDL_bool,SDL_IsTablet,(void),(),return) SDL_DYNAPI_PROC(SDL_DisplayOrientation,SDL_GetDisplayOrientation,(int a),(a),return) SDL_DYNAPI_PROC(int,SDL_RenderFlush,(SDL_Renderer *a),(a),return) +SDL_DYNAPI_PROC(int,SDL_RenderDrawPointF,(SDL_Renderer *a, float b, float c),(a,b,c),return) +SDL_DYNAPI_PROC(int,SDL_RenderDrawPointsF,(SDL_Renderer *a, const SDL_FPoint *b, int c),(a,b,c),return) +SDL_DYNAPI_PROC(int,SDL_RenderDrawLineF,(SDL_Renderer *a, float b, float c, float d, float e),(a,b,c,d,e),return) +SDL_DYNAPI_PROC(int,SDL_RenderDrawLinesF,(SDL_Renderer *a, const SDL_FPoint *b, int c),(a,b,c),return) +SDL_DYNAPI_PROC(int,SDL_RenderDrawRectF,(SDL_Renderer *a, const SDL_FRect *b),(a,b),return) +SDL_DYNAPI_PROC(int,SDL_RenderDrawRectsF,(SDL_Renderer *a, const SDL_FRect *b, int c),(a,b,c),return) +SDL_DYNAPI_PROC(int,SDL_RenderFillRectF,(SDL_Renderer *a, const SDL_FRect *b),(a,b),return) +SDL_DYNAPI_PROC(int,SDL_RenderFillRectsF,(SDL_Renderer *a, const SDL_FRect *b, int c),(a,b,c),return) +SDL_DYNAPI_PROC(int,SDL_RenderCopyF,(SDL_Renderer *a, SDL_Texture *b, const SDL_Rect *c, const SDL_FRect *d),(a,b,c,d),return) +SDL_DYNAPI_PROC(int,SDL_RenderCopyExF,(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) diff --git a/src/render/SDL_render.c b/src/render/SDL_render.c index b03524b4c5..8d1ca56d82 100644 --- a/src/render/SDL_render.c +++ b/src/render/SDL_render.c @@ -2157,31 +2157,37 @@ SDL_RenderClear(SDL_Renderer * renderer) return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer); } + +/* !!! FIXME: delete all the duplicate code for the integer versions in 2.1, + !!! FIXME: making the floating point versions the only available APIs. */ + int SDL_RenderDrawPoint(SDL_Renderer * renderer, int x, int y) { - SDL_Point point; - int retval; + const SDL_FPoint fpoint = { (float) x, (float) y }; + return SDL_RenderDrawPointsF(renderer, &fpoint, 1); +} - point.x = x; - point.y = y; - retval = SDL_RenderDrawPoints(renderer, &point, 1); - return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer); +int +SDL_RenderDrawPointF(SDL_Renderer * renderer, float x, float y) +{ + const SDL_FPoint fpoint = { x, y }; + return SDL_RenderDrawPointsF(renderer, &fpoint, 1); } static int RenderDrawPointsWithRects(SDL_Renderer * renderer, - const SDL_Point * points, int count) + const SDL_Point * points, const int count) { - SDL_FRect *frects; - int i; int retval = -1; SDL_bool isstack; + SDL_FRect *frects = SDL_small_alloc(SDL_FRect, count, &isstack); + int i; - frects = SDL_small_alloc(SDL_FRect, count, &isstack); if (!frects) { return SDL_OutOfMemory(); } + for (i = 0; i < count; ++i) { frects[i].x = points[i].x * renderer->scale.x; frects[i].y = points[i].y * renderer->scale.y; @@ -2239,21 +2245,148 @@ SDL_RenderDrawPoints(SDL_Renderer * renderer, return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer); } +static int +RenderDrawPointsWithRectsF(SDL_Renderer * renderer, + const SDL_FPoint * fpoints, const int count) +{ + int retval = -1; + SDL_bool isstack; + SDL_FRect *frects = SDL_small_alloc(SDL_FRect, count, &isstack); + int i; + + if (!frects) { + return SDL_OutOfMemory(); + } + + for (i = 0; i < count; ++i) { + frects[i].x = fpoints[i].x * renderer->scale.x; + frects[i].y = fpoints[i].y * renderer->scale.y; + frects[i].w = renderer->scale.x; + frects[i].h = renderer->scale.y; + } + + retval = QueueCmdFillRects(renderer, frects, count); + + SDL_small_free(frects, isstack); + + return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer); +} + +int +SDL_RenderDrawPointsF(SDL_Renderer * renderer, + const SDL_FPoint * points, int count) +{ + SDL_FPoint *fpoints; + int i; + int retval; + SDL_bool isstack; + + CHECK_RENDERER_MAGIC(renderer, -1); + + if (!points) { + return SDL_SetError("SDL_RenderDrawFPoints(): Passed NULL points"); + } + if (count < 1) { + return 0; + } + + /* Don't draw while we're hidden */ + if (renderer->hidden) { + return 0; + } + + if (renderer->scale.x != 1.0f || renderer->scale.y != 1.0f) { + return RenderDrawPointsWithRectsF(renderer, points, count); + } + + fpoints = SDL_small_alloc(SDL_FPoint, count, &isstack); + if (!fpoints) { + return SDL_OutOfMemory(); + } + for (i = 0; i < count; ++i) { + fpoints[i].x = points[i].x * renderer->scale.x; + fpoints[i].y = points[i].y * renderer->scale.y; + } + + retval = QueueCmdDrawPoints(renderer, fpoints, count); + + SDL_small_free(fpoints, isstack); + + return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer); +} + int SDL_RenderDrawLine(SDL_Renderer * renderer, int x1, int y1, int x2, int y2) { - SDL_Point points[2]; + const SDL_FPoint points[2] = { { (float) x1, (float) y1 }, { (float) x2, (float) y2 } }; + return SDL_RenderDrawLinesF(renderer, points, 2); +} - points[0].x = x1; - points[0].y = y1; - points[1].x = x2; - points[1].y = y2; - return SDL_RenderDrawLines(renderer, points, 2); +int +SDL_RenderDrawLineF(SDL_Renderer * renderer, float x1, float y1, float x2, float y2) +{ + const SDL_FPoint points[2] = { { x1, y1 }, { x2, y2 } }; + return SDL_RenderDrawLinesF(renderer, points, 2); } static int RenderDrawLinesWithRects(SDL_Renderer * renderer, - const SDL_Point * points, int count) + const SDL_Point * points, const int count) +{ + SDL_FRect *frect; + SDL_FRect *frects; + SDL_FPoint fpoints[2]; + int i, nrects = 0; + int retval = 0; + SDL_bool isstack; + + frects = SDL_small_alloc(SDL_FRect, count-1, &isstack); + if (!frects) { + return SDL_OutOfMemory(); + } + + for (i = 0; i < count-1; ++i) { + if (points[i].x == points[i+1].x) { + const int minY = SDL_min(points[i].y, points[i+1].y); + const int maxY = SDL_max(points[i].y, points[i+1].y); + + frect = &frects[nrects++]; + frect->x = points[i].x * renderer->scale.x; + frect->y = minY * renderer->scale.y; + frect->w = renderer->scale.x; + frect->h = (maxY - minY + 1) * renderer->scale.y; + } else if (points[i].y == points[i+1].y) { + const int minX = SDL_min(points[i].x, points[i+1].x); + const int maxX = SDL_max(points[i].x, points[i+1].x); + + frect = &frects[nrects++]; + frect->x = minX * renderer->scale.x; + frect->y = points[i].y * renderer->scale.y; + frect->w = (maxX - minX + 1) * renderer->scale.x; + frect->h = renderer->scale.y; + } else { + /* FIXME: We can't use a rect for this line... */ + fpoints[0].x = points[i].x * renderer->scale.x; + fpoints[0].y = points[i].y * renderer->scale.y; + fpoints[1].x = points[i+1].x * renderer->scale.x; + fpoints[1].y = points[i+1].y * renderer->scale.y; + retval += QueueCmdDrawLines(renderer, fpoints, 2); + } + } + + retval += QueueCmdFillRects(renderer, frects, nrects); + + SDL_small_free(frects, isstack); + + if (retval < 0) { + retval = -1; + } + return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer); +} + +static int +RenderDrawLinesWithRectsF(SDL_Renderer * renderer, + const SDL_FPoint * points, const int count) { SDL_FRect *frect; SDL_FRect *frects; @@ -2349,20 +2482,83 @@ SDL_RenderDrawLines(SDL_Renderer * renderer, return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer); } +int +SDL_RenderDrawLinesF(SDL_Renderer * renderer, + const SDL_FPoint * points, int count) +{ + SDL_FPoint *fpoints; + int i; + int retval; + SDL_bool isstack; + + CHECK_RENDERER_MAGIC(renderer, -1); + + if (!points) { + return SDL_SetError("SDL_RenderDrawLines(): Passed NULL points"); + } + if (count < 2) { + return 0; + } + + /* Don't draw while we're hidden */ + if (renderer->hidden) { + return 0; + } + + if (renderer->scale.x != 1.0f || renderer->scale.y != 1.0f) { + return RenderDrawLinesWithRectsF(renderer, points, count); + } + + fpoints = SDL_small_alloc(SDL_FPoint, count, &isstack); + if (!fpoints) { + return SDL_OutOfMemory(); + } + for (i = 0; i < count; ++i) { + fpoints[i].x = points[i].x * renderer->scale.x; + fpoints[i].y = points[i].y * renderer->scale.y; + } + + retval = QueueCmdDrawLines(renderer, fpoints, count); + + SDL_small_free(fpoints, isstack); + + return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer); +} + int SDL_RenderDrawRect(SDL_Renderer * renderer, const SDL_Rect * rect) { - SDL_Rect full_rect; - SDL_Point points[5]; + SDL_FRect frect; + SDL_FRect *prect = NULL; + + if (rect) { + frect.x = (float) rect->x; + frect.y = (float) rect->y; + frect.w = (float) rect->w; + frect.h = (float) rect->h; + prect = &frect; + } + + return SDL_RenderDrawRectF(renderer, prect); +} + +int +SDL_RenderDrawRectF(SDL_Renderer * renderer, const SDL_FRect * rect) +{ + SDL_FRect frect; + SDL_FPoint points[5]; CHECK_RENDERER_MAGIC(renderer, -1); /* If 'rect' == NULL, then outline the whole surface */ if (!rect) { - SDL_RenderGetViewport(renderer, &full_rect); - full_rect.x = 0; - full_rect.y = 0; - rect = &full_rect; + SDL_Rect r; + SDL_RenderGetViewport(renderer, &r); + frect.x = 0.0f; + frect.y = 0.0f; + frect.w = (float) r.w; + frect.h = (float) r.h; + rect = &frect; } points[0].x = rect->x; @@ -2375,7 +2571,7 @@ SDL_RenderDrawRect(SDL_Renderer * renderer, const SDL_Rect * rect) points[3].y = rect->y+rect->h-1; points[4].x = rect->x; points[4].y = rect->y; - return SDL_RenderDrawLines(renderer, points, 5); + return SDL_RenderDrawLinesF(renderer, points, 5); } int @@ -2406,21 +2602,76 @@ SDL_RenderDrawRects(SDL_Renderer * renderer, return 0; } +int +SDL_RenderDrawRectsF(SDL_Renderer * renderer, + const SDL_FRect * rects, int count) +{ + int i; + + CHECK_RENDERER_MAGIC(renderer, -1); + + if (!rects) { + return SDL_SetError("SDL_RenderDrawRects(): Passed NULL rects"); + } + if (count < 1) { + return 0; + } + + /* Don't draw while we're hidden */ + if (renderer->hidden) { + return 0; + } + + for (i = 0; i < count; ++i) { + if (SDL_RenderDrawRectF(renderer, &rects[i]) < 0) { + return -1; + } + } + return 0; +} + int SDL_RenderFillRect(SDL_Renderer * renderer, const SDL_Rect * rect) { - SDL_Rect full_rect = { 0, 0, 0, 0 }; + SDL_FRect frect; + + CHECK_RENDERER_MAGIC(renderer, -1); + + /* If 'rect' == NULL, then outline the whole surface */ + if (rect) { + frect.x = (float) rect->x; + frect.y = (float) rect->y; + frect.w = (float) rect->w; + frect.h = (float) rect->h; + } else { + SDL_Rect r; + SDL_RenderGetViewport(renderer, &r); + frect.x = 0.0f; + frect.y = 0.0f; + frect.w = (float) r.w; + frect.h = (float) r.h; + } + return SDL_RenderFillRectsF(renderer, &frect, 1); +} + +int +SDL_RenderFillRectF(SDL_Renderer * renderer, const SDL_FRect * rect) +{ + SDL_FRect frect; CHECK_RENDERER_MAGIC(renderer, -1); /* If 'rect' == NULL, then outline the whole surface */ if (!rect) { - SDL_RenderGetViewport(renderer, &full_rect); - full_rect.x = 0; - full_rect.y = 0; - rect = &full_rect; + SDL_Rect r; + SDL_RenderGetViewport(renderer, &r); + frect.x = 0.0f; + frect.y = 0.0f; + frect.w = (float) r.w; + frect.h = (float) r.h; + rect = &frect; } - return SDL_RenderFillRects(renderer, rect, 1); + return SDL_RenderFillRectsF(renderer, rect, 1); } int @@ -2464,13 +2715,124 @@ SDL_RenderFillRects(SDL_Renderer * renderer, return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer); } +int +SDL_RenderFillRectsF(SDL_Renderer * renderer, + const SDL_FRect * rects, int count) +{ + SDL_FRect *frects; + int i; + int retval; + SDL_bool isstack; + + CHECK_RENDERER_MAGIC(renderer, -1); + + if (!rects) { + return SDL_SetError("SDL_RenderFillFRects(): Passed NULL rects"); + } + if (count < 1) { + return 0; + } + + /* Don't draw while we're hidden */ + if (renderer->hidden) { + return 0; + } + + frects = SDL_small_alloc(SDL_FRect, count, &isstack); + if (!frects) { + return SDL_OutOfMemory(); + } + for (i = 0; i < count; ++i) { + frects[i].x = rects[i].x * renderer->scale.x; + frects[i].y = rects[i].y * renderer->scale.y; + frects[i].w = rects[i].w * renderer->scale.x; + frects[i].h = rects[i].h * renderer->scale.y; + } + + retval = QueueCmdFillRects(renderer, frects, count); + + SDL_small_free(frects, isstack); + + return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer); +} + +/* !!! FIXME: move this to a public API if we want to do float versions of all of these later */ +SDL_FORCE_INLINE SDL_bool SDL_FRectEmpty(const SDL_FRect *r) +{ + return ((!r) || (r->w <= 0.0f) || (r->h <= 0.0f)) ? SDL_TRUE : SDL_FALSE; +} + +/* !!! FIXME: move this to a public API if we want to do float versions of all of these later */ +static SDL_bool +SDL_HasIntersectionF(const SDL_FRect * A, const SDL_FRect * B) +{ + float Amin, Amax, Bmin, Bmax; + + if (!A) { + SDL_InvalidParamError("A"); + return SDL_FALSE; + } + + if (!B) { + SDL_InvalidParamError("B"); + return SDL_FALSE; + } + + /* Special cases for empty rects */ + if (SDL_FRectEmpty(A) || SDL_FRectEmpty(B)) { + return SDL_FALSE; + } + + /* Horizontal intersection */ + Amin = A->x; + Amax = Amin + A->w; + Bmin = B->x; + Bmax = Bmin + B->w; + if (Bmin > Amin) + Amin = Bmin; + if (Bmax < Amax) + Amax = Bmax; + if (Amax <= Amin) + return SDL_FALSE; + + /* Vertical intersection */ + Amin = A->y; + Amax = Amin + A->h; + Bmin = B->y; + Bmax = Bmin + B->h; + if (Bmin > Amin) + Amin = Bmin; + if (Bmax < Amax) + Amax = Bmax; + if (Amax <= Amin) + return SDL_FALSE; + + return SDL_TRUE; +} + int SDL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture, const SDL_Rect * srcrect, const SDL_Rect * dstrect) { - SDL_Rect real_srcrect = { 0, 0, 0, 0 }; - SDL_Rect real_dstrect = { 0, 0, 0, 0 }; - SDL_FRect frect; + SDL_FRect dstfrect; + SDL_FRect *pdstfrect = NULL; + if (dstrect) { + dstfrect.x = (float) dstrect->x; + dstfrect.y = (float) dstrect->y; + dstfrect.w = (float) dstrect->w; + dstfrect.h = (float) dstrect->h; + pdstfrect = &dstfrect; + } + return SDL_RenderCopyF(renderer, texture, srcrect, pdstfrect); +} + +int +SDL_RenderCopyF(SDL_Renderer * renderer, SDL_Texture * texture, + const SDL_Rect * srcrect, const SDL_FRect * dstrect) +{ + SDL_Rect real_srcrect; + SDL_FRect real_dstrect; + SDL_Rect r; int retval; CHECK_RENDERER_MAGIC(renderer, -1); @@ -2495,11 +2857,13 @@ SDL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture, } } - SDL_RenderGetViewport(renderer, &real_dstrect); - real_dstrect.x = 0; - real_dstrect.y = 0; + SDL_RenderGetViewport(renderer, &r); + real_dstrect.x = 0.0f; + real_dstrect.y = 0.0f; + real_dstrect.w = (float) r.w; + real_dstrect.h = (float) r.h; if (dstrect) { - if (!SDL_HasIntersection(dstrect, &real_dstrect)) { + if (!SDL_HasIntersectionF(dstrect, &real_dstrect)) { return 0; } real_dstrect = *dstrect; @@ -2509,31 +2873,56 @@ SDL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture, texture = texture->native; } - frect.x = real_dstrect.x * renderer->scale.x; - frect.y = real_dstrect.y * renderer->scale.y; - frect.w = real_dstrect.w * renderer->scale.x; - frect.h = real_dstrect.h * renderer->scale.y; + real_dstrect.x *= renderer->scale.x; + real_dstrect.y *= renderer->scale.y; + real_dstrect.w *= renderer->scale.x; + real_dstrect.h *= renderer->scale.y; texture->last_command_generation = renderer->render_command_generation; - retval = QueueCmdCopy(renderer, texture, &real_srcrect, &frect); + retval = QueueCmdCopy(renderer, texture, &real_srcrect, &real_dstrect); return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer); } - int SDL_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture, const SDL_Rect * srcrect, const SDL_Rect * dstrect, const double angle, const SDL_Point *center, const SDL_RendererFlip flip) { - SDL_Rect real_srcrect = { 0, 0, 0, 0 }; - SDL_Rect real_dstrect = { 0, 0, 0, 0 }; - SDL_Point real_center; - SDL_FRect frect; + SDL_FRect dstfrect; + SDL_FRect *pdstfrect = NULL; SDL_FPoint fcenter; + SDL_FPoint *pfcenter = NULL; + + if (dstrect) { + dstfrect.x = (float) dstrect->x; + dstfrect.y = (float) dstrect->y; + dstfrect.w = (float) dstrect->w; + dstfrect.h = (float) dstrect->h; + pdstfrect = &dstfrect; + } + + if (center) { + fcenter.x = (float) center->x; + fcenter.y = (float) center->y; + pfcenter = &fcenter; + } + + return SDL_RenderCopyExF(renderer, texture, srcrect, pdstfrect, angle, pfcenter, flip); +} + +int +SDL_RenderCopyExF(SDL_Renderer * renderer, SDL_Texture * texture, + const SDL_Rect * srcrect, const SDL_FRect * dstrect, + const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip) +{ + SDL_Rect real_srcrect; + SDL_FRect real_dstrect; + SDL_FPoint real_center; + int retval; if (flip == SDL_FLIP_NONE && (int)(angle/360) == angle/360) { /* fast path when we don't need rotation or flipping */ - return SDL_RenderCopy(renderer, texture, srcrect, dstrect); + return SDL_RenderCopyF(renderer, texture, srcrect, dstrect); } CHECK_RENDERER_MAGIC(renderer, -1); @@ -2565,9 +2954,12 @@ SDL_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture, if (dstrect) { real_dstrect = *dstrect; } else { - SDL_RenderGetViewport(renderer, &real_dstrect); - real_dstrect.x = 0; - real_dstrect.y = 0; + SDL_Rect r; + SDL_RenderGetViewport(renderer, &r); + real_dstrect.x = 0.0f; + real_dstrect.y = 0.0f; + real_dstrect.w = (float) r.w; + real_dstrect.h = (float) r.h; } if (texture->native) { @@ -2577,21 +2969,22 @@ SDL_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture, if (center) { real_center = *center; } else { - real_center.x = real_dstrect.w/2; - real_center.y = real_dstrect.h/2; + real_center.x = real_dstrect.w / 2.0f; + real_center.y = real_dstrect.h / 2.0f; } - frect.x = real_dstrect.x * renderer->scale.x; - frect.y = real_dstrect.y * renderer->scale.y; - frect.w = real_dstrect.w * renderer->scale.x; - frect.h = real_dstrect.h * renderer->scale.y; + real_dstrect.x *= renderer->scale.x; + real_dstrect.y *= renderer->scale.y; + real_dstrect.w *= renderer->scale.x; + real_dstrect.h *= renderer->scale.y; - fcenter.x = real_center.x * renderer->scale.x; - fcenter.y = real_center.y * renderer->scale.y; + real_center.x *= renderer->scale.x; + real_center.y *= renderer->scale.y; texture->last_command_generation = renderer->render_command_generation; - return QueueCmdCopyEx(renderer, texture, &real_srcrect, &frect, angle, &fcenter, flip); + retval = QueueCmdCopyEx(renderer, texture, &real_srcrect, &real_dstrect, angle, &real_center, flip); + return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer); } int diff --git a/src/render/SDL_sysrender.h b/src/render/SDL_sysrender.h index 240ce2de12..87586f1bb9 100644 --- a/src/render/SDL_sysrender.h +++ b/src/render/SDL_sysrender.h @@ -39,20 +39,6 @@ typedef enum SDL_ScaleModeBest } SDL_ScaleMode; -typedef struct -{ - float x; - float y; -} SDL_FPoint; - -typedef struct -{ - float x; - float y; - float w; - float h; -} SDL_FRect; - /* Define the SDL texture structure */ struct SDL_Texture { -- cgit v1.2.3