rename files
authorReto Zingg <g.d0b3rm4n@gmail.com>
Mon, 5 Oct 2009 14:57:27 +0000 (17:57 +0300)
committerReto Zingg <g.d0b3rm4n@gmail.com>
Mon, 5 Oct 2009 14:57:27 +0000 (17:57 +0300)
src/main.c [deleted file]
src/main.h [deleted file]
src/play.c [new file with mode: 0644]
src/play.h [new file with mode: 0644]

diff --git a/src/main.c b/src/main.c
deleted file mode 100644 (file)
index 2a2bc30..0000000
+++ /dev/null
@@ -1,281 +0,0 @@
-/*  
- *  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.
- *
- */
-
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <unistd.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;
-
-}
-
-/*  End main.c */
diff --git a/src/main.h b/src/main.h
deleted file mode 100644 (file)
index f89ca88..0000000
+++ /dev/null
@@ -1,35 +0,0 @@
-/*  
- *  Main Mancala Program Module Header -- main.h 
- *  $Id: main.h,v 1.1.2.8 2004/01/16 20:49:30 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.
- *
- */
-
-/* path to resource files */
-#define RES_PATH "/usr/share/pixmaps/mancala"
-#define FONT_PATH "/usr/share/fonts/bitstream-vera"
-
-/* various constants */
-#define STRING_MAX 1000
-#define STONE_MAX 10
-#define DELAY_MAX 1
-
-/* font sizes */
-#define TITLE_SIZE 75
-#define HOME_SIZE 50
-#define BOARD_SIZE 35
-
-/*  End main.h */
diff --git a/src/play.c b/src/play.c
new file mode 100644 (file)
index 0000000..2a2bc30
--- /dev/null
@@ -0,0 +1,281 @@
+/*  
+ *  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.
+ *
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.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;
+
+}
+
+/*  End main.c */
diff --git a/src/play.h b/src/play.h
new file mode 100644 (file)
index 0000000..f89ca88
--- /dev/null
@@ -0,0 +1,35 @@
+/*  
+ *  Main Mancala Program Module Header -- main.h 
+ *  $Id: main.h,v 1.1.2.8 2004/01/16 20:49:30 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.
+ *
+ */
+
+/* path to resource files */
+#define RES_PATH "/usr/share/pixmaps/mancala"
+#define FONT_PATH "/usr/share/fonts/bitstream-vera"
+
+/* various constants */
+#define STRING_MAX 1000
+#define STONE_MAX 10
+#define DELAY_MAX 1
+
+/* font sizes */
+#define TITLE_SIZE 75
+#define HOME_SIZE 50
+#define BOARD_SIZE 35
+
+/*  End main.h */