1 /* Pongix - clone of the "Pong" video game with net support
2  * Copyright (C) 2005 - Hugo Ruscitti (see AUTHORS file)
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  */
18 
19 #include "mundo.h"
20 #include "juego.h"
21 #include "menu.h"
22 
mundo_iniciar(void)23 Mundo * mundo_iniciar (void)
24 {
25 	Mundo * data;
26 
27 	data = (Mundo *) malloc (sizeof (Mundo));
28 
29 	if (data == NULL)
30 	{
31 		printf ("error, no se puede crear la estructura Mundo\n");
32 		return NULL;
33 	}
34 
35 	data->screen = iniciar_sdl ();
36 
37 	if (data->screen == NULL)
38 		return NULL;
39 
40 	data->fondo = cargar_imagen ("fondo_juego.jpg");
41 
42 	if (data->fondo == NULL)
43 		return NULL;
44 
45 	if (mundo_cargar_modulos (data))
46 		return NULL;
47 
48 	SDL_BlitSurface (data->fondo, NULL, data->screen, NULL);
49 	dirty_agregar_pantalla_completa (data->dirty);
50 	mundo_cambiar_estado(data, MENU);
51 
52 	data->salir = 0;
53 
54 	return data;
55 }
56 
mundo_cargar_modulos(Mundo * data)57 int mundo_cargar_modulos (Mundo * data)
58 {
59 	if ((data->creditos = creditos_iniciar (data)) == NULL)
60 		return 1;
61 
62 	if ((data->menu = menu_iniciar (data)) == NULL)
63 		return 1;
64 
65 	if ((data->juego = juego_iniciar (data)) == NULL)
66 		return 1;
67 
68 	if ((data->dirty = dirty_iniciar ()) == NULL)
69 		return 1;
70 
71 	if ((data->fuente = fuente_iniciar ("fuente.bmp", data)) == NULL)
72 		return 1;
73 
74 	if ((data->cliente = cliente_iniciar (data)) == NULL)
75 		return 1;
76 
77 	if ((data->servidor = servidor_iniciar (data)) == NULL)
78 		return 1;
79 
80 	return 0;
81 }
82 
mundo_actualizar(Mundo * data)83 void mundo_actualizar (Mundo * data)
84 {
85 	static Uint8 * teclas;
86 
87 	teclas = SDL_GetKeyState (NULL);
88 
89 	switch (data->estado)
90 	{
91 		case MENU:
92 			menu_actualizar (data->menu, teclas);
93 			break;
94 
95 		case JUEGO:
96 		case JUEGORED_CLIENTE:
97 		case JUEGORED_SERVIDOR:
98 			juego_actualizar (data->juego, teclas);
99 			break;
100 
101 		case CREDITOS:
102 			creditos_actualizar (data->creditos, teclas);
103 			break;
104 
105 		case SERVIDOR:
106 			servidor_actualizar (data->servidor, teclas);
107 			break;
108 
109 		case CLIENTE:
110 			cliente_actualizar (data->cliente, teclas);
111 			break;
112 
113 		default:
114 			printf ("en Mundo: el estado '%d' es incorrecto\n", \
115 					data->estado);
116 			break;
117 	}
118 }
119 
mundo_imprimir(Mundo * data)120 void mundo_imprimir (Mundo * data)
121 {
122 	switch (data->estado)
123 	{
124 		case MENU:
125 			menu_imprimir (data->menu);
126 			break;
127 
128 		case JUEGO:
129 		case JUEGORED_CLIENTE:
130 		case JUEGORED_SERVIDOR:
131 			juego_imprimir (data->juego);
132 			break;
133 
134 		case CREDITOS:
135 			creditos_imprimir (data->creditos);
136 			break;
137 
138 		case SERVIDOR:
139 			servidor_imprimir (data->servidor);
140 			break;
141 
142 		case CLIENTE:
143 			cliente_imprimir (data->cliente);
144 			break;
145 
146 
147 		default:
148 			printf ("en Mundo: el estado '%d' es incorrecto\n", \
149 					data->estado);
150 			break;
151 	}
152 
153 	dirty_actualizar (data->dirty, data->screen, data->fondo);
154 }
155 
mundo_cambiar_estado(Mundo * data,enum estados nuevo_estado)156 void mundo_cambiar_estado (Mundo * data, enum estados nuevo_estado)
157 {
158 	data->estado = nuevo_estado;
159 
160 	switch (nuevo_estado)
161 	{
162 		case MENU:
163 			menu_reiniciar (data->menu);
164 			break;
165 
166 		case JUEGORED_SERVIDOR:
167 			juego_reiniciar (data->juego, JUEGO_SERVIDOR);
168 			break;
169 
170 		case JUEGORED_CLIENTE:
171 			juego_reiniciar (data->juego, JUEGO_CLIENTE);
172 			break;
173 
174 		case JUEGO:
175 			juego_reiniciar (data->juego, NORED);
176 			break;
177 
178 		case CREDITOS:
179 			break;
180 
181 		case SERVIDOR:
182 			servidor_reiniciar (data->servidor);
183 			break;
184 
185 		case CLIENTE:
186 			cliente_reiniciar (data->cliente);
187 			break;
188 
189 		default:
190 			printf ("Escena inexistente, no se puede reiniciar\n");
191 			break;
192 	}
193 }
194 
mundo_pantalla_completa(Mundo * data)195 void mundo_pantalla_completa (Mundo * data)
196 {
197 
198 #ifndef WIN32
199 	SDL_WM_ToggleFullScreen (data->screen);
200 #else
201 	int flags = data->screen->flags;
202 	int w = data->screen->w;
203 	int h = data->screen->h;
204 	int bpp = data->screen->format->BitsPerPixel;
205 	SDL_Surface * nueva;
206 
207 	flags ^= SDL_FULLSCREEN;
208 
209 	nueva = SDL_SetVideoMode (w, h, bpp, flags);
210 
211 	if (nueva)
212 	{
213 		SDL_FreeSurface (data->screen);
214 		data->screen = nueva;
215 
216 		dirty_agregar_pantalla_completa (data->dirty);
217 	}
218 	else
219 		printf ("No se puede cambiar el modo de video\n");
220 
221 #endif
222 
223 }
224 
mundo_terminar(Mundo * data)225 void mundo_terminar (Mundo * data)
226 {
227 	menu_terminar (data->menu);
228 	juego_terminar (data->juego);
229 	dirty_terminar (data->dirty);
230 	fuente_terminar (data->fuente);
231 	servidor_terminar (data->servidor);
232 	cliente_terminar (data->cliente);
233 	SDL_FreeSurface (data->fondo);
234 
235 	free (data);
236 	SDLNet_Quit ();
237 	SDL_Quit ();
238 
239 	printf ("� Gracias por jugar a %s !\n", PACKAGE_STRING);
240 }
241 
242 
mundo_salir(Mundo * data)243 void mundo_salir (Mundo * data)
244 {
245 	data->salir = 1;
246 }
247