bump up version number
[mancala] / src / play.c
index c833d56..cb761dd 100644 (file)
 */
 
 #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 "graphics.h"
 #include "mancala.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(){
         
@@ -65,6 +87,11 @@ void sdl_clean_up(){
 }
 
 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;
@@ -87,34 +114,33 @@ int play() {
         /* initialize our libraries */
         //if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_AUDIO) < 0) {
        if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
-               fprintf(stderr, "Unable to initialize SDL: %s\n", 
+               printf("Unable to initialize SDL: %s\n", 
                         SDL_GetError());
                         sdl_clean_up();
                         return 1;
        }
        
        if (TTF_Init() < 0) {
-               fprintf(stderr, "Unable to initialize SDL_ttf: %s\n", 
+               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 )
        {
-               fprintf(stderr, "Unable to initialize Mix_OpenAudio: %s\n",
+               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) {
-               fprintf(stderr, "Unable to load resource: %s\n", 
+               printf("Unable to load resource: %s\n", 
                         SDL_GetError());
                         sdl_clean_up();
                         return 1;
@@ -122,9 +148,9 @@ int play() {
        
        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");
+                       printf("Problems assembling path.\n");
                if (!(stone[i] = LoadRes(stone_path))) {
-                       fprintf(stderr, "Unable to load resource: %s\n",
+                       printf("Unable to load resource: %s\n",
                                 SDL_GetError());
                                 sdl_clean_up();
                                 return 1;
@@ -133,25 +159,25 @@ int play() {
        
        /* Load our font(s) */
        if (sprintf(title_path, "%s/luxisr.ttf", FONT_PATH) == 0)
-               fprintf(stderr, "Problems assembling path.\n");
+               printf("Problems assembling path.\n");
        if (!(title_font = TTF_OpenFont(title_path, TITLE_SIZE))) {
-               fprintf(stderr, "Could not load font: %s\n", TTF_GetError());
+               printf("Could not load font: %s\n", TTF_GetError());
                sdl_clean_up();
                return 1;
        }
        
        if (sprintf(board_path, "%s/luxisr.ttf", FONT_PATH) == 0)
-               fprintf(stderr, "Problems assembling path.\n");
+               printf("Problems assembling path.\n");
        if (!(board_font = TTF_OpenFont(board_path, BOARD_SIZE))) {
-               fprintf(stderr, "Could not load font: %s\n", TTF_GetError());
+               printf("Could not load font: %s\n", TTF_GetError());
                sdl_clean_up();
                return 1;
        }
        
        if (sprintf(home_path, "%s/luxisr.ttf", FONT_PATH) == 0)
-               fprintf(stderr, "Problems assembling path.\n");
+               printf("Problems assembling path.\n");
        if (!(home_font = TTF_OpenFont(home_path, HOME_SIZE))) {
-               fprintf(stderr, "Could not load font: %s\n", TTF_GetError());
+               printf("Could not load font: %s\n", TTF_GetError());
                sdl_clean_up();
                return 1;
        }
@@ -159,7 +185,7 @@ int play() {
        /* Load sound effects */ 
        sprintf(pick_path, "%s/pick.wav", RES_PATH); 
        if ((pick = Mix_LoadWAV(pick_path)) == NULL) { 
-               fprintf(stderr, "Unable to load sound: %s\n", 
+               printf("Unable to load sound: %s\n", 
                         SDL_GetError());
                         sdl_clean_up();
                         return 1; 
@@ -185,29 +211,27 @@ int play() {
        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)
-               fprintf(stderr, "Problems assembling icon path.\n");
-       SDL_WM_SetIcon(LoadRes(icon_path), NULL);
+       // 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) {
-               fprintf(stderr, "Unable to set %dx%d video: %s", tile->w*8,
+               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))) {
-                     fprintf(stderr, "SDL_ttf: %s\n", TTF_GetError());
+                     printf("SDL_ttf: %s\n", TTF_GetError());
                      return 1;
         }
        
@@ -237,10 +261,10 @@ int play() {
                while (SDL_PollEvent(&event)) {
                        /* BAIL OUT! BAIL OUT! */
                        if (event.type == SDL_KEYDOWN){
-                               fprintf(stderr, "event SDL_KEYDOWN found....\n");
+                               printf("event SDL_KEYDOWN found....\n");
                                if ( event.key.keysym.sym == SDLK_q )
                                {
-                                       fprintf(stderr, "event SDLK_q found....\n");
+                                       printf("event SDLK_q found....\n");
                                        SDL_Event quit_event;
                                        quit_event.type = SDL_QUIT;
                                        SDL_PushEvent(&quit_event);
@@ -250,7 +274,7 @@ int play() {
                        if (event.type == SDL_MOUSEBUTTONDOWN) {
                                if ((event.button.button = 1) &&
                                    (event.button.y < Y_OFFSET)) {
-                                       fprintf(stderr, "clicked out side the board in exit area...\n");
+                                       printf("clicked out side the board in exit area...\n");
                                        SDL_Event quit_event;
                                        quit_event.type = SDL_QUIT;
                                        SDL_PushEvent(&quit_event);
@@ -258,7 +282,7 @@ int play() {
                        }
                        
                        if (event.type == SDL_QUIT) {
-                               fprintf(stderr, "event SDL_QUIT found....\n");
+                               printf("event SDL_QUIT found....\n");
                                sdl_clean_up();
                                return 0;
                        }
@@ -276,7 +300,7 @@ int play() {
 
                                                         // pitch 0 and 7 are the homebases which you can't play
                                                         if ( pitch == 0 || pitch == 7 ){
-                                                                 fprintf(stderr, "clicked out side the board...\n");
+                                                                 printf("clicked out side the board...\n");
                                                         }
                                                         else{
                                                                 current_move = pitch;
@@ -341,12 +365,12 @@ int play() {
                                if (aiBoard[0] > humanBoard[0]) {
                                        if (!(title_text = TTF_RenderText_Blended(title_font, 
                                                "Computer Wins!", font_color)))
-                                               fprintf(stderr, "TTF: %s\n", TTF_GetError());
+                                               printf("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());
+                                               printf("TTF: %s\n", TTF_GetError());
                                }
                                redraw_board = 1;
                                break;
@@ -361,7 +385,7 @@ int play() {
                                           home_font, tile, stone, active, 
                                           highlight);
                                           if (!board) {
-                                                  fprintf(stderr, "Could not draw the board.\n");
+                                                  printf("Could not draw the board.\n");
                                           }
                                           else {
                                                   // board_rect = SurfaceToRect(board);
@@ -383,12 +407,44 @@ int play() {
                                           redraw_board = 0;
                }
                
+               hgw_msg_compat_receive(hgw_context, 0);
+               
        }
        
        SDL_Delay(DELAY_AI);
        
+       
        sdl_clean_up();
        return 0;
        
 }
 
+
+int main(int argc, char **argv) {
+
+    hgw_context = hgw_context_compat_init(argc, argv);
+    if (!hgw_context) {
+        printf("Cannot init hildon-games-startup!\n");
+        return 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);
+
+    // Main game
+    play();
+
+    // hgw_context_compat_destroy_deinit(hgw_context);
+    // hgw_context_compat_destroy_quit(hgw_context);
+    // hgw_context_destroy(hgw_context);
+    hgw_context_destroy(hgw_context, HGW_BYE_INACTIVE);
+
+    return 0;
+}
+