summaryrefslogtreecommitdiff
path: root/jeu-test/tetris_lan_src/main.c
blob: b410889ed56710d2786bd296affd97b0046978da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#ifdef __cplusplus
    #include <cstdlib>
#else
    #include <stdlib.h>
#endif
#ifdef __APPLE__
#include <SDL/SDL.h>
#else
#include <SDL.h>
#endif

//-----------------------
//
// A Little Tetris Game
// Done by Cl�ment CORDE
// c1702@yahoo.com
//
//-----------------------

#include "includes.h"


// Variables g�n�rales.
struct SGene gVar;
struct SExchange gExg;


// Gestionnaire d'�v�nements.
int EventHandler(void)
{
	static	u8	nFullScreen = 0;
	SDL_Event event;

	while (SDL_PollEvent(&event))
	{
		switch (event.type)
		{
		case SDL_KEYDOWN:
			gVar.pKeys = SDL_GetKeyState(NULL);

			// Triggers.
			if (event.key.keysym.sym == SDLK_UP) gVar.pKeysTrig[TRIG_UP] = 1;
			if (event.key.keysym.sym == SDLK_DOWN) gVar.pKeysTrig[TRIG_DOWN] = 1;
			if (event.key.keysym.sym == SDLK_RIGHT) gVar.pKeysTrig[TRIG_RIGHT] = 1;
			if (event.key.keysym.sym == SDLK_LEFT) gVar.pKeysTrig[TRIG_LEFT] = 1;
			if (event.key.keysym.sym == SDLK_p) gVar.pKeysTrig[TRIG_KeyP] = 1;
			if (event.key.keysym.sym == SDLK_RETURN) gVar.pKeysTrig[TRIG_Return] = 1;
			if (event.key.keysym.sym == SDLK_SPACE) gVar.pKeysTrig[TRIG_Space] = 1;

			// Ingame toggles.
			if (event.key.keysym.sym == SDLK_r) gVar.nOptFlags ^= OPT_Rotation;
			if (event.key.keysym.sym == SDLK_g) gVar.nOptFlags ^= OPT_Ghost;

			if (gVar.pKeys[SDLK_ESCAPE])	// Arr�t d'urgence !
			{
				TCP_CnxClose();
				exit(0);//return (1);
			}

			// Toggle fullscreen/windowed.
			if (gVar.pKeys[SDLK_F10])
			{
				SDL_Surface *pTmp = gVar.pScreen;

				gVar.pScreen = SDL_SetVideoMode(SCR_Width, SCR_Height, 8, SDL_HWSURFACE | SDL_DOUBLEBUF | ((nFullScreen ^ 1) ? SDL_FULLSCREEN : 0));
				if (gVar.pScreen == NULL)
				{
					// Rat�.
					fprintf(stderr, "Couldn't set video mode: %sn",SDL_GetError());
					gVar.pScreen = pTmp;	// R�cup�re l'ancien.
				}
				else
				{
					// Ok.
					SDL_SetPalette(gVar.pScreen, SDL_LOGPAL | SDL_PHYSPAL, gVar.pCurBkg->format->palette->colors, 0, gVar.pCurBkg->format->palette->ncolors);
					SDL_FreeSurface(pTmp);	// Lib�re l'ancien.
					nFullScreen ^= 1;
				}
			}

			break;

		case SDL_KEYUP:
			gVar.pKeys = SDL_GetKeyState(NULL);
			break;

		case SDL_QUIT:		// Fermeture de la fen�tre.
			TCP_CnxClose();
			exit(0);
			break;
		}
	}
	return (0);
}

// Load image.
void LoadPic(SDL_Surface **pDst, char *pFilename)
{
	*pDst = SDL_LoadBMP(pFilename);
	if (*pDst == NULL) {
		fprintf(stderr, "Couldn't load picture '%s': %s\n", pFilename, SDL_GetError());
		exit(1);
	}
}

// Le Menu (g�n�rique).
u32 Menu(void (*pFctInit)(void), u32 (*pFctMain)(void))
{
	u32	i;
	u32	nMenuVal = MENU_Null;

	// Sets up palette.
	//SDL_SetColors(gVar.pScreen, gVar.pBkgMenu->format->palette->colors, 0, gVar.pBkgMenu->format->palette->ncolors);
	SDL_SetPalette(gVar.pScreen, SDL_LOGPAL, gVar.pBkgMenu->format->palette->colors, 0, gVar.pBkgMenu->format->palette->ncolors);
	gVar.pCurBkg = gVar.pBkgMenu;

	// Main loop.
	(*pFctInit)();
	gVar.pKeys = SDL_GetKeyState(NULL);		// Lecture dans le vide, pour init du ptr.
	FrameInit();
	while (nMenuVal == MENU_Null)
	{
		// Nettoyage du trigger.
		for (i = 0; i < TRIG_MaxKeys; i++) gVar.pKeysTrig[i] = 0;

		// Gestion des �venements.
		EventHandler();

		// Recopie le d�cor.
		if (SDL_BlitSurface(gVar.pBkgMenu, NULL, gVar.pScreen, NULL) < 0)
		{
       		fprintf(stderr, "BlitSurface error: %s\n", SDL_GetError());
			exit(1);
		}

		// Menu Main.
		nMenuVal = (*pFctMain)();

		// Wait for frame.
		FrameWait();
		SDL_Flip(gVar.pScreen);		// => Refresh �cran.

	}

	return (nMenuVal);
}

// Le jeu.
void Game(void)
{
	u32	i;
	u32	nTetVal = GAME_Null;

	// Sets up palette.
	//SDL_SetColors(gVar.pScreen, gVar.pBackground->format->palette->colors, 0, gVar.pBackground->format->palette->ncolors);
	SDL_SetPalette(gVar.pScreen, SDL_LOGPAL, gVar.pBackground->format->palette->colors, 0, gVar.pBackground->format->palette->ncolors);
	gVar.pCurBkg = gVar.pBackground;

	// Main loop.
	TetrisInit();
	gVar.pKeys = SDL_GetKeyState(NULL);		// Lecture dans le vide, pour init du ptr.
	FrameInit();
	while (nTetVal == GAME_Null)
	{
		// Nettoyage du trigger.
		for (i = 0; i < TRIG_MaxKeys; i++) gVar.pKeysTrig[i] = 0;

		// Gestion des �venements.
		EventHandler();

		// Recopie le d�cor. (possible optim => seulement l'aire de jeu).
		if (SDL_BlitSurface(gVar.pBackground, NULL, gVar.pScreen, NULL) < 0)
		{
       		fprintf(stderr, "BlitSurface error: %s\n", SDL_GetError());
			exit(1);
		}

		// Game.
		nTetVal = TetrisMain();

		// Wait for frame.
		FrameWait();
		SDL_Flip(gVar.pScreen);		// => Refresh �cran.

	}

	// High score ? (Slt en mode 1 joueur).
	if (gVar.nGameMode == GAME_MODE_1Player && nTetVal == GAME_GameOver)
	{
		if (Scr_CheckHighSc(gExg.nScore) >= 0)
		{
			// Saisie du nom.
			Menu(MenuGetNameInit, MenuGetName);
			// Affichage de la table des high scores.
			Menu(MenuMainInit, MenuHighScores);	// !!! M�me init que main !!!
		}
	}

}

// Point d'entr�e.
int main(int argc, char *argv[])
{
	u32	nLoop = 1;
	u32	nMenuVal;


//> revoir comment faire
	// R�cup�ration des param�tres de la ligne de commande :
	// 1 = Adresse IP.
	// 2 = Port.
	if (argc != 3)
	{
		printf("Usage: Tetris <IP address> <Port>\n");
		exit (1);
	}
	gVar.pIPAddress = argv[1];
	gVar.nPort = atoi(argv[2]);
	printf("Connect to %s, port %d\n", gVar.pIPAddress, (int)gVar.nPort);
//<


	// SDL Init.
	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
		fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
		exit(1);
	}
	// atexit : Quand on quittera (exit, return...), SDL_Quit() sera appel�e.
	atexit(SDL_Quit);

	// Video mode init.
	gVar.pScreen = SDL_SetVideoMode(SCR_Width, SCR_Height, 8, SDL_HWSURFACE | SDL_HWPALETTE | SDL_DOUBLEBUF);
	if (gVar.pScreen == NULL)
	{
		fprintf(stderr, "Couldn't set video mode: %sn",SDL_GetError());
		exit(1);
	}
	SDL_WM_SetCaption("Tetris", NULL);	// Nom de la fen�tre.

	// Load backgound picture.
	LoadPic(&gVar.pBackground, "gfx/bkg.bmp");
	// Load menu backgound picture.
	LoadPic(&gVar.pBkgMenu, "gfx/bkg_menu.bmp");
	// Load fonts.
	LoadPic(&gVar.pFont1616, "gfx/fnt.bmp");
	LoadPic(&gVar.pFont88, "gfx/fnt8.bmp");

	// Init sound.
	Sfx_SoundInit();
	Sfx_LoadWavFiles();
	Sfx_SoundOn();	// Starts playback.

	TCP_EngineInit();

	Scr_Load();		// Lecture du fichier des high scores.
	Opt_Load();		// Lecture des options.

	MenuInitMisc();
	gVar.pWins[0] = gVar.pWins[1] = 0;	// RAZ du nombre de victoires en LAN.

	SDL_ShowCursor(SDL_DISABLE);	// Cache le pointeur de la souris.

	// Boucle infinie.
	gExg.nStartingLevel = 1;
	gExg.nHandicap = 0;
	nMenuVal = MENU_Main;
	while (nLoop)
	{
		switch (nMenuVal)
		{
		case MENU_Main :	// Main menu.
			nMenuVal = Menu(MenuMainInit2, MenuMain);
			break;

		case MENU_Game_1Player :	// Jeu : 1 joueur.
			gVar.nGameMode = GAME_MODE_1Player;
			Game();
			nMenuVal = MENU_Main;
			break;

		case MENU_Game_LAN :		// Jeu : LAN.
			gVar.nGameMode = GAME_MODE_LAN;
			Game();
			nMenuVal = MENU_Main;
			break;

		case MENU_HallOfFame :	// High scores.
			Menu(MenuMainInit, MenuHighScores);	// !!! M�me init que main !!!
			nMenuVal = MENU_Main;
			break;

		case MENU_Options :	// Options.
			Menu(MenuMainInit, MenuOptions);	// !!! M�me init que main !!!
			nMenuVal = MENU_Main;
			break;

		case MENU_Quit :	// Sortie.
			nLoop = 0;
			break;
		}

	}

	SDL_ShowCursor(SDL_ENABLE);		// R�autorise l'affichage du curseur de la souris.

	Sfx_SoundOff();		// Stops playback.
	Sfx_FreeWavFiles();	// Lib�re les ressources des fx.

	// Free the allocated BMP surfaces.
	SDL_FreeSurface(gVar.pBackground);
	SDL_FreeSurface(gVar.pBkgMenu);
	SDL_FreeSurface(gVar.pFont1616);
	SDL_FreeSurface(gVar.pFont88);

	TCP_EngineClose();

	return (0);
}