deepstone

view dosemu/dosemu.c @ 26:61d97b17cd2b

shortened the name fixed_point.c/h to fixedp
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 22 Sep 2013 02:25:41 +0300
parents ad6b185723cd
children 11d14f688485
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_WM_SetCaption("Deepstone", 0);
42 SDL_ShowCursor(0);
43 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
44 break;
46 case 3:
47 SDL_ShowCursor(1);
48 SDL_EnableKeyRepeat(0, 0);
49 SDL_Quit();
50 break;
52 default:
53 break;
54 }
56 return 0;
57 }
59 void set_palette(int idx, int *col, int count)
60 {
61 int i;
63 for(i=0; i<count; i++) {
64 set_pal_entry(idx + i, col[0], col[1], col[2]);
65 col += 3;
66 }
67 }
69 void set_pal_entry(int idx, int r, int g, int b)
70 {
71 SDL_Color col;
72 col.r = r;
73 col.g = g;
74 col.b = b;
76 if(SDL_SetPalette(fbsurf, SDL_LOGPAL | SDL_PHYSPAL, &col, idx, 1) != 1) {
77 fprintf(stderr, "set_palette failed to set the required color\n");
78 }
79 }
81 void copy_frame(void *pixels)
82 {
83 unsigned char *frame = (unsigned char*)pixels;
85 if(SDL_MUSTLOCK(fbsurf)) {
86 SDL_LockSurface(fbsurf);
87 }
89 if(DOUBLESZ) {
90 int i, j;
91 Uint16 *dest = fbsurf->pixels;
93 for(i=0; i<200; i++) {
94 for(j=0; j<320; j++) {
95 Uint16 twopix = ((Uint16)*frame << 8) | (Uint16)*frame;
96 dest[j] = dest[j + 320] = twopix;
97 frame++;
98 }
99 dest += fbsurf->pitch;
100 }
101 } else {
102 memcpy(fbsurf->pixels, frame, 64000);
103 }
105 if(SDL_MUSTLOCK(fbsurf)) {
106 SDL_UnlockSurface(fbsurf);
107 }
108 SDL_Flip(fbsurf);
109 }
111 void wait_vsync(void)
112 {
113 }
115 /* ----- event handling (conio.h) ----- */
116 static SDL_Event *keybev;
117 static int mousex, mousey, bnmask;
119 int kbhit(void)
120 {
121 if(!keybev) {
122 proc_events();
123 }
124 return keybev != 0;
125 }
127 int getch(void)
128 {
129 int res;
131 while(!keybev) {
132 SDL_Event ev;
133 SDL_WaitEvent(&ev);
134 SDL_PushEvent(&ev);
135 proc_events();
136 }
137 res = keybev->key.keysym.sym;
138 keybev = 0;
139 return res;
140 }
142 /* mouse handling (mouse.c implementation) */
143 int have_mouse(void)
144 {
145 return 1;
146 }
148 int read_mouse(int *xp, int *yp)
149 {
150 if(xp) *xp = mousex;
151 if(yp) *yp = mousey;
152 return bnmask;
153 }
155 static void proc_events(void)
156 {
157 static SDL_Event ev;
159 while(SDL_PollEvent(&ev)) {
160 switch(ev.type) {
161 case SDL_KEYDOWN:
162 keybev = &ev;
163 return;
165 case SDL_MOUSEMOTION:
166 mousex = ev.motion.x;
167 mousey = ev.motion.y;
169 if(DOUBLESZ) {
170 mousex /= 2;
171 mousey /= 2;
172 }
173 break;
175 case SDL_MOUSEBUTTONDOWN:
176 case SDL_MOUSEBUTTONUP:
177 {
178 int mask = 0;
179 switch(ev.button.button) {
180 case SDL_BUTTON_LEFT:
181 mask = MOUSE_LEFT;
182 break;
183 case SDL_BUTTON_MIDDLE:
184 mask = MOUSE_MIDDLE;
185 break;
186 case SDL_BUTTON_RIGHT:
187 mask = MOUSE_RIGHT;
188 default:
189 break;
190 }
191 if(!mask) {
192 break;
193 }
195 if(ev.button.state == SDL_PRESSED) {
196 bnmask |= mask;
197 } else {
198 bnmask &= ~mask;
199 }
200 }
201 break;
203 default:
204 break;
205 }
206 }
207 }
209 /* ---- timer.c implementation ---- */
210 static Uint32 start_time;
212 void init_timer(int res_hz)
213 {
214 reset_timer();
215 }
217 void reset_timer(void)
218 {
219 start_time = SDL_GetTicks();
220 }
222 unsigned long get_msec(void)
223 {
224 return (unsigned long)(SDL_GetTicks() - start_time);
225 }