foo
authorJohn Tsiombikas <nuclear@member.fsf.org>
Wed, 4 Mar 2020 14:35:49 +0000 (16:35 +0200)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Wed, 4 Mar 2020 14:35:49 +0000 (16:35 +0200)
GNUmakefile [new file with mode: 0644]
Makefile
libs/imago/GNUmakefile [new file with mode: 0644]
src/dos/main.c
src/game.h
src/menuscr.c
src/sdl/gfx.h [new file with mode: 0644]
src/sdl/main.c [new file with mode: 0644]
src/sdl/timer.c [new file with mode: 0644]

diff --git a/GNUmakefile b/GNUmakefile
new file mode 100644 (file)
index 0000000..6bc3c69
--- /dev/null
@@ -0,0 +1,35 @@
+csrc = $(wildcard src/*.c) $(wildcard src/sdl/*.c)
+
+obj = $(csrc:.c=.o)
+dep = $(obj:.o=.d)
+bin = game
+
+inc = -Isrc -Isrc/sdl
+
+CFLAGS = $(arch) -pedantic -Wall -g -MMD $(inc) `sdl-config --cflags`
+LDFLAGS = $(arch) -Llibs/imago -limago `sdl-config --libs` -lm
+
+ifneq ($(shell uname -m), i386)
+       arch = -m32
+       sdl_ldflags = -L/usr/lib/i386-linux-gnu -lSDL-1.2
+else
+       sdl_ldflags = `sdl-config --libs`
+endif
+
+
+$(bin): $(obj) imago
+       $(CC) -o $@ $(obj) $(LDFLAGS)
+
+-include $(dep)
+
+.PHONY: imago
+imago:
+       $(MAKE) -C libs/imago
+
+.PHONY: clean
+clean:
+       rm -f $(obj) $(bin)
+
+.PHONY: cleandep
+cleandep:
+       rm -f $(dep)
index 6cb64ca..c8ed0d2 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -26,7 +26,8 @@ LDFLAGS = option map $(libpath) library { $(libs) }
 
 $(bin): $(obj)
        %write objects.lnk $(obj)
-       $(LD) debug all name $@ system dos4g file { @objects } $(LDFLAGS)
+       %write ldflags.lnk $(LDFLAGS)
+       $(LD) debug all name $@ system dos4g file { @objects } @ldflags
 
 .c: src;src/dos
 .asm: src;src/dos
@@ -45,6 +46,6 @@ clean: .symbolic
 clean: .symbolic
        del src\*.obj
        del src\dos\*.obj
-       del objects.lnk
+       del *.lnk
        del $(bin)
 !endif
diff --git a/libs/imago/GNUmakefile b/libs/imago/GNUmakefile
new file mode 100644 (file)
index 0000000..8940d2a
--- /dev/null
@@ -0,0 +1,15 @@
+src = $(wildcard src/*.c) \
+         $(wildcard zlib/*.c) \
+         $(wildcard libpng/*.c) \
+         $(wildcard jpeglib/*.c)
+obj = $(src:.c=.o)
+alib = libimago.a
+
+CFLAGS = -m32 -Wno-main -g -Izlib -Ilibpng -Ijpeglib
+
+$(alib): $(obj)
+       $(AR) rcs $@ $(obj)
+
+.PHONY: clean
+clean:
+       rm -f $(obj) $(alib)
index 911781c..72b666e 100644 (file)
@@ -46,6 +46,11 @@ int main(int argc, char **argv)
        }
        fb_pixels = (char*)fb_buf + vmode->pitch;
 
+       if(init(argc, argv) == -1) {
+               status = -1;
+               goto break_evloop;
+       }
+
        reset_timer();
 
        for(;;) {
@@ -62,6 +67,7 @@ int main(int argc, char **argv)
 
 break_evloop:
        free(fb_buf);
+       cleanup();
        set_text_mode();
        cleanup_video();
        kb_shutdown();
index d9bfdb6..d247799 100644 (file)
@@ -31,8 +31,8 @@ enum {
        KB_HELP, KB_PRINT, KB_SYSRQ, KB_BREAK
 };
 
-int game_init(int argc, char **argv);
-void game_cleanup(void);
+int init(int argc, char **argv);
+void cleanup(void);
 
 extern void (*draw)(void);
 extern void (*key_event)(int key, int pressed);
index 11abf44..27b9c26 100644 (file)
@@ -10,7 +10,7 @@ static int bgwidth, bgheight;
 
 int menu_init(void)
 {
-       if(!(bgpix = img_load_pixels("data/menpg640.png", &bgwidth, &bgheight, IMG_FMT_RGB24))) {
+       if(!(bgpix = img_load_pixels("data/menbg640.png", &bgwidth, &bgheight, IMG_FMT_RGB24))) {
                fprintf(stderr, "failed to load menu bg image\n");
                return -1;
        }
diff --git a/src/sdl/gfx.h b/src/sdl/gfx.h
new file mode 100644 (file)
index 0000000..d624480
--- /dev/null
@@ -0,0 +1,7 @@
+#ifndef GFX_H_
+#define GFX_H_
+
+void blit_frame(void *pixels, int vsync);
+void wait_vsync(void);
+
+#endif /* GFX_H_ */
diff --git a/src/sdl/main.c b/src/sdl/main.c
new file mode 100644 (file)
index 0000000..9b109c8
--- /dev/null
@@ -0,0 +1,227 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <math.h>
+#include <limits.h>
+#include <SDL/SDL.h>
+#include "game.h"
+#include "timer.h"
+
+#define FB_WIDTH       640
+#define FB_HEIGHT      480
+
+static void handle_event(SDL_Event *ev);
+static void toggle_fullscreen(void);
+
+static int sdlkey_to_gamekey(int sdlkey, unsigned int mod);
+
+
+static int quit;
+static SDL_Surface *fbsurf;
+
+static int fbscale = 2;
+static int xsz, ysz;
+static unsigned int sdl_flags = SDL_SWSURFACE;
+
+
+int main(int argc, char **argv)
+{
+       int s;
+       char *env;
+
+       if((env = getenv("FBSCALE")) && (s = atoi(env))) {
+               fbscale = s;
+               printf("Framebuffer scaling x%d\n", fbscale);
+       }
+
+       xsz = FB_WIDTH * fbscale;
+       ysz = FB_HEIGHT * fbscale;
+
+       /* allocate 1 extra row as a guard band, until we fucking fix the rasterizer */
+       if(!(fb_pixels = malloc(FB_WIDTH * (FB_HEIGHT + 1) * FB_BPP / CHAR_BIT))) {
+               fprintf(stderr, "failed to allocate virtual framebuffer\n");
+               return 1;
+       }
+
+       SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE);
+       if(!(fbsurf = SDL_SetVideoMode(xsz, ysz, FB_BPP, sdl_flags))) {
+               fprintf(stderr, "failed to set video mode %dx%d %dbpp\n", FB_WIDTH, FB_HEIGHT, FB_BPP);
+               free(fb_pixels);
+               SDL_Quit();
+               return 1;
+       }
+       SDL_WM_SetCaption("eradicate/SDL", 0);
+       SDL_ShowCursor(0);
+
+       time_msec = 0;
+       if(init(argc, argv) == -1) {
+               free(fb_pixels);
+               SDL_Quit();
+               return 1;
+       }
+
+       reset_timer();
+
+       while(!quit) {
+               SDL_Event ev;
+               while(SDL_PollEvent(&ev)) {
+                       handle_event(&ev);
+                       if(quit) goto break_evloop;
+               }
+
+               time_msec = get_msec();
+               draw();
+       }
+
+break_evloop:
+       cleanup();
+       SDL_Quit();
+       return 0;
+}
+
+void game_quit(void)
+{
+       quit = 1;
+}
+
+void wait_vsync(void)
+{
+       unsigned long start = SDL_GetTicks();
+       unsigned long until = (start | 0xf) + 1;
+       while(SDL_GetTicks() <= until);
+}
+
+void blit_frame(void *pixels, int vsync)
+{
+       int i, j;
+       unsigned short *sptr, *dptr;
+
+       if(vsync) {
+               wait_vsync();
+       }
+
+       if(SDL_MUSTLOCK(fbsurf)) {
+               SDL_LockSurface(fbsurf);
+       }
+
+       sptr = fb_pixels;
+       dptr = (unsigned short*)fbsurf->pixels + (fbsurf->w - xsz) / 2;
+       for(i=0; i<FB_HEIGHT; i++) {
+               for(j=0; j<FB_WIDTH; j++) {
+                       int x, y;
+                       unsigned short pixel = *sptr++;
+
+                       for(y=0; y<fbscale; y++) {
+                               for(x=0; x<fbscale; x++) {
+                                       dptr[y * fbsurf->w + x] = pixel;
+                               }
+                       }
+                       dptr += fbscale;
+               }
+               dptr += (fbsurf->w - FB_WIDTH) * fbscale;
+       }
+
+       if(SDL_MUSTLOCK(fbsurf)) {
+               SDL_UnlockSurface(fbsurf);
+       }
+       SDL_Flip(fbsurf);
+}
+
+/*
+static int bnmask(int sdlbn)
+{
+       switch(sdlbn) {
+       case SDL_BUTTON_LEFT:
+               return MOUSE_BN_LEFT;
+       case SDL_BUTTON_RIGHT:
+               return MOUSE_BN_RIGHT;
+       case SDL_BUTTON_MIDDLE:
+               return MOUSE_BN_MIDDLE;
+       default:
+               break;
+       }
+       return 0;
+}
+*/
+
+static void handle_event(SDL_Event *ev)
+{
+       int key;
+
+       switch(ev->type) {
+       case SDL_QUIT:
+               quit = 1;
+               break;
+
+       case SDL_KEYDOWN:
+       case SDL_KEYUP:
+               if(ev->key.keysym.sym == SDLK_RETURN && (SDL_GetModState() & KMOD_ALT) &&
+                               ev->key.state == SDL_PRESSED) {
+                       toggle_fullscreen();
+                       break;
+               }
+               key = sdlkey_to_gamekey(ev->key.keysym.sym, ev->key.keysym.mod);
+               key_event(key, ev->key.state == SDL_PRESSED ? 1 : 0);
+               break;
+
+               /*
+       case SDL_MOUSEMOTION:
+               mouse_x = ev->motion.x / fbscale;
+               mouse_y = ev->motion.y / fbscale;
+               break;
+
+       case SDL_MOUSEBUTTONDOWN:
+               mouse_bmask |= bnmask(ev->button.button);
+               if(0) {
+       case SDL_MOUSEBUTTONUP:
+                       mouse_bmask &= ~bnmask(ev->button.button);
+               }
+               mouse_x = ev->button.x / fbscale;
+               mouse_y = ev->button.y / fbscale;
+               break;
+               */
+
+       default:
+               break;
+       }
+}
+
+static void toggle_fullscreen(void)
+{
+       SDL_Surface *newsurf;
+       unsigned int newflags = sdl_flags ^ SDL_FULLSCREEN;
+
+       if(!(newsurf = SDL_SetVideoMode(xsz, ysz, FB_BPP, newflags))) {
+               fprintf(stderr, "failed to go %s\n", newflags & SDL_FULLSCREEN ? "fullscreen" : "windowed");
+               return;
+       }
+
+       fbsurf = newsurf;
+       sdl_flags = newflags;
+}
+
+#define SSORG  '\''
+#define SSEND  '`'
+static char symshift[] = {
+       '"', 0, 0, 0, 0, '<', '_', '>', '?',
+       ')', '!', '@', '#', '$', '%', '^', '&', '*', '(',
+       0, ':', 0, '+', 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       '{', '|', '}', 0, 0, '~'
+};
+
+
+static int sdlkey_to_gamekey(int sdlkey, unsigned int mod)
+{
+       if(sdlkey < 128) {
+               if(mod & (KMOD_SHIFT)) {
+                       if(sdlkey >= 'a' && sdlkey <= 'z') {
+                               sdlkey = toupper(sdlkey);
+                       } else if(sdlkey >= SSORG && sdlkey <= SSEND) {
+                               sdlkey = symshift[sdlkey - SSORG];
+                       }
+               }
+               return sdlkey;
+       }
+       if(sdlkey < 256) return 0;
+       return sdlkey - 128;
+}
diff --git a/src/sdl/timer.c b/src/sdl/timer.c
new file mode 100644 (file)
index 0000000..2f2993b
--- /dev/null
@@ -0,0 +1,23 @@
+#include <SDL/SDL.h>
+#include "timer.h"
+
+static unsigned long start_time;
+
+void init_timer(int res_hz)
+{
+}
+
+void reset_timer(void)
+{
+       start_time = SDL_GetTicks();
+}
+
+unsigned long get_msec(void)
+{
+       return SDL_GetTicks() - start_time;
+}
+
+void sleep_msec(unsigned long msec)
+{
+       SDL_Delay(msec);
+}