Switched to an interpolated table generator in org.c
[cantaveria.git] / text.c
blob8bc1eb4df4359c8f0099a15f2bf67f8c04e90320
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 <util.h>
31 #include <video.h>
32 #include <loader.h>
34 typedef struct {
35 utf32 u;
36 int gfx;
37 int x, y, w, h;
38 int k1, k2;
39 } vwchar;
41 struct treenode* chartree = NULL;
44 int ptrcomp(void* k1, void* k2){
45 return (k2 - k1);
48 void set_message(char* str){
49 utf32 u;
50 int N = unicode_getc(str, &u);
51 while(u) {
52 vwchar* C = tree_search(chartree, ptrcomp, (void*)u);
53 if(C){
54 /* append this character to the message
55 if the current word is too long,
56 move the current word to the next line
57 if the current word is longer than a whole line
58 then just break here (would happen with japanese).
60 //printf("%04lx[%lc] ",u, C->u);
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
68 //printf("%04lx[???] ", u);
70 N += unicode_getc(str+N, &u);
72 printf("\n");
77 void advance_message(){
81 void clear_message(){
85 void complete_message(){
90 void text_init(){
91 vwchar* C = xmalloc(sizeof(vwchar));
92 C->gfx = 0;
93 C->u = ' ';
94 C->x = 0;
95 C->y = 0;
96 C->w = 7;
97 C->k1 = 0;
98 C->k2 = 0;
99 chartree = xmalloc(sizeof(treenode));
100 chartree->l = NULL;
101 chartree->r = NULL;
102 chartree->value = C;
103 chartree->key = (void*)' ';
105 //set font height
110 vwchar* load_vwchar(reader* rd, int gfx){
111 utf32 u;
112 int x, y, w, k1, k2;
113 char str[256];
114 int ret = loader_scanline(rd, "%256s %d %d %d %d %d\n",str,&x,&y,&w,&k1,&k2);
115 if(ret == EOF){
116 return NULL;
118 unicode_getc(str,&u);
119 vwchar* C = xmalloc(sizeof(vwchar));
120 C->gfx = gfx;
121 C->u = u;
122 C->x = x;
123 C->y = y;
124 C->w = w;
125 C->k1 = k1;
126 C->k2 = k2;
127 return C;
131 void print_tree(treenode* node){
132 printf("(%lx,",(utf32)node->key);
133 if(node->l){
134 print_tree(node->l);
136 else{
137 printf("()");
139 printf(",");
140 if(node->r){
141 print_tree(node->r);
143 else{
144 printf("()");
146 printf(")");
150 void randomly_insert(vwchar* C[], int count){
151 int i;
152 for(i=0; i<count-1; i++){
153 int j = randi(0, count-i-1);
154 tree_insert(chartree, ptrcomp, (void*)C[j]->u, C[j]);
155 C[j] = C[count-i-1];
156 C[count-i-1] = NULL;
161 int load_font(char* filename){
162 printf("load_font: loading %s\n",filename);
163 char buf[256] = "fonts/";
164 strmcat(buf, filename, 256);
165 reader* rd = loader_open(buf);
166 if(!rd){
167 fatal_error("load_font: cannot open %s\n",filename);
170 char str[256];
171 loader_scanline(rd, "%256s", str);
172 int gfx = load_gfx(str);
174 /* we read 64 characters at a time and insert them
175 randomly into the binary search tree. this is supposed
176 to help produce a more balanced tree. */
177 vwchar* C[64];
178 int ptr = 0;
179 int N = 0;
180 C[ptr] = load_vwchar(rd, gfx);
181 while(C[ptr]){
182 N++;
183 if(ptr==64){
184 randomly_insert(C, 64);
185 ptr = 0;
187 else{
188 C[++ptr] = load_vwchar(rd, gfx);
192 randomly_insert(C, ptr);
194 printf(" loaded %d characters\n",N);
196 printf(" character tree is the following\n");
197 print_tree(chartree);
198 printf("\n");
200 return 0;