Merge branch 'stage'
[cantaveria.git] / text.c
blob34c433d9ba8418e01c9df60f174299e23398b5ad
1 /*
2 Cantaveria - action adventure platform game
3 Copyright (C) 2009 2010 Evan Rinehart
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to
18 The Free Software Foundation, Inc.
19 51 Franklin Street, Fifth Floor
20 Boston, MA 02110-1301, USA
22 evanrinehart@gmail.com
25 /* messages and text */
27 #include <stdio.h>
28 #include <string.h>
30 #include <list.h>
31 #include <util.h>
32 #include <video.h>
33 #include <loader.h>
35 typedef struct {
36 utf32 u;
37 int gfx;
38 int x, y, w, h;
39 int k1, k2;
40 } vwchar;
42 struct treenode* chartree = NULL;
45 int ptrcomp(void* k1, void* k2){
46 return (k2 - k1);
49 void set_message(char* str){
50 utf32 u;
51 int N = unicode_getc(str, &u);
52 while(u) {
53 vwchar* C = tree_search(chartree, ptrcomp, (void*)u);
54 if(C){
55 /* append this character to the message
56 if the current word is too long,
57 move the current word to the next line
58 if the current word is longer than a whole line
59 then just break here (would happen with japanese).
61 //printf("%04lx[%lc] ",u, C->u);
63 else{
65 character not found, so use a rectangle or something
66 use four tiny numbers to indicate the character
67 do the same as above
69 //printf("%04lx[???] ", u);
71 N += unicode_getc(str+N, &u);
73 printf("\n");
78 void advance_message(){
82 void clear_message(){
86 void complete_message(){
91 void text_init(){
92 vwchar* C = xmalloc(sizeof(vwchar));
93 C->gfx = 0;
94 C->u = ' ';
95 C->x = 0;
96 C->y = 0;
97 C->w = 7;
98 C->k1 = 0;
99 C->k2 = 0;
100 chartree = xmalloc(sizeof(treenode));
101 chartree->l = NULL;
102 chartree->r = NULL;
103 chartree->value = C;
104 chartree->key = (void*)' ';
106 //set font height
111 vwchar* load_vwchar(reader* rd, int gfx){
112 utf32 u;
113 int x, y, w, k1, k2;
114 char str[256];
115 int ret = loader_scanline(rd, "%256s %d %d %d %d %d\n",str,&x,&y,&w,&k1,&k2);
116 if(ret == EOF){
117 return NULL;
119 unicode_getc(str,&u);
120 vwchar* C = xmalloc(sizeof(vwchar));
121 C->gfx = gfx;
122 C->u = u;
123 C->x = x;
124 C->y = y;
125 C->w = w;
126 C->k1 = k1;
127 C->k2 = k2;
128 return C;
132 void print_tree(treenode* node){
133 printf("(%lx,",(utf32)node->key);
134 if(node->l){
135 print_tree(node->l);
137 else{
138 printf("()");
140 printf(",");
141 if(node->r){
142 print_tree(node->r);
144 else{
145 printf("()");
147 printf(")");
151 void randomly_insert(vwchar* C[], int count){
152 int i;
153 for(i=0; i<count-1; i++){
154 int j = randi(0, count-i-1);
155 tree_insert(chartree, ptrcomp, (void*)C[j]->u, C[j]);
156 C[j] = C[count-i-1];
157 C[count-i-1] = NULL;
162 int load_font(char* filename){
163 printf("load_font: loading %s\n",filename);
164 char buf[256] = "fonts/";
165 strmcat(buf, filename, 256);
166 reader* rd = loader_open(buf);
167 if(!rd){
168 fatal_error("load_font: cannot open %s\n",filename);
171 char str[256];
172 loader_scanline(rd, "%256s", str);
173 int gfx = load_gfx(str);
175 /* we read 64 characters at a time and insert them
176 randomly into the binary search tree. this is supposed
177 to help produce a more balanced tree. */
178 vwchar* C[64];
179 int ptr = 0;
180 int N = 0;
181 C[ptr] = load_vwchar(rd, gfx);
182 while(C[ptr]){
183 N++;
184 if(ptr==64){
185 randomly_insert(C, 64);
186 ptr = 0;
188 else{
189 C[++ptr] = load_vwchar(rd, gfx);
193 randomly_insert(C, ptr);
195 printf(" loaded %d characters\n",N);
197 printf(" character tree is the following\n");
198 print_tree(chartree);
199 printf("\n");
201 return 0;