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