nuclear@0: /* This file implements all calls made to dos-specific code using SDL nuclear@0: * Don't ask why ... nuclear@0: */ nuclear@0: nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@24: #include "wvga.h" nuclear@0: #include "conio.h" nuclear@0: #include "mouse.h" nuclear@34: #include "keyb.h" nuclear@0: #include "timer.h" nuclear@0: nuclear@0: static void proc_events(void); nuclear@0: nuclear@34: static void init_sdl() nuclear@34: { nuclear@34: if(!SDL_WasInit(SDL_INIT_EVERYTHING)) { nuclear@34: SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER); nuclear@34: } nuclear@34: } nuclear@34: nuclear@24: /* ----- graphics (wvga.c implementation) ----- */ nuclear@0: static SDL_Surface *fbsurf; nuclear@28: static int scale = 1; nuclear@0: nuclear@24: int set_video_mode(int mode) nuclear@0: { nuclear@0: int resx = 320, resy = 200; nuclear@9: unsigned int sdl_flags = SDL_HWPALETTE; nuclear@28: char *env; nuclear@0: nuclear@0: if(getenv("DOSEMU_DOUBLESIZE")) { nuclear@28: scale = 2; nuclear@0: } nuclear@0: nuclear@28: if((env = getenv("DOSEMU_SCALE"))) { nuclear@28: int n = atoi(env); nuclear@28: if(n > 0) { nuclear@28: scale = n; nuclear@28: } nuclear@28: } nuclear@28: resx *= scale; nuclear@28: resy *= scale; nuclear@28: nuclear@9: if(getenv("DOSEMU_FULLSCREEN")) { nuclear@9: sdl_flags |= SDL_FULLSCREEN; nuclear@9: } nuclear@9: nuclear@34: init_sdl(); nuclear@34: nuclear@0: switch(mode) { nuclear@0: case 0x13: nuclear@9: if(!(fbsurf = SDL_SetVideoMode(resx, resy, 8, sdl_flags))) { nuclear@0: fprintf(stderr, "failed to set video mode\n"); nuclear@0: abort(); nuclear@0: } nuclear@25: SDL_WM_SetCaption("Deepstone", 0); nuclear@28: /*SDL_ShowCursor(0);*/ nuclear@34: /*SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);*/ nuclear@0: break; nuclear@0: nuclear@0: case 3: nuclear@0: SDL_ShowCursor(1); nuclear@11: SDL_EnableKeyRepeat(0, 0); nuclear@0: SDL_Quit(); nuclear@0: break; nuclear@0: nuclear@0: default: nuclear@0: break; nuclear@0: } nuclear@24: nuclear@24: return 0; nuclear@0: } nuclear@0: nuclear@24: void set_palette(int idx, int *col, int count) nuclear@24: { nuclear@24: int i; nuclear@24: nuclear@24: for(i=0; i 1) { nuclear@28: int i, j, xsz, ysz; nuclear@28: unsigned char *dest = fbsurf->pixels; nuclear@0: nuclear@28: xsz = 320 * scale; nuclear@28: ysz = 200 * scale; nuclear@28: nuclear@28: for(i=0; ipixels, frame, 64000); nuclear@0: } nuclear@0: nuclear@0: if(SDL_MUSTLOCK(fbsurf)) { nuclear@0: SDL_UnlockSurface(fbsurf); nuclear@0: } nuclear@0: SDL_Flip(fbsurf); nuclear@34: nuclear@34: nuclear@34: /* also print fps every second ... */ nuclear@34: { nuclear@34: static long prev_fps, num_frames; nuclear@34: long msec, dt; nuclear@34: nuclear@34: msec = get_msec(); nuclear@34: dt = msec - prev_fps; nuclear@34: if(dt >= 1000) { nuclear@34: float fps = (float)num_frames / ((float)dt / 1000.0f); nuclear@34: printf("framerate: %.1f \r", fps); nuclear@34: fflush(stdout); nuclear@34: num_frames = 0; nuclear@34: prev_fps = msec; nuclear@34: } else { nuclear@34: num_frames++; nuclear@34: } nuclear@34: } nuclear@0: } nuclear@0: nuclear@0: void wait_vsync(void) nuclear@0: { nuclear@0: } nuclear@0: nuclear@0: /* ----- event handling (conio.h) ----- */ nuclear@0: static SDL_Event *keybev; nuclear@0: static int mousex, mousey, bnmask; nuclear@0: nuclear@34: static int keystate[256]; nuclear@34: static int num_pressed; nuclear@34: nuclear@0: int kbhit(void) nuclear@0: { nuclear@0: if(!keybev) { nuclear@0: proc_events(); nuclear@0: } nuclear@0: return keybev != 0; nuclear@0: } nuclear@0: nuclear@9: int getch(void) nuclear@0: { nuclear@9: int res; nuclear@0: nuclear@0: while(!keybev) { nuclear@0: SDL_Event ev; nuclear@0: SDL_WaitEvent(&ev); nuclear@0: SDL_PushEvent(&ev); nuclear@0: proc_events(); nuclear@0: } nuclear@0: res = keybev->key.keysym.sym; nuclear@0: keybev = 0; nuclear@0: return res; nuclear@0: } nuclear@0: nuclear@34: /* ----- improved event handling (keyb.h) ---- */ nuclear@34: nuclear@34: int kb_init(int bufsz) nuclear@34: { nuclear@34: init_sdl(); nuclear@34: nuclear@34: return 0; nuclear@34: } nuclear@34: nuclear@34: void kb_shutdown(void) nuclear@34: { nuclear@34: } nuclear@34: nuclear@34: int kb_getkey(void) nuclear@34: { nuclear@34: int res = -1; nuclear@34: nuclear@34: proc_events(); nuclear@34: if(keybev) { nuclear@34: res = keybev->key.keysym.sym; nuclear@34: keybev = 0; nuclear@34: } nuclear@34: return res; nuclear@34: } nuclear@34: nuclear@34: int kb_isdown(int key) nuclear@34: { nuclear@34: if(key == KB_ANY) { nuclear@34: return num_pressed; nuclear@34: } nuclear@34: return keystate[key]; nuclear@34: } nuclear@34: nuclear@0: /* mouse handling (mouse.c implementation) */ nuclear@34: static unsigned long last_mouse_hide_time; nuclear@34: nuclear@0: int have_mouse(void) nuclear@0: { nuclear@0: return 1; nuclear@0: } nuclear@0: nuclear@34: void set_mouse(int x, int y) nuclear@34: { nuclear@34: SDL_ShowCursor(0); nuclear@34: last_mouse_hide_time = get_msec(); nuclear@34: nuclear@34: SDL_WarpMouse(x * scale, y * scale); nuclear@34: mousex = x; nuclear@34: mousey = y; nuclear@34: } nuclear@34: nuclear@0: int read_mouse(int *xp, int *yp) nuclear@0: { nuclear@0: if(xp) *xp = mousex; nuclear@0: if(yp) *yp = mousey; nuclear@0: return bnmask; nuclear@0: } nuclear@0: nuclear@0: static void proc_events(void) nuclear@0: { nuclear@0: static SDL_Event ev; nuclear@0: nuclear@34: if(last_mouse_hide_time > 0 && get_msec() - last_mouse_hide_time > 3000) { nuclear@34: last_mouse_hide_time = 0; nuclear@34: SDL_ShowCursor(1); nuclear@34: } nuclear@34: nuclear@0: while(SDL_PollEvent(&ev)) { nuclear@0: switch(ev.type) { nuclear@0: case SDL_KEYDOWN: nuclear@34: { nuclear@34: int key = ev.key.keysym.sym; nuclear@34: nuclear@34: if(!keybev) { nuclear@34: keybev = &ev; nuclear@34: } nuclear@34: nuclear@34: if(!keystate[key]) { nuclear@34: keystate[key] = 1; nuclear@34: num_pressed++; nuclear@34: } nuclear@34: } nuclear@34: break; nuclear@34: nuclear@34: case SDL_KEYUP: nuclear@34: { nuclear@34: int key = ev.key.keysym.sym; nuclear@34: nuclear@34: if(keystate[key]) { nuclear@34: keystate[key] = 0; nuclear@34: if(--num_pressed < 0) { nuclear@34: num_pressed = 0; nuclear@34: } nuclear@34: } nuclear@34: } nuclear@34: break; nuclear@0: nuclear@0: case SDL_MOUSEMOTION: nuclear@28: mousex = ev.motion.x / scale; nuclear@28: mousey = ev.motion.y / scale; nuclear@0: break; nuclear@0: nuclear@0: case SDL_MOUSEBUTTONDOWN: nuclear@0: case SDL_MOUSEBUTTONUP: nuclear@0: { nuclear@0: int mask = 0; nuclear@0: switch(ev.button.button) { nuclear@0: case SDL_BUTTON_LEFT: nuclear@0: mask = MOUSE_LEFT; nuclear@0: break; nuclear@0: case SDL_BUTTON_MIDDLE: nuclear@0: mask = MOUSE_MIDDLE; nuclear@0: break; nuclear@0: case SDL_BUTTON_RIGHT: nuclear@0: mask = MOUSE_RIGHT; nuclear@0: default: nuclear@0: break; nuclear@0: } nuclear@0: if(!mask) { nuclear@0: break; nuclear@0: } nuclear@0: nuclear@0: if(ev.button.state == SDL_PRESSED) { nuclear@0: bnmask |= mask; nuclear@0: } else { nuclear@0: bnmask &= ~mask; nuclear@0: } nuclear@0: } nuclear@0: break; nuclear@0: nuclear@0: default: nuclear@0: break; nuclear@0: } nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: /* ---- timer.c implementation ---- */ nuclear@0: static Uint32 start_time; nuclear@0: nuclear@0: void init_timer(int res_hz) nuclear@0: { nuclear@34: init_sdl(); nuclear@0: reset_timer(); nuclear@0: } nuclear@0: nuclear@0: void reset_timer(void) nuclear@0: { nuclear@0: start_time = SDL_GetTicks(); nuclear@34: printf("resetting timer: %u, %lu\n", start_time, get_msec()); nuclear@0: } nuclear@0: nuclear@0: unsigned long get_msec(void) nuclear@0: { nuclear@34: Uint32 ticks = SDL_GetTicks(); nuclear@34: return (unsigned long)(ticks - start_time); nuclear@0: }