+void AssetLoader::LoadModels(
+ const string &set_name,
+ ModelRegistry &models,
+ ResourceIndex &tex_index,
+ const ShapeRegistry &shapes
+) const {
+ string full = data + set_name + ".models";
+ ifstream file(full);
+ if (!file) {
+ throw runtime_error("failed to open model file " + full);
+ }
+ TokenStreamReader in(file);
+ string model_name;
+ string prop_name;
+ while (in.HasMore()) {
+ in.ReadIdentifier(model_name);
+ in.Skip(Token::EQUALS);
+ in.Skip(Token::ANGLE_BRACKET_OPEN);
+ Model &model = models.Add(model_name);
+ while (in.HasMore() && in.Peek().type != Token::ANGLE_BRACKET_CLOSE) {
+ in.ReadIdentifier(prop_name);
+ in.Skip(Token::EQUALS);
+ if (prop_name == "root") {
+ model.RootPart().Read(in, tex_index, shapes);
+ } else if (prop_name == "body") {
+ model.SetBody(in.GetULong());
+ } else if (prop_name == "eyes") {
+ model.SetEyes(in.GetULong());
+ } else {
+ while (in.HasMore() && in.Peek().type != Token::SEMICOLON) {
+ in.Next();
+ }
+ }
+ in.Skip(Token::SEMICOLON);
+ }
+ model.Enumerate();
+ in.Skip(Token::ANGLE_BRACKET_CLOSE);
+ in.Skip(Token::SEMICOLON);
+ }
+}
+
+void AssetLoader::LoadShapes(const string &set_name, ShapeRegistry &shapes) const {
+ string full = data + set_name + ".shapes";
+ ifstream file(full);
+ if (!file) {
+ throw runtime_error("failed to open shape file " + full);
+ }
+ TokenStreamReader in(file);
+ string shape_name;
+ while (in.HasMore()) {
+ in.ReadIdentifier(shape_name);
+ in.Skip(Token::EQUALS);
+ Shape &shape = shapes.Add(shape_name);
+ shape.Read(in);
+ in.Skip(Token::SEMICOLON);
+ }
+}
+
+Sound AssetLoader::LoadSound(const string &name) const {
+ string full = sounds + name + ".wav";
+ return Sound(full.c_str());
+}
+
+Texture AssetLoader::LoadTexture(const string &name) const {
+ string full = textures + name + ".png";
+ Texture tex;
+ SDL_Surface *srf = IMG_Load(full.c_str());
+ if (!srf) {
+ throw SDLError("IMG_Load");
+ }
+ tex.Bind();
+ tex.Data(*srf);
+ SDL_FreeSurface(srf);
+ return tex;
+}
+
+void AssetLoader::LoadTexture(const string &name, ArrayTexture &tex, int layer) const {
+ string full = textures + name + ".png";
+ SDL_Surface *srf = IMG_Load(full.c_str());
+ if (!srf) {
+ throw SDLError("IMG_Load");
+ }
+ tex.Bind();
+ try {
+ tex.Data(layer, *srf);
+ } catch (...) {
+ SDL_FreeSurface(srf);
+ throw;
+ }
+ SDL_FreeSurface(srf);
+}
+
+void AssetLoader::LoadTextures(const ResourceIndex &index, ArrayTexture &tex) const {
+ // TODO: where the hell should that size come from?
+ tex.Reserve(16, 16, index.Size(), Format());
+ for (const auto &entry : index.Entries()) {
+ LoadTexture(entry.first, tex, entry.second);
+ }
+}
+
+
+void FrameCounter::EnterFrame() noexcept {
+ last_enter = SDL_GetTicks();
+ last_tick = last_enter;
+}
+
+void FrameCounter::EnterHandle() noexcept {
+ Tick();
+}
+
+void FrameCounter::ExitHandle() noexcept {
+ current.handle = Tick();
+}
+
+void FrameCounter::EnterUpdate() noexcept {
+ Tick();
+}
+
+void FrameCounter::ExitUpdate() noexcept {
+ current.update = Tick();
+}
+
+void FrameCounter::EnterRender() noexcept {
+ Tick();
+}
+
+void FrameCounter::ExitRender() noexcept {
+ current.render = Tick();
+}
+
+void FrameCounter::ExitFrame() noexcept {
+ Uint32 now = SDL_GetTicks();
+ current.total = now - last_enter;
+ current.running = current.handle + current.update + current.render;
+ current.waiting = current.total - current.running;
+ Accumulate();
+
+ ++cur_frame;
+ if (cur_frame >= NUM_FRAMES) {
+ Push();
+ cur_frame = 0;
+ changed = true;
+ } else {
+ changed = false;
+ }
+}
+
+int FrameCounter::Tick() noexcept {
+ Uint32 now = SDL_GetTicks();
+ int delta = now - last_tick;
+ last_tick = now;
+ return delta;
+}
+
+void FrameCounter::Accumulate() noexcept {
+ sum.handle += current.handle;
+ sum.update += current.update;
+ sum.render += current.render;
+ sum.running += current.running;
+ sum.waiting += current.waiting;
+ sum.total += current.total;
+
+ max.handle = std::max(current.handle, max.handle);
+ max.update = std::max(current.update, max.update);
+ max.render = std::max(current.render, max.render);
+ max.running = std::max(current.running, max.running);
+ max.waiting = std::max(current.waiting, max.waiting);
+ max.total = std::max(current.total, max.total);
+
+ current = Frame<int>();
+}
+
+void FrameCounter::Push() noexcept {
+ peak = max;
+ avg.handle = sum.handle * factor;
+ avg.update = sum.update * factor;
+ avg.render = sum.render * factor;
+ avg.running = sum.running * factor;
+ avg.waiting = sum.waiting * factor;
+ avg.total = sum.total * factor;
+
+ //Print(cout);
+
+ sum = Frame<int>();
+ max = Frame<int>();
+}
+
+void FrameCounter::Print(ostream &out) const {
+ out << fixed << right << setprecision(2) << setfill(' ')
+ << "PEAK handle: " << setw(2) << peak.handle
+ << ".00ms, update: " << setw(2) << peak.update
+ << ".00ms, render: " << setw(2) << peak.render
+ << ".00ms, running: " << setw(2) << peak.running
+ << ".00ms, waiting: " << setw(2) << peak.waiting
+ << ".00ms, total: " << setw(2) << peak.total
+ << ".00ms" << endl
+ << " AVG handle: " << setw(5) << avg.handle
+ << "ms, update: " << setw(5) << avg.update
+ << "ms, render: " << setw(5) << avg.render
+ << "ms, running: " << setw(5) << avg.running
+ << "ms, waiting: " << setw(5) << avg.waiting
+ << "ms, total: " << setw(5) << avg.total
+ << "ms" << endl;
+}
+