1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #include "SDL.h"
6 #include "roms.h"
7 #include "resfile.h"
8 #include "unzip.h"
9 //#include "stb_zlib.h"
10 #include "conf.h"
11 #include "stb_image.h"
12
13
zread_char(ZFILE * gz,char * c,int len)14 void zread_char(ZFILE *gz, char *c, int len) {
15 int rc;
16 rc = gn_unzip_fread(gz, (Uint8*)c, len);
17 //printf("HS %s %d\n",c,rc);
18 }
zread_uint8(ZFILE * gz,Uint8 * c)19 void zread_uint8(ZFILE *gz, Uint8 *c) {
20 int rc;
21 rc = gn_unzip_fread(gz, c, 1);
22 //printf("H8 %02x %d\n",*c,rc);
23 }
zread_uint32le(ZFILE * gz,Uint32 * c)24 void zread_uint32le(ZFILE *gz, Uint32 *c) {
25 int rc;
26 rc = gn_unzip_fread(gz, (Uint8*)c, sizeof(Uint32));
27 #ifdef WORDS_BIGENDIAN
28 *c=SDL_Swap32(*c);
29 #endif
30 //printf("H32 %08x %d\n",*c,rc);
31 }
32
33 /*
34 * Load a rom definition file from gngeo.dat (rom/name.drv)
35 * return ROM_DEF*, NULL on error
36 */
res_load_drv(char * name)37 ROM_DEF *res_load_drv(char *name) {
38 char *gngeo_dat = CF_STR(cf_get_item_by_name("datafile"));
39 ROM_DEF *drv;
40 char drvfname[32];
41 PKZIP *pz;
42 ZFILE *z;
43 int i;
44
45 drv = calloc(sizeof(ROM_DEF), 1);
46
47 /* Open the rom driver def */
48 pz = gn_open_zip(gngeo_dat);
49 if (pz == NULL) {
50 fprintf(stderr, "Can't open the %s\n", gngeo_dat);
51 return NULL;
52 }
53 sprintf(drvfname, "rom/%s.drv", name);
54
55 if ((z=gn_unzip_fopen(pz,drvfname,0x0)) == NULL) {
56 fprintf(stderr, "Can't open rom driver for %s\n", name);
57 return NULL;
58 }
59
60 //Fill the driver struct
61 zread_char(z, drv->name, 32);
62 zread_char(z, drv->parent, 32);
63 zread_char(z, drv->longname, 128);
64 zread_uint32le(z, &drv->year);
65 for (i = 0; i < 10; i++)
66 zread_uint32le(z, &drv->romsize[i]);
67 zread_uint32le(z, &drv->nb_romfile);
68 for (i = 0; i < drv->nb_romfile; i++) {
69 zread_char(z, drv->rom[i].filename, 32);
70 zread_uint8(z, &drv->rom[i].region);
71 zread_uint32le(z, &drv->rom[i].src);
72 zread_uint32le(z, &drv->rom[i].dest);
73 zread_uint32le(z, &drv->rom[i].size);
74 zread_uint32le(z, &drv->rom[i].crc);
75 }
76 gn_unzip_fclose(z);
77 gn_close_zip(pz);
78 return drv;
79 }
80
81
82
83 /*
84 * Load a stb image from gngeo.dat
85 * return a SDL_Surface, NULL on error
86 * supported format: bmp, tga, jpeg, png, psd
87 * 24&32bpp only
88 */
res_load_stbi(char * bmp)89 SDL_Surface *res_load_stbi(char *bmp) {
90 PKZIP *pz;
91 SDL_Surface *s;
92 Uint8 * buffer;
93 unsigned int size;
94 int x, y, comp;
95 stbi_uc *data = NULL;
96
97 pz = gn_open_zip(CF_STR(cf_get_item_by_name("datafile")));
98 if (!pz)
99 return NULL;
100 buffer = gn_unzip_file_malloc(pz, bmp, 0x0, &size);
101 if (!buffer)
102 return NULL;
103
104 data = stbi_load_from_memory(buffer, size, &x, &y, &comp, 0);
105
106 printf("STBILOAD %p %d %d %d %d\n", data, x, y, comp, x * comp);
107 switch (comp) {
108 #ifdef WORDS_BIGENDIAN
109 case 3:
110 s = SDL_CreateRGBSurfaceFrom((void*) data, x, y, comp * 8, x * comp,
111 0xFF0000, 0xFF00, 0xFF, 0);
112 break;
113 case 4:
114 s = SDL_CreateRGBSurfaceFrom((void*) data, x, y, comp * 8, x * comp,
115 0xFF000000, 0xFF0000, 0xFF00, 0xFF);
116 break;
117 #else
118 case 3:
119 s = SDL_CreateRGBSurfaceFrom((void*) data, x, y, comp * 8, x * comp,
120 0xFF, 0xFF00, 0xFF0000, 0);
121 break;
122 case 4:
123 s = SDL_CreateRGBSurfaceFrom((void*) data, x, y, comp * 8, x * comp,
124 0xFF, 0xFF00, 0xFF0000, 0xFF000000);
125 break;
126 #endif
127 default:
128 printf("RES load STBI: Unhandled bpp surface\n");
129 s = NULL;
130 break;
131 }
132 free(buffer);
133 if (s == NULL)
134 printf("RES load STBI: Couldn't create surface\n");
135 gn_close_zip(pz);
136 return s;
137 }
138 /*
139 * Load a Microsoft BMP from gngeo.dat
140 * return a SDL Surface, NULL on error
141 */
res_load_bmp(char * bmp)142 SDL_Surface *res_load_bmp(char *bmp) {
143 PKZIP *pz;
144 SDL_Surface *s;
145 Uint8 * buffer;
146 SDL_RWops *rw;
147 unsigned int size;
148
149 pz = gn_open_zip(CF_STR(cf_get_item_by_name("datafile")));
150 if (!pz)
151 return NULL;
152 buffer = gn_unzip_file_malloc(pz, bmp, 0x0, &size);
153 if (!buffer) {
154 gn_close_zip(pz);
155 return NULL;
156 }
157
158 rw = SDL_RWFromMem(buffer, size);
159 if (!rw) {
160 printf("Error with SDL_RWFromMem: %s\n", SDL_GetError());
161 gn_close_zip(pz);
162 return NULL;
163 }
164
165 /* The function that does the loading doesn't change at all */
166 s = SDL_LoadBMP_RW(rw, 0);
167 if (!s) {
168 printf("Error loading to SDL_Surface: %s\n", SDL_GetError());
169 gn_close_zip(pz);
170 return NULL;
171 }
172
173 /* Clean up after ourselves */
174 free(buffer);
175 SDL_FreeRW(rw);
176 gn_close_zip(pz);
177 return s;
178 }
res_load_data(char * name)179 void *res_load_data(char *name) {
180 PKZIP *pz;
181 Uint8 * buffer;
182 unsigned int size;
183
184 pz = gn_open_zip(CF_STR(cf_get_item_by_name("datafile")));
185 if (!pz)
186 return NULL;
187 buffer = gn_unzip_file_malloc(pz, name, 0x0, &size);
188 gn_close_zip(pz);
189 return buffer;
190 }
191