diff options
author | Clyne Sullivan <tullivan99@gmail.com> | 2017-05-06 22:10:34 -0400 |
---|---|---|
committer | Clyne Sullivan <tullivan99@gmail.com> | 2017-05-06 22:10:34 -0400 |
commit | 70eced563f3ba4d98ed0a7952a81b69903ed1803 (patch) | |
tree | 66612356aeb22a4c2074cb19b30135db5cdd8722 | |
parent | d6dac7e8336b435c2f335aa824df0aedd63074c6 (diff) |
rendering improvements
-rw-r--r-- | include/config.hpp | 5 | ||||
-rw-r--r-- | include/font.hpp | 6 | ||||
-rw-r--r-- | include/player.hpp | 3 | ||||
-rw-r--r-- | include/thread.hpp | 24 | ||||
-rw-r--r-- | include/world.hpp | 8 | ||||
-rw-r--r-- | main.cpp | 44 | ||||
-rw-r--r-- | src/components.cpp | 42 | ||||
-rw-r--r-- | src/config.cpp | 5 | ||||
-rw-r--r-- | src/font.cpp | 79 | ||||
-rw-r--r-- | src/inventory.cpp | 48 | ||||
-rw-r--r-- | src/player.cpp | 13 | ||||
-rw-r--r-- | src/render.cpp | 5 | ||||
-rw-r--r-- | src/ui.cpp | 51 | ||||
-rw-r--r-- | src/world.cpp | 175 |
14 files changed, 236 insertions, 272 deletions
diff --git a/include/config.hpp b/include/config.hpp index 305f61e..e825299 100644 --- a/include/config.hpp +++ b/include/config.hpp @@ -31,6 +31,11 @@ namespace game { */ extern bool FULLSCREEN; + /** + * V-Sync is enabled if this is true. Only checked at program start. + */ + extern bool vsync; + namespace config { /** * The current volume level of the master channel. diff --git a/include/font.hpp b/include/font.hpp index a520c15..7dc7fc6 100644 --- a/include/font.hpp +++ b/include/font.hpp @@ -1,8 +1,9 @@ #ifndef FONT_HPP_ #define FONT_HPP_ -#include <vector> #include <map> +#include <memory> +#include <vector> #include <color.hpp> #include <render.hpp> @@ -28,6 +29,7 @@ private: static std::string fontFamily; static std::map<int, std::vector<FT_Info>> fontData; + static std::vector<std::unique_ptr<GLfloat>> drawData; static int currentSize; static Color currentColor; @@ -46,6 +48,8 @@ public: static vec2 putChar(float xx, float yy, char c); + static void render(void); + static inline int getSize(void) { return currentSize; } diff --git a/include/player.hpp b/include/player.hpp index 17ce2c1..077e798 100644 --- a/include/player.hpp +++ b/include/player.hpp @@ -80,8 +80,7 @@ public: * Gets the width of the player. * @return the player's width, according to its sprite */ - static inline float getWidth(void) - { return game::entities.component<Solid>(player.id())->width; } + static float getWidth(void); }; #endif // PLAYER_HPP_ diff --git a/include/thread.hpp b/include/thread.hpp index 3adc43d..0dd20f9 100644 --- a/include/thread.hpp +++ b/include/thread.hpp @@ -12,15 +12,22 @@ class GameThread : public entityx::Receiver<GameThread> { private: + static std::atomic_bool pause; + std::atomic_bool die; std::thread thread; public: GameThread(std::function<void(void)> func) { die.store(false); + pause.store(false); thread = std::thread([&](std::function<void(void)> f) { - while (!die.load()) - f(); + while (!die.load()) { + if (!pause.load()) + f(); + else + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } }, func); } @@ -31,6 +38,17 @@ public: inline void stop(void) { die.store(true); } + + static inline void pauseAll(void) { + pause.store(true); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } + + static inline void resumeAll(void) + { pause.store(false); } + + static inline bool isPaused(void) + { return pause.load(); } }; -#endif // THREAD_HPP_
\ No newline at end of file +#endif // THREAD_HPP_ diff --git a/include/world.hpp b/include/world.hpp index ac55b56..bfd0464 100644 --- a/include/world.hpp +++ b/include/world.hpp @@ -136,8 +136,12 @@ private: */ static std::string currentXMLFile; + static std::string toLoad; + static std::vector<vec2> stars; + static int getLineIndex(float x); + public: static std::thread thAmbient; @@ -178,6 +182,10 @@ public: void fight(entityx::Entity entity); static void die(void); + static void loader(void); + + static inline bool shouldLoad(void) + { return !toLoad.empty(); } }; #endif // WORLD_HPP_ @@ -25,25 +25,13 @@ using namespace std::literals::chrono_literals; #include <ui.hpp> #include <inventory.hpp> -/** - * The currently used folder to look for XML files in. - */ -std::string xmlFolder = "./xml/"; +std::atomic_bool GameThread::pause; /** * The current center of the screen, updated in main render. */ vec2 offset; -std::size_t getUsedMem(void); -void deleteRemaining(void); -extern int balance; - -void print(void) { - std::cout << "Used memory: " << getUsedMem() << " bytes\n"; - std::cout << "New/delete balance: " << balance << '\n'; -} - /** * The main program. * Init, load, run. Die. @@ -53,10 +41,6 @@ int main(int argc, char *argv[]) static bool worldReset = false, worldDontReallyRun = false; std::string worldActuallyUseThisXMLFile; - print(); - atexit(print); - //atexit(deleteRemaining); - // handle command line args if (argc > 1) { for (int i = 1; i < argc; i++) { @@ -71,10 +55,7 @@ int main(int argc, char *argv[]) } } - // // init the main game engine - // - game::engine.init(); // initialize the renderer @@ -89,14 +70,14 @@ int main(int argc, char *argv[]) // read in all XML file names in the folder std::list<std::string> xmlFiles; - if (getdir(std::string("./" + xmlFolder), xmlFiles)) + if (getdir(std::string("./" + game::config::xmlFolder), xmlFiles)) UserError("Error reading XML files!!!"); // kill the world if needed if (worldReset) { for (const auto& s : xmlFiles) { if (s.find(".dat", s.size() - 4) != std::string::npos) { - std::string re = xmlFolder; + std::string re = game::config::xmlFolder; re.append(s); auto r = re.c_str(); std::cout << "Removing " << r << "...\n"; @@ -123,17 +104,14 @@ int main(int argc, char *argv[]) } } + WorldSystem::loader(); + ///////////////////////////// // // // actually start the game // // // ///////////////////////////// - - InventorySystem::add("Hunters Bow", 1); - InventorySystem::add("Wood Sword", 1); - InventorySystem::add("Arrow", 198); - std::list<SDL_Event> eventQueue; if (!worldDontReallyRun) { @@ -167,14 +145,15 @@ int main(int argc, char *argv[]) std::this_thread::sleep_for(1s); }); - /*GameThread gtFade ([&] { - ui::fadeUpdate(); - std::this_thread::sleep_for(20ms); - });*/ - // the render loop, renders const bool &run = game::engine.shouldRun; while (run) { + if (WorldSystem::shouldLoad()) { + GameThread::pauseAll(); + WorldSystem::loader(); + GameThread::resumeAll(); + } + fpsInternal++; Render::render(fps); @@ -188,7 +167,6 @@ int main(int argc, char *argv[]) // on game end, get back together gtMain.stop(); gtDebug.stop(); - //gtFade.stop(); //game::engine.getSystem<WorldSystem>()->thAmbient.join(); // segfault or something } diff --git a/src/components.cpp b/src/components.cpp index f5faf6b..056d48b 100644 --- a/src/components.cpp +++ b/src/components.cpp @@ -128,18 +128,18 @@ Texture RenderSystem::loadTexture(const std::string& file) } void RenderSystem::render(void) -{ +{ if (!loadTexString.empty()) { loadTexResult = Texture(loadTexString, false); loadTexString.clear(); } - - Render::worldShader.use(); - Render::worldShader.enable(); if (!loadTexResult.isEmpty()) return; + Render::worldShader.use(); + Render::worldShader.enable(); + game::entities.each<Visible, Sprite, Position>([](entityx::Entity entity, Visible &visible, Sprite &sprite, Position &pos) { // Verticies and shit float its = 0; @@ -161,25 +161,17 @@ void RenderSystem::render(void) for (auto &S : sprite.sprite) { auto sp = S.first; auto size = sp.size * game::HLINE; - vec2 drawOffset(HLINES(S.second.x), HLINES(S.second.y)); - vec2 loc(pos.x + drawOffset.x, pos.y + drawOffset.y); - - GLfloat tex_coord[] = {sp.offset_tex.x, sp.offset_tex.y, - sp.offset_tex.x + sp.size_tex.x, sp.offset_tex.y, - sp.offset_tex.x + sp.size_tex.x, sp.offset_tex.y + sp.size_tex.y, - - sp.offset_tex.x, sp.offset_tex.y, - sp.offset_tex.x + sp.size_tex.x, sp.offset_tex.y + sp.size_tex.y, - sp.offset_tex.x, sp.offset_tex.y + sp.size_tex.y}; - - GLfloat coords[] = {loc.x, loc.y, visible.z + its, - loc.x + size.x, loc.y, visible.z + its, - loc.x + size.x, loc.y + size.y, visible.z + its, - - loc.x, loc.y, visible.z + its, - loc.x + size.x, loc.y + size.y, visible.z + its, - loc.x, loc.y + size.y, visible.z + its}; - + vec2 drawOffset (HLINES(S.second.x), HLINES(S.second.y)); + vec2 loc (pos.x + drawOffset.x, pos.y + drawOffset.y); + + GLfloat verts[] = { + loc.x, loc.y, visible.z + its, sp.offset_tex.x, sp.offset_tex.y, + loc.x + size.x, loc.y, visible.z + its, sp.offset_tex.x + sp.size_tex.x, sp.offset_tex.y, + loc.x + size.x, loc.y + size.y, visible.z + its, sp.offset_tex.x + sp.size_tex.x, sp.offset_tex.y + sp.size_tex.y, + loc.x, loc.y, visible.z + its, sp.offset_tex.x, sp.offset_tex.y, + loc.x + size.x, loc.y + size.y, visible.z + its, sp.offset_tex.x + sp.size_tex.x, sp.offset_tex.y + sp.size_tex.y, + loc.x, loc.y + size.y, visible.z + its, sp.offset_tex.x, sp.offset_tex.y + sp.size_tex.y + }; // make the entity hit flash red // TODO @@ -192,8 +184,8 @@ void RenderSystem::render(void) glUniform1i(Render::worldShader.uniform[WU_texture], 0); - glVertexAttribPointer(Render::worldShader.coord, 3, GL_FLOAT, GL_FALSE, 0, coords); - glVertexAttribPointer(Render::worldShader.tex, 2, GL_FLOAT, GL_FALSE, 0, tex_coord); + glVertexAttribPointer(Render::worldShader.coord, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), verts); + glVertexAttribPointer(Render::worldShader.tex, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), verts + 3); glDrawArrays(GL_TRIANGLES, 0, 6); //glUniform4f(Render::worldShader.uniform[WU_tex_color], 1.0, 1.0, 1.0, 1.0); diff --git a/src/config.cpp b/src/config.cpp index 7f0af00..0bbb400 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -12,6 +12,7 @@ namespace game { unsigned int SCREEN_WIDTH; unsigned int SCREEN_HEIGHT; bool FULLSCREEN; + bool vsync; namespace config { static XMLDocument xml; @@ -34,6 +35,8 @@ namespace game { SCREEN_HEIGHT = 768; if (exml->QueryBoolAttribute("fullscreen", &FULLSCREEN) != XML_NO_ERROR) FULLSCREEN = false; + if (exml->QueryBoolAttribute("vsync", &vsync) != XML_NO_ERROR) + vsync = true; if (xml.FirstChildElement("hline")->QueryUnsignedAttribute("size", &HLINE) != XML_NO_ERROR) HLINE = 3; @@ -47,8 +50,6 @@ namespace game { VOLUME_SFX = 50; xmlFolder = xml.FirstChildElement("world")->StrAttribute("start"); - if (xmlFolder.empty()) - xmlFolder = "xml/"; // FONT SETUP fontFamily = xml.FirstChildElement("font")->Attribute("path"); diff --git a/src/font.cpp b/src/font.cpp index adffa9c..3fdd928 100644 --- a/src/font.cpp +++ b/src/font.cpp @@ -1,12 +1,13 @@ #include <font.hpp> -FT_Library FontSystem::ftLibrary; -FT_Face FontSystem::ftFace; - -std::string FontSystem::fontFamily; -std::map<int, std::vector<FT_Info>> FontSystem::fontData; +#include <cstring> -int FontSystem::currentSize = 0; +FT_Library FontSystem::ftLibrary; +FT_Face FontSystem::ftFace; +std::string FontSystem::fontFamily; +std::map<int, std::vector<FT_Info>> FontSystem::fontData; +std::vector<std::unique_ptr<GLfloat>> FontSystem::drawData; +int FontSystem::currentSize = 0; Color FontSystem::currentColor; float FontSystem::currentZ = -8.0f; @@ -74,58 +75,44 @@ void FontSystem::setFontZ(float z) currentZ = z; } -vec2 FontSystem::putChar(float xx, float yy, char c) +vec2 FontSystem::putChar(float x, float y, char c) { const auto& ch = fontData.at(currentSize)[c - 33]; - int x = xx, y = yy; - // get dimensions of the rendered character - vec2 c1 = { - static_cast<float>(floor(x) + ch.bl.x), - static_cast<float>(floor(y) + ch.bl.y) + vec2 c1 (static_cast<float>(floor(x) + ch.bl.x), static_cast<float>(floor(y) + ch.bl.y)); + vec2 c2 (c1.x + ch.wh.x, c1.y - ch.wh.y); + + GLfloat verts[31] = { + static_cast<GLfloat>(ch.tex), + c1.x, c1.y, currentZ, 0, 0, + c2.x, c1.y, currentZ, 1, 0, + c2.x, c2.y, currentZ, 1, 1, + c2.x, c2.y, currentZ, 1, 1, + c1.x, c2.y, currentZ, 0, 1, + c1.x, c1.y, currentZ, 0, 0, }; - const auto& c2 = ch.wh; + drawData.emplace_back(reinterpret_cast<GLfloat*>(std::memcpy(new GLfloat[31], verts, 31 * sizeof(GLfloat)))); + return ch.ad; +} + +void FontSystem::render(void) +{ Render::textShader.use(); Render::textShader.enable(); - // draw the character - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, ch.tex); - - glUniform4f(Render::textShader.uniform[WU_tex_color], 1.0f, 1.0f, 1.0f, 1.0f); - - GLfloat tex_coord[] = { - 0.0, 1.0, // bottom left - 1.0, 1.0, // bottom right - 1.0, 0.0, // top right - 1.0, 0.0, // top right - 0.0, 0.0, // top left - 0.0, 1.0, // bottom left - }; - - GLfloat text_vert[] = { - c1.x, c1.y - c2.y, currentZ, // bottom left - c1.x + c2.x, c1.y - c2.y, currentZ, // bottom right - c1.x + c2.x, c1.y + c2.y - c2.y, currentZ, // top right - c1.x + c2.x, c1.y + c2.y - c2.y, currentZ, // top right - c1.x, c1.y + c2.y - c2.y, currentZ, // top left - c1.x, c1.y - c2.y, currentZ // bottom left - }; - glUniform4f(Render::textShader.uniform[WU_tex_color], currentColor.red, currentColor.green, currentColor.blue, currentColor.alpha); - glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, 0, text_vert); - glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, 0, tex_coord); - glDrawArrays(GL_TRIANGLES, 0, 6); - - glUniform4f(Render::textShader.uniform[WU_tex_color], 1.0, 1.0, 1.0, 1.0); + for (const auto& d : drawData) { + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, static_cast<GLuint>(d.get()[0])); + glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), d.get() + 1); + glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), d.get() + 4); + glDrawArrays(GL_TRIANGLES, 0, 6); + } Render::textShader.disable(); Render::textShader.unuse(); - - // return the width. - return ch.ad; + drawData.clear(); } - diff --git a/src/inventory.cpp b/src/inventory.cpp index 96c1d6d..55fe3aa 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -91,8 +91,6 @@ void InventorySystem::render(void) size = hotbarSize; } - static const GLfloat tex[12] = {0,1,1,1,1,0,1,0,0,0,0,1}; - // draw items for (int n = 0; n < size; n++) { auto &i = items[n]; @@ -103,20 +101,24 @@ void InventorySystem::render(void) Colors::black.use(); glUniform4f(Render::textShader.uniform[WU_tex_color], 1, 1, 1, .6); - glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, 0, Colors::texCoord); vec2 end = i.loc + entrySize - 6; - GLfloat coords[18] = { - i.loc.x, i.loc.y, inventoryZ - 0.1, end.x, i.loc.y, inventoryZ - 0.1, end.x, end.y, inventoryZ - 0.1, - end.x, end.y, inventoryZ - 0.1, i.loc.x, end.y, inventoryZ - 0.1, i.loc.x, i.loc.y, inventoryZ - 0.1 + GLfloat coords[] = { + i.loc.x, i.loc.y, inventoryZ - 0.1, 0, 0, + end.x, i.loc.y, inventoryZ - 0.1, 0, 0, + end.x, end.y, inventoryZ - 0.1, 0, 0, + end.x, end.y, inventoryZ - 0.1, 0, 0, + i.loc.x, end.y, inventoryZ - 0.1, 0, 0, + i.loc.x, i.loc.y, inventoryZ - 0.1, 0, 0, }; - glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, 0, coords); + + glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), coords); + glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), coords + 3); glDrawArrays(GL_TRIANGLES, 0, 6); glUniform4f(Render::textShader.uniform[WU_tex_color], 1, 1, 1, 1); // draw the item if (isGoodEntry(i)) { i.item->sprite.use(); - glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, 0, tex); auto& dim = i.item->sprite.getDim(); vec2 truDim; @@ -128,11 +130,18 @@ void InventorySystem::render(void) vec2 loc (i.loc.x + ((entrySize - 6) / 2 - truDim.x / 2), i.loc.y); vec2 sta ((n == movingItem) ? ui::mouse - truDim / 2 : loc); vec2 end = (n == movingItem) ? ui::mouse + truDim / 2 : loc + truDim; - GLfloat coords[18] = { - sta.x, sta.y, inventoryZ - 0.2, end.x, sta.y, inventoryZ - 0.2, end.x, end.y, inventoryZ - 0.2, - end.x, end.y, inventoryZ - 0.2, sta.x, end.y, inventoryZ - 0.2, sta.x, sta.y, inventoryZ - 0.2 + + GLfloat coords[] = { + sta.x, sta.y, inventoryZ - 0.2, 0, 1, + end.x, sta.y, inventoryZ - 0.2, 1, 1, + end.x, end.y, inventoryZ - 0.2, 1, 0, + end.x, end.y, inventoryZ - 0.2, 1, 0, + sta.x, end.y, inventoryZ - 0.2, 0, 0, + sta.x, sta.y, inventoryZ - 0.2, 0, 1, }; - glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, 0, coords); + + glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), coords); + glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), coords + 3); if (n == movingItem) glUniform4f(Render::textShader.uniform[WU_tex_color], .8, .8, 1, .8); glDrawArrays(GL_TRIANGLES, 0, 6); @@ -149,16 +158,21 @@ void InventorySystem::render(void) auto& i = items[0]; i.item->sprite.use(); - glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, 0, tex); auto pos = PlayerSystem::getPosition(); vec2 sta (pos.x, pos.y); vec2 end (sta + (i.item->sprite.getDim() * game::HLINE)); - GLfloat coords[18] = { - sta.x, sta.y, -8, end.x, sta.y, -8, end.x, end.y, -8, - end.x, end.y, -8, sta.x, end.y, -8, sta.x, sta.y, -8 + GLfloat coords[] = { + sta.x, sta.y, -8, 0, 1, + end.x, sta.y, -8, 1, 1, + end.x, end.y, -8, 1, 0, + end.x, end.y, -8, 1, 0, + sta.x, end.y, -8, 0, 0, + sta.x, sta.y, -8, 0, 1, }; - glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, 0, coords); + + glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), coords); + glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), coords + 3); glDrawArrays(GL_TRIANGLES, 0, 6); } diff --git a/src/player.cpp b/src/player.cpp index 01b8a7c..0e43988 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -215,10 +215,21 @@ void PlayerSystem::receive(const KeyDownEvent &kde) vec2 PlayerSystem::getPosition(void) { - auto& loc = *game::entities.component<Position>(player.id()).get(); + Position loc; + if (player) + loc = *game::entities.component<Position>(player.id()).get(); + return vec2(loc.x, loc.y); } +float PlayerSystem::getWidth(void) +{ + float width = 0; + if (player) + width = game::entities.component<Solid>(player.id())->width; + return width; +} + void PlayerSystem::receive(const UseItemEvent& uie) { static std::atomic_bool cool (true); diff --git a/src/render.cpp b/src/render.cpp index 935e609..5da74c8 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -86,7 +86,7 @@ void init(void) + reinterpret_cast<const char *>(glewGetErrorString(glewError))); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); // anti-aliasing - SDL_GL_SetSwapInterval(1); // v-sync + SDL_GL_SetSwapInterval(game::vsync); // v-sync SDL_ShowCursor(SDL_DISABLE); // hide the cursor glViewport(0, 0, game::SCREEN_WIDTH, game::SCREEN_HEIGHT); // pixel coordinates glEnable(GL_BLEND); // alpha enabling @@ -193,9 +193,8 @@ void render(const int& fps) } UISystem::render(); - - //ui::drawFade(); ui::draw(); + FontSystem::render(); WindowSystem::render(); } @@ -377,21 +377,13 @@ namespace ui { if (pageTexReady) { - GLfloat page_loc[] = {offset.x - 300, SCREEN_HEIGHT - 100, -6.0, - offset.x + 300, SCREEN_HEIGHT - 100, -6.0, - offset.x + 300, SCREEN_HEIGHT - 600, -6.0, - - offset.x + 300, SCREEN_HEIGHT - 600, -6.0, - offset.x - 300, SCREEN_HEIGHT - 600, -6.0, - offset.x - 300, SCREEN_HEIGHT - 100, -6.0}; - - static const GLfloat page_tex[] = { - 0.0, 0.0, - 1.0, 0.0, - 1.0, 1.0, - 1.0, 1.0, - 0.0, 1.0, - 0.0, 0.0 + GLfloat page_verts[] = { + offset.x - 300, SCREEN_HEIGHT - 100, -6.0, 0, 0, + offset.x + 300, SCREEN_HEIGHT - 100, -6.0, 1, 0, + offset.x + 300, SCREEN_HEIGHT - 600, -6.0, 1, 1, + offset.x + 300, SCREEN_HEIGHT - 600, -6.0, 1, 1, + offset.x - 300, SCREEN_HEIGHT - 600, -6.0, 0, 1, + offset.x - 300, SCREEN_HEIGHT - 100, -6.0, 0, 0, }; glActiveTexture(GL_TEXTURE0); @@ -401,8 +393,8 @@ namespace ui { Render::textShader.use(); Render::textShader.enable(); - glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, 0, page_loc); - glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, 0, page_tex); + glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), page_verts); + glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), page_verts + 3); glDrawArrays(GL_TRIANGLES, 0 ,6); Render::textShader.disable(); @@ -938,20 +930,16 @@ void UISystem::render(void) fadeIntensity = 255; if (fadeIntensity != 0) { - static const GLfloat tex[12] = { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 - }; - vec2 p1 (offset.x - game::SCREEN_WIDTH / 2, offset.y - game::SCREEN_HEIGHT / 2); vec2 p2 (p1.x + game::SCREEN_WIDTH, p1.y + game::SCREEN_HEIGHT); - GLfloat backdrop[18] = { - p1.x, p1.y, -7.9, - p2.x, p1.y, -7.9, - p2.x, p2.y, -7.9, - - p2.x, p2.y, -7.9, - p1.x, p2.y, -7.9, - p1.x, p1.y, -7.9 + + GLfloat backdrop[] = { + p1.x, p1.y, -7.9, 0, 0, + p2.x, p1.y, -7.9, 0, 0, + p2.x, p2.y, -7.9, 0, 0, + p2.x, p2.y, -7.9, 0, 0, + p1.x, p2.y, -7.9, 0, 0, + p1.x, p1.y, -7.9, 0, 0, }; Render::textShader.use(); @@ -959,8 +947,8 @@ void UISystem::render(void) Colors::black.use(); glUniform4f(Render::textShader.uniform[WU_tex_color], 1.0f, 1.0f, 1.0f, fadeIntensity / 255.0f); - glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, 0, backdrop); - glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, 0, tex); + glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), backdrop); + glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), backdrop + 3); glDrawArrays(GL_TRIANGLES, 0, 6); Render::textShader.disable(); @@ -971,6 +959,7 @@ void UISystem::render(void) if (!dialogText.empty()) { vec2 where (offset.x - 300, game::SCREEN_HEIGHT - 60); ui::drawNiceBox(vec2(where.x - 10, where.y - 200), vec2(where.x + 620, where.y + 20), -5.5f); + FontSystem::setFontZ(-6.0f); putString(where, ui::typeOut(dialogText), where.x + 600); if (!dialogOptions.empty()) { diff --git a/src/world.cpp b/src/world.cpp index 73862ec..80c92fb 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -37,8 +37,7 @@ XMLDocument WorldSystem::xmlDoc; std::string WorldSystem::currentXMLFile; std::thread WorldSystem::thAmbient; std::vector<vec2> WorldSystem::stars; - -extern std::string xmlFolder; +std::string WorldSystem::toLoad; // wait static bool waitToSwap = false; @@ -98,6 +97,12 @@ constexpr const char* buildPaths[] = { ss >> mag >> ':'; }*/ +int WorldSystem::getLineIndex(float x) +{ + return std::clamp(static_cast<int>((x - world.startX) / game::HLINE), + 0, static_cast<int>(world.data.size())); +} + void WorldSystem::generate(int width) { float geninc = 0; @@ -146,10 +151,7 @@ void WorldSystem::generate(int width) float WorldSystem::isAboveGround(const vec2& p) { - int line = std::clamp(static_cast<int>((p.x - world.startX) / game::HLINE), - 0, static_cast<int>(world.data.size())); - - const auto& gh = world.data[line].groundHeight; + const auto& gh = world.data[getLineIndex(p.x)].groundHeight; return p.y >= gh ? 0 : gh; } @@ -160,7 +162,7 @@ bool WorldSystem::save(void) if (world.indoor) return false; - std::ofstream save (xmlFolder + currentXMLFile + ".dat"); + std::ofstream save (game::config::xmlFolder + currentXMLFile + ".dat"); // signature? save << "831998 "; @@ -210,10 +212,15 @@ void WorldSystem::fight(entityx::Entity entity) void WorldSystem::load(const std::string& file) { + toLoad = file; +} + +void WorldSystem::loader(void) +{ entityx::Entity entity; // check for empty file name - if (file.empty()) + if (toLoad.empty()) return; // save the current world's data @@ -221,7 +228,7 @@ void WorldSystem::load(const std::string& file) save(); // load file data to string - auto xmlPath = xmlFolder + file; + auto xmlPath = game::config::xmlFolder + toLoad; auto xmlRaw = readFile(xmlPath); // let tinyxml parse the file @@ -237,7 +244,7 @@ void WorldSystem::load(const std::string& file) if (file != nullptr) { DEBUG_printf("Including file: %s\n", file); - toAdd.emplace_back(xmlFolder + file); + toAdd.emplace_back(game::config::xmlFolder + file); //xmlRaw.append(readFile(xmlFolder + file)); } else { UserError("XML Error: <include> tag file not given"); @@ -269,7 +276,7 @@ void WorldSystem::load(const std::string& file) } world.toLeft = world.toRight = ""; - currentXMLFile = file; + currentXMLFile = toLoad; //game::entities.reset(); if (!savedEntities.empty()) { @@ -319,9 +326,9 @@ void WorldSystem::load(const std::string& file) UserError("<house> can only be used inside <IndoorWorld>"); //world.indoorWidth = wxml->FloatAttribute("width"); - world.indoorTex = RenderSystem::loadTexture(wxml->StrAttribute("texture")); // TODO winbloze lol + world.indoorTex = Texture(wxml->StrAttribute("texture")); // TODO winbloze lol auto str = wxml->StrAttribute("texture"); - auto tex = RenderSystem::loadTexture(str); + auto tex = Texture(str); world.indoorTex = tex; } @@ -411,7 +418,7 @@ void WorldSystem::load(const std::string& file) } // attempt to load data - std::ifstream save (xmlFolder + currentXMLFile + ".dat"); + std::ifstream save (game::config::xmlFolder + currentXMLFile + ".dat"); if (save.good()) { // check signature int signature; @@ -444,6 +451,7 @@ void WorldSystem::load(const std::string& file) save.close(); } + toLoad.clear(); game::events.emit<BGMToggleEvent>(); } @@ -591,23 +599,20 @@ void WorldSystem::render(void) // world width in pixels int width = HLINES(world.data.size()); - static bool ambientUpdaterStarted = false; - if (!ambientUpdaterStarted) { - ambientUpdaterStarted = true; + static std::once_flag init; + std::call_once(init, [&](void) { thAmbient = std::thread([&](void) { - const bool &run = game::engine.shouldRun; - while (run) { + while (game::engine.shouldRun) { float v = 75 * sin((game::time::getTickCount() + (DAY_CYCLE / 2)) / (DAY_CYCLE / PI)); float rg = std::clamp(.5f + (-v / 100.0f), 0.01f, .9f); float b = std::clamp(.5f + (-v / 80.0f), 0.03f, .9f); ambient = Color(rg, rg, b, 1.0f); - std::this_thread::sleep_for(1ms); } }); thAmbient.detach(); - } + }); // shade value for GLSL float shadeAmbient = std::max(0.0f, static_cast<float>(-worldShade) / 50 + 0.5f); // 0 to 1.5f @@ -619,24 +624,6 @@ void WorldSystem::render(void) //GLfloat bgOff = game::time::getTickCount() / static_cast<float>(DAY_CYCLE * 2); GLfloat bgOff = -0.5f * cos(PI / DAY_CYCLE * game::time::getTickCount()) + 0.5f; - GLfloat topS = .125f + bgOff; - GLfloat bottomS = bgOff; - - if (topS < 0.0f) - topS += 1.0f; - if (bottomS < 0.0f) - bottomS += 1.0f; - - GLfloat scrolling_tex_coord[] = { - 0.0f, bottomS, - 1.0f, bottomS, - 1.0f, bottomS, - - 1.0f, bottomS, - 0.0f, bottomS, - 0.0f, bottomS - }; - static const vec2 bg_tex_coord[] = { vec2(0.0f, 0.0f), vec2(1.0f, 0.0f), @@ -647,14 +634,17 @@ void WorldSystem::render(void) vec2(0.0f, 0.0f) }; - GLfloat back_tex_coord[] = { - offset.x - backgroundOffset.x - 5, offset.y - backgroundOffset.y, 9.9f, - offset.x + backgroundOffset.x + 5, offset.y - backgroundOffset.y, 9.9f, - offset.x + backgroundOffset.x + 5, offset.y + backgroundOffset.y, 9.9f, + GLfloat bottomS = bgOff; + if (bottomS < 0.0f) + bottomS += 1.0f; - offset.x + backgroundOffset.x + 5, offset.y + backgroundOffset.y, 9.9f, - offset.x - backgroundOffset.x - 5, offset.y + backgroundOffset.y, 9.9f, - offset.x - backgroundOffset.x - 5, offset.y - backgroundOffset.y, 9.9f + GLfloat farBack[] = { + offset.x - backgroundOffset.x - 5, offset.y - backgroundOffset.y, 9.9f, 0, bottomS, + offset.x + backgroundOffset.x + 5, offset.y - backgroundOffset.y, 9.9f, 1, bottomS, + offset.x + backgroundOffset.x + 5, offset.y + backgroundOffset.y, 9.9f, 1, bottomS, + offset.x + backgroundOffset.x + 5, offset.y + backgroundOffset.y, 9.9f, 1, bottomS, + offset.x - backgroundOffset.x - 5, offset.y + backgroundOffset.y, 9.9f, 0, bottomS, + offset.x - backgroundOffset.x - 5, offset.y - backgroundOffset.y, 9.9f, 0, bottomS }; // rendering!! @@ -671,17 +661,11 @@ void WorldSystem::render(void) bgTex(0); glUniform4f(Render::worldShader.uniform[WU_tex_color], 1.0, 1.0, 1.0, 1.0); - glVertexAttribPointer(Render::worldShader.coord, 3, GL_FLOAT, GL_FALSE, 0, back_tex_coord); - glVertexAttribPointer(Render::worldShader.tex , 2, GL_FLOAT, GL_FALSE, 0, scrolling_tex_coord); + glVertexAttribPointer(Render::worldShader.coord, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), farBack); + glVertexAttribPointer(Render::worldShader.tex , 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), farBack + 3); glDrawArrays(GL_TRIANGLES, 0, 6); - // no more night bg - //bgTex++; - //glUniform4f(Render::worldShader.uniform[WU_tex_color], 1.0, 1.0, 1.0, 1.3 - static_cast<float>(alpha) / 255.0f); - //makeWorldDrawingSimplerEvenThoughAndyDoesntThinkWeCanMakeItIntoFunctions(0, fron_tex_coord, tex_coord, 6); - if (worldShade > 0) { - static const Texture starTex ("assets/style/classic/bg/star.png"); // TODO why in theme, not just std.? static const float stardim = 24; @@ -949,49 +933,34 @@ void WorldSystem::render(void) static const float s = -(static_cast<float>(SCREEN_WIDTH) / 2.0f); // the ending pixel of the world static const float e = static_cast<float>(SCREEN_WIDTH) / 2.0f; - static const float sheight = static_cast<float>(SCREEN_HEIGHT); + auto yOffset = offset.y - static_cast<float>(SCREEN_HEIGHT) / 2.0f; + GLfloat blackBar[] = { + s, yOffset, -3.5f, 0.0f, 0.0f, + world.startX, yOffset, -3.5f, 1.0f, 0.0f, + world.startX, yOffset + sheight, -3.5f, 1.0f, 1.0f, + world.startX, yOffset + sheight, -3.5f, 1.0f, 1.0f, + s, yOffset + sheight, -3.5f, 0.0f, 1.0f, + s, yOffset, -3.5f, 0.0f, 0.0f + }; if (offset.x + world.startX > s) { Colors::black.use(); glUniform1f(Render::worldShader.uniform[WU_light_impact], 0.0f); - - auto off = offset.y - static_cast<float>(SCREEN_HEIGHT) / 2.0f; - - GLfloat blackBarLeft[] = { - s, 0.0f + off, -3.5f, 0.0f, 0.0f, - world.startX, 0.0f + off, -3.5f, 1.0f, 0.0f, - world.startX, sheight + off, -3.5f, 1.0f, 1.0f, - - world.startX, sheight + off, -3.5f, 1.0f, 1.0f, - s, sheight + off, -3.5f, 0.0f, 1.0f, - s, 0.0f + off, -3.5f, 0.0f, 0.0f - }; - - glVertexAttribPointer(Render::worldShader.coord, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 5, &blackBarLeft[0]); - glVertexAttribPointer(Render::worldShader.tex, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 5, &blackBarLeft[3]); + glVertexAttribPointer(Render::worldShader.coord, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 5, blackBar); + glVertexAttribPointer(Render::worldShader.tex, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 5, blackBar + 3); glDrawArrays(GL_TRIANGLES, 0, 6); } if (offset.x - world.startX < e) { + blackBar[0] = blackBar[20] = blackBar[25] = -world.startX; + blackBar[5] = blackBar[10] = blackBar[15] = e; + Colors::black.use(); glUniform1f(Render::worldShader.uniform[WU_light_impact], 0.0f); - - auto off = offset.y - static_cast<float>(SCREEN_HEIGHT) / 2.0f; - - GLfloat blackBarRight[] = { - -(world.startX), 0.0f + off, -3.5f, 0.0f, 0.0f, - e, 0.0f + off, -3.5f, 1.0f, 0.0f, - e, sheight + off, -3.5f, 1.0f, 1.0f, - - e, sheight + off, -3.5f, 1.0f, 1.0f, - -(world.startX), sheight + off, -3.5f, 0.0f, 1.0f, - -(world.startX), 0.0f + off, -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]); + glVertexAttribPointer(Render::worldShader.coord, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 5, blackBar); + glVertexAttribPointer(Render::worldShader.tex, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 5, blackBar + 3); glDrawArrays(GL_TRIANGLES, 0, 6); } @@ -1053,7 +1022,6 @@ void WorldSystem::detect(entityx::TimeDelta dt) } e.kill(); - //e.destroy(); } }); @@ -1061,13 +1029,10 @@ void WorldSystem::detect(entityx::TimeDelta dt) [&](entityx::Entity e, Grounded &g, Position &loc, Solid &dim) { (void)e; if (!g.grounded) { - // get the line the entity is on - int line = std::clamp(static_cast<int>((loc.x + dim.width / 2 - world.startX) / game::HLINE), - 0, static_cast<int>(world.data.size())); - // make sure entity is above ground - const auto& data = world.data; - if (loc.y != data[line].groundHeight) { - loc.y = data[line].groundHeight; + // make sure entity is above ground + auto height = world.data[getLineIndex(loc.x + dim.width / 2)].groundHeight; + if (loc.y != height) { + loc.y = height; e.remove<Grounded>(); } } @@ -1078,35 +1043,29 @@ void WorldSystem::detect(entityx::TimeDelta dt) (void)e; // handle gravity if (vel.y > -2.0f) - vel.y -= (GRAVITY_CONSTANT * phys.g) * dt; + vel.y -= GRAVITY_CONSTANT * phys.g * dt; }); game::entities.each<Position, Direction, Solid>( [&](entityx::Entity e, Position &loc, Direction &vel, Solid &dim) { (void)e; - //if (health.health <= 0) - // UserError("die mofo"); - // get the line the entity is on - int line = std::clamp(static_cast<int>((loc.x + dim.width / 2 - world.startX) / game::HLINE), - 0, static_cast<int>(world.data.size())); + auto line = getLineIndex(loc.x + dim.width / 2); // make sure entity is above ground - const auto& data = world.data; - if (loc.y < data[line].groundHeight) { + if (loc.y < world.data[line].groundHeight) { int dir = vel.x < 0 ? -1 : 1; - auto thing = line + dir * 2; - if (thing > 0 && thing < static_cast<int>(data.size()) && - data[line + dir * 2].groundHeight - 30 > data[line + dir].groundHeight) { + auto near = std::clamp(line + dir * 2, 0, static_cast<int>(world.data.size())); + if (world.data[near].groundHeight - 30 > world.data[line + dir].groundHeight) { loc.x -= (PLAYER_SPEED_CONSTANT + 2.7f) * dir * 2; vel.x = 0; } else { - loc.y = data[line].groundHeight - 0.001f * dt; + loc.y = world.data[line].groundHeight - 0.001f * dt; vel.y = 0; if (!vel.grounded) { vel.grounded = true; ParticleSystem::addMultiple(20, ParticleType::SmallPoof, - [&](){ return vec2(loc.x + randGet() % static_cast<int>(dim.width), loc.y);}, 500, 30); + [&](){ return vec2(loc.x + randGet() % static_cast<int>(dim.width), loc.y); }, 500, 30); } } } @@ -1116,9 +1075,9 @@ void WorldSystem::detect(entityx::TimeDelta dt) if (loc.x < world.startX) { vel.x = 0; loc.x = world.startX + HLINES(0.5f); - } else if (loc.x + dim.width + game::HLINE > -(static_cast<int>(world.startX))) { + } else if (loc.x + dim.width + game::HLINE > -static_cast<int>(world.startX)) { vel.x = 0; - loc.x = -(static_cast<int>(world.startX)) - dim.width - game::HLINE; + loc.x = -static_cast<int>(world.startX) - dim.width - game::HLINE; } }); } |