deepstone

annotate dosemu/dosemu.c @ 33:03a0b307706a

added proper keyboard handling
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 23 Sep 2013 04:34:43 +0300
parents 5ff8ce78059a
children c6406e4aa0fb
rev   line source
nuclear@0 1 /* This file implements all calls made to dos-specific code using SDL
nuclear@0 2 * Don't ask why ...
nuclear@0 3 */
nuclear@0 4
nuclear@0 5 #include <stdlib.h>
nuclear@0 6 #include <assert.h>
nuclear@0 7 #include <SDL.h>
nuclear@24 8 #include "wvga.h"
nuclear@0 9 #include "conio.h"
nuclear@0 10 #include "mouse.h"
nuclear@0 11 #include "timer.h"
nuclear@0 12
nuclear@0 13 static void proc_events(void);
nuclear@0 14
nuclear@24 15 /* ----- graphics (wvga.c implementation) ----- */
nuclear@0 16 static SDL_Surface *fbsurf;
nuclear@28 17 static int scale = 1;
nuclear@0 18
nuclear@24 19 int set_video_mode(int mode)
nuclear@0 20 {
nuclear@0 21 int resx = 320, resy = 200;
nuclear@9 22 unsigned int sdl_flags = SDL_HWPALETTE;
nuclear@28 23 char *env;
nuclear@0 24
nuclear@0 25 if(getenv("DOSEMU_DOUBLESIZE")) {
nuclear@28 26 scale = 2;
nuclear@0 27 }
nuclear@0 28
nuclear@28 29 if((env = getenv("DOSEMU_SCALE"))) {
nuclear@28 30 int n = atoi(env);
nuclear@28 31 if(n > 0) {
nuclear@28 32 scale = n;
nuclear@28 33 }
nuclear@28 34 }
nuclear@28 35 resx *= scale;
nuclear@28 36 resy *= scale;
nuclear@28 37
nuclear@9 38 if(getenv("DOSEMU_FULLSCREEN")) {
nuclear@9 39 sdl_flags |= SDL_FULLSCREEN;
nuclear@9 40 }
nuclear@9 41
nuclear@0 42 switch(mode) {
nuclear@0 43 case 0x13:
nuclear@0 44 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
nuclear@9 45 if(!(fbsurf = SDL_SetVideoMode(resx, resy, 8, sdl_flags))) {
nuclear@0 46 fprintf(stderr, "failed to set video mode\n");
nuclear@0 47 abort();
nuclear@0 48 }
nuclear@25 49 SDL_WM_SetCaption("Deepstone", 0);
nuclear@28 50 /*SDL_ShowCursor(0);*/
nuclear@11 51 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
nuclear@0 52 break;
nuclear@0 53
nuclear@0 54 case 3:
nuclear@0 55 SDL_ShowCursor(1);
nuclear@11 56 SDL_EnableKeyRepeat(0, 0);
nuclear@0 57 SDL_Quit();
nuclear@0 58 break;
nuclear@0 59
nuclear@0 60 default:
nuclear@0 61 break;
nuclear@0 62 }
nuclear@24 63
nuclear@24 64 return 0;
nuclear@0 65 }
nuclear@0 66
nuclear@24 67 void set_palette(int idx, int *col, int count)
nuclear@24 68 {
nuclear@24 69 int i;
nuclear@24 70
nuclear@24 71 for(i=0; i<count; i++) {
nuclear@24 72 set_pal_entry(idx + i, col[0], col[1], col[2]);
nuclear@24 73 col += 3;
nuclear@24 74 }
nuclear@24 75 }
nuclear@24 76
nuclear@24 77 void set_pal_entry(int idx, int r, int g, int b)
nuclear@0 78 {
nuclear@0 79 SDL_Color col;
nuclear@0 80 col.r = r;
nuclear@0 81 col.g = g;
nuclear@0 82 col.b = b;
nuclear@0 83
nuclear@24 84 if(SDL_SetPalette(fbsurf, SDL_LOGPAL | SDL_PHYSPAL, &col, idx, 1) != 1) {
nuclear@0 85 fprintf(stderr, "set_palette failed to set the required color\n");
nuclear@0 86 }
nuclear@0 87 }
nuclear@0 88
nuclear@24 89 void copy_frame(void *pixels)
nuclear@0 90 {
nuclear@24 91 unsigned char *frame = (unsigned char*)pixels;
nuclear@24 92
nuclear@0 93 if(SDL_MUSTLOCK(fbsurf)) {
nuclear@0 94 SDL_LockSurface(fbsurf);
nuclear@0 95 }
nuclear@0 96
nuclear@28 97 if(scale > 1) {
nuclear@28 98 int i, j, xsz, ysz;
nuclear@28 99 unsigned char *dest = fbsurf->pixels;
nuclear@0 100
nuclear@28 101 xsz = 320 * scale;
nuclear@28 102 ysz = 200 * scale;
nuclear@28 103
nuclear@28 104 for(i=0; i<ysz; i++) {
nuclear@28 105 for(j=0; j<xsz; j++) {
nuclear@28 106 *dest++ = frame[(i / scale) * 320 + (j / scale)];
nuclear@0 107 }
nuclear@0 108 }
nuclear@0 109 } else {
nuclear@0 110 memcpy(fbsurf->pixels, frame, 64000);
nuclear@0 111 }
nuclear@0 112
nuclear@0 113 if(SDL_MUSTLOCK(fbsurf)) {
nuclear@0 114 SDL_UnlockSurface(fbsurf);
nuclear@0 115 }
nuclear@0 116 SDL_Flip(fbsurf);
nuclear@0 117 }
nuclear@0 118
nuclear@0 119 void wait_vsync(void)
nuclear@0 120 {
nuclear@0 121 }
nuclear@0 122
nuclear@0 123 /* ----- event handling (conio.h) ----- */
nuclear@0 124 static SDL_Event *keybev;
nuclear@0 125 static int mousex, mousey, bnmask;
nuclear@0 126
nuclear@0 127 int kbhit(void)
nuclear@0 128 {
nuclear@0 129 if(!keybev) {
nuclear@0 130 proc_events();
nuclear@0 131 }
nuclear@0 132 return keybev != 0;
nuclear@0 133 }
nuclear@0 134
nuclear@9 135 int getch(void)
nuclear@0 136 {
nuclear@9 137 int res;
nuclear@0 138
nuclear@0 139 while(!keybev) {
nuclear@0 140 SDL_Event ev;
nuclear@0 141 SDL_WaitEvent(&ev);
nuclear@0 142 SDL_PushEvent(&ev);
nuclear@0 143 proc_events();
nuclear@0 144 }
nuclear@0 145 res = keybev->key.keysym.sym;
nuclear@0 146 keybev = 0;
nuclear@0 147 return res;
nuclear@0 148 }
nuclear@0 149
nuclear@0 150 /* mouse handling (mouse.c implementation) */
nuclear@0 151 int have_mouse(void)
nuclear@0 152 {
nuclear@0 153 return 1;
nuclear@0 154 }
nuclear@0 155
nuclear@0 156 int read_mouse(int *xp, int *yp)
nuclear@0 157 {
nuclear@0 158 if(xp) *xp = mousex;
nuclear@0 159 if(yp) *yp = mousey;
nuclear@0 160 return bnmask;
nuclear@0 161 }
nuclear@0 162
nuclear@0 163 static void proc_events(void)
nuclear@0 164 {
nuclear@0 165 static SDL_Event ev;
nuclear@0 166
nuclear@0 167 while(SDL_PollEvent(&ev)) {
nuclear@0 168 switch(ev.type) {
nuclear@0 169 case SDL_KEYDOWN:
nuclear@0 170 keybev = &ev;
nuclear@0 171 return;
nuclear@0 172
nuclear@0 173 case SDL_MOUSEMOTION:
nuclear@28 174 mousex = ev.motion.x / scale;
nuclear@28 175 mousey = ev.motion.y / scale;
nuclear@0 176 break;
nuclear@0 177
nuclear@0 178 case SDL_MOUSEBUTTONDOWN:
nuclear@0 179 case SDL_MOUSEBUTTONUP:
nuclear@0 180 {
nuclear@0 181 int mask = 0;
nuclear@0 182 switch(ev.button.button) {
nuclear@0 183 case SDL_BUTTON_LEFT:
nuclear@0 184 mask = MOUSE_LEFT;
nuclear@0 185 break;
nuclear@0 186 case SDL_BUTTON_MIDDLE:
nuclear@0 187 mask = MOUSE_MIDDLE;
nuclear@0 188 break;
nuclear@0 189 case SDL_BUTTON_RIGHT:
nuclear@0 190 mask = MOUSE_RIGHT;
nuclear@0 191 default:
nuclear@0 192 break;
nuclear@0 193 }
nuclear@0 194 if(!mask) {
nuclear@0 195 break;
nuclear@0 196 }
nuclear@0 197
nuclear@0 198 if(ev.button.state == SDL_PRESSED) {
nuclear@0 199 bnmask |= mask;
nuclear@0 200 } else {
nuclear@0 201 bnmask &= ~mask;
nuclear@0 202 }
nuclear@0 203 }
nuclear@0 204 break;
nuclear@0 205
nuclear@0 206 default:
nuclear@0 207 break;
nuclear@0 208 }
nuclear@0 209 }
nuclear@0 210 }
nuclear@0 211
nuclear@0 212 /* ---- timer.c implementation ---- */
nuclear@0 213 static Uint32 start_time;
nuclear@0 214
nuclear@0 215 void init_timer(int res_hz)
nuclear@0 216 {
nuclear@0 217 reset_timer();
nuclear@0 218 }
nuclear@0 219
nuclear@0 220 void reset_timer(void)
nuclear@0 221 {
nuclear@0 222 start_time = SDL_GetTicks();
nuclear@0 223 }
nuclear@0 224
nuclear@0 225 unsigned long get_msec(void)
nuclear@0 226 {
nuclear@0 227 return (unsigned long)(SDL_GetTicks() - start_time);
nuclear@0 228 }