-/*
- * Main Mancala Program Module Source -- main.c
- * $Id: main.c,v 1.7.2.25 2004/01/17 06:56:23 sparrow_hawk Exp $
- *
- * Copyright (C) 2003 Kevin Riggle
- * http://cmancala.sourcefoge.net
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the
- * Free Software Foundation; either version 2, or (at your option) any
- * later version.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details, a copy of which may be found in
- * the file COPYING provided in the main directory of this release.
- *
- */
+/*
+* for Maemo renamed to play.c and some bugfixes (2009, Reto Zingg)
+*
+* Main Mancala Program Module Source -- main.c
+* $Id: main.c,v 1.7.2.25 2004/01/17 06:56:23 sparrow_hawk Exp $
+*
+* Copyright (C) 2003 Kevin Riggle
+* http://cmancala.sourcefoge.net
+* Copyright (C) 2009 Reto Zingg
+*
+* This program is free software; you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by the
+* Free Software Foundation; either version 2, or (at your option) any
+* later version.
+*
+* This program is distributed in the hope that it will be useful, but
+* WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* General Public License for more details, a copy of which may be found in
+* the file COPYING provided in the main directory of this release.
+*
+*/
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
+#include <hildon/hildon.h>
+#include <hgw/hgw.h>
#include <unistd.h>
+#include "SDL_mixer.h"
#include "SDL.h"
#include "SDL_image.h"
#include "SDL_ttf.h"
+#include "SDL_getenv.h"
-#include "main.h"
#include "graphics.h"
#include "mancala.h"
#include "ai.h"
+#include "sounds.h"
+#include "play.h"
+
+#include "callbacks.h"
+
+// the surfaces
+SDL_Surface *screen, *board, *title_text, *tile, *stone[STONE_MAX+1], *exit_text;
+// the fonts
+TTF_Font *title_font, *home_font, *board_font;
+// The sound effects that will be used
+Mix_Chunk *pick = NULL;
+// the hildon-game-wrapper context
+HgwContext *hgw_context = NULL;
+
+// for debugging, example:
+
+// rmz_debug(g_strdup_printf("play step 2: SDL_AUDIODRIVER: %s",
+// my_sdl_audiodrv)
+// );
+
+// void rmz_debug (char *msg){
+// FILE *pFile;
+//
+// pFile = fopen ("/home/rzingg/mancala.debug", "a");
+// if (pFile) {
+// fprintf (pFile, "rmz_debug: %s \n", msg);
+// fclose (pFile);
+// }
+// }
+
+void sdl_clean_up(){
+
+ int i = 0;
+
+ SDL_FreeSurface(board);
+ SDL_FreeSurface(tile);
+ SDL_FreeSurface(exit_text);
+ SDL_FreeSurface(title_text);
+ for(i=0; i<=STONE_MAX; i++)
+ SDL_FreeSurface(stone[i]);
+ SDL_FreeSurface(screen);
+ TTF_CloseFont(title_font);
+ TTF_CloseFont(board_font);
+ TTF_CloseFont(home_font);
+ Mix_FreeChunk(pick);
+ /* Make sure we clean up after ourselves */
+ TTF_Quit();
+ Mix_CloseAudio();
+ SDL_Quit();
+ return;
+}
-#define HMN_WAIT 1
-#define HMN_MOVE 2
-#define CMP_WAIT 3
-#define CMP_MOVE 4
-#define GAME_WON 0
-
-int main(int argc, char **argv) {
-
- SDL_Surface *screen, *board, *title_text, *tile, *stone[STONE_MAX+1];
- SDL_Rect board_rect, title_rect;
- SDL_Color font_color;
- SDL_Event event;
-
- TTF_Font *title_font, *home_font, *board_font;
-
- char tile_path[STRING_MAX], stone_path[STRING_MAX];
- char icon_path[STRING_MAX], title_path[STRING_MAX];
- char home_path[STRING_MAX], board_path[STRING_MAX];
- int aiBoard[BOARD_MAX+1], humanBoard[BOARD_MAX+1];
- int i, redraw_board, highlight, old_highlight, active;
- int current_move, ai_last_move, human_last_move, state;
-
- /* Set up the game board and game variables. */
- gameInit(aiBoard, humanBoard);
- current_move = 0;
- ai_last_move = human_last_move = -99;
-
- /* initialize our libraries */
- if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) < 0) {
- fprintf(stderr, "Unable to initialize SDL: %s\n",
- SDL_GetError());
- exit(1);
- }
-
- if (TTF_Init() < 0) {
- fprintf(stderr, "Unable to initialize SDL_ttf: %s\n",
- SDL_GetError());
- exit(1);
- }
-
- /* Make sure we clean up after ourselves */
- atexit(SDL_Quit);
- atexit(TTF_Quit);
-
- /* Load our images... PNGs now, maybe XPMs later */
- sprintf(tile_path, "%s/tile.png", RES_PATH);
- if ((tile = LoadRes(tile_path)) == NULL) {
- fprintf(stderr, "Unable to load resource: %s\n",
- SDL_GetError());
- return 1;
- }
-
- for (i=0; i<=STONE_MAX; i++) {
- if (sprintf(stone_path, "%s/stone%02d.png", RES_PATH, i) == 0)
- fprintf(stderr, "Problems assembling path.\n");
- if (!(stone[i] = LoadRes(stone_path))) {
- fprintf(stderr, "Unable to load resource: %s\n",
- SDL_GetError());
- return 1;
- }
- }
-
- /* Load our font(s) */
- if (sprintf(title_path, "%s/VeraSeBd.ttf", FONT_PATH) == 0)
- fprintf(stderr, "Problems assembling path.\n");
- if (!(title_font = TTF_OpenFont(title_path, TITLE_SIZE))) {
- fprintf(stderr, "Could not load font: %s\n", TTF_GetError());
- exit(1);
- }
-
- if (sprintf(board_path, "%s/VeraSeBd.ttf", FONT_PATH) == 0)
- fprintf(stderr, "Problems assembling path.\n");
- if (!(board_font = TTF_OpenFont(board_path, BOARD_SIZE))) {
- fprintf(stderr, "Could not load font: %s\n", TTF_GetError());
- exit(1);
- }
-
- if (sprintf(home_path, "%s/VeraSeBd.ttf", FONT_PATH) == 0)
- fprintf(stderr, "Problems assembling path.\n");
- if (!(home_font = TTF_OpenFont(home_path, HOME_SIZE))) {
- fprintf(stderr, "Could not load font: %s\n", TTF_GetError());
- exit(1);
- }
-
- /* store the font's color */
- font_color.r = 255;
- font_color.b = 255;
- font_color.g = 255;
-
- /* render the title text
- if (!(title_text = TTF_RenderText_Solid(title_font,
- "Mancala", font_color)))
- fprintf(stderr, "TTF: %s\n", TTF_GetError());
-*/
- title_text = NULL;
-
- /* set window properties and create it */
- SDL_WM_SetCaption("Mancala", "Mancala");
- if (sprintf(icon_path, "%s/icon.png", RES_PATH) == 0)
- fprintf(stderr, "Problems assembling icon path.\n");
- SDL_WM_SetIcon(LoadRes(icon_path), NULL);
- if ((screen = SDL_SetVideoMode(tile->w*8, tile->h*2, 16, SDL_SWSURFACE))
- == NULL) {
- fprintf(stderr, "Unable to set %dx%d video: %s", tile->w*8,
- tile->h*2, SDL_GetError());
- exit(1);
- }
-
-
- state = HMN_WAIT;
- redraw_board = 1;
- old_highlight = 0;
- highlight = 0;
- active = 0;
-
- /* GAME LOOP */
- while (state != GAME_WON) {
- /* figure out if anything important happened */
- old_highlight = highlight;
- while (SDL_PollEvent(&event)) {
- /* BAIL OUT! BAIL OUT! */
- if (event.type == SDL_QUIT) {
- SDL_FreeSurface(board);
- SDL_FreeSurface(tile);
- for(i=0; i<=STONE_MAX; i++)
- SDL_FreeSurface(stone[i]);
- TTF_CloseFont(title_font);
- TTF_CloseFont(board_font);
- TTF_CloseFont(home_font);
- exit(0);
- }
- /* get events */
- if (state == HMN_WAIT) {
- switch (event.type) {
- case SDL_MOUSEBUTTONDOWN:
- if ((event.button.button = 1) &&
- (event.button.y < tile->h)) {
- current_move = event.button.x / tile->w;
- state = HMN_MOVE;
- }
- break;
- case SDL_MOUSEMOTION:
- if (event.motion.y < tile->h) {
- highlight = event.motion.x / tile->w;
- }
- else
- highlight = 0;
- break;
- case SDL_ACTIVEEVENT:
- if (event.active.gain == 0)
- highlight = 0;
- break;
- }
- }
- }
- SDL_Delay(DELAY_MAX);
-
- /* GAME LOGIC */
- if (gameWon(aiBoard, humanBoard) == 1)
- state = GAME_WON;
-
- /* happy happy state machine */
- switch(state) {
- case HMN_WAIT:
- active = 0;
- if (highlight != old_highlight)
- redraw_board = 1;
- break;
- case HMN_MOVE:
- human_last_move = move(humanBoard,aiBoard,current_move);
- redraw_board = 1;
- if (human_last_move == 0)
- state = HMN_WAIT;
- else
- state = CMP_WAIT;
- printf("Human moving from %d to %d, now %d\n", current_move, human_last_move, state);
- break;
- case CMP_WAIT:
- SDL_Delay(5000);
- active = 1;
- current_move = aiMove(aiBoard, humanBoard);
- state = CMP_MOVE;
- break;
- case CMP_MOVE:
- printf("Computer moving\n");
- ai_last_move = move(aiBoard,humanBoard,current_move);
- redraw_board = 1;
- if (ai_last_move == 0)
- state = CMP_WAIT;
- else
- state = HMN_WAIT;
- break;
- case GAME_WON:
- if (aiBoard[0] > humanBoard[0]) {
- if (!(title_text = TTF_RenderText_Blended(title_font,
- "Computer Wins!", font_color)))
- fprintf(stderr, "TTF: %s\n", TTF_GetError());
- }
- else {
- if (!(title_text = TTF_RenderText_Blended(title_font,
- "Human Wins!", font_color)))
- fprintf(stderr, "TTF: %s\n", TTF_GetError());
- }
- redraw_board = 1;
- break;
- }
+int play() {
+
+ // Scratchbox needs different SDL_AudiDriver env
+ #if __i386__
+ putenv("SDL_AUDIODRIVER=alsa") ;
+ #endif /* __i386__ */
+
+ SDL_Rect board_rect, title_rect, exit_rect;
+ SDL_Color font_color;
+ SDL_Event event;
+ SDL_Color font_color_exit;
+
+ char tile_path[STRING_MAX], stone_path[STRING_MAX];
+ char icon_path[STRING_MAX], title_path[STRING_MAX];
+ char home_path[STRING_MAX], board_path[STRING_MAX];
+ char pick_path[STRING_MAX];
+ int aiBoard[BOARD_MAX+1], humanBoard[BOARD_MAX+1];
+ int i, redraw_board, highlight, old_highlight, active;
+ int current_move, ai_last_move, human_last_move, state;
+
+ /* Set up the game board and game variables. */
+ gameInit(aiBoard, humanBoard);
+ current_move = 0;
+ ai_last_move = human_last_move = -99;
+
+ /* initialize our libraries */
+ //if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_AUDIO) < 0) {
+ if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
+ printf("Unable to initialize SDL: %s\n",
+ SDL_GetError());
+ sdl_clean_up();
+ return 1;
+ }
+
+ if (TTF_Init() < 0) {
+ printf("Unable to initialize SDL_ttf: %s\n",
+ SDL_GetError());
+ sdl_clean_up();
+ return 1;
+ }
+
+ //Initialize SDL_mixer
+ if( Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 4096 ) == -1 )
+ // if( Mix_OpenAudio(22050, (Uint16)AUDIO_U8, 2, 256 ) == -1 )
+ {
+ printf("Unable to initialize Mix_OpenAudio: %s\n",
+ SDL_GetError());
+ sdl_clean_up();
+ return 1;
+ }
+
+ /* Load our images... PNGs now, maybe XPMs later */
+ sprintf(tile_path, "%s/tile.png", RES_PATH);
+ if ((tile = LoadRes(tile_path)) == NULL) {
+ printf("Unable to load resource: %s\n",
+ SDL_GetError());
+ sdl_clean_up();
+ return 1;
+ }
+
+ for (i=0; i<=STONE_MAX; i++) {
+ if (sprintf(stone_path, "%s/stone%02d.png", RES_PATH, i) == 0)
+ printf("Problems assembling path.\n");
+ if (!(stone[i] = LoadRes(stone_path))) {
+ printf("Unable to load resource: %s\n",
+ SDL_GetError());
+ sdl_clean_up();
+ return 1;
+ }
+ }
+
+ /* Load our font(s) */
+ if (sprintf(title_path, "%s/luxisr.ttf", FONT_PATH) == 0)
+ printf("Problems assembling path.\n");
+ if (!(title_font = TTF_OpenFont(title_path, TITLE_SIZE))) {
+ printf("Could not load font: %s\n", TTF_GetError());
+ sdl_clean_up();
+ return 1;
+ }
+
+ if (sprintf(board_path, "%s/luxisr.ttf", FONT_PATH) == 0)
+ printf("Problems assembling path.\n");
+ if (!(board_font = TTF_OpenFont(board_path, BOARD_SIZE))) {
+ printf("Could not load font: %s\n", TTF_GetError());
+ sdl_clean_up();
+ return 1;
+ }
+
+ if (sprintf(home_path, "%s/luxisr.ttf", FONT_PATH) == 0)
+ printf("Problems assembling path.\n");
+ if (!(home_font = TTF_OpenFont(home_path, HOME_SIZE))) {
+ printf("Could not load font: %s\n", TTF_GetError());
+ sdl_clean_up();
+ return 1;
+ }
+
+ /* Load sound effects */
+ sprintf(pick_path, "%s/pick.wav", RES_PATH);
+ if ((pick = Mix_LoadWAV(pick_path)) == NULL) {
+ printf("Unable to load sound: %s\n",
+ SDL_GetError());
+ sdl_clean_up();
+ return 1;
+ }
+
+ /* store the font's color */
+ font_color.r = 255;
+ font_color.b = 255;
+ font_color.g = 255;
+
+ /* render the title text
+ if (!(title_text = TTF_RenderText_Solid(title_font,
+ "Mancala", font_color)))
+ fprintf(stderr, "TTF: %s\n", TTF_GetError());
+ */
+ title_text = NULL;
+
+
+ /* define the position of the board in the screen */
+ board_rect.x = 0;
+ board_rect.y = Y_OFFSET;
+ board_rect.w = 0;
+ board_rect.h = 0;
+
+ /* set window properties and create it */
+ SDL_WM_SetCaption("Mancala", "Mancala");
+ if (sprintf(icon_path, "%s/icon.png", RES_PATH) == 0)
+ printf("Problems assembling icon path.\n");
+ SDL_WM_SetIcon(LoadRes(icon_path), NULL);
+ if ((screen = SDL_SetVideoMode(tile->w*8, (tile->h*2) + Y_OFFSET, 16, SDL_FULLSCREEN))
+ == NULL) {
+ printf("Unable to set %dx%d video: %s", tile->w*8,
+ tile->h*2, SDL_GetError());
+ sdl_clean_up();
+ return 1;
+ }
+ SDL_ShowCursor(SDL_DISABLE);
+
+ /* define the font color fot the exit text */
+ font_color_exit.r = 255;
+ font_color_exit.r = 255;
+ font_color_exit.r = 255;
+
+ if (!(exit_text = TTF_RenderText_Blended(home_font, "EXIT",
+ font_color_exit))) {
+ printf("SDL_ttf: %s\n", TTF_GetError());
+ return 1;
+ }
+
+ exit_rect.x = 400;
+ exit_rect.y = 0;
+ exit_rect.w = 0;
+ exit_rect.h = 0;
+
+ SDL_BlitSurface(exit_text, NULL, screen, &exit_rect);
+
+ state = HMN_WAIT;
+ redraw_board = 1;
+ old_highlight = 0;
+ highlight = 0;
+ active = 0;
+
+ /* GAME LOOP */
+ while (state != GAME_WON) {
+
+ /* check for GTK events... */
+ /* otherwise hildon thinks the app hangs... */
+ while (gtk_events_pending ())
+ gtk_main_iteration ();
+
+ /* figure out if anything important happened */
+ old_highlight = highlight;
+ while (SDL_PollEvent(&event)) {
+ /* BAIL OUT! BAIL OUT! */
+ if (event.type == SDL_KEYDOWN){
+ printf("event SDL_KEYDOWN found....\n");
+ if ( event.key.keysym.sym == SDLK_q )
+ {
+ printf("event SDLK_q found....\n");
+ SDL_Event quit_event;
+ quit_event.type = SDL_QUIT;
+ SDL_PushEvent(&quit_event);
+ }
+ }
+
+ if (event.type == SDL_MOUSEBUTTONDOWN) {
+ if ((event.button.button = 1) &&
+ (event.button.y < Y_OFFSET)) {
+ printf("clicked out side the board in exit area...\n");
+ SDL_Event quit_event;
+ quit_event.type = SDL_QUIT;
+ SDL_PushEvent(&quit_event);
+ }
+ }
+
+ if (event.type == SDL_QUIT) {
+ printf("event SDL_QUIT found....\n");
+ sdl_clean_up();
+ return 0;
+ }
+
+ /* get events */
+ if (state == HMN_WAIT) {
+ switch (event.type) {
+ case SDL_MOUSEBUTTONDOWN:
+ if ((event.button.button = 1) &&
+ (event.button.y < tile->h) &&
+ (event.button.y > Y_OFFSET)) {
+
+ int pitch=0;
+ pitch = event.button.x / tile->w;
+
+ // pitch 0 and 7 are the homebases which you can't play
+ if ( pitch == 0 || pitch == 7 ){
+ printf("clicked out side the board...\n");
+ }
+ else{
+ current_move = pitch;
+ state = HMN_MOVE;
+ }
+ }
+ break;
+ case SDL_MOUSEMOTION:
+ if (event.motion.y < tile->h) {
+ highlight = event.motion.x / tile->w;
+ }
+ else
+ highlight = 0;
+ break;
+ case SDL_ACTIVEEVENT:
+ if (event.active.gain == 0)
+ highlight = 0;
+ break;
+ }
+ }
+ }
+ SDL_Delay(DELAY_MAX);
+
+ /* GAME LOGIC */
+ if (gameWon(aiBoard, humanBoard) == 1)
+ state = GAME_WON;
+
+ /* happy happy state machine */
+ switch(state) {
+ case HMN_WAIT:
+ active = 0;
+ if (highlight != old_highlight)
+ redraw_board = 1;
+ break;
+ case HMN_MOVE:
+ human_last_move = move(humanBoard,aiBoard,current_move);
+ play_sound(pick);
+ redraw_board = 1;
+ if (human_last_move == 0)
+ state = HMN_WAIT;
+ else
+ state = CMP_WAIT;
+ printf("Human moving from %d to %d, now %d\n", current_move, human_last_move, state);
+ break;
+ case CMP_WAIT:
+ SDL_Delay(DELAY_AI);
+ active = 1;
+ current_move = aiMove(aiBoard, humanBoard);
+ state = CMP_MOVE;
+ break;
+ case CMP_MOVE:
+ printf("Computer moving\n");
+ ai_last_move = move(aiBoard,humanBoard,current_move);
+ play_sound(pick);
+ redraw_board = 1;
+ if (ai_last_move == 0)
+ state = CMP_WAIT;
+ else
+ state = HMN_WAIT;
+ break;
+ case GAME_WON:
+ if (aiBoard[0] > humanBoard[0]) {
+ if (!(title_text = TTF_RenderText_Blended(title_font,
+ "Computer Wins!", font_color)))
+ printf("TTF: %s\n", TTF_GetError());
+ }
+ else {
+ if (!(title_text = TTF_RenderText_Blended(title_font,
+ "Human Wins!", font_color)))
+ printf("TTF: %s\n", TTF_GetError());
+ }
+ redraw_board = 1;
+ break;
+ }
+
+ /* redraw the board if needed */
+ if (redraw_board == 1) {
+ /*printf("redrawing board\n");*/
+
+ /* draw and blit the board */
+ board = DrawBoard(aiBoard, humanBoard, board_font,
+ home_font, tile, stone, active,
+ highlight);
+ if (!board) {
+ printf("Could not draw the board.\n");
+ }
+ else {
+ // board_rect = SurfaceToRect(board);
+ SDL_BlitSurface(board, NULL, screen,
+ &board_rect);
+ }
+
+ /* draw, center, and blit the title */
+ if (title_text) {
+ title_rect = SurfaceToRect(title_text);
+ title_rect.x = ((screen->w - title_rect.w)/2);
+ title_rect.y = ((screen->h - title_rect.h)/2);
+ SDL_BlitSurface(title_text, NULL, screen,
+ &title_rect);
+ }
+
+ SDL_UpdateRect(screen, 0,0,0,0);
+
+ redraw_board = 0;
+ }
+
+ hgw_msg_compat_receive(hgw_context, 0);
+
+ }
+
+ SDL_Delay(DELAY_AI);
+
+
+ sdl_clean_up();
+ return 0;
+
+}
- /* redraw the board if needed */
- if (redraw_board == 1) {
- /*printf("redrawing board\n");*/
- /* draw and blit the board */
- board = DrawBoard(aiBoard, humanBoard, board_font,
- home_font, tile, stone, active,
- highlight);
- if (!board) {
- fprintf(stderr, "Could not draw the board.\n");
- }
- else {
- board_rect = SurfaceToRect(board);
- SDL_BlitSurface(board, NULL, screen,
- &board_rect);
- }
+int main(int argc, char **argv) {
- /* draw, center, and blit the title */
- if (title_text) {
- title_rect = SurfaceToRect(title_text);
- title_rect.x = ((screen->w - title_rect.w)/2);
- title_rect.y = ((screen->h - title_rect.h)/2);
- SDL_BlitSurface(title_text, NULL, screen,
- &title_rect);
- }
-
- SDL_UpdateRect(screen, 0,0,0,0);
+ hgw_context = hgw_context_compat_init(argc, argv);
+ if (!hgw_context) {
+ printf("Cannot init hildon-games-startup!\n");
+ return 0;
+ }
- redraw_board = 0;
- }
+ hgw_compat_set_cb_exit(hgw_context, exit_callback);
+ hgw_compat_set_cb_quit(hgw_context, quit_callback);
+ hgw_compat_set_cb_flush(hgw_context, flush_callback);
+ if(!hgw_context_compat_check(hgw_context)) return 0;
- }
+ /* hildon-games-wrapper part */
+ hgw_msg_compat_receive(hgw_context, 0);
+ usleep(100);
- SDL_Delay(5000);
+ // Main game
+ play();
- return 0;
+ // hgw_context_compat_destroy_deinit(hgw_context);
+ // hgw_context_compat_destroy_quit(hgw_context);
+ hgw_context_destroy(hgw_context);
+ return 0;
}
-/* End main.c */