Commit df43b34b authored by Jonathan Michalon's avatar Jonathan Michalon

Beginning of implementation of the board.

parent 6d9944fd
......@@ -3,7 +3,8 @@ noinst_PROGRAMS = test
libovcc_la_CPPFLAGS = -DG_LOG_DOMAIN=\"libovcc\"
libovcc_la_SOURCES = tile.c \
tileset.c
tileset.c \
board.c
test_LDADD = libovcc.la -lpthread
test_SOURCES = test.c
/*
*
* Copyright (C) 2009 Colomban Wendling <ban@herbesfolles.org>
* Jonathan Michalon <studios.chalmion@no-log.org>
*
* 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 3 of the License, 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <glib.h>
#include "board.h"
#include "tile.h"
struct s_OVCCBoard
{
GHashTable *board;
gint leftedge;
gint topedge;
gint rightedge;
gint bottomedge;
};
struct s_OVCCPosition
{
gint xpos;
gint ypos;
};
static gpointer
compute_hash_key (OVCCPosition *pos)
{
/* FIXME is this a good way to determine a unique key from two values ??? */
return GINT_TO_POINTER (pos->xpos * 1000 + pos->ypos);
}
/**
* @brief Creates a new Board
* @returns The newly created Board
*
*/
OVCCBoard *
ovcc_board_new (void)
{
OVCCBoard *board;
board = g_malloc (sizeof *board);
if (board) {
board->board = g_hash_table_new_full (g_direct_hash, g_direct_equal,
NULL, NULL);
}
return board;
}
/**
* @brief Frees a Board
* @param set The Board to free
*
*/
void
ovcc_board_free (OVCCBoard *board)
{
if (board) {
g_hash_table_destroy (board->board);
g_free (board);
}
}
/**
* @brief Clears a Board
* @param set The Board to clear.
*
* Removes all tiles on the given board.
*/
void
ovcc_board_clear (OVCCBoard *board)
{
g_hash_table_remove_all (board->board);
}
/**
* @brief Get whether there is a tile at the given position
* @param board A Board
* @param pos The OVCCPosition of the tile to search for
* @returns TRUE if a tile is found there, FALSE otherwise.
*
* Searches for a tile on a given position of the board.
*/
gboolean
ovcc_board_is_tile_there (OVCCBoard *board,
OVCCPosition *pos)
{
return g_hash_table_lookup (board->board, compute_hash_key (pos)) != NULL;
}
/**
* @brief Add a Tile to a Board
* @param set The Board to use
* @param tile The Tile to add
* @param tile The Position to place it
*
* Add a tile at the given position on the board.
* This function doesn't perform any test of any kind.
*
*/
void
ovcc_board_add_tile (OVCCBoard *board,
OVCCTile *tile,
OVCCPosition *pos)
{
g_hash_table_insert (board->board, compute_hash_key (pos), tile);
board->leftedge = MIN (pos->xpos, board->leftedge);
board->topedge = MAX (pos->ypos, board->topedge);
board->rightedge = MAX (pos->xpos, board->rightedge);
board->bottomedge = MIN (pos->ypos, board->bottomedge);
}
......@@ -22,10 +22,20 @@
#define H_OVCC_BOARD
typedef struct s_OVCCBoard OVCCBoard;
#include "tile.h"
/*ovcc_board_*/
typedef struct s_OVCCBoard OVCCBoard;
typedef struct s_OVCCPosition OVCCPosition;
OVCCBoard *ovcc_board_new (void);
void ovcc_board_free (OVCCBoard *board);
void ovcc_board_clear (OVCCBoard *board);
gboolean ovcc_board_is_tile_there (OVCCBoard *board,
OVCCPosition *pos);
void ovcc_board_add_tile (OVCCBoard *board,
OVCCTile *tile,
OVCCPosition *pos);
#endif /* guard */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment