/[projet1]/public/pc/emulators/caloric/src/sdl.c
Defence Force logotype

Contents of /public/pc/emulators/caloric/src/sdl.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 68 - (show annotations)
Sun May 17 18:19:15 2009 UTC (10 years, 7 months ago) by Jede
File MIME type: text/plain
File size: 10812 byte(s)
Ajout lignes pour version debug
1 /*
2 * sdl.c - SDL interface for Euphoric
3 * (c) 2007 Jean-Yves Lamoureux
4 */
5
6 /*
7 This file is copyright Jean-Yves Lamoureux 2007.
8
9 This program is free software; you can redistribute it and/or modify it under
10 the terms of version 2 of the GNU General Public License as published by the
11 Free Software Foundation.
12
13 This program is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License along with
18 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 Place, Suite 330, Boston, MA 02111-1307, USA.
20 */
21
22 #include "config.h"
23 #if HAVE_SDL
24 #include <ctype.h>
25 #include <errno.h>
26 #include <limits.h>
27 #include <stdarg.h>
28 #include <stddef.h>
29 #include <stdint.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <time.h>
34 #include <unistd.h>
35
36 #include <SDL/SDL.h>
37 #include <SDL/SDL_timer.h>
38
39 #include "caloric.h"
40 #include "hosttraps.h"
41 #include "stats.h"
42 #include "dsp.h"
43
44 extern volatile unsigned char Tick;
45 static long palette[8]={0,0xFF0000,0x00FF00,0xFFFF00,0x0000FF,0xFF00FF,0x00FFFF,0xFFFFFF};
46 extern int zoom;
47 int zoomChange ;
48 char windowChanging = 0;
49 uint8_t scanlineEmulation = 0;
50 uint8_t oldEffect = 0;
51 uint8_t setfullScreen = 0, fullScreen = 0;
52
53
54
55 SDL_Surface *screen;
56
57 void display_frame (void);
58
59 int sdl_start()
60 {
61 char title[1024];
62 Uint32 flags = SDL_INIT_VIDEO|SDL_INIT_NOPARACHUTE|SDL_INIT_TIMER;
63 Uint32 flags_video= SDL_HWPALETTE; // Correcting full screen
64 zoomChange = zoom;
65 SDL_Rect **modes;
66 int i;
67 windowChanging=1;
68
69 /* Init video blah */
70 /*Get some infos from video card and resolution*/
71
72
73
74 /*
75 typedef struct{
76 Uint32 hw_available:1;
77 Uint32 wm_available:1;
78 Uint32 blit_hw:1;
79 Uint32 blit_hw_CC:1;
80 Uint32 blit_hw_A:1;
81 Uint32 blit_sw:1;
82 Uint32 blit_sw_CC:1;
83 Uint32 blit_sw_A:1;
84 Uint32 blit_fill;
85 Uint32 video_mem;
86 SDL_PixelFormat *vfmt;
87 } SDL_VideoInfo;
88
89 Structure Data
90
91 hw_available Is it possible to create hardware surfaces?
92 wm_available Is there a window manager available
93 blit_hw Are hardware to hardware blits accelerated?
94 blit_hw_CC Are hardware to hardware colorkey blits accelerated?
95 blit_hw_A Are hardware to hardware alpha blits accelerated?
96 blit_sw Are software to hardware blits accelerated?
97 blit_sw_CC Are software to hardware colorkey blits accelerated?
98 blit_sw_A Are software to hardware alpha blits accelerated?
99 blit_fill Are color fills accelerated?
100 video_mem Total amount of video memory in Kilobytes
101 vfmt Pixel format of the video device
102 */
103 // printf("Can we start a hardware surface : %d\n",vid_info->video_mem);
104 zoom=2;
105 if(fullScreen) {
106 //flags |= SDL_FULLSCREEN;
107 flags_video ^= SDL_FULLSCREEN;
108 zoom=2; // We set zoom, we don't care zoom>1 if it's in full screen
109 fprintf(stderr, "Full!\n");
110 }
111 if(audio_method == AM_SDL)
112 flags |= SDL_INIT_AUDIO;
113 if (SDL_Init(flags) < 0) {
114 fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
115 return 0;
116 }
117
118 const SDL_VideoInfo *pVideoInfo = SDL_GetVideoInfo();
119
120 if (!pVideoInfo) {
121 printf("\n\t SDL_GetVideoInfo return NULL\n");
122 }
123 #if HAVE_DEBUG_MODE
124 else {
125
126
127 printf("\thardware surfaces : %d\n", pVideoInfo->hw_available);
128 printf("\twindow manager available : %d\n", pVideoInfo->wm_available);
129 printf("\thardware to hardware blits : %d\n", pVideoInfo->blit_hw);
130 printf("\thardware to hardware colorkey blits : %d\n", pVideoInfo->blit_hw_CC);
131 printf("\thardware to hardware alpha blits : %d\n", pVideoInfo->blit_hw_A);
132 printf("\tsoftware to hardware blits : %d\n", pVideoInfo->blit_sw);
133 printf("\tsoftware to hardware colorkey blits : %d\n", pVideoInfo->blit_sw_CC);
134 printf("\tsoftware to hardware alpha blits : %d\n", pVideoInfo->blit_sw_A);
135 printf("\tcolor fills accelerated : %d\n", pVideoInfo->blit_fill);
136 printf("\tTotal video memory Kilobytes : %d\n", pVideoInfo->video_mem);
137 }
138
139 #endif
140 modes=SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_HWSURFACE);
141
142 /* Check is there are any modes available */
143 if(modes == (SDL_Rect **)0){
144 printf("No modes available!\n");
145 exit(-1);
146 }
147
148 /* Check if or resolution is restricted */
149 if(modes == (SDL_Rect **)-1){
150 printf("All resolutions available.\n");
151 }
152 else{
153 /* Print valid modes */
154 printf("Available Modes\n");
155 for(i=0;modes[i];++i)
156 printf(" %d x %d\n", modes[i]->w, modes[i]->h);
157 }
158
159
160
161 /* 320x240x32 */
162 screen = SDL_SetVideoMode(VIDEO_WIDTH*zoom, VIDEO_HEIGHT*zoom, 32, flags_video); //
163 if ( screen == NULL ) {
164 fprintf(stderr, "Unable to set %dx%dx32 video: %s\n", VIDEO_WIDTH*zoom, VIDEO_HEIGHT*zoom, SDL_GetError());
165 return 0;
166 }
167
168 sprintf(title, "Caloric %s", VERSION);
169 SDL_WM_SetCaption(title, NULL);
170
171 windowChanging=0;
172 return 1;
173 }
174
175 void sdl_end()
176 {
177 windowChanging=1;
178 SDL_FreeSurface(screen);
179 screen = NULL;
180 SDL_QuitSubSystem(SDL_INIT_VIDEO);
181 SDL_Quit();
182 windowChanging=0;
183 }
184
185 void sdl_display (void)
186 {
187 windowChanging=1;
188 SDL_Flip(screen);
189 windowChanging=0;
190 }
191
192 void Reset_Screen (void)
193 {
194 display_frame ();
195 }
196
197 #define GET_R(a) ((a&0x00FF0000)>>16)&0xFF
198 #define GET_G(a) ((a&0x0000FF00)>>8)&0xFF
199 #define GET_B(a) (a&0x000000FF)&0xFF
200 #define RGB(r, g, b) (r<<16) | (g<<8) | (b)
201
202 void display_frame (void)
203 {
204 int x, y;
205 int i = 0;
206 int j = 0;
207 uint8_t *src;
208 uint32_t *dst;
209 static uint32_t *tmp = NULL;
210 static uint32_t *tmpback;
211 uint8_t *bs;
212 /*
213 if(zoomChange != zoom) {
214 sdl_end();
215 zoom = zoomChange;
216 sdl_start();
217 }
218 */
219 if(setfullScreen != fullScreen) {
220 fullScreen = setfullScreen;
221 /*
222 flags ^= SDL_FULLSCREEN;
223 screen = SDL_SetVideoMode(..., flags);
224 SDL_Surface *SDL_SetVideoMode (int width, int height, int bpp, Uint32 flags);
225 */
226 sdl_end();
227 sdl_start();
228 }
229
230 render_frame ();
231
232
233 /* Xeuphoric uses 1 byte at the end of each line,
234 indicating a state change.
235 We don't need that anymore */
236
237 if(scanlineEmulation == 0) {
238 src = (uint8_t *)buf;
239 dst = (uint32_t *)screen->pixels;
240
241
242 oldEffect = 0;
243 if(zoom == 1) {
244 for(y=0; y<VIDEO_HEIGHT; y++) {
245 for(x=0; x<VIDEO_WIDTH; x++) {
246 *dst = palette[*src];
247 dst++;
248 src++;
249 }
250 src++;
251 }
252 } else {
253
254 for(y=0; y<VIDEO_HEIGHT; y++) {
255 for(i = 0; i < zoom; i++) {
256 bs = src;
257 for(x=0; x<VIDEO_WIDTH; x++) {
258 for(j = 0; j < zoom; j++) {
259 *dst = palette[*src];
260 dst++;
261 }
262 src++;
263 }
264 src = bs;
265 }
266 src+=(VIDEO_WIDTH+1);
267 }
268 }
269 } else {
270 /* Scanline emulation */
271 if(oldEffect == 0) {
272 if(tmp != NULL) {
273 free(tmp);
274 }
275 sdl_end();
276 zoom = 2;
277 sdl_start();
278 oldEffect = 1;
279 if(tmp == NULL) {
280 tmp = (uint32_t*)malloc((VIDEO_HEIGHT*2)*(VIDEO_WIDTH*2) * 4);
281 tmpback = tmp;
282 }
283 } else {
284 }
285
286 src = (uint8_t *)buf;
287 dst = (uint32_t *)screen->pixels;
288
289
290 /* Double the screen, making 1 scanline of 2 black */
291 for(y=0; y<VIDEO_HEIGHT; y++) {
292 for(x=0; x<VIDEO_WIDTH; x++) {
293 for(j = 0; j < zoom; j++) {
294 *dst = palette[*src];
295 dst++;
296 }
297 src++;
298 }
299 dst+=(VIDEO_WIDTH*2);
300 src++;
301 }
302
303 /* Blur midline */
304 dst = (uint32_t *)screen->pixels;
305 dst+=VIDEO_WIDTH*2;
306 for(y=1; y < (VIDEO_HEIGHT*2)-1; y+=2) {
307 for(x=0; x<VIDEO_WIDTH*2; x++) {
308 uint8_t r1 = GET_R(*(dst-(VIDEO_WIDTH*2)));
309 uint8_t g1 = GET_G(*(dst-(VIDEO_WIDTH*2)));
310 uint8_t b1 = GET_B(*(dst-(VIDEO_WIDTH*2)));
311 uint8_t r2 = GET_R(*(dst+(VIDEO_WIDTH*2)));
312 uint8_t g2 = GET_G(*(dst+(VIDEO_WIDTH*2)));
313 uint8_t b2 = GET_B(*(dst+(VIDEO_WIDTH*2)));
314 uint8_t r = (r1+r2)/3;
315 uint8_t g = (g1+g2)/3;
316 uint8_t b = (b1+b2)/3;
317
318 *dst = RGB(r, g, b);
319 dst++;
320 }
321 dst+=(VIDEO_WIDTH*2);
322 }
323
324 /* Blend at right */
325 dst = (uint32_t *)screen->pixels;
326 memcpy(tmp, dst, (VIDEO_HEIGHT*2)*(VIDEO_WIDTH*2)*4);
327 for(y=VIDEO_WIDTH*2; y < ((VIDEO_HEIGHT*2) * (VIDEO_WIDTH*2))- (VIDEO_WIDTH*2); y++) {
328 uint8_t r1 = GET_R(tmp[y]);
329 uint8_t g1 = GET_G(tmp[y]);
330 uint8_t b1 = GET_B(tmp[y]);
331 uint8_t r2 = GET_R(tmp[y+1]);
332 uint8_t g2 = GET_G(tmp[y+1]);
333 uint8_t b2 = GET_B(tmp[y+1]);
334 uint8_t r = (r1+r2)/2;
335 uint8_t g = (g1+g2)/2;
336 uint8_t b = (b1+b2)/2;
337 tmp[y] = RGB(r, g, b);
338 }
339
340
341 dst++;
342 tmp++;
343 for(y=1; y < (VIDEO_HEIGHT*2); y++) {
344 uint8_t decal = rand()%3;
345 for(x=decal; x<(VIDEO_WIDTH*2); x++) {
346 uint8_t r1 = GET_R(*(dst));
347 uint8_t g1 = GET_G(*(dst));
348 uint8_t b1 = GET_B(*(dst));
349 uint32_t v1 = (r1+g1)/2;
350 uint8_t r2 = GET_R(*(tmp-decal));
351 uint8_t g2 = GET_G(*(tmp-decal));
352 uint8_t b2 = GET_B(*(tmp-decal));
353 uint32_t v2 = (r2+g2)/2;
354 uint8_t r;
355 uint8_t g;
356 uint8_t b;
357
358 if(v2>v1) {
359 r = (r1+r2)/2;
360 g = (g1+g2)/2;
361 b = (b1+b2)/2;
362 } else {
363 r = r1;
364 g = g1;
365 b = b1;
366 }
367 *tmp = RGB(r, g, b);
368 dst+=1;
369 tmp+=1;
370 }
371 }
372 tmp = tmpback;
373 memcpy(screen->pixels, tmp, (VIDEO_HEIGHT*2)*(VIDEO_WIDTH*2)*4);
374
375 }
376 sdl_display();
377 poll_keyboard();
378 SDL_Delay(20); /* max(1/50 Hz, 1/60 Hz) = 20 ms */
379 }
380 #else
381 #error You must have SDL to compile Xeuphoric
382 #endif

  ViewVC Help
Powered by ViewVC 1.1.26