1 /*---------------------------------------------------------------------------*
2 Project: Dolphin OS Overview - OSInitFont(), OSGetFontTexture()
3 File: fontdemo1.c
4
5 Copyright 2002 Nintendo. All rights reserved.
6
7 These coded instructions, statements, and computer programs contain
8 proprietary information of Nintendo of America Inc. and/or Nintendo
9 Company Ltd., and are protected by Federal copyright law. They may
10 not be disclosed to third parties or copied or duplicated in any form,
11 in whole or in part, without the prior written consent of Nintendo.
12
13 $Log: fontdemo3.c,v $
14 Revision 1.1 01/13/2006 11:24:13 hiratsu
15 Initial check in.
16
17
18 2 3/10/04 17:57 Shiki
19 Added comment.
20
21 1 3/10/04 17:54:00 Shiki
22 Added support for UTF code.
23
24 3 1/11/02 9:05 Shiki
25 Minor change
26
27 2 1/09/02 16:14:00 Shiki
28 Minor fix.
29
30 1 1/09/02 15:26 Shiki
31 Initial check-in.
32 $NoKeywords: $
33 *---------------------------------------------------------------------------*/
34
35 #include <stdlib.h>
36 #include <string.h>
37 #include <demo.h>
38
39 const GXColor Bkcolor = { 0, 0, 192, 0 };
40
41 static OSFontHeader* FontData;
42
DrawCell(int x,int y,int xChar,int yChar)43 static void DrawCell(int x, int y, int xChar, int yChar)
44 {
45 s16 posLeft = (s16) x;
46 s16 posRight = (s16) (posLeft + FontData->cellWidth);
47 s16 posTop = (s16) y;
48 s16 posBottom = (s16) (posTop + FontData->cellHeight);
49
50 s16 texLeft = (s16) xChar;
51 s16 texRight = (s16) (xChar + FontData->cellWidth);
52 s16 texTop = (s16) yChar;
53 s16 texBottom = (s16) (yChar + FontData->cellHeight);
54
55 GXBegin(GX_QUADS, GX_VTXFMT0, 4);
56 GXPosition3s16(posLeft , posTop , 0);
57 GXTexCoord2s16(texLeft , texTop);
58
59 GXPosition3s16(posRight, posTop , 0);
60 GXTexCoord2s16(texRight, texTop);
61
62 GXPosition3s16(posRight, posBottom, 0);
63 GXTexCoord2s16(texRight, texBottom);
64
65 GXPosition3s16(posLeft , posBottom, 0);
66 GXTexCoord2s16(texLeft , texBottom);
67 GXEnd();
68 }
69
LoadSheet(void * image)70 static void LoadSheet(void* image)
71 {
72 Mtx mtx;
73 GXTexObj texObj;
74
75 // Set up and load texture object
76 GXInitTexObj(&texObj, // obj
77 image, // image_ptr
78 FontData->sheetWidth, // weight
79 FontData->sheetHeight, // height
80 (GXTexFmt) FontData->sheetFormat, // format
81 GX_CLAMP, // wrap_s (don't care)
82 GX_CLAMP, // wrap_t (don't care)
83 GX_FALSE); // mipmap (don't care)
84
85 GXInitTexObjLOD(&texObj,
86 GX_LINEAR, // min_filt
87 GX_LINEAR, // max_filt
88 0.0f, // min_lod (don't care)
89 0.0f, // max_lod (don't care)
90 0.0f, // lod_bias (don't care)
91 GX_DISABLE, // bias_clamp (don't care)
92 GX_FALSE, // do_edge_lod (don't care)
93 GX_ANISO_1); // max_aniso (don't care)
94
95 GXLoadTexObj(&texObj, GX_TEXMAP0);
96 MTXScale(mtx, 1.0f / FontData->sheetWidth,
97 1.0f / FontData->sheetHeight,
98 1.0f);
99 GXLoadTexMtxImm(mtx, GX_TEXMTX0, GX_MTX2x4);
100 GXSetNumTexGens(1);
101 GXSetTexCoordGen(GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, GX_TEXMTX0);
102 }
103
DrawChar(int x,int y,u16 code)104 static s32 DrawChar(int x, int y, u16 code)
105 {
106 char* p;
107 char buffer[5];
108 s32 width;
109 void* image;
110 s32 xChar;
111 s32 yChar;
112
113 if (code == 0)
114 {
115 return 0;
116 }
117
118 // UTF-8
119 p = OSUTF32to8(code, buffer);
120 if (p == NULL)
121 {
122 return 0;
123 }
124 *p = '\0';
125
126 OSGetFontTexture(buffer, &image, &xChar, &yChar, &width);
127 LoadSheet(image);
128 DrawCell(x, y, xChar, yChar);
129
130 return width;
131 }
132
DrawString(int x,int y,char * string)133 static s32 DrawString(int x, int y, char* string)
134 {
135 s32 cx = 0;
136
137 while (*string)
138 {
139 void* image;
140 s32 xChar;
141 s32 yChar;
142 s32 width;
143
144 string = OSGetFontTexture(string, &image, &xChar, &yChar, &width);
145 LoadSheet(image);
146 DrawCell(x + cx, y, xChar, yChar);
147 cx += width;
148 }
149
150 return cx;
151 }
152
PrintIntro(void)153 static void PrintIntro(void)
154 {
155 OSReport("\n\n");
156 OSReport("*******************************************************\n");
157 OSReport("fontdemo: IPL font list\n");
158 OSReport("*******************************************************\n");
159 OSReport("to quit hit the start button\n");
160
161 if (OSGetFontEncode() == OS_FONT_ENCODE_SJIS)
162 {
163 OSReport("\n");
164 OSReport(" Up button: page up\n");
165 OSReport(" Down button: page down\n");
166 }
167
168 OSReport("*******************************************************\n\n");
169 }
170
main(void)171 void main(void)
172 {
173 Mtx44 projection;
174 Mtx modelview;
175 GXRenderModeObj* rmp;
176 u16 top = 0x00;
177 u16 code;
178 u32 count;
179 s32 repeat;
180
181 DEMOInit(NULL);
182
183 // Use UTF-8 code
184 OSSetFontEncode(OS_FONT_ENCODE_UTF8);
185 PrintIntro();
186 FontData = OSAlloc(OS_FONT_SIZE_UTF);
187 OSInitFont(FontData);
188
189 // Clear EFB
190 GXSetCopyClear(Bkcolor, 0x00ffffff);
191 GXCopyDisp(DEMOGetCurrentBuffer(), GX_TRUE);
192
193 // Set matrices up to screen coordinates system
194 rmp = DEMOGetRenderModeObj();
195 MTXOrtho(projection, 0.0f, (f32) rmp->efbHeight, 0.0f, (f32) rmp->fbWidth, 0.0f, -100.0f);
196 GXSetProjection(projection, GX_ORTHOGRAPHIC);
197 MTXIdentity(modelview);
198 GXLoadPosMtxImm(modelview, GX_PNMTX0);
199 GXSetCurrentMtx(GX_PNMTX0);
200
201 // Set pixel processing mode
202 GXSetZMode(GX_ENABLE, GX_ALWAYS, GX_ENABLE);
203
204 // Set TEV parameters to "REPLACE COLOR"
205 GXSetNumChans(0);
206 GXSetNumTevStages(1);
207 GXSetTevOp(GX_TEVSTAGE0, GX_REPLACE);
208 GXSetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD0, GX_TEXMAP0, GX_COLOR_NULL);
209
210 // Translucent mode
211 GXSetBlendMode(GX_BM_BLEND, GX_BL_ONE, GX_BL_ONE, GX_LO_CLEAR);
212
213 // Set up vertex descriptors
214 GXClearVtxDesc();
215 GXSetVtxDesc(GX_VA_POS, GX_DIRECT);
216 GXSetVtxDesc(GX_VA_TEX0, GX_DIRECT);
217 GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_S16, 0);
218 GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_TEX0, GX_TEX_ST, GX_S16, 0);
219
220 while (!(DEMOPadGetButton(0) & PAD_BUTTON_START))
221 {
222 int x;
223 int y;
224
225 DEMOPadRead();
226 repeat = (s32) (VIGetRetraceCount() - count);
227 repeat = (repeat < 20) ? 0 : (repeat % 3 == 0);
228 if (DEMOPadGetButtonDown(0) & PAD_BUTTON_DOWN)
229 {
230 count = VIGetRetraceCount();
231 repeat = 1;
232 }
233 if ((DEMOPadGetButton(0) & PAD_BUTTON_DOWN) && repeat)
234 {
235 if (top < 0x10000 - 256)
236 {
237 top += 256;
238 }
239 if (0xa000 <= top && top < 0xff00)
240 {
241 top = 0xff00;
242 }
243 }
244 if (DEMOPadGetButtonDown(0) & PAD_BUTTON_UP)
245 {
246 count = VIGetRetraceCount();
247 repeat = 1;
248 }
249 if ((DEMOPadGetButton(0) & PAD_BUTTON_UP) && repeat)
250 {
251 if (0 < top)
252 {
253 top -= 256;
254 }
255 if (0xa000 <= top && top < 0xff00)
256 {
257 top = 0x9f00;
258 }
259 }
260 if (OSGetFontEncode() == OS_FONT_ENCODE_ANSI)
261 {
262 top = 0;
263 }
264
265 DEMOBeforeRender();
266
267 code = top;
268 for (y = 25;
269 y < 25 + FontData->cellHeight * 16;
270 y += FontData->cellHeight)
271 {
272 char addr[5];
273
274 sprintf(addr, "%04x", code);
275 OSSetFontWidth(OS_FONT_FIXED);
276 DrawString(50, y, addr);
277 OSSetFontWidth(OS_FONT_PROPORTIONAL);
278
279 for (x = 150;
280 x < 150 + FontData->cellHeight * 16;
281 x += FontData->cellWidth)
282 {
283 DrawChar(x, y, code);
284 ++code;
285 }
286 }
287
288 DEMODoneRender();
289 }
290
291 OSHalt("End of demo");
292 }
293