-
Notifications
You must be signed in to change notification settings - Fork 3
/
blitting.c
1121 lines (919 loc) · 34.2 KB
/
blitting.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "blitting.h"
#include "png_dec.h"
#include "misc.h"
#include "mem.h"
#include <cell/rtc.h>
// graphic buffers and drawing context
static DrawCtx ctx __attribute__((aligned(16))); // drawing context
// display values
static uint32_t unk1 = 0, offset = 0, pitch = 0;
static uint16_t h = 0, w = 0, canvas_x = 0, canvas_y = 0;
/***********************************************************************
* pause rsx fifo
***********************************************************************/
void pause_RSX_rendering()
{
// is a flip occurred ? (the moment RSX has finished the rendering of a new frame and flip him on screen)
while(1)
if(*(uint32_t*)0x60201100 == 0x80000000) break;
rsx_fifo_pause(1); // pause rsx fifo (no new frames...)
}
/***********************************************************************
* alpha blending (ARGB)
*
* uint32_t bg = background color
* uint32_t fg = foreground color
***********************************************************************/
static uint32_t mix_color(const uint32_t bg, const uint32_t fg)
{
uint32_t a = fg >>24;
if(a == 0) return bg;
uint32_t rb = (((fg & 0x00FF00FF) * a) + ((bg & 0x00FF00FF) * (255 - a))) & 0xFF00FF00;
uint32_t g = (((fg & 0x0000FF00) * a) + ((bg & 0x0000FF00) * (255 - a))) & 0x00FF0000;
uint32_t Fg = a + ((bg >>24) * (255 - a) / 255);
return (Fg <<24) | ((rb | g) >>8);
}
/***********************************************************************
* dump background
***********************************************************************/
static void dump_bg(void)
{
uint16_t i, k;
uint64_t *bg = (uint64_t*)ctx.bg;
for(i = 0; i < CANVAS_H; i++)
for(k = 0; k < CANVAS_W /2; k++)
bg[k + i * CANVAS_W /2] = *(uint64_t*)(OFFSET(canvas_x + (k*2), canvas_y + (i)));
}
#ifdef HAVE_SYS_FONT
static Bitmap *bitmap = NULL; // font glyph cache
static const CellFontLibrary *font_lib_ptr = NULL; // font library pointer
static uint32_t vsh_fonts[16] = {}; // addresses of the 16 system font slots
int32_t LINE_HEIGHT = 0;
/***********************************************************************
* get font object
***********************************************************************/
static int32_t get_font_object(void)
{
uint8_t i;
int32_t font_obj = 0; // vsh font library object address
int32_t pm_start = 0x10000;
uint64_t pat[2] = {0x3800001090810080ULL, 0x90A100849161008CULL};
while(pm_start < 0x700000)
{
if((*(uint64_t*) pm_start == pat[0])
&& (*(uint64_t*)(pm_start +8) == pat[1]))
{
// get font object
font_obj = (int32_t)(
(int32_t)((*(int32_t*)(pm_start +0x4C) & 0x0000FFFF) <<16) +
(int16_t)( *(int32_t*)(pm_start +0x54) & 0x0000FFFF));
// get font library pointer
font_lib_ptr = (void*)(*(int32_t*)font_obj);
// get addresses of loaded sys fonts
for(i = 0; i < 16; i++)
vsh_fonts[i] = (font_obj +0x14 + (i * 0x100));
return 0;
}
pm_start += 4;
}
return -1;
}
/***********************************************************************
* set font with default settings
***********************************************************************/
static void set_font_default(void)
{
int32_t i;
bitmap = mem_alloc(sizeof(Bitmap));
memset(bitmap, 0, sizeof(Bitmap));
// set font
FontSetScalePixel(&ctx.font, FONT_W, FONT_H);
FontSetEffectWeight(&ctx.font, FONT_WEIGHT);
FontGetHorizontalLayout(&ctx.font, &bitmap->horizontal_layout);
LINE_HEIGHT = bitmap->horizontal_layout.lineHeight;
bitmap->max = FONT_CACHE_MAX;
bitmap->count = 0;
bitmap->font_w = FONT_W;
bitmap->font_h = FONT_H;
bitmap->weight = FONT_WEIGHT;
for(i = 0; i < FONT_CACHE_MAX; i++)
bitmap->glyph[i].image = (uint8_t *)ctx.font_cache + (i * 0x400);
}
/***********************************************************************
* unbind and destroy renderer, close font instance
***********************************************************************/
static void font_init(void)
{
uint32_t user_id = 0, val = 0;
CellFontRendererConfig rd_cfg;
CellFont *opened_font = NULL;
get_font_object();
// get id of current logged in user for the xRegistry query we do next
user_id = xsetting_CC56EB2D()->GetCurrentUserNumber();
// get current font style for the current logged in user
xsetting_CC56EB2D()->GetRegistryValue(user_id, 0x5C, &val);
// get sysfont
switch(val)
{
case 0: // original
opened_font = (void*)(vsh_fonts[5]);
break;
case 1: // rounded
opened_font = (void*)(vsh_fonts[8]);
break;
case 3: // pop
opened_font = (void*)(vsh_fonts[10]);
break;
default: // better than nothing
opened_font = (void*)(vsh_fonts[0]);
break;
}
FontOpenFontInstance(opened_font, &ctx.font);
memset(&rd_cfg, 0, sizeof(CellFontRendererConfig));
FontCreateRenderer(font_lib_ptr, &rd_cfg, &ctx.renderer);
FontBindRenderer(&ctx.font, &ctx.renderer);
set_font_default();
}
/***********************************************************************
* unbind and destroy renderer, close font instance
***********************************************************************/
void font_finalize(void)
{
FontUnbindRenderer(&ctx.font);
FontDestroyRenderer(&ctx.renderer);
FontCloseFont(&ctx.font);
}
/***********************************************************************
* render a char glyph bitmap into bitmap cache by index
*
* int32_t cache_idx = index into cache
* uint32_t code = unicode of char glyph to render
***********************************************************************/
static void render_glyph(int32_t idx, uint32_t code)
{
CellFontRenderSurface surface;
CellFontGlyphMetrics metrics;
CellFontImageTransInfo transinfo;
int32_t i, k, x, y, w, h;
int32_t ibw;
// setup render settings
FontSetupRenderScalePixel(&ctx.font, bitmap->font_w, bitmap->font_h);
FontSetupRenderEffectWeight(&ctx.font, bitmap->weight);
x = ((int32_t)bitmap->font_w) *2, w = x *2;
y = ((int32_t)bitmap->font_h) *2, h = y *2;
// set surface
FontRenderSurfaceInit(&surface, NULL, w, 1, w, h);
// set render surface scissor, (full area/no scissoring)
FontRenderSurfaceSetScissor(&surface, 0, 0, w, h);
bitmap->glyph[idx].code = code;
FontRenderCharGlyphImage(&ctx.font, bitmap->glyph[idx].code, &surface,
(float_t)x, (float_t)y, &metrics, &transinfo);
bitmap->count++;
ibw = transinfo.imageWidthByte;
bitmap->glyph[idx].w = transinfo.imageWidth; // width of char image
bitmap->glyph[idx].h = transinfo.imageHeight; // height of char image
// copy glyph bitmap into cache
for(k = 0; k < bitmap->glyph[idx].h; k++)
for(i = 0; i < bitmap->glyph[idx].w; i++)
bitmap->glyph[idx].image[k*bitmap->glyph[idx].w + i] =
transinfo.Image[k * ibw + i];
bitmap->glyph[idx].metrics = metrics;
}
/***********************************************************************
*
***********************************************************************/
static Glyph *get_glyph(uint32_t code)
{
int32_t i, new;
Glyph *glyph;
// search glyph into cache
for(i = 0; i < bitmap->count; i++)
{
glyph = &bitmap->glyph[i];
if(glyph->code == code) return glyph;
}
// if glyph not into cache
new = bitmap->count + 1;
if(new >= bitmap->max) // if cache full
bitmap->count = new = 0; // reset
// render glyph
render_glyph(new, code);
glyph = &bitmap->glyph[new];
return glyph;
}
/***********************************************************************
* set new font values
*
* float_t font_w = char width
* float_t font_h = char height
* float_t weight = line weight
* int32_t distance = distance between chars
***********************************************************************/
void set_font(float_t font_w, float_t font_h, float_t weight, int32_t distance)
{
// max size is 32 * 32 pixels
if(font_w > 32.f && font_h > 32.f)
font_w = font_h = 32.f;
// set font
FontSetScalePixel(&ctx.font, font_w, font_h);
FontSetEffectWeight(&ctx.font, weight);
// get and set new line height
FontGetHorizontalLayout(&ctx.font, &bitmap->horizontal_layout);
LINE_HEIGHT = bitmap->horizontal_layout.lineHeight;
bitmap->count = 0; // reset font cache
bitmap->font_w = font_w;
bitmap->font_h = font_h;
bitmap->weight = weight;
bitmap->distance = distance;
}
/***********************************************************************
* get ucs4 code from utf8 sequence
*
* uint8_t *utf8 = utf8 string
* uint32_t *ucs4 = variable to hold ucs4 code
***********************************************************************/
static int32_t utf8_to_ucs4(uint8_t *utf8, uint32_t *ucs4)
{
uint8_t len = 0;
uint32_t c1 = 0, c2 = 0, c3 = 0, c4 = 0;
c1 = (uint32_t)*utf8; utf8++;
if(c1 <= 0x7F) // 1 byte sequence, ascii
{
len = 1; *ucs4 = c1;
}
else if((c1 & 0xE0) == 0xC0) // 2 byte sequence
{
len = 2; c2 = (uint32_t)*utf8;
if((c2 & 0xC0) == 0x80)
*ucs4 = ((c1 & 0x1F) << 6) | (c2 & 0x3F);
else
len = *ucs4 = 0;
}
else if((c1 & 0xF0) == 0xE0) // 3 bytes sequence
{
len = 3; c2 = (uint32_t)*utf8; utf8++;
if((c2 & 0xC0) == 0x80)
{
c3 = (uint32_t)*utf8;
if((c3 & 0xC0) == 0x80)
*ucs4 = ((c1 & 0x0F) << 12) | ((c2 & 0x3F) << 6) | (c3 & 0x3F);
else
len = *ucs4 = 0;
}
else
len = *ucs4 = 0;
}
else if((c1 & 0xF8) == 0xF0) // 4 bytes sequence
{
len = 4; c2 = (uint32_t)*utf8; utf8++;
if((c2 & 0xC0) == 0x80)
{
c3 = (uint32_t)*utf8; utf8++;
if((c3 & 0xC0) == 0x80)
{
c4 = (uint32_t)*utf8;
if((c4 & 0xC0) == 0x80)
*ucs4 = ((c1 & 0x07) << 18) | ((c2 & 0x3F) << 12) | ((c3 & 0x3F) << 6) | (c4 & 0x3F);
else
len = *ucs4 = 0;
}
else
len = *ucs4 = 0;
}
else
len = *ucs4 = 0;
}
else
len = *ucs4 = 0;
return len;
}
/***********************************************************************
* get render lenght of text, in pixel
*
* const char *str = string to measure
***********************************************************************/
uint16_t get_render_length(const char *str)
{
uint32_t code = 0; // char unicode
uint8_t *utf8 = (uint8_t*)str;
uint16_t len = 0;
Glyph *glyph; // char glyph
memset(&glyph, 0, sizeof(Glyph));
while(1) // get render length
{
utf8 += utf8_to_ucs4(utf8, &code);
if(code == 0) break;
glyph = get_glyph(code);
len += glyph->metrics.Horizontal.advance + bitmap->distance;
}
return (uint16_t)(len - bitmap->distance);
}
/***********************************************************************
* print text, from prerendered TTF
*
* int32_t x = start x coordinate into canvas
* int32_t y = start y coordinate into canvas
* const char *str = string to print
***********************************************************************/
int32_t print_text(int32_t x, int32_t y, const char *str)
{
int32_t i, k;
uint32_t code = 0; // char unicode
int32_t t_x = x, t_y = y; // temp x/y
int32_t o_x = x, o_y = y + bitmap->horizontal_layout.baseLineY; // origin x/y
Glyph *glyph; // char glyph
uint8_t *utf8 = (uint8_t*)str;
uint32_t *px = NULL;
memset(&glyph, 0, sizeof(Glyph));
// render text
while(*str != '\0')
{
utf8 += utf8_to_ucs4(utf8, &code);
if(code == 0)
{
break;
}
else if(code == '\n')
{
o_x = x;
o_y += bitmap->horizontal_layout.lineHeight;
continue;
}
else
{
// get glyph to draw
glyph = get_glyph(code);
// get bitmap origin(x, y)
t_x = o_x + glyph->metrics.Horizontal.bearingX;
t_y = o_y - glyph->metrics.Horizontal.bearingY;
// draw bitmap
for(i = 0; i < glyph->h; i++)
for(k = 0; k < glyph->w; k++)
if((glyph->image[i * glyph->w + k])
&& (t_x + k < CANVAS_W)
&& (t_y + i < CANVAS_H))
{
px = &ctx.canvas[(t_y + i) * CANVAS_W + t_x + k];
// paint FG pixel with precomputed fading colors
*px = mix_color(*px,
((uint32_t)glyph->image[i * glyph->w + k] <<24) |
(ctx.fading_color[i /2] & 0x00FFFFFF));
// displace shadow by (+SHADOW_PX, +SHADOW_PX)
px += (SHADOW_PX * CANVAS_W + SHADOW_PX);
// paint the shadow
*px = mix_color(*px, ctx.bg_color & 0x66000000);
}
// get origin-x for next char
o_x += glyph->metrics.Horizontal.advance + bitmap->distance;
}
}
return o_x;
}
#endif // HAVE_SYS_FONT
/***********************************************************************
*
***********************************************************************/
void init_graphic()
{
memset(&ctx, 0, sizeof(DrawCtx));
// set drawing context
ctx.canvas = mem_alloc(CANVAS_W * CANVAS_H * sizeof(uint32_t)); // canvas buffer
ctx.bg = mem_alloc(CANVAS_W * CANVAS_H * sizeof(uint32_t)); // background buffer
ctx.bg_color = 0xFF000000; // black, opaque
#ifdef HAVE_SYS_FONT
ctx.font_cache = mem_alloc(FONT_CACHE_MAX * 32 * 32); // glyph bitmap cache
font_init();
#elif HAVE_PNG_FONT
Buffer font = load_png(PNG_FONT_PATH); // load font png
ctx.font = font.addr;
ctx.fg_color = 0xFFFFFFFF; // white, opaque
#endif
// get current display values
offset = *(uint32_t*)0x60201104; // start offset of current framebuffer
getDisplayPitch(&pitch, &unk1); // framebuffer pitch size
h = getDisplayHeight(); // display height
w = getDisplayWidth(); // display width
// get x/y start coordinates for our canvas, always center
canvas_x = (w - CANVAS_W) /2;
canvas_y = (h - CANVAS_H) /2;
// dump background, for alpha blending
dump_bg();
// init first frame with background dump
memcpy((uint32_t*)ctx.canvas, (uint32_t*)ctx.bg, CANVAS_W * CANVAS_H * sizeof(uint32_t));
}
/***********************************************************************
* flip finished frame into paused ps3-framebuffer
***********************************************************************/
void flip_frame()
{
uint16_t i, k;
uint64_t *canvas = (uint64_t*)ctx.canvas;
for(i = 0; i < CANVAS_H; i++)
for(k = 0; k < CANVAS_W /2; k++)
*(uint64_t*)(OFFSET(canvas_x + (k*2), canvas_y + (i))) = canvas[k + i * CANVAS_W /2];
// after flip, clear frame buffer with background
memcpy((uint32_t*)ctx.canvas, (uint32_t*)ctx.bg, CANVAS_W * CANVAS_H * sizeof(uint32_t));
}
/***********************************************************************
* set background color
***********************************************************************/
void set_background_color(uint32_t color)
{
ctx.bg_color = color;
}
#ifdef HAVE_PNG_FONT
/***********************************************************************
* set foreground color
***********************************************************************/
void set_foreground_color(uint32_t color)
{
ctx.fg_color = color;
}
#else
/***********************************************************************
* linear gradient (ARGB)
*
* uint32_t *a = pointer to foreground start color
* uint32_t *b = pointer to foreground end color
* uint8_t steps = number of chunk we split fading
* uint8_t step = which step we compute and return
***********************************************************************/
static uint32_t linear_gradient(const uint32_t *a, const uint32_t *b, const uint8_t steps, const uint8_t step)
{
uint8_t fr[4], to[4];
float_t st[4];
fr[0] = GET_A(*a), fr[1] = GET_R(*a), fr[2] = GET_G(*a), fr[3] = GET_B(*a),
to[0] = GET_A(*b), to[1] = GET_R(*b), to[2] = GET_G(*b), to[3] = GET_B(*b);
st[0] = ((to[0] - fr[0]) / (float_t)(steps -1));
st[1] = ((to[1] - fr[1]) / (float_t)(steps -1));
st[2] = ((to[2] - fr[2]) / (float_t)(steps -1));
st[3] = ((to[3] - fr[3]) / (float_t)(steps -1));
return ARGB((int)fr[0] + (int)(st[0] * step),
(int)fr[1] + (int)(st[1] * step),
(int)fr[2] + (int)(st[2] * step),
(int)fr[3] + (int)(st[3] * step));
}
/***********************************************************************
* update_gradient
*
* precompute palette to use in print_text() and setup colors range
*
* uint32_t *a = pointer to foreground start color
* uint32_t *b = pointer to foreground end color
***********************************************************************/
void update_gradient(const uint32_t *a, const uint32_t *b)
{
for(uint8_t i = 0; i < LINEAR_GRADIENT_STEP; i++)
{
ctx.fading_color[i] = (*a == *b) ? *a : linear_gradient(a, b, LINEAR_GRADIENT_STEP, i);
}
}
#endif
/***********************************************************************
* draw background, with current background color
***********************************************************************/
void draw_background(void)
{
for(uint32_t i = 0; i < CANVAS_W * CANVAS_H; i++)
{
ctx.canvas[i] = mix_color(ctx.bg[i], ctx.bg_color);
}
}
/***********************************************************************
* apply alpha-blended background color to canvas
***********************************************************************/
void blend_canvas(void)
{
uint32_t *px = &ctx.canvas[0];
for(uint32_t i = 0; i < CANVAS_W * CANVAS_H; i++)
{
*px = mix_color(*px, ctx.bg_color); px++;
}
}
/***********************************************************************
* compute x to align text into canvas
*
* const char *str = referring string
* uint8_t align. = RIGHT / CENTER (1/2)
***********************************************************************/
uint16_t get_aligned_x(const char *str, const uint8_t alignment)
{
uint16_t len; // str lenght, in pixels!
#ifdef HAVE_SYS_FONT
len = get_render_length(str);
#else
len = (strlen(str) * FONT_W); // monospaced font
#endif
return (uint16_t)((CANVAS_W - len) / alignment);
}
#ifdef HAVE_PNG_FONT
/***********************************************************************
* print text, with data from font.png
*
* int32_t x = start x coordinate into canvas
* int32_t y = start y coordinate into canvas
* const char *str = string to print
***********************************************************************/
int32_t print_text(int32_t x, int32_t y, const char *str)
{
int32_t c_x = x, c_y = y;
int32_t i = 0, char_w = 0, p_x = 0, p_y = 0;
int32_t tmp_x = 0, tmp_y = 0;
uint8_t c = 0;
while(*str != '\0')
{
c = *str;
if(c > 127)
{
str++;
c = (*str & 0x3F) | 0x80;
}
if(c == '\n')
{
c_x = x;
c_y += FONT_H;
}
else
{
p_y = (c >> 4) * FONT_H * FONT_PNG_W;
p_x = (c & 0x0F) * FONT_W;
char_w = ctx.font[p_x + p_y];
for(i = 0; i < FONT_H * char_w; i++)
{
if((ctx.font[(p_x + tmp_x) + (p_y + tmp_y * FONT_PNG_W)]) != 0)
{
ctx.canvas[(c_y + tmp_y) * CANVAS_W + c_x + tmp_x] =
mix_color(ctx.canvas[(c_y + tmp_y) * CANVAS_W + c_x + tmp_x],
(ctx.font[(p_x + tmp_x) + (p_y + tmp_y * FONT_PNG_W)] & 0xFF000000) |
(ctx.fg_color & 0x00FFFFFF));
}
tmp_x++;
if(tmp_x == char_w)
{
tmp_x = 0, tmp_y++;
}
}
tmp_y = 0;
c_x += char_w;
}
str++;
}
return c_x;
}
#elif HAVE_XBM_FONT
#include "xbm_font.h"
/***********************************************************************
* print text, with bitmap data from xbm_font.h
*
* int32_t x = start x coordinate into canvas
* int32_t y = start y coordinate into canvas
* const char *str = string to print
***********************************************************************/
int32_t print_text(int32_t x, int32_t y, const char *str)
{
uint8_t *c, i, j, tx = 0, ty = 0;
uint32_t *px = NULL;
while(*str != '\0')
{
c = (uint8_t*)str++; // address the current char
if(*c < LOWER_ASCII_CODE
|| *c > UPPER_ASCII_CODE)
{ x += FONT_W; continue; } // skipped, move one char in canvas
char *bit = xbmFont[*c - LOWER_ASCII_CODE];
// dump bits map (bytes_per_line 2, size 32 char of 8 bit)
for(i = 0; i < ((FONT_W * FONT_H) / BITS_IN_BYTE); i++)
{
for(j = 0; j < BITS_IN_BYTE; j++)
{
if(bit[i] & (1 << j)) // least significant bit first
{
px = &ctx.canvas[(x + tx * BITS_IN_BYTE + j) + (y + ty) * CANVAS_W];
// paint FG pixel with precomputed fading colors
*px = ctx.fading_color[ty /2];
// displace shadow by (+SHADOW_PX, +SHADOW_PX)
px += (SHADOW_PX * CANVAS_W + SHADOW_PX);
// paint the shadow
*px = mix_color(*px, ctx.bg_color & 0x66000000);
}
}
tx++;
if(tx == (FONT_W / BITS_IN_BYTE))
tx = 0, ty++; // step to decrease gradient
}
// glyph painted, move one char right in text
x += FONT_W, ty = 0;
}
return x;
}
#endif // HAVE_XBM_FONT
/***********************************************************************
* load a png file
*
* int32_t idx = index of png, max 4 (0 - 3)
* const char *path = path to png file
***********************************************************************/
int32_t load_png_bitmap(const int32_t idx, const char *path)
{
if(idx > PNG_MAX) return -1;
mem_free(ctx.png[idx].w * ctx.png[idx].h * 4); // first run is zeroed
ctx.png[idx] = load_png(path);
return 0;
}
/***********************************************************************
* draw png part into frame.
*
* int32_t idx = index of loaded png
* int32_t can_x = start x coordinate into canvas
* int32_t can_y = start y coordinate into canvas
* int32_t png_x = start x coordinate into png
* int32_t png_y = start y coordinate into png
* int32_t w = width of png part to blit
* int32_t h = height of png part to blit
***********************************************************************/
int32_t draw_png(const int32_t idx, const int32_t c_x, const int32_t c_y, const int32_t p_x, const int32_t p_y, const int32_t w, const int32_t h)
{
int32_t i, k;
uint32_t *px = NULL;
for(i = 0; i < h; i++)
for(k = 0; k < w; k++)
if((c_x + k < CANVAS_W)
&& (c_y + i < CANVAS_H))
{
px = &ctx.canvas[(c_y + i) * CANVAS_W + c_x + k];
*px = mix_color(*px,
ctx.png[idx].addr[(p_x + p_y * ctx.png[idx].w) + (k + i * ctx.png[idx].w)]);
}
return (c_x + w);
}
/* Simple 2x scale factor variant */
int32_t draw_png_2x(const int32_t idx,
const int32_t c_x, const int32_t c_y, // coordinate into canvas
const int32_t p_x, const int32_t p_y, // coordinate into png
const uint32_t w, const uint32_t h) // original size of png part to blit
{
uint32_t i, k;
uint32_t *px = NULL, *src = NULL;
for(i = 0; i < h; i++)
for(k = 0; k < w; k++)
{
if((c_x + 2* k < CANVAS_W)
&& (c_y + 2* i < CANVAS_H))
{
src = &ctx.png[idx].addr[(p_x + p_y * ctx.png[idx].w) + (k + i * ctx.png[idx].w)];
/*
copying src in four pixels: H * CANVAS_W + W
(2* H ) * CANVAS_W) + 2* W,
(2* H ) * CANVAS_W) + 2* W +1,
(2* H +1) * CANVAS_W) + 2* W,
(2* H +1) * CANVAS_W) + 2* W +1
px = &ctx.canvas[ 2* (c_y + i) * CANVAS_W + 2* (c_x + k) ]; *px = mix_color(*px, *src);
px = &ctx.canvas[ 2* (c_y + i) * CANVAS_W + 2* (c_x + k) +1]; *px = mix_color(*px, *src);
px = &ctx.canvas[(2* (c_y + i) +1) * CANVAS_W + 2* (c_x + k) ]; *px = mix_color(*px, *src);
px = &ctx.canvas[(2* (c_y + i) +1) * CANVAS_W + 2* (c_x + k) +1]; *px = mix_color(*px, *src);
easily can become painting clockwise:
*/ px = &ctx.canvas[ 2* (c_y + i) * CANVAS_W + 2* (c_x + k) ]; *px = mix_color(*px, *src);
px++; *px = mix_color(*px, *src);
px += CANVAS_W; *px = mix_color(*px, *src);
px--; *px = mix_color(*px, *src);
}
}
return (c_x + 2* w);
}
/***********************************************************************
* xmb screenshot
*
* uint8_t mode = 0(XMB only), 1(XMB + menu)
***********************************************************************/
uint8_t bmp_header[] = {
0x42, 0x4D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00, 0x28, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x0B, 0x00, 0x00, 0x12, 0x0B, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
void screenshot(const uint8_t mode)
{
FILE *fd = NULL;
uint32_t tmp = 0;
int32_t i, k, idx = 0, pad = 0, mem_size = (15 * 1024 * 1024); // 9MB(frame dump) 6MB(bmp data)
sys_addr_t sys_mem = NULL;
sys_memory_container_t mc_app = (sys_memory_container_t)-1;
CellRtcDateTime t;
char path[128];
// alloc buffers
mc_app = vsh_memory_container_by_id(1);
sys_memory_allocate_from_container(mem_size, mc_app, SYS_MEMORY_PAGE_SIZE_1M, &sys_mem);
uint64_t *dump_buf = (uint64_t*)sys_mem;
uint8_t *bmp_buf = (uint8_t*)sys_mem + (9 * 1024 * 1024);
uint64_t *bg = (uint64_t*)ctx.bg;
// build file path
cellRtcGetCurrentClockLocalTime(&t);
sprintf(path, "/dev_hdd0/screenshot_%02d_%02d_%02d_%02d_%02d_%02d.bmp", t.year, t.month, t.day, t.hour, t.minute, t.second);
// create bmp file
fd = fopen(path, "wb");
// dump framebuffer (restore if needed)
for(i = 0; i < h; i++)
for(k = 0; k < w/2; k++)
{
dump_buf[k + i * w/2] = *(uint64_t*)(OFFSET(k*2, i));
if(mode == 0)
if((k*2 >= canvas_x) && (k*2 < canvas_x + CANVAS_W)
&& ( i >= canvas_y) && ( i < canvas_y + CANVAS_H))
dump_buf[k + i * w/2] = bg[(((i - canvas_y) * CANVAS_W) + ((k*2) - canvas_x)) /2];
}
// convert dump color data from ARGB to RGB
uint8_t *tmp_buf = (uint8_t*)sys_mem;
for(i = 0; i < h; i++)
{
idx = (h-1-i)*w*3;
for(k = 0; k < w; k++)
{
bmp_buf[idx ] = tmp_buf[(i*w+k)*4 +3]; // R
bmp_buf[idx +1] = tmp_buf[(i*w+k)*4 +2]; // G
bmp_buf[idx +2] = tmp_buf[(i*w+k)*4 +1]; // B
idx += 3;
}
}
// set bmp header
tmp = _ES32(w*h*3+0x36);
memcpy(bmp_header + 2 , &tmp, 4); // file size
tmp = _ES32(w);
memcpy(bmp_header + 18, &tmp, 4); // bmp width
tmp = _ES32(h);
memcpy(bmp_header + 22, &tmp, 4); // bmp height
tmp = _ES32(w*h*3);
memcpy(bmp_header + 34, &tmp, 4); // bmp data size
// write bmp header
fwrite(bmp_header, 1, sizeof(bmp_header), fd);
// write bmp data
fwrite(bmp_buf, 1, (w*h*3), fd);
// padding
int32_t rest = (w*3) % 4;
if(rest)
pad = 4 - rest;
fseek(fd, pad, SEEK_CUR);
fclose(fd);
sys_memory_free((sys_addr_t)sys_mem);
}
/***********************************************************************
* a wrapper to write config file
***********************************************************************/
void store_palette(menu_palette_ctx *data, size_t len)
{
const char *path = "/dev_hdd0/ps3_vsh_menu.cfg"; // CFG_FILE_PATH
write_bin(path, (uint8_t*)data, len);
}
/*
hexdump -C ps3_vsh_menu.cfg
(view)0000 7f 00 00 ff ff b0 b0 b0 ff 60 00 90 0a 00 00 00 |.........`......|
(view)0010 70 00 00 00 ff a0 a0 a0 ff 60 60 a0 07 00 00 00 |p........``.....|
(view)0020 7f 00 ff 00 ff ff ff ff ff 30 30 30 05 00 00 00 |.........000....|
(view)0030 33 33 00 66 ff 99 99 ff ff 60 60 d0 0c 00 00 00 |33.f.....``.....|
*/
/***********************************************************************
* default init
***********************************************************************/
void init_menu_palette(menu_palette_ctx *palette)
{
menu_palette_ctx *p = palette;
memset(p, 0, sizeof(menu_palette_ctx) * VIEWS);
p = palette; // Default view
p->max_lines = 10, // max entries, then stride
p->c[0] = 0x7F0000FF, // Background
p->c[1] = 0xFFB0B0B0, // Foreground 1 (upper)
p->c[2] = 0xFF600090; // Foreground 2 (lower)
p = palette + 1; // Dump pad data
p->max_lines = 7,
p->c[0] = 0x70000000, p->c[1] = 0xFFA0A0A0, p->c[2] = 0xFF6060A0;
p = palette + 2; // Setup Color
p->max_lines = VIEWS +1,
p->c[0] = 0x7F00FF00, p->c[1] = 0xFFFFFFFF, p->c[2] = 0xFF303030;
// more view...
p = palette + (VIEWS -1); // Browse games
p->max_lines = 12,
p->c[0] = 0x33330066, p->c[1] = 0xFF9999FF, p->c[2] = 0xFF6060D0;
}
#ifdef HAVE_STARFIELD
#include "starfield.h"
void draw_stars(void)
{
move_star((uint32_t*)ctx.canvas);
}
#endif
// some primitives...
/***********************************************************************
* draw a single pixel,
*
* int32_t x = start x coordinate into frame
* int32_t y = start y coordinate into frame
**********************************************************************
void draw_pixel(int32_t x, int32_t y)
{
if((x < CANVAS_W) && (y < CANVAS_H))
ctx.canvas[x + y * CANVAS_W] = ctx.fg_color;
}*/
/***********************************************************************
* draw a line,
*