Browse code

make reui_balloon() able to display several lines when input string has newline characters or when the line is too large for the screen width

Dario Rodriguez authored on 21/02/2022 19:02:33
Showing 1 changed files
... ...
@@ -351,7 +351,7 @@ reui_balloon(reui_t *ui, char direction, int x, int y, const char *rgbafg, const
351 351
 {
352 352
         char buf[1024];
353 353
         SDL_Surface *bgsurface;
354
-        SDL_Surface *fgsurface;
354
+        SDL_Surface *fgsurface,*linefgsurface;
355 355
         SDL_Texture *tex;
356 356
         SDL_Color c={((unsigned char *)rgbafg)[0],
357 357
                      ((unsigned char *)rgbafg)[1],
... ...
@@ -363,6 +363,8 @@ reui_balloon(reui_t *ui, char direction, int x, int y, const char *rgbafg, const
363 363
         int marginoffx,marginoffy;
364 364
         int radius;
365 365
         int offx,offy;
366
+        int i,w,iw,maxw,lastspace,lastspacew,nlines;
367
+        char *ptr,*end;
366 368
         if(ui==NULL || rgbafg==NULL || rgbabg==NULL || (str==NULL && nchar!=0))
367 369
                 return(-1);
368 370
         if(nchar<sizeof(buf)) {
... ...
@@ -372,8 +374,63 @@ reui_balloon(reui_t *ui, char direction, int x, int y, const char *rgbafg, const
372 374
                 memcpy(buf,str,sizeof(buf)-1);
373 375
                 buf[sizeof(buf)-1]='\0';
374 376
         }
375
-        if((fgsurface=TTF_RenderUTF8_Blended(ui->font,buf,c))==NULL)
376
-                return(-1);
377
+        /* split into lines if width is greater than screen width */
378
+        nlines=1;
379
+        for(i=0,w=0,lastspace=-1,maxw=0;buf[i]!='\0';w+=iw,i++) {
380
+                iw=((buf[i]&0xc0)!=0x80)?ui->fontwidth:0;
381
+                if(buf[i]==' ') {
382
+                        lastspace=i;
383
+                        lastspacew=w;
384
+                }
385
+                if(w>(ui->w-ui->fontwidth*2)) {
386
+                        nlines++;
387
+                        if(lastspace==-1) {
388
+                                memmove(buf+i+1,buf+i,sizeof(buf)-i-1);
389
+                                buf[i]='\n';
390
+                                buf[sizeof(buf)-1]='\0';
391
+                                maxw=(w>maxw)?w:maxw;
392
+                                iw=w=0;
393
+                                continue;
394
+                        } else {
395
+                                buf[lastspace]='\n';
396
+                                i=lastspace+1;
397
+                                w=lastspacew;
398
+                                maxw=(w>maxw)?w:maxw;
399
+                                iw=w=0;
400
+                                continue;
401
+                        }
402
+                }
403
+        }
404
+        maxw=(w>maxw)?w:maxw;
405
+        /* render fg */
406
+        if(strchr(buf,'\n')==NULL) {
407
+                /* text without newlines */
408
+                if((fgsurface=TTF_RenderUTF8_Blended(ui->font,buf,c))==NULL)
409
+                        return(-1);
410
+        } else {
411
+                /* text with newlines */
412
+                if((fgsurface=SDL_CreateRGBSurface(0
413
+                      ,maxw,nlines*ui->fontheight
414
+                      ,32, ui->rmask,ui->gmask, ui->bmask, ui->amask))==NULL) {
415
+                        return(-1); /* couldn't create fg surface */
416
+                }
417
+                for(i=0,ptr=buf,end=strchr(ptr,'\n')
418
+                  ;ptr!=NULL
419
+                  ;ptr=((end!=NULL)?end+1:NULL),end=((ptr!=NULL)?strchr(ptr,'\n'):NULL),i++) {
420
+                        if(end!=NULL)
421
+                                *end='\0';
422
+                        if(*ptr=='\0')
423
+                                continue; /* blank line */
424
+                        if((linefgsurface=TTF_RenderUTF8_Blended(ui->font,ptr,c))==NULL) {
425
+                                SDL_FreeSurface(fgsurface),fgsurface=NULL;
426
+                                return(-1); /* couldn't create linefssurface */
427
+                        }
428
+                        RECTFILL(dstrect,0,i*ui->fontheight,linefgsurface->w,linefgsurface->h);
429
+                        SDL_BlitSurface(linefgsurface,NULL,fgsurface,&dstrect);
430
+                        SDL_FreeSurface(linefgsurface),linefgsurface=NULL;
431
+                }                
432
+        }
433
+        /* render bg */
377 434
         trianglew=ui->fontwidth;
378 435
         triangleh=ui->fontheight/2;
379 436
         marginw=ui->fontwidth*4/3;