deepstone

annotate dosemu/dosemu.c @ 25:5ff8ce78059a

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