-
Notifications
You must be signed in to change notification settings - Fork 310
/
grrlib.c
2635 lines (2284 loc) · 81.7 KB
/
grrlib.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
/*------------------------------------------------------------------------------
Copyright (c) 2010 The GRRLIB Team
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
------------------------------------------------------------------------------*/
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <ogc/conf.h>
#include <fat.h>
#include <wchar.h>
#include <ft2build.h>
#include <pngu.h>
#include <stdio.h>
#ifdef ENABLE_JPEG
#include <jpeglib.h>
#endif
#include <string.h>
#define __GRRLIB_CORE__
#include "grrlib.h"
#include "global.h"
#include "exi.h"
#define DEFAULT_FIFO_SIZE (256 * 1024) /**< GX fifo buffer size. */
//#define ENABLE_JPEG
#define ENABLE_TTF
GRRLIB_drawSettings GRRLIB_Settings;
Mtx GXmodelView2D;
static void *gp_fifo = NULL;
static bool is_setup = false; // To control entry and exit
static bool enable_output;
/**
* Initialize GRRLIB. Call this once at the beginning your code.
* @return A integer representating a code:
* - 0 : The operation completed successfully.
* - -1 : Not enough memory is available to initialize GRRLIB.
* - -2 : Failed to add the fat device driver to the devoptab.
* - -3 : Failed to initialize the font engine.
* @see GRRLIB_Exit
*/
int GRRLIB_Init (void) {
f32 yscale;
u32 xfbHeight;
Mtx44 perspective;
s8 error_code = 0;
// Ensure this function is only ever called once
if (is_setup) return 0;
VIDEO_Init();
if(CONF_GetProgressiveScan() > 0 && VIDEO_HaveComponentCable())
{
rmode = &TVNtsc480Prog;
gprintf("PROG\n");
}
else
{
switch(CONF_GetVideo())
{
case CONF_VIDEO_PAL:
if (CONF_GetEuRGB60() > 0)
{
gprintf("PAL60\n");
rmode = &TVEurgb60Hz480Int;
}
else
{
gprintf("PAL50\n");
rmode = &TVPal576IntDfScale;
}
break;
case CONF_VIDEO_MPAL:
gprintf("MPAL\n");
rmode = &TVMpal480IntDf;
break;
case CONF_VIDEO_NTSC:
default:
gprintf("NTSC\n");
rmode = &TVNtsc480Int;
break;
}
}
VIDEO_Configure(rmode);
// Get some memory to use for a "double buffered" frame buffer
if ( !(xfb[0] = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode))) ) return -1;
if ( !(xfb[1] = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode))) ) return -1;
VIDEO_ClearFrameBuffer(rmode, xfb[0], COLOR_BLACK);
VIDEO_ClearFrameBuffer(rmode, xfb[1], COLOR_BLACK);
VIDEO_SetNextFramebuffer(xfb[fb]); // Choose a frame buffer to start with
VIDEO_Flush(); // flush the frame to the TV
VIDEO_WaitVSync(); // Wait for the TV to finish updating
// If the TV image is interlaced it takes two passes to display the image
if (rmode->viTVMode & VI_NON_INTERLACE) VIDEO_WaitVSync();
// The FIFO is the buffer the CPU uses to send commands to the GPU
if ( !(gp_fifo = memalign(32, DEFAULT_FIFO_SIZE)) ) return -1;
memset(gp_fifo, 0, DEFAULT_FIFO_SIZE);
GX_Init(gp_fifo, DEFAULT_FIFO_SIZE);
// Clear the background to opaque black and clears the z-buffer
GX_SetCopyClear((GXColor){ 0, 0, 0, 0 }, GX_MAX_Z24);
if (rmode->aa) GX_SetPixelFmt(GX_PF_RGB565_Z16, GX_ZC_LINEAR); // Set 16 bit RGB565
else GX_SetPixelFmt(GX_PF_RGB8_Z24 , GX_ZC_LINEAR); // Set 24 bit Z24
// Other GX setup
yscale = GX_GetYScaleFactor(rmode->efbHeight, rmode->xfbHeight);
xfbHeight = GX_SetDispCopyYScale(yscale);
GX_SetDispCopySrc(0, 0, rmode->fbWidth, rmode->efbHeight);
GX_SetDispCopyDst(rmode->fbWidth, xfbHeight);
GX_SetCopyFilter(rmode->aa, rmode->sample_pattern, GX_TRUE, rmode->vfilter);
GX_SetFieldMode(rmode->field_rendering, ((rmode->viHeight == 2 * rmode->xfbHeight) ? GX_ENABLE : GX_DISABLE));
GX_SetDispCopyGamma(GX_GM_1_0);
if(rmode->fbWidth <= 0){ printf("GRRLIB " GRRLIB_VER_STRING); }
// Setup the vertex descriptor
GX_ClearVtxDesc(); // clear all the vertex descriptors
GX_InvVtxCache(); // Invalidate the vertex cache
GX_InvalidateTexAll(); // Invalidate all textures
// Tells the flipper to expect direct data
GX_SetVtxDesc(GX_VA_TEX0, GX_NONE);
GX_SetVtxDesc(GX_VA_POS, GX_DIRECT);
GX_SetVtxDesc(GX_VA_CLR0, GX_DIRECT);
GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_F32, 0);
GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_TEX0, GX_TEX_ST, GX_F32, 0);
// Colour 0 is 8bit RGBA format
GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGBA8, 0);
GX_SetZMode(GX_FALSE, GX_LEQUAL, GX_TRUE);
GX_SetNumChans(1); // colour is the same as vertex colour
GX_SetNumTexGens(1); // One texture exists
GX_SetTevOp(GX_TEVSTAGE0, GX_PASSCLR);
GX_SetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD0, GX_TEXMAP0, GX_COLOR0A0);
GX_SetTexCoordGen(GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, GX_IDENTITY);
guMtxIdentity(GXmodelView2D);
guMtxTransApply(GXmodelView2D, GXmodelView2D, 0.0F, 0.0F, -100.0F);
GX_LoadPosMtxImm(GXmodelView2D, GX_PNMTX0);
guOrtho(perspective, 0, rmode->efbHeight, 0, rmode->fbWidth, 0, 1000.0f);
GX_LoadProjectionMtx(perspective, GX_ORTHOGRAPHIC);
GX_SetViewport(0, 0, rmode->fbWidth, rmode->efbHeight, 0, 1);
GX_SetBlendMode(GX_BM_BLEND, GX_BL_SRCALPHA, GX_BL_INVSRCALPHA, GX_LO_CLEAR);
GX_SetAlphaUpdate(GX_TRUE);
GX_SetAlphaCompare(GX_GREATER, 0, GX_AOP_AND, GX_ALWAYS, 0);
GX_SetColorUpdate(GX_ENABLE);
GX_SetCullMode(GX_CULL_NONE);
GRRLIB_ClipReset();
// Default settings
GRRLIB_Settings.antialias = true;
GRRLIB_Settings.blend = GRRLIB_BLEND_ALPHA;
GRRLIB_Settings.lights = 0;
// Schedule cleanup for when program exits
is_setup = true;
atexit(GRRLIB_Exit);
enable_output = false;
// Initialise the filing system
//if (!fatInitDefault()) error_code = -2;
// Initialise TTF
#ifdef ENABLE_TTF
if (GRRLIB_InitTTF()) error_code = -3;
#endif
//VIDEO_SetBlack(false); // Enable video output (let do this to render subr)
return error_code;
}
/**
* Call this before exiting your application.
* Ensure this function is only ever called once
* and only if the setup function has been called.
*/
void GRRLIB_Exit (void)
{
if (!is_setup)
return;
else
is_setup = false;
// Allow write access to the full screen
GX_SetClipMode( GX_CLIP_DISABLE );
GX_SetScissor( 0, 0, rmode->fbWidth, rmode->efbHeight );
// We empty both frame buffers on our way out
// otherwise dead frames are sometimes seen when starting the next app
GRRLIB_FillScreen( 0x000000FF ); GRRLIB_Render();
GRRLIB_FillScreen( 0x000000FF ); GRRLIB_Render();
// Shut down the GX engine
GX_DrawDone();
GX_AbortFrame();
GX_Flush();
if (gp_fifo != NULL) { free(gp_fifo); gp_fifo = NULL; }
// Free up memory allocated for frame buffers & FIFOs
if (xfb[0] != NULL) { free(MEM_K1_TO_K0(xfb[0])); xfb[0] = NULL; }
if (xfb[1] != NULL) { free(MEM_K1_TO_K0(xfb[1])); xfb[1] = NULL; }
if (gp_fifo != NULL) { free(gp_fifo); gp_fifo = NULL; }
//VIDEO_SetBlack(TRUE);
VIDEO_Flush();
VIDEO_WaitVSync();
if (rmode->viTVMode & VI_NON_INTERLACE)
VIDEO_WaitVSync();
// Done with TTF
#ifdef ENABLE_TTF
GRRLIB_ExitTTF();
#endif
}
void GRRLIB_ExitLight (void)
{
if (!is_setup)
return;
else
is_setup = false;
// Allow write access to the full screen
GX_SetClipMode( GX_CLIP_DISABLE );
GX_SetScissor( 0, 0, rmode->fbWidth, rmode->efbHeight );
// Shut down the GX engine
GX_DrawDone();
GX_AbortFrame();
GX_Flush();
if (gp_fifo != NULL) { free(gp_fifo); gp_fifo = NULL; }
// Free up memory allocated for frame buffers & FIFOs
if (xfb[0] != NULL) { free(MEM_K1_TO_K0(xfb[0])); xfb[0] = NULL; }
if (xfb[1] != NULL) { free(MEM_K1_TO_K0(xfb[1])); xfb[1] = NULL; }
// Done with TTF
#ifdef ENABLE_TTF
GRRLIB_ExitTTF();
#endif
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// GRRLIB_ttf
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include FT_FREETYPE_H
static FT_Library ftLibrary; /**< A handle to a FreeType library instance. */
// Static function prototypes
static void DrawBitmap(FT_Bitmap *bitmap, int offset, int top, const u8 cR, const u8 cG, const u8 cB);
/**
* Initialize FreeType library.
* @return int 0=OK; -1=Failed
*/
int GRRLIB_InitTTF () {
if (FT_Init_FreeType(&ftLibrary)) {
return -1;
}
return 0;
}
/**
* Call this when your done with FreeType.
*/
void GRRLIB_ExitTTF (void) {
FT_Done_FreeType(ftLibrary);
}
/**
* Load a TTF from a buffer.
* @param file_base Buffer with TTF data. You must not deallocate the memory before calling GRRLIB_FreeTTF.
* @param file_size Size of the TTF buffer.
* @return A handle to a given TTF font object.
* @see GRRLIB_FreeTTF
*/
GRRLIB_ttfFont* GRRLIB_LoadTTF (const u8* file_base, s32 file_size) {
FT_Face Face;
GRRLIB_ttfFont* myFont = (GRRLIB_ttfFont*)malloc(sizeof(GRRLIB_ttfFont));
FT_New_Memory_Face(ftLibrary, file_base, file_size, 0, &Face);
myFont->kerning = FT_HAS_KERNING(Face);
/*
if (FT_Set_Pixel_Sizes(Face, 0, fontSize)) {
FT_Set_Pixel_Sizes(Face, 0, 12);
}
*/
myFont->face = Face;
return myFont;
}
/**
* Free memory allocated by TTF fonts.
* @param myFont A TTF.
*/
void GRRLIB_FreeTTF (GRRLIB_ttfFont *myFont) {
if(myFont) {
FT_Done_Face(myFont->face);
free(myFont);
myFont = NULL;
}
}
/**
* Print function for TTF font.
* @param x Specifies the x-coordinate of the upper-left corner of the text.
* @param y Specifies the y-coordinate of the upper-left corner of the text.
* @param myFont A TTF.
* @param string Text to draw.
* @param fontSize Size of the font.
* @param color Text color in RGBA format.
*/
void GRRLIB_PrintfTTF(int x, int y, GRRLIB_ttfFont *myFont, const char *string, unsigned int fontSize, const u32 color) {
if(myFont == NULL || string == NULL)
return;
size_t length = strlen(string) + 1;
wchar_t *utf32 = (wchar_t*)malloc(length * sizeof(wchar_t));
if(utf32) {
length = mbstowcs(utf32, string, length);
if(length > 0) {
utf32[length] = L'\0';
GRRLIB_PrintfTTFW(x, y, myFont, utf32, fontSize, color);
}
free(utf32);
}
}
/**
* Print function for TTF font.
* @author wplaat and DrTwox
* @param x Specifies the x-coordinate of the upper-left corner of the text.
* @param y Specifies the y-coordinate of the upper-left corner of the text.
* @param myFont A TTF.
* @param utf32 Text to draw.
* @param fontSize Size of the font.
* @param color Text color in RGBA format.
*/
void GRRLIB_PrintfTTFW(int x, int y, GRRLIB_ttfFont *myFont, const wchar_t *utf32, unsigned int fontSize, const u32 color) {
if(myFont == NULL || utf32 == NULL)
return;
FT_Face Face = (FT_Face)myFont->face;
int penX = 0;
int penY = fontSize;
FT_GlyphSlot slot = Face->glyph;
FT_UInt glyphIndex = 0;
FT_UInt previousGlyph = 0;
u8 cR = R(color), cG = G(color), cB = B(color);
if (FT_Set_Pixel_Sizes(Face, 0, fontSize)) {
FT_Set_Pixel_Sizes(Face, 0, 12);
}
/* Loop over each character, until the
* end of the string is reached, or until the pixel width is too wide */
while(*utf32) {
glyphIndex = FT_Get_Char_Index(myFont->face, *utf32++);
if (myFont->kerning && previousGlyph && glyphIndex) {
FT_Vector delta;
FT_Get_Kerning(myFont->face, previousGlyph, glyphIndex, FT_KERNING_DEFAULT, &delta);
penX += delta.x >> 6;
}
if (FT_Load_Glyph(myFont->face, glyphIndex, FT_LOAD_RENDER)) {
continue;
}
DrawBitmap(&slot->bitmap,
penX + slot->bitmap_left + x,
penY - slot->bitmap_top + y,
cR, cG, cB);
penX += slot->advance.x >> 6;
previousGlyph = glyphIndex;
}
}
/**
* Draw a character on the screen.
* @param bitmap Bitmap to draw.
* @param offset x-coordinate offset.
* @param top y-coordinate.
* @param cR Red component of the colour.
* @param cG Green component of the colour.
* @param cB Blue component of the colour.
*/
static void DrawBitmap(FT_Bitmap *bitmap, int offset, int top, const u8 cR, const u8 cG, const u8 cB) {
FT_Int i, j, p, q;
FT_Int x_max = offset + bitmap->width;
FT_Int y_max = top + bitmap->rows;
for ( i = offset, p = 0; i < x_max; i++, p++ ) {
for ( j = top, q = 0; j < y_max; j++, q++ ) {
GX_Begin(GX_POINTS, GX_VTXFMT0, 1);
GX_Position3f32(i, j, 0);
GX_Color4u8(cR, cG, cB,
bitmap->buffer[ q * bitmap->width + p ]);
GX_End();
}
}
}
/**
* Get the width of a text in pixel.
* @param myFont A TTF.
* @param string The text to check.
* @param fontSize The size of the font.
* @return The width of a text in pixel.
*/
unsigned int GRRLIB_WidthTTF(GRRLIB_ttfFont *myFont, const char *string, unsigned int fontSize) {
if(myFont == NULL || string == NULL) {
return 0;
}
unsigned int penX = 0;
int length = strlen(string) + 1;
wchar_t *utf32 = (wchar_t*)malloc(length * sizeof(wchar_t)+1);
if (!utf32) return 0;
length = mbstowcs(utf32, string, length);
if (length > 0)
{
utf32[length] = L'\0';
penX = GRRLIB_WidthTTFW(myFont, utf32, fontSize);
}
free(utf32);
return penX;
/* unsigned int penX;
size_t length = strlen(string) + 1;
wchar_t *utf32 = (wchar_t*)malloc(length * sizeof(wchar_t));
length = mbstowcs(utf32, string, length);
utf32[length] = L'\0';
penX = GRRLIB_WidthTTFW(myFont, utf32, fontSize);
free(utf32);
return penX;*/
}
/**
* Get the width of a text in pixel.
* @param myFont A TTF.
* @param utf32 The text to check.
* @param fontSize The size of the font.
* @return The width of a text in pixel.
*/
unsigned int GRRLIB_WidthTTFW(GRRLIB_ttfFont *myFont, const wchar_t *utf32, unsigned int fontSize) {
if(myFont == NULL || utf32 == NULL) {
return 0;
}
FT_Face Face = (FT_Face)myFont->face;
unsigned int penX = 0;
FT_UInt glyphIndex;
FT_UInt previousGlyph = 0;
if(FT_Set_Pixel_Sizes(myFont->face, 0, fontSize)) {
FT_Set_Pixel_Sizes(myFont->face, 0, 12);
}
while(*utf32) {
glyphIndex = FT_Get_Char_Index(myFont->face, *utf32++);
if(myFont->kerning && previousGlyph && glyphIndex) {
FT_Vector delta;
FT_Get_Kerning(Face, previousGlyph, glyphIndex, FT_KERNING_DEFAULT, &delta);
penX += delta.x >> 6;
}
if(FT_Load_Glyph(Face, glyphIndex, FT_LOAD_RENDER)) {
continue;
}
penX += Face->glyph->advance.x >> 6;
previousGlyph = glyphIndex;
}
return penX;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// GRRLIB_texEdit
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* This structure contains information about the type, size, and layout of a file that containing a device-independent bitmap (DIB).
*/
typedef struct tagBITMAPFILEHEADER {
u16 bfType; /**< Specifies the file type. It must be set to the signature word BM (0x4D42) to indicate bitmap. */
u32 bfSize; /**< Specifies the size, in bytes, of the bitmap file. */
u16 bfReserved1; /**< Reserved; set to zero. */
u16 bfReserved2; /**< Reserved; set to zero. */
u32 bfOffBits; /**< Specifies the offset, in bytes, from the BITMAPFILEHEADER structure to the bitmap bits. */
} BITMAPFILEHEADER;
/**
* This structure contains information about the dimensions and color format of a device-independent bitmap (DIB).
*/
typedef struct tagBITMAPINFOHEADER {
u32 biSize; /**< Specifies the size of the structure, in bytes. */
u32 biWidth; /**< Specifies the width of the bitmap, in pixels. */
u32 biHeight; /**< Specifies the height of the bitmap, in pixels. */
u16 biPlanes; /**< Specifies the number of planes for the target device. */
u16 biBitCount; /**< Specifies the number of bits per pixel. */
u32 biCompression; /**< Specifies the type of compression for a compressed bottom-up bitmap.*/
u32 biSizeImage; /**< Specifies the size, in bytes, of the image. */
u32 biXPelsPerMeter; /**< Specifies the horizontal resolution, in pixels per meter, of the target device for the bitmap. */
u32 biYPelsPerMeter; /**< Specifies the vertical resolution, in pixels per meter, of the target device for the bitmap. */
u32 biClrUsed; /**< Specifies the number of color indexes in the color table that are actually used by the bitmap. */
u32 biClrImportant; /**< Specifies the number of color indexes required for displaying the bitmap. */
} BITMAPINFOHEADER;
/**
* The RGBQUAD structure describes a color consisting of relative intensities of
* red, green, and blue. The bmiColors member of the BITMAPINFO structure
* consists of an array of RGBQUAD structures.
*/
typedef struct tagRGBQUAD {
u8 rgbBlue; /**< Specifies the intensity of blue in the color. */
u8 rgbGreen; /**< Specifies the intensity of green in the color. */
u8 rgbRed; /**< Specifies the intensity of red in the color. */
u8 rgbReserved; /**< Not used; must be set to zero. */
} RGBQUAD;
/**
* Convert a raw BMP (RGB, no alpha) to 4x4RGBA.
* @author DragonMinded
* @param src Source.
* @param dst Destination.
* @param width Width.
* @param height Height.
*/
#ifdef ENABLE_JPEG
static
void RawTo4x4RGBA (const u8 *src, void *dst,
const uint width, const uint height) {
uint block;
uint i;
u8 c;
u8 argb;
u8 *p = (u8*)dst;
for (block = 0; block < height; block += 4) {
for (i = 0; i < width; i += 4) {
// Alpha and Red
for (c = 0; c < 4; ++c) {
for (argb = 0; argb < 4; ++argb) {
// Alpha pixels
*p++ = 255;
// Red pixels
*p++ = src[((i + argb) + ((block + c) * width)) * 3];
}
}
// Green and Blue
for (c = 0; c < 4; ++c) {
for (argb = 0; argb < 4; ++argb) {
// Green pixels
*p++ = src[(((i + argb) + ((block + c) * width)) * 3) + 1];
// Blue pixels
*p++ = src[(((i + argb) + ((block + c) * width)) * 3) + 2];
}
}
}
}
}
#endif
/**
* Load a texture from a buffer.
* @param my_img The JPEG, PNG or Bitmap buffer to load.
* @return A GRRLIB_texImg structure filled with image information.
*/
GRRLIB_texImg* GRRLIB_LoadTexture (const u8 *my_img) {
if (my_img[0]==0xFF && my_img[1]==0xD8 && my_img[2]==0xFF)
return (GRRLIB_LoadTextureJPG(my_img));
else if (my_img[0]=='B' && my_img[1]=='M')
return (GRRLIB_LoadTextureBMP(my_img));
else
return (GRRLIB_LoadTexturePNG(my_img));
}
/**
* Load a texture from a buffer.
* @param my_png the PNG buffer to load.
* @return A GRRLIB_texImg structure filled with image information.
* If image size is not correct, the texture will be completely transparent.
*/
GRRLIB_texImg* GRRLIB_LoadTexturePNG (const u8 *my_png) {
int width = 0, height = 0;
PNGUPROP imgProp;
IMGCTX ctx;
GRRLIB_texImg *my_texture = calloc(1, sizeof(GRRLIB_texImg));
if(my_texture != NULL) {
ctx = PNGU_SelectImageFromBuffer(my_png);
PNGU_GetImageProperties(ctx, &imgProp);
my_texture->data = PNGU_DecodeTo4x4RGBA8(ctx, imgProp.imgWidth, imgProp.imgHeight, &width, &height, NULL);
if(my_texture->data != NULL) {
my_texture->w = width;
my_texture->h = height;
GRRLIB_SetHandle( my_texture, 0, 0 );
if(imgProp.imgWidth != width || imgProp.imgHeight != height) {
// PNGU has resized the texture
memset(my_texture->data, 0, (my_texture->h * my_texture->w) << 2);
}
GRRLIB_FlushTex( my_texture );
}
PNGU_ReleaseImageContext(ctx);
}
return my_texture;
}
/**
* Create an array of palette.
* @param my_bmp Bitmap buffer to parse.
* @param Size The number of palette to add.
* @return An array of palette. Memory must be deleted.
*/
static RGBQUAD* GRRLIB_CreatePalette (const u8 *my_bmp, u32 Size) {
u32 n, i = 0;
RGBQUAD *Palette = calloc(Size, sizeof(RGBQUAD));
for(n=0; n<Size; n++) {
Palette[n].rgbBlue = my_bmp[i];
Palette[n].rgbGreen = my_bmp[i+1];
Palette[n].rgbRed = my_bmp[i+2];
Palette[n].rgbReserved = 0;
i += 4;
}
return Palette;
}
/**
* Load a texture from a buffer.
* It only works for the MS-Windows standard format uncompressed (1-bit, 4-bit, 8-bit, 24-bit and 32-bit).
* @param my_bmp the Bitmap buffer to load.
* @return A GRRLIB_texImg structure filled with image information.
*/
GRRLIB_texImg* GRRLIB_LoadTextureBMP (const u8 *my_bmp) {
BITMAPFILEHEADER MyBitmapFileHeader;
BITMAPINFOHEADER MyBitmapHeader;
RGBQUAD *Palette;
u16 pal_ref;
u32 BufferSize;
s32 y, x, i;
GRRLIB_texImg *my_texture = calloc(1, sizeof(GRRLIB_texImg));
if(my_texture != NULL) {
// Fill file header structure
MyBitmapFileHeader.bfType = (my_bmp[0] | my_bmp[1]<<8);
MyBitmapFileHeader.bfSize = (my_bmp[2] | my_bmp[3]<<8 | my_bmp[4]<<16 | my_bmp[5]<<24);
MyBitmapFileHeader.bfReserved1 = (my_bmp[6] | my_bmp[7]<<8);
MyBitmapFileHeader.bfReserved2 = (my_bmp[8] | my_bmp[9]<<8);
MyBitmapFileHeader.bfOffBits = (my_bmp[10] | my_bmp[11]<<8 | my_bmp[12]<<16 | my_bmp[13]<<24);
// Fill the bitmap structure
MyBitmapHeader.biSize = (my_bmp[14] | my_bmp[15]<<8 | my_bmp[16]<<16 | my_bmp[17]<<24);
MyBitmapHeader.biWidth = (my_bmp[18] | my_bmp[19]<<8 | my_bmp[20]<<16 | my_bmp[21]<<24);
MyBitmapHeader.biHeight = (my_bmp[22] | my_bmp[23]<<8 | my_bmp[24]<<16 | my_bmp[25]<<24);
MyBitmapHeader.biPlanes = (my_bmp[26] | my_bmp[27]<<8);
MyBitmapHeader.biBitCount = (my_bmp[28] | my_bmp[29]<<8);
MyBitmapHeader.biCompression = (my_bmp[30] | my_bmp[31]<<8 | my_bmp[32]<<16 | my_bmp[33]<<24);
MyBitmapHeader.biSizeImage = (my_bmp[34] | my_bmp[35]<<8 | my_bmp[36]<<16 | my_bmp[37]<<24);
MyBitmapHeader.biXPelsPerMeter = (my_bmp[38] | my_bmp[39]<<8 | my_bmp[40]<<16 | my_bmp[41]<<24);
MyBitmapHeader.biYPelsPerMeter = (my_bmp[42] | my_bmp[43]<<8 | my_bmp[44]<<16 | my_bmp[45]<<24);
MyBitmapHeader.biClrUsed = (my_bmp[46] | my_bmp[47]<<8 | my_bmp[48]<<16 | my_bmp[49]<<24);
MyBitmapHeader.biClrImportant = (my_bmp[50] | my_bmp[51]<<8 | my_bmp[52]<<16 | my_bmp[53]<<24);
my_texture->data = memalign(32, MyBitmapHeader.biWidth * MyBitmapHeader.biHeight * 4);
if(my_texture->data != NULL && MyBitmapFileHeader.bfType == 0x4D42) {
my_texture->w = MyBitmapHeader.biWidth;
my_texture->h = MyBitmapHeader.biHeight;
switch(MyBitmapHeader.biBitCount) {
case 32: // RGBA images
i = 54;
for(y=MyBitmapHeader.biHeight-1; y>=0; y--) {
for(x=0; x<MyBitmapHeader.biWidth; x++) {
GRRLIB_SetPixelTotexImg(x, y, my_texture,
RGBA(my_bmp[i+2], my_bmp[i+1], my_bmp[i], my_bmp[i+3]));
i += 4;
}
}
break;
case 24: // truecolor images
BufferSize = (MyBitmapHeader.biWidth % 4);
i = 54;
for(y=MyBitmapHeader.biHeight-1; y>=0; y--) {
for(x=0; x<MyBitmapHeader.biWidth; x++) {
GRRLIB_SetPixelTotexImg(x, y, my_texture,
RGBA(my_bmp[i+2], my_bmp[i+1], my_bmp[i], 0xFF));
i += 3;
}
i += BufferSize; // Padding
}
break;
case 8: // 256 color images
BufferSize = (int) MyBitmapHeader.biWidth;
while(BufferSize % 4) {
BufferSize++;
}
BufferSize -= MyBitmapHeader.biWidth;
Palette = GRRLIB_CreatePalette(&my_bmp[54], 256);
i = 1078; // 54 + (MyBitmapHeader.biBitCount * 4)
for(y=MyBitmapHeader.biHeight-1; y>=0; y--) {
for(x=0; x<MyBitmapHeader.biWidth; x++) {
GRRLIB_SetPixelTotexImg(x, y, my_texture,
RGBA(Palette[my_bmp[i]].rgbRed,
Palette[my_bmp[i]].rgbGreen,
Palette[my_bmp[i]].rgbBlue,
0xFF));
i++;
}
i += BufferSize; // Padding
}
free(Palette);
break;
case 4: // 16 color images
BufferSize = (int)((MyBitmapHeader.biWidth*4) / 8.0);
while(8*BufferSize < MyBitmapHeader.biWidth*4) {
BufferSize++;
}
while(BufferSize % 4) {
BufferSize++;
}
Palette = GRRLIB_CreatePalette(&my_bmp[54], 16);
i = 118; // 54 + (MyBitmapHeader.biBitCount * 4)
for(y=MyBitmapHeader.biHeight-1; y>=0; y--) {
for(x=0; x<MyBitmapHeader.biWidth; x++) {
pal_ref = (my_bmp[i + (x / 2)] >> ((x % 2) ? 0 : 4)) & 0x0F;
GRRLIB_SetPixelTotexImg(x, y, my_texture,
RGBA(Palette[pal_ref].rgbRed,
Palette[pal_ref].rgbGreen,
Palette[pal_ref].rgbBlue,
0xFF));
}
i += BufferSize; // Padding
}
free(Palette);
break;
case 1: // black & white images
BufferSize = (int)(MyBitmapHeader.biWidth / 8.0);
while(8*BufferSize < MyBitmapHeader.biWidth) {
BufferSize++;
}
while(BufferSize % 4) {
BufferSize++;
}
Palette = GRRLIB_CreatePalette(&my_bmp[54], 2);
i = 62; // 54 + (MyBitmapHeader.biBitCount * 4)
for(y=MyBitmapHeader.biHeight-1; y>=0; y--) {
for(x=0; x<MyBitmapHeader.biWidth; x++) {
pal_ref = (my_bmp[i + (x / 8)] >> (-x%8+7)) & 0x01;
GRRLIB_SetPixelTotexImg(x, y, my_texture,
RGBA(Palette[pal_ref].rgbRed,
Palette[pal_ref].rgbGreen,
Palette[pal_ref].rgbBlue,
0xFF));
}
i += BufferSize; // Padding
}
free(Palette);
break;
default:
GRRLIB_ClearTex(my_texture);
}
GRRLIB_SetHandle( my_texture, 0, 0 );
GRRLIB_FlushTex( my_texture );
}
}
return my_texture;
}
/**
* Load a texture from a buffer.
* Take care to have the JPG finish with 0xFF 0xD9!
* @param my_jpg The JPEG buffer to load.
* @return A GRRLIB_texImg structure filled with image information.
*/
GRRLIB_texImg* GRRLIB_LoadTextureJPG (const u8 *my_jpg) {
int n = 0;
if ((my_jpg[0]==0xFF) && (my_jpg[1]==0xD8) && (my_jpg[2]==0xFF)) {
while(true) {
if ((my_jpg[n]==0xFF) && (my_jpg[n+1]==0xD9))
break;
n++;
}
n+=2;
}
#ifdef ENABLE_JPEG
return GRRLIB_LoadTextureJPGEx(my_jpg, n);
#else
return NULL;
#endif
}
/**
* Load a texture from a buffer.
* @author DrTwox
* @param my_jpg The JPEG buffer to load.
* @param my_size Size of the JPEG buffer to load.
* @return A GRRLIB_texImg structure filled with image information.
*/
#ifdef ENABLE_JPEG
GRRLIB_texImg* GRRLIB_LoadTextureJPGEx (const u8 *my_jpg, const int my_size) {
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
GRRLIB_texImg *my_texture = calloc(1, sizeof(GRRLIB_texImg));
unsigned int i;
if(my_texture == NULL)
return NULL;
jpeg_create_decompress(&cinfo);
cinfo.err = jpeg_std_error(&jerr);
cinfo.progress = NULL;
jpeg_mem_src(&cinfo, (unsigned char *)my_jpg, my_size);
jpeg_read_header(&cinfo, TRUE);
if(cinfo.jpeg_color_space == JCS_GRAYSCALE) {
cinfo.out_color_space = JCS_RGB;
}
jpeg_start_decompress(&cinfo);
unsigned char *tempBuffer = malloc(cinfo.output_width * cinfo.output_height * cinfo.output_components);
JSAMPROW row_pointer[1];
row_pointer[0] = malloc(cinfo.output_width * cinfo.output_components);
size_t location = 0;
while (cinfo.output_scanline < cinfo.output_height) {
jpeg_read_scanlines(&cinfo, row_pointer, 1);
for (i = 0; i < cinfo.image_width * cinfo.output_components; i++) {
/* Put the decoded scanline into the tempBuffer */
tempBuffer[ location++ ] = row_pointer[0][i];
}
}
/* Create a buffer to hold the final texture */
my_texture->data = memalign(32, cinfo.output_width * cinfo.output_height * 4);
RawTo4x4RGBA(tempBuffer, my_texture->data, cinfo.output_width, cinfo.output_height);
/* Done - Do cleanup and release allocated memory */
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
free(row_pointer[0]);
free(tempBuffer);
my_texture->w = cinfo.output_width;
my_texture->h = cinfo.output_height;
GRRLIB_SetHandle( my_texture, 0, 0 );
GRRLIB_FlushTex( my_texture );
return my_texture;
}
#endif
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// GRRLIB_snapshot
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Make a snapshot of the screen in a texture WITHOUT ALPHA LAYER.
* @param posx top left corner of the grabbed part.
* @param posy top left corner of the grabbed part.
* @param tex A pointer to a texture representing the screen or NULL if an error occurs.
* @param clear When this flag is set to true, the screen is cleared after copy.
*/
void GRRLIB_Screen2Texture (int posx, int posy, GRRLIB_texImg *tex, bool clear) {
if(tex->data != NULL) {
GX_SetTexCopySrc(posx, posy, tex->w, tex->h);
GX_SetTexCopyDst(tex->w, tex->h, GX_TF_RGBA8, GX_FALSE);
GX_CopyTex(tex->data, GX_FALSE);
GX_PixModeSync();
GRRLIB_FlushTex(tex);
if(clear)
GX_CopyDisp(xfb[!fb], GX_TRUE);
}
}
/**
* Start GX compositing process.
* @see GRRLIB_CompoEnd
*/
void GRRLIB_CompoStart (void) {
GX_SetPixelFmt(GX_PF_RGBA6_Z24, GX_ZC_LINEAR);
GX_PokeAlphaRead(GX_READ_NONE);
}
/**
* End GX compositing process (Make a snapshot of the screen in a texture WITH ALPHA LAYER).
* EFB is cleared after this function.
* @see GRRLIB_CompoStart
* @param posx top left corner of the grabbed part.
* @param posy top left corner of the grabbed part.
* @param tex A pointer to a texture representing the screen or NULL if an error occurs.
*/
void GRRLIB_CompoEnd(int posx, int posy, GRRLIB_texImg *tex) {
GRRLIB_Screen2Texture(posx, posy, tex, GX_TRUE);
if (rmode->aa) GX_SetPixelFmt(GX_PF_RGB565_Z16, GX_ZC_LINEAR);
else GX_SetPixelFmt(GX_PF_RGB8_Z24 , GX_ZC_LINEAR);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// GRRLIB_render
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
extern GRRLIB_drawSettings GRRLIB_Settings;
extern Mtx GXmodelView2D;
static guVector axis = (guVector){0, 0, 1};
/**
* Draw a texture.
* @param xpos Specifies the x-coordinate of the upper-left corner.
* @param ypos Specifies the y-coordinate of the upper-left corner.
* @param tex The texture to draw.
* @param degrees Angle of rotation.
* @param scaleX Specifies the x-coordinate scale. -1 could be used for flipping the texture horizontally.
* @param scaleY Specifies the y-coordinate scale. -1 could be used for flipping the texture vertically.
* @param color Color in RGBA format.
*/
void GRRLIB_DrawImg (const f32 xpos, const f32 ypos, const GRRLIB_texImg *tex, const f32 degrees, const f32 scaleX, const f32 scaleY, const u32 color) {
GXTexObj texObj;
u16 width, height;
Mtx m, m1, m2, mv;
if (tex == NULL || tex->data == NULL) return;
GX_InitTexObj(&texObj, tex->data, tex->w, tex->h,
GX_TF_RGBA8, GX_CLAMP, GX_CLAMP, GX_FALSE);
if (GRRLIB_Settings.antialias == false) {
GX_InitTexObjLOD(&texObj, GX_NEAR, GX_NEAR,
0.0f, 0.0f, 0.0f, 0, 0, GX_ANISO_1);
GX_SetCopyFilter(GX_FALSE, rmode->sample_pattern, GX_FALSE, rmode->vfilter);
}
else {
GX_SetCopyFilter(rmode->aa, rmode->sample_pattern, GX_TRUE, rmode->vfilter);
}
GX_LoadTexObj(&texObj, GX_TEXMAP0);
GX_SetTevOp (GX_TEVSTAGE0, GX_MODULATE);
GX_SetVtxDesc(GX_VA_TEX0, GX_DIRECT);
guMtxIdentity (m1);
guMtxScaleApply(m1, m1, scaleX, scaleY, 1.0);
guMtxRotAxisDeg(m2, &axis, degrees);
guMtxConcat (m2, m1, m);
width = tex->w * 0.5;
height = tex->h * 0.5;
guMtxTransApply(m, m,
xpos +width +tex->handlex
-tex->offsetx +( scaleX *(-tex->handley *sin(-DegToRad(degrees))
-tex->handlex *cos(-DegToRad(degrees))) ),
ypos +height +tex->handley
-tex->offsety +( scaleY *(-tex->handley *cos(-DegToRad(degrees))
+tex->handlex *sin(-DegToRad(degrees))) ),
0);
guMtxConcat(GXmodelView2D, m, mv);
GX_LoadPosMtxImm(mv, GX_PNMTX0);
GX_Begin(GX_QUADS, GX_VTXFMT0, 4);
GX_Position3f32(-width, -height, 0);
GX_Color1u32 (color);
GX_TexCoord2f32(0, 0);
GX_Position3f32(width, -height, 0);
GX_Color1u32 (color);