Mercurial > hg > old > magoroku_racing
annotate gFont.c @ 96:0b65ca27f113
compile on Mac OS X 2
author | Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 02 Jun 2011 14:56:11 +0900 |
parents | 8edae89a3877 |
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; // スプライトロードフラグ |
0 | 26 |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
27 static TEXTURE *font_tex; |
0 | 28 |
29 static void | |
30 gFont_Init_Png(char *texname) | |
31 { | |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
32 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
|
33 fprintf(stderr, |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
34 "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
|
35 fflush(stderr); |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
36 exit(EXIT_FAILURE); |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
37 } |
0 | 38 |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
39 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
|
40 fprintf(stderr, |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
41 "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
|
42 fflush(stderr); |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
43 exit(EXIT_FAILURE); |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
44 } |
0 | 45 } |
46 | |
47 static void | |
48 gFont_Create() | |
49 { | |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
50 int i; |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
51 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
|
52 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
|
53 fprintf(stderr, |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
54 "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
|
55 fflush(stderr); |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
56 exit(EXIT_FAILURE); |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
57 } |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
58 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
|
59 } |
0 | 60 |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
61 |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
62 flag_spriteInited = true; |
0 | 63 } |
64 | |
65 static void | |
66 gFont_DefSprite(int number, int x, int y, int w, int h) | |
67 { | |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
68 sptable[number].tx = x; |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
69 sptable[number].ty = y; |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
70 sptable[number].tw = w; |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
71 sptable[number].th = h; |
0 | 72 } |
73 | |
74 void | |
75 gFont_Init() | |
76 { | |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
77 int i, j; |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
78 int number = 0; |
0 | 79 |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
80 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
|
81 gFont_Create(); // スプライト生成 |
0 | 82 |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
83 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
|
84 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
|
85 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
|
86 number++; |
0 | 87 } |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
88 } |
0 | 89 } |
90 | |
91 | |
92 static void | |
93 gFont_PutSprite(int number, int x, int y) | |
94 { | |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
95 ps2utilSprite *p = &sp[sp_counter]; |
0 | 96 |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
97 // ushortってなんですか?:miya |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
98 /* |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
99 ps2util_sprite_Set_basicAttribute( |
0 | 100 p, |
101 (ushort)x, (ushort)y, | |
102 (ushort)sptable[number].tw*1.5, (ushort)sptable[number].th*1.5, | |
103 (ushort)sptable[number].tx, (ushort)sptable[number].ty, | |
104 (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
|
105 SPRITE_PRIO_FOREGROUND); |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
106 */ |
0 | 107 |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
108 ps2util_sprite_Request(p); |
0 | 109 sp_counter++; |
110 } | |
111 | |
112 | |
113 static void | |
114 gFont_Clear() | |
115 { | |
116 int i; | |
117 | |
118 for (i=0; i<DEFOBJ; i++) { | |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
119 sprite_disappear(&sp[i].attribute); |
0 | 120 } |
121 sp_counter = 0; | |
122 } | |
123 | |
124 void | |
125 gFont_Draw_Reset() | |
126 { | |
90
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
127 if (flag_spriteInited == true) { |
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
128 gFont_Clear(); |
0 | 129 } |
130 } | |
131 | |
132 static int | |
133 get_number_char(char a) | |
134 { | |
135 return (int)a-32; | |
136 } | |
137 | |
138 /* | |
88 | 139 * str をスプライド表示 |
0 | 140 */ |
141 void | |
142 gFont_SetString(char *str, int x, int y) | |
143 { | |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
144 int number; |
0 | 145 |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
146 while (*str != '\0') { |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
147 number = get_number_char(*str); |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
148 gFont_PutSprite(number, x, y); |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
149 str++; |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
150 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
|
151 } |
0 | 152 } |
153 | |
154 /* | |
88 | 155 * numを文字列に変換してSetStringに渡す |
0 | 156 */ |
157 void | |
158 gFont_SetStringInt(int num, int x, int y) | |
159 { | |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
160 char bufp[256]; |
0 | 161 |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
162 snprintf(bufp, 256, "%d", num); |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
90
diff
changeset
|
163 gFont_SetString(bufp, x, y); |
0 | 164 } |
165 | |
166 void | |
167 gFont_SetFont_OP() | |
168 { | |
169 } |