1 /***********************************************************
2 * Rocks'n'Diamonds -- McDuffin Strikes Back! *
3 *----------------------------------------------------------*
4 * ©1995 Artsoft Development *
6 * 33659 Bielefeld-Senne *
7 * Telefon: (0521) 493245 *
8 * eMail: aeglos@valinor.owl.de *
9 * aeglos@uni-paderborn.de *
10 * q99492@pbhrzx.uni-paderborn.de *
11 *----------------------------------------------------------*
13 ***********************************************************/
15 #ifdef USE_SDL_LIBRARY
20 inline void SDLInitBufferedDisplay(DrawBuffer *backbuffer, DrawWindow *window)
22 /* initialize SDL video */
23 if (SDL_Init(SDL_INIT_VIDEO) < 0)
24 Error(ERR_EXIT, "SDL_Init() failed: %s", SDL_GetError());
26 /* automatically cleanup SDL stuff after exit() */
29 /* open SDL video output device (window or fullscreen mode) */
30 if (!SDLSetVideoMode(backbuffer, window))
31 Error(ERR_EXIT, "setting video mode failed");
33 /* set window and icon title */
34 SDL_WM_SetCaption(WINDOW_TITLE_STRING, WINDOW_TITLE_STRING);
36 /* create additional buffer for double-buffering */
38 pix[PIX_DB_BACK] = CreateBitmap(WIN_XSIZE, WIN_YSIZE, DEFAULT_DEPTH);
40 /* SDL cannot directly draw to the visible video framebuffer like X11,
41 but always uses a backbuffer, which is then blitted to the visible
42 video framebuffer with 'SDL_UpdateRect' (or replaced with the current
43 visible video framebuffer with 'SDL_Flip', if the hardware supports
44 this). Therefore do not use an additional backbuffer for drawing, but
45 use a symbolic buffer (distinguishable from the SDL backbuffer) called
46 'window', which indicates that the SDL backbuffer should be updated to
47 the visible video framebuffer when attempting to blit to it.
49 For convenience, it seems to be a good idea to create this symbolic
50 buffer 'window' at the same size as the SDL backbuffer. Although it
51 should never be drawn to directly, it would do no harm nevertheless. */
53 *window = pix[PIX_DB_BACK]; /* 'window' is only symbolic buffer */
54 pix[PIX_DB_BACK] = *backbuffer; /* 'backbuffer' is SDL screen buffer */
57 inline boolean SDLSetVideoMode(DrawBuffer *backbuffer, DrawWindow *window)
59 boolean success = TRUE;
61 if (setup.fullscreen && !fullscreen_enabled && fullscreen_available)
63 /* switch display to fullscreen mode, if available */
64 DrawWindow window_old = *backbuffer;
65 DrawWindow window_new;
67 if ((window_new = SDL_SetVideoMode(WIN_XSIZE, WIN_YSIZE, WIN_SDL_DEPTH,
68 SDL_HWSURFACE|SDL_FULLSCREEN))
71 /* switching display to fullscreen mode failed */
72 Error(ERR_WARN, "SDL_SetVideoMode() failed: %s", SDL_GetError());
74 /* do not try it again */
75 fullscreen_available = FALSE;
81 SDL_FreeSurface(window_old);
82 *backbuffer = window_new;
84 fullscreen_enabled = TRUE;
89 if ((!setup.fullscreen && fullscreen_enabled) || !*backbuffer)
91 /* switch display to window mode */
92 DrawWindow window_old = *backbuffer;
93 DrawWindow window_new;
95 if ((window_new = SDL_SetVideoMode(WIN_XSIZE, WIN_YSIZE, WIN_SDL_DEPTH,
99 /* switching display to window mode failed -- should not happen */
100 Error(ERR_WARN, "SDL_SetVideoMode() failed: %s", SDL_GetError());
107 SDL_FreeSurface(window_old);
108 *backbuffer = window_new;
110 fullscreen_enabled = FALSE;
118 inline void SDLCopyArea(SDL_Surface *src_surface, SDL_Surface *dst_surface,
119 int src_x, int src_y,
120 int width, int height,
121 int dst_x, int dst_y)
123 SDL_Surface *surface = (dst_surface == window ? backbuffer : dst_surface);
124 SDL_Rect src_rect, dst_rect;
136 if (src_surface != backbuffer || dst_surface != window)
137 SDL_BlitSurface(src_surface, &src_rect, surface, &dst_rect);
139 if (dst_surface == window)
140 SDL_UpdateRect(backbuffer, dst_x, dst_y, width, height);
143 inline void SDLFillRectangle(SDL_Surface *dst_surface, int x, int y,
144 int width, int height, unsigned int color)
146 SDL_Surface *surface = (dst_surface == window ? backbuffer : dst_surface);
148 unsigned int color_r = (color >> 16) && 0xff;
149 unsigned int color_g = (color >> 8) && 0xff;
150 unsigned int color_b = (color >> 0) && 0xff;
157 SDL_FillRect(surface, &rect,
158 SDL_MapRGB(surface->format, color_r, color_g, color_b));
160 if (dst_surface == window)
161 SDL_UpdateRect(backbuffer, x, y, width, height);
164 inline void SDLDrawSimpleLine(SDL_Surface *surface, int from_x, int from_y,
165 int to_x, int to_y, unsigned int color)
168 unsigned int color_r = (color >> 16) & 0xff;
169 unsigned int color_g = (color >> 8) & 0xff;
170 unsigned int color_b = (color >> 0) & 0xff;
173 swap_numbers(&from_x, &to_x);
176 swap_numbers(&from_y, &to_y);
180 rect.w = (to_x - from_x + 1);
181 rect.h = (to_y - from_y + 1);
183 SDL_FillRect(surface, &rect,
184 SDL_MapRGB(surface->format, color_r, color_g, color_b));
187 #endif /* USE_SDL_LIBRARY */