fix redraw at top-left of map
[Tsunagari.git] / src / window.cpp
blob5231ffca372f3582d8a28d97250beeae987f77ad
1 /******************************
2 ** Tsunagari Tile Engine **
3 ** window.cpp **
4 ** Copyright 2011 OmegaSDG **
5 ******************************/
7 #include <Gosu/Timing.hpp>
8 #include <Gosu/Utility.hpp>
10 #include "config.h"
11 #include "resourcer.h"
12 #include "world.h"
13 #include "window.h"
16 static GameWindow* globalWindow = NULL;
18 GameWindow& GameWindow::getWindow()
20 return *globalWindow;
23 GameWindow::GameWindow(unsigned x, unsigned y, bool fullscreen)
24 : Gosu::Window(x, y, fullscreen),
25 lastTime((int)Gosu::milliseconds()),
26 now(lastTime),
27 currentSecond(now/1000)
29 globalWindow = this;
32 GameWindow::~GameWindow()
36 bool GameWindow::init(ClientValues* conf)
38 rc.reset(new Resourcer(this, conf));
39 world.reset(new World(rc.get(), this));
40 return rc->init() && world->init();
43 void GameWindow::buttonDown(const Gosu::Button btn)
45 now = (int)Gosu::milliseconds();
46 if (btn == Gosu::kbEscape)
47 close();
48 else {
49 if (keystates.find(btn) == keystates.end()) {
50 keystate& state = keystates[btn];
51 state.since = now;
52 state.initiallyResolved = false;
53 state.consecutive = false;
55 // We process the initial buttonDown here so that it
56 // gets handled even if we receive a buttonUp before an
57 // update.
58 world->buttonDown(btn);
63 void GameWindow::buttonUp(const Gosu::Button btn)
65 keystates.erase(btn);
66 world->buttonUp(btn);
69 void GameWindow::draw()
71 world->draw();
74 bool GameWindow::needsRedraw() const
76 return world->needsRedraw();
79 void GameWindow::update()
81 calculateDt();
82 if (GAME_MODE == JUMP_MOVE)
83 handleKeyboardInput();
84 world->update(dt);
86 // Run once per second.
87 if (now/1000 > currentSecond) {
88 currentSecond = now/1000;
89 rc->garbageCollect();
93 int GameWindow::time()
95 return now;
98 void GameWindow::calculateDt()
100 now = (int)Gosu::milliseconds();
101 dt = now - lastTime;
102 lastTime = now;
105 void GameWindow::handleKeyboardInput()
107 std::map<Gosu::Button, keystate>::iterator it;
109 // Persistent input handling code
110 for (it = keystates.begin(); it != keystates.end(); it++) {
111 Gosu::Button btn = it->first;
112 keystate& state = it->second;
114 // If there is PERSIST_DELAY_CONSECUTIVE milliseconds of latency
115 // between when a button is depressed and when we first look at
116 // it here, we'll incorrectly try to fire off a second round of
117 // input.
118 // This can happen if an intermediary function blocks the thread
119 // for a while.
120 if (!state.initiallyResolved) {
121 state.initiallyResolved = true;
122 continue;
125 int delay = state.consecutive ?
126 ROGUELIKE_PERSIST_DELAY_CONSECUTIVE : ROGUELIKE_PERSIST_DELAY_INIT;
127 if (now >= state.since + delay) {
128 state.since = now;
129 world->buttonDown(btn);
130 state.consecutive = true;