Browse code

Make the function listing slightly transparent (as intended)

Dario Rodriguez authored on 05/03/2022 19:08:23
Showing 1 changed files
... ...
@@ -275,6 +275,37 @@ reui_fillrounded(reui_t *ui, int x, int y, int w, int h, const char *rgba)
275 275
         return(0);
276 276
 }
277 277
 
278
+int
279
+reui_fillblended(reui_t *ui, int x, int y, int w, int h, const char *rgba)
280
+{
281
+        SDL_Surface *bgsurface;
282
+        SDL_Texture *tex;
283
+        SDL_Rect dstrect;
284
+        Uint32 color;
285
+        if(ui==NULL || rgba==NULL)
286
+                return(-1);
287
+        if((bgsurface=SDL_CreateRGBSurface(0
288
+          ,1
289
+          ,1
290
+          ,32, ui->rmask, ui->gmask, ui->bmask, ui->amask))==NULL) {
291
+                return(-1);
292
+        }
293
+        color=CHARRGBA2UINT(rgba);
294
+        RECTFILL(dstrect,0,0,1,1);
295
+        SDL_FillRect(bgsurface,&dstrect,color);
296
+        if((tex=SDL_CreateTextureFromSurface(ui->renderer,bgsurface))==NULL) {
297
+                SDL_FreeSurface(bgsurface),bgsurface=NULL;
298
+                return(-1);
299
+        }
300
+        RECTFILL(dstrect,x,y,w,h);
301
+        SDL_RenderCopy(ui->renderer,tex,NULL,&dstrect);
302
+        ui->rendererdirty=1;
303
+        SDL_FreeSurface(bgsurface),bgsurface=NULL;
304
+        SDL_DestroyTexture(tex),tex=NULL;
305
+        return(0);
306
+}
307
+
308
+
278 309
 
279 310
 int
280 311
 reui_scr2renderer(reui_t *ui, int x, int y, int w, int h)
... ...
@@ -493,7 +524,6 @@ reui_balloon(reui_t *ui, char direction, int x, int y, const char *rgbafg, const
493 524
         SDL_FreeSurface(bgsurface),bgsurface=NULL;
494 525
         SDL_DestroyTexture(tex),tex=NULL;
495 526
         return(0);
496
-
497 527
 }
498 528
 
499 529
 
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;
Browse code

Update author email

Dario Rodriguez authored on 07/01/2022 17:57:32
Showing 1 changed files
... ...
@@ -5,7 +5,7 @@
5 5
  *
6 6
  * Simple UI in SDL2
7 7
  *
8
- * Author: Dario Rodriguez dario@softhome.net
8
+ * Author: Dario Rodriguez antartica@whereismybit.com
9 9
  * This program is licensed under the terms of GNU GPL v2.1+
10 10
  */
11 11
 
Browse code

rounded corners for commandbuf suggestion

Dario Rodriguez authored on 01/09/2021 20:33:06
Showing 1 changed files
... ...
@@ -45,8 +45,8 @@
45 45
 #endif
46 46
 
47 47
 
48
-static int reui_fillrounded(SDL_Surface *dst, int xo, int yo, int w, int h, int r, const char *rgba);
49
-static int reui_triangle(SDL_Surface *dst, int x, int y, int w, int h, char direction, const char *rgba);
48
+static int intreui_fillroundedr(SDL_Surface *dst, int xo, int yo, int w, int h, int r, const char *rgba);
49
+static int intreui_triangle(SDL_Surface *dst, int x, int y, int w, int h, char direction, const char *rgba);
50 50
 
51 51
 reui_t *
52 52
 reui_init(int fontheight, reui_t *parent)
... ...
@@ -245,6 +245,37 @@ reui_fill(reui_t *ui, int x, int y, int w, int h, const char *rgba)
245 245
         return(0);
246 246
 }
247 247
 
248
+int
249
+reui_fillrounded(reui_t *ui, int x, int y, int w, int h, const char *rgba)
250
+{
251
+        static int rounded5[]={5,3,2,1,1};
252
+        static int rounded4[]={4,2,1,1};
253
+        int i;
254
+        if(ui==NULL || rgba==NULL)
255
+                return(-1);
256
+        if(w<2 || h<2)
257
+                return(reui_fill(ui,x,y,w,h,rgba));
258
+        if(w<8 || h<8 || ui->fontheight<8 || ui->fontwidth<8) {
259
+                reui_fill(ui,x+1,y,w-2,1,rgba);
260
+                reui_fill(ui,x,y+1,w,h-2,rgba);
261
+                reui_fill(ui,x+1,y+h-1,w-2,1,rgba);
262
+        } else if(w<10 || h<10 || ui->fontheight<10 || ui->fontwidth<10) {
263
+                reui_fill(ui,x,y+4,w,h-8,rgba);
264
+                for(i=0;i<(sizeof(rounded4)/sizeof(rounded4[0]));i++) {
265
+                        reui_fill(ui,x+rounded4[i],i,w-(rounded4[i]<<1),1,rgba);
266
+                        reui_fill(ui,x+rounded4[i],y+h-1-i,w-(rounded4[i]<<1),1,rgba);
267
+                }
268
+        } else {
269
+                reui_fill(ui,x,y+5,w,h-10,rgba);
270
+                for(i=0;i<(sizeof(rounded5)/sizeof(rounded5[0]));i++) {
271
+                        reui_fill(ui,x+rounded5[i],i,w-(rounded5[i]<<1),1,rgba);
272
+                        reui_fill(ui,x+rounded5[i],y+h-1-i,w-(rounded5[i]<<1),1,rgba);
273
+                }
274
+        }
275
+        return(0);
276
+}
277
+
278
+
248 279
 int
249 280
 reui_scr2renderer(reui_t *ui, int x, int y, int w, int h)
250 281
 {
... ...
@@ -359,35 +390,35 @@ reui_balloon(reui_t *ui, char direction, int x, int y, const char *rgbafg, const
359 390
                 return(-1);
360 391
         }
361 392
         if(direction=='n') {
362
-                reui_fillrounded(bgsurface,0,triangleh-1,bgsurface->w,bgsurface->h-triangleh,radius,rgbabg);
363
-                reui_triangle(bgsurface,(bgsurface->w-trianglew)/2,0,trianglew,triangleh,direction,rgbabg);
393
+                intreui_fillroundedr(bgsurface,0,triangleh-1,bgsurface->w,bgsurface->h-triangleh,radius,rgbabg);
394
+                intreui_triangle(bgsurface,(bgsurface->w-trianglew)/2,0,trianglew,triangleh,direction,rgbabg);
364 395
                 RECTFILL(dstrect,marginw+marginoffx,triangleh+marginh+marginoffy,fgsurface->w,fgsurface->h);
365 396
                 SDL_BlitSurface(fgsurface,NULL,bgsurface,&dstrect);
366 397
                 offx=-bgsurface->w/2;
367 398
                 offy=0;
368 399
         } else if(direction=='e') {
369
-                reui_fillrounded(bgsurface,0,0,bgsurface->w-trianglew,bgsurface->h,radius,rgbabg);
370
-                reui_triangle(bgsurface,bgsurface->w-trianglew,(bgsurface->h-triangleh)/2,trianglew,triangleh,direction,rgbabg);
400
+                intreui_fillroundedr(bgsurface,0,0,bgsurface->w-trianglew,bgsurface->h,radius,rgbabg);
401
+                intreui_triangle(bgsurface,bgsurface->w-trianglew,(bgsurface->h-triangleh)/2,trianglew,triangleh,direction,rgbabg);
371 402
                 RECTFILL(dstrect,marginw+marginoffx,marginh+marginoffy,fgsurface->w,fgsurface->h);
372 403
                 SDL_BlitSurface(fgsurface,NULL,bgsurface,&dstrect);
373 404
                 offx=-bgsurface->w;
374 405
                 offy=-bgsurface->h/2;
375 406
         } else if(direction=='s') {
376
-                reui_fillrounded(bgsurface,0,0,bgsurface->w,bgsurface->h-triangleh,radius,rgbabg);
377
-                reui_triangle(bgsurface,(bgsurface->w-trianglew)/2,bgsurface->h-triangleh,trianglew,triangleh,direction,rgbabg);
407
+                intreui_fillroundedr(bgsurface,0,0,bgsurface->w,bgsurface->h-triangleh,radius,rgbabg);
408
+                intreui_triangle(bgsurface,(bgsurface->w-trianglew)/2,bgsurface->h-triangleh,trianglew,triangleh,direction,rgbabg);
378 409
                 RECTFILL(dstrect,marginw+marginoffx,marginh+marginoffy,fgsurface->w,fgsurface->h);
379 410
                 SDL_BlitSurface(fgsurface,NULL,bgsurface,&dstrect);
380 411
                 offx=-bgsurface->w/2;
381 412
                 offy=-bgsurface->h;
382 413
         } else if(direction=='w') {
383
-                reui_fillrounded(bgsurface,trianglew,0,bgsurface->w-trianglew,bgsurface->h,radius,rgbabg);
384
-                reui_triangle(bgsurface,0,(bgsurface->h-triangleh)/2,trianglew,triangleh,direction,rgbabg);
414
+                intreui_fillroundedr(bgsurface,trianglew,0,bgsurface->w-trianglew,bgsurface->h,radius,rgbabg);
415
+                intreui_triangle(bgsurface,0,(bgsurface->h-triangleh)/2,trianglew,triangleh,direction,rgbabg);
385 416
                 RECTFILL(dstrect,trianglew+marginw+marginoffx,marginh+marginoffy,fgsurface->w,fgsurface->h);
386 417
                 SDL_BlitSurface(fgsurface,NULL,bgsurface,&dstrect);
387 418
                 offx=0;
388 419
                 offy=-bgsurface->h/2;
389 420
         } else { /* default: center */
390
-                reui_fillrounded(bgsurface,0,0,bgsurface->w,bgsurface->h,radius,rgbabg);
421
+                intreui_fillroundedr(bgsurface,0,0,bgsurface->w,bgsurface->h,radius,rgbabg);
391 422
                 RECTFILL(dstrect,marginw+marginoffx,marginh+marginoffy,fgsurface->w,fgsurface->h);
392 423
                 SDL_BlitSurface(fgsurface,NULL,bgsurface,&dstrect);
393 424
                 offx=-bgsurface->w/2;
... ...
@@ -433,7 +464,7 @@ reui_balloon(reui_t *ui, char direction, int x, int y, const char *rgbafg, const
433 464
  * returns 0 on success
434 465
  */
435 466
 static int
436
-reui_fillrounded(SDL_Surface *dst, int xo, int yo, int w, int h, int r, const char *rgba)
467
+intreui_fillroundedr(SDL_Surface *dst, int xo, int yo, int w, int h, int r, const char *rgba)
437 468
 {
438 469
         int yd=(dst->pitch)/(dst->format->BytesPerPixel);
439 470
         Uint32 *pixels=NULL;
... ...
@@ -501,7 +532,7 @@ reui_fillrounded(SDL_Surface *dst, int xo, int yo, int w, int h, int r, const ch
501 532
 /* end of external code */
502 533
 
503 534
 static int
504
-reui_triangle(SDL_Surface *dst, int x, int y, int w, int h, char direction, const char *rgba)
535
+intreui_triangle(SDL_Surface *dst, int x, int y, int w, int h, char direction, const char *rgba)
505 536
 {
506 537
         Uint32 color;
507 538
         SDL_Rect dstrect;
Browse code

Add font iosevka-fixed-regular for Control+P printouts

Dario Rodriguez authored on 19/01/2021 18:53:05
Showing 1 changed files
... ...
@@ -16,6 +16,7 @@
16 16
 #include <stdarg.h>
17 17
 #include "re_ui.h"
18 18
 #include "hack_regular.h"
19
+#include "iosevka_fixed_regular.h"
19 20
 
20 21
 #define DEFAULTTITLEPREFIX "re - "
21 22
 #define DEFAULTWINDOWWIDTH 80
... ...
@@ -88,7 +89,7 @@ reui_init(int fontheight, reui_t *parent)
88 89
         if(parent==NULL)
89 90
                 ui->fontdata=SDL_RWFromConstMem(hack_regular,SIZE_HACK_REGULAR);
90 91
         else
91
-                ui->fontdata=SDL_RWFromConstMem(hack_regular,SIZE_HACK_REGULAR);
92
+                ui->fontdata=SDL_RWFromConstMem(iosevka_fixed_regular,SIZE_IOSEVKA_FIXED_REGULAR);
92 93
         SDL_RWseek(ui->fontdata,0,RW_SEEK_SET);
93 94
         if(ui->fontdata==NULL
94 95
           || TTF_Init()==-1
Browse code

Remove saxmono font as it doesn't look good for small font sizes

Dario Rodriguez authored on 18/01/2021 15:51:41
Showing 1 changed files
... ...
@@ -16,7 +16,6 @@
16 16
 #include <stdarg.h>
17 17
 #include "re_ui.h"
18 18
 #include "hack_regular.h"
19
-#include "saxmono.h"
20 19
 
21 20
 #define DEFAULTTITLEPREFIX "re - "
22 21
 #define DEFAULTWINDOWWIDTH 80
... ...
@@ -89,7 +88,7 @@ reui_init(int fontheight, reui_t *parent)
89 88
         if(parent==NULL)
90 89
                 ui->fontdata=SDL_RWFromConstMem(hack_regular,SIZE_HACK_REGULAR);
91 90
         else
92
-                ui->fontdata=SDL_RWFromConstMem(saxmono,SIZE_SAXMONO);
91
+                ui->fontdata=SDL_RWFromConstMem(hack_regular,SIZE_HACK_REGULAR);
93 92
         SDL_RWseek(ui->fontdata,0,RW_SEEK_SET);
94 93
         if(ui->fontdata==NULL
95 94
           || TTF_Init()==-1
Browse code

make reui_init() use saxmono font for child windows (right now that equates windows for printed data)

Dario Rodriguez authored on 15/01/2021 22:20:48
Showing 1 changed files
... ...
@@ -16,6 +16,7 @@
16 16
 #include <stdarg.h>
17 17
 #include "re_ui.h"
18 18
 #include "hack_regular.h"
19
+#include "saxmono.h"
19 20
 
20 21
 #define DEFAULTTITLEPREFIX "re - "
21 22
 #define DEFAULTWINDOWWIDTH 80
... ...
@@ -85,7 +86,10 @@ reui_init(int fontheight, reui_t *parent)
85 86
         ui->screenh=screenrect.h;
86 87
         /* font */
87 88
         ui->fontheight=fontheight; /* placeholder, will fill later with real value */
88
-        ui->fontdata=SDL_RWFromConstMem(hack_regular,SIZE_HACK_REGULAR);
89
+        if(parent==NULL)
90
+                ui->fontdata=SDL_RWFromConstMem(hack_regular,SIZE_HACK_REGULAR);
91
+        else
92
+                ui->fontdata=SDL_RWFromConstMem(saxmono,SIZE_SAXMONO);
89 93
         SDL_RWseek(ui->fontdata,0,RW_SEEK_SET);
90 94
         if(ui->fontdata==NULL
91 95
           || TTF_Init()==-1
Browse code

Add print selection to new window with Control+P, close that window with ESC

Dario Rodriguez authored on 15/01/2021 22:11:33
Showing 1 changed files
... ...
@@ -48,7 +48,7 @@ static int reui_fillrounded(SDL_Surface *dst, int xo, int yo, int w, int h, int
48 48
 static int reui_triangle(SDL_Surface *dst, int x, int y, int w, int h, char direction, const char *rgba);
49 49
 
50 50
 reui_t *
51
-reui_init(int fontheight)
51
+reui_init(int fontheight, reui_t *parent)
52 52
 {
53 53
         reui_t *ui;
54 54
         SDL_Rect screenrect;
... ...
@@ -70,10 +70,16 @@ reui_init(int fontheight)
70 70
         ui->amask=0xff000000;
71 71
 #endif
72 72
         /* video */
73
-        if(SDL_Init(SDL_INIT_VIDEO)<0
74
-          || SDL_GetDisplayBounds(0,&screenrect)!=0) {
75
-                reui_free(ui),ui=NULL;
76
-                return(NULL);
73
+        if(parent==NULL) {
74
+                if(SDL_Init(SDL_INIT_VIDEO)<0
75
+                  || SDL_GetDisplayBounds(0,&screenrect)!=0) {
76
+                        reui_free(ui),ui=NULL;
77
+                        return(NULL);
78
+                }
79
+        } else {
80
+                ui->parent=(void *)parent;
81
+                screenrect.w=parent->screenw;
82
+                screenrect.h=parent->screenh;
77 83
         }
78 84
         ui->screenw=screenrect.w;
79 85
         ui->screenh=screenrect.h;
... ...
@@ -143,7 +149,7 @@ reui_free(reui_t *ui)
143 149
                 SDL_DestroyTexture(ui->onepx),ui->onepx=NULL;
144 150
         if(ui->font!=NULL)
145 151
                 TTF_CloseFont(ui->font),ui->font=NULL;
146
-        if(TTF_WasInit())
152
+        if(ui->parent==NULL && TTF_WasInit())
147 153
                 TTF_Quit();
148 154
         if(ui->fontdata!=NULL)
149 155
                 SDL_FreeRW(ui->fontdata),ui->fontdata=NULL;
... ...
@@ -153,7 +159,8 @@ reui_free(reui_t *ui)
153 159
                 SDL_DestroyRenderer(ui->renderer),ui->renderer=NULL;
154 160
         if(ui->win!=NULL)
155 161
                 SDL_DestroyWindow(ui->win),ui->win=NULL;
156
-        SDL_Quit();
162
+        if(ui->parent==NULL)
163
+                SDL_Quit();
157 164
         free(ui),ui=NULL;
158 165
 }
159 166
 
Browse code

ui: fix bug of incorrect initial window height in certain conditions

Dario Rodriguez authored on 29/11/2020 22:27:20
Showing 1 changed files
... ...
@@ -113,7 +113,7 @@ reui_init(int fontheight)
113 113
         ui->w=ui->fontwidth*DEFAULTWINDOWWIDTH;
114 114
         ui->w=(ui->w>ui->screenw)?ui->screenw:ui->w;
115 115
         ui->h=ui->fontheight*DEFAULTWINDOWHEIGHT;
116
-        ui->h=(ui->h>ui->screenh)?ui->screenw:ui->h;
116
+        ui->h=(ui->h>ui->screenh)?ui->screenh:ui->h;
117 117
         if((ui->win=SDL_CreateWindow(
118 118
               DEFAULTTITLEPREFIX,
119 119
               SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,
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 1 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
-}
Browse code

ui: experimental font cache implementation

Dario Rodriguez authored on 11/11/2020 21:39:04
Showing 1 changed files
... ...
@@ -20,6 +20,8 @@
20 20
 #define DEFAULTTITLEPREFIX "re - "
21 21
 #define DEFAULTWINDOWWIDTH 80
22 22
 #define DEFAULTWINDOWHEIGHT 66
23
+#define FONTCACHEXSIZE 512
24
+#define FONTCACHEYSIZE 512
23 25
 
24 26
 #define RECTFILL(r,rx,ry,rw,rh) (r).x=(rx),(r).y=(ry),(r).w=(rw),(r).h=(rh)
25 27
 
... ...
@@ -46,7 +48,7 @@
46 48
 
47 49
 static int reui_fillrounded(SDL_Surface *dst, int xo, int yo, int w, int h, int r, const char *rgba);
48 50
 static int reui_triangle(SDL_Surface *dst, int x, int y, int w, int h, char direction, const char *rgba);
49
-
51
+static int reui_fontcachereset(reui_t *ui);
50 52
 
51 53
 reui_t *
52 54
 reui_init(int fontheight)
... ...
@@ -79,7 +81,7 @@ reui_init(int fontheight)
79 81
         ui->screenw=screenrect.w;
80 82
         ui->screenh=screenrect.h;
81 83
         /* font */
82
-        ui->fontheight=fontheight;
84
+        ui->fontheight=fontheight; /* placeholder, will fill later with real value */
83 85
         ui->fontdata=SDL_RWFromConstMem(hack_regular,SIZE_HACK_REGULAR);
84 86
         SDL_RWseek(ui->fontdata,0,RW_SEEK_SET);
85 87
         if(ui->fontdata==NULL
... ...
@@ -99,6 +101,17 @@ reui_init(int fontheight)
99 101
                 ui->fontwidth=s->w;
100 102
                 SDL_FreeSurface(s),s=NULL;
101 103
         }
104
+        /* font height */
105
+        {
106
+                SDL_Surface *s;
107
+                SDL_Color c={0,0,0,0};
108
+                if((s=TTF_RenderUTF8_Blended(ui->font,"gjpqy_",c))==NULL) {
109
+                        reui_free(ui),ui=NULL;
110
+                        return(NULL);
111
+                }
112
+                ui->fontheight=s->h;
113
+                SDL_FreeSurface(s),s=NULL;
114
+        }
102 115
         /* main window */
103 116
         ui->w=ui->fontwidth*DEFAULTWINDOWWIDTH;
104 117
         ui->w=(ui->w>ui->screenw)?ui->screenw:ui->w;
... ...
@@ -113,10 +126,15 @@ reui_init(int fontheight)
113 126
           || (ui->scr=SDL_CreateTexture(ui->renderer,SDL_PIXELFORMAT_ARGB8888,
114 127
               SDL_TEXTUREACCESS_STREAMING,ui->w,ui->h))==NULL
115 128
           || (ui->onepx=SDL_CreateTexture(ui->renderer,SDL_PIXELFORMAT_ARGB8888,
116
-              SDL_TEXTUREACCESS_STATIC,1,1))==NULL) {
129
+              SDL_TEXTUREACCESS_STATIC,1,1))==NULL
130
+          || (ui->fontcache=SDL_CreateTexture(ui->renderer,SDL_PIXELFORMAT_ARGB8888,
131
+              SDL_TEXTUREACCESS_STATIC,FONTCACHEXSIZE,FONTCACHEYSIZE))==NULL) {
117 132
                 reui_free(ui),ui=NULL;
118 133
                 return(NULL);
119 134
         }
135
+        /* font cache */
136
+        SDL_SetTextureBlendMode(ui->fontcache, SDL_BLENDMODE_BLEND);
137
+        reui_fontcachereset(ui);
120 138
         /* finished */
121 139
         ui->scrdirty=1;
122 140
         ui->rendererdirty=1;
... ...
@@ -129,6 +147,8 @@ reui_free(reui_t *ui)
129 147
 {
130 148
         if(ui==NULL)
131 149
                 return;
150
+        if(ui->fontcache!=NULL)
151
+                SDL_DestroyTexture(ui->fontcache),ui->fontcache=NULL;
132 152
         if(ui->onepx!=NULL)
133 153
                 SDL_DestroyTexture(ui->onepx),ui->onepx=NULL;
134 154
         if(ui->font!=NULL)
... ...
@@ -143,7 +163,6 @@ reui_free(reui_t *ui)
143 163
                 SDL_DestroyRenderer(ui->renderer),ui->renderer=NULL;
144 164
         if(ui->win!=NULL)
145 165
                 SDL_DestroyWindow(ui->win),ui->win=NULL;
146
-
147 166
         SDL_Quit();
148 167
         free(ui),ui=NULL;
149 168
 }
... ...
@@ -183,23 +202,29 @@ int
183 202
 reui_setfontheight(reui_t *ui, int fontheight)
184 203
 {
185 204
         TTF_Font *newfont;
186
-        SDL_Surface *s;
205
+        SDL_Surface *s,*s2;
187 206
         SDL_Color c={0,0,0,0};
188 207
         if(ui==NULL || fontheight<=0)
189 208
                 return(-1); /* sanity check failed */
190 209
         SDL_RWseek(ui->fontdata,0,RW_SEEK_SET);
191 210
         if((newfont=TTF_OpenFontRW(ui->fontdata,0,fontheight))==NULL)
192 211
                 return(-1); /* couln't setup new font */
193
-        if((s=TTF_RenderUTF8_Blended(newfont,"m",c))==NULL) {
212
+        if((s=TTF_RenderUTF8_Blended(newfont,"m",c))==NULL
213
+          || (s2=TTF_RenderUTF8_Blended(newfont,"gjpqy_",c))==NULL) {
214
+                if(s!=NULL)
215
+                        SDL_FreeSurface(s),s=NULL;
194 216
                 TTF_CloseFont(newfont),newfont=NULL;
195 217
                 return(-1); /* couldn't use new font */
196 218
         }
197 219
         if(ui->font!=NULL)
198 220
                 TTF_CloseFont(ui->font),ui->font=NULL;
199 221
         ui->font=newfont;
200
-        ui->fontheight=fontheight;
222
+        ui->fontheight=s2->h;
201 223
         ui->fontwidth=s->w;
202 224
         SDL_FreeSurface(s),s=NULL;
225
+        SDL_FreeSurface(s2),s2=NULL;
226
+        /* font cache */
227
+        reui_fontcachereset(ui);
203 228
         return(0);
204 229
 }
205 230
 
... ...
@@ -247,6 +272,7 @@ reui_present(reui_t *ui)
247 272
 int
248 273
 reui_write(reui_t *ui, int x, int y, const char *rgba, const char *str, int nchar)
249 274
 {
275
+#if 0
250 276
         char buf[1024];
251 277
         SDL_Surface *fgsurface;
252 278
         SDL_Texture *fg;
... ...
@@ -274,6 +300,84 @@ reui_write(reui_t *ui, int x, int y, const char *rgba, const char *str, int ncha
274 300
         SDL_FreeSurface(fgsurface),fgsurface=NULL;
275 301
         SDL_DestroyTexture(fg),fg=NULL;
276 302
         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
277 381
 }
278 382
 
279 383
 
... ...
@@ -523,3 +627,12 @@ reui_triangle(SDL_Surface *dst, int x, int y, int w, int h, char direction, cons
523 627
         return(0);
524 628
 }
525 629
 
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
+}
Browse code

ui: change the way the fills are done (new method is gentler to the linux intel dri driver). Add some missing const qualifiers.

Dario Rodriguez authored on 08/11/2020 11:59:03
Showing 1 changed files
... ...
@@ -28,16 +28,24 @@
28 28
                             |(((Uint32)(((unsigned char *)rgba)[1]))<<16) \
29 29
                             |(((Uint32)(((unsigned char *)rgba)[2]))<<8)  \
30 30
                             |((Uint32)(((unsigned char *)rgba)[3])))
31
+#define CHARRGBA2TEXARGB8888(rgba) ((((Uint32)(((unsigned char *)rgba)[2]))<<24) \
32
+                            |(((Uint32)(((unsigned char *)rgba)[1]))<<16) \
33
+                            |(((Uint32)(((unsigned char *)rgba)[0]))<<8)  \
34
+                            |((Uint32)(((unsigned char *)rgba)[3])))
31 35
 #else
32 36
 #define CHARRGBA2UINT(rgba) ((((Uint32)(((unsigned char *)rgba)[3]))<<24) \
33 37
                             |(((Uint32)(((unsigned char *)rgba)[2]))<<16) \
34 38
                             |(((Uint32)(((unsigned char *)rgba)[1]))<<8)  \
35 39
                             |((Uint32)(((unsigned char *)rgba)[0])))
40
+#define CHARRGBA2TEXARGB8888(rgba) ((((Uint32)(((unsigned char *)rgba)[3]))<<24) \
41
+                            |(((Uint32)(((unsigned char *)rgba)[0]))<<16) \
42
+                            |(((Uint32)(((unsigned char *)rgba)[1]))<<8)  \
43
+                            |((Uint32)(((unsigned char *)rgba)[2])))
36 44
 #endif
37 45
 
38 46
 
39
-static int reui_fillrounded(SDL_Surface *dst, int xo, int yo, int w, int h, int r, char *rgba);
40
-static int reui_triangle(SDL_Surface *dst, int x, int y, int w, int h, char direction, char *rgba);
47
+static int reui_fillrounded(SDL_Surface *dst, int xo, int yo, int w, int h, int r, const char *rgba);
48
+static int reui_triangle(SDL_Surface *dst, int x, int y, int w, int h, char direction, const char *rgba);
41 49
 
42 50
 
43 51
 reui_t *
... ...
@@ -50,6 +58,18 @@ reui_init(int fontheight)
50 58
         if((ui=malloc(sizeof(reui_t)))==NULL)
51 59
                 return(NULL);
52 60
         memset(ui,0,sizeof(reui_t));
61
+        /* masks */
62
+#if SDL_BYTEORDER==SDL_BIG_ENDIAN
63
+        ui->rmask=0xff000000;
64
+        ui->gmask=0x00ff0000;
65
+        ui->bmask=0x0000ff00;
66
+        ui->amask=0x000000ff;
67
+#else
68
+        ui->rmask=0x000000ff;
69
+        ui->gmask=0x0000ff00;
70
+        ui->bmask=0x00ff0000;
71
+        ui->amask=0xff000000;
72
+#endif
53 73
         /* video */
54 74
         if(SDL_Init(SDL_INIT_VIDEO)<0
55 75
           || SDL_GetDisplayBounds(0,&screenrect)!=0) {
... ...
@@ -91,7 +111,9 @@ reui_init(int fontheight)
91 111
           || (ui->renderer=SDL_CreateRenderer(ui->win,-1,
92 112
               SDL_RENDERER_ACCELERATED|SDL_RENDERER_PRESENTVSYNC))==NULL
93 113
           || (ui->scr=SDL_CreateTexture(ui->renderer,SDL_PIXELFORMAT_ARGB8888,
94
-              SDL_TEXTUREACCESS_STREAMING,ui->w,ui->h))==NULL) {
114
+              SDL_TEXTUREACCESS_STREAMING,ui->w,ui->h))==NULL
115
+          || (ui->onepx=SDL_CreateTexture(ui->renderer,SDL_PIXELFORMAT_ARGB8888,
116
+              SDL_TEXTUREACCESS_STATIC,1,1))==NULL) {
95 117
                 reui_free(ui),ui=NULL;
96 118
                 return(NULL);
97 119
         }
... ...
@@ -107,6 +129,8 @@ reui_free(reui_t *ui)
107 129
 {
108 130
         if(ui==NULL)
109 131
                 return;
132
+        if(ui->onepx!=NULL)
133
+                SDL_DestroyTexture(ui->onepx),ui->onepx=NULL;
110 134
         if(ui->font!=NULL)
111 135
                 TTF_CloseFont(ui->font),ui->font=NULL;
112 136
         if(TTF_WasInit())
... ...
@@ -119,6 +143,7 @@ reui_free(reui_t *ui)
119 143
                 SDL_DestroyRenderer(ui->renderer),ui->renderer=NULL;
120 144
         if(ui->win!=NULL)
121 145
                 SDL_DestroyWindow(ui->win),ui->win=NULL;
146
+
122 147
         SDL_Quit();
123 148
         free(ui),ui=NULL;
124 149
 }
... ...
@@ -180,18 +205,18 @@ reui_setfontheight(reui_t *ui, int fontheight)
180 205
 
181 206
 
182 207
 int
183
-reui_fill(reui_t *ui, int x, int y, int w, int h, char *rgba)
208
+reui_fill(reui_t *ui, int x, int y, int w, int h, const char *rgba)
184 209
 {
210
+        SDL_Rect srcrect;
185 211
         SDL_Rect dstrect;
212
+        Uint32 pixels[1];
186 213
         if(ui==NULL || rgba==NULL)
187 214
                 return(-1);
215
+        pixels[0]=CHARRGBA2TEXARGB8888(rgba);
216
+        SDL_UpdateTexture(ui->onepx,NULL,pixels,sizeof(Uint32));
217
+        RECTFILL(srcrect,0,0,1,1);
188 218
         RECTFILL(dstrect,x,y,w,h);
189
-        SDL_SetRenderDrawColor(ui->renderer,
190
-                ((unsigned char *)rgba)[0],
191
-                ((unsigned char *)rgba)[1],
192
-                ((unsigned char *)rgba)[2],
193
-                ((unsigned char *)rgba)[3]);
194
-        SDL_RenderFillRect(ui->renderer, &dstrect);
219
+        SDL_RenderCopy(ui->renderer,ui->onepx,&srcrect,&dstrect);
195 220
         ui->rendererdirty=1;
196 221
         return(0);
197 222
 }
... ...
@@ -220,7 +245,7 @@ reui_present(reui_t *ui)
220 245
 }
221 246
 
222 247
 int
223
-reui_write(reui_t *ui, int x, int y, char *rgba, char *str, int nchar)
248
+reui_write(reui_t *ui, int x, int y, const char *rgba, const char *str, int nchar)
224 249
 {
225 250
         char buf[1024];
226 251
         SDL_Surface *fgsurface;
... ...
@@ -253,7 +278,7 @@ reui_write(reui_t *ui, int x, int y, char *rgba, char *str, int nchar)
253 278
 
254 279
 
255 280
 int
256
-reui_printf(reui_t *ui, int x, int y, char *rgba, char *format, ...)
281
+reui_printf(reui_t *ui, int x, int y, const char *rgba, char *format, ...)
257 282
 {
258 283
         char buf[1024];
259 284
         va_list l;
... ...
@@ -267,7 +292,7 @@ reui_printf(reui_t *ui, int x, int y, char *rgba, char *format, ...)
267 292
 }
268 293
 
269 294
 int
270
-reui_balloon(reui_t *ui, char direction, int x, int y, char *rgbafg, char *rgbabg, char *str, int nchar)
295
+reui_balloon(reui_t *ui, char direction, int x, int y, const char *rgbafg, const char *rgbabg, const char *str, int nchar)
271 296
 {
272 297
         char buf[1024];
273 298
         SDL_Surface *bgsurface;
... ...
@@ -278,7 +303,6 @@ reui_balloon(reui_t *ui, char direction, int x, int y, char *rgbafg, char *rgbab
278 303
                      ((unsigned char *)rgbafg)[2],
279 304
                      ((unsigned char *)rgbafg)[3]};
280 305
         SDL_Rect dstrect;
281
-        Uint32 rmask, gmask, bmask, amask;
282 306
         int trianglew,triangleh;
283 307
         int marginw,marginh;
284 308
         int marginoffx,marginoffy;
... ...
@@ -286,17 +310,6 @@ reui_balloon(reui_t *ui, char direction, int x, int y, char *rgbafg, char *rgbab
286 310
         int offx,offy;
287 311
         if(ui==NULL || rgbafg==NULL || rgbabg==NULL || (str==NULL && nchar!=0))
288 312
                 return(-1);
289
-#if SDL_BYTEORDER==SDL_BIG_ENDIAN
290
-        rmask=0xff000000;
291
-        gmask=0x00ff0000;
292
-        bmask=0x0000ff00;
293
-        amask=0x000000ff;
294
-#else
295
-        rmask=0x000000ff;
296
-        gmask=0x0000ff00;
297
-        bmask=0x00ff0000;
298
-        amask=0xff000000;
299
-#endif
300 313
         if(nchar<sizeof(buf)) {
301 314
                 memcpy(buf,str,nchar);
302 315
                 buf[nchar]='\0';
... ...
@@ -316,7 +329,7 @@ reui_balloon(reui_t *ui, char direction, int x, int y, char *rgbafg, char *rgbab
316 329
         if((bgsurface=SDL_CreateRGBSurface(0
317 330
           ,(fgsurface->w+marginw*2)+((direction=='w' || direction=='e')?trianglew:0)
318 331
           ,(fgsurface->h+marginh*2)+((direction=='n' || direction=='s')?triangleh:0)
319
-          ,32, rmask, gmask, bmask, amask))==NULL) {
332
+          ,32, ui->rmask, ui->gmask, ui->bmask, ui->amask))==NULL) {
320 333
                 if(fgsurface!=NULL)
321 334
                         SDL_FreeSurface(fgsurface),fgsurface=NULL;
322 335
                 return(-1);
... ...
@@ -396,7 +409,7 @@ reui_balloon(reui_t *ui, char direction, int x, int y, char *rgbafg, char *rgbab
396 409
  * returns 0 on success
397 410
  */
398 411
 static int
399
-reui_fillrounded(SDL_Surface *dst, int xo, int yo, int w, int h, int r, char *rgba)
412
+reui_fillrounded(SDL_Surface *dst, int xo, int yo, int w, int h, int r, const char *rgba)
400 413
 {
401 414
         int yd=(dst->pitch)/(dst->format->BytesPerPixel);
402 415
         Uint32 *pixels=NULL;
... ...
@@ -404,6 +417,7 @@ reui_fillrounded(SDL_Surface *dst, int xo, int yo, int w, int h, int r, char *rg
404 417
         int rpsqrt2= (int) (r/sqrt(2));
405 418
         int sy,ey,sx,ex;
406 419
         int d,x2m1;
420
+        int o,l;
407 421
         Uint32 color;
408 422
         if(dst==NULL || rgba==NULL || xo<0 || y0<0 || (xo+w)>dst->w || (yo+h)>dst->h)
409 423
                 return(-1); /* sanity check failed */
... ...
@@ -418,13 +432,15 @@ reui_fillrounded(SDL_Surface *dst, int xo, int yo, int w, int h, int r, char *rg
418 432
                 return(-1);
419 433
         SDL_LockSurface(dst);
420 434
         pixels=(Uint32*)(dst->pixels);
435
+        l=dst->w*dst->h;
421 436
         sy=(yo-h)*yd;
422 437
         ey=(yo+h)*yd;
423 438
         sx=(xo-w);
424 439
         ex=(xo+w);
425 440
         for(i=sy;i<=ey;i+=yd) {
426 441
                 for(j=sx-r;j<=ex+r;j++) {
427
-                        pixels[i+j]=color;
442
+                        if((o=i+j)>=0 && o<l)
443
+                                pixels[o]=color;
428 444
                 }
429 445
         }
430 446
         d=-r;
... ...
@@ -437,17 +453,22 @@ reui_fillrounded(SDL_Surface *dst, int xo, int yo, int w, int h, int r, char *rg
437 453
                         y--;
438 454
                         d-=(y*2);
439 455
                 }
440
-                for(i=sx-x;i<=ex+x;i++)
441
-                        pixels[sy-y*yd+i]=color;
442
-
443
-                for(i=sx-y;i<=ex+y;i++)
444
-                        pixels[sy-x*yd+i]=color;
445
-
446
-                for(i=sx-y;i<=ex+y;i++)
447
-                        pixels[ey+x*yd+i]=color;
448
-
449
-                for(i=sx-x;i<=ex+x;i++)
450
-                        pixels[ey+y*yd+i]=color;
456
+                for(i=sx-x;i<=ex+x;i++) {
457
+                        if((o=(sy-y*yd+i))>=0 && o<l)
458
+                                pixels[o]=color;
459
+                }
460
+                for(i=sx-y;i<=ex+y ;i++) {
461
+                        if((o=(sy-x*yd+i))>=0 && o<l)
462
+                                pixels[o]=color;
463
+                }
464
+                for(i=sx-y;i<=ex+y;i++) {
465
+                        if((o=(ey+x*yd+i))>=0 && o<l)
466
+                                pixels[o]=color;
467
+                }
468
+                for(i=sx-x;i<=ex+x;i++) {
469
+                        if((o=(ey+y*yd+i))>=0 && o<l)
470
+                                pixels[o]=color;
471
+                }
451 472
         }
452 473
         SDL_UnlockSurface(dst);
453 474
         return(0);
... ...
@@ -456,7 +477,7 @@ reui_fillrounded(SDL_Surface *dst, int xo, int yo, int w, int h, int r, char *rg
456 477
 /* end of external code */
457 478
 
458 479
 static int
459
-reui_triangle(SDL_Surface *dst, int x, int y, int w, int h, char direction, char *rgba)
480
+reui_triangle(SDL_Surface *dst, int x, int y, int w, int h, char direction, const char *rgba)
460 481
 {
461 482
         Uint32 color;
462 483
         SDL_Rect dstrect;
Browse code

Implement font size change with control+'+'/'-'/'0', as in browsers

Dario Rodriguez authored on 01/11/2020 22:29:06
Showing 1 changed files
... ...
@@ -17,7 +17,6 @@
17 17
 #include "re_ui.h"
18 18
 #include "hack_regular.h"
19 19
 
20
-#define DEFAULTFONTSIZE 16
21 20
 #define DEFAULTTITLEPREFIX "re - "
22 21
 #define DEFAULTWINDOWWIDTH 80
23 22
 #define DEFAULTWINDOWHEIGHT 66
... ...
@@ -42,10 +41,12 @@ static int reui_triangle(SDL_Surface *dst, int x, int y, int w, int h, char dire
42 41
 
43 42
 
44 43
 reui_t *
45
-reui_init()
44
+reui_init(int fontheight)
46 45
 {
47 46
         reui_t *ui;
48 47
         SDL_Rect screenrect;
48
+        if(fontheight<=0)
49
+                return(NULL); /* sanity check failed */
49 50
         if((ui=malloc(sizeof(reui_t)))==NULL)
50 51
                 return(NULL);
51 52
         memset(ui,0,sizeof(reui_t));
... ...
@@ -58,7 +59,7 @@ reui_init()
58 59
         ui->screenw=screenrect.w;
59 60
         ui->screenh=screenrect.h;
60 61
         /* font */
61
-        ui->fontheight=DEFAULTFONTSIZE;
62
+        ui->fontheight=fontheight;
62 63
         ui->fontdata=SDL_RWFromConstMem(hack_regular,SIZE_HACK_REGULAR);
63 64
         SDL_RWseek(ui->fontdata,0,RW_SEEK_SET);
64 65
         if(ui->fontdata==NULL
... ...
@@ -153,6 +154,31 @@ reui_resize(reui_t *ui, int w, int h)
153 154
         return(0);
154 155
 }
155 156
 
157
+int
158
+reui_setfontheight(reui_t *ui, int fontheight)
159
+{
160
+        TTF_Font *newfont;
161
+        SDL_Surface *s;
162
+        SDL_Color c={0,0,0,0};
163
+        if(ui==NULL || fontheight<=0)
164
+                return(-1); /* sanity check failed */
165
+        SDL_RWseek(ui->fontdata,0,RW_SEEK_SET);
166
+        if((newfont=TTF_OpenFontRW(ui->fontdata,0,fontheight))==NULL)
167
+                return(-1); /* couln't setup new font */
168
+        if((s=TTF_RenderUTF8_Blended(newfont,"m",c))==NULL) {
169
+                TTF_CloseFont(newfont),newfont=NULL;
170
+                return(-1); /* couldn't use new font */
171
+        }
172
+        if(ui->font!=NULL)
173
+                TTF_CloseFont(ui->font),ui->font=NULL;
174
+        ui->font=newfont;
175
+        ui->fontheight=fontheight;
176
+        ui->fontwidth=s->w;
177
+        SDL_FreeSurface(s),s=NULL;
178
+        return(0);
179
+}
180
+
181
+
156 182
 int
157 183
 reui_fill(reui_t *ui, int x, int y, int w, int h, char *rgba)
158 184
 {
Browse code

ui: fix memory leak on ui_free (fontdata was not being freed because the pointer was being mistakenly nulled a few lines before)

Dario Rodriguez authored on 01/11/2020 22:27:03
Showing 1 changed files
... ...
@@ -107,7 +107,7 @@ reui_free(reui_t *ui)
107 107
         if(ui==NULL)
108 108
                 return;
109 109
         if(ui->font!=NULL)
110
-                TTF_CloseFont(ui->font),ui->fontdata=NULL;
110
+                TTF_CloseFont(ui->font),ui->font=NULL;
111 111
         if(TTF_WasInit())
112 112
                 TTF_Quit();
113 113
         if(ui->fontdata!=NULL)
Browse code

UI: add ability to put balloons on screen

Dario Rodriguez authored on 21/10/2020 22:06:25
Showing 1 changed files
... ...
@@ -24,6 +24,23 @@
24 24
 
25 25
 #define RECTFILL(r,rx,ry,rw,rh) (r).x=(rx),(r).y=(ry),(r).w=(rw),(r).h=(rh)
26 26
 
27
+#if SDL_BYTEORDER==SDL_BIG_ENDIAN
28
+#define CHARRGBA2UINT(rgba) ((((Uint32)(((unsigned char *)rgba)[0]))<<24) \
29
+                            |(((Uint32)(((unsigned char *)rgba)[1]))<<16) \
30
+                            |(((Uint32)(((unsigned char *)rgba)[2]))<<8)  \
31
+                            |((Uint32)(((unsigned char *)rgba)[3])))
32
+#else
33
+#define CHARRGBA2UINT(rgba) ((((Uint32)(((unsigned char *)rgba)[3]))<<24) \
34
+                            |(((Uint32)(((unsigned char *)rgba)[2]))<<16) \
35
+                            |(((Uint32)(((unsigned char *)rgba)[1]))<<8)  \
36
+                            |((Uint32)(((unsigned char *)rgba)[0])))
37
+#endif
38
+
39
+
40
+static int reui_fillrounded(SDL_Surface *dst, int xo, int yo, int w, int h, int r, char *rgba);
41
+static int reui_triangle(SDL_Surface *dst, int x, int y, int w, int h, char direction, char *rgba);
42
+
43
+
27 44
 reui_t *
28 45
 reui_init()
29 46
 {
... ...
@@ -223,4 +240,239 @@ reui_printf(reui_t *ui, int x, int y, char *rgba, char *format, ...)
223 240
         return(0);
224 241
 }
225 242
 
243
+int
244
+reui_balloon(reui_t *ui, char direction, int x, int y, char *rgbafg, char *rgbabg, char *str, int nchar)
245
+{
246
+        char buf[1024];
247
+        SDL_Surface *bgsurface;
248
+        SDL_Surface *fgsurface;
249
+        SDL_Texture *tex;
250
+        SDL_Color c={((unsigned char *)rgbafg)[0],
251
+                     ((unsigned char *)rgbafg)[1],
252
+                     ((unsigned char *)rgbafg)[2],
253
+                     ((unsigned char *)rgbafg)[3]};
254
+        SDL_Rect dstrect;
255
+        Uint32 rmask, gmask, bmask, amask;
256
+        int trianglew,triangleh;
257
+        int marginw,marginh;
258
+        int marginoffx,marginoffy;
259
+        int radius;
260
+        int offx,offy;
261
+        if(ui==NULL || rgbafg==NULL || rgbabg==NULL || (str==NULL && nchar!=0))
262
+                return(-1);
263
+#if SDL_BYTEORDER==SDL_BIG_ENDIAN
264
+        rmask=0xff000000;
265
+        gmask=0x00ff0000;
266
+        bmask=0x0000ff00;
267
+        amask=0x000000ff;
268
+#else
269
+        rmask=0x000000ff;
270
+        gmask=0x0000ff00;
271
+        bmask=0x00ff0000;
272
+        amask=0xff000000;
273
+#endif
274
+        if(nchar<sizeof(buf)) {
275
+                memcpy(buf,str,nchar);
276
+                buf[nchar]='\0';
277
+        } else {
278
+                memcpy(buf,str,sizeof(buf)-1);
279
+                buf[sizeof(buf)-1]='\0';
280
+        }
281
+        if((fgsurface=TTF_RenderUTF8_Blended(ui->font,buf,c))==NULL)
282
+                return(-1);
283
+        trianglew=ui->fontwidth;
284
+        triangleh=ui->fontheight/2;
285
+        marginw=ui->fontwidth*4/3;
286
+        marginh=ui->fontheight/3;
287
+        marginoffx=0;
288
+        marginoffy=-ui->fontheight/18;
289
+        radius=ui->fontheight/2;
290
+        if((bgsurface=SDL_CreateRGBSurface(0
291
+          ,(fgsurface->w+marginw*2)+((direction=='w' || direction=='e')?trianglew:0)
292
+          ,(fgsurface->h+marginh*2)+((direction=='n' || direction=='s')?triangleh:0)
293
+          ,32, rmask, gmask, bmask, amask))==NULL) {
294
+                if(fgsurface!=NULL)
295
+                        SDL_FreeSurface(fgsurface),fgsurface=NULL;
296
+                return(-1);
297
+        }
298
+        if(direction=='n') {
299
+                reui_fillrounded(bgsurface,0,triangleh-1,bgsurface->w,bgsurface->h-triangleh,radius,rgbabg);
300
+                reui_triangle(bgsurface,(bgsurface->w-trianglew)/2,0,trianglew,triangleh,direction,rgbabg);
301
+                RECTFILL(dstrect,marginw+marginoffx,triangleh+marginh+marginoffy,fgsurface->w,fgsurface->h);
302
+                SDL_BlitSurface(fgsurface,NULL,bgsurface,&dstrect);
303
+                offx=-bgsurface->w/2;
304
+                offy=0;
305
+        } else if(direction=='e') {
306
+                reui_fillrounded(bgsurface,0,0,bgsurface->w-trianglew,bgsurface->h,radius,rgbabg);
307
+                reui_triangle(bgsurface,bgsurface->w-trianglew,(bgsurface->h-triangleh)/2,trianglew,triangleh,direction,rgbabg);
308
+                RECTFILL(dstrect,marginw+marginoffx,marginh+marginoffy,fgsurface->w,fgsurface->h);
309
+                SDL_BlitSurface(fgsurface,NULL,bgsurface,&dstrect);
310
+                offx=-bgsurface->w;
311
+                offy=-bgsurface->h/2;
312
+        } else if(direction=='s') {
313
+                reui_fillrounded(bgsurface,0,0,bgsurface->w,bgsurface->h-triangleh,radius,rgbabg);
314
+                reui_triangle(bgsurface,(bgsurface->w-trianglew)/2,bgsurface->h-triangleh,trianglew,triangleh,direction,rgbabg);
315
+                RECTFILL(dstrect,marginw+marginoffx,marginh+marginoffy,fgsurface->w,fgsurface->h);
316
+                SDL_BlitSurface(fgsurface,NULL,bgsurface,&dstrect);
317
+                offx=-bgsurface->w/2;
318
+                offy=-bgsurface->h;
319
+        } else if(direction=='w') {
320
+                reui_fillrounded(bgsurface,trianglew,0,bgsurface->w-trianglew,bgsurface->h,radius,rgbabg);
321
+                reui_triangle(bgsurface,0,(bgsurface->h-triangleh)/2,trianglew,triangleh,direction,rgbabg);
322
+                RECTFILL(dstrect,trianglew+marginw+marginoffx,marginh+marginoffy,fgsurface->w,fgsurface->h);
323
+                SDL_BlitSurface(fgsurface,NULL,bgsurface,&dstrect);
324
+                offx=0;
325
+                offy=-bgsurface->h/2;
326
+        } else { /* default: center */
327
+                reui_fillrounded(bgsurface,0,0,bgsurface->w,bgsurface->h,radius,rgbabg);
328
+                RECTFILL(dstrect,marginw+marginoffx,marginh+marginoffy,fgsurface->w,fgsurface->h);
329
+                SDL_BlitSurface(fgsurface,NULL,bgsurface,&dstrect);
330
+                offx=-bgsurface->w/2;
331
+                offy=-bgsurface->h/2;
332
+        }
333
+        if((tex=SDL_CreateTextureFromSurface(ui->renderer,bgsurface))==NULL) {
334
+                SDL_FreeSurface(fgsurface),fgsurface=NULL;
335
+                SDL_FreeSurface(bgsurface),bgsurface=NULL;
336
+                return(-1);
337
+        }
338
+        RECTFILL(dstrect,x+offx,y+offy,bgsurface->w,bgsurface->h);
339
+        SDL_RenderCopy(ui->renderer,tex,NULL,&dstrect);
340
+        ui->rendererdirty=1;
341
+        SDL_FreeSurface(fgsurface),fgsurface=NULL;
342
+        SDL_FreeSurface(bgsurface),bgsurface=NULL;
343
+        SDL_DestroyTexture(tex),tex=NULL;
344
+        return(0);
345
+
346
+}
347
+
348
+
349
+
350
+/*
351
+ * https://sdl.libsdl.narkive.com/rX3aNgKp/rectangle-with-round-corners-in-sdl
352
+ *
353
+ * static int fill_rounded_box_b(SDL_Surface* dst, int xo, int yo,
354
+ *                               int w, int h, int r, Uint32 color);
355
+ *
356
+ * by "Sami N��t�nen" (s***@bayminer.com)
357
+ * (lightly edited to conform to this file code style and semantics)
358
+ *
359
+ * draws a rounded box with...
360
+ *   corner radius of 'r'
361
+ *   width of 'w'
362
+ *   and height of 'h'
363
+ *
364
+ * draws the box right and down of...
365
+ *   x-offset xo
366
+ *   y-offset yo
367
+ *
368
+ * returns -1 if 2*r is bigger than w or h
369
+ *           and draws nothing
370
+ * returns 0 on success
371
+ */
372
+static int
373
+reui_fillrounded(SDL_Surface *dst, int xo, int yo, int w, int h, int r, char *rgba)
374
+{
375
+        int yd=(dst->pitch)/(dst->format->BytesPerPixel);
376
+        Uint32 *pixels=NULL;
377
+        int x,y,i,j;
378
+        int rpsqrt2= (int) (r/sqrt(2));
379
+        int sy,ey,sx,ex;
380
+        int d,x2m1;
381
+        Uint32 color;
382
+        if(dst==NULL || rgba==NULL || xo<0 || y0<0 || (xo+w)>dst->w || (yo+h)>dst->h)
383
+                return(-1); /* sanity check failed */
384
+        color=CHARRGBA2UINT(rgba);
385
+        w/=2;
386
+        h/=2;
387
+        xo+=w;
388
+        yo+=h;
389
+        w-=r;
390
+        h-=r;
391
+        if(w<0||h<0)
392
+                return(-1);
393
+        SDL_LockSurface(dst);
394
+        pixels=(Uint32*)(dst->pixels);
395
+        sy=(yo-h)*yd;
396
+        ey=(yo+h)*yd;
397
+        sx=(xo-w);
398
+        ex=(xo+w);
399
+        for(i=sy;i<=ey;i+=yd) {
400
+                for(j=sx-r;j<=ex+r;j++) {
401
+                        pixels[i+j]=color;
402
+                }
403
+        }
404
+        d=-r;
405
+        x2m1=-1;
406
+        y=r;
407
+        for(x=0;x<=rpsqrt2;x++) {
408
+                x2m1+=2;
409
+                d+=x2m1;
410
+                if(d>=0) {
411
+                        y--;
412
+                        d-=(y*2);
413
+                }
414
+                for(i=sx-x;i<=ex+x;i++)
415
+                        pixels[sy-y*yd+i]=color;
416
+
417
+                for(i=sx-y;i<=ex+y;i++)
418
+                        pixels[sy-x*yd+i]=color;
419
+
420
+                for(i=sx-y;i<=ex+y;i++)
421
+                        pixels[ey+x*yd+i]=color;
422
+
423
+                for(i=sx-x;i<=ex+x;i++)
424
+                        pixels[ey+y*yd+i]=color;
425
+        }
426
+        SDL_UnlockSurface(dst);
427
+        return(0);
428
+}
429
+
430
+/* end of external code */
431
+
432
+static int
433
+reui_triangle(SDL_Surface *dst, int x, int y, int w, int h, char direction, char *rgba)
434
+{
435
+        Uint32 color;
436
+        SDL_Rect dstrect;
437
+        int i;
438
+        int myw,myh;
439
+        if(dst==NULL || rgba==NULL)
440
+                return(-1);
441
+        color=CHARRGBA2UINT(rgba);
442
+        SDL_LockSurface(dst);
443
+        if(direction=='n') {
444
+                for(i=0;i<h;i++) {
445
+                        myw=(i*w*2)/(3*h);
446
+                        myw=(myw<0)?1:(myw>w)?w:myw;
447
+                        RECTFILL(dstrect,x+(w-myw)/2,y+i,myw,1);
448
+                        SDL_FillRect(dst,&dstrect,color);
449
+                }
450
+        } else if(direction=='e') {
451
+                for(i=0;i<w;i++) {
452
+                        myh=(i*h*2)/(3*w);
453
+                        myh=(myh<0)?1:(myh>h)?h:myh;
454
+                        RECTFILL(dstrect,x+w-1-i,y+(h-myh)/2,1,myh);
455
+                        SDL_FillRect(dst,&dstrect,color);
456
+                }
457
+        } else if(direction=='s') {
458
+                for(i=0;i<h;i++) {
459
+                        myw=(i*w*2)/(3*h);
460
+                        myw=(myw<0)?1:(myw>w)?w:myw;
461
+                        RECTFILL(dstrect,x+(w-myw)/2,y+h-1-i,myw,1);
462
+                        SDL_FillRect(dst,&dstrect,color);
463
+                }
464
+        } else if(direction=='w') {
465
+                for(i=0;i<w;i++) {
466
+                        myh=(i*h*2)/(3*w);
467
+                        myh=(myh<0)?1:(myh>h)?h:myh;
468
+                        RECTFILL(dstrect,x+i,y+(h-myh)/2,1,myh);
469
+                        SDL_FillRect(dst,&dstrect,color);
470
+                }
471
+        } else { /* default: center */
472
+                RECTFILL(dstrect,x,y,w,h);
473
+                SDL_FillRect(dst,&dstrect,color);
474
+        }
475
+        SDL_UnlockSurface(dst);
476
+        return(0);
477
+}
226 478
 
Browse code

Implement window resizizing

Dario Rodriguez authored on 08/10/2020 19:52:50
Showing 1 changed files
... ...
@@ -69,7 +69,7 @@ reui_init()
69 69
         if((ui->win=SDL_CreateWindow(
70 70
               DEFAULTTITLEPREFIX,
71 71
               SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,
72
-              ui->w,ui->h,0))==NULL
72
+              ui->w,ui->h,SDL_WINDOW_RESIZABLE))==NULL
73 73
           || (ui->renderer=SDL_CreateRenderer(ui->win,-1,
74 74
               SDL_RENDERER_ACCELERATED|SDL_RENDERER_PRESENTVSYNC))==NULL
75 75
           || (ui->scr=SDL_CreateTexture(ui->renderer,SDL_PIXELFORMAT_ARGB8888,
... ...
@@ -117,6 +117,24 @@ reui_title(reui_t *ui, char *titlefilename)
117 117
         return(0);
118 118
 }
119 119
 
120
+int
121
+reui_resize(reui_t *ui, int w, int h)
122
+{
123
+        SDL_Texture *newscr;
124
+        if(ui==NULL || w<=0 || h<=0)
125
+                return(-1);
126
+        if(ui->w==w && ui->h==h)
127
+                return(0); /* nothing to do */
128
+        if((newscr=SDL_CreateTexture(ui->renderer,SDL_PIXELFORMAT_ARGB8888,
129
+              SDL_TEXTUREACCESS_STREAMING,w,h))==NULL)
130
+                return(-1); /* couldn't create texture */
131
+        if(ui->scr!=NULL)
132
+                SDL_DestroyTexture(ui->scr),ui->scr=NULL;
133
+        ui->scr=newscr;
134
+        ui->h=h;
135
+        ui->w=w;
136
+        return(0);
137
+}
120 138
 
121 139
 int
122 140
 reui_fill(reui_t *ui, int x, int y, int w, int h, char *rgba)
Browse code

Implement side scroll. Fix vertical scroll. Set window size to something more workable than fullscreen.

Dario Rodriguez authored on 06/09/2020 22:21:40
Showing 1 changed files
... ...
@@ -19,6 +19,8 @@
19 19
 
20 20
 #define DEFAULTFONTSIZE 16
21 21
 #define DEFAULTTITLEPREFIX "re - "
22
+#define DEFAULTWINDOWWIDTH 80
23
+#define DEFAULTWINDOWHEIGHT 66
22 24
 
23 25
 #define RECTFILL(r,rx,ry,rw,rh) (r).x=(rx),(r).y=(ry),(r).w=(rw),(r).h=(rh)
24 26
 
... ...
@@ -32,20 +34,12 @@ reui_init()
32 34
         memset(ui,0,sizeof(reui_t));
33 35
         /* video */
34 36
         if(SDL_Init(SDL_INIT_VIDEO)<0
35
-          || SDL_GetDisplayBounds(0,&screenrect)!=0
36
-          || (ui->win=SDL_CreateWindow(
37
-              DEFAULTTITLEPREFIX,
38
-              SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,
39
-              screenrect.w,screenrect.h,0))==NULL
40
-          || (ui->renderer=SDL_CreateRenderer(ui->win,-1,
41
-              SDL_RENDERER_ACCELERATED|SDL_RENDERER_PRESENTVSYNC))==NULL
42
-          || (ui->scr=SDL_CreateTexture(ui->renderer,SDL_PIXELFORMAT_ARGB8888,
43
-              SDL_TEXTUREACCESS_STREAMING,screenrect.w,screenrect.h))==NULL) {
37
+          || SDL_GetDisplayBounds(0,&screenrect)!=0) {
44 38
                 reui_free(ui),ui=NULL;
45 39
                 return(NULL);
46 40
         }
47
-        ui->w=screenrect.w;
48
-        ui->h=screenrect.h;
41
+        ui->screenw=screenrect.w;
42
+        ui->screenh=screenrect.h;
49 43
         /* font */
50 44
         ui->fontheight=DEFAULTFONTSIZE;
51 45
         ui->fontdata=SDL_RWFromConstMem(hack_regular,SIZE_HACK_REGULAR);
... ...
@@ -67,6 +61,22 @@ reui_init()
67 61
                 ui->fontwidth=s->w;
68 62
                 SDL_FreeSurface(s),s=NULL;
69 63
         }
64
+        /* main window */
65
+        ui->w=ui->fontwidth*DEFAULTWINDOWWIDTH;
66
+        ui->w=(ui->w>ui->screenw)?ui->screenw:ui->w;
67
+        ui->h=ui->fontheight*DEFAULTWINDOWHEIGHT;
68
+        ui->h=(ui->h>ui->screenh)?ui->screenw:ui->h;
69
+        if((ui->win=SDL_CreateWindow(
70
+              DEFAULTTITLEPREFIX,
71
+              SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,
72
+              ui->w,ui->h,0))==NULL
73
+          || (ui->renderer=SDL_CreateRenderer(ui->win,-1,
74
+              SDL_RENDERER_ACCELERATED|SDL_RENDERER_PRESENTVSYNC))==NULL
75
+          || (ui->scr=SDL_CreateTexture(ui->renderer,SDL_PIXELFORMAT_ARGB8888,
76
+              SDL_TEXTUREACCESS_STREAMING,ui->w,ui->h))==NULL) {
77
+                reui_free(ui),ui=NULL;
78
+                return(NULL);
79
+        }
70 80
         /* finished */
71 81
         ui->scrdirty=1;
72 82
         ui->rendererdirty=1;
Browse code

Remove old utf8 functions from re_ui. Add comments to utf8 functions in re_data header file.

Dario Rodriguez authored on 02/09/2020 22:14:50
Showing 1 changed files
... ...
@@ -195,61 +195,4 @@ reui_printf(reui_t *ui, int x, int y, char *rgba, char *format, ...)
195 195
         return(0);
196 196
 }
197 197
 
198
-int
199
-reui_utf8len(reui_t *ui, char *ptr, int size)
200
-{
201
-        int len,i;
202
-        /* calculate the number of utf8-charaters in buffer */
203
-        if(ui==NULL || size<0 || (ptr==NULL && size!=0))
204
-                return(-1);
205
-        /* for now we only count the number of code points */
206
-        /* in UTF8: 0x00-0x7f single byte chars
207
-         *          0xc0-0xff leading bytes
208
-         *          0x80-0xbf continuation bytes (ignore these for len)*/
209
-/*#warning TODO: XXX support combining code points (at least U+0300 - U+036F ( https://en.wikipedia.org/wiki/Combining_character ) */
210
-        for(len=0,i=0;i<size;i++)
211
-                len+=((ptr[i]&0xc0)!=0x80)?1:0;
212
-        return(len);
213
-/*#warning TODO: XXX Also consider tabs*/
214
-}
215
-
216
-char *
217
-reui_utf8col(reui_t *ui, char *ptr, int size, int col)
218
-{
219
-        int len,i;
220
-        /* return a pointer to the "n"th ("col"th) utf8-character in buffer */
221
-        if(ui==NULL || size<0 || (ptr==NULL && size!=0))
222
-                return(NULL); /* sanity check failed */
223
-        /* see reui_utf8len() for explanation of algorithm */
224
-/*#warning TODO: support combining code points (at least U+0300 - U+036F ( https://en.wikipedia.org/wiki/Combining_character ) */
225
-        if(col>=size)
226
-                return(NULL);/* col greater than maximum possible char. count */
227
-        /* skip "col" amount of single byte chars and leading bytes */
228
-        for(len=0,i=0;len<col && i<size;i++)
229
-                len+=((ptr[i]&0xc0)!=0x80)?1:0;
230
-        /* if we landed in a continuation byte, advance until next single byte chars or leading byte */
231
-        while(i<size && (ptr[i]&0xc0)==0x80)
232
-                i++;
233
-        if(i>=size)
234
-                return(NULL); /* col is beyond end of string */
235
-        return(ptr+i);
236
-/*#warning TODO: XXX Also consider tabs*/
237
-}
238
-
239
-int
240
-reui_utf8charlen(reui_t *ui, char *ptr, int maxsize)
241
-{
242
-        int i;
243
-        /* returns the len in bytes of the character starting at ptr (zero on error)*/
244
-        if(ui==NULL || ptr==NULL || maxsize<1)
245
-                return(0); /* sanity check failed */
246
-/*#warning TODO: support combining code points (at least U+0300 - U+036F ( https://en.wikipedia.org/wiki/Combining_character ) */
247
-        if(((unsigned char *)ptr)[0]<0x80)
248
-                return(1); /* single byte char */
249
-        if((ptr[0]&0xc0)==0x80)
250
-                return(0); /* error: this is continuation, not leading byte */
251
-        for(i=1;i<maxsize && (ptr[i]&0xc0)==0x80;i++)
252
-                ;
253
-        return(i);
254
-}
255 198
 
Browse code

Implement left/right movement. Add test_ui with utf8 tests.

Dario Rodriguez authored on 30/08/2020 21:47:55
Showing 1 changed files
... ...
@@ -198,21 +198,58 @@ reui_printf(reui_t *ui, int x, int y, char *rgba, char *format, ...)
198 198
 int
199 199
 reui_utf8len(reui_t *ui, char *ptr, int size)
200 200
 {
201
+        int len,i;
201 202
         /* calculate the number of utf8-charaters in buffer */
202 203
         if(ui==NULL || size<0 || (ptr==NULL && size!=0))
203 204
                 return(-1);
204
-#warning TODO
205
-        return(size);
206
-#warning Also consider tabs
205
+        /* for now we only count the number of code points */
206
+        /* in UTF8: 0x00-0x7f single byte chars
207
+         *          0xc0-0xff leading bytes
208
+         *          0x80-0xbf continuation bytes (ignore these for len)*/
209
+/*#warning TODO: XXX support combining code points (at least U+0300 - U+036F ( https://en.wikipedia.org/wiki/Combining_character ) */
210
+        for(len=0,i=0;i<size;i++)
211
+                len+=((ptr[i]&0xc0)!=0x80)?1:0;
212
+        return(len);
213
+/*#warning TODO: XXX Also consider tabs*/
207 214
 }
208 215
 
209 216
 char *
210 217
 reui_utf8col(reui_t *ui, char *ptr, int size, int col)
211 218
 {
219
+        int len,i;
212 220
         /* return a pointer to the "n"th ("col"th) utf8-character in buffer */
213
-#warning TODO
214
-        if(col<size)
215
-                return(ptr+col);
216
-        return(NULL);
217
-#warning Also consider tabs
221
+        if(ui==NULL || size<0 || (ptr==NULL && size!=0))
222
+                return(NULL); /* sanity check failed */
223
+        /* see reui_utf8len() for explanation of algorithm */
224
+/*#warning TODO: support combining code points (at least U+0300 - U+036F ( https://en.wikipedia.org/wiki/Combining_character ) */
225
+        if(col>=size)
226
+                return(NULL);/* col greater than maximum possible char. count */
227
+        /* skip "col" amount of single byte chars and leading bytes */
228
+        for(len=0,i=0;len<col && i<size;i++)
229
+                len+=((ptr[i]&0xc0)!=0x80)?1:0;
230
+        /* if we landed in a continuation byte, advance until next single byte chars or leading byte */
231
+        while(i<size && (ptr[i]&0xc0)==0x80)
232
+                i++;
233
+        if(i>=size)
234
+                return(NULL); /* col is beyond end of string */
235
+        return(ptr+i);
236
+/*#warning TODO: XXX Also consider tabs*/
237
+}
238
+
239
+int
240
+reui_utf8charlen(reui_t *ui, char *ptr, int maxsize)
241
+{
242
+        int i;
243
+        /* returns the len in bytes of the character starting at ptr (zero on error)*/
244
+        if(ui==NULL || ptr==NULL || maxsize<1)
245
+                return(0); /* sanity check failed */
246
+/*#warning TODO: support combining code points (at least U+0300 - U+036F ( https://en.wikipedia.org/wiki/Combining_character ) */
247
+        if(((unsigned char *)ptr)[0]<0x80)
248
+                return(1); /* single byte char */
249
+        if((ptr[0]&0xc0)==0x80)
250
+                return(0); /* error: this is continuation, not leading byte */
251
+        for(i=1;i<maxsize && (ptr[i]&0xc0)==0x80;i++)
252
+                ;
253
+        return(i);
218 254
 }
255
+
Browse code

Implement up/down movement

Dario Rodriguez authored on 26/08/2020 21:50:45
Showing 1 changed files
... ...
@@ -56,6 +56,17 @@ reui_init()
56 56
                 reui_free(ui),ui=NULL;
57 57
                 return(NULL);
58 58
         }
59
+        /* font width */
60
+        {
61
+                SDL_Surface *s;
62
+                SDL_Color c={0,0,0,0};
63
+                if((s=TTF_RenderUTF8_Blended(ui->font,"m",c))==NULL) {
64
+                        reui_free(ui),ui=NULL;
65
+                        return(NULL);
66
+                }
67
+                ui->fontwidth=s->w;
68
+                SDL_FreeSurface(s),s=NULL;
69
+        }
59 70
         /* finished */
60 71
         ui->scrdirty=1;
61 72
         ui->rendererdirty=1;
... ...
@@ -98,13 +109,17 @@ reui_title(reui_t *ui, char *titlefilename)
98 109
 
99 110
 
100 111
 int
101
-reui_fill(reui_t *ui, int x, int y, int w, int h, unsigned char *rgba)
112
+reui_fill(reui_t *ui, int x, int y, int w, int h, char *rgba)
102 113
 {
103 114
         SDL_Rect dstrect;
104 115
         if(ui==NULL || rgba==NULL)
105 116
                 return(-1);
106 117
         RECTFILL(dstrect,x,y,w,h);
107
-        SDL_SetRenderDrawColor(ui->renderer,rgba[0],rgba[1],rgba[2],rgba[3]);
118
+        SDL_SetRenderDrawColor(ui->renderer,
119
+                ((unsigned char *)rgba)[0],
120
+                ((unsigned char *)rgba)[1],
121
+                ((unsigned char *)rgba)[2],
122
+                ((unsigned char *)rgba)[3]);
108 123
         SDL_RenderFillRect(ui->renderer, &dstrect);
109 124
         ui->rendererdirty=1;
110 125
         return(0);
... ...
@@ -134,12 +149,15 @@ reui_present(reui_t *ui)
134 149
 }
135 150
 
136 151
 int
137
-reui_write(reui_t *ui, int x, int y, unsigned char *rgba, char *str, int nchar)
152
+reui_write(reui_t *ui, int x, int y, char *rgba, char *str, int nchar)
138 153
 {
139 154
         char buf[1024];
140 155
         SDL_Surface *fgsurface;
141 156
         SDL_Texture *fg;
142
-        SDL_Color c={rgba[0],rgba[1],rgba[2],rgba[3]};
157
+        SDL_Color c={((unsigned char *)rgba)[0],
158
+                     ((unsigned char *)rgba)[1],
159
+                     ((unsigned char *)rgba)[2],
160
+                     ((unsigned char *)rgba)[3]};
143 161
         SDL_Rect dstrect;
144 162
         if(nchar<sizeof(buf)) {
145 163
                 memcpy(buf,str,nchar);
... ...
@@ -164,7 +182,7 @@ reui_write(reui_t *ui, int x, int y, unsigned char *rgba, char *str, int nchar)
164 182
 
165 183
 
166 184
 int
167
-reui_printf(reui_t *ui, int x, int y, unsigned char *rgba, char *format, ...)
185
+reui_printf(reui_t *ui, int x, int y, char *rgba, char *format, ...)
168 186
 {
169 187
         char buf[1024];
170 188
         va_list l;
... ...
@@ -176,3 +194,25 @@ reui_printf(reui_t *ui, int x, int y, unsigned char *rgba, char *format, ...)
176 194
                 return(-1);
177 195
         return(0);
178 196
 }
197
+
198
+int
199
+reui_utf8len(reui_t *ui, char *ptr, int size)
200
+{
201
+        /* calculate the number of utf8-charaters in buffer */
202
+        if(ui==NULL || size<0 || (ptr==NULL && size!=0))
203
+                return(-1);
204
+#warning TODO
205
+        return(size);
206
+#warning Also consider tabs
207
+}
208
+
209
+char *
210
+reui_utf8col(reui_t *ui, char *ptr, int size, int col)
211
+{
212
+        /* return a pointer to the "n"th ("col"th) utf8-character in buffer */
213
+#warning TODO
214
+        if(col<size)
215
+                return(ptr+col);
216
+        return(NULL);
217
+#warning Also consider tabs
218
+}
Browse code

Display first lines of the loaded file

Dario Rodriguez authored on 18/08/2020 20:31:28
Showing 1 changed files
... ...
@@ -13,24 +13,166 @@
13 13
 #include <stdlib.h>
14 14
 #include <unistd.h>
15 15
 #include <string.h>
16
+#include <stdarg.h>
16 17
 #include "re_ui.h"
18
+#include "hack_regular.h"
19
+
20
+#define DEFAULTFONTSIZE 16
21
+#define DEFAULTTITLEPREFIX "re - "
22
+
23
+#define RECTFILL(r,rx,ry,rw,rh) (r).x=(rx),(r).y=(ry),(r).w=(rw),(r).h=(rh)
17 24
 
18 25
 reui_t *
19 26
 reui_init()
20 27
 {
21 28
         reui_t *ui;
29
+        SDL_Rect screenrect;
22 30
         if((ui=malloc(sizeof(reui_t)))==NULL)
23 31
                 return(NULL);
24 32
         memset(ui,0,sizeof(reui_t));
33
+        /* video */
34
+        if(SDL_Init(SDL_INIT_VIDEO)<0
35
+          || SDL_GetDisplayBounds(0,&screenrect)!=0
36
+          || (ui->win=SDL_CreateWindow(
37
+              DEFAULTTITLEPREFIX,
38
+              SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,
39
+              screenrect.w,screenrect.h,0))==NULL
40
+          || (ui->renderer=SDL_CreateRenderer(ui->win,-1,
41
+              SDL_RENDERER_ACCELERATED|SDL_RENDERER_PRESENTVSYNC))==NULL
42
+          || (ui->scr=SDL_CreateTexture(ui->renderer,SDL_PIXELFORMAT_ARGB8888,
43
+              SDL_TEXTUREACCESS_STREAMING,screenrect.w,screenrect.h))==NULL) {
44
+                reui_free(ui),ui=NULL;
45
+                return(NULL);
46
+        }
47
+        ui->w=screenrect.w;
48
+        ui->h=screenrect.h;
49
+        /* font */
50
+        ui->fontheight=DEFAULTFONTSIZE;
51
+        ui->fontdata=SDL_RWFromConstMem(hack_regular,SIZE_HACK_REGULAR);
52
+        SDL_RWseek(ui->fontdata,0,RW_SEEK_SET);
53
+        if(ui->fontdata==NULL
54
+          || TTF_Init()==-1
55
+          || (ui->font=TTF_OpenFontRW(ui->fontdata,0,ui->fontheight))==NULL) {
56
+                reui_free(ui),ui=NULL;
57
+                return(NULL);
58
+        }
59
+        /* finished */
60
+        ui->scrdirty=1;
61
+        ui->rendererdirty=1;
25 62
         return(ui);
26 63
 }
27 64
 
28 65
 
29 66
 void
30
-reui_fini(reui_t *ui)
67
+reui_free(reui_t *ui)
31 68
 {
32 69
         if(ui==NULL)
33 70
                 return;
71
+        if(ui->font!=NULL)
72
+                TTF_CloseFont(ui->font),ui->fontdata=NULL;
73
+        if(TTF_WasInit())
74
+                TTF_Quit();
75
+        if(ui->fontdata!=NULL)
76
+                SDL_FreeRW(ui->fontdata),ui->fontdata=NULL;
77
+        if(ui->scr!=NULL)
78
+                SDL_DestroyTexture(ui->scr),ui->scr=NULL;
79
+        if(ui->renderer!=NULL)
80
+                SDL_DestroyRenderer(ui->renderer),ui->renderer=NULL;
81
+        if(ui->win!=NULL)
82
+                SDL_DestroyWindow(ui->win),ui->win=NULL;
83
+        SDL_Quit();
34 84
         free(ui),ui=NULL;
35 85
 }
36 86
 
87
+int
88
+reui_title(reui_t *ui, char *titlefilename)
89
+{
90
+        char buf[1024];
91
+        if(ui==NULL || titlefilename==NULL)
92
+                return(-1);
93
+        snprintf(buf,sizeof(buf),"%s%s",DEFAULTTITLEPREFIX,titlefilename);
94
+        buf[sizeof(buf)-1]='\0';
95
+        SDL_SetWindowTitle(ui->win,buf);
96
+        return(0);
97
+}
98
+
99
+
100
+int
101
+reui_fill(reui_t *ui, int x, int y, int w, int h, unsigned char *rgba)
102
+{
103
+        SDL_Rect dstrect;
104
+        if(ui==NULL || rgba==NULL)
105
+                return(-1);
106
+        RECTFILL(dstrect,x,y,w,h);
107
+        SDL_SetRenderDrawColor(ui->renderer,rgba[0],rgba[1],rgba[2],rgba[3]);
108
+        SDL_RenderFillRect(ui->renderer, &dstrect);
109
+        ui->rendererdirty=1;
110
+        return(0);
111
+}
112
+
113
+int
114
+reui_scr2renderer(reui_t *ui, int x, int y, int w, int h)
115
+{
116
+        SDL_Rect srcrect,dstrect;
117
+        if(ui==NULL)
118
+                return(-1);
119
+        RECTFILL(srcrect,x,y,w,h);
120
+        RECTFILL(dstrect,x,y,w,h);
121
+        SDL_RenderCopy(ui->renderer,ui->scr,&srcrect,&dstrect);
122
+        ui->rendererdirty=1;
123
+        return(0);
124
+}
125
+
126
+int
127
+reui_present(reui_t *ui)
128
+{
129
+        if(ui==NULL)
130
+                return(-1);
131
+        SDL_RenderPresent(ui->renderer);
132
+        ui->rendererdirty=0;
133
+        return(0);
134
+}
135
+
136
+int
137
+reui_write(reui_t *ui, int x, int y, unsigned char *rgba, char *str, int nchar)
138
+{
139
+        char buf[1024];
140
+        SDL_Surface *fgsurface;
141
+        SDL_Texture *fg;
142
+        SDL_Color c={rgba[0],rgba[1],rgba[2],rgba[3]};
143
+        SDL_Rect dstrect;
144
+        if(nchar<sizeof(buf)) {
145
+                memcpy(buf,str,nchar);
146
+                buf[nchar]='\0';
147
+        } else {
148
+                memcpy(buf,str,sizeof(buf)-1);
149
+                buf[sizeof(buf)-1]='\0';
150
+        }
151
+        if((fgsurface=TTF_RenderUTF8_Blended(ui->font,buf,c))==NULL
152
+          || (fg=SDL_CreateTextureFromSurface(ui->renderer,fgsurface))==NULL) {
153
+                if(fgsurface!=NULL)
154
+                        SDL_FreeSurface(fgsurface),fgsurface=NULL;
155
+                return(-1);
156
+        }
157
+        RECTFILL(dstrect,x,y,fgsurface->w,fgsurface->h);
158
+        SDL_RenderCopy(ui->renderer,fg,NULL,&dstrect);
159
+        ui->rendererdirty=1;
160
+        SDL_FreeSurface(fgsurface),fgsurface=NULL;
161
+        SDL_DestroyTexture(fg),fg=NULL;
162
+        return(0);
163
+}
164
+
165
+
166
+int
167
+reui_printf(reui_t *ui, int x, int y, unsigned char *rgba, char *format, ...)
168
+{
169
+        char buf[1024];
170
+        va_list l;
171
+        va_start(l,format);
172
+        vsnprintf(buf,sizeof(buf),format,l);
173
+        va_end(l);
174
+        buf[sizeof(buf)-1]='\0';
175
+        if(reui_write(ui,x,y,rgba,buf,strlen(buf))!=0)
176
+                return(-1);
177
+        return(0);
178
+}
Browse code

Add re_ui.c stub, fix Makefile

Dario Rodriguez authored on 07/08/2020 21:30:22
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,36 @@
1
+/*
2
+ * re_ui.c
3
+ *
4
+ * A programmers editor
5
+ *
6
+ * Simple UI in SDL2
7
+ *
8
+ * Author: Dario Rodriguez dario@softhome.net
9
+ * This program is licensed under the terms of GNU GPL v2.1+
10
+ */
11
+
12
+#include <stdio.h>
13
+#include <stdlib.h>
14
+#include <unistd.h>
15
+#include <string.h>
16
+#include "re_ui.h"
17
+
18
+reui_t *
19
+reui_init()
20
+{
21
+        reui_t *ui;
22
+        if((ui=malloc(sizeof(reui_t)))==NULL)
23
+                return(NULL);
24
+        memset(ui,0,sizeof(reui_t));
25
+        return(ui);
26
+}
27
+
28
+
29
+void
30
+reui_fini(reui_t *ui)
31
+{
32
+        if(ui==NULL)
33
+                return;
34
+        free(ui),ui=NULL;
35
+}
36
+