1 /*
2  * Biloba
3  * Copyright (C) 2004-2008 Guillaume Demougeot, Colin Leroy
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19 
20 /**
21  * Biloba - Q1 2005
22  * Game by Guillaume Demougeot <dmgt@wanadoo.fr>
23  * Code by Colin Leroy <colin@colino.net>
24  *
25  * This file contains the board handling code (drawing
26  * and placing pawns).
27  */
28 
29 #include <SDL.h>
30 #include <SDL_image.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include "utils.h"
34 #include "tile.h"
35 #include "pawn.h"
36 #include "player.h"
37 #include "options.h"
38 #include "font.h"
39 #include "msg.h"
40 
41 static SDL_Surface *border = NULL;
42 static SDL_Surface *player = NULL;
43 
board_show_help(void)44 static void board_show_help(void)
45 {
46 	static SDL_Surface *help = NULL;
47 #ifndef MAEMO
48 	if (help == NULL)
49 		help = biloba_load_image("commands.png");
50 #else
51 	if (help == NULL)
52 		help = biloba_load_image("commands-maemo.png");
53 #endif
54 	assert(help != NULL);
55 
56 	put_image(help, HELP_X, HELP_Y);
57 }
58 
59 /**
60  * Build the playing board from scratch, and display it on screen
61  */
board_build(void)62 void board_build(void)
63 {
64 	int x, y;
65 	int num = 0, nb_players = 0;
66 
67 	for (x = 0; x < MAX_TILES_X; x++) {
68 		for (y = 0; y < MAX_TILES_Y; y++) {
69 			tile_get(x, y)->pawn = NULL;
70 			tile_draw(tile_get(x, y));
71 		}
72 	}
73 
74 	for (nb_players = 0; nb_players < game_num_players(); nb_players++) {
75 		Player *p = NULL;
76 		for (num = 0; num < pawn_get_max(); num++) {
77 			pawn_draw(pawn_get(num, (PawnColor)nb_players, TRUE));
78 		}
79 		p = player_get((PawnColor)nb_players, TRUE, INPUT_LOCAL);
80 		player_set_name(p, options_get_player_name(nb_players));
81 		p->method = options_get_player_type(nb_players);
82 	}
83 
84 	if (!border) {
85 #ifndef MAEMO
86 		border = biloba_load_image("border.png");
87 #else
88 		border = biloba_load_image("border-maemo.png");
89 #endif
90 		assert(border != NULL);
91 	}
92 	if (!player) {
93 		player = biloba_load_image("player.png");
94 		assert(player != NULL);
95 	}
96 	put_image(border, X_OFFSET - 36, Y_OFFSET - 36);
97 	put_image(player, 5, YS - 65);
98 
99 	board_show_help();
100 	SDL_UpdateRect(screen, 0, 0, 0, 0);
101 }
102 
103 /**
104  * Destroy the board.
105  */
board_destroy(void)106 void board_destroy(void)
107 {
108 	tile_free_all();
109 	pawn_free_all();
110 }
111 
112 /**
113  * Set the player turn
114  *
115  * @param[in] player		The player that should play
116  * @param[in] display_your_turn	Whether to print "your turn"
117  */
board_set_player(Player * player,int display_your_turn)118 void board_set_player(Player *player, int display_your_turn)
119 {
120 	LList *pawns = pawn_get_all(player->color);
121 	Pawn *pawn;
122 	char *turn;
123 	int len;
124 
125 	if (display_your_turn)
126 		len = strlen(get_msg(M_YOUR_TURN)) + strlen(player->name) + 1;
127 	else
128 		len = strlen(get_msg(M_ROUND)) + strlen(player->name) + 1;
129 
130 	if (!pawns)
131 		return;
132 
133 	turn = malloc(len);
134 
135 	pawn = pawns->data;
136 
137 	put_image(pawn->surface, 15, YS - 45);
138 	SDL_UpdateRect(screen, 15, YS - 45, 30, 30);
139 
140 	if (display_your_turn)
141 		snprintf(turn, len, "%s%s", get_msg(M_YOUR_TURN), player->name);
142 	else
143 		snprintf(turn, len, "%s%s", get_msg(M_ROUND), player->name);
144 
145 	clear_text(-1, G_MSG_X, G_MSG_Y);
146 	draw_message(turn, G_MSG_X, G_MSG_Y, -1, FALSE);
147 
148 	free(turn);
149 
150 	llist_free(pawns);
151 }
152 
153 static int frozen = 0;
154 
155 /**
156  * Freeze GUI updates to the board
157  */
board_freeze(void)158 void board_freeze(void)
159 {
160 	frozen++;
161 }
162 
163 /**
164  * Thaw GUI updates to the board
165  */
board_thaw(void)166 void board_thaw(void) {
167 	frozen--;
168 	if (frozen < 0)
169 		frozen = 0;
170 }
171 
172 /**
173  * Get whether the board GUI updates are frozen
174  *
175  * @return TRUE if it is, FALSE if not
176  */
board_frozen(void)177 int board_frozen(void) {
178 	return frozen > 0 ? TRUE : FALSE;
179 }
180