1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
|
/*
* Spider-Arachnid: A plugin for the Video Disk Recorder
*
* Copyright (C) 2005-2007, Thomas Gnther <tom@toms-cafe.de>
*
* 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 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* $Id: game.cpp 97 2007-09-24 22:29:48Z tom $
*/
#include "game.h"
#include "deck.h"
#include "tableau.h"
#include "heap.h"
#include "setup.h"
#include "bitmap.h"
#include "i18n.h"
#include <vdr/config.h>
#include <vdr/osdbase.h>
#include <vdr/osd.h>
using namespace SpiderPlugin;
using namespace Spider;
// Definitions for bitmaps
const int cursorWidth = 16;
const int cursorHeight = 22;
const int cardWidth = 71;
const int cardHeight = 96;
const char* cursorName = "cursor";
const char* coverName = "cover";
const char* suitNames[] = { "herz", "karo", "pik", "kreuz" };
const char* rankNames[] = { "ace", "two", "three", "four", "five",
"six", "seven", "eight", "nine", "ten",
"jack", "queen", "king" };
const int suitCount = sizeof(suitNames) / sizeof(*suitNames);
const int rankCount = sizeof(rankNames) / sizeof(*rankNames);
Bitmap* cursor = NULL;
Bitmap* back = NULL;
Bitmap* frame = NULL;
Bitmap* cards[suitCount][rankCount];
//--- class SpiderPlugin::Game -------------------------------------------------
const Game::Colors Game::full_colors =
{
clrGray50, // background 50% gray
clrRed, // osd_frame red
clrWhite, // card_frame white
clrGray50, // card_frame_bg 50% gray
clrBlue, // inactive_cursor blue
clrYellow // active_cursor yellow
};
const Game::Colors Game::reduced_colors =
{
0xFF0000FF, // background blue
0xFFFF0000, // osd_frame red
0xFFFFFFFF, // card_frame white
0xFF000000, // card_frame_bg black
0xFF0000FF, // inactive_cursor blue
0xFF0000FF // active_cursor blue
};
/** Constructor */
Game::Game(const SetupData& setup, const char* confdir,
Deck*& deck, Tableau*& tableau) :
cOsdObject(true), setup(setup), confdir(confdir), deck(deck), tableau(tableau)
{
osd = NULL;
currentPile = 0;
status = cursorOnPile;
info = new Bitmap(360, 60);
infoText = NULL;
colors = full_colors;
}
/** Destructor */
Game::~Game()
{
delete info;
delete osd;
for (int s = 0; s < suitCount; ++s)
for (int r = 0; r < rankCount; ++r)
delete cards[s][r];
delete frame;
delete back;
delete cursor;
frame = back = cursor = NULL;
}
/** Display the game on the OSD */
void Game::Show()
{
osd = cOsdProvider::NewOsd(0, 0);
if (osd)
{
tArea area = { setup.osd_left,
setup.osd_top,
setup.osd_left + (setup.osd_width & ~0x03) - 1,
setup.osd_top + setup.osd_height - 1,
4 };
eOsdError err = osd->CanHandleAreas(&area, 1);
if (err == oeOutOfMemory &&
setup.osd_error_compensation == SetupData::ReduceColors)
{
// Reduce colors
area.bpp = 2;
err = osd->CanHandleAreas(&area, 1);
}
while (err == oeOutOfMemory)
{
if (setup.osd_error_compensation != SetupData::ShrinkWidth)
{
// Shrink height
area.y1 += 2;
area.y2 -= 2;
}
if (setup.osd_error_compensation == SetupData::ShrinkWidth ||
setup.osd_error_compensation == SetupData::ShrinkWidthHeight)
{
// Shrink width
area.x1 += 2;
area.x2 -= 2;
}
err = osd->CanHandleAreas(&area, 1);
}
// Before setting the osd area - check if we are near the osd memory limit -
// then we use an workaround for driver error (no output near memory limit)
// I hope an extra shrink of 12 lines height is enough to avoid the error
area.y2 += 12;
err = osd->CanHandleAreas(&area, 1);
area.y2 -= 12;
if (err == oeOutOfMemory)
{
area.y1 += 6;
area.y2 -= 6;
}
osd->SetAreas(&area, 1);
if (area.bpp <= 2)
colors = reduced_colors;
// Load bitmaps
cursor = new Bitmap(cursorWidth, cursorHeight, confdir, cursorName);
back = new Bitmap(cardWidth, cardHeight, confdir, coverName);
frame = new Bitmap(cardWidth, cardHeight, colors.card_frame,
colors.card_frame_bg);
for (int s = 0; s < suitCount; ++s)
for (int r = 0; r < rankCount; ++r)
cards[s][r] = new Bitmap(cardWidth, cardHeight, confdir,
suitNames[s], rankNames[r]);
if (deck == NULL || tableau == NULL)
start();
paint();
}
}
/** Process user events */
eOSState Game::ProcessKey(eKeys key)
{
eOSState state = cOsdObject::ProcessKey(key);
if (state == osUnknown)
{
if (key == kBack)
return osEnd;
if (key == kBlue || (status == gameOver && key == kOk))
{
start();
status = cursorOnPile;
currentPile = 0;
}
else if (status == cursorOnPile)
{
switch (key)
{
case kLeft:
case kLeft|k_Repeat:
if (currentPile > 0)
--currentPile;
break;
case kRight:
case kRight|k_Repeat:
if (currentPile < tableau->piles.size() - 1)
++currentPile;
break;
case kUp:
status = cursorOnPack;
break;
case kOk:
if (!tableau->piles[currentPile]->empty())
{
tableau->select(currentPile);
status = selectedPile;
}
break;
case kGreen:
tableau->backward();
break;
case kYellow:
tableau->forward();
break;
default:
return osContinue;
}
}
else if (status == selectedPile)
{
int selected = tableau->selected->selected();
int destination;
switch (key)
{
case kLeft:
destination = tableau->autoMoveLeft(currentPile);
if (destination >= 0)
{
currentPile = destination;
tableau->select(currentPile, selected);
}
break;
case kRight:
destination = tableau->autoMoveRight(currentPile);
if (destination >= 0)
{
currentPile = destination;
tableau->select(currentPile, selected);
}
break;
case kUp:
case kUp|k_Repeat:
tableau->selected->select(selected + 1);
if (key == kUp)
{
if (selected == deck->cardsInSuit)
{
tableau->remove();
status = cursorOnPile;
}
else if (selected == tableau->selected->selected())
infoText = tr("Only complete suits are allowed to remove");
}
break;
case kDown:
case kDown|k_Repeat:
if (selected > 1)
tableau->selected->select(selected - 1);
break;
case kOk:
tableau->unselect();
status = cursorOnPile;
break;
case kGreen:
tableau->unselect();
tableau->backward();
status = cursorOnPile;
break;
case kYellow:
tableau->unselect();
tableau->forward();
status = cursorOnPile;
break;
default:
return osContinue;
}
}
else if (status == cursorOnPack)
{
switch (key)
{
case kLeft:
case kRight:
case kDown:
status = cursorOnPile;
break;
case kOk:
if (tableau->pack->empty())
infoText = tr("No cards left");
else if (!tableau->noPileEmpty())
infoText = tr("Deal not allowed with empty piles");
else
tableau->deal();
break;
case kGreen:
tableau->backward();
break;
case kYellow:
tableau->forward();
break;
default:
return osContinue;
}
}
if (tableau->gameOver())
{
status = gameOver;
infoText = tr("Congratulations!\nPress OK to start a new game");
}
paint();
state = osContinue;
}
return state;
}
/** Start a new game */
void Game::start()
{
delete deck;
delete tableau;
deck = NULL;
tableau = NULL;
int deckCount, dealCount, pileCount;
if (setup.variation == SetupData::Mini)
{
deckCount = 1;
dealCount = 4;
pileCount = 7;
}
else if (setup.variation == SetupData::Normal)
{
deckCount = 2;
dealCount = 5;
pileCount = 10;
}
else // SetupData::Custom: custom variation
{
deckCount = setup.deck_count;
dealCount = setup.deal_count;
pileCount = setup.pile_count;
}
if (pileCount > rankCount * suitCount * deckCount)
pileCount = rankCount * suitCount * deckCount;
int finalCount = suitCount * deckCount;
deck = new Deck(rankCount, suitCount, deckCount);
tableau = new Tableau(*deck, pileCount, finalCount, dealCount);
}
/** Paint all pieces of the game */
void Game::paint()
{
const cBitmap* bm = osd->GetBitmap(0);
int x1 = bm->X0();
int x2 = bm->X0() + bm->Width() - 1;
int y1 = bm->Y0();
int y2 = bm->Y0() + bm->Height() - 1;
// Save and restore palette to reduce flickering
cPalette savePalette(*bm);
osd->DrawRectangle(x1, y1, x2, y2, colors.background);
osd->SetPalette(savePalette, 0);
// Paint red frame
osd->DrawRectangle(x1, y1, x2, y1 + 1, colors.osd_frame);
osd->DrawRectangle(x1, y1, x1 + 1, y2, colors.osd_frame);
osd->DrawRectangle(x1, y2 - 1, x2, y2, colors.osd_frame);
osd->DrawRectangle(x2 - 1, y1, x2, y2, colors.osd_frame);
if (!setup.hide_toprow || status == cursorOnPack)
{
paintPack();
unsigned int f;
for (f = 0; f < tableau->finals.size(); ++f)
if (tableau->finals[f]->empty())
paintFinal(f);
for (f = 0; f < tableau->finals.size(); ++f)
if (!tableau->finals[f]->empty())
paintFinal(f);
}
unsigned int p;
for (p = 0; p < tableau->piles.size(); ++p)
if (tableau->piles[p]->empty())
paintPile(p);
for (p = 0; p < tableau->piles.size(); ++p)
if (!tableau->piles[p]->empty())
paintPile(p);
if (infoText)
{
info->text(infoText);
osd->DrawBitmap(x1 + (x2 - x1 + 1 - info->Width()) / 2, y1 + 10, *info);
infoText = NULL;
}
osd->Flush();
}
/** Paint the pack */
void Game::paintPack()
{
const cBitmap* bm = osd->GetBitmap(0);
int packX = bm->X0() + 1;
int packY = bm->Y0() + 1;
if (tableau->pack->empty())
paintFrame(packX, packY);
else
paintBack(packX, packY);
if (status == cursorOnPack)
paintCursor(packX, packY);
}
/** Paint a final heap */
void Game::paintFinal(unsigned int f)
{
const cBitmap* bm = osd->GetBitmap(0);
int offset = tableau->piles.size() - tableau->finals.size();
int finalX = bm->X0() + 1 + ((f + offset) * (bm->Width() - 1 - cardWidth)) /
(tableau->piles.size() - 1);
int finalY = bm->Y0() + 1;
if (tableau->finals[f]->empty())
paintFrame(finalX, finalY);
else
paintCard(finalX, finalY, tableau->finals[f]->top());
}
/** Paint a pile */
void Game::paintPile(unsigned int p)
{
const cBitmap* bm = osd->GetBitmap(0);
int pileX = bm->X0() + 1 + (p * (bm->Width() - 1 - cardWidth)) /
(tableau->piles.size() - 1);
int pileY = bm->Y0() + 1;
if (!setup.hide_toprow || status == cursorOnPack)
pileY += cardHeight + 1;
paintFrame(pileX, pileY);
int count = tableau->piles[p]->count();
int closed = count - tableau->piles[p]->open();
int unselected = count - tableau->piles[p]->selected();
int dist = cardHeight / 6;
if (pileY + (count + 1) * dist > bm->Y0() + bm->Height())
dist = (bm->Y0() + bm->Height() - pileY) / (count + 1);
for (int c = 0; c < count; ++c, pileY += dist)
{
if (c == unselected)
pileY += dist;
if (c < closed)
paintBack(pileX, pileY);
else
paintCard(pileX, pileY, tableau->piles[p]->card(c));
}
if (count > 0)
pileY -= dist;
if ((status == cursorOnPile || status == selectedPile) && p == currentPile)
paintCursor(pileX, pileY);
}
/** Paint the cursor onto a card */
void Game::paintCursor(int x, int y)
{
int x0 = x + (cardWidth - cursorWidth) / 2;
int y0 = y + (cardHeight - cursorHeight) / 2;
tColor color = colors.inactive_cursor;
if (status == selectedPile)
color = colors.active_cursor;
for (x = 0; x < cursorWidth; ++x)
for (y = 0; y < cursorHeight; ++y)
if (cursor->Color(*cursor->Data(x, y)) != clrTransparent)
osd->DrawRectangle(x0 + x, y0 + y, x0 + x, y0 + y, color);
}
/** Paint an empty card frame */
void Game::paintFrame(int x, int y)
{
osd->DrawBitmap(x, y, *frame);
}
/** Paint a card back */
void Game::paintBack(int x, int y)
{
osd->DrawBitmap(x, y, *back);
}
/** Paint a card */
void Game::paintCard(int x, int y, const Card& card)
{
osd->DrawBitmap(x, y, *cards[card.suit][card.rank]);
}
|