Mercurial > hg > old > magoroku_racing
annotate gFont.cc @ 134:42337585fb7c
fix merge
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 22 Jul 2011 18:32:39 +0900 |
parents | 4f50f5a3b363 |
children |
rev | line source |
---|---|
0 | 1 /* |
18 | 2 * $Id$ |
0 | 3 * |
88 | 4 * フォント表示。といっても結局スプライトです。 |
5 * gSprite.c と一緒にしてもよかったんだけど | |
6 * 文字は文字でいろいろあるので分けてみる | |
0 | 7 */ |
8 #include <stdio.h> | |
9 #include <stdlib.h> | |
90
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
10 #include <stdbool.h> |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
11 #include "libps2.h" |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
12 #include "ps2util.h" |
0 | 13 #include "gSprite.h" |
14 | |
15 #define DEFOBJ 120 | |
16 #define SPRITE_COL 0x4f | |
17 #define SCREEN_HALF_W 320 | |
18 #define SCREEN_HALF_H 240 | |
19 #define CHAR_W_SIZE 17 | |
20 #define CHAR_H_SIZE 34 | |
21 | |
22 static SpriteTable sptable[DEFOBJ]; | |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
23 static ps2utilSprite sp[DEFOBJ*2]; // 表示スプライト用スタック |
0 | 24 static unsigned int sp_counter; |
90
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
25 static bool flag_spriteInited = false; // スプライトロードフラグ |
116 | 26 //static TEXTURE *font_tex; |
130 | 27 extern SceneGraphPtr root; |
0 | 28 |
29 static void | |
100 | 30 gFont_Init_Png(const char *texname) |
0 | 31 { |
116 | 32 /* |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
33 if (!(font_tex = read_png_file(texname))) { |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
34 fprintf(stderr, |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
35 "cannot creat texture - gFont_Init_Png().\n"); |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
36 fflush(stderr); |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
37 exit(EXIT_FAILURE); |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
38 } |
0 | 39 |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
40 if (ps2util_tex_Set(font_tex) == -1) { |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
41 fprintf(stderr, |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
42 "cannot set texture - gFont_Init_Png().\n"); |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
43 fflush(stderr); |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
44 exit(EXIT_FAILURE); |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
45 } |
116 | 46 */ |
0 | 47 } |
48 | |
49 static void | |
50 gFont_Create() | |
51 { | |
116 | 52 /* |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
53 int i; |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
54 for (i=0; i<DEFOBJ; i++) { |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
55 if (ps2util_sprite_Create(&sp[i], font_tex) == -1) { |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
56 fprintf(stderr, |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
57 "cannot create sprite - gFont_Create()\n"); |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
58 fflush(stderr); |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
59 exit(EXIT_FAILURE); |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
60 } |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
61 ps2util_sprite_Unuse_alpha(&sp[i]); |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
62 } |
0 | 63 |
116 | 64 flag_spriteInited = true; |
65 */ | |
0 | 66 } |
67 | |
68 static void | |
69 gFont_DefSprite(int number, int x, int y, int w, int h) | |
70 { | |
116 | 71 sptable[number].mx = x; |
72 sptable[number].my = y; | |
73 sptable[number].w = w; | |
74 sptable[number].h = h; | |
75 } | |
76 | |
77 void | |
78 DefSprite(int number, const char *name, float w, float h, int color, OBJECT *obj) | |
79 { | |
80 SpriteTable *m = &sptable[number]; | |
81 m->w = w; | |
82 m->h = h; | |
83 m->color = (color & 32); | |
84 m->mx = w / 2; | |
85 m->my = h / 2; | |
86 m->tex_w = power_of_two(m->w); | |
87 m->tex_h = power_of_two(m->h); | |
88 m->texture = (int *)name; | |
0 | 89 } |
90 | |
91 void | |
92 gFont_Init() | |
93 { | |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
94 int i, j; |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
95 int number = 0; |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
96 gFont_Init_Png("img/font.png"); |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
97 gFont_Create(); // スプライト生成 |
0 | 98 |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
99 for (i=0; i<7; i++) { |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
100 for (j=0; j<15; j++) { |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
101 gFont_DefSprite(number, j*17, i*34, 17, 34); |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
102 number++; |
0 | 103 } |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
104 } |
0 | 105 } |
106 | |
107 | |
108 static void | |
109 gFont_PutSprite(int number, int x, int y) | |
110 { | |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
111 ps2utilSprite *p = &sp[sp_counter]; |
0 | 112 |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
113 // ushortってなんですか?:miya |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
114 /* |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
115 ps2util_sprite_Set_basicAttribute( |
0 | 116 p, |
117 (ushort)x, (ushort)y, | |
118 (ushort)sptable[number].tw*1.5, (ushort)sptable[number].th*1.5, | |
119 (ushort)sptable[number].tx, (ushort)sptable[number].ty, | |
120 (ushort)sptable[number].tw, (ushort)sptable[number].th, | |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
121 SPRITE_PRIO_FOREGROUND); |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
122 */ |
0 | 123 |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
124 ps2util_sprite_Request(p); |
0 | 125 sp_counter++; |
126 } | |
127 | |
128 | |
129 static void | |
130 gFont_Clear() | |
131 { | |
119 | 132 int i; |
0 | 133 |
119 | 134 for (i=0; i<DEFOBJ; i++) { |
135 sprite_disappear(&sp[i].attribute); | |
136 } | |
137 sp_counter = 0; | |
0 | 138 } |
139 | |
140 void | |
141 gFont_Draw_Reset() | |
142 { | |
119 | 143 if (flag_spriteInited == true) { |
144 gFont_Clear(); | |
145 } | |
0 | 146 } |
147 | |
148 static int | |
149 get_number_char(char a) | |
150 { | |
119 | 151 return (int)a-32; |
0 | 152 } |
153 | |
154 /* | |
88 | 155 * str をスプライド表示 |
0 | 156 */ |
157 void | |
100 | 158 gFont_SetString(const char *str, int x, int y) |
0 | 159 { |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
160 int number; |
130 | 161 char *ch_str = (char *)str; |
0 | 162 |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
163 while (*str != '\0') { |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
164 number = get_number_char(*str); |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
165 gFont_PutSprite(number, x, y); |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
166 str++; |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
167 x += (int)CHAR_W_SIZE*1.5; |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
168 } |
130 | 169 |
170 //SceneGraphPtr font = droot->createSceneGraph(); | |
171 droot->createStringFont(root,ch_str,50,/*640*/x,/*480*/y,0x00ff00ff); | |
172 //droot->setSceneData(font); | |
0 | 173 } |
174 | |
175 /* | |
88 | 176 * numを文字列に変換してSetStringに渡す |
0 | 177 */ |
178 void | |
179 gFont_SetStringInt(int num, int x, int y) | |
180 { | |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
181 char bufp[256]; |
0 | 182 |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
183 snprintf(bufp, 256, "%d", num); |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
184 gFont_SetString(bufp, x, y); |
0 | 185 } |
186 | |
187 void | |
188 gFont_SetFont_OP() | |
189 { | |
190 } |