/// \file boardscreen.cpp
/// Main source file for main game screen manager
#include <allegro.h>
#include "boardscreen.h"
#include "widgets.h"
using namespace std;
BoardScreen::BoardScreen(): playCard(NULL), running(true), mode(M_SelectCard)
{
show_os_cursor(2);
board = new BoardManager();
LoadGraphics();
InitializeGui();
}
BoardScreen::~BoardScreen()
{
delete board;
delete mainScreen;
for(vector<BITMAP*>::iterator i = gfx_cards.begin(); i != gfx_cards.end(); i++)
destroy_bitmap(*i);
for(vector<BITMAP*>::iterator i = gfx_objects.begin(); i != gfx_objects.end(); i++)
destroy_bitmap(*i);
}
void BoardScreen::Run()
{
mainScreen->Begin();
while(running)
{
active = board->Advance();
if(!active)
break;
mainScreen->Continue();
}
mainScreen->Stop();
}
void BoardScreen::InitializeGui()
{
mainScreen = new WidgetManager();
mainScreen->w = SCREEN_W;
mainScreen->h = SCREEN_H;
mainScreen->background = makecol(32, 32, 32);
// Create quit button
Button* quit = new Button(mainScreen);
mainScreen->widgets.push_back(quit);
quit->text = "Quit";
quit->h = 8 + text_height(font);
quit->w = 8 + text_length(font, quit->text.c_str());
quit->x = BoardPadding;
quit->y = SCREEN_H - quit->h - BoardPadding;
quit->onClick += new Delegate<BoardScreen>(this, &BoardScreen::OnQuit);
// Create console
Console* console = new Console(this);
mainScreen->widgets.push_back(console);
console->x = BoardManager::Width * (BoardManager::SpaceSize + 1) + 2 * BoardPadding;
console->w = SCREEN_W - console->x;
console->h = 15 * (text_height(font) + 1) - 3;
console->y = SCREEN_H - console->h;
// Create the hand buttons
const int hx = BoardManager::Width * (BoardManager::SpaceSize + 1) + 2 * BoardPadding + CardAreaPadding;
const int nc = (SCREEN_W - hx) / (CardSize + CardPadding * 2);
const int hax = ((SCREEN_W - hx) - (nc * (CardSize + CardPadding * 2))) / 2 - CardPadding;
for(int i = 0; i < 15; i++)
{
CardButton * cb = new CardButton(this, i);
cb->x = hx + hax + 1 + (i % nc) * (CardSize + CardPadding * 2);
cb->y = TopMargin + (i / nc) * (CardSize + CardPadding * 2);
cb->w = CardSize + CardPadding * 2 + 1;
cb->h = CardSize + CardPadding * 2 + 1;
mainScreen->widgets.push_back(cb);
}
// Create the space buttons
for(int y = 0; y < BoardManager::Height; y++)
{
for(int x = 0; x < BoardManager::Width; x++)
{
SpaceButton* sb = new SpaceButton(this, x + y * BoardManager::Width);
sb->x = BoardPadding + 1 + x * (BoardManager::SpaceSize + 1);
sb->y = BoardPadding + 1 + y * (BoardManager::SpaceSize + 1);
sb->w = BoardManager::SpaceSize + 2;
sb->h = BoardManager::SpaceSize + 2;
mainScreen->widgets.push_back(sb);
}
}
// Create "Play Card" button
playCard = new Button(mainScreen);
mainScreen->widgets.push_back(playCard);
playCard->text = "Play Card";
playCard->h = 10 + text_height(font);
playCard->w = SCREEN_W - hx - (hax + CardPadding) * 2;
playCard->x = hx + hax + 1;
playCard->y = console->y - 3 - playCard->h;
playCard->visible = false;
}
void BoardScreen::LoadGraphics()
{
const char* filename;
BITMAP* bmp;
ostringstream str;
bmp = create_bitmap(CardSize, CardSize);
clear(bmp);
rect(bmp, 0, 0, CardSize - 1, CardSize - 1, makecol(255, 255, 255));
line(bmp, 2, 2, 7, 7, makecol(255, 0, 0));
line(bmp, 2, 7, 7, 2, makecol(255, 0, 0));
gfx_cards.push_back(bmp);
bmp = create_bitmap(BoardManager::SpaceSize, BoardManager::SpaceSize);
clear(bmp);
line(bmp, 1, 1, 4, 4, makecol(255, 0, 0));
line(bmp, 1, 4, 4, 1, makecol(255, 0, 0));
gfx_objects.push_back(bmp);
push_config_state();
set_config_file("graphics/graphics.ini");
int card = 1;
str << card;
while((filename = get_config_string("cards", str.str().c_str(), NULL)))
{
string fullname = "graphics/";
fullname += filename;
bmp = load_bitmap(fullname.c_str(), NULL);
if(!bmp)
break;
gfx_cards.push_back(bmp);
card++;
str.str("");
str << card;
}
int object = 1;
str << object;
while((filename = get_config_string("objects", str.str().c_str(), NULL)))
{
string fullname = "graphics/";
fullname += filename;
bmp = load_bitmap(fullname.c_str(), NULL);
if(!bmp)
break;
gfx_objects.push_back(bmp);
object++;
str.str("");
str << object;
}
}
void BoardScreen::OnQuit(void* args)
{
running = false;
mainScreen->Pause();
}
void BoardScreen::DrawCard(BITMAP* to, int x, int y, int card)
{
if(card >= gfx_cards.size())
card = 0;
BITMAP* bmp = gfx_cards[card];
blit(bmp, to, 0, 0, x, y, CardSize, CardSize);
}
void BoardScreen::DrawObject(BITMAP* to, int x, int y, int object)
{
if(object >= gfx_objects.size())
object = 0;
BITMAP* bmp = gfx_objects[object];
masked_blit(bmp, to, 0, 0, x, y, BoardManager::SpaceSize, BoardManager::SpaceSize);
}
CardButton::CardButton(BoardScreen* board, int myCard): Widget(board->mainScreen), board(board), myCard(myCard)
{
}
void CardButton::Redraw(RedrawEventArgs* e)
{
/* Player& p = board->board->player[board->board->turn];
if(myCard >= p.hand.size())
return;
board->DrawCard(e->to, x + BoardScreen::CardPadding + 1, y + BoardScreen::CardPadding + 1, p.hand[myCard]->graphic);
rect(e->to, x, y, x + w - 1, y + h - 1, makecol(0, 255, 0));*/
}
SpaceButton::SpaceButton(BoardScreen* board, int mySpace): Widget(board->mainScreen), board(board), mySpace(mySpace)
{
}
void SpaceButton::Redraw(RedrawEventArgs* e)
{
const int gcol = makecol(96, 96, 96);
const int red = makecol(64, 32, 32);
const int blue = makecol(32, 32, 64);
const int pink = makecol(64, 32, 64);
int c = makecol(0, 0, 0);
if(board->board->spaces[mySpace].status == 1)
c = red;
else if(board->board->spaces[mySpace].status == 2)
c = blue;
else if(board->board->spaces[mySpace].status == 3)
c = pink;
rectfill(e->to, x + 1, y + 1, x + w - 2, y + h - 2, c);
/*Board::Object* o = board->board->spaces[mySpace].object;
if(!o)
o = board->board->spaces[mySpace].background;
if(o)
board->DrawObject(e->to, x + 1, y + 1, o->graphic);*/
rect(e->to, x, y, x + w - 1, y + h - 1, gcol);
/*if(player.local && sel_mode == SM_Space)
{
if(mouse_x >= sx && mouse_x < sx + Board::SpaceSize && mouse_y >= sy && mouse_y < sy + Board::SpaceSize)
{
rect(to, sx - 1, sy - 1, sx + Board::SpaceSize, sy + Board::SpaceSize, makecol(255, 255, 255));
}
}*/
}
Console::Console(BoardScreen* board): Widget(board->mainScreen), board(board)
{
}
void Console::Redraw(RedrawEventArgs* e)
{
const int nl = 15;
const int nc = (w - 3) / text_length(font, " ");
const int th = text_height(font) + 1;
hline(e->to, x, y, x + w - 1, makecol(255, 255, 255));
vline(e->to, x, y, y + h - 1, makecol(255, 255, 255));
rectfill(e->to, x + 1, y + 1, x + w - 1, y + h - 1, makecol(0, 0, 0));
int ly = y + h;
for(list<string>::reverse_iterator i = board->board->board->log.rbegin(); i != board->board->board->log.rend(); i++)
{
for(int j = i->length() - (i->length() % nc); j >= 0; j -= nc)
{
ly -= th;
if(ly <= y + 3)
break;
textprintf_ex(e->to, font, x + 3, ly, makecol(255, 255, 255), -1, "%s", i->substr(j, nc).c_str());
}
}
}
// The end