annotate gFont.c @ 66:4af6c92304bb

*** empty log message ***
author gongo
date Thu, 09 Nov 2006 03:27:18 +0000
parents 3780d1b2943e
children ce9ef7dcf2f0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
1 /*
18
3780d1b2943e *** empty log message ***
gongo
parents: 2
diff changeset
2 * $Id$
0
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
3 *
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
4 * フォント表示。といっても結局スプライトです。
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
5 * gSprite.c と一緒にしてもよかったんだけど
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
6 * 文字は文字でいろいろあるので分けてみる
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
7 */
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
8 #include <stdio.h>
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
9 #include <stdlib.h>
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
10 #include "libps2.h"
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
11 #include "ps2util.h"
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
12 #include "gSprite.h"
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
13
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
14 #define DEFOBJ 120
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
15 #define SPRITE_COL 0x4f
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
16 #define SCREEN_HALF_W 320
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
17 #define SCREEN_HALF_H 240
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
18 #define CHAR_W_SIZE 17
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
19 #define CHAR_H_SIZE 34
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
20
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
21 static SpriteTable sptable[DEFOBJ];
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
22 static ps2utilSprite sp[DEFOBJ*2]; // 表示スプライト用スタック
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
23 static unsigned int sp_counter;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
24 static Bool flag_spriteInited = FALSE; // スプライトロードフラグ
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
25
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
26 static TEXTURE *font_tex;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
27
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
28 static void
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
29 gFont_Init_Png(char *texname)
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
30 {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
31 if (!(font_tex = read_png_file(texname))) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
32 fprintf(stderr,
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
33 "cannot creat texture - gFont_Init_Png().\n");
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
34 fflush(stderr);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
35 exit(EXIT_FAILURE);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
36 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
37
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
38 if (ps2util_tex_Set(font_tex) == -1) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
39 fprintf(stderr,
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
40 "cannot set texture - gFont_Init_Png().\n");
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
41 fflush(stderr);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
42 exit(EXIT_FAILURE);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
43 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
44 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
45
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
46 static void
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
47 gFont_Create()
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
48 {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
49 int i;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
50
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
51 for (i=0; i<DEFOBJ; i++) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
52 if (ps2util_sprite_Create(&sp[i], font_tex) == -1) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
53 fprintf(stderr,
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
54 "cannot create sprite - gFont_Create()\n");
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
55 fflush(stderr);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
56 exit(EXIT_FAILURE);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
57 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
58 ps2util_sprite_Unuse_alpha(&sp[i]);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
59 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
60 flag_spriteInited = TRUE;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
61 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
62
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
63 static void
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
64 gFont_DefSprite(int number, int x, int y, int w, int h)
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
65 {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
66 sptable[number].tx = x;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
67 sptable[number].ty = y;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
68 sptable[number].tw = w;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
69 sptable[number].th = h;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
70 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
71
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
72 void
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
73 gFont_Init()
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
74 {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
75 int i, j;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
76 int number = 0;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
77
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
78 gFont_Init_Png("img/font.png");
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
79 gFont_Create(); // スプライト生成
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
80
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
81 for (i=0; i<7; i++) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
82 for (j=0; j<15; j++) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
83 gFont_DefSprite(number, j*17, i*34, 17, 34);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
84 number++;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
85 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
86 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
87 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
88
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
89
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
90 static void
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
91 gFont_PutSprite(int number, int x, int y)
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
92 {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
93 ps2utilSprite *p = &sp[sp_counter];
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
94
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
95 ps2util_sprite_Set_basicAttribute(
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
96 p,
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
97 (ushort)x, (ushort)y,
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
98 (ushort)sptable[number].tw*1.5, (ushort)sptable[number].th*1.5,
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
99 (ushort)sptable[number].tx, (ushort)sptable[number].ty,
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
100 (ushort)sptable[number].tw, (ushort)sptable[number].th,
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
101 SPRITE_PRIO_FOREGROUND);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
102
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
103 ps2util_sprite_Request(p);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
104 sp_counter++;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
105 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
106
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
107
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
108 static void
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
109 gFont_Clear()
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
110 {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
111 int i;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
112
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
113 for (i=0; i<DEFOBJ; i++) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
114 sprite_disappear(&sp[i].attribute);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
115 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
116 sp_counter = 0;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
117 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
118
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
119 void
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
120 gFont_Draw_Reset()
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
121 {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
122 if (flag_spriteInited == TRUE) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
123 gFont_Clear();
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
124 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
125 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
126
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
127 static int
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
128 get_number_char(char a)
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
129 {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
130 return (int)a-32;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
131 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
132
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
133 /*
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
134 * str をスプライド表示
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
135 */
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
136 void
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
137 gFont_SetString(char *str, int x, int y)
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
138 {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
139 int number;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
140
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
141 while (*str != '\0') {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
142 number = get_number_char(*str);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
143 gFont_PutSprite(number, x, y);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
144 str++;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
145 x += (int)CHAR_W_SIZE*1.5;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
146 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
147 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
148
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
149 /*
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
150 * numを文字列に変換してSetStringに渡す
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
151 */
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
152 void
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
153 gFont_SetStringInt(int num, int x, int y)
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
154 {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
155 char bufp[256];
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
156
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
157 snprintf(bufp, 256, "%d", num);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
158 gFont_SetString(bufp, x, y);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
159 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
160
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
161 void
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
162 gFont_SetFont_OP()
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
163 {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
164 }