deepstone

view dosemu/dosemu.c @ 24:ad6b185723cd

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