]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
who knows
authorClyne Sullivan <tullivan99@gmail.com>
Thu, 12 Jan 2017 02:20:57 +0000 (21:20 -0500)
committerClyne Sullivan <tullivan99@gmail.com>
Thu, 12 Jan 2017 02:20:57 +0000 (21:20 -0500)
include/common.hpp
include/components.hpp
main.cpp
src/common.cpp
src/components.cpp
src/world.cpp

index 1178540ba0221760ab2921b106c4fd2613805bc6..12231dcf48f2d44595b4284ab9e675c2388d7fb6 100644 (file)
@@ -54,8 +54,6 @@ using uint = unsigned int;
  */
 #define DEBUG_printf(message, ...) DEBUG_prints(__FILE__, __LINE__, message, __VA_ARGS__)
 
-#define BREAKPOINT __asm__("int $3")
-
 #define coalesce(v1, v2) ((v1 != nullptr) ? v1 : v2)
 
 /**
@@ -193,12 +191,12 @@ public:
 /**
  * The amount of game ticks that should occur each second.
  */
-constexpr const unsigned int TICKS_PER_SEC = 20;
+constexpr unsigned int TICKS_PER_SEC = 20;
 
 /**
  * The amount of milliseconds it takes for a game tick to fire.
  */
-constexpr const float MSEC_PER_TICK = 1000.0f / TICKS_PER_SEC;
+constexpr float MSEC_PER_TICK = 1000.0f / TICKS_PER_SEC;
 
 /**
  * Separates a string into tokens using the given delimiter.
@@ -217,16 +215,6 @@ std::vector<std::string> StringTokenizer(const std::string& str, char delim);
  */
 vec2 str2coord(std::string s);
 
-/**
- * A function to draw a colored box for OpenGL.
- * To use it, the lower left hand and upper right hand coords are given.
- *
- * @param the lower left coordinate
- * @param the upper right coordinate
- * @param the z coordinate
- */
-void drawRect(vec2 ll, vec2 ur, float z);
-
 /**
  * Returns a measurement in HLINEs
  *
@@ -252,7 +240,7 @@ inline T HLINES(const T &n)
 #define randGet rand
 
 // defines pi for calculations that need it.
-constexpr const float PI = 3.1415926535f;
+constexpr float PI = 3.1415926535f;
 
 // references the variable in main.cpp, used for drawing with the player
 extern vec2 offset;
@@ -266,10 +254,7 @@ void DEBUG_prints(const char* file, int line, const char *s,...);
 unsigned int millis(void);
 
 // reads the names of files in a directory into the given string vector
-int getdir(std::string dir, std::vector<std::string> &files);
-
-// sorts a vector of strings alphabetically
-void strVectorSortAlpha(std::vector<std::string> *v);
+int getdir(std::string dir, std::list<std::string>& files);
 
 // reads the given file into a buffer and returns a pointer to the buffer
 std::string readFile(const std::string& path);
index 805e8b2f390a1e3e244bae98675826fae701692b..00ac8fad1b8553383df853c8f2c0e6783937a3fb 100644 (file)
@@ -335,9 +335,20 @@ class RenderSystem : public entityx::System<RenderSystem> {
 private:
        std::string loadTexString;
        Texture loadTexResult;
+
+       float fadeIntensity;
+       bool fadeIn;
 public:
+       RenderSystem(void)
+               : fadeIntensity(1), fadeIn(true) {}
+
        Texture loadTexture(const std::string& file);
        void update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) override;
+
+       void fade(void);
+       void fadeLock(void);
+       void unfade(void);
+       void unfadeLock(void);
 };
 
 class DialogSystem : public entityx::System<DialogSystem>, public entityx::Receiver<DialogSystem> {
index 66b729a0f4018b153eda532b6a885c304ccab574..712c44afdb101eeacda1820140cc2ffeb171a7d5 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -142,13 +142,10 @@ int main(int argc, char *argv[])
                xmlFolder = "xml/";
 
        // read in all XML file names in the folder
-       std::vector<std::string> xmlFiles;
+       std::list<std::string> xmlFiles;
        if (getdir(std::string("./" + xmlFolder).c_str(), xmlFiles))
                UserError("Error reading XML files!!!");
 
-       // alphabetically sort files
-       strVectorSortAlpha(&xmlFiles);
-
        // kill the world if needed
        if (worldReset) {
                // TODO TODO TODO we do xml/*.dat now...
index 6539a05f569a3e76bebe4201d64d0e40664b489d..2ec80a7d20f9626ebc0749eed0ccc23e49d99eaa 100644 (file)
 #endif // __WIN32__
 
 unsigned int millis(void) {
-       std::chrono::system_clock::time_point now=std::chrono::system_clock::now();
-       return std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
+       using namespace std::chrono;
+
+       auto now = system_clock::now();
+       return duration_cast<milliseconds>(now.time_since_epoch()).count();
 }
 
 
@@ -37,7 +39,7 @@ std::vector<std::string> StringTokenizer(const std::string& str, char delim)
        std::string token;
 
        while (getline(is, token, delim))
-               tokens.push_back(token);
+               tokens.emplace_back(token);
 
        return tokens;
 }
@@ -51,56 +53,41 @@ void DEBUG_prints(const char* file, int line, const char *s,...)
        va_end(args);
 }
 
-int getdir(std::string dir, std::vector<std::string> &files)
+int getdir(std::string dir, std::list<std::string>& files)
 {
 #ifndef __WIN32__
-    DIR *dp;
-    struct dirent *dirp;
-    if (!(dp = opendir(dir.c_str()))) {
-        std::cout <<"Error ("<<errno<<") opening "<<dir<<std::endl;
-        return errno;
-    }
-    while((dirp = readdir(dp)))
-        files.push_back(std::string(dirp->d_name));
-    closedir(dp);
-#else
-       HANDLE dirh;
-       WIN32_FIND_DATA file_data;
+       auto dp = opendir(dir.c_str());
+       if (dp == nullptr)
+               UserError("Couldn\'t open folder: " + dir);
 
-       if ((dirh = FindFirstFile((dir + "/*").c_str(), &file_data)) == INVALID_HANDLE_VALUE)
-        return -1; /* No files found */
+       auto dirp = readdir(dp);
+       while (dirp != nullptr) {
+               files.emplace_back(dirp->d_name);
+               dirp = readdir(dp);
+       }
 
-    do {
-        const std::string file_name = file_data.cFileName;
-        const std::string full_file_name = dir + file_name;
-        const bool is_directory = (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
+    closedir(dp);
+#else
+       WIN32_FIND_DATA fileData;
+       auto dirh = FindFirstFile((dir + "/*").c_str(), &fileData);
+       if (dirh == INVALID_HANDLE_VALUE)
+               UserError("Couldn\'t open folder: " + dir);
 
-        if (file_name[0] == '.')
-            continue;
+       do {
+               auto fileName = fileData.cFileName;
 
-        if (is_directory)
-            continue;
+               if (fileName[0] == '.')
+                       continue;
 
-        files.push_back(file_name);
-    } while (FindNextFile(dirh, &file_data));
+               if (!(fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
+                       files.emplace_back(fileName);
+    } while (FindNextFile(dirh, &fileData));
 
     FindClose(dirh);
 #endif // __WIN32__
-       return 0;
-}
 
-void strVectorSortAlpha(std::vector<std::string> *v)
-{
-       static bool change;
-       do {
-               change = false;
-               for (unsigned int i=0; i < v->size() - 1; i++) {
-                       if (v[0][i] > v[0][i + 1]) {
-                               std::swap(v[0][i], v[0][i + 1]);
-                               change = true;
-                       }
-               }
-       } while (change);
+       files.sort();
+       return 0;
 }
 
 std::string readFile(const std::string& path)
index a7d504d17a5412a01ebc16afdb8984a50defadee..4d80d258f836a5577b39076f0d74ba5ceff388cf 100644 (file)
@@ -12,6 +12,8 @@
 
 #include <atomic>
 
+using namespace std::literals::chrono_literals;
+
 static std::vector<std::string> randomDialog (readFileA("assets/dialog_en-us"));
 
 void MovementSystem::update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt)
@@ -66,10 +68,34 @@ Texture RenderSystem::loadTexture(const std::string& file)
        loadTexString = file;
        loadTexResult = Texture();
        while (loadTexResult.isEmpty())
-               std::this_thread::sleep_for(std::chrono::milliseconds(1));
+               std::this_thread::sleep_for(1ms);
        return loadTexResult;
 }
 
+void RenderSystem::fade(void)
+{
+       fadeIn = false, fadeIntensity = 0;
+}
+
+void RenderSystem::fadeLock(void)
+{
+       fade();
+       while (fadeIntensity < 1)
+               std::this_thread::sleep_for(1ms);
+}
+
+void RenderSystem::unfade(void)
+{
+       fadeIn = true, fadeIntensity = 1;
+}
+
+void RenderSystem::unfadeLock(void)
+{
+       fade();
+       while (fadeIntensity > 0)
+               std::this_thread::sleep_for(1ms);
+}
+
 void RenderSystem::update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt)
 {
        (void)ev;
@@ -79,7 +105,14 @@ void RenderSystem::update(entityx::EntityManager &en, entityx::EventManager &ev,
                loadTexString.clear();
        }
 
+       // update fade system
+       if (fadeIn && fadeIntensity > 0)
+               fadeIntensity -= 0.01f;
+       else if(!fadeIn && fadeIntensity < 1)
+               fadeIntensity += 0.01f;
+       
        Render::worldShader.use();
+       Render::worldShader.enable();
 
        en.each<Visible, Sprite, Position>([dt](entityx::Entity entity, Visible &visible, Sprite &sprite, Position &pos) {
                // Verticies and shit
@@ -131,7 +164,6 @@ void RenderSystem::update(entityx::EntityManager &en, entityx::EventManager &ev,
                        sp.tex.use();
 
                        glUniform1i(Render::worldShader.uniform[WU_texture], 0);
-                       Render::worldShader.enable();
 
                        glVertexAttribPointer(Render::worldShader.coord, 3, GL_FLOAT, GL_FALSE, 0, coords);
                        glVertexAttribPointer(Render::worldShader.tex, 2, GL_FLOAT, GL_FALSE, 0 ,tex_coord);
@@ -153,6 +185,32 @@ void RenderSystem::update(entityx::EntityManager &en, entityx::EventManager &ev,
                ui::setFontZ(-5.0);
                ui::putStringCentered(pos.x + dim.width / 2, pos.y - ui::fontSize - HLINES(0.5), name.name);
        });
+
+       // draw fade
+       static const GLfloat tex[8] = {
+               0, 0, 0, 0, 0, 0, 0, 0
+       };
+
+       auto SCREEN_WIDTH2 = game::SCREEN_WIDTH / 2, SCREEN_HEIGHT2 = game::SCREEN_HEIGHT / 2;
+       GLfloat coord[12] = {
+               offset.x - SCREEN_WIDTH2 - 1, offset.y - SCREEN_HEIGHT2, -7.9,
+               offset.x + SCREEN_WIDTH2,     offset.y - SCREEN_HEIGHT2, -7.9,
+               offset.x - SCREEN_WIDTH2 - 1, offset.y + SCREEN_HEIGHT2, -7.9,
+               offset.x + SCREEN_WIDTH2,     offset.y + SCREEN_HEIGHT2, -7.9
+       };
+
+       Render::textShader.use();
+       Render::textShader.enable();
+
+       Colors::black.use();
+       glUniform4f(Render::textShader.uniform[WU_tex_color], 1.0f, 1.0f, 1.0f, fadeIntensity);
+       glUniform1i(Render::textShader.uniform[WU_texture], 0);
+       glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, 0, coord);
+       glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, 0, tex);
+       glDrawArrays(GL_QUADS, 0, 4);
+
+       Render::textShader.disable();
+       Render::textShader.unuse();
 }
 
 void DialogSystem::configure(entityx::EventManager &ev)
index a1d2f3cd707a45cc91a5c3f0c4be25710f7c0fa1..a80fba8b47c896aa7d312869f909a1ec828ce17e 100644 (file)
@@ -1178,13 +1178,13 @@ void WorldSystem::detect(entityx::TimeDelta dt)
 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& rs = *game::engine.getSystem<RenderSystem>();
+               rs.fadeLock();
                while (waitToSwap)
                        std::this_thread::sleep_for(1ms);
                load(world.toRight);
                game::engine.getSystem<PlayerSystem>()->setX(world.startX + HLINES(10));
-               ui::toggleBlack();
+               rs.unfade();
        }
 }