Rewrite main loop
This commit is contained in:
parent
8f964bb27d
commit
24ae848613
4 changed files with 62 additions and 60 deletions
|
@ -55,6 +55,10 @@ public:
|
|||
|
||||
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) {
|
||||
return std::make_shared<View::MapView>(window, map, tiles, entities);
|
||||
}
|
||||
|
|
|
@ -25,8 +25,6 @@
|
|||
|
||||
|
||||
#include "RPGEdit.hpp"
|
||||
#include "EventBus.hpp"
|
||||
#include "InputHandler.hpp"
|
||||
#include "MapContext.hpp"
|
||||
#include "../view/MapView.hpp"
|
||||
|
||||
|
@ -40,33 +38,8 @@ namespace RPGEdit {
|
|||
|
||||
namespace Control {
|
||||
|
||||
RPGEdit::RPGEdit() {
|
||||
}
|
||||
|
||||
RPGEdit::~RPGEdit() {
|
||||
}
|
||||
|
||||
void RPGEdit::run() {
|
||||
EventBus eventBus;
|
||||
|
||||
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;
|
||||
bool RPGEdit::systemIter(unsigned ticks) {
|
||||
int timeout = 0;
|
||||
|
||||
SDL_Event event;
|
||||
if (SDL_WaitEventTimeout(&event, timeout)) {
|
||||
|
@ -80,29 +53,39 @@ void RPGEdit::run() {
|
|||
break;
|
||||
|
||||
case SDL_QUIT:
|
||||
running = false;
|
||||
break;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!running)
|
||||
break;
|
||||
ctx->advance(&inputHandler, ticks);
|
||||
|
||||
uint32_t newTicks = SDL_GetTicks();
|
||||
ctx->advance(&inputHandler, newTicks - ticks);
|
||||
|
||||
ticks = newTicks;
|
||||
|
||||
if (!SDL_TICKS_PASSED(SDL_GetTicks(), lastFrameTicks + MIN_FRAME_DELAY))
|
||||
continue;
|
||||
|
||||
Model::Position pos = map->getPlayerEntity().getPosition();
|
||||
Model::Position pos = ctx->getViewPosition();
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -26,7 +26,12 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <SDL.h>
|
||||
#include "EventBus.hpp"
|
||||
#include "InputHandler.hpp"
|
||||
#include "MapContext.hpp"
|
||||
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
|
||||
|
||||
namespace RPGEdit {
|
||||
|
@ -35,11 +40,21 @@ namespace Control {
|
|||
|
||||
class RPGEdit {
|
||||
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:
|
||||
RPGEdit();
|
||||
~RPGEdit();
|
||||
|
||||
void run();
|
||||
};
|
||||
|
||||
|
|
|
@ -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->entities.push_back(map->playerEntity);
|
||||
|
|
Reference in a new issue