initial load of http://downloads.sourceforge.net/project/cmancala/mancala-gui/mancala...
[mancala] / src / main.c
1 /*  
2  *  Main Mancala Program Module Source -- main.c 
3  *  $Id: main.c,v 1.7.2.25 2004/01/17 06:56:23 sparrow_hawk Exp $
4  *
5  *  Copyright (C) 2003 Kevin Riggle 
6  *  http://cmancala.sourcefoge.net
7  *
8  *  This program is free software; you can redistribute it and/or modify it
9  *  under the terms of the GNU General Public License as published by the
10  *  Free Software Foundation; either version 2, or (at your option) any
11  *  later version.
12  *
13  *  This program is distributed in the hope that it will be useful, but
14  *  WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  *  General Public License for more details, a copy of which may be found in
17  *  the file COPYING provided in the main directory of this release.
18  *
19  */
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #include "SDL.h"
27 #include "SDL_image.h"
28 #include "SDL_ttf.h"
29
30 #include "main.h"
31 #include "graphics.h"
32 #include "mancala.h"
33 #include "ai.h"
34
35 #define HMN_WAIT 1
36 #define HMN_MOVE 2
37 #define CMP_WAIT 3
38 #define CMP_MOVE 4
39 #define GAME_WON 0
40
41 int main(int argc, char **argv) {
42
43         SDL_Surface *screen, *board, *title_text, *tile, *stone[STONE_MAX+1];
44         SDL_Rect board_rect, title_rect;
45         SDL_Color font_color;
46         SDL_Event event;
47
48         TTF_Font *title_font, *home_font, *board_font;
49
50         char tile_path[STRING_MAX], stone_path[STRING_MAX];
51         char icon_path[STRING_MAX], title_path[STRING_MAX];
52         char home_path[STRING_MAX], board_path[STRING_MAX];
53         int aiBoard[BOARD_MAX+1], humanBoard[BOARD_MAX+1];
54         int i, redraw_board, highlight, old_highlight, active;
55         int current_move, ai_last_move, human_last_move, state;
56
57         /* Set up the game board and game variables. */
58         gameInit(aiBoard, humanBoard);
59         current_move = 0;
60         ai_last_move = human_last_move = -99;
61
62         /* initialize our libraries */
63         if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) < 0) {
64                 fprintf(stderr, "Unable to initialize SDL: %s\n", 
65                         SDL_GetError());
66                 exit(1);
67         }
68
69         if (TTF_Init() < 0) {
70                 fprintf(stderr, "Unable to initialize SDL_ttf: %s\n", 
71                         SDL_GetError());
72                 exit(1);
73         } 
74
75         /* Make sure we clean up after ourselves */
76         atexit(SDL_Quit);
77         atexit(TTF_Quit);
78
79         /* Load our images... PNGs now, maybe XPMs later */
80         sprintf(tile_path, "%s/tile.png", RES_PATH);
81         if ((tile = LoadRes(tile_path)) == NULL) {
82                 fprintf(stderr, "Unable to load resource: %s\n", 
83                         SDL_GetError());
84                 return 1;
85         }
86
87         for (i=0; i<=STONE_MAX; i++) {
88                 if (sprintf(stone_path, "%s/stone%02d.png", RES_PATH, i) == 0)
89                         fprintf(stderr, "Problems assembling path.\n");
90                 if (!(stone[i] = LoadRes(stone_path))) {
91                         fprintf(stderr, "Unable to load resource: %s\n",
92                                 SDL_GetError());
93                         return 1;
94                 }
95         }
96
97         /* Load our font(s) */
98         if (sprintf(title_path, "%s/VeraSeBd.ttf", FONT_PATH) == 0)
99                 fprintf(stderr, "Problems assembling path.\n");
100         if (!(title_font = TTF_OpenFont(title_path, TITLE_SIZE))) {
101                 fprintf(stderr, "Could not load font: %s\n", TTF_GetError());
102                 exit(1);
103         }
104
105         if (sprintf(board_path, "%s/VeraSeBd.ttf", FONT_PATH) == 0)
106                 fprintf(stderr, "Problems assembling path.\n");
107         if (!(board_font = TTF_OpenFont(board_path, BOARD_SIZE))) {
108                 fprintf(stderr, "Could not load font: %s\n", TTF_GetError());
109                 exit(1);
110         }
111
112         if (sprintf(home_path, "%s/VeraSeBd.ttf", FONT_PATH) == 0)
113                 fprintf(stderr, "Problems assembling path.\n");
114         if (!(home_font = TTF_OpenFont(home_path, HOME_SIZE))) {
115                 fprintf(stderr, "Could not load font: %s\n", TTF_GetError());
116                 exit(1);
117         }
118
119         /* store the font's color */
120         font_color.r = 255;
121         font_color.b = 255;
122         font_color.g = 255;
123
124         /* render the title text 
125         if (!(title_text = TTF_RenderText_Solid(title_font, 
126                 "Mancala", font_color))) 
127                 fprintf(stderr, "TTF: %s\n", TTF_GetError());
128 */
129         title_text = NULL;
130
131         /* set window properties and create it */
132         SDL_WM_SetCaption("Mancala", "Mancala");
133         if (sprintf(icon_path, "%s/icon.png", RES_PATH) == 0)
134                 fprintf(stderr, "Problems assembling icon path.\n");
135         SDL_WM_SetIcon(LoadRes(icon_path), NULL);
136         if ((screen = SDL_SetVideoMode(tile->w*8, tile->h*2, 16, SDL_SWSURFACE))
137                     == NULL) {
138                 fprintf(stderr, "Unable to set %dx%d video: %s", tile->w*8,
139                         tile->h*2, SDL_GetError());
140                 exit(1);
141         }
142
143
144         state = HMN_WAIT;
145         redraw_board = 1;
146         old_highlight = 0;      
147         highlight = 0;
148         active = 0;
149
150         /* GAME LOOP */
151         while (state != GAME_WON) {
152                 /* figure out if anything important happened */
153                 old_highlight = highlight;
154                 while (SDL_PollEvent(&event)) {
155                         /* BAIL OUT! BAIL OUT! */
156                         if (event.type == SDL_QUIT) {
157                                 SDL_FreeSurface(board);
158                                 SDL_FreeSurface(tile);
159                                 for(i=0; i<=STONE_MAX; i++)
160                                         SDL_FreeSurface(stone[i]);
161                                 TTF_CloseFont(title_font);
162                                 TTF_CloseFont(board_font);
163                                 TTF_CloseFont(home_font);
164                                 exit(0);
165                         }
166                         /* get events */
167                         if (state == HMN_WAIT) {
168                         switch (event.type) {
169                         case SDL_MOUSEBUTTONDOWN:
170                                 if ((event.button.button = 1) &&
171                                         (event.button.y < tile->h)) {
172                                         current_move = event.button.x / tile->w;
173                                         state = HMN_MOVE;
174                                 }
175                                 break;
176                         case SDL_MOUSEMOTION:
177                                 if (event.motion.y < tile->h) {
178                                         highlight = event.motion.x / tile->w;
179                                 }
180                                 else
181                                         highlight = 0;
182                                 break;
183                         case SDL_ACTIVEEVENT:
184                                 if (event.active.gain == 0)
185                                         highlight = 0;
186                                 break;
187                         }
188                         }
189                 }
190                 SDL_Delay(DELAY_MAX);
191
192                 /* GAME LOGIC */
193                 if (gameWon(aiBoard, humanBoard) == 1)
194                         state = GAME_WON;
195
196                 /* happy happy state machine */
197                 switch(state) {
198                 case HMN_WAIT:
199                         active = 0;
200                         if (highlight != old_highlight)
201                                 redraw_board = 1;
202                         break;
203                 case HMN_MOVE:
204                         human_last_move = move(humanBoard,aiBoard,current_move);
205                         redraw_board = 1;
206                         if (human_last_move == 0)
207                                 state = HMN_WAIT;
208                         else 
209                                 state = CMP_WAIT;
210                         printf("Human moving from %d to %d, now %d\n", current_move, human_last_move, state);
211                         break;
212                 case CMP_WAIT:
213                         SDL_Delay(5000);
214                         active = 1;
215                         current_move = aiMove(aiBoard, humanBoard);
216                         state = CMP_MOVE;
217                         break;
218                 case CMP_MOVE:
219                         printf("Computer moving\n");
220                         ai_last_move = move(aiBoard,humanBoard,current_move);
221                         redraw_board = 1;
222                         if (ai_last_move == 0)
223                                 state = CMP_WAIT;
224                         else
225                                 state = HMN_WAIT;
226                         break;
227                 case GAME_WON:
228                         if (aiBoard[0] > humanBoard[0]) {
229                         if (!(title_text = TTF_RenderText_Blended(title_font, 
230                                 "Computer Wins!", font_color)))
231                                 fprintf(stderr, "TTF: %s\n", TTF_GetError());
232                         }
233                         else {
234                         if (!(title_text = TTF_RenderText_Blended(title_font, 
235                                 "Human Wins!", font_color))) 
236                                 fprintf(stderr, "TTF: %s\n", TTF_GetError());
237                         }
238                         redraw_board = 1;
239                         break;
240                 }
241
242                 /* redraw the board if needed */
243                 if (redraw_board == 1) {
244                         /*printf("redrawing board\n");*/
245
246                         /* draw and blit the board */
247                         board = DrawBoard(aiBoard, humanBoard, board_font, 
248                                         home_font, tile, stone, active, 
249                                         highlight);
250                         if (!board) {
251                                 fprintf(stderr, "Could not draw the board.\n");
252                         }
253                         else {
254                                 board_rect = SurfaceToRect(board);
255                                 SDL_BlitSurface(board, NULL, screen, 
256                                                 &board_rect);
257                         }
258
259                         /* draw, center, and blit the title */
260                         if (title_text) {
261                                 title_rect = SurfaceToRect(title_text);
262                                 title_rect.x = ((screen->w - title_rect.w)/2);
263                                 title_rect.y = ((screen->h - title_rect.h)/2);
264                                 SDL_BlitSurface(title_text, NULL, screen, 
265                                                 &title_rect);
266                         }
267                         
268                         SDL_UpdateRect(screen, 0,0,0,0);
269
270                         redraw_board = 0;
271                 }
272
273         }
274
275         SDL_Delay(5000);
276
277         return 0;
278
279 }
280
281 /*  End main.c */