Rewrite of the stage module.
[cantaveria.git] / text.c
blob5daf8f02a53cacf474daae117c4e04c8de5d89b2
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 <graphics.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).
62 else{
64 character not found, so use a rectangle or something
65 use four tiny numbers to indicate the character
66 do the same as above
69 N += unicode_getc(str+N, &u);
75 void advance_message(){
79 void clear_message(){
83 void complete_message(){
88 void text_init(){
89 vwchar* C = xmalloc(sizeof(vwchar));
90 C->gfx = 0;
91 C->u = ' ';
92 C->x = 0;
93 C->y = 0;
94 C->w = 7;
95 C->k1 = 0;
96 C->k2 = 0;
97 chartree = xmalloc(sizeof(treenode));
98 chartree->l = NULL;
99 chartree->r = NULL;
100 chartree->value = C;
101 chartree->key = (void*)' ';
103 //set font height
108 vwchar* load_vwchar(reader* rd, int gfx){
109 utf32 u;
110 int x, y, w, k1, k2;
111 char str[256];
112 int ret = loader_scanline(rd, "%256s %d %d %d %d %d\n",str,&x,&y,&w,&k1,&k2);
113 if(ret == EOF){
114 return NULL;
116 unicode_getc(str,&u);
117 vwchar* C = xmalloc(sizeof(vwchar));
118 C->gfx = gfx;
119 C->u = u;
120 C->x = x;
121 C->y = y;
122 C->w = w;
123 C->k1 = k1;
124 C->k2 = k2;
125 return C;
129 void print_tree(treenode* node){
130 printf("(%lx,",(utf32)node->key);
131 if(node->l){
132 print_tree(node->l);
134 else{
135 printf("()");
137 printf(",");
138 if(node->r){
139 print_tree(node->r);
141 else{
142 printf("()");
144 printf(")");
148 void randomly_insert(vwchar* C[], int count){
149 int i;
150 for(i=0; i<count-1; i++){
151 int j = randi(0, count-i-1);
152 tree_insert(chartree, ptrcomp, (void*)C[j]->u, C[j]);
153 C[j] = C[count-i-1];
154 C[count-i-1] = NULL;
159 int load_font(char* filename){
160 //printf("load_font: loading %s\n",filename);
161 char buf[256] = "fonts/";
162 strmcat(buf, filename, 256);
163 reader* rd = loader_open(buf);
164 if(!rd){
165 fatal_error("load_font: cannot open %s\n",filename);
168 char str[256];
169 loader_scanline(rd, "%256s", str);
170 int gfx = load_bitmap(str);
172 /* we read 64 characters at a time and insert them
173 randomly into the binary search tree. this is supposed
174 to help produce a more balanced tree. */
175 vwchar* C[64];
176 int ptr = 0;
177 int N = 0;
178 C[ptr] = load_vwchar(rd, gfx);
179 while(C[ptr]){
180 N++;
181 if(ptr==64){
182 randomly_insert(C, 64);
183 ptr = 0;
185 else{
186 C[++ptr] = load_vwchar(rd, gfx);
190 randomly_insert(C, ptr);
192 printf(" loaded %d characters\n",N);
194 printf(" character tree is the following\n");
195 print_tree(chartree);
196 printf("\n");
198 return 0;