From 21ce3f06312a02c23d6b3c89f64f84222f54fe6d Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Sat, 26 Nov 2016 12:39:22 -0500 Subject: optimizing work --- include/engine.hpp | 11 +-- main.cpp | 73 ++++++++--------- src/engine.cpp | 2 +- src/ui.cpp | 9 +- src/world.cpp | 237 ++++++++++++++++++++--------------------------------- xml/bobshouse.xml | 2 +- 6 files changed, 134 insertions(+), 200 deletions(-) diff --git a/include/engine.hpp b/include/engine.hpp index d4b1e34..c842b59 100644 --- a/include/engine.hpp +++ b/include/engine.hpp @@ -11,10 +11,9 @@ //game::engine::Systems->add>(); class Engine : public entityx::Receiver { -private: - bool gameRunning; - public: + bool shouldRun; + entityx::SystemManager systems; explicit Engine(void); @@ -33,11 +32,7 @@ public: }*/ inline void receive(const GameEndEvent &gee) { - gameRunning = !(gee.really); - } - - inline bool shouldRun(void) const { - return gameRunning; + shouldRun = !(gee.really); } }; diff --git a/main.cpp b/main.cpp index 2205897..982d70b 100644 --- a/main.cpp +++ b/main.cpp @@ -28,6 +28,9 @@ using namespace tinyxml2; #include #include +#include + +using namespace std::literals::chrono_literals; /* ---------------------------------------------------------------------------- ** Variables section @@ -63,9 +66,6 @@ void logic(void); // handles all rendering operations void render(void); -// takes care of *everything* -void mainLoop(void); - /******************************************************************************* ** MAIN ************************************************************************ ********************************************************************************/ @@ -179,9 +179,6 @@ int main(int argc, char *argv[]) pdat.close(); } - if (worldDontReallyRun) - goto EXIT_ROUTINE; - if (!worldActuallyUseThisXMLFile.empty()) { game::engine.getSystem()->load(worldActuallyUseThisXMLFile); } else { @@ -198,30 +195,41 @@ int main(int argc, char *argv[]) ui::menu::init(); - // the main loop, in all of its gloriousness.. - std::thread([&]{ - while (game::engine.shouldRun()) { - mainLoop(); - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } - }).detach(); - // the debug loop, gets debug screen values - std::thread([&]{ - while (game::engine.shouldRun()) { - fps = 1000 / game::time::getDeltaTime(); -// debugY = player->loc.y; - std::this_thread::sleep_for(std::chrono::seconds(1)); - } - }).detach(); + if (!worldDontReallyRun) { + // the main loop, in all of its gloriousness.. + std::thread([&] { + const bool &run = game::engine.shouldRun; + while (run) { + game::time::mainLoopHandler(); - while (game::engine.shouldRun()) { - game::engine.render(0); - render(); - } + if (game::time::tickHasPassed()) + logic(); + + game::engine.update(game::time::getDeltaTime()); + + std::this_thread::sleep_for(1ms); + } + }).detach(); + + // the debug loop, gets debug screen values + std::thread([&] { + const bool &run = game::engine.shouldRun; + while (run) { + fps = 1000 / game::time::getDeltaTime(); +// debugY = player->loc.y; -EXIT_ROUTINE: + std::this_thread::sleep_for(1s); + } + }).detach(); + + const bool &run = game::engine.shouldRun; + while (run) { + game::engine.render(0); + render(); + } + } // put away the brice for later game::briceSave(); @@ -242,19 +250,6 @@ EXIT_ROUTINE: return 0; // Calls everything passed to atexit } -void mainLoop(void){ - game::time::mainLoopHandler(); - - if (currentMenu) { - return; - } else { - if (game::time::tickHasPassed()) - logic(); - - game::engine.update(game::time::getDeltaTime()); - } -} - void render() { const auto SCREEN_WIDTH = game::SCREEN_WIDTH; const auto SCREEN_HEIGHT = game::SCREEN_HEIGHT; diff --git a/src/engine.cpp b/src/engine.cpp index 2aa19b0..5e8b3e5 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -11,7 +11,7 @@ extern World *currentWorld; Engine::Engine(void) - : gameRunning(true), systems(game::entities, game::events) + : shouldRun(true), systems(game::entities, game::events) { } diff --git a/src/ui.cpp b/src/ui.cpp index 345cfb3..94100e4 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -10,6 +10,9 @@ #include #include +#include +using namespace std::literals::chrono_literals; + extern Menu* currentMenu; std::array controlMap = { @@ -571,14 +574,14 @@ namespace ui { void waitForDialog(void) { while (dialogBoxExists) - std::this_thread::sleep_for(std::chrono::milliseconds(1)); + std::this_thread::sleep_for(1ms); } void waitForCover(void) { auto& fi = fadeIntensity; fi = 0; while (fi < 255) - std::this_thread::sleep_for(std::chrono::milliseconds(1)); + std::this_thread::sleep_for(1ms); fi = 255; } @@ -787,7 +790,7 @@ namespace ui { glUniform4f(Render::textShader.uniform[WU_tex_color], c.red, c.green, c.blue, c.alpha); glBindTexture(GL_TEXTURE_2D, box_corner); - + // draw upper left corner glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, stride, &box_ul[0]); glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, stride, &box_ul[3]); diff --git a/src/world.cpp b/src/world.cpp index 52cbe45..f4795ac 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -10,6 +10,8 @@ #include #include #include +#include +using namespace std::literals::chrono_literals; // local game headers #include @@ -50,6 +52,9 @@ void makeWorldDrawingSimplerEvenThoughAndyDoesntThinkWeCanMakeItIntoFunctions_Ju extern std::string xmlFolder; +// wait +static bool waitToSwap = false; + // particle mutex std::mutex partMutex; @@ -652,7 +657,7 @@ void WorldSystem::render(void) ambient = Color(rg, rg, b, 1.0f); - std::this_thread::sleep_for(std::chrono::milliseconds(1)); + std::this_thread::sleep_for(1ms); } }).detach(); } @@ -882,18 +887,23 @@ void WorldSystem::render(void) 0, static_cast(world.data.size())) + 1; // draw the dirt - bgTex++; - std::vector> c; + waitToSwap = true; - for (int i = iStart; i < iEnd; i++) { + bgTex++; - // world switching changes world data size, render doesn't know - // because it's in a different thread, I guess. make sure we don't - // die: - if (i > static_cast(world.data.size())) - return; // death for a frame is okay, right? + static std::vector dirt; + if (dirt.size() != world.data.size() * 30) { + dirt.clear(); + dirt.resize(world.data.size() * 30); + } + auto push5 = [](GLfloat *&vec, GLfloat *five) { + for (int i = 0; i < 5; i++) + *vec++ = *five++; + }; + GLfloat *dirtp = &dirt[0]; + for (int i = iStart; i < iEnd; i++) { if (world.data[i].groundHeight <= 0) { // TODO holes (andy) world.data[i].groundHeight = GROUND_HEIGHT_MINIMUM - 1; glColor4ub(0, 0, 0, 255); @@ -903,38 +913,33 @@ void WorldSystem::render(void) int ty = world.data[i].groundHeight / 64 + world.data[i].groundColor; - c.push_back(std::make_pair(vec2(0, 0), vec3(world.startX + HLINES(i), world.data[i].groundHeight - GRASS_HEIGHT, -4.0f))); - c.push_back(std::make_pair(vec2(1, 0), vec3(world.startX + HLINES(i) + HLINE, world.data[i].groundHeight - GRASS_HEIGHT, -4.0f))); - c.push_back(std::make_pair(vec2(1, ty),vec3(world.startX + HLINES(i) + HLINE, 0, -4.0f))); - - c.push_back(std::make_pair(vec2(1, ty),vec3(world.startX + HLINES(i) + HLINE, 0, -4.0f))); - c.push_back(std::make_pair(vec2(0, ty),vec3(world.startX + HLINES(i), 0, -4.0f))); - c.push_back(std::make_pair(vec2(0, 0), vec3(world.startX + HLINES(i), world.data[i].groundHeight - GRASS_HEIGHT, -4.0f))); + GLfloat five[5] = { + 0, 0, world.startX + HLINES(i), world.data[i].groundHeight - GRASS_HEIGHT, -4 + }; + + push5(dirtp, five); + five[0]++, five[2] += game::HLINE; + push5(dirtp, five); + five[1] += ty, five[3] = 0; + push5(dirtp, five); + push5(dirtp, five); + five[0]--, five[2] -= game::HLINE; + push5(dirtp, five); + five[1] = 0, five[3] = world.data[i].groundHeight - GRASS_HEIGHT; + push5(dirtp, five); if (world.data[i].groundHeight == GROUND_HEIGHT_MINIMUM - 1) world.data[i].groundHeight = 0; } - std::vector dirtc; - std::vector dirtt; - - for (auto &v : c) { - dirtc.push_back(v.second.x); - dirtc.push_back(v.second.y); - dirtc.push_back(v.second.z); - - dirtt.push_back(v.first.x); - dirtt.push_back(v.first.y); - } - Render::worldShader.use(); glUniform1f(Render::worldShader.uniform[WU_light_impact], 0.45f); Render::worldShader.enable(); - glVertexAttribPointer(Render::worldShader.coord, 3, GL_FLOAT, GL_FALSE, 0, &dirtc[0]); - glVertexAttribPointer(Render::worldShader.tex, 2, GL_FLOAT, GL_FALSE, 0, &dirtt[0]); - glDrawArrays(GL_TRIANGLES, 0 , c.size()); + glVertexAttribPointer(Render::worldShader.coord, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), &dirt[2]); + glVertexAttribPointer(Render::worldShader.tex, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), &dirt[0]); + glDrawArrays(GL_TRIANGLES, 0 , dirt.size() / 5); Render::worldShader.disable(); Render::worldShader.unuse(); @@ -943,15 +948,14 @@ void WorldSystem::render(void) bgTex++; safeSetColorA(255, 255, 255, 255); - c.clear(); - std::vector grassc; - std::vector grasst; + static std::vector grass; + if (grass.size() != world.data.size() * 60) { + grass.clear(); + grass.resize(world.data.size() * 60); + } + GLfloat *grassp = &grass[0]; for (int i = iStart; i < iEnd; i++) { - - if (i > static_cast(world.data.size())) - return; // see dirt rendering - auto wd = world.data[i]; auto gh = wd.grassHeight; @@ -963,102 +967,34 @@ void WorldSystem::render(void) // actually draw the grass. if (wd.groundHeight) { - const auto& worldStart = world.startX; - - grasst.push_back(0); - grasst.push_back(1); - - grassc.push_back(worldStart + HLINES(i)); - grassc.push_back(wd.groundHeight + gh[0]); - grassc.push_back(-3); - - - grasst.push_back(1); - grasst.push_back(0); - - grassc.push_back(worldStart + HLINES(i) + HLINE / 2); - grassc.push_back(wd.groundHeight + gh[0]); - grassc.push_back(-3); - - - grasst.push_back(1); - grasst.push_back(1); - - grassc.push_back(worldStart + HLINES(i) + HLINE / 2); - grassc.push_back(wd.groundHeight - GRASS_HEIGHT); - grassc.push_back(-3); - - - grasst.push_back(1); - grasst.push_back(1); - - grassc.push_back(worldStart + HLINES(i) + HLINE / 2); - grassc.push_back(wd.groundHeight - GRASS_HEIGHT); - grassc.push_back(-3); - - - grasst.push_back(0); - grasst.push_back(1); - - grassc.push_back(worldStart + HLINES(i)); - grassc.push_back(wd.groundHeight - GRASS_HEIGHT); - grassc.push_back(-3); - - - grasst.push_back(0); - grasst.push_back(0); - - grassc.push_back(worldStart + HLINES(i)); - grassc.push_back(wd.groundHeight + gh[0]); - grassc.push_back(-3); - - - grasst.push_back(0); - grasst.push_back(0); - - grassc.push_back(worldStart + HLINES(i) + HLINE / 2); - grassc.push_back(wd.groundHeight + gh[1]); - grassc.push_back(-3); - - - grasst.push_back(1); - grasst.push_back(0); - - grassc.push_back(worldStart + HLINES(i) + HLINE); - grassc.push_back(wd.groundHeight + gh[1]); - grassc.push_back(-3); - - - grasst.push_back(1); - grasst.push_back(1); - - grassc.push_back(worldStart + HLINES(i) + HLINE); - grassc.push_back(wd.groundHeight - GRASS_HEIGHT); - grassc.push_back(-3); - - - grasst.push_back(1); - grasst.push_back(1); - - grassc.push_back(worldStart + HLINES(i) + HLINE); - grassc.push_back(wd.groundHeight - GRASS_HEIGHT); - grassc.push_back(-3); - - - grasst.push_back(0); - grasst.push_back(1); - - grassc.push_back(worldStart + HLINES(i) + HLINE / 2); - grassc.push_back(wd.groundHeight - GRASS_HEIGHT); - grassc.push_back(-3); - - - grasst.push_back(0); - grasst.push_back(0); - - grassc.push_back(worldStart + HLINES(i) + HLINE / 2); - grassc.push_back(wd.groundHeight + gh[1]); - grassc.push_back(-3); + float five[5] = { + 0, 1, world.startX + HLINES(i), wd.groundHeight + gh[0], -3 + }; + + push5(grassp, five); + five[0]++, five[1]--, five[2] += game::HLINE / 2; + push5(grassp, five); + five[1]++, five[3] = wd.groundHeight - GRASS_HEIGHT; + push5(grassp, five); + push5(grassp, five); + five[0]--, five[2] -= game::HLINE / 2; + push5(grassp, five); + five[1]--, five[3] = wd.groundHeight + gh[0]; + push5(grassp, five); + five[1]++; + + five[2] = world.startX + HLINES(i + 0.5), five[3] = wd.groundHeight + gh[1]; + + push5(grassp, five); + five[0]++, five[1]--, five[2] += game::HLINE / 2 + 1; + push5(grassp, five); + five[1]++, five[3] = wd.groundHeight - GRASS_HEIGHT; + push5(grassp, five); + push5(grassp, five); + five[0]--, five[2] -= game::HLINE / 2 + 1; + push5(grassp, five); + five[1]--, five[3] = wd.groundHeight + gh[1]; + push5(grassp, five); } } @@ -1067,9 +1003,9 @@ void WorldSystem::render(void) Render::worldShader.enable(); - glVertexAttribPointer(Render::worldShader.coord, 3, GL_FLOAT, GL_FALSE, 0, &grassc[0]); - glVertexAttribPointer(Render::worldShader.tex, 2, GL_FLOAT, GL_FALSE, 0, &grasst[0]); - glDrawArrays(GL_TRIANGLES, 0 , grassc.size()/3); + glVertexAttribPointer(Render::worldShader.coord, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), &grass[2]); + glVertexAttribPointer(Render::worldShader.tex, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), &grass[0]); + glDrawArrays(GL_TRIANGLES, 0 , grass.size() / 5); // the starting pixel of the world float s = -(static_cast(SCREEN_WIDTH)/2.0f); @@ -1099,13 +1035,12 @@ void WorldSystem::render(void) glBindTexture(GL_TEXTURE_2D, Texture::genColor(Color(0,0,0))); glUniform1f(Render::worldShader.uniform[WU_light_impact], 0.0f); - GLfloat blackBarRight[] = {grassc[grassc.size()-3], 0.0f, -3.5f, 0.0f, 0.0f, - e, 0.0f, -3.5f, 1.0f, 0.0f, - e, static_cast(SCREEN_HEIGHT), -3.5f, 1.0f, 1.0f, - - e, static_cast(SCREEN_HEIGHT), -3.5f, 1.0f, 1.0f, - grassc[grassc.size()-3], static_cast(SCREEN_HEIGHT), -3.5f, 0.0f, 1.0f, - grassc[grassc.size()-3], 0.0f, -3.5f, 0.0f, 0.0f}; + GLfloat blackBarRight[] = {-(world.startX), 0.0f, -3.5f, 0.0f, 0.0f, + e, 0.0f, -3.5f, 1.0f, 0.0f, + e, static_cast(SCREEN_HEIGHT), -3.5f, 1.0f, 1.0f, + e, static_cast(SCREEN_HEIGHT), -3.5f, 1.0f, 1.0f, + -(world.startX), static_cast(SCREEN_HEIGHT), -3.5f, 0.0f, 1.0f, + -(world.startX), 0.0f, -3.5f, 0.0f, 0.0f}; glVertexAttribPointer(Render::worldShader.coord, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat)*5, &blackBarRight[0]); glVertexAttribPointer(Render::worldShader.tex, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat)*5, &blackBarRight[3]); @@ -1125,6 +1060,8 @@ void WorldSystem::render(void) Render::worldShader.unuse(); } + waitToSwap = false; + //player->draw(); } @@ -1161,7 +1098,7 @@ void WorldSystem::update(entityx::EntityManager &en, entityx::EventManager &ev, (void)dt; // fade in music if not playing - if (bgmObj != nullptr && !Mix_PlayingMusic()) + if (!Mix_PlayingMusic() && bgmObj != nullptr) Mix_FadeInMusic(bgmObj, -1, 2000); // run detect stuff @@ -1241,8 +1178,9 @@ void WorldSystem::goWorldRight(Position& p, Solid &d) if (!(world.toRight.empty()) && (p.x + d.width > world.startX * -1 - HLINES(5))) { ui::toggleBlack(); ui::waitForCover(); - auto file = world.toRight; - load(file); + while (waitToSwap) + std::this_thread::sleep_for(1ms); + load(world.toRight); game::engine.getSystem()->setX(world.startX + HLINES(10)); ui::toggleBlack(); } @@ -1253,6 +1191,8 @@ void WorldSystem::goWorldLeft(Position& p) if (!(world.toLeft.empty()) && (p.x < world.startX + HLINES(10))) { ui::toggleBlack(); ui::waitForCover(); + while (waitToSwap) + std::this_thread::sleep_for(1ms); load(world.toLeft); game::engine.getSystem()->setX(world.startX * -1 - HLINES(15)); ui::toggleBlack(); @@ -1282,9 +1222,10 @@ void WorldSystem::goWorldPortal(Position& p) } if (!file.empty()) { - std::cout << file << std::endl; ui::toggleBlack(); ui::waitForCover(); + while (waitToSwap) + std::this_thread::sleep_for(1ms); load(file); ui::toggleBlack(); } diff --git a/xml/bobshouse.xml b/xml/bobshouse.xml index c9c08b9..8993a5c 100644 --- a/xml/bobshouse.xml +++ b/xml/bobshouse.xml @@ -3,7 +3,7 @@