Added opengl rendering.
[cantaveria.git] / loader.c
blob451efd17d874db1cf8585b2cf36a2601ff524d51
1 /*
2 Cantaveria - action adventure platform game
3 Copyright (C) 2009 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
23 #include <string.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <stdio.h>
28 #include <zzip/lib.h>
32 #include "loader.h"
33 #include "util.h"
35 struct reader {
36 ZZIP_FILE* f;
37 int next_c;
40 ZZIP_DIR* zzip_dir;
41 int errno;
43 void loader_init(char* filename){
44 //zzip_dir = zzip_dir_open(filename, 0);
45 //if(!zzip_dir){
46 // report_error("loader: unable to open game data in %s (%s)\n",
47 // filename, strerror( errno ) );
48 // exit(-1);
49 // }
52 void loader_quit(){
53 //zzip_dir_close(zzip_dir);
56 reader* loader_open(char* filename){
57 char buf[1024] = "data/";
58 int L = strlen(buf);
59 strncpy(buf+L,filename,1024-L);
60 buf[1023] = 0;
62 reader* rd = malloc(sizeof(reader));
63 if(!rd) return NULL;
64 rd->next_c = -1;
65 //rd->f = zzip_file_open(zzip_dir, buf, 0);
66 rd->f = zzip_open(buf, 0);
67 if(!rd->f){
68 //report_error("loader: unable to open %s (%s)\n",
69 // filename, zzip_strerror_of( zzip_dir ) );
70 report_error("loader: unable to open %s (%s)\n",
71 filename, strerror( errno ) );
72 free(rd);
73 return NULL;
75 return rd;
79 void loader_close(reader* rd){
80 //zzip_file_close(rd->f);
81 zzip_fclose(rd->f);
82 free(rd);
86 int loader_read(reader* rd, void* buf, int count){
87 //return zzip_file_read(rd->f, buf, count);
88 return zzip_read(rd->f, buf, count);
91 unsigned char* loader_readall(char* filename, int* size){
92 ZZIP_STAT zs;
93 reader* rd = loader_open(filename);
94 if(!rd) return NULL;
95 if(zzip_fstat(rd->f, &zs) < 0){
96 report_error("loader: stat error on %s\n",filename);
97 return NULL;
99 int N = zs.st_size;
100 unsigned char* buf = malloc(N);
101 if(!buf){
102 report_error("loader: out of memory\n");
103 return NULL;
105 loader_read(rd,buf,N);
106 if(size) *size = N;
107 loader_close(rd);
108 return buf;
112 int loader_scanline(reader* rd, char* format, ...){
114 char buf[256];
115 int i=0;
116 while(i<255){
117 char c;
119 /* get next character */
120 if(rd->next_c != -1){
121 c = rd->next_c;
122 rd->next_c = -1;
124 else{
125 loader_read(rd, &c, 1);
128 /* see if it is a end of line sequence */
129 if(c=='\r'){
130 loader_read(rd, &c, 1);
131 if(c!='\n'){
132 rd->next_c = c;
134 break;
136 else if(c=='\n'){
137 break;
140 buf[i++] = c;
142 buf[i]='\0';
144 //printf("loader_scanline: %s\n",buf);
146 va_list ap;
147 va_start(ap, format);
149 int ret = vsscanf(buf,format,ap);
151 va_end(ap);
153 return ret;