window.cpp: ~GameWindow(): delete Resourcer
[Tsunagari.git] / src / window.cpp
blobdde63d3513a6cbd9f3e0c8846d6b03d9700320a7
1 /******************************
2 ** Tsunagari Tile Engine **
3 ** window.cpp **
4 ** Copyright 2011 OmegaSDG **
5 ******************************/
7 #include "resourcer.h"
8 #include "world.h"
9 #include "window.h"
11 // === Roguelike Input Mode Settings ===
12 // Milliseconds of button down before starting persistent input.
13 #define ROGUELIKE_PERSIST_DELAY_INIT 500
15 // Milliseconds between persistent input sends.
16 #define ROGUELIKE_PERSIST_DELAY_CONSECUTIVE 100
17 // ===
19 static GameWindow* globalWindow = NULL;
21 GameWindow* GameWindow::getWindow()
23 return globalWindow;
26 GameWindow::GameWindow(uint x, uint y, bool fullscreen)
27 : Gosu::Window(x, y, fullscreen)
29 globalWindow = this;
32 GameWindow::~GameWindow()
34 delete world;
35 delete rc;
38 bool GameWindow::init(const std::string descriptor)
40 rc = new Resourcer(this, descriptor);
41 world = new World(rc, this);
42 return rc->init() && world->init();
45 void GameWindow::buttonDown(const Gosu::Button btn)
47 if (btn == Gosu::kbEscape)
48 close();
49 else {
50 if (keystates.find(btn) == keystates.end())
51 keystates[btn].second = Gosu::milliseconds();
52 world->buttonDown(btn);
56 void GameWindow::buttonUp(const Gosu::Button btn)
58 if (keystates.find(btn) != keystates.end())
59 keystates.erase(btn);
62 void GameWindow::draw()
64 world->draw();
67 bool GameWindow::needsRedraw() const
69 return world->needsRedraw();
72 void GameWindow::update()
74 std::map<Gosu::Button, std::pair<bool, unsigned long> >::iterator it;
76 // Persistent input handling code
77 for (it = keystates.begin(); it != keystates.end(); it++) {
78 if ((*it).second.first == false) {
79 if (Gosu::milliseconds() >=
80 (*it).second.second+ROGUELIKE_PERSIST_DELAY_INIT) {
81 (*it).second.second = Gosu::milliseconds();
82 (*it).second.first = true;
83 world->buttonDown((*it).first);
85 else
86 continue;
88 else {
89 if (Gosu::milliseconds() >=
90 (*it).second.second+ROGUELIKE_PERSIST_DELAY_CONSECUTIVE) {
91 (*it).second.second = Gosu::milliseconds();
92 world->buttonDown((*it).first);
94 else
95 continue;