Added smf reader routine.
[cantaveria.git] / text.c
blobf02075a5b803b200d5861031689ba4e89d406ecf
1 /* messages and text */
3 #include <stdio.h>
4 #include <string.h>
6 #include "util.h"
7 #include "backend.h"
8 #include "loader.h"
10 typedef struct {
11 utf32 u;
12 int gfx;
13 int x, y, w, h;
14 int k1, k2;
15 } vwchar;
17 struct treenode* chartree = NULL;
20 int ptrcomp(void* k1, void* k2){
21 return (k2 - k1);
24 void set_message(char* str){
25 utf32 u;
26 int N = unicode_getc(str, &u);
27 while(u) {
28 vwchar* C = tree_search(chartree, ptrcomp, (void*)u);
29 if(C){
30 /* append this character to the message
31 if the current word is too long,
32 move the current word to the next line
33 if the current word is longer than a whole line
34 then just break here (would happen with japanese).
36 //printf("%04lx[%lc] ",u, C->u);
38 else{
40 character not found, so use a rectangle or something
41 use four tiny numbers to indicate the character
42 do the same as above
44 //printf("%04lx[???] ", u);
46 N += unicode_getc(str+N, &u);
48 printf("\n");
53 void advance_message(){
57 void clear_message(){
61 void complete_message(){
66 void text_init(){
67 vwchar* C = xmalloc(sizeof(vwchar));
68 C->gfx = 0;
69 C->u = ' ';
70 C->x = 0;
71 C->y = 0;
72 C->w = 7;
73 C->k1 = 0;
74 C->k2 = 0;
75 chartree = xmalloc(sizeof(treenode));
76 chartree->l = NULL;
77 chartree->r = NULL;
78 chartree->value = C;
79 chartree->key = (void*)' ';
81 //set font height
86 vwchar* load_vwchar(reader* rd, int gfx){
87 utf32 u;
88 int x, y, w, k1, k2;
89 char str[256];
90 int ret = loader_scanline(rd, "%256s %d %d %d %d %d\n",str,&x,&y,&w,&k1,&k2);
91 if(ret == EOF){
92 return NULL;
94 unicode_getc(str,&u);
95 vwchar* C = xmalloc(sizeof(vwchar));
96 C->gfx = gfx;
97 C->u = u;
98 C->x = x;
99 C->y = y;
100 C->w = w;
101 C->k1 = k1;
102 C->k2 = k2;
103 return C;
107 void print_tree(treenode* node){
108 printf("(%lx,",(utf32)node->key);
109 if(node->l){
110 print_tree(node->l);
112 else{
113 printf("()");
115 printf(",");
116 if(node->r){
117 print_tree(node->r);
119 else{
120 printf("()");
122 printf(")");
126 void randomly_insert(vwchar* C[], int count){
127 for(int i=0; i<count-1; i++){
128 int j = randint(0,count-i-1);
129 tree_insert(chartree, ptrcomp, (void*)C[j]->u, C[j]);
130 C[j] = C[count-i-1];
131 C[count-i-1] = NULL;
136 int load_font(char* filename){
137 printf("load_font: loading %s\n",filename);
138 char buf[256] = "fonts/";
139 strmcat(buf, filename, 256);
140 reader* rd = loader_open(buf);
141 if(!rd){
142 fatal_error("load_font: cannot open %s\n",filename);
145 char str[256];
146 loader_scanline(rd, "%256s", str);
147 int gfx = load_gfx(str);
149 /* we read 64 characters at a time and insert them
150 randomly into the binary search tree. this is supposed
151 to help produce a more balanced tree. */
152 vwchar* C[64];
153 int ptr = 0;
154 int N = 0;
155 C[ptr] = load_vwchar(rd, gfx);
156 while(C[ptr]){
157 N++;
158 if(ptr==64){
159 randomly_insert(C, 64);
160 ptr = 0;
162 else{
163 C[++ptr] = load_vwchar(rd, gfx);
167 randomly_insert(C, ptr);
169 printf(" loaded %d characters\n",N);
171 printf(" character tree is the following\n");
172 print_tree(chartree);
173 printf("\n");
175 return 0;