Browse code

ui: remove experimental font cache implementation as it was slower than naive implementation

Dario Rodriguez authored on 11/11/2020 21:44:45
Showing 2 changed files
... ...
@@ -20,8 +20,6 @@
20 20
 #define DEFAULTTITLEPREFIX "re - "
21 21
 #define DEFAULTWINDOWWIDTH 80
22 22
 #define DEFAULTWINDOWHEIGHT 66
23
-#define FONTCACHEXSIZE 512
24
-#define FONTCACHEYSIZE 512
25 23
 
26 24
 #define RECTFILL(r,rx,ry,rw,rh) (r).x=(rx),(r).y=(ry),(r).w=(rw),(r).h=(rh)
27 25
 
... ...
@@ -48,7 +46,6 @@
48 46
 
49 47
 static int reui_fillrounded(SDL_Surface *dst, int xo, int yo, int w, int h, int r, const char *rgba);
50 48
 static int reui_triangle(SDL_Surface *dst, int x, int y, int w, int h, char direction, const char *rgba);
51
-static int reui_fontcachereset(reui_t *ui);
52 49
 
53 50
 reui_t *
54 51
 reui_init(int fontheight)
... ...
@@ -126,15 +123,10 @@ reui_init(int fontheight)
126 123
           || (ui->scr=SDL_CreateTexture(ui->renderer,SDL_PIXELFORMAT_ARGB8888,
127 124
               SDL_TEXTUREACCESS_STREAMING,ui->w,ui->h))==NULL
128 125
           || (ui->onepx=SDL_CreateTexture(ui->renderer,SDL_PIXELFORMAT_ARGB8888,
129
-              SDL_TEXTUREACCESS_STATIC,1,1))==NULL
130
-          || (ui->fontcache=SDL_CreateTexture(ui->renderer,SDL_PIXELFORMAT_ARGB8888,
131
-              SDL_TEXTUREACCESS_STATIC,FONTCACHEXSIZE,FONTCACHEYSIZE))==NULL) {
126
+              SDL_TEXTUREACCESS_STATIC,1,1))==NULL) {
132 127
                 reui_free(ui),ui=NULL;
133 128
                 return(NULL);
134 129
         }
135
-        /* font cache */
136
-        SDL_SetTextureBlendMode(ui->fontcache, SDL_BLENDMODE_BLEND);
137
-        reui_fontcachereset(ui);
138 130
         /* finished */
139 131
         ui->scrdirty=1;
140 132
         ui->rendererdirty=1;
... ...
@@ -147,8 +139,6 @@ reui_free(reui_t *ui)
147 139
 {
148 140
         if(ui==NULL)
149 141
                 return;
150
-        if(ui->fontcache!=NULL)
151
-                SDL_DestroyTexture(ui->fontcache),ui->fontcache=NULL;
152 142
         if(ui->onepx!=NULL)
153 143
                 SDL_DestroyTexture(ui->onepx),ui->onepx=NULL;
154 144
         if(ui->font!=NULL)
... ...
@@ -223,8 +213,6 @@ reui_setfontheight(reui_t *ui, int fontheight)
223 213
         ui->fontwidth=s->w;
224 214
         SDL_FreeSurface(s),s=NULL;
225 215
         SDL_FreeSurface(s2),s2=NULL;
226
-        /* font cache */
227
-        reui_fontcachereset(ui);
228 216
         return(0);
229 217
 }
230 218
 
... ...
@@ -272,7 +260,6 @@ reui_present(reui_t *ui)
272 260
 int
273 261
 reui_write(reui_t *ui, int x, int y, const char *rgba, const char *str, int nchar)
274 262
 {
275
-#if 0
276 263
         char buf[1024];
277 264
         SDL_Surface *fgsurface;
278 265
         SDL_Texture *fg;
... ...
@@ -300,84 +287,6 @@ reui_write(reui_t *ui, int x, int y, const char *rgba, const char *str, int ncha
300 287
         SDL_FreeSurface(fgsurface),fgsurface=NULL;
301 288
         SDL_DestroyTexture(fg),fg=NULL;
302 289
         return(0);
303
-#else
304
-        int curcol;
305
-        char smallbuf[2];
306
-        int maxcached;
307
-        int cachelines,cachecols;
308
-        Uint32 *pixels;
309
-        char buf[1024];
310
-        SDL_Surface *fgsurface;
311
-        SDL_Texture *fg;
312
-        SDL_Color c={((unsigned char *)rgba)[0],
313
-                     ((unsigned char *)rgba)[1],
314
-                     ((unsigned char *)rgba)[2],
315
-                     ((unsigned char *)rgba)[3]};
316
-        SDL_Color whitec={255,255,255,255};
317
-        SDL_Rect srcrect,dstrect;
318
-        int i,n;
319
-        int has_uncached;
320
-        if(nchar<sizeof(buf)) {
321
-                memcpy(buf,str,nchar);
322
-                buf[nchar]='\0';
323
-        } else {
324
-                memcpy(buf,str,sizeof(buf)-1);
325
-                buf[sizeof(buf)-1]='\0';
326
-        }
327
-        SDL_SetTextureColorMod(ui->fontcache, c.r, c.g, c.b);
328
-        cachelines=(ui->fontheight>0)?(FONTCACHEYSIZE/ui->fontheight):0;
329
-        cachecols=(ui->fontwidth>0)?(FONTCACHEXSIZE/ui->fontwidth):0;
330
-        maxcached=cachelines*cachecols;
331
-        /* write cached characters (or fill cache) */
332
-        has_uncached=0;
333
-        for(i=0,curcol=-1;buf[i]!='\0';i++) {
334
-                curcol+=(((buf[i]&0xc0)!=0x80)?1:0); /* this detects an utf-8 start byte, and advances one pos if true */
335
-                if(!(buf[i]>=FONTCACHESTART && buf[i]<=FONTCACHEEND) || maxcached==0) {
336
-                        has_uncached=1;
337
-                        continue;
338
-                }
339
-                /* fill cache if possible */
340
-                if(ui->usedfontcache<maxcached && ui->fontcachewhere[buf[i]-FONTCACHESTART]==0) {
341
-                        smallbuf[0]=buf[i],smallbuf[1]='\0';
342
-                        if((fgsurface=TTF_RenderUTF8_Blended(ui->font,smallbuf,whitec))==NULL)
343
-                                return(-1); /* cannot create text */
344
-                        SDL_LockSurface(fgsurface);
345
-                        pixels=(Uint32*)(fgsurface->pixels);
346
-                        RECTFILL(dstrect,(ui->usedfontcache%cachecols)*ui->fontwidth,(ui->usedfontcache/cachecols)*ui->fontheight,ui->fontwidth,ui->fontheight);
347
-                        SDL_UpdateTexture(ui->fontcache,&dstrect,pixels,sizeof(Uint32)*fgsurface->w);
348
-                        SDL_UnlockSurface(fgsurface);
349
-                        if(fgsurface!=NULL)
350
-                                SDL_FreeSurface(fgsurface),fgsurface=NULL;
351
-                        ui->usedfontcache++;
352
-                        ui->fontcachewhere[buf[i]-FONTCACHESTART]=ui->usedfontcache; /* one more than real pos, as 0 is not init */
353
-                }
354
-                /* if cached, write from there and change the letter to space */
355
-                if(ui->fontcachewhere[buf[i]-FONTCACHESTART]!=0) {
356
-                        n=ui->fontcachewhere[buf[i]-FONTCACHESTART]-1;
357
-                        RECTFILL(srcrect,((n%cachecols)*ui->fontwidth),((n/cachecols)*ui->fontheight),ui->fontwidth,ui->fontheight);
358
-                        RECTFILL(dstrect,(x+ui->fontwidth*curcol),y,ui->fontwidth,ui->fontheight);
359
-                        SDL_RenderCopy(ui->renderer,ui->fontcache,&srcrect,&dstrect);
360
-                        buf[i]=' ';
361
-                } else {
362
-                        has_uncached=1;
363
-                }
364
-        }
365
-        /* write uncached characters */
366
-        if(has_uncached) {
367
-                if((fgsurface=TTF_RenderUTF8_Blended(ui->font,buf,c))==NULL
368
-                  || (fg=SDL_CreateTextureFromSurface(ui->renderer,fgsurface))==NULL) {
369
-                        if(fgsurface!=NULL)
370
-                                SDL_FreeSurface(fgsurface),fgsurface=NULL;
371
-                        return(-1);
372
-                }
373
-                RECTFILL(dstrect,x,y,fgsurface->w,fgsurface->h);
374
-                SDL_RenderCopy(ui->renderer,fg,NULL,&dstrect);
375
-                SDL_FreeSurface(fgsurface),fgsurface=NULL;
376
-                SDL_DestroyTexture(fg),fg=NULL;
377
-        }
378
-        ui->rendererdirty=1;
379
-        return(0);
380
-#endif
381 290
 }
382 291
 
383 292
 
... ...
@@ -627,12 +536,3 @@ reui_triangle(SDL_Surface *dst, int x, int y, int w, int h, char direction, cons
627 536
         return(0);
628 537
 }
629 538
 
630
-static int
631
-reui_fontcachereset(reui_t *ui)
632
-{
633
-        if(ui==NULL || ui->fontcache==NULL)
634
-                return(-1); /* sanity check failed */
635
-        ui->usedfontcache=0;
636
-        memset(ui->fontcachewhere,0,sizeof(ui->fontcachewhere));
637
-        return(0);
638
-}
... ...
@@ -17,9 +17,6 @@
17 17
 #include "SDL_ttf.h"
18 18
 
19 19
 
20
-#define FONTCACHESTART ' '
21
-#define FONTCACHEEND '~'
22
-
23 20
 typedef struct reui_t {
24 21
         int screenw,screenh;
25 22
         int w,h;
... ...
@@ -30,9 +27,6 @@ typedef struct reui_t {
30 27
         SDL_RWops *fontdata;
31 28
         TTF_Font *font;
32 29
         SDL_Texture *onepx;
33
-        int usedfontcache;
34
-        SDL_Texture *fontcache;
35
-        int fontcachewhere[FONTCACHEEND-FONTCACHESTART];
36 30
         int fontheight;
37 31
         int fontwidth;
38 32
         int scrdirty;