Mercurial > hg > forks > 3x666-SDL
comparison oxl.c @ 0:0e4f2da58161
Initial import.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 15 Mar 2013 05:00:01 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:0e4f2da58161 |
---|---|
1 /*** OCSALiB 0.0.1alpha -- see oxl.h ***/ | |
2 | |
3 #include "config.h" | |
4 | |
5 #if VIDEO==vga | |
6 #include "vga.h" | |
7 #endif | |
8 #if VIDEO==X11 | |
9 #include "X11/Xlib.h" | |
10 #include "X11/Xutil.h" | |
11 #endif | |
12 | |
13 #if AUDIO | |
14 #include "sys/soundcard.h" | |
15 #include "sys/ioctl.h" | |
16 #endif | |
17 | |
18 #include "stdio.h" | |
19 #include "stdlib.h" | |
20 #include "fcntl.h" | |
21 #include "unistd.h" | |
22 #include "malloc.h" | |
23 #include "time.h" | |
24 #include "sys/time.h" | |
25 | |
26 #ifdef __linux__ | |
27 #include "linux/string.h" | |
28 #else | |
29 #include "string.h" | |
30 #endif | |
31 | |
32 #define DEMOHZ 100 | |
33 | |
34 int TIKLGT; | |
35 | |
36 typedef struct { int r,g,b; } rgb; | |
37 | |
38 /*** gLOBAL vARS ***/ | |
39 | |
40 int AUFREQ=OUTFREQ; | |
41 | |
42 #if AUDIO==1 | |
43 int devdsp; | |
44 char *audiobuf; | |
45 #else | |
46 int baseclock; | |
47 #endif | |
48 | |
49 #if VIDEO==vga | |
50 int SCRH,SCRW; | |
51 #endif | |
52 | |
53 #if VIDEO==X11 | |
54 Display *oxl_disp; | |
55 Visual *oxl_vis; | |
56 GC oxl_gc; | |
57 int oxl_win,oxl_scr; | |
58 Colormap oxl_pal; | |
59 XImage *oxl_im; | |
60 XColor oxl_col[256]; | |
61 #endif | |
62 | |
63 void oxl_init_video() | |
64 { | |
65 printf("oxl: buffer size = %ix%ix8bit\n",BUFW,BUFH); | |
66 | |
67 #if VIDEO==vga | |
68 { int i,fit=0,fitw=9999,fith=9999; | |
69 vga_modeinfo*inf; | |
70 | |
71 if(vga_init()){printf("oxl: can't init svgalib!\n");exit(0);} | |
72 | |
73 /* Finds the best-fitting 256-color videomode */ | |
74 for(i=0;i<GLASTMODE;i++){ | |
75 if(vga_hasmode(i)){ | |
76 inf=vga_getmodeinfo(i); | |
77 if((inf->colors==256)&& | |
78 (inf->width >=BUFW)&&(inf->height>=BUFH)&& | |
79 (inf->width <=fitw)&&(inf->height<=fith)){ | |
80 fith=SCRH=inf->height; | |
81 fitw=SCRW=inf->width; | |
82 fit=i; | |
83 } } } | |
84 | |
85 if(!fit){ | |
86 printf("oxl: no suitable modes found.\n");exit(0);} | |
87 else{ | |
88 printf("oxl: using %s\n",vga_getmodename(fit)); | |
89 vga_setmode(fit); | |
90 } | |
91 } | |
92 #endif | |
93 | |
94 #if VIDEO==X11 | |
95 { XSetWindowAttributes att; | |
96 Window rtwin; | |
97 | |
98 /* get some pointers'n'stuff */ | |
99 if(!(oxl_disp=XOpenDisplay(NULL))){ | |
100 perror("oxl: can't open X display!\n");exit(0);} | |
101 oxl_scr=DefaultScreen(oxl_disp); | |
102 oxl_gc=DefaultGC(oxl_disp,oxl_scr); | |
103 oxl_vis=DefaultVisual(oxl_disp,oxl_scr); | |
104 | |
105 rtwin=DefaultRootWindow(oxl_disp); | |
106 | |
107 /* don't store the contents */ | |
108 att.backing_store=NotUseful; | |
109 | |
110 /* allocate a private colormap */ | |
111 oxl_pal=XCreateColormap(oxl_disp,rtwin,oxl_vis,AllocAll); | |
112 att.colormap=oxl_pal; | |
113 | |
114 /* a black emptiness replaces the cursor */ | |
115 {XColor blk;Pixmap e; | |
116 blk.red=blk.green=blk.blue=0; | |
117 blk.flags=DoRed+DoGreen+DoBlue; | |
118 e=XCreatePixmap(oxl_disp,rtwin,2,2,1); | |
119 att.cursor=XCreatePixmapCursor(oxl_disp,e,e,&blk,&blk,0,0);} | |
120 | |
121 oxl_win=XCreateWindow( | |
122 oxl_disp,rtwin, | |
123 0,0,BUFW,BUFH,4, | |
124 8, /* colordepth */ | |
125 InputOutput, | |
126 oxl_vis, | |
127 CWBackingStore+CWColormap+CWCursor,&att); | |
128 | |
129 /* tell the wm you can't resize it */ | |
130 {XSizeHints shints; | |
131 shints.min_width= shints.max_width= BUFW; | |
132 shints.min_height=shints.max_height=BUFH; | |
133 shints.flags=PMinSize+PMaxSize; | |
134 | |
135 /* set the names etc */ | |
136 XSetStandardProperties( | |
137 oxl_disp,oxl_win, | |
138 WINNAME,WINNAME, | |
139 None,NULL,0, | |
140 &shints); | |
141 } | |
142 | |
143 /* and draw the window */ | |
144 XMapWindow(oxl_disp,oxl_win); | |
145 XSync(oxl_disp,False); | |
146 | |
147 /* create the ximage to be hooked on the frame buffer */ | |
148 oxl_im=XCreateImage(oxl_disp,oxl_vis, | |
149 8,ZPixmap, /* depth, format */ | |
150 0,NULL, /* offset, data */ | |
151 BUFW,BUFH, /* width, height */ | |
152 32,BUFW); /* align bits, bytes per scanline */ | |
153 oxl_im->bitmap_unit=32; | |
154 | |
155 /* some values for the palette */ | |
156 {int i; | |
157 for(i=0;i<256;i++){ | |
158 oxl_col[i].flags=DoRed|DoGreen|DoBlue; | |
159 oxl_col[i].pixel=i; | |
160 oxl_col[i].pad=0; | |
161 }} | |
162 } | |
163 #endif | |
164 } | |
165 | |
166 void oxl_init_audio(int rowtix) | |
167 { | |
168 #if AUDIO==1 | |
169 int tmp; | |
170 printf("oxl: initing oss..\n"); | |
171 | |
172 devdsp=open("/dev/dsp",O_WRONLY|O_NDELAY,0); | |
173 ioctl(devdsp,SNDCTL_DSP_SPEED,&AUFREQ); | |
174 | |
175 tmp=(NUMFRAGS<<16)|FRAGSIZE; | |
176 ioctl(devdsp,SNDCTL_DSP_SETFRAGMENT,&tmp); | |
177 ioctl(devdsp,SNDCTL_DSP_SYNC,0); | |
178 | |
179 tmp=0;ioctl(devdsp,SNDCTL_DSP_STEREO,&tmp); /* mono */ | |
180 tmp=8;ioctl(devdsp,SNDCTL_DSP_SAMPLESIZE,&tmp); /* 8*/ | |
181 | |
182 audiobuf=malloc((rowtix*AUFREQ/DEMOHZ)*sizeof(char)); | |
183 #endif | |
184 } | |
185 | |
186 #if AUDIO==0 | |
187 int klok() /* just an internal clumsie */ | |
188 { | |
189 struct timeval tym; | |
190 gettimeofday(&tym,NULL); | |
191 return (tym.tv_sec*DEMOHZ)+(tym.tv_usec*DEMOHZ/1000000); | |
192 } | |
193 #endif | |
194 | |
195 void oxl_init_timer() | |
196 { | |
197 #if AUDIO==0 | |
198 baseclock=klok(); | |
199 #endif | |
200 } | |
201 | |
202 void oxl_end() | |
203 { | |
204 /* These things should be done automatically on exit but | |
205 let's put them here anyway :) */ | |
206 | |
207 #if AUDIO==1 | |
208 close(devdsp); | |
209 #endif | |
210 | |
211 #if VIDEO==vga | |
212 vga_setmode(TEXT); | |
213 #endif | |
214 | |
215 #if VIDEO==X11 | |
216 XCloseDisplay(oxl_disp); | |
217 #endif | |
218 } | |
219 | |
220 int oxl_timer() | |
221 { | |
222 #if AUDIO==1 | |
223 count_info p; | |
224 ioctl(devdsp,SNDCTL_DSP_GETOPTR,&p); | |
225 return p.bytes/TIKLGT; | |
226 #else | |
227 return (klok()-baseclock); | |
228 #endif | |
229 } | |
230 | |
231 void oxl_doaudio(int*(*player)(void)) | |
232 { | |
233 #if AUDIO==1 | |
234 audio_buf_info a; | |
235 | |
236 for(;;){ | |
237 ioctl(devdsp,SNDCTL_DSP_GETOSPACE,&a); | |
238 if(a.fragments<a.fragstotal/2) break; | |
239 | |
240 {register int i,*s=(*player)(); /* output 8bit */ | |
241 int lgt=*s++;char*d=audiobuf; | |
242 for(i=lgt;i;i--){ | |
243 int x=*s++>>8; | |
244 if(x<0)x=0;else if(x>255)x=255; | |
245 *d++=x; | |
246 } | |
247 write(devdsp,audiobuf,lgt); | |
248 } | |
249 } | |
250 #endif | |
251 } | |
252 | |
253 void oxl_setpalette(rgb*pal) | |
254 { | |
255 #if VIDEO==vga | |
256 int *p=(int*)pal,*c;int pp[256*3],i;c=pp; | |
257 for(i=256*3;i;i--)*c++=*p++>>2; | |
258 vga_setpalvec(0,256,pp); | |
259 #endif | |
260 | |
261 #if VIDEO==X11 | |
262 { int i; | |
263 rgb*p=pal;XColor *c=oxl_col; | |
264 for(i=256;i;i--){ | |
265 c->red=(p->r<<8); | |
266 c->green=(p->g<<8); | |
267 c->blue=(p->b<<8); | |
268 c++;p++; | |
269 } | |
270 XStoreColors(oxl_disp,oxl_pal,oxl_col,256); | |
271 } | |
272 #endif | |
273 } | |
274 | |
275 void oxl_doframe(char *vscr) | |
276 { | |
277 #if VIDEO==vga | |
278 int y0=(SCRH-BUFH)>>1; | |
279 | |
280 #if VGARETRACE==1 | |
281 vga_waitretrace(); | |
282 #endif | |
283 | |
284 if(SCRW==BUFW) | |
285 vga_drawscansegment(vscr,0,y0,BUFW*BUFH); | |
286 else | |
287 { | |
288 int y;char *s=vscr;int x0=(SCRW-BUFW)>>1; | |
289 for(y=y0;y<y0+BUFH;y++){vga_drawscansegment(s,x0,y,BUFW);s+=BUFW;} | |
290 } | |
291 #endif | |
292 | |
293 #if VIDEO==X11 | |
294 oxl_im->data=vscr; | |
295 XPutImage(oxl_disp,oxl_win,oxl_gc,oxl_im,-1,-1,0,0,BUFW,BUFH); | |
296 XSync(oxl_disp,False); | |
297 #endif | |
298 } |