TWP: Add stats and fix performance issues

This commit is contained in:
scemino 2024-03-21 20:44:23 +01:00
parent c40c59845f
commit 70b4b442e0
9 changed files with 72 additions and 28 deletions

View File

@ -795,7 +795,9 @@ static SQInteger createActor(HSQUIRRELVM v) {
sq_resetobject(&actor->_table);
sq_getstackobj(v, 2, &actor->_table);
sq_addref(vm, &actor->_table);
setId(actor->_table, g_twp->_resManager->newActorId());
const int id = g_twp->_resManager->newActorId();
setId(actor->_table, id);
g_twp->_resManager->_allObjects[id] = actor;
Common::String key;
sqgetf(actor->_table, "_key", key);

View File

@ -72,13 +72,14 @@ static void drawThreads() {
ImGui::Text("# threads: %u", threads.size());
ImGui::Separator();
if (ImGui::BeginTable("Threads", 6, ImGuiTableFlags_Borders | ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_Resizable | ImGuiTableFlags_RowBg)) {
if (ImGui::BeginTable("Threads", 7, ImGuiTableFlags_Borders | ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_Resizable | ImGuiTableFlags_RowBg)) {
ImGui::TableSetupColumn("Id");
ImGui::TableSetupColumn("Name");
ImGui::TableSetupColumn("Type");
ImGui::TableSetupColumn("Func");
ImGui::TableSetupColumn("Src");
ImGui::TableSetupColumn("Line");
ImGui::TableSetupColumn("Upd. Time");
ImGui::TableHeadersRow();
if (g_twp->_cutscene) {
@ -102,6 +103,8 @@ static void drawThreads() {
ImGui::TableNextColumn();
ImGui::TableNextColumn();
}
ImGui::TableNextColumn();
ImGui::Text("?");
}
for (const auto &thread : threads) {
@ -124,6 +127,8 @@ static void drawThreads() {
ImGui::TableNextColumn();
ImGui::TableNextColumn();
}
ImGui::TableNextColumn();
ImGui::Text("%u", thread->_lastUpdateTime);
}
ImGui::EndTable();
}
@ -557,6 +562,14 @@ static void drawGeneral() {
ImGui::Separator();
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
ImGui::Text("Draw time: %u ms", g_twp->_stats.drawTime);
ImGui::Text("Update time: %u ms", g_twp->_stats.totalUpdateTime);
ImGui::Text(" Update room time: %u ms", g_twp->_stats.updateRoomTime);
ImGui::Text(" Update tasks time: %u ms", g_twp->_stats.updateTasksTime);
ImGui::Text(" Update misc time: %u ms", g_twp->_stats.updateMiscTime);
ImGui::Text(" Update cutscene time: %u ms", g_twp->_stats.updateCutsceneTime);
ImGui::Text(" Update threads time: %u ms", g_twp->_stats.updateThreadsTime);
ImGui::Text(" Update callbacks time: %u ms", g_twp->_stats.updateCallbacksTime);
ImGui::End();
}

View File

@ -29,6 +29,8 @@
namespace Twp {
class Font;
class ResManager {
private:
enum {
@ -86,6 +88,7 @@ public:
Common::HashMap<Common::String, Texture> _textures;
Common::HashMap<Common::String, SpriteSheet> _spriteSheets;
Common::HashMap<Common::String, Common::SharedPtr<Font> > _fonts;
Common::SharedPtr<Object> _allObjects[100000];
private:
int _roomId = START_ROOMID;

View File

@ -161,7 +161,9 @@ Common::SharedPtr<Object> Room::createObject(const Common::String &sheet, const
sq_pop(v, 1);
// assign an id
setId(obj->_table, g_twp->_resManager->newObjId());
const int id = g_twp->_resManager->newObjId();
setId(obj->_table, id);
g_twp->_resManager->_allObjects[id] = obj;
Common::String name = frames.size() > 0 ? frames[0] : "noname";
sqsetf(obj->_table, "name", name);
obj->_key = name;
@ -200,7 +202,9 @@ Common::SharedPtr<Object> Room::createTextObject(const Common::String &fontName,
sq_pop(v, 1);
// assign an id
setId(obj->_table, g_twp->_resManager->newObjId());
const int id = g_twp->_resManager->newObjId();
setId(obj->_table, id);
g_twp->_resManager->_allObjects[id] = obj;
debugC(kDebugGame, "Create object with new table: %s #%d", obj->_name.c_str(), obj->getId());
obj->_name = Common::String::format("text#%d: %s", obj->getId(), text.c_str());
@ -311,7 +315,9 @@ void Room::load(Common::SharedPtr<Room> room, Common::SeekableReadStream &s) {
for (auto it = jobjects.begin(); it != jobjects.end(); it++) {
const Common::JSONObject &jObject = (*it)->asObject();
Common::SharedPtr<Object> obj(new Object());
Twp::setId(obj->_table, g_twp->_resManager->newObjId());
const int id = g_twp->_resManager->newObjId();
Twp::setId(obj->_table, id);
g_twp->_resManager->_allObjects[id] = obj;
obj->_key = jObject["name"]->asString();
obj->_node->setName(obj->_key.c_str());
obj->_node->setPos(Math::Vector2d(parseVec2(jObject["pos"]->asString())));

View File

@ -22,9 +22,9 @@
#include "twp/detection.h"
#include "twp/lighting.h"
#include "twp/object.h"
#include "twp/resmanager.h"
#include "twp/room.h"
#include "twp/squtil.h"
#include "twp/thread.h"
#include "twp/squirrel/squirrel.h"
#include "twp/squirrel/sqvm.h"
#include "twp/squirrel/sqstring.h"
@ -33,6 +33,7 @@
#include "twp/squirrel/sqstdaux.h"
#include "twp/squirrel/sqfuncproto.h"
#include "twp/squirrel/sqclosure.h"
#include "twp/thread.h"
namespace Twp {
@ -275,24 +276,7 @@ Common::SharedPtr<Room> sqroom(HSQUIRRELVM v, int i) {
}
Common::SharedPtr<Object> sqobj(int id) {
for (size_t i = 0; i < g_twp->_actors.size(); i++) {
Common::SharedPtr<Object> actor = g_twp->_actors[i];
if (getId(actor->_table) == id)
return actor;
}
for (size_t i = 0; i < g_twp->_rooms.size(); i++) {
Common::SharedPtr<Room> room = g_twp->_rooms[i];
for (size_t j = 0; j < room->_layers.size(); j++) {
Common::SharedPtr<Layer> layer = room->_layers[j];
for (size_t k = 0; k < layer->_objects.size(); k++) {
Common::SharedPtr<Object> obj = layer->_objects[k];
if (getId(obj->_table) == id)
return obj;
}
}
}
return nullptr;
return g_twp->_resManager->_allObjects[id];
}
Common::SharedPtr<Object> sqobj(HSQOBJECT table) {

View File

@ -97,6 +97,7 @@ bool Thread::call() {
}
bool Thread::update(float elapsed) {
uint32 startTime = g_system->getMillis();
if (_paused) {
} else if (_waitTime > 0) {
_waitTime -= elapsed;
@ -111,6 +112,7 @@ bool Thread::update(float elapsed) {
resume();
}
}
_lastUpdateTime = g_system->getMillis() - startTime;
return isDead();
}

View File

@ -66,6 +66,7 @@ public:
int _numFrames = 0;
bool _paused = false;
bool _pauseable = false;
uint32 _lastUpdateTime = 0;
protected:
int _id = 0;

View File

@ -443,6 +443,7 @@ public:
};
void TwpEngine::update(float elapsed) {
const uint32 startUpdateTime = _system->getMillis();
_time += elapsed;
_frameCounter++;
@ -551,6 +552,7 @@ void TwpEngine::update(float elapsed) {
// update actorswitcher
_actorSwitcher.update(actorSwitcherSlots(), elapsed);
const uint32 endMiscTime = _system->getMillis();
// update cutscene
if (_cutscene) {
@ -558,6 +560,7 @@ void TwpEngine::update(float elapsed) {
_cutscene.reset();
}
}
const uint32 endUpdateCutsceneTime = _system->getMillis();
// update threads: make a copy of the threads because during threads update, new threads can be added
Common::Array<Common::SharedPtr<ThreadBase> > threads(_threads);
@ -570,7 +573,6 @@ void TwpEngine::update(float elapsed) {
threadsToRemove.push_back(thread);
}
}
// remove threads that are terminated
for (auto it = threadsToRemove.begin(); it != threadsToRemove.end(); it++) {
Common::SharedPtr<ThreadBase> thread(*it);
@ -579,6 +581,7 @@ void TwpEngine::update(float elapsed) {
_threads.remove_at(i);
}
}
const uint32 endUpdateThreadTime = _system->getMillis();
// update callbacks
for (auto it = _callbacks.begin(); it != _callbacks.end();) {
@ -590,6 +593,7 @@ void TwpEngine::update(float elapsed) {
it++;
}
const uint32 endUpdateCallbacksTime = _system->getMillis();
// update tasks
Common::Array<Common::SharedPtr<Task> > tasks(_tasks);
Common::Array<Common::SharedPtr<Task> > tasksToRemove;
@ -609,11 +613,13 @@ void TwpEngine::update(float elapsed) {
_tasks.remove_at(i);
}
}
const uint32 endUpdateTasksTimes = _system->getMillis();
// update objects
if (_room) {
_room->update(elapsed);
}
const uint32 endUpdateTimeRoom = _system->getMillis();
// update inventory
const bool hudActive = (_room->_fullscreen == FULLSCREENROOM && (scrPos.getY() < 180.f));
@ -628,6 +634,14 @@ void TwpEngine::update(float elapsed) {
}
updateTriggers();
const uint32 endUpdateTime = _system->getMillis();
_stats.totalUpdateTime = endUpdateTime - startUpdateTime;
_stats.updateRoomTime = endUpdateTimeRoom - endUpdateTasksTimes;
_stats.updateTasksTime = endUpdateTasksTimes - endUpdateCallbacksTime;
_stats.updateMiscTime = endMiscTime - startUpdateTime;
_stats.updateCutsceneTime = endUpdateCutsceneTime - endMiscTime;
_stats.updateThreadsTime = endUpdateThreadTime - endUpdateCutsceneTime;
_stats.updateCallbacksTime = endUpdateCallbacksTime - endUpdateThreadTime;
}
void TwpEngine::setShaderEffect(RoomEffect effect) {
@ -1011,7 +1025,9 @@ Common::Error TwpEngine::run() {
time = newTime;
update(_speed * delta / 1000.f);
const uint32 startDrawTime = _system->getMillis();
draw();
_stats.drawTime = _system->getMillis() - startDrawTime;
_cursor.update();
// Delay for a bit. All events loops should have a delay
@ -1090,7 +1106,9 @@ static void onGetPairs(const Common::String &k, HSQOBJECT &oTable, void *data) {
if (!sqrawexists(oTable, "flags"))
sqsetf(oTable, "flags", 0);
Common::SharedPtr<Object> obj(new Object(oTable, k));
setId(obj->_table, g_twp->_resManager->newObjId());
const int id = g_twp->_resManager->newObjId();
setId(obj->_table, id);
g_twp->_resManager->_allObjects[id] = obj;
obj->_node = Common::SharedPtr<Node>(new Node(k));
obj->_nodeAnim = Common::SharedPtr<Anim>(new Anim(obj.get()));
obj->_node->addChild(obj->_nodeAnim.get());
@ -1112,7 +1130,9 @@ static void onGetPairs(const Common::String &k, HSQOBJECT &oTable, void *data) {
}
sqgetf(params->room->_table, k, obj->_table);
setId(obj->_table, g_twp->_resManager->newObjId());
const int id = g_twp->_resManager->newObjId();
setId(obj->_table, id);
g_twp->_resManager->_allObjects[id] = obj;
debugC(kDebugGame, "Create object: %s #%d", k.c_str(), obj->getId());
// add it to the root table if not a pseudo room
@ -1179,7 +1199,9 @@ Common::SharedPtr<Room> TwpEngine::defineRoom(const Common::String &name, HSQOBJ
sq_pop(v, 1);
// assign an id
setId(obj->_table, g_twp->_resManager->newObjId());
const int id = _resManager->newObjId();
setId(obj->_table, id);
_resManager->_allObjects[id] = obj;
// info fmt"Create object with new table: {obj.name} #{obj.id}"
// adds the object to the room table

View File

@ -232,6 +232,17 @@ public:
bool isLeftDown() { return !oldLeftDown && leftDown; }
bool isRightDown() { return !oldRightDown && rightDown; }
} _cursor;
struct Stats {
uint32 totalUpdateTime = 0;
uint32 updateRoomTime = 0;
uint32 updateTasksTime = 0;
uint32 updateMiscTime = 0;
uint32 updateCutsceneTime = 0;
uint32 updateThreadsTime = 0;
uint32 updateCallbacksTime = 0;
uint32 drawTime = 0;
} _stats;
unique_ptr<Hud> _hud;
Inventory _uiInv;
ActorSwitcher _actorSwitcher;