added backbuffer for level statics; -10% in CPU meter
[awish.git] / src / awish.c
blob5c9e819bf2b3680f9b865518cbb8dcf21daf5259
1 /*
2 * This program is free software: you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation, either version 3 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 #include <ctype.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <unistd.h>
20 #include "SDL.h"
21 #include "SDL_endian.h"
23 #ifdef WIN32
24 # include <windows.h>
25 #endif
27 #include "resfile.h"
28 #include "video.h"
29 #include "mainloop.h"
30 #include "vm.h"
31 #include "gameglobals.h"
32 #include "game.h"
33 #include "title.h"
36 int goobers = 0;
37 int datfirst = 0;
40 ////////////////////////////////////////////////////////////////////////////////
41 static SDL_Surface *loadCFScreen (const char *fname, SDL_Surface *old, int w, int h, int chained) {
42 SDL_Surface *new;
43 static Uint8 pal[256][3];
44 static Uint32 opal[256];
45 static Uint8 scr[64000];
46 int rsz;
47 uint8_t *diskdata = tryDiskFile(fname, &rsz);
49 if (diskdata == NULL) return old;
50 if (rsz < w*h+(chained >= 0 ? 768 : 0)) { free(diskdata); return old; }
52 new = createSurface(320*2, 200*2);
53 if (!new) { free(diskdata); return old; }
54 SDL_SetColorKey(new, 0, 0); // clear colorkey info (it's the fullscreen image after all!)
56 for (int dy = 0; dy < 200; ++dy) {
57 for (int dx = 0; dx < 320; ++dx) {
58 putPixel2x(new, dx, dy, 0);
62 memcpy(scr, diskdata, w*h);
63 if (chained >= 0) memcpy(pal, diskdata+w*h, 768);
64 free(diskdata);
66 if (chained >= 0) {
67 memcpy(opal, palette, 256*4);
68 for (int f = 0; f < 256; ++f) {
69 pal[f][0] <<= 2;
70 if (pal[f][0] != 0) pal[f][0] |= 3;
71 pal[f][1] <<= 2;
72 if (pal[f][1] != 0) pal[f][1] |= 3;
73 pal[f][2] <<= 2;
74 if (pal[f][2] != 0) pal[f][2] |= 3;
75 palette[f] = SDL_MapRGB(screen->format, pal[f][0], pal[f][1], pal[f][2]);
79 if (chained > 0) {
80 for (int dy = 0; dy < h; ++dy) {
81 for (int dx = 0; dx < w/4; ++dx) {
82 for (int c = 0; c < 4; ++c) {
83 putPixel2x(new, dx*4+c, dy, scr[dy*(w/4)+(w*h)/4*c+dx]);
87 } else {
88 for (int dy = 0; dy < h; ++dy) {
89 for (int dx = 0; dx < w; ++dx) {
90 putPixel2x(new, dx, dy, scr[dy*w+dx]);
95 memcpy(palette, opal, 256*4);
96 SDL_FreeSurface(old);
97 return new;
101 ////////////////////////////////////////////////////////////////////////////////
102 static int loadFont (void) {
103 int sz;
104 uint8_t *buf = loadResFile(&resfile, 92, &sz);
106 if (buf == NULL) return -1;
107 if (sz != 256*8) { free(buf); return -1; }
108 memcpy(font, buf, 256*8);
109 free(buf);
110 return 0;
114 static int loadPalette (void) {
115 int sz;
116 uint8_t *buf = loadResFile(&resfile, 83, &sz);
118 if (buf == NULL) return -1;
119 if (sz != 768) { free(buf); return -1; }
120 for (int f = 0; f < 768; ++f) {
121 buf[f] <<= 2;
122 if (buf[f] != 0) buf[f] |= 3;
124 for (int f = 0; f < 256; ++f) palette[f] = SDL_MapRGB(screen->format, buf[f*3+0], buf[f*3+1], buf[f*3+2]);
125 free(buf);
126 return 0;
130 static SDL_Surface *loadImage (ResFile *resfile, int idx) {
131 int sz;
132 static Uint8 *img;
133 SDL_Surface *res;
135 img = loadResFile(resfile, idx, &sz);
136 if (img == NULL) return NULL;
137 if (sz < 320*200) { free(img); return NULL; }
138 res = createSurface(320*2, 200*2);
139 if (res == NULL) { free(img); return NULL; }
140 SDL_SetColorKey(res, 0, 0); // clear colorkey info (it's the fullscreen image after all!)
141 for (int f = 0; f < 320*200; ++f) putPixel2x(res, f%320, f/320, img[f]);
142 free(img);
143 return res;
147 // return # of sprites loaded or <0 on error
148 static int loadSpriteBank (SpriteBank *bank, ResFile *resfile, int idx) {
149 Uint8 *buf;
150 int sz, pos, cnt;
152 if ((buf = loadResFile(resfile, idx, &sz)) == NULL) return -1;
153 if (sz < 3) { free(buf); return -1; }
154 pos = 0;
155 cnt = 0;
156 bank->count = 0;
157 while (pos+4 <= sz) {
158 int h = ((Uint32)buf[pos+0])+256*((Uint32)buf[pos+1]);
159 int w = ((Uint32)buf[pos+2])+256*((Uint32)buf[pos+3]);
161 pos += 4;
162 if (h > 0 && w > 0) {
163 SDL_Surface *s0 = createSurface(w*2, h*2);
164 SDL_Surface *s1 = createSurface(w*2, h*2);
166 if (!s0 || !s1) { free(buf); return -1; }
168 for (int y = 0; y < h; ++y) {
169 for (int x = 0; x < w; ++x) {
170 if (pos < sz) {
171 putPixel2x(s0, x, y, buf[pos]);
172 putPixel2x(s1, w-x-1, y, buf[pos]);
173 ++pos;
177 bank->spr[bank->count][0] = s0;
178 bank->spr[bank->count][1] = s1;
179 ++(bank->count);
182 free(buf);
183 return cnt;
188 static void freeSpriteBank (SpriteBank *bank) {
189 for (int f = bank->count-1; f >= 0; --f) {
190 SDL_FreeSurface(bank->spr[f][1]);
191 SDL_FreeSurface(bank->spr[f][0]);
197 ////////////////////////////////////////////////////////////////////////////////
198 static void quitCleanupRes (void) {
199 vmFreeLabels();
200 deinitResFile(&resfile);
201 //for (int f = 0; f < sizeof(banks)/sizeof(SpriteBank); ++f) freeSpriteBank(&banks[f]);
205 ////////////////////////////////////////////////////////////////////////////////
206 static int awishRST (int tid, int opcode, int argc, int argv[], int *argp[]) {
207 vmGVars[GVAR_RST_RESULT] = 0;
209 if (argv[0] == CONST_FRST_ML_TITLE) {
210 setMainLoopTitle();
211 } else if (argv[0] == CONST_FRST_ML_GAME) {
212 setMainLoopGame();
213 } else if (argv[0] == CONST_FRST_GET_MAX_THREADS) {
214 vmGVars[GVAR_RST_RESULT] = VM_MAX_THREADS;
215 } else if (argv[0] == CONST_FRST_GET_MAX_THREAD_ID) {
216 vmGVars[GVAR_RST_RESULT] = vmLastThread();
217 } else if (argv[0] == CONST_FRST_GET_RAND) {
218 int nmin = 0, nmax = 32767;
220 switch (argc) {
221 case 2: nmax = argv[1]; break;
222 case 3: nmin = argv[1]; nmax = argv[2]; break;
224 if (nmin >= nmax) {
225 vmGVars[GVAR_RST_RESULT] = nmin;
226 } else {
227 vmGVars[GVAR_RST_RESULT] = nmin+(randUInt32()%(nmax-nmin+1));
229 } else if (argv[0] == CONST_FRST_GET_SEED_H) {
230 vmGVars[GVAR_RST_RESULT] = getSeedH();
231 } else if (argv[0] == CONST_FRST_GET_SEED_L) {
232 vmGVars[GVAR_RST_RESULT] = getSeedL();
233 } else if (argv[0] == CONST_FRST_SET_SEED_H) {
234 if (argc >= 2) setSeedH(argv[1]);
235 } else if (argv[0] == CONST_FRST_SET_SEED_L) {
236 if (argc >= 2) setSeedL(argv[1]);
237 } else if (argv[0] == CONST_FRST_DEBUG_PRINT_STR) {
238 int pos = 0, len = 0;
240 switch (argc) {
241 case 1:
242 pos = vmPop(tid);
243 goto dolen;
244 case 2:
245 pos = argv[1];
246 dolen: if (pos >= 0) {
247 while (pos < vmCodeSize && vmCode[pos+len]) ++len;
249 break;
250 case 3:
251 pos = argv[1];
252 len = argv[2];
253 break;
255 if (goobers) {
256 if (pos >= -255 && pos <= -1) {
257 fputc(-pos, stderr);
258 } else {
259 for (; len > 0; --len, ++pos) if (pos >= 0 && pos < vmCodeSize) fputc(vmCode[pos], stderr);
262 } else if (argv[0] == CONST_FRST_DEBUG_PRINT_NUM) {
263 switch (argc) {
264 case 1: argv[1] = vmPop(tid); // fallthru
265 case 2: if (goobers) fprintf(stderr, "%d", argv[1]); break;
266 case 3: if (goobers) fprintf(stderr, "%d,%d", argv[1], argv[2]); break;
268 } else {
269 if (gameRSTCB) return gameRSTCB(tid, opcode, argc, argv, argp);
270 fatal("invalid RST: %d", argv[0]);
272 return 0; // continue
276 ////////////////////////////////////////////////////////////////////////////////
277 static int checkLevelCode (const char *t) {
278 int ctrd;
280 if (!t || !t[0]) return -1;
281 ctrd = vmNewThread(0);
282 for (int level = 0; level < vmGVars[GVAR_MAX_LEVEL]; ++level) {
283 //fprintf(stderr, "%d/%d\n", level+1, vmGVars[GVAR_MAX_LEVEL]);
284 vmPush(ctrd, level);
285 if (vmExecuteBSR(ctrd, CODE_ENTRY_GET_LEVEL_CODE, 0) == 0) {
286 int pos = vmGVars[GVAR_LEVEL_CODE_OFS], len = vmGVars[GVAR_LEVEL_CODE_LEN], ok = 1;
288 if (strlen(t) == len && pos >= 0 && len > 0 && pos+len <= vmCodeSize) {
289 //fwrite(vmCode+pos, len, 1, stderr);
290 //fprintf(stderr, " [%s]\n", t);
291 for (int f = 0; f < len; ++f) {
292 //fprintf(stderr, "%c %c\n", tolower(t[f]), tolower(vmCode[pos+f]));
293 if (tolower(t[f]) != tolower(vmCode[pos+f])) { ok = 0; break; }
295 if (ok) {
296 if (goobers) fprintf(stderr, "found code for level #%02d\n", level+1);
297 return level;
300 } else {
301 if (goobers) fprintf(stderr, "sorry!\n");
302 break;
305 vmKillThread(ctrd);
306 return -1;
310 ////////////////////////////////////////////////////////////////////////////////
311 #ifdef _WIN32
312 # include "cmdline.c"
313 #endif
316 ////////////////////////////////////////////////////////////////////////////////
317 int main (int argc, char *argv[]) {
318 int csz;
320 #ifdef _WIN32
321 cmdLineParse();
322 argc = k8argc;
323 argv = k8argv;
324 #endif
326 for (int f = 1; f < argc; ++f) {
327 int eaten = 1;
329 if (strcmp(argv[f], "-goobers") == 0) goobers = 1;
330 else if (strcmp(argv[f], "-dat") == 0) datfirst = 1;
331 else if (strcmp(argv[f], "-trace") == 0) vmDebugTrace = 1;
332 else if (strcmp(argv[f], "-tracelog") == 0) {
333 eaten = 1;
334 vmDebugTrace = 1;
335 vmDebugOutput = fopen("ztrace.log", "w");
336 } else {
337 eaten = 0;
339 if (eaten > 0) {
340 for (int c = f+eaten; c < argc; ++c) argv[c-1] = argv[c];
341 argv[argc -= eaten] = NULL;
342 f -= eaten;
346 initResFile(&resfile, "RESOURCE.DAT");
347 atexit(quitCleanupRes);
349 if (loadFont() != 0) {
350 fprintf(stderr, "FATAL: can't load font!\n");
351 exit(1);
354 sdlInit();
355 initVideo();
357 if (loadPalette() != 0) {
358 fprintf(stderr, "FATAL: can't load palette!\n");
359 exit(1);
363 SurfaceLock lock;
365 lockSurface(&lock, screen);
366 drawString(screen, "loading...", 2, 200-9, 1);
367 unlockSurface(&lock);
368 SDL_Flip(screen);
371 for (int f = 0; f < 8; ++f) {
372 if ((backs[(f+1)%8] = loadImage(&resfile, f)) == NULL) {
373 fprintf(stderr, "FATAL: can't load image #%d!\n", f);
374 exit(1);
378 backs[0] = loadCFScreen("CFTITLE.DAT", backs[0], 320, 200, 1);
379 backs[0] = loadCFScreen("cftitle.dat", backs[0], 320, 200, 1);
381 memset(banks, 0, sizeof(banks));
382 for (int f = 84; f <= 90; ++f) {
383 if (loadSpriteBank(&banks[f], &resfile, f)) {
384 fprintf(stderr, "FATAL: can't load sprint bank #%d!\n", f);
385 exit(1);
389 if (vmInitialize() != 0) fatal("can't init VM");
391 memset(vmGVars, 0, sizeof(vmGVars));
392 vmRSTCB = awishRST;
394 vmAddLabel("flag_skip_title", LB_GVAR, 120, 1); // 1: public
395 vmGVars[120] = 0;
397 vmCodeSize = 0;
398 if ((csz = loadCodeFile(&resfile, vmCodeSize, 93)) < 1) {
399 fprintf(stderr, "FATAL: can't load VM code!\n");
400 exit(1);
402 vmCodeSize += csz;
403 initLabels();
405 //vmGVars[GVAR_KEY_QUIT] = 0;
406 //vmGVars[GVAR_KEY_START] = 0;
407 vmGVars[GVAR_GOOBERS] = goobers;
409 vmSetPC(0, CODE_ENTRY_TITLE);
410 if (vmExecuteBSR(0, CODE_ENTRY_MAIN_INIT, 0) != 0) fatal("can't initialize game");
411 if (goobers) fprintf(stderr, "MAX LEVEL: %d\n", vmGVars[GVAR_MAX_LEVEL]);
413 if (argc > 1) {
414 // check level code
415 for (int f = 1; f < argc; ++f) {
416 int lvl = checkLevelCode(argv[f]);
418 if (lvl >= 0) {
419 vmGVars[GVAR_START_LEVEL] = lvl;
420 break;
425 mainLoop();
427 return 0;
431 #ifdef WIN32
432 int CALLBACK WinMain (HINSTANCE hInstance, HINSTANCE unused__, LPSTR lpszCmdLine, int nCmdShow) {
433 char *shit[] = { (char *)"shit", NULL };
434 return SDL_main(1, shit);
436 #endif