2 * for Maemo renamed to play.c and some bugfixes (2009, Reto Zingg)
4 * Main Mancala Program Module Source -- main.c
5 * $Id: main.c,v 1.7.2.25 2004/01/17 06:56:23 sparrow_hawk Exp $
7 * Copyright (C) 2003 Kevin Riggle
8 * http://cmancala.sourcefoge.net
9 * Copyright (C) 2009 Reto Zingg
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2, or (at your option) any
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details, a copy of which may be found in
20 * the file COPYING provided in the main directory of this release.
24 #include <hildon/hildon.h>
26 #include "SDL_mixer.h"
28 #include "SDL_image.h"
38 SDL_Surface *screen, *board, *title_text, *tile, *stone[STONE_MAX+1], *exit_text;
40 TTF_Font *title_font, *home_font, *board_font;
41 // The sound effects that will be used
42 Mix_Chunk *pick = NULL;
49 SDL_FreeSurface(board);
50 SDL_FreeSurface(tile);
51 SDL_FreeSurface(exit_text);
52 SDL_FreeSurface(title_text);
53 for(i=0; i<=STONE_MAX; i++)
54 SDL_FreeSurface(stone[i]);
55 SDL_FreeSurface(screen);
56 TTF_CloseFont(title_font);
57 TTF_CloseFont(board_font);
58 TTF_CloseFont(home_font);
60 /* Make sure we clean up after ourselves */
69 SDL_Rect board_rect, title_rect, exit_rect;
72 SDL_Color font_color_exit;
74 char tile_path[STRING_MAX], stone_path[STRING_MAX];
75 char icon_path[STRING_MAX], title_path[STRING_MAX];
76 char home_path[STRING_MAX], board_path[STRING_MAX];
77 char pick_path[STRING_MAX];
78 int aiBoard[BOARD_MAX+1], humanBoard[BOARD_MAX+1];
79 int i, redraw_board, highlight, old_highlight, active;
80 int current_move, ai_last_move, human_last_move, state;
82 /* Set up the game board and game variables. */
83 gameInit(aiBoard, humanBoard);
85 ai_last_move = human_last_move = -99;
87 /* initialize our libraries */
88 //if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_AUDIO) < 0) {
89 if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
90 fprintf(stderr, "Unable to initialize SDL: %s\n",
97 fprintf(stderr, "Unable to initialize SDL_ttf: %s\n",
103 //Initialize SDL_mixer
105 if( Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 4096 ) == -1 )
107 fprintf(stderr, "Unable to initialize Mix_OpenAudio: %s\n",
114 /* Load our images... PNGs now, maybe XPMs later */
115 sprintf(tile_path, "%s/tile.png", RES_PATH);
116 if ((tile = LoadRes(tile_path)) == NULL) {
117 fprintf(stderr, "Unable to load resource: %s\n",
123 for (i=0; i<=STONE_MAX; i++) {
124 if (sprintf(stone_path, "%s/stone%02d.png", RES_PATH, i) == 0)
125 fprintf(stderr, "Problems assembling path.\n");
126 if (!(stone[i] = LoadRes(stone_path))) {
127 fprintf(stderr, "Unable to load resource: %s\n",
134 /* Load our font(s) */
135 if (sprintf(title_path, "%s/SwaRR4nh.ttf", FONT_PATH) == 0)
136 fprintf(stderr, "Problems assembling path.\n");
137 if (!(title_font = TTF_OpenFont(title_path, TITLE_SIZE))) {
138 fprintf(stderr, "Could not load font: %s\n", TTF_GetError());
143 if (sprintf(board_path, "%s/SwaRR4nh.ttf", FONT_PATH) == 0)
144 fprintf(stderr, "Problems assembling path.\n");
145 if (!(board_font = TTF_OpenFont(board_path, BOARD_SIZE))) {
146 fprintf(stderr, "Could not load font: %s\n", TTF_GetError());
151 if (sprintf(home_path, "%s/SwaRR4nh.ttf", FONT_PATH) == 0)
152 fprintf(stderr, "Problems assembling path.\n");
153 if (!(home_font = TTF_OpenFont(home_path, HOME_SIZE))) {
154 fprintf(stderr, "Could not load font: %s\n", TTF_GetError());
159 /* Load sound effects */
160 sprintf(pick_path, "%s/pick.wav", RES_PATH);
161 if ((pick = Mix_LoadWAV(pick_path)) == NULL) {
162 fprintf(stderr, "Unable to load sound: %s\n",
168 /* store the font's color */
173 /* render the title text
174 if (!(title_text = TTF_RenderText_Solid(title_font,
175 "Mancala", font_color)))
176 fprintf(stderr, "TTF: %s\n", TTF_GetError());
181 /* define the position of the board in the screen */
183 board_rect.y = Y_OFFSET;
187 /* set window properties and create it */
188 SDL_WM_SetCaption("Mancala", "Mancala");
189 if (sprintf(icon_path, "%s/icon.png", RES_PATH) == 0)
190 fprintf(stderr, "Problems assembling icon path.\n");
191 SDL_WM_SetIcon(LoadRes(icon_path), NULL);
192 if ((screen = SDL_SetVideoMode(tile->w*8, (tile->h*2) + Y_OFFSET, 16, SDL_FULLSCREEN))
194 fprintf(stderr, "Unable to set %dx%d video: %s", tile->w*8,
195 tile->h*2, SDL_GetError());
201 /* define the font color fot the exit text */
202 font_color_exit.r = 255;
203 font_color_exit.r = 255;
204 font_color_exit.r = 255;
208 if (!(exit_text = TTF_RenderText_Blended(home_font, "EXIT",
210 fprintf(stderr, "SDL_ttf: %s\n", TTF_GetError());
219 SDL_BlitSurface(exit_text, NULL, screen, &exit_rect);
228 while (state != GAME_WON) {
230 /* check for GTK events... */
231 /* otherwise hildon thinks the app hangs... */
232 while (gtk_events_pending ())
233 gtk_main_iteration ();
235 /* figure out if anything important happened */
236 old_highlight = highlight;
237 while (SDL_PollEvent(&event)) {
238 /* BAIL OUT! BAIL OUT! */
239 if (event.type == SDL_KEYDOWN){
240 fprintf(stderr, "event SDL_KEYDOWN found....\n");
241 if ( event.key.keysym.sym == SDLK_q )
243 fprintf(stderr, "event SDLK_q found....\n");
244 SDL_Event quit_event;
245 quit_event.type = SDL_QUIT;
246 SDL_PushEvent(&quit_event);
250 if (event.type == SDL_MOUSEBUTTONDOWN) {
251 if ((event.button.button = 1) &&
252 (event.button.y < Y_OFFSET)) {
253 fprintf(stderr, "clicked out side the board in exit area...\n");
254 SDL_Event quit_event;
255 quit_event.type = SDL_QUIT;
256 SDL_PushEvent(&quit_event);
260 if (event.type == SDL_QUIT) {
261 fprintf(stderr, "event SDL_QUIT found....\n");
267 if (state == HMN_WAIT) {
268 switch (event.type) {
269 case SDL_MOUSEBUTTONDOWN:
270 if ((event.button.button = 1) &&
271 (event.button.y < tile->h) &&
272 (event.button.y > Y_OFFSET)) {
275 pitch = event.button.x / tile->w;
277 // pitch 0 and 7 are the homebases which you can't play
278 if ( pitch == 0 || pitch == 7 ){
279 fprintf(stderr, "clicked out side the board...\n");
282 current_move = pitch;
287 case SDL_MOUSEMOTION:
288 if (event.motion.y < tile->h) {
289 highlight = event.motion.x / tile->w;
294 case SDL_ACTIVEEVENT:
295 if (event.active.gain == 0)
301 SDL_Delay(DELAY_MAX);
304 if (gameWon(aiBoard, humanBoard) == 1)
307 /* happy happy state machine */
311 if (highlight != old_highlight)
315 human_last_move = move(humanBoard,aiBoard,current_move);
318 if (human_last_move == 0)
322 printf("Human moving from %d to %d, now %d\n", current_move, human_last_move, state);
327 current_move = aiMove(aiBoard, humanBoard);
331 printf("Computer moving\n");
332 ai_last_move = move(aiBoard,humanBoard,current_move);
335 if (ai_last_move == 0)
341 if (aiBoard[0] > humanBoard[0]) {
342 if (!(title_text = TTF_RenderText_Blended(title_font,
343 "Computer Wins!", font_color)))
344 fprintf(stderr, "TTF: %s\n", TTF_GetError());
347 if (!(title_text = TTF_RenderText_Blended(title_font,
348 "Human Wins!", font_color)))
349 fprintf(stderr, "TTF: %s\n", TTF_GetError());
355 /* redraw the board if needed */
356 if (redraw_board == 1) {
357 /*printf("redrawing board\n");*/
359 /* draw and blit the board */
360 board = DrawBoard(aiBoard, humanBoard, board_font,
361 home_font, tile, stone, active,
364 fprintf(stderr, "Could not draw the board.\n");
367 // board_rect = SurfaceToRect(board);
368 SDL_BlitSurface(board, NULL, screen,
372 /* draw, center, and blit the title */
374 title_rect = SurfaceToRect(title_text);
375 title_rect.x = ((screen->w - title_rect.w)/2);
376 title_rect.y = ((screen->h - title_rect.h)/2);
377 SDL_BlitSurface(title_text, NULL, screen,
381 SDL_UpdateRect(screen, 0,0,0,0);