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;
50 // void rmz_debug (char *msg){
53 // pFile = fopen ("/home/rzingg/mancala.debug", "a");
55 // fprintf (pFile, "rmz_debug: %s \n", msg);
64 SDL_FreeSurface(board);
65 SDL_FreeSurface(tile);
66 SDL_FreeSurface(exit_text);
67 SDL_FreeSurface(title_text);
68 for(i=0; i<=STONE_MAX; i++)
69 SDL_FreeSurface(stone[i]);
70 SDL_FreeSurface(screen);
71 TTF_CloseFont(title_font);
72 TTF_CloseFont(board_font);
73 TTF_CloseFont(home_font);
75 /* Make sure we clean up after ourselves */
83 // rmz_debug("play step 1");
84 SDL_Rect board_rect, title_rect, exit_rect;
87 SDL_Color font_color_exit;
89 // rmz_debug("play step 2");
90 char tile_path[STRING_MAX], stone_path[STRING_MAX];
91 char icon_path[STRING_MAX], title_path[STRING_MAX];
92 char home_path[STRING_MAX], board_path[STRING_MAX];
93 char pick_path[STRING_MAX];
94 int aiBoard[BOARD_MAX+1], humanBoard[BOARD_MAX+1];
95 int i, redraw_board, highlight, old_highlight, active;
96 int current_move, ai_last_move, human_last_move, state;
98 // rmz_debug("play step 3");
99 /* Set up the game board and game variables. */
100 gameInit(aiBoard, humanBoard);
102 ai_last_move = human_last_move = -99;
104 // rmz_debug("play step 4");
105 /* initialize our libraries */
106 //if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_AUDIO) < 0) {
107 if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
108 fprintf(stderr, "Unable to initialize SDL: %s\n",
114 // rmz_debug("play step 5");
115 if (TTF_Init() < 0) {
116 fprintf(stderr, "Unable to initialize SDL_ttf: %s\n",
122 // rmz_debug("play step 6");
123 //Initialize SDL_mixer
124 if( Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 4096 ) == -1 )
125 // if( Mix_OpenAudio(22050, (Uint16)AUDIO_U8, 2, 256 ) == -1 )
127 // rmz_debug(g_strdup_printf("Unable to initialize Mix_OpenAudio: %s\n",SDL_GetError()));
128 fprintf(stderr, "Unable to initialize Mix_OpenAudio: %s\n",
134 // rmz_debug("play step 7");
135 /* Load our images... PNGs now, maybe XPMs later */
136 sprintf(tile_path, "%s/tile.png", RES_PATH);
137 if ((tile = LoadRes(tile_path)) == NULL) {
138 fprintf(stderr, "Unable to load resource: %s\n",
144 // rmz_debug("play step 8");
145 for (i=0; i<=STONE_MAX; i++) {
146 if (sprintf(stone_path, "%s/stone%02d.png", RES_PATH, i) == 0)
147 fprintf(stderr, "Problems assembling path.\n");
148 if (!(stone[i] = LoadRes(stone_path))) {
149 fprintf(stderr, "Unable to load resource: %s\n",
156 // rmz_debug("play step 9");
157 /* Load our font(s) */
158 if (sprintf(title_path, "%s/luxisr.ttf", FONT_PATH) == 0)
159 fprintf(stderr, "Problems assembling path.\n");
160 if (!(title_font = TTF_OpenFont(title_path, TITLE_SIZE))) {
161 fprintf(stderr, "Could not load font: %s\n", TTF_GetError());
166 // rmz_debug("play step 10");
167 if (sprintf(board_path, "%s/luxisr.ttf", FONT_PATH) == 0)
168 fprintf(stderr, "Problems assembling path.\n");
169 if (!(board_font = TTF_OpenFont(board_path, BOARD_SIZE))) {
170 fprintf(stderr, "Could not load font: %s\n", TTF_GetError());
175 // rmz_debug("play step 11");
176 if (sprintf(home_path, "%s/luxisr.ttf", FONT_PATH) == 0)
177 fprintf(stderr, "Problems assembling path.\n");
178 if (!(home_font = TTF_OpenFont(home_path, HOME_SIZE))) {
179 fprintf(stderr, "Could not load font: %s\n", TTF_GetError());
184 // rmz_debug("play step 12");
185 /* Load sound effects */
186 sprintf(pick_path, "%s/pick.wav", RES_PATH);
187 if ((pick = Mix_LoadWAV(pick_path)) == NULL) {
188 fprintf(stderr, "Unable to load sound: %s\n",
194 // rmz_debug("play step 13");
195 /* store the font's color */
200 /* render the title text
201 if (!(title_text = TTF_RenderText_Solid(title_font,
202 "Mancala", font_color)))
203 fprintf(stderr, "TTF: %s\n", TTF_GetError());
208 /* define the position of the board in the screen */
210 board_rect.y = Y_OFFSET;
214 // rmz_debug("play step 14");
215 /* set window properties and create it */
216 SDL_WM_SetCaption("Mancala", "Mancala");
217 if (sprintf(icon_path, "%s/icon.png", RES_PATH) == 0)
218 fprintf(stderr, "Problems assembling icon path.\n");
219 SDL_WM_SetIcon(LoadRes(icon_path), NULL);
220 if ((screen = SDL_SetVideoMode(tile->w*8, (tile->h*2) + Y_OFFSET, 16, SDL_FULLSCREEN))
222 fprintf(stderr, "Unable to set %dx%d video: %s", tile->w*8,
223 tile->h*2, SDL_GetError());
227 SDL_ShowCursor(SDL_DISABLE);
229 // rmz_debug("play step 15");
230 /* define the font color fot the exit text */
231 font_color_exit.r = 255;
232 font_color_exit.r = 255;
233 font_color_exit.r = 255;
236 // rmz_debug("play step 16");
237 if (!(exit_text = TTF_RenderText_Blended(home_font, "EXIT",
239 fprintf(stderr, "SDL_ttf: %s\n", TTF_GetError());
248 SDL_BlitSurface(exit_text, NULL, screen, &exit_rect);
256 // rmz_debug("play step 17");
258 while (state != GAME_WON) {
260 /* check for GTK events... */
261 /* otherwise hildon thinks the app hangs... */
262 while (gtk_events_pending ())
263 gtk_main_iteration ();
265 /* figure out if anything important happened */
266 old_highlight = highlight;
267 while (SDL_PollEvent(&event)) {
268 /* BAIL OUT! BAIL OUT! */
269 if (event.type == SDL_KEYDOWN){
270 fprintf(stderr, "event SDL_KEYDOWN found....\n");
271 if ( event.key.keysym.sym == SDLK_q )
273 fprintf(stderr, "event SDLK_q found....\n");
274 SDL_Event quit_event;
275 quit_event.type = SDL_QUIT;
276 SDL_PushEvent(&quit_event);
280 if (event.type == SDL_MOUSEBUTTONDOWN) {
281 if ((event.button.button = 1) &&
282 (event.button.y < Y_OFFSET)) {
283 fprintf(stderr, "clicked out side the board in exit area...\n");
284 SDL_Event quit_event;
285 quit_event.type = SDL_QUIT;
286 SDL_PushEvent(&quit_event);
290 if (event.type == SDL_QUIT) {
291 fprintf(stderr, "event SDL_QUIT found....\n");
297 if (state == HMN_WAIT) {
298 switch (event.type) {
299 case SDL_MOUSEBUTTONDOWN:
300 if ((event.button.button = 1) &&
301 (event.button.y < tile->h) &&
302 (event.button.y > Y_OFFSET)) {
305 pitch = event.button.x / tile->w;
307 // pitch 0 and 7 are the homebases which you can't play
308 if ( pitch == 0 || pitch == 7 ){
309 fprintf(stderr, "clicked out side the board...\n");
312 current_move = pitch;
317 case SDL_MOUSEMOTION:
318 if (event.motion.y < tile->h) {
319 highlight = event.motion.x / tile->w;
324 case SDL_ACTIVEEVENT:
325 if (event.active.gain == 0)
331 SDL_Delay(DELAY_MAX);
334 if (gameWon(aiBoard, humanBoard) == 1)
337 /* happy happy state machine */
341 if (highlight != old_highlight)
345 human_last_move = move(humanBoard,aiBoard,current_move);
348 if (human_last_move == 0)
352 printf("Human moving from %d to %d, now %d\n", current_move, human_last_move, state);
357 current_move = aiMove(aiBoard, humanBoard);
361 printf("Computer moving\n");
362 ai_last_move = move(aiBoard,humanBoard,current_move);
365 if (ai_last_move == 0)
371 if (aiBoard[0] > humanBoard[0]) {
372 if (!(title_text = TTF_RenderText_Blended(title_font,
373 "Computer Wins!", font_color)))
374 fprintf(stderr, "TTF: %s\n", TTF_GetError());
377 if (!(title_text = TTF_RenderText_Blended(title_font,
378 "Human Wins!", font_color)))
379 fprintf(stderr, "TTF: %s\n", TTF_GetError());
385 /* redraw the board if needed */
386 if (redraw_board == 1) {
387 /*printf("redrawing board\n");*/
389 /* draw and blit the board */
390 board = DrawBoard(aiBoard, humanBoard, board_font,
391 home_font, tile, stone, active,
394 fprintf(stderr, "Could not draw the board.\n");
397 // board_rect = SurfaceToRect(board);
398 SDL_BlitSurface(board, NULL, screen,
402 /* draw, center, and blit the title */
404 title_rect = SurfaceToRect(title_text);
405 title_rect.x = ((screen->w - title_rect.w)/2);
406 title_rect.y = ((screen->h - title_rect.h)/2);
407 SDL_BlitSurface(title_text, NULL, screen,
411 SDL_UpdateRect(screen, 0,0,0,0);
416 hgw_msg_compat_receive(hgw_context, 0);
429 int main(int argc, char **argv) {
432 hgw_context = hgw_context_compat_init(argc, argv);
434 fprintf(stderr, "Cannot init hildon-games-startup!\n");
438 hgw_compat_set_cb_exit(hgw_context, exit_callback);
439 hgw_compat_set_cb_quit(hgw_context, quit_callback);
440 hgw_compat_set_cb_flush(hgw_context, flush_callback);
441 if(!hgw_context_compat_check(hgw_context)) return 0;
443 /* hildon-games-wrapper part */
444 hgw_msg_compat_receive(hgw_context, 0);
451 // rmz_debug("play > 1");
455 hgw_context_compat_destroy_deinit(hgw_context);