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"
32 #include "SDL_getenv.h"
40 #include "callbacks.h"
43 SDL_Surface *screen, *board, *title_text, *tile, *stone[STONE_MAX+1], *exit_text;
45 TTF_Font *title_font, *home_font, *board_font;
46 // The sound effects that will be used
47 Mix_Chunk *pick = NULL;
48 // the hildon-game-wrapper context
49 HgwContext *hgw_context = NULL;
51 // for debugging, example:
53 // rmz_debug(g_strdup_printf("play step 2: SDL_AUDIODRIVER: %s",
57 // void rmz_debug (char *msg){
60 // pFile = fopen ("/home/rzingg/mancala.debug", "a");
62 // fprintf (pFile, "rmz_debug: %s \n", msg);
71 SDL_FreeSurface(board);
72 SDL_FreeSurface(tile);
73 SDL_FreeSurface(exit_text);
74 SDL_FreeSurface(title_text);
75 for(i=0; i<=STONE_MAX; i++)
76 SDL_FreeSurface(stone[i]);
77 SDL_FreeSurface(screen);
78 TTF_CloseFont(title_font);
79 TTF_CloseFont(board_font);
80 TTF_CloseFont(home_font);
82 /* Make sure we clean up after ourselves */
91 // Scratchbox needs different SDL_AudiDriver env
93 putenv("SDL_AUDIODRIVER=alsa") ;
96 SDL_Rect board_rect, title_rect, exit_rect;
99 SDL_Color font_color_exit;
101 char tile_path[STRING_MAX], stone_path[STRING_MAX];
102 char icon_path[STRING_MAX], title_path[STRING_MAX];
103 char home_path[STRING_MAX], board_path[STRING_MAX];
104 char pick_path[STRING_MAX];
105 int aiBoard[BOARD_MAX+1], humanBoard[BOARD_MAX+1];
106 int i, redraw_board, highlight, old_highlight, active;
107 int current_move, ai_last_move, human_last_move, state;
109 /* Set up the game board and game variables. */
110 gameInit(aiBoard, humanBoard);
112 ai_last_move = human_last_move = -99;
114 /* initialize our libraries */
115 //if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_AUDIO) < 0) {
116 if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
117 printf("Unable to initialize SDL: %s\n",
123 if (TTF_Init() < 0) {
124 printf("Unable to initialize SDL_ttf: %s\n",
130 //Initialize SDL_mixer
131 if( Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 4096 ) == -1 )
132 // if( Mix_OpenAudio(22050, (Uint16)AUDIO_U8, 2, 256 ) == -1 )
134 printf("Unable to initialize Mix_OpenAudio: %s\n",
140 /* Load our images... PNGs now, maybe XPMs later */
141 sprintf(tile_path, "%s/tile.png", RES_PATH);
142 if ((tile = LoadRes(tile_path)) == NULL) {
143 printf("Unable to load resource: %s\n",
149 for (i=0; i<=STONE_MAX; i++) {
150 if (sprintf(stone_path, "%s/stone%02d.png", RES_PATH, i) == 0)
151 printf("Problems assembling path.\n");
152 if (!(stone[i] = LoadRes(stone_path))) {
153 printf("Unable to load resource: %s\n",
160 /* Load our font(s) */
161 if (sprintf(title_path, "%s/luxisr.ttf", FONT_PATH) == 0)
162 printf("Problems assembling path.\n");
163 if (!(title_font = TTF_OpenFont(title_path, TITLE_SIZE))) {
164 printf("Could not load font: %s\n", TTF_GetError());
169 if (sprintf(board_path, "%s/luxisr.ttf", FONT_PATH) == 0)
170 printf("Problems assembling path.\n");
171 if (!(board_font = TTF_OpenFont(board_path, BOARD_SIZE))) {
172 printf("Could not load font: %s\n", TTF_GetError());
177 if (sprintf(home_path, "%s/luxisr.ttf", FONT_PATH) == 0)
178 printf("Problems assembling path.\n");
179 if (!(home_font = TTF_OpenFont(home_path, HOME_SIZE))) {
180 printf("Could not load font: %s\n", TTF_GetError());
185 /* Load sound effects */
186 sprintf(pick_path, "%s/pick.wav", RES_PATH);
187 if ((pick = Mix_LoadWAV(pick_path)) == NULL) {
188 printf("Unable to load sound: %s\n",
194 /* store the font's color */
199 /* render the title text
200 if (!(title_text = TTF_RenderText_Solid(title_font,
201 "Mancala", font_color)))
202 fprintf(stderr, "TTF: %s\n", TTF_GetError());
207 /* define the position of the board in the screen */
209 board_rect.y = Y_OFFSET;
213 /* set window properties and create it */
214 SDL_WM_SetCaption("Mancala", "Mancala");
215 if (sprintf(icon_path, "%s/icon.png", RES_PATH) == 0)
216 printf("Problems assembling icon path.\n");
217 SDL_WM_SetIcon(LoadRes(icon_path), NULL);
218 if ((screen = SDL_SetVideoMode(tile->w*8, (tile->h*2) + Y_OFFSET, 16, SDL_FULLSCREEN))
220 printf("Unable to set %dx%d video: %s", tile->w*8,
221 tile->h*2, SDL_GetError());
225 SDL_ShowCursor(SDL_DISABLE);
227 /* define the font color fot the exit text */
228 font_color_exit.r = 255;
229 font_color_exit.r = 255;
230 font_color_exit.r = 255;
232 if (!(exit_text = TTF_RenderText_Blended(home_font, "EXIT",
234 printf("SDL_ttf: %s\n", TTF_GetError());
243 SDL_BlitSurface(exit_text, NULL, screen, &exit_rect);
252 while (state != GAME_WON) {
254 /* check for GTK events... */
255 /* otherwise hildon thinks the app hangs... */
256 while (gtk_events_pending ())
257 gtk_main_iteration ();
259 /* figure out if anything important happened */
260 old_highlight = highlight;
261 while (SDL_PollEvent(&event)) {
262 /* BAIL OUT! BAIL OUT! */
263 if (event.type == SDL_KEYDOWN){
264 printf("event SDL_KEYDOWN found....\n");
265 if ( event.key.keysym.sym == SDLK_q )
267 printf("event SDLK_q found....\n");
268 SDL_Event quit_event;
269 quit_event.type = SDL_QUIT;
270 SDL_PushEvent(&quit_event);
274 if (event.type == SDL_MOUSEBUTTONDOWN) {
275 if ((event.button.button = 1) &&
276 (event.button.y < Y_OFFSET)) {
277 printf("clicked out side the board in exit area...\n");
278 SDL_Event quit_event;
279 quit_event.type = SDL_QUIT;
280 SDL_PushEvent(&quit_event);
284 if (event.type == SDL_QUIT) {
285 printf("event SDL_QUIT found....\n");
291 if (state == HMN_WAIT) {
292 switch (event.type) {
293 case SDL_MOUSEBUTTONDOWN:
294 if ((event.button.button = 1) &&
295 (event.button.y < tile->h) &&
296 (event.button.y > Y_OFFSET)) {
299 pitch = event.button.x / tile->w;
301 // pitch 0 and 7 are the homebases which you can't play
302 if ( pitch == 0 || pitch == 7 ){
303 printf("clicked out side the board...\n");
306 current_move = pitch;
311 case SDL_MOUSEMOTION:
312 if (event.motion.y < tile->h) {
313 highlight = event.motion.x / tile->w;
318 case SDL_ACTIVEEVENT:
319 if (event.active.gain == 0)
325 SDL_Delay(DELAY_MAX);
328 if (gameWon(aiBoard, humanBoard) == 1)
331 /* happy happy state machine */
335 if (highlight != old_highlight)
339 human_last_move = move(humanBoard,aiBoard,current_move);
342 if (human_last_move == 0)
346 printf("Human moving from %d to %d, now %d\n", current_move, human_last_move, state);
351 current_move = aiMove(aiBoard, humanBoard);
355 printf("Computer moving\n");
356 ai_last_move = move(aiBoard,humanBoard,current_move);
359 if (ai_last_move == 0)
365 if (aiBoard[0] > humanBoard[0]) {
366 if (!(title_text = TTF_RenderText_Blended(title_font,
367 "Computer Wins!", font_color)))
368 printf("TTF: %s\n", TTF_GetError());
371 if (!(title_text = TTF_RenderText_Blended(title_font,
372 "Human Wins!", font_color)))
373 printf("TTF: %s\n", TTF_GetError());
379 /* redraw the board if needed */
380 if (redraw_board == 1) {
381 /*printf("redrawing board\n");*/
383 /* draw and blit the board */
384 board = DrawBoard(aiBoard, humanBoard, board_font,
385 home_font, tile, stone, active,
388 printf("Could not draw the board.\n");
391 // board_rect = SurfaceToRect(board);
392 SDL_BlitSurface(board, NULL, screen,
396 /* draw, center, and blit the title */
398 title_rect = SurfaceToRect(title_text);
399 title_rect.x = ((screen->w - title_rect.w)/2);
400 title_rect.y = ((screen->h - title_rect.h)/2);
401 SDL_BlitSurface(title_text, NULL, screen,
405 SDL_UpdateRect(screen, 0,0,0,0);
410 hgw_msg_compat_receive(hgw_context, 0);
423 int main(int argc, char **argv) {
425 hgw_context = hgw_context_compat_init(argc, argv);
427 printf("Cannot init hildon-games-startup!\n");
431 hgw_compat_set_cb_exit(hgw_context, exit_callback);
432 hgw_compat_set_cb_quit(hgw_context, quit_callback);
433 hgw_compat_set_cb_flush(hgw_context, flush_callback);
434 if(!hgw_context_compat_check(hgw_context)) return 0;
436 /* hildon-games-wrapper part */
437 hgw_msg_compat_receive(hgw_context, 0);
443 // hgw_context_compat_destroy_deinit(hgw_context);
444 // hgw_context_compat_destroy_quit(hgw_context);
445 // hgw_context_destroy(hgw_context);
446 hgw_context_destroy(hgw_context, HGW_BYE_INACTIVE);