clean up and added own TTF file so we are independent from eff-content-fonts
[mancala] / src / main.c
index 2a2bc30..2214dcd 100644 (file)
-/*  
- *  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.
- *
- */
+/*
+*  Copyright (C) 2009 Reto Zingg
+*
+*  Some of the code is based on the examples on:
+*  http://wiki.maemo.org/Documentation/Maemo_5_Developer_Guide
+*
+*  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 <unistd.h>
+#include <hildon/hildon.h>
+#include <libosso.h>
+#include <gtk/gtk.h>
 
-#include "SDL.h"
-#include "SDL_image.h"
-#include "SDL_ttf.h"
-
-#include "main.h"
-#include "graphics.h"
-#include "mancala.h"
-#include "ai.h"
-
-#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;
-               }
-
-               /* 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);
-                       }
-
-                       /* 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;
-               }
-
-       }
-
-       SDL_Delay(5000);
-
-       return 0;
 
+#include "play.h"
+
+#define OSSO_SERVICE "com.nokia.mancala"
+
+osso_context_t *ossoContext = NULL;
+
+gint
+dbus_callback (const gchar *interface, const gchar *method,
+                GArray *arguments, gpointer data,
+                osso_rpc_t *retval)
+{
+        printf ("Msg dbus: %s, %s\n", interface, method);
+
+        if (!strcmp (method, "top_application"))
+                gtk_window_present (GTK_WINDOW (data));
+
+        retval->type = DBUS_TYPE_INVALID;
+        return OSSO_OK;
+}
+
+
+void play_wrapper (GtkWidget *win)
+{
+
+        fprintf(stderr, "Button start-play pressed\n");
+        if (play() > 0){
+                fprintf(stderr, "start play failed\n");
+        }
+        else{
+                fprintf(stderr, "successfull played...\n");
+        }
+
+        osso_application_top(ossoContext, OSSO_SERVICE, NULL);
+
+}
+
+static HildonAppMenu* create_menu (GtkWidget *win)
+        {
+                int i;
+                gchar *command_id;
+                GtkWidget * button;
+                HildonAppMenu *menu = HILDON_APP_MENU (hildon_app_menu_new ());
+                for (i = 1; i <= 2; i++) {
+                        /* Create menu entries */
+                        button = hildon_gtk_button_new (HILDON_SIZE_AUTO);
+                        command_id = g_strdup_printf ("Menu command %d", i);
+                        gtk_button_set_label (GTK_BUTTON (button), command_id);
+
+                        /* Attach callback to clicked signal */
+                        switch (i) {
+                                // Start game
+                                case 1:
+                                        command_id = g_strdup_printf ("Play");
+                                        gtk_button_set_label (GTK_BUTTON (button), command_id);
+                                        g_signal_connect_after (button, "clicked",
+                                                G_CALLBACK (play_wrapper),win);
+                                break;
+                                // Show help
+                                case 2:
+                                        command_id = g_strdup_printf ("Quit");
+                                        gtk_button_set_label (GTK_BUTTON (button), command_id);
+                                        g_signal_connect_after (button, "clicked",
+                                                G_CALLBACK (gtk_main_quit), NULL);
+                                break;
+                        }
+                        /* Add entry to the view menu */
+                        hildon_app_menu_append (menu, GTK_BUTTON (button));
+                }
+
+        gtk_widget_show_all (GTK_WIDGET (menu));
+        return menu;
+}
+
+
+int main (int argc, char **argv) {
+
+        osso_return_t ret;
+
+        g_print("Initializing LibOSSO\n");
+        ossoContext = osso_initialize(OSSO_SERVICE, "0.1", FALSE, NULL);
+        if (ossoContext == NULL) {
+                g_error("Failed to initialize LibOSSO\n");
+        }
+
+        GtkWidget *win;
+        GtkWidget *textbox;
+        GtkTextBuffer *textbox_buffer;
+        gchar *textbox_text;
+        GtkBox *vbox;
+        HildonAppMenu *menu;
+        PangoFontDescription *font_desc;
+        GtkWidget * button;
+        gchar *button_lable_text;
+        GtkWidget *pannable_text;
+
+        hildon_gtk_init (&argc, &argv);
+        win = hildon_window_new ();
+
+        /* Create and pack labels */     
+        textbox_text = g_strdup_printf("Welcome to Mancala, the ancient African game of skill!\n\nMancala is a very simple, easy-to-learn game. Each player begins with a horizontal row of holes or pits filled with a certain number of stones. At either end of the board are the players' home pits, which are used to keep score. In this case, the human player has the left home pit and the upper row holes. A move is made by clicking into the hole you wish to move from. The stones are then picked up and distributed, one to each hole, moving toward your home pit is reached, and then moving through your opponent's row, bypassing his/her home, and moving back towards in a circle (counterclockwise), until all the stones have been distributed.\n\nIf the last stone is placed in your home, you receive an extra turn. If the last stone lands in an empty hole on your side, you 'capture' the stones in the opposite hole on your opponent's side, moving them to your home.\n\nThe game ends when one player cannot play (ie. there are no stones on his/her side of the board.  The player who has the most stones on his/her *side* wins.");
+
+        textbox = hildon_text_view_new();
+        // gtk_text_view_set_editable(GTK_TEXT_VIEW(textbox), FALSE);
+        // gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(textbox), FALSE);
+        gtk_text_view_set_indent(GTK_TEXT_VIEW(textbox), 10);
+        gtk_text_view_set_left_margin(GTK_TEXT_VIEW(textbox), 10);
+        gtk_text_view_set_right_margin(GTK_TEXT_VIEW(textbox), 10);
+        gtk_text_view_set_pixels_above_lines(GTK_TEXT_VIEW(textbox), 3);
+        gtk_text_view_set_pixels_below_lines(GTK_TEXT_VIEW(textbox), 2);
+        gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(textbox), GTK_WRAP_WORD);
+        // gtk_widget_set_sensitive(textbox, FALSE);
+        font_desc = pango_font_description_from_string ("vera 18");
+        gtk_widget_modify_font(textbox, font_desc);
+        pango_font_description_free (font_desc);
+
+        textbox_buffer = hildon_text_view_get_buffer (HILDON_TEXT_VIEW (textbox));
+
+        gtk_text_buffer_set_text(textbox_buffer, textbox_text, -1);
+
+
+        pannable_text = hildon_pannable_area_new();
+        hildon_pannable_area_add_with_viewport (HILDON_PANNABLE_AREA(pannable_text), textbox);
+
+
+        vbox = GTK_BOX (gtk_vbox_new (FALSE, 2));
+
+        button = hildon_button_new (HILDON_SIZE_AUTO | HILDON_SIZE_FINGER_HEIGHT,
+                                        HILDON_BUTTON_ARRANGEMENT_VERTICAL);
+        button_lable_text = g_strdup_printf ("Play");
+        gtk_button_set_label (GTK_BUTTON (button), button_lable_text);
+        g_signal_connect_after (button, "clicked",
+                                 G_CALLBACK (play_wrapper),win);
+
+        gtk_box_pack_start (vbox, button, FALSE, TRUE, 1);
+        gtk_box_pack_start (vbox, pannable_text, TRUE, TRUE, 1);
+
+        /* Create menu */
+        menu = create_menu (win);
+
+        /* Attach menu to the window */
+        hildon_window_set_app_menu (HILDON_WINDOW (win), menu);
+
+        /* Add label's box to window */
+        gtk_container_add (GTK_CONTAINER (win), GTK_WIDGET (vbox));
+
+        g_signal_connect (win, "delete_event", G_CALLBACK (gtk_main_quit), NULL);
+
+        gtk_widget_show_all (win);
+
+        ret = osso_rpc_set_default_cb_f (ossoContext, dbus_callback, GTK_WINDOW(win));
+        if (ret != OSSO_OK)
+        {
+                fprintf (stderr, "osso_rpc_set_default_cb_f failed: %d.\n", ret);
+                exit (1);
+        }
+
+        gtk_main ();
+
+        osso_deinitialize(ossoContext);
+        ossoContext = NULL;
+
+        return 0;
 }
 
-/*  End main.c */