1 /******************************
2 ** Tsunagari Tile Engine **
4 ** Copyright 2011 OmegaSDG **
5 ******************************/
7 #include <Gosu/Timing.hpp>
8 #include <Gosu/Utility.hpp>
11 #include "resourcer.h"
16 static GameWindow
* globalWindow
= NULL
;
18 GameWindow
& GameWindow::getWindow()
23 GameWindow::GameWindow(unsigned x
, unsigned y
, bool fullscreen
)
24 : Gosu::Window(x
, y
, fullscreen
),
25 lastTime((int)Gosu::milliseconds()),
27 currentSecond(now
/1000)
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
)
49 if (keystates
.find(btn
) == keystates
.end()) {
50 keystate
& state
= keystates
[btn
];
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
58 world
->buttonDown(btn
);
63 void GameWindow::buttonUp(const Gosu::Button btn
)
69 void GameWindow::draw()
74 bool GameWindow::needsRedraw() const
76 return world
->needsRedraw();
79 void GameWindow::update()
82 if (GAME_MODE
== JUMP_MOVE
)
83 handleKeyboardInput();
86 // Run once per second.
87 if (now
/1000 > currentSecond
) {
88 currentSecond
= now
/1000;
93 int GameWindow::time()
98 void GameWindow::calculateDt()
100 now
= (int)Gosu::milliseconds();
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
118 // This can happen if an intermediary function blocks the thread
120 if (!state
.initiallyResolved
) {
121 state
.initiallyResolved
= true;
125 int delay
= state
.consecutive
?
126 ROGUELIKE_PERSIST_DELAY_CONSECUTIVE
: ROGUELIKE_PERSIST_DELAY_INIT
;
127 if (now
>= state
.since
+ delay
) {
129 world
->buttonDown(btn
);
130 state
.consecutive
= true;