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
34 void report_verror(const char* format
, va_list ap
){
38 void fatal_error(const char* format
, ...){
41 report_verror(format
, ap
);
46 void boot_msg(const char* format
, ...){
53 void error_msg(const char* format
, ...){
56 report_verror(format
, ap
);
60 void out_of_memory(const char* prefix
){
61 error_msg("%s: *out of memory*\n", prefix
);
65 /* 'safe' functions */
66 void* xmalloc(size_t size
){
67 void* v
= malloc(size
);
69 /* On Linux malloc by default does not return NULL
70 even if there is no more memory. So this function
71 isn't really 'safe' on that system. */
72 out_of_memory("xmalloc");
77 char* strxcpy(const char* str
){
78 char* cpy
= xmalloc(strlen(str
)+1);
83 void strmcat(char* dst
, const char* src
, size_t n
){
84 strncat(dst
, src
, n
- strlen(src
) - 1);
91 decodes utf8 encoded string str and places the
93 returns the number of bytes of str consumed.
95 int unicode_getc(char* str
, utf32
* u
){
96 /*unsigned char b[4] = {str[0], str[1], str[2], str[3]};*/
98 unsigned char a
[4] = {0,0,0,0};
108 if((b
[0] & 0x80) == 0){ /*one byte sequence*/
114 ((b
[0]&0xe0)==0xc0) &&
117 /*two byte sequence*/
118 a
[3] = ((b
[0]&0x03)<<6)|(b
[1]&0x3f);
119 a
[2] = (b
[0]&0x1c)>>2;
124 ((b
[0]&0xf0)==0xe0) &&
125 ((b
[1]&0xc0)==0x80) &&
128 /*three byte sequence*/
129 a
[3] = ((b
[1]&0x03)<<6)|(b
[2]&0x3f);
130 a
[2] = ((b
[0]&0x0f)<<4)|((b
[1]&0x3c)>>2);
135 ((b
[0]&0xf8)==0xf0) &&
136 ((b
[1]&0xc0)==0x80) &&
137 ((b
[2]&0xc0)==0x80) &&
140 /*four byte sequence*/
141 a
[3] = ((b
[2]&0x03)<<6)|(b
[3]&0x3f);
142 a
[2] = ((b
[1]&0x0f)<<4)|((b
[2]&0x3c)>>2);
143 a
[1] = ((b
[0]&0x03)<<2)|((b
[1]&0x30)>>4);
149 N
= 4;/*FIXME find next valid byte*/
152 *u
= (a
[0]<<24) | (a
[1]<<16) | (a
[2]<<8) | a
[3];
159 struct treenode
* root
,
160 int (*compare
)(void* k1
, void* k2
),
161 void* key
, void* value
)
164 struct treenode
* node
= xmalloc(sizeof(struct treenode
));
165 struct treenode
* ptr
;
173 if( compare(ptr
->key
, key
) < 0 ){
174 if(ptr
->l
){ptr
= ptr
->l
;}
175 else{ptr
->l
= node
; break;}
177 else if( compare(ptr
->key
, key
) > 0){
178 if(ptr
->r
){ptr
= ptr
->r
;}
179 else{ptr
->r
= node
; break;}
181 else{ /* key already exists */
182 error_msg("tree_insert: key already exists\n");
189 struct treenode
* root
,
190 int (*compare
)(void* k1
, void* k2
),
196 else if(compare(root
->key
, key
)>0){
197 return tree_search(root
->r
, compare
, key
);
199 else if(compare(root
->key
, key
)<0){
200 return tree_search(root
->l
, compare
, key
);
209 /* rng utility functions */
210 void rand_reset(unsigned s
){
214 int randi(int a
, int b
){
232 unsigned x
= zrand();
238 char* path_ending(char* path
){
239 char* c
= path
+ strlen(path
);
240 while(*(c
-1) != '/' && c
> path
) c
--;
245 /* Copyright 300BC Euclid */
246 int gcd(int a
, int b
){