1 /*
2  * XGalaga-SDL - a SDL port of XGalaga, clone of the game Galaga
3  * Copyright (c) 1995-1998 Joe Rumsey (mrogre@mediaone.net)
4  * Copyright (c) 2000 Andy Tarkinson <atark@thepipeline.net>
5  * Copyright (c) 2010 Frank Zago
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  * 02110-1301, USA.
21  */
22 
23 #include "xgalaga.h"
24 
25 SDL_Surface *screen;
26 static Uint32 background_color;
27 
toggle_fullscreen(void)28 void toggle_fullscreen(void)
29 {
30 	Uint32 flags = SDL_SWSURFACE;
31 	Uint32 old_flags = screen->flags;
32 
33 	fullscreen = !fullscreen;
34 
35 	if (fullscreen)
36 		flags |= SDL_FULLSCREEN;
37 
38 	screen = SDL_SetVideoMode(0, 0, 0, flags);
39 	if (!screen)
40 		screen = SDL_SetVideoMode(0, 0, 0, old_flags);
41 
42 	if (!screen) {
43 		fprintf(stderr, "Couldn't toggle screen\n");
44 		exit(1);
45     }
46 
47 	background_color = SDL_MapRGBA(screen->format, 0, 0, 0, 255);
48 }
49 
S_Initialize(int fullscreen)50 void S_Initialize(int fullscreen)
51 {
52 	int bpp;
53 	Uint32 flags = SDL_SWSURFACE;
54 	const int screen_height = winheight+WINTOPOV+WINBOTOV;
55 
56 	if (fullscreen)
57 		flags |= SDL_FULLSCREEN;
58 
59     if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) < 0) {
60         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
61 		exit(1);
62     }
63 
64     bpp = SDL_VideoModeOK(winwidth, screen_height, 8, flags);
65     if (bpp == 0) {
66         fprintf(stderr, "Couldn't get a video mode: %s\n", SDL_GetError());
67 		exit(1);
68     }
69 
70     screen = SDL_SetVideoMode(winwidth, screen_height, bpp, flags);
71     if (screen == NULL) {
72         fprintf(stderr, "Couldn't get video mode: %s\n", SDL_GetError());
73         exit(1);
74     }
75 
76 	SDL_ShowCursor(SDL_DISABLE);
77 }
78 
79 /* Draw a point. Surface must be locked with SDL_LockSurface(). */
S_DrawPoint(unsigned int x,unsigned int y,Uint32 pixel)80 void S_DrawPoint(unsigned int x, unsigned int y, Uint32 pixel)
81 {
82 	Uint8 *bits, bpp;
83 	Uint8 r, g, b;
84 
85 	y+= WINTOPOV;
86 
87 	/* Calculate the framebuffer offset of the center of the screen */
88 	bpp = screen->format->BytesPerPixel;
89 	bits = (Uint8 *)screen->pixels + y*screen->pitch + x*bpp;
90 
91 	/* Set the pixel */
92 	switch(bpp) {
93 	case 1:
94 		*((Uint8 *)(bits)) = pixel;
95 		break;
96 
97 	case 2:
98 		*((Uint16 *)(bits)) = pixel;
99 		break;
100 
101 	case 3:
102 		/* Format/endian independent */
103 		r = (pixel>>screen->format->Rshift)&0xFF;
104 		g = (pixel>>screen->format->Gshift)&0xFF;
105 		b = (pixel>>screen->format->Bshift)&0xFF;
106 		*((bits)+screen->format->Rshift/8) = r;
107 		*((bits)+screen->format->Gshift/8) = g;
108 		*((bits)+screen->format->Bshift/8) = b;
109 		break;
110 
111 	case 4:
112 		*((Uint32 *)(bits)) = pixel;
113 		break;
114 	}
115 	return;
116 }
117 
S_ClearScreen(void)118 void S_ClearScreen(void)
119 {
120     SDL_FillRect (screen, NULL, background_color);
121 }
122 
S_DrawImage(int x,int y,int frame,struct W_Image * image)123 void S_DrawImage(int x, int y, int frame, struct W_Image *image)
124 {
125 	int height, width;
126 	SDL_Rect srcrect, dstrect;
127 
128 	y+= WINTOPOV;
129 
130 	if (frame < 0) {
131 		/* Draw the whole thing regardless of frames. */
132 		height = image->height * image->frames;
133 		frame = 0;
134 	} else {
135 		/* Draw the given frame. */
136 		height = image->height;
137 		frame = frame % image->frames;
138 	}
139 	width = image->width;
140 
141 	dstrect.x = x;
142 	dstrect.y = y;
143 	dstrect.w = width;
144 	dstrect.h = height;
145 
146 	srcrect.x = 0;
147 	srcrect.y = height * frame;
148 	srcrect.w = width;
149 	srcrect.h = height;
150 
151 	SDL_BlitSurface (image->surface, &srcrect, screen, &dstrect);
152 }
153 
S_DrawRect(int x,int y,int w,int h,Uint32 color)154 void S_DrawRect(int x, int y, int w, int h, Uint32 color)
155 {
156     SDL_Rect dstrect;
157 
158 	y+= WINTOPOV;
159 
160     dstrect.x = x;
161     dstrect.y = y;
162     dstrect.w = w;
163     dstrect.h = h;
164     SDL_FillRect (screen, &dstrect, color);
165 }
166