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>
28 #include "SDL_mixer.h"
30 #include "SDL_image.h"
39 #include "callbacks.h"
42 SDL_Surface *screen, *board, *title_text, *tile, *stone[STONE_MAX+1], *exit_text;
44 TTF_Font *title_font, *home_font, *board_font;
45 // The sound effects that will be used
46 Mix_Chunk *pick = NULL;
47 // the hildon-game-wrapper context
48 HgwContext *hgw_context = NULL;
54 SDL_FreeSurface(board);
55 SDL_FreeSurface(tile);
56 SDL_FreeSurface(exit_text);
57 SDL_FreeSurface(title_text);
58 for(i=0; i<=STONE_MAX; i++)
59 SDL_FreeSurface(stone[i]);
60 SDL_FreeSurface(screen);
61 TTF_CloseFont(title_font);
62 TTF_CloseFont(board_font);
63 TTF_CloseFont(home_font);
65 /* Make sure we clean up after ourselves */
74 SDL_Rect board_rect, title_rect, exit_rect;
77 SDL_Color font_color_exit;
79 char tile_path[STRING_MAX], stone_path[STRING_MAX];
80 char icon_path[STRING_MAX], title_path[STRING_MAX];
81 char home_path[STRING_MAX], board_path[STRING_MAX];
82 char pick_path[STRING_MAX];
83 int aiBoard[BOARD_MAX+1], humanBoard[BOARD_MAX+1];
84 int i, redraw_board, highlight, old_highlight, active;
85 int current_move, ai_last_move, human_last_move, state;
87 /* Set up the game board and game variables. */
88 gameInit(aiBoard, humanBoard);
90 ai_last_move = human_last_move = -99;
92 /* initialize our libraries */
93 //if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_AUDIO) < 0) {
94 if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
95 fprintf(stderr, "Unable to initialize SDL: %s\n",
101 if (TTF_Init() < 0) {
102 fprintf(stderr, "Unable to initialize SDL_ttf: %s\n",
108 //Initialize SDL_mixer
110 if( Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 4096 ) == -1 )
112 fprintf(stderr, "Unable to initialize Mix_OpenAudio: %s\n",
119 /* Load our images... PNGs now, maybe XPMs later */
120 sprintf(tile_path, "%s/tile.png", RES_PATH);
121 if ((tile = LoadRes(tile_path)) == NULL) {
122 fprintf(stderr, "Unable to load resource: %s\n",
128 for (i=0; i<=STONE_MAX; i++) {
129 if (sprintf(stone_path, "%s/stone%02d.png", RES_PATH, i) == 0)
130 fprintf(stderr, "Problems assembling path.\n");
131 if (!(stone[i] = LoadRes(stone_path))) {
132 fprintf(stderr, "Unable to load resource: %s\n",
139 /* Load our font(s) */
140 if (sprintf(title_path, "%s/luxisr.ttf", FONT_PATH) == 0)
141 fprintf(stderr, "Problems assembling path.\n");
142 if (!(title_font = TTF_OpenFont(title_path, TITLE_SIZE))) {
143 fprintf(stderr, "Could not load font: %s\n", TTF_GetError());
148 if (sprintf(board_path, "%s/luxisr.ttf", FONT_PATH) == 0)
149 fprintf(stderr, "Problems assembling path.\n");
150 if (!(board_font = TTF_OpenFont(board_path, BOARD_SIZE))) {
151 fprintf(stderr, "Could not load font: %s\n", TTF_GetError());
156 if (sprintf(home_path, "%s/luxisr.ttf", FONT_PATH) == 0)
157 fprintf(stderr, "Problems assembling path.\n");
158 if (!(home_font = TTF_OpenFont(home_path, HOME_SIZE))) {
159 fprintf(stderr, "Could not load font: %s\n", TTF_GetError());
164 /* Load sound effects */
165 sprintf(pick_path, "%s/pick.wav", RES_PATH);
166 if ((pick = Mix_LoadWAV(pick_path)) == NULL) {
167 fprintf(stderr, "Unable to load sound: %s\n",
173 /* store the font's color */
178 /* render the title text
179 if (!(title_text = TTF_RenderText_Solid(title_font,
180 "Mancala", font_color)))
181 fprintf(stderr, "TTF: %s\n", TTF_GetError());
186 /* define the position of the board in the screen */
188 board_rect.y = Y_OFFSET;
192 /* set window properties and create it */
193 SDL_WM_SetCaption("Mancala", "Mancala");
194 if (sprintf(icon_path, "%s/icon.png", RES_PATH) == 0)
195 fprintf(stderr, "Problems assembling icon path.\n");
196 SDL_WM_SetIcon(LoadRes(icon_path), NULL);
197 if ((screen = SDL_SetVideoMode(tile->w*8, (tile->h*2) + Y_OFFSET, 16, SDL_FULLSCREEN))
199 fprintf(stderr, "Unable to set %dx%d video: %s", tile->w*8,
200 tile->h*2, SDL_GetError());
206 /* define the font color fot the exit text */
207 font_color_exit.r = 255;
208 font_color_exit.r = 255;
209 font_color_exit.r = 255;
213 if (!(exit_text = TTF_RenderText_Blended(home_font, "EXIT",
215 fprintf(stderr, "SDL_ttf: %s\n", TTF_GetError());
224 SDL_BlitSurface(exit_text, NULL, screen, &exit_rect);
233 while (state != GAME_WON) {
235 /* check for GTK events... */
236 /* otherwise hildon thinks the app hangs... */
237 while (gtk_events_pending ())
238 gtk_main_iteration ();
240 /* figure out if anything important happened */
241 old_highlight = highlight;
242 while (SDL_PollEvent(&event)) {
243 /* BAIL OUT! BAIL OUT! */
244 if (event.type == SDL_KEYDOWN){
245 fprintf(stderr, "event SDL_KEYDOWN found....\n");
246 if ( event.key.keysym.sym == SDLK_q )
248 fprintf(stderr, "event SDLK_q found....\n");
249 SDL_Event quit_event;
250 quit_event.type = SDL_QUIT;
251 SDL_PushEvent(&quit_event);
255 if (event.type == SDL_MOUSEBUTTONDOWN) {
256 if ((event.button.button = 1) &&
257 (event.button.y < Y_OFFSET)) {
258 fprintf(stderr, "clicked out side the board in exit area...\n");
259 SDL_Event quit_event;
260 quit_event.type = SDL_QUIT;
261 SDL_PushEvent(&quit_event);
265 if (event.type == SDL_QUIT) {
266 fprintf(stderr, "event SDL_QUIT found....\n");
272 if (state == HMN_WAIT) {
273 switch (event.type) {
274 case SDL_MOUSEBUTTONDOWN:
275 if ((event.button.button = 1) &&
276 (event.button.y < tile->h) &&
277 (event.button.y > Y_OFFSET)) {
280 pitch = event.button.x / tile->w;
282 // pitch 0 and 7 are the homebases which you can't play
283 if ( pitch == 0 || pitch == 7 ){
284 fprintf(stderr, "clicked out side the board...\n");
287 current_move = pitch;
292 case SDL_MOUSEMOTION:
293 if (event.motion.y < tile->h) {
294 highlight = event.motion.x / tile->w;
299 case SDL_ACTIVEEVENT:
300 if (event.active.gain == 0)
306 SDL_Delay(DELAY_MAX);
309 if (gameWon(aiBoard, humanBoard) == 1)
312 /* happy happy state machine */
316 if (highlight != old_highlight)
320 human_last_move = move(humanBoard,aiBoard,current_move);
323 if (human_last_move == 0)
327 printf("Human moving from %d to %d, now %d\n", current_move, human_last_move, state);
332 current_move = aiMove(aiBoard, humanBoard);
336 printf("Computer moving\n");
337 ai_last_move = move(aiBoard,humanBoard,current_move);
340 if (ai_last_move == 0)
346 if (aiBoard[0] > humanBoard[0]) {
347 if (!(title_text = TTF_RenderText_Blended(title_font,
348 "Computer Wins!", font_color)))
349 fprintf(stderr, "TTF: %s\n", TTF_GetError());
352 if (!(title_text = TTF_RenderText_Blended(title_font,
353 "Human Wins!", font_color)))
354 fprintf(stderr, "TTF: %s\n", TTF_GetError());
360 /* redraw the board if needed */
361 if (redraw_board == 1) {
362 /*printf("redrawing board\n");*/
364 /* draw and blit the board */
365 board = DrawBoard(aiBoard, humanBoard, board_font,
366 home_font, tile, stone, active,
369 fprintf(stderr, "Could not draw the board.\n");
372 // board_rect = SurfaceToRect(board);
373 SDL_BlitSurface(board, NULL, screen,
377 /* draw, center, and blit the title */
379 title_rect = SurfaceToRect(title_text);
380 title_rect.x = ((screen->w - title_rect.w)/2);
381 title_rect.y = ((screen->h - title_rect.h)/2);
382 SDL_BlitSurface(title_text, NULL, screen,
386 SDL_UpdateRect(screen, 0,0,0,0);
401 int main(int argc, char **argv) {
403 fprintf(stderr, "mancala main loop\n");
405 hgw_context = hgw_context_compat_init(argc, argv);
407 fprintf(stderr, "Cannot init hildon-games-startup!\n");
410 hgw_compat_set_cb_exit(hgw_context, exit_callback);
411 hgw_compat_set_cb_quit(hgw_context, quit_callback);
412 hgw_compat_set_cb_flush(hgw_context, flush_callback);
413 if(!hgw_context_compat_check(hgw_context)) return 0;
415 /* hildon-games-wrapper part */
416 hgw_msg_compat_receive(hgw_context, 0);
423 hgw_context_compat_destroy_deinit(hgw_context);