Rewrite main loop

This commit is contained in:
Matthias Schiffer 2014-09-23 20:58:49 +02:00
parent 8f964bb27d
commit 24ae848613
4 changed files with 62 additions and 60 deletions

View file

@ -55,6 +55,10 @@ public:
void advance(InputHandler *inputHandler, unsigned ticks); void advance(InputHandler *inputHandler, unsigned ticks);
Model::Position getViewPosition() {
return map->getPlayerEntity().getPosition();
}
std::shared_ptr<View::MapView> initView(const std::shared_ptr<View::Window> &window) { std::shared_ptr<View::MapView> initView(const std::shared_ptr<View::Window> &window) {
return std::make_shared<View::MapView>(window, map, tiles, entities); return std::make_shared<View::MapView>(window, map, tiles, entities);
} }

View file

@ -25,8 +25,6 @@
#include "RPGEdit.hpp" #include "RPGEdit.hpp"
#include "EventBus.hpp"
#include "InputHandler.hpp"
#include "MapContext.hpp" #include "MapContext.hpp"
#include "../view/MapView.hpp" #include "../view/MapView.hpp"
@ -40,69 +38,54 @@ namespace RPGEdit {
namespace Control { namespace Control {
RPGEdit::RPGEdit() { bool RPGEdit::systemIter(unsigned ticks) {
} int timeout = 0;
RPGEdit::~RPGEdit() { SDL_Event event;
} if (SDL_WaitEventTimeout(&event, timeout)) {
switch (event.type) {
void RPGEdit::run() { case SDL_KEYDOWN:
EventBus eventBus; inputHandler.keyPressed(event.key.keysym.scancode);
InputHandler inputHandler;
ImageLoader tileLoader;
std::shared_ptr<Model::Map> map = Model::Map::load("test");
std::shared_ptr<MapContext> ctx(new MapContext(&tileLoader, map));
std::shared_ptr<View::Window> window(new View::Window);
std::shared_ptr<View::MapView> mapView = ctx->initView(window);
bool running = true;
uint32_t ticks = SDL_GetTicks();
uint32_t lastFrameTicks = ticks;
while (true) {
int timeout = lastFrameTicks + MIN_FRAME_DELAY - SDL_GetTicks();
if (timeout < 0)
timeout = 0;
SDL_Event event;
if (SDL_WaitEventTimeout(&event, timeout)) {
switch (event.type) {
case SDL_KEYDOWN:
inputHandler.keyPressed(event.key.keysym.scancode);
break;
case SDL_KEYUP:
inputHandler.keyReleased(event.key.keysym.scancode);
break;
case SDL_QUIT:
running = false;
break;
}
}
if (!running)
break; break;
uint32_t newTicks = SDL_GetTicks(); case SDL_KEYUP:
ctx->advance(&inputHandler, newTicks - ticks); inputHandler.keyReleased(event.key.keysym.scancode);
break;
ticks = newTicks; case SDL_QUIT:
return false;
}
}
if (!SDL_TICKS_PASSED(SDL_GetTicks(), lastFrameTicks + MIN_FRAME_DELAY)) ctx->advance(&inputHandler, ticks);
continue;
Model::Position pos = map->getPlayerEntity().getPosition(); Model::Position pos = ctx->getViewPosition();
mapView->render(pos.x, pos.y); mapView->render(pos.x, pos.y);
lastFrameTicks = ticks; return true;
}
void RPGEdit::systemLoop() {
uint32_t ticks1 = SDL_GetTicks();
uint32_t ticks2 = ticks1;
while (systemIter(ticks2 - ticks1)) {
ticks1 = ticks2;
ticks2 = SDL_GetTicks();
} }
} }
void RPGEdit::run() {
std::shared_ptr<Model::Map> map = Model::Map::load("test");
ctx = std::make_shared<MapContext>(&tileLoader, map);
window = std::make_shared<View::Window>();
mapView = ctx->initView(window);
systemLoop();
}
} }
} }

View file

@ -26,7 +26,12 @@
#pragma once #pragma once
#include <SDL.h> #include "EventBus.hpp"
#include "InputHandler.hpp"
#include "MapContext.hpp"
#include <memory>
#include <mutex>
namespace RPGEdit { namespace RPGEdit {
@ -35,11 +40,21 @@ namespace Control {
class RPGEdit { class RPGEdit {
private: private:
EventBus eventBus;
InputHandler inputHandler;
ImageLoader tileLoader;
std::shared_ptr<MapContext> ctx;
std::shared_ptr<View::Window> window;
std::shared_ptr<View::MapView> mapView;
std::mutex modelLock;
bool systemIter(unsigned ticks);
void systemLoop();
public: public:
RPGEdit();
~RPGEdit();
void run(); void run();
}; };

View file

@ -45,7 +45,7 @@ std::shared_ptr<Map> Map::load(__attribute__((unused)) const std::string &name)
} }
} }
map->playerEntity.reset(new Entity("square")); map->playerEntity = std::make_shared<Entity>("square");
map->playerEntity->moveTo(6, 6); map->playerEntity->moveTo(6, 6);
map->entities.push_back(map->playerEntity); map->entities.push_back(map->playerEntity);