diff options
author | Jaakko Keränen <jaakko.keranen@iki.fi> | 2020-08-18 10:53:01 +0300 |
---|---|---|
committer | Jaakko Keränen <jaakko.keranen@iki.fi> | 2020-08-18 10:53:01 +0300 |
commit | ef297dafa84154d8f28f014410ab742f7994c557 (patch) | |
tree | ae092c965eb93ec855f6f833606d6904cefa263e | |
parent | 6a550da2b01921e66f49badff11ac6ab9b88cd21 (diff) |
Render target switching
-rw-r--r-- | src/ui/paint.c | 18 | ||||
-rw-r--r-- | src/ui/paint.h | 10 | ||||
-rw-r--r-- | src/ui/text.c | 6 |
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 | |||
3 | iLocalDef SDL_Renderer *renderer_Paint_(const iPaint *d) { | 5 | iLocalDef 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 | ||
13 | void init_Paint(iPaint *d) { | 15 | void init_Paint(iPaint *d) { |
14 | d->dst = get_Window(); | 16 | d->dst = get_Window(); |
17 | d->oldTarget = NULL; | ||
18 | } | ||
19 | |||
20 | void 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 | |||
26 | void endTarget_Paint(iPaint *d) { | ||
27 | SDL_SetRenderTarget(renderer_Paint_(d), d->oldTarget); | ||
28 | d->oldTarget = NULL; | ||
15 | } | 29 | } |
16 | 30 | ||
17 | void setClip_Paint(iPaint *d, iRect rect) { | 31 | void setClip_Paint(iPaint *d, iRect rect) { |
@@ -19,8 +33,12 @@ void setClip_Paint(iPaint *d, iRect rect) { | |||
19 | } | 33 | } |
20 | 34 | ||
21 | void unsetClip_Paint(iPaint *d) { | 35 | void 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 | ||
26 | void drawRect_Paint(const iPaint *d, iRect rect, int color) { | 44 | void 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 | ||
10 | struct Impl_Paint { | 10 | struct Impl_Paint { |
11 | iWindow *dst; | 11 | iWindow *dst; |
12 | SDL_Texture *oldTarget; | ||
12 | }; | 13 | }; |
13 | 14 | ||
14 | void init_Paint (iPaint *); | 15 | void init_Paint (iPaint *); |
15 | 16 | ||
16 | void setClip_Paint (iPaint *, iRect rect); | 17 | void beginTarget_Paint (iPaint *, SDL_Texture *target); |
17 | void unsetClip_Paint (iPaint *); | 18 | void endTarget_Paint (iPaint *); |
19 | |||
20 | void setClip_Paint (iPaint *, iRect rect); | ||
21 | void unsetClip_Paint (iPaint *); | ||
18 | 22 | ||
19 | void drawRect_Paint (const iPaint *, iRect rect, int color); | 23 | void drawRect_Paint (const iPaint *, iRect rect, int color); |
20 | void drawRectThickness_Paint (const iPaint *, iRect rect, int thickness, int color); | 24 | void 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 | ||
678 | void deinit_TextBuf(iTextBuf *d) { | 680 | void deinit_TextBuf(iTextBuf *d) { |