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 $
5 * Copyright (C) 2003 Kevin Riggle
6 * http://cmancala.sourcefoge.net
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
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.
27 #include "SDL_image.h"
41 int main(int argc, char **argv) {
43 SDL_Surface *screen, *board, *title_text, *tile, *stone[STONE_MAX+1];
44 SDL_Rect board_rect, title_rect;
48 TTF_Font *title_font, *home_font, *board_font;
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;
57 /* Set up the game board and game variables. */
58 gameInit(aiBoard, humanBoard);
60 ai_last_move = human_last_move = -99;
62 /* initialize our libraries */
63 if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) < 0) {
64 fprintf(stderr, "Unable to initialize SDL: %s\n",
70 fprintf(stderr, "Unable to initialize SDL_ttf: %s\n",
75 /* Make sure we clean up after ourselves */
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",
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",
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());
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());
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());
119 /* store the font's color */
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());
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))
138 fprintf(stderr, "Unable to set %dx%d video: %s", tile->w*8,
139 tile->h*2, SDL_GetError());
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);
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;
176 case SDL_MOUSEMOTION:
177 if (event.motion.y < tile->h) {
178 highlight = event.motion.x / tile->w;
183 case SDL_ACTIVEEVENT:
184 if (event.active.gain == 0)
190 SDL_Delay(DELAY_MAX);
193 if (gameWon(aiBoard, humanBoard) == 1)
196 /* happy happy state machine */
200 if (highlight != old_highlight)
204 human_last_move = move(humanBoard,aiBoard,current_move);
206 if (human_last_move == 0)
210 printf("Human moving from %d to %d, now %d\n", current_move, human_last_move, state);
215 current_move = aiMove(aiBoard, humanBoard);
219 printf("Computer moving\n");
220 ai_last_move = move(aiBoard,humanBoard,current_move);
222 if (ai_last_move == 0)
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());
234 if (!(title_text = TTF_RenderText_Blended(title_font,
235 "Human Wins!", font_color)))
236 fprintf(stderr, "TTF: %s\n", TTF_GetError());
242 /* redraw the board if needed */
243 if (redraw_board == 1) {
244 /*printf("redrawing board\n");*/
246 /* draw and blit the board */
247 board = DrawBoard(aiBoard, humanBoard, board_font,
248 home_font, tile, stone, active,
251 fprintf(stderr, "Could not draw the board.\n");
254 board_rect = SurfaceToRect(board);
255 SDL_BlitSurface(board, NULL, screen,
259 /* draw, center, and blit the title */
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,
268 SDL_UpdateRect(screen, 0,0,0,0);