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 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
|
#include "main.h"
#include "utils.h"
#include "widgets.h"
#include "puzzle.h"
#include "verthints.h"
#include "horhints.h"
#include "widgets.h"
#include "font.h"
#include "topscores.h"
#include "opensave.h"
#include "options.h"
#include "game.h"
#include "messages.h"
#include "sound.h"
#include "descr.h"
class GameBackground: public Widget
{
public:
virtual void draw();
};
void GameBackground::draw()
{
// draw background
drawWallpaper(L"rain.bmp");
// draw title
SDL_Surface *tile = loadImage(L"title.bmp");
screen.draw(8, 10, tile);
SDL_FreeSurface(tile);
Font titleFont(L"DejaVuSans.ttf", 28);
titleFont.draw(screen.getSurface(), 20, 20, 255,255,0, true,
msg(L"einsteinPuzzle"));
screen.addRegionToUpdate(0, 0, screen.getWidth(), screen.getHeight());
}
class ToggleHintCommand: public Command
{
private:
VertHints *verHints;
HorHints *horHints;
public:
ToggleHintCommand(VertHints *v, HorHints *h) {
verHints = v;
horHints = h;
};
virtual void doAction() {
verHints->toggleExcluded();
horHints->toggleExcluded();
};
};
class Watch: public TimerHandler, public Widget
{
private:
Uint32 lastRun;
Uint32 elapsed;
bool stoped;
int lastUpdate;
Font *font;
public:
Watch();
Watch(std::istream &stream);
virtual ~Watch();
public:
virtual void onTimer();
void stop();
void start();
virtual void draw();
int getElapsed() { return elapsed; };
void save(std::ostream &stream);
void reset();
};
Watch::Watch()
{
lastRun = elapsed = lastUpdate = 0;
stop();
font = new Font(L"DejaVuSans.ttf", 16);
}
Watch::Watch(std::istream &stream)
{
elapsed = readInt(stream);
lastUpdate = 0;
stop();
font = new Font(L"DejaVuSans.ttf", 16);
}
Watch::~Watch()
{
delete font;
}
void Watch::onTimer()
{
if (stoped)
return;
Uint32 now = SDL_GetTicks();
elapsed += now - lastRun;
lastRun = now;
int seconds = elapsed / 1000;
if (seconds != lastUpdate)
draw();
}
void Watch::stop()
{
stoped = true;
}
void Watch::start()
{
stoped = false;
lastRun = SDL_GetTicks();
}
void Watch::draw()
{
int time = elapsed / 1000;
std::wstring s = secToStr(time);
int x = 700;
int y = 24;
int w, h;
font->getSize(s, w, h);
SDL_Rect rect = { x-2, y-2, w+4, h+4 };
SDL_FillRect(screen.getSurface(), &rect,
SDL_MapRGB(screen.getSurface()->format, 0, 0, 255));
font->draw(x, y, 255,255,255, true, s);
screen.addRegionToUpdate(x-2, y-2, w+4, h+4);
lastUpdate = time;
}
void Watch::save(std::ostream &stream)
{
writeInt(stream, elapsed);
}
void Watch::reset()
{
elapsed = lastUpdate = 0;
lastRun = SDL_GetTicks();
}
class PauseGameCommand: public Command
{
private:
Area *gameArea;
Watch *watch;
Widget *background;
public:
PauseGameCommand(Area *a, Watch *w, Widget *bg) {
gameArea = a;
watch = w;
background = bg;
};
virtual void doAction() {
watch->stop();
Area area;
area.add(background, false);
Font font(L"DejaVuSans.ttf", 16);
area.add(new Window(280, 275, 240, 50, L"greenpattern.bmp", 6));
area.add(new Label(&font, 280, 275, 240, 50, Label::ALIGN_CENTER,
Label::ALIGN_MIDDLE, 255,255,0, msg(L"paused")));
area.add(new AnyKeyAccel());
area.run();
sound->play(L"click.wav");
gameArea->updateMouse();
gameArea->draw();
watch->start();
};
};
class WinCommand: public Command
{
private:
Area *gameArea;
Watch *watch;
Game *game;
public:
WinCommand(Area *a, Watch *w, Game *g) {
gameArea = a;
watch = w;
game = g;
};
virtual void doAction() {
sound->play(L"applause.wav");
watch->stop();
Font font(L"DejaVuSans.ttf", 20);
showMessageWindow(gameArea, L"marble1.bmp",
500, 70, &font, 255,0,0, msg(L"won"));
gameArea->draw();
TopScores scores;
int score = watch->getElapsed() / 1000;
int pos = -1;
if (! game->isHinted()) {
if ((! scores.isFull()) || (score < scores.getMaxScore())) {
std::wstring name = enterNameDialog(gameArea);
pos = scores.add(name, score);
}
}
showScoresWindow(gameArea, &scores, pos);
gameArea->finishEventLoop();
};
};
class OkDlgCommand: public Command
{
private:
bool &res;
Area *area;
public:
OkDlgCommand(Area *a, bool &r): res(r) {
area = a;
};
virtual void doAction() {
res = true;
area->finishEventLoop();
};
};
class FailCommand: public Command
{
private:
Area *gameArea;
Game *game;
public:
FailCommand(Area *a, Game *g) { gameArea = a; game = g; };
virtual void doAction() {
sound->play(L"glasbk2.wav");
bool restart = false;
bool newGame = false;
Font font(L"DejaVuSans.ttf", 24);
Font btnFont(L"DejaVuSans.ttf", 14);
Area area;
area.add(gameArea);
area.add(new Window(220, 240, 360, 140, L"redpattern.bmp", 6));
area.add(new Label(&font, 250, 230, 300, 100, Label::ALIGN_CENTER,
Label::ALIGN_MIDDLE, 255,255,0, msg(L"loose")));
OkDlgCommand newGameCmd(&area, newGame);
area.add(new Button(250, 340, 90, 25, &btnFont, 255,255,0,
L"redpattern.bmp", msg(L"startNew"), &newGameCmd));
OkDlgCommand restartCmd(&area, restart);
area.add(new Button(350, 340, 90, 25, &btnFont, 255,255,0,
L"redpattern.bmp", msg(L"tryAgain"), &restartCmd));
ExitCommand exitCmd(area);
area.add(new Button(450, 340, 90, 25, &btnFont, 255,255,0,
L"redpattern.bmp", msg(L"exit"), &exitCmd));
area.run();
if (restart || newGame) {
if (newGame)
game->newGame();
else
game->restart();
gameArea->draw();
gameArea->updateMouse();
} else
gameArea->finishEventLoop();
};
};
class CheatAccel: public Widget
{
private:
Command *command;
std::wstring typed;
std::wstring cheat;
public:
CheatAccel(const std::wstring s, Command *cmd): cheat(s) {
command = cmd;
};
public:
virtual bool onKeyDown(SDLKey key, unsigned char ch) {
if ((key >= SDLK_a) && (key <= SDLK_z)) {
wchar_t s = L'a' + key - SDLK_a;
typed += s;
if (typed.length() == cheat.length()) {
if (command && (typed == cheat))
command->doAction();
} else {
int pos = typed.length() - 1;
if (typed[pos] == cheat[pos])
return false;
}
}
if (typed.length() > 0)
typed = L"";
return false;
}
};
class CheatCommand: public Command
{
private:
Area *gameArea;
public:
CheatCommand(Area *a) { gameArea = a; };
virtual void doAction() {
Font font(L"DejaVuSans.ttf", 30);
showMessageWindow(gameArea, L"darkpattern.bmp",
500, 100, &font, 255,255,255,
msg(L"iddqd"));
gameArea->draw();
};
};
class SaveGameCommand: public Command
{
private:
Area *gameArea;
Watch *watch;
Widget *background;
Game *game;
public:
SaveGameCommand(Area *a, Watch *w, Widget *bg, Game *g) {
gameArea = a;
watch = w;
background = bg;
game = g;
};
virtual void doAction() {
watch->stop();
Area area;
area.add(background, false);
saveGame(&area, game);
gameArea->updateMouse();
gameArea->draw();
watch->start();
};
};
class GameOptionsCommand: public Command
{
private:
Area *gameArea;
public:
GameOptionsCommand(Area *a) {
gameArea = a;
};
virtual void doAction() {
showOptionsWindow(gameArea);
gameArea->updateMouse();
gameArea->draw();
};
};
class HelpCommand: public Command
{
private:
Area *gameArea;
Watch *watch;
Widget *background;
public:
HelpCommand(Area *a, Watch *w, Widget *b) {
gameArea = a;
watch = w;
background = b;
};
virtual void doAction() {
watch->stop();
Area area;
area.add(background, false);
area.draw();
showDescription(&area);
gameArea->updateMouse();
gameArea->draw();
watch->start();
};
};
Game::Game()
{
genPuzzle();
possibilities = new Possibilities();
openInitial(*possibilities, rules);
puzzle = new Puzzle(iconSet, solvedPuzzle, possibilities);
verHints = new VertHints(iconSet, rules);
horHints = new HorHints(iconSet, rules);
watch = new Watch();
}
Game::Game(std::istream &stream)
{
pleaseWait();
loadPuzzle(solvedPuzzle, stream);
loadRules(rules, stream);
memcpy(savedSolvedPuzzle, solvedPuzzle, sizeof(solvedPuzzle));
savedRules = rules;
possibilities = new Possibilities(stream);
puzzle = new Puzzle(iconSet, solvedPuzzle, possibilities);
verHints = new VertHints(iconSet, rules, stream);
horHints = new HorHints(iconSet, rules, stream);
watch = new Watch(stream);
hinted = true;
}
Game::~Game()
{
delete watch;
delete possibilities;
delete verHints;
delete horHints;
delete puzzle;
deleteRules();
}
void Game::save(std::ostream &stream)
{
savePuzzle(solvedPuzzle, stream);
saveRules(rules, stream);
possibilities->save(stream);
verHints->save(stream);
horHints->save(stream);
watch->save(stream);
}
void Game::deleteRules()
{
for (Rules::iterator i = rules.begin(); i != rules.end(); i++)
delete *i;
rules.clear();
}
void Game::pleaseWait()
{
drawWallpaper(L"rain.bmp");
Window window(230, 260, 340, 80, L"greenpattern.bmp", 6);
window.draw();
Font font(L"DejaVuSans.ttf", 16);
Label label(&font, 280, 275, 240, 50, Label::ALIGN_CENTER,
Label::ALIGN_MIDDLE, 255,255,0, msg(L"loading"));
label.draw();
screen.addRegionToUpdate(0, 0, screen.getWidth(), screen.getHeight());
screen.flush();
}
void Game::genPuzzle()
{
pleaseWait();
int horRules, verRules;
do {
if (rules.size() > 0)
deleteRules();
::genPuzzle(solvedPuzzle, rules);
getHintsQty(rules, verRules, horRules);
} while ((horRules > 24) || (verRules > 15));
memcpy(savedSolvedPuzzle, solvedPuzzle, sizeof(solvedPuzzle));
savedRules = rules;
hinted = false;
}
void Game::resetVisuals()
{
possibilities->reset();
openInitial(*possibilities, rules);
puzzle->reset();
verHints->reset(rules);
horHints->reset(rules);
watch->reset();
}
void Game::newGame()
{
genPuzzle();
resetVisuals();
}
void Game::restart()
{
memcpy(solvedPuzzle, savedSolvedPuzzle, sizeof(solvedPuzzle));
rules = savedRules;
resetVisuals();
hinted = true;
}
#define BUTTON(x, y, text, cmd) \
area.add(new Button(x, y, 94, 30, &btnFont, 255,255,0, \
L"btn.bmp", msg(text), false, cmd));
void Game::run()
{
Area area;
Font btnFont(L"DejaVuSans.ttf", 14);
area.setTimer(300, watch);
GameBackground *background = new GameBackground();
area.add(background);
CheatCommand cheatCmd(&area);
area.add(new CheatAccel(L"iddqd", &cheatCmd));
WinCommand winCmd(&area, watch, this);
FailCommand failCmd(&area, this);
puzzle->setCommands(&winCmd, &failCmd);
area.add(puzzle, false);
area.add(verHints, false);
area.add(horHints, false);
PauseGameCommand pauseGameCmd(&area, watch, background);
BUTTON(12, 400, L"pause", &pauseGameCmd)
ToggleHintCommand toggleHintsCmd(verHints, horHints);
BUTTON(119, 400, L"switch", &toggleHintsCmd)
SaveGameCommand saveCmd(&area, watch, background, this);
BUTTON(12, 440, L"save", &saveCmd)
GameOptionsCommand optionsCmd(&area);
BUTTON(119, 440, L"options", &optionsCmd)
ExitCommand exitGameCmd(area);
BUTTON(226, 400, L"exit", &exitGameCmd)
area.add(new KeyAccel(SDLK_ESCAPE, &exitGameCmd));
HelpCommand helpCmd(&area, watch, background);
BUTTON(226, 440, L"help", &helpCmd)
area.add(watch, false);
watch->start();
area.run();
}
|