summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2020-08-18 10:53:01 +0300
committerJaakko Keränen <jaakko.keranen@iki.fi>2020-08-18 10:53:01 +0300
commitef297dafa84154d8f28f014410ab742f7994c557 (patch)
treeae092c965eb93ec855f6f833606d6904cefa263e /src
parent6a550da2b01921e66f49badff11ac6ab9b88cd21 (diff)
Render target switching
Diffstat (limited to 'src')
-rw-r--r--src/ui/paint.c18
-rw-r--r--src/ui/paint.h10
-rw-r--r--src/ui/text.c6
3 files changed, 29 insertions, 5 deletions
diff --git a/src/ui/paint.c b/src/ui/paint.c
index 85e75f15..a55670e9 100644
--- a/src/ui/paint.c
+++ b/src/ui/paint.c
@@ -1,5 +1,7 @@
1#include "paint.h" 1#include "paint.h"
2 2
3#include <SDL_version.h>
4
3iLocalDef SDL_Renderer *renderer_Paint_(const iPaint *d) { 5iLocalDef SDL_Renderer *renderer_Paint_(const iPaint *d) {
4 iAssert(d->dst); 6 iAssert(d->dst);
5 return d->dst->render; 7 return d->dst->render;
@@ -12,6 +14,18 @@ static void setColor_Paint_(const iPaint *d, int color) {
12 14
13void init_Paint(iPaint *d) { 15void init_Paint(iPaint *d) {
14 d->dst = get_Window(); 16 d->dst = get_Window();
17 d->oldTarget = NULL;
18}
19
20void beginTarget_Paint(iPaint *d, SDL_Texture *target) {
21 SDL_Renderer *rend = renderer_Paint_(d);
22 d->oldTarget = SDL_GetRenderTarget(rend);
23 SDL_SetRenderTarget(rend, target);
24}
25
26void endTarget_Paint(iPaint *d) {
27 SDL_SetRenderTarget(renderer_Paint_(d), d->oldTarget);
28 d->oldTarget = NULL;
15} 29}
16 30
17void setClip_Paint(iPaint *d, iRect rect) { 31void setClip_Paint(iPaint *d, iRect rect) {
@@ -19,8 +33,12 @@ void setClip_Paint(iPaint *d, iRect rect) {
19} 33}
20 34
21void unsetClip_Paint(iPaint *d) { 35void unsetClip_Paint(iPaint *d) {
36#if SDL_VERSION_ATLEAST(2, 0, 12)
37 SDL_RenderSetClipRect(renderer_Paint_(d), NULL);
38#else
22 const SDL_Rect winRect = { 0, 0, d->dst->root->rect.size.x, d->dst->root->rect.size.y }; 39 const SDL_Rect winRect = { 0, 0, d->dst->root->rect.size.x, d->dst->root->rect.size.y };
23 SDL_RenderSetClipRect(renderer_Paint_(d), &winRect); 40 SDL_RenderSetClipRect(renderer_Paint_(d), &winRect);
41#endif
24} 42}
25 43
26void drawRect_Paint(const iPaint *d, iRect rect, int color) { 44void drawRect_Paint(const iPaint *d, iRect rect, int color) {
diff --git a/src/ui/paint.h b/src/ui/paint.h
index 90c5f504..aafc7496 100644
--- a/src/ui/paint.h
+++ b/src/ui/paint.h
@@ -9,12 +9,16 @@ iDeclareType(Paint)
9 9
10struct Impl_Paint { 10struct Impl_Paint {
11 iWindow *dst; 11 iWindow *dst;
12 SDL_Texture *oldTarget;
12}; 13};
13 14
14void init_Paint (iPaint *); 15void init_Paint (iPaint *);
15 16
16void setClip_Paint (iPaint *, iRect rect); 17void beginTarget_Paint (iPaint *, SDL_Texture *target);
17void unsetClip_Paint (iPaint *); 18void endTarget_Paint (iPaint *);
19
20void setClip_Paint (iPaint *, iRect rect);
21void unsetClip_Paint (iPaint *);
18 22
19void drawRect_Paint (const iPaint *, iRect rect, int color); 23void drawRect_Paint (const iPaint *, iRect rect, int color);
20void drawRectThickness_Paint (const iPaint *, iRect rect, int thickness, int color); 24void drawRectThickness_Paint (const iPaint *, iRect rect, int thickness, int color);
diff --git a/src/ui/text.c b/src/ui/text.c
index f947c4df..52af34bb 100644
--- a/src/ui/text.c
+++ b/src/ui/text.c
@@ -306,10 +306,11 @@ static void cache_Font_(iFont *d, iGlyph *glyph, int hoff) {
306 } 306 }
307 /* Determine placement in the glyph cache texture, advancing in rows. */ 307 /* Determine placement in the glyph cache texture, advancing in rows. */
308 glRect->pos = assignCachePos_Text_(txt, glRect->size); 308 glRect->pos = assignCachePos_Text_(txt, glRect->size);
309 SDL_Texture *oldTarget = SDL_GetRenderTarget(render);
309 SDL_SetRenderTarget(render, txt->cache); 310 SDL_SetRenderTarget(render, txt->cache);
310 const SDL_Rect dstRect = sdlRect_(*glRect); 311 const SDL_Rect dstRect = sdlRect_(*glRect);
311 SDL_RenderCopy(render, tex, &(SDL_Rect){ 0, 0, dstRect.w, dstRect.h }, &dstRect); 312 SDL_RenderCopy(render, tex, &(SDL_Rect){ 0, 0, dstRect.w, dstRect.h }, &dstRect);
312 SDL_SetRenderTarget(render, NULL); 313 SDL_SetRenderTarget(render, oldTarget);
313 if (tex) { 314 if (tex) {
314 SDL_DestroyTexture(tex); 315 SDL_DestroyTexture(tex);
315 iAssert(surface); 316 iAssert(surface);
@@ -670,9 +671,10 @@ void init_TextBuf(iTextBuf *d, int font, const char *text) {
670 d->size.x, 671 d->size.x,
671 d->size.y); 672 d->size.y);
672 SDL_SetTextureBlendMode(d->texture, SDL_BLENDMODE_BLEND); 673 SDL_SetTextureBlendMode(d->texture, SDL_BLENDMODE_BLEND);
674 SDL_Texture *oldTarget = SDL_GetRenderTarget(render);
673 SDL_SetRenderTarget(render, d->texture); 675 SDL_SetRenderTarget(render, d->texture);
674 draw_Text_(font, zero_I2(), white_ColorId, range_CStr(text)); 676 draw_Text_(font, zero_I2(), white_ColorId, range_CStr(text));
675 SDL_SetRenderTarget(render, NULL); 677 SDL_SetRenderTarget(render, oldTarget);
676} 678}
677 679
678void deinit_TextBuf(iTextBuf *d) { 680void deinit_TextBuf(iTextBuf *d) {