deepstone
view dosemu/dosemu.c @ 4:c3e0bccd673e
added texture mapping
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 21 Nov 2011 11:51:23 +0200 |
parents | |
children | bce78aaafc68 |
line source
1 /*
2 256-color 3D graphics hack for real-mode DOS.
3 Copyright (C) 2011 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
19 /* This file implements all calls made to dos-specific code using SDL
20 * Don't ask why ...
21 */
23 #include <stdlib.h>
24 #include <assert.h>
25 #include <SDL.h>
26 #include "vga.h"
27 #include "conio.h"
28 #include "mouse.h"
29 #include "timer.h"
31 static void proc_events(void);
33 /* ----- graphics (vga.c implementation) ----- */
34 static SDL_Surface *fbsurf;
36 #define DOUBLESZ (fbsurf->w != 320)
38 void set_video_mode(int mode)
39 {
40 int resx = 320, resy = 200;
42 if(getenv("DOSEMU_DOUBLESIZE")) {
43 resx *= 2;
44 resy *= 2;
45 }
47 switch(mode) {
48 case 0x13:
49 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
50 if(!(fbsurf = SDL_SetVideoMode(resx, resy, 8, SDL_HWPALETTE))) {
51 fprintf(stderr, "failed to set video mode\n");
52 abort();
53 }
54 SDL_ShowCursor(0);
55 break;
57 case 3:
58 SDL_ShowCursor(1);
59 SDL_Quit();
60 break;
62 default:
63 break;
64 }
65 }
67 void set_palette(unsigned char c, unsigned char r, unsigned char g, unsigned char b)
68 {
69 SDL_Color col;
70 col.r = r;
71 col.g = g;
72 col.b = b;
74 if(SDL_SetPalette(fbsurf, SDL_LOGPAL | SDL_PHYSPAL, &col, c, 1) != 1) {
75 fprintf(stderr, "set_palette failed to set the required color\n");
76 }
77 }
79 void copy_frame(unsigned char *frame)
80 {
81 if(SDL_MUSTLOCK(fbsurf)) {
82 SDL_LockSurface(fbsurf);
83 }
85 if(DOUBLESZ) {
86 int i, j;
87 Uint16 *dest = fbsurf->pixels;
89 for(i=0; i<200; i++) {
90 for(j=0; j<320; j++) {
91 Uint16 twopix = ((Uint16)*frame << 8) | (Uint16)*frame;
92 dest[j] = dest[j + 320] = twopix;
93 frame++;
94 }
95 dest += fbsurf->pitch;
96 }
97 } else {
98 memcpy(fbsurf->pixels, frame, 64000);
99 }
101 if(SDL_MUSTLOCK(fbsurf)) {
102 SDL_UnlockSurface(fbsurf);
103 }
104 SDL_Flip(fbsurf);
105 }
107 void wait_vsync(void)
108 {
109 }
111 /* ----- event handling (conio.h) ----- */
112 static SDL_Event *keybev;
113 static int mousex, mousey, bnmask;
115 int kbhit(void)
116 {
117 if(!keybev) {
118 proc_events();
119 }
120 return keybev != 0;
121 }
123 char getch(void)
124 {
125 char res;
127 while(!keybev) {
128 SDL_Event ev;
129 SDL_WaitEvent(&ev);
130 SDL_PushEvent(&ev);
131 proc_events();
132 }
133 res = keybev->key.keysym.sym;
134 keybev = 0;
135 return res;
136 }
138 /* mouse handling (mouse.c implementation) */
139 int have_mouse(void)
140 {
141 return 1;
142 }
144 int read_mouse(int *xp, int *yp)
145 {
146 if(xp) *xp = mousex;
147 if(yp) *yp = mousey;
148 return bnmask;
149 }
151 static void proc_events(void)
152 {
153 static SDL_Event ev;
155 while(SDL_PollEvent(&ev)) {
156 switch(ev.type) {
157 case SDL_KEYDOWN:
158 keybev = &ev;
159 return;
161 case SDL_MOUSEMOTION:
162 mousex = ev.motion.x;
163 mousey = ev.motion.y;
165 if(DOUBLESZ) {
166 mousex /= 2;
167 mousey /= 2;
168 }
169 break;
171 case SDL_MOUSEBUTTONDOWN:
172 case SDL_MOUSEBUTTONUP:
173 {
174 int mask = 0;
175 switch(ev.button.button) {
176 case SDL_BUTTON_LEFT:
177 mask = MOUSE_LEFT;
178 break;
179 case SDL_BUTTON_MIDDLE:
180 mask = MOUSE_MIDDLE;
181 break;
182 case SDL_BUTTON_RIGHT:
183 mask = MOUSE_RIGHT;
184 default:
185 break;
186 }
187 if(!mask) {
188 break;
189 }
191 if(ev.button.state == SDL_PRESSED) {
192 bnmask |= mask;
193 } else {
194 bnmask &= ~mask;
195 }
196 }
197 break;
199 default:
200 break;
201 }
202 }
203 }
205 /* ---- timer.c implementation ---- */
206 static Uint32 start_time;
208 void init_timer(int res_hz)
209 {
210 reset_timer();
211 }
213 void reset_timer(void)
214 {
215 start_time = SDL_GetTicks();
216 }
218 unsigned long get_msec(void)
219 {
220 return (unsigned long)(SDL_GetTicks() - start_time);
221 }