While I probably won't be writing any games any time soon, I thought I'd try to implement a (crude) keyboard input system (out of theoretical interest).
Its idea is to separate scanning for keyboard input from processing it. (I hope this is a worthy goal and I have indeed achieved this?)
The KeyboardReader keeps a list of EventWatchers which call the process() method of a previously registered EventHandler and pass it some information via a KeyboardEvent object.
The usage looks like this (using Allegro):
Any criticism or suggestions?Code:int main() { init(); GameManager game; //derived from EventHandler Object obj(SCREEN_W/2, SCREEN_H/2, 40); //also from EventHander KeyboardReader kb; kb.attach(&game, KeyboardReader::KeyDown, GameManager::GAME_OVER, KEY_ESC); kb.attach(&game, KeyboardReader::KeyDown, GameManager::GAME_OVER, KEY_SPACE); kb.attach(&game, KeyboardReader::KeyUp, GameManager::GAME_OVER, KEY_ENTER); kb.attach(&game, KeyboardReader::KeyDown, GameManager::DRAW_BLUE, KEY_B); kb.attach(&game, KeyboardReader::KeyDown, GameManager::DRAW_GREEN, KEY_G); kb.attach(&game, KeyboardReader::KeyUp, GameManager::DRAW_RED, KEY_B); kb.attach(&game, KeyboardReader::KeyUp, GameManager::DRAW_RED, KEY_G); kb.attach(&obj, KeyboardReader::KeyHeld, Object::LEFT, KEY_LEFT); kb.attach(&obj, KeyboardReader::KeyHeld, Object::RIGHT, KEY_RIGHT); kb.attach(&obj, KeyboardReader::KeyHeld, Object::UP, KEY_UP); kb.attach(&obj, KeyboardReader::KeyHeld, Object::DOWN, KEY_DOWN); BITMAP* buffer = create_bitmap(SCREEN_W, SCREEN_H); while (!game.over()) { kb.run_scan(); game.draw_background(buffer); obj.draw(buffer); vsync(); blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H); } destroy_bitmap(buffer); deinit(); return 0; } END_OF_MAIN()
(I'll attach the source files. Since there's a limit of 5 files, the rest will follow in the next post.)



LinkBack URL
About LinkBacks



