1

I have opened a window which shows two rects on the screen then using SDL_TTF to show the mouse position on the screen.

The bit I am having hard time understanding is why after rendering text the the two rects before it do not show up.

I am using SDL_RenderFillRect to draw two rects on screen

SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderFillRect(renderer, rect1);

SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
SDL_RenderFillRect(renderer, rect2);

Code for rendering the text is

// define string with mouse x, y coords
sprintf(someString, "x: %d, y: %d", mouse.x, mouse.y);
SDL_Point textPos = {10, 10};
WriteText(renderer, font, someString, textPos, (SDL_Color){255, 255, 255, 255});
SDL_Surface *fontSurface = TTF_RenderText_Blended(font, someString, COLOR_BLACK); // create font surface
SDL_Texture *fontTexture = SDL_CreateTextureFromSurface(renderer, fontSurface);  // create the texture

// get clip width and height from fontsurface clip rect
SDL_Rect *fontRect = &fontSurface->clip_rect;
fontRect->x = pos.x;
fontRect->y = pos.y;

SDL_RenderCopy(renderer, fontTexture, NULL, fontRect); // copy text to the renderer
// delete surface and texture
SDL_FreeSurface(fontSurface);
SDL_DestroyTexture(fontTexture);

It to shows the mouse positon top left corner of the window. However this makes the rest of the window blank.

To prevent this my work around is having to draw something on the screen after calling SDL_RendererCopy (and weirdly before calling SDL_DestroyTexture too) For example drawing single point

...
SDL_RenderCopy(renderer, fontTexture, NULL, fontRect); // copy text to the renderer

// why is this needed??
SDL_RenderDrawPoint(renderer, 0, 0);

// delete surface and texture
SDL_FreeSurface(fontSurface);
SDL_DestroyTexture(fontTexture); // have to draw a point before this
...

This then shows the two rects rendered before the text

If I set dstRect to NULL when calling SDL_RenderCopy then the text spans the whole window but I can see what was rendered before underneath the text.

Why am I having to draw a point after calling SDL_RenderCopy to stop what was rendered before from not showing up?

NOTE: Link to full source code https://pastebin.com/tRSFT0PV

genpfault
  • 51,148
  • 11
  • 85
  • 139
DanM
  • 19
  • 1
  • I compiled the source in linux, I changed the font (I don't have Arial), I see no difference commenting `SDL_RenderDrawPoint`, it always works as supposed, text coordinate mouse + 2 rectangles (SDL version 2.0.9) – Alex Aug 19 '19 at 06:00
  • 1
    Looks like a bug introduced in https://hg.libsdl.org/SDL/rev/5b0c4bfbd083 (which is a fix for another issue). I suppose a bug should be reported to SDL bugzilla (if you can't or don't want to, please let me know in a comment). As a workaround you can use another SDL renderer implementation (e.g. opengl). – keltar Aug 19 '19 at 06:04
  • Intresting I compiled this code on a Manjaro Linux VM using gcc and it ran just fine without the need for `SDL_RenderDrawPoint`. When compiling on Win10 using Visual Studio Code configured to compile using mingw32-gcc.exe then I have this bug. Both are using SDL version 2.0.10 which the bug keltar linked to should be fixed for? – DanM Aug 19 '19 at 16:50
  • @DanM no, it is in 2.0.10 (and only with direct3d renderer), but not in e.g. 2.0.9. https://bugzilla.libsdl.org/show_bug.cgi?id=4768 – keltar Aug 20 '19 at 05:00

1 Answers1

0

This is a bug in SDL 2.0.10. It's fixed by https://hg.libsdl.org/SDL/rev/6ee12b88beed and this fix will ship with 2.0.11. Sorry about that!

Ryan C. Gordon
  • 186
  • 1
  • 3