canvas.c
4.42 KB
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
/*---------------------------------------------------------------------
File : canvas.c version 1.3
Coded b Yoshitaka Yasumoto. Feb 5, 1996.
Modified by Koji Mitsunari Apr 13, 1996.
Copyright by Nintendo, Co., Ltd. 1996.
---------------------------------------------------------------------*/
#include <ultra64.h>
#include "pakmenu.h"
#if LANG_JAPANESE
#include "fontbmp.h"
#endif
typedef struct {
u8 code;
u8 number;
} FTBL;
FTBL canvas_ftbl[] = {
{ FONT_CODE_A-FONT_CODE_TOP, 26 }, // A-Z
{ FONT_CODE_0-FONT_CODE_TOP, 10 }, // 0-9
{ FONT_CODE_i-FONT_CODE_TOP, 14 }, // !"#'*+,-./:=?@
#if LANG_JAPANESE
{ FONT_CODE_o-FONT_CODE_TOP, 58 }, // KANA
#endif
};
#if LANG_JAPANESE
s8 font_kana_dot[] = { // font pattern of DAKUTEN,HANDAKUTEN
/* 0123456789012345
-------------XO-
-----------XOXXO
-----------XXOXX
------------XX-- */
0x02, 0x09, 0x04, 0x00,
~0x04, ~0x16, ~0x1b, ~0x0c,
/* 0123456789012345
------------XOOO
------------XOXO
------------XOOO
------------XXXX */
0x07, 0x05, 0x07, 0x00,
~0x08, ~0x0a, ~0x08, ~0x0f,
};
#endif
u16 *__font_buf;
void nosLoadFont(u16 *font_buf)
{
register u16 *fpix;
register u32 *farc = (u32 *)FONTARC_TOP+1;
register u32 bitmap = *(farc-1);
register u32 position = 0;
register int i;
register int j;
int k;
__font_buf = font_buf;
// Clear font buffer
for (i = 0; i < FONT_HT * (FONT_CODE_BTM - FONT_CODE_TOP + 2); i++){
*font_buf++ = 0x0000;
}
// Extract font
for (i = 0; i < sizeof(canvas_ftbl)/sizeof(FTBL); i ++){
#if LANG_JAPANESE
if (i == 3){
farc = (u32 *)font_bitmaps;
position = 0;
bitmap = *farc ++;
}
#endif
fpix = __font_buf +
(canvas_ftbl[i].code * FONT_HT + FONT_HOFS);
for (j = 0; j < canvas_ftbl[i].number; j ++){
for (k = 0; k < FONTARC_HT; k ++){
position += FONTARC_WD;
if (position & ~0x1f) {
position &= 0x1f;
*fpix = (u16)(bitmap << position) & 0x1fff;
bitmap = *farc ++;
if (!position) {
goto nosLoadFont_00;
}
}
*fpix |= (u16)(bitmap >> (32 - position)) & 0x1fff;
nosLoadFont_00:
fpix ++;
#if DEBUG_FONT
{
int n;
osSyncPrintf("j=%3d k=%3d position=%3d ", j, k, position);
for (n = 15; n >= 0; n --){
if (*(fpix-1) & (1 << n)) osSyncPrintf("O");
else osSyncPrintf("-");
}
osSyncPrintf("\n");
}
#endif
}
fpix += (FONT_HT-FONTARC_HT);
position += 2;
}
}
#if LANG_JAPANESE
// put DAKUTEN,HANDAKUTEN
for (i = F_ADRS(KA); i < F_ADRS(MA); i ++) {
(__font_buf+(F_ADRS(GA)-F_ADRS(KA)))[i] = __font_buf[i];
}
for (i = F_ADRS(HA); i < F_ADRS(MA); i ++) {
(__font_buf+(F_ADRS(BA)-F_ADRS(HA)))[i] = __font_buf[i];
}
k = 0;
fpix = __font_buf + F_ADRS(GA);
for (i = FONT_CODE_GA; i < FONT_CODE_PA+5; i ++){
if (i == FONT_CODE_PA) k = 8;
for (j = 0; j < 4; j ++){
fpix[j] = (fpix[j] | font_kana_dot[j+k]) & font_kana_dot[j+k+4];
}
#if DEBUG_FONT
{
int n;
for (j = 0; j < FONT_HT; j ++){
for (n = 15; n >= 0; n --){
if (fpix[j] & (1 << n)) osSyncPrintf("O");
else osSyncPrintf("-");
}
osSyncPrintf("\n");
}
osSyncPrintf("\n");
}
#endif
fpix += FONT_HT;
}
#endif
}
void canvas_u8_to_str(u16 n, u8 *str)
{
if (n > 999) {
n = 999;
}
#if 1
str[0] = n / 100 + 0x0f + (n >= 100);
str[1] = (n % 100) / 10 + 0x0f + (n >= 10);
str[2] = n % 10 + 0x10;
#else
static u8 ofs[] = { 0x0f, 0x0f, 0x10, 0x10, 0x10 };
s32 i;
if (n >= 100) i = 2;
else if (n >= 10) i = 1;
else i = 0;
str[0] = n / 100 + ofs[i+0];
n %= 100;
str[1] = n / 10 + ofs[i+1];
str[2] = n % 10 + ofs[i+2];
#endif
}
u16 *__cursor;
void canvas_putc(int c, u16 color_fg, u16 color_bg)
{
register int i;
register int j;
register u16 *fpix;
register u16 *curp;
if (c == FONT_CODE_QSKIP) {
__cursor += FONT_WSKIP/3;
} else {
if (c < FONT_CODE_TOP || FONT_CODE_BTM < c) {
c = FONT_CODE_NON;
}
fpix = __font_buf + (c - FONT_CODE_TOP)*FONT_HT;
curp = __cursor;
for (i = 0; i < FONT_HT; i ++) {
for (j = FONT_WD - 1; j >= 0; j --) {
if ((*fpix >> j) & 1){
*(curp) = color_fg;
*(curp+1+SCREEN_WD) = color_bg;
}
curp ++;
}
curp += (SCREEN_WD - FONT_WD);
fpix ++;
}
__cursor += FONT_WSKIP;
}
}
void canvas_puts(u8 *str, int n, u16 color_fg, u16 color_bg)
{
while (n -- && *str) canvas_putc(*str ++, color_fg, color_bg);
}