Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text Flickering on Window Resize in SDL2 && SDL3 #11656

Open
rjopek opened this issue Dec 17, 2024 · 0 comments
Open

Text Flickering on Window Resize in SDL2 && SDL3 #11656

rjopek opened this issue Dec 17, 2024 · 0 comments

Comments

@rjopek
Copy link

rjopek commented Dec 17, 2024

I raised this issue on the discourse.libsdl.org forum, but there wasn't much interest in helping me. The example shows that the text loses precision, or perhaps I am not using the correct functions to handle it properly.

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stddef.h>

#include <SDL3/SDL.h>
#include <SDL3_ttf/SDL_ttf.h>

#define LEN( n ) ( sizeof( n ) / sizeof( ( n ) [ 0 ] ) )

static size_t iUtf8Len( const char *szUtf8String )
{
   size_t iLen = 0;
   while( *szUtf8String )
   {
      unsigned char cByte = *szUtf8String;

      if( ( cByte & 0x80 ) == 0 )         // ASCII (0xxxxxxx)
         szUtf8String += 1;
      else if( ( cByte & 0xE0 ) == 0xC0 ) // (110xxxxx)
         szUtf8String += 2;
      else if( ( cByte & 0xF0 ) == 0xE0 ) // (1110xxxx)
         szUtf8String += 3;
      else if( ( cByte & 0xF8 ) == 0xF0 ) // (11110xxx)
         szUtf8String += 4;
      else
         szUtf8String += 1;

      iLen++;
   }

   return iLen;
}

void calculateFontCellSize( TTF_Font *pFont, int *iFontCellWidth, int *iFontCellHeight )
{
   const char *szTestChar = "M";
   int iMeasuredWidth = 0;
   size_t iMeasuredLength = 0;

   TTF_MeasureString( pFont, szTestChar, 0, 0, &iMeasuredWidth, &iMeasuredLength );

   *iFontCellWidth = iMeasuredWidth;
   *iFontCellHeight = TTF_GetFontHeight( pFont );
}

void drawText( TTF_Font *pFont, SDL_Renderer *pRenderer, float fX, float fY, const char *szText, SDL_Color sFg, SDL_Color sBg )
{
   int iFontCellWidth, iFontCellHeight;

   calculateFontCellSize( pFont, &iFontCellWidth, &iFontCellHeight );

   fX *= iFontCellWidth;
   fY *= iFontCellHeight;

   size_t iLen = iUtf8Len( szText );
   if( iLen == 0 )
   {
      SDL_FRect sRect = { fX, fY, ( float ) iFontCellWidth, ( float ) iFontCellHeight };
      SDL_SetRenderDrawColor( pRenderer, sBg.r, sBg.g, sBg.b, sBg.a );
      SDL_RenderFillRect( pRenderer, &sRect );
      return;
   }

   SDL_Surface *pSurface = TTF_RenderText_Shaded( pFont, szText, 0, sFg, sBg );
   if( pSurface )
   {
      SDL_Texture *pTextTexture = SDL_CreateTextureFromSurface( pRenderer, pSurface );
      SDL_DestroySurface( pSurface );

      if( pTextTexture )
      {
         SDL_FRect sTextRect = { fX, fY, ( float ) iFontCellWidth * iLen, ( float ) iFontCellHeight };
         SDL_RenderTexture( pRenderer, pTextTexture, NULL, &sTextRect );
         SDL_DestroyTexture( pTextTexture );
      }
      else
      {
         fprintf( stderr, "Error creating texture: %s\n", SDL_GetError() );
      }
   }
   else
   {
      fprintf( stderr, "Error rendering text: %s\n", SDL_GetError() );
   }
}

int main()
{
   SDL_Window *pWindow = NULL;
   SDL_Renderer *pRenderer = NULL;
   TTF_Font *pFont = NULL;
   SDL_Event sEvent;
   bool lQuit = false;
   SDL_Color sFg = { 0, 0, 0, 255 };
   SDL_Color sBg = { 255, 255, 255, 255 };
   const char *szText[] =
   {
      "",
      "Mathematics and sciences:",
      "",
      "  ∮ E⋅da = Q,  n → ∞, ∑ f(i) = ∏ g(i),      ⎧⎡⎛┌─────┐⎞⎤⎫",
      "                                            ⎪⎢⎜│a²+b³ ⎟⎥⎪",
      "  ∀x∈ℝ: ⌈x⌉ = −⌊−x⌋, α ∧ ¬β = ¬(¬α ∨ β),    ⎪⎢⎜│───── ⎟⎥⎪",
      "                                            ⎪⎢⎜⎷ c₈   ⎟⎥⎪",
      "  ℕ ⊆ ℕ₀ ⊂ ℤ ⊂ ℚ ⊂ ℝ ⊂ ℂ,                   ⎨⎢⎜       ⎟⎥⎬",
      "                                            ⎪⎢⎜ ∞     ⎟⎥⎪",
      "  ⊥ < a ≠ b ≡ c ≤ d ≪ ⊤ ⇒ (⟦A⟧ ⇔ ⟪B⟫),      ⎪⎢⎜ ⎲     ⎟⎥⎪",
      "                                            ⎪⎢⎜ ⎳aⁱ-bⁱ⎟⎥⎪",
      "  2H₂ + O₂ ⇌ 2H₂O, R = 4.7 kΩ, ⌀ 200 mm     ⎩⎣⎝i=1    ⎠⎦⎭",
      "",
      "Linguistics and dictionaries:",
      "",
      "  ði ıntəˈnæʃənəl fəˈnɛtık əsoʊsiˈeıʃn",
      "  Y [ˈʏpsilɔn], Yen [jɛn], Yoga [ˈjoːgɑ]",
      "",
      "APL:",
      "",
      "  ((V⍳V)=⍳⍴V)/V←,V    ⌷←⍳→⍴∆∇⊃‾⍎⍕⌈"
   };

   int iLines = LEN( szText );

   SDL_SetHint( SDL_HINT_SHUTDOWN_DBUS_ON_QUIT, "1" );

   if( !SDL_Init( SDL_INIT_VIDEO ) )
   {
      fprintf( stderr, "Error initializing SDL: %s\n", SDL_GetError() );
      return 1;
   }

   if( !TTF_Init() )
   {
      fprintf( stderr, "Error initializing SDL_ttf: %s\n", SDL_GetError() );
      SDL_Quit();
      return 1;
   }

   pWindow = SDL_CreateWindow( "Simple SDL3 Text", 800, 600, SDL_WINDOW_RESIZABLE );
   if( pWindow == NULL )
   {
      fprintf( stderr, "Error creating SDL window: %s\n", SDL_GetError() );
      SDL_Quit();
      return 1;
   }

   pRenderer = SDL_CreateRenderer( pWindow, NULL );
   if( pRenderer == NULL )
   {
      fprintf( stderr, "Error creating SDL renderer: %s\n", SDL_GetError() );
      SDL_DestroyWindow( pWindow );
      SDL_Quit();
      return 1;
   }

   pFont = TTF_OpenFont( "../../docs/assets/font/9x18.pcf.gz", 18 );
   if( pFont == NULL )
   {
      fprintf( stderr, "Error opening SDL_ttf font: %s\n", SDL_GetError() );
      SDL_DestroyRenderer( pRenderer );
      SDL_DestroyWindow( pWindow );
      SDL_Quit();
      return 1;
   }

   while( !lQuit )
   {
      while( SDL_PollEvent( &sEvent ) )
      {
         switch( sEvent.type )
         {
         case SDL_EVENT_QUIT:
            lQuit = true;
            break;
         case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
            lQuit = true;
            break;
         }

      }

      SDL_SetRenderDrawColor( pRenderer, 50, 50, 50, 255 );
      SDL_RenderClear( pRenderer );

      for( int i = 0; i < iLines; i++ )
      {
         drawText( pFont, pRenderer, 1, i, szText[ i ], sFg, sBg );
      }

      SDL_RenderPresent( pRenderer );
   }

   TTF_CloseFont( pFont );
   SDL_DestroyRenderer( pRenderer );
   SDL_DestroyWindow( pWindow );
   TTF_Quit();
   SDL_Quit();

   return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant