소스 검색

PIAF : File restructuration. SFML: WIP texture laod from PIAF File

Eiyeron Fulmincendii 10 년 전
부모
커밋
8815ef3d48
10개의 변경된 파일66개의 추가작업 그리고 63개의 파일을 삭제
  1. BIN
      data/out.wrf
  2. 2 0
      platform/include/Texture.h
  3. 12 6
      platform/sfml/Texture.cpp
  4. 4 4
      src/engine/StateMachine.cpp
  5. 5 1
      src/engine/main.cpp
  6. 4 3
      src/map/Map.cpp
  7. 3 1
      src/map/Map.h
  8. 24 35
      src/piaf/Archive.cpp
  9. 11 13
      src/piaf/Archive.h
  10. 1 0
      src/render/Text.cpp

BIN
data/out.wrf


+ 2 - 0
platform/include/Texture.h

@@ -9,6 +9,7 @@
 #include "utility/Rect.h"
 #include "platform.h"
 #include "render/Pixel.h"
+#include "piaf/Archive.h"
 //#include "PLATFORM/texture_type.h"
 
 namespace WalrusRPG
@@ -22,6 +23,7 @@ namespace WalrusRPG
           public:
             texture_data_t data;
 
+            Texture(WalrusRPG::PIAF::File& entry);
             Texture(char *data);
             ~Texture();
             // Getters

+ 12 - 6
platform/sfml/Texture.cpp

@@ -10,14 +10,10 @@
 
 using WalrusRPG::Graphics::Pixel;
 
-TEXTURE::Texture(char *data) : data()
+TEXTURE::Texture(char *data)
+:data()
 {
-    // UNUSED(data);
     uint16_t *data_16 = (uint16_t *) data;
-    // TOOD : load from PIAF
-    // this->data.loadFromFile("art/overworld.png");
-    // this->data.loadFromMemory(data+6, data_16[0]*data_16[1], sf::IntRect(0, 0,
-    // data_16[0], data_16[1]));
     this->data.create(data_16[0], data_16[1]);
     sf::Uint8 *pixels = new sf::Uint8[data_16[0] * data_16[1] * 4];
     for (unsigned y = 0; y < data_16[1]; y++)
@@ -35,6 +31,16 @@ TEXTURE::Texture(char *data) : data()
     this->data.update(pixels);
 
     delete[] pixels;
+
+}
+
+TEXTURE::Texture(WalrusRPG::PIAF::File& entry)
+:data()
+{
+    // UNUSED(data);
+    // TOOD : load from PIAF
+    // this->data.loadFromFile("art/overworld.png");
+    this->data.loadFromMemory(entry.get(), entry.file_size);
 }
 
 TEXTURE::~Texture()

+ 4 - 4
src/engine/StateMachine.cpp

@@ -2,7 +2,7 @@
 #include "Timing.h"
 #include "platform.h"
 #include "Graphics.h"
-#include "render/Text.h"
+// #include "render/Text.h"
 #include "version.h"
 #include "Input.h"
 
@@ -58,11 +58,11 @@ void STATEMACHINE::run()
             stack.back()->render(100 * frame_time / TIMER_FREQ);
             last_frame = frame_stamp;
 
-            Text::print_format(0, 0, "WalrusRPG test build %s", git_version);
+            // Text::print_format(0, 0,0 "WalrusRPG test build %s", git_version);
             if (frame_time != 0 && update_time != 0)
             {
-                Text::print_format(0, 240 - 8, "%ufps, %uups", TIMER_FREQ / frame_time,
-                                   TIMER_FREQ / update_time);
+                // Text::print_format(0, 240 - 8, "%ufps, %uups", TIMER_FREQ / frame_time,
+                //                    TIMER_FREQ / update_time);
             }
             Graphics::frame_end();
         }

+ 5 - 1
src/engine/main.cpp

@@ -9,6 +9,7 @@
 
 using namespace WalrusRPG;
 using WalrusRPG::PIAF::Archive;
+using WalrusRPG::Graphics::Texture;
 
 int main(int argc, char *argv[])
 {
@@ -19,6 +20,8 @@ int main(int argc, char *argv[])
     Quirks::init(argv[0]);
 
     // Archive arc(Quirks::solve_absolute_path("samples/one_file.wrf"));
+    Archive arc(Quirks::solve_absolute_path("data/out.wrf").get());
+    Texture tex(arc.get("ov.png"));
 
     uint16_t dungeonTest[] = {
         21, 21,  1,   1,   1,   1,   21,  22,  21,  22, 21,  22,  21,  21,  1,   22,  21,
@@ -69,7 +72,7 @@ int main(int argc, char *argv[])
         0, 0, 0, 0, 0,   0,   0,   0,   0,   0,  0,  0,  0,  0,  0,   0,   0,   0,   0, 0,
     };
 
-    Map map(20, 20, dungeonTest, dungeonTest2);
+    Map map(20, 20, dungeonTest, dungeonTest2, tex);
     tinystl::vector<Frame> stripe21;
     tinystl::vector<Frame> stripe22;
     stripe21.push_back({21, 23});
@@ -86,4 +89,5 @@ int main(int argc, char *argv[])
     Timing::deinit();
     Graphics::deinit();
     return 0;
+
 }

+ 4 - 3
src/map/Map.cpp

@@ -8,12 +8,13 @@
 #define MAP WalrusRPG::Map
 using namespace WalrusRPG;
 using namespace WalrusRPG::Utils;
+using WalrusRPG::Graphics::Texture;
 
-Graphics::Texture tex_overworld((char *) overworld);
+// Graphics::Texture tex_overworld((char *) overworld);
 
-MAP::Map(int width, int height, uint16_t *layer0, uint16_t *layer1) : anim()
+MAP::Map(int width, int height, uint16_t *layer0, uint16_t *layer1, Texture& tex) : anim(), tex(tex)
 {
-    this->renderer = new TileRenderer(tex_overworld, 16, 16);
+    this->renderer = new TileRenderer(tex, 16, 16);
     this->width = width;
     this->height = height;
     this->layer0 = layer0;

+ 3 - 1
src/map/Map.h

@@ -2,6 +2,7 @@
 #define INCLUDE_MAP_H
 
 #include <stdint.h>
+#include "Texture.h"
 #include "render/Camera.h"
 #include "render/TileRenderer.h"
 #include "render/Animator.h"
@@ -20,10 +21,11 @@ namespace WalrusRPG
         int height;
         uint16_t *layer0;
         uint16_t *layer1;
+        WalrusRPG::Graphics::Texture tex;
         TileRenderer *renderer;
         // TODO?: add a boolean/getter to know if a second layer exist?
       public:
-        Map(int width, int height, uint16_t *layer0, uint16_t *layer1);
+        Map(int width, int height, uint16_t *layer0, uint16_t *layer1, WalrusRPG::Graphics::Texture& tex);
         ~Map();
         void render(Camera &camera, unsigned dt);
         void update(unsigned dt);

+ 24 - 35
src/piaf/Archive.cpp

@@ -8,7 +8,6 @@
 using tinystl::string;
 using WalrusRPG::PIAF::Archive;
 using WalrusRPG::PIAF::File;
-using WalrusRPG::PIAF::FileEntry;
 using WalrusRPG::PIAF::FileType;
 using WalrusRPG::PIAF::CompressionType;
 
@@ -37,7 +36,7 @@ namespace
      * array long enough and a given number of files to load.
      * The pointer must directly access the file entry region of the archive.
      */
-    void load_file_table(FileEntry *entries, char *data, uint32_t nb_files)
+    void load_file_table(File *entries, char *data, uint32_t nb_files)
     {
         for (unsigned index = 0; index < nb_files; index++)
         {
@@ -174,7 +173,7 @@ Archive::Archive(const char *filepath) : file(nullptr), entries(nullptr)
             // fprintf(stderr, "Bad filetable checksum\n");
         }
         // Create the filetable.
-        entries = new FileEntry[nb_files];
+        entries = new File[nb_files];
         // Parse and story the filetable.
         load_file_table(entries, file_entry_data, nb_files);
         delete[] file_entry_data;
@@ -189,60 +188,50 @@ Archive::~Archive()
         delete[] entries;
 }
 
-/**
- * Searches a fileentry through the filetable and returns it
- * /?\ How would the function react if it doesn't find the file?
- * Exceptions? Empty entry?
- */
-FileEntry Archive::get_file_entry(char *filename)
-{
-    for (uint32_t index = 0; index < nb_files; index++)
-    {
-        if (strcmp(entries[index].filename, filename) == 0)
-        {
-            return entries[index];
-        }
-    }
-    // throw exception
-}
-
 /**
  * Returns the stored file's data from giving a filename.
  */
-File Archive::get(char *filename)
+File& Archive::get(char *filename)
 {
     for (unsigned index = 0; index < nb_files; index++)
     {
         if (strncmp(filename, entries[index].filename, 8) == 0)
         {
-            uint8_t *data = new uint8_t[entries[index].file_size];
-            fseek(file, entries[index].data_offset + 32 + 24 * nb_files, SEEK_SET);
-            if (fread(data, sizeof(uint8_t), entries[index].file_size, file) !=
-                entries[index].file_size)
+            // On demand load
+            if(!entries[index].loaded)
             {
-                // throw loading error
+                uint8_t *data = new uint8_t[entries[index].file_size];
+                fseek(file, entries[index].data_offset + 32 + 24 * nb_files, SEEK_SET);
+                if (fread(data, sizeof(uint8_t), entries[index].file_size, file) !=
+                    entries[index].file_size)
+                {
+                    // throw loading error
+                }
+                else
+                {
+                    entries[index].data = data;
+                }
+                entries[index].loaded = true;
+
             }
-            return File(data, entries[index].file_size);
+            return entries[index];
         }
     }
     // throw not found exception
 }
 
-
-File::File(uint8_t *data, std::size_t size) : data(data), size(size)
+File::File() : data(nullptr), loaded(false)
 {
+
 }
 
+
 File::~File()
 {
     delete[] data;
 }
 
-uint8_t &File::operator[](std::size_t idx)
+uint8_t* File::get()
 {
-    if (idx >= size)
-    {
-        // throw up
-    }
-    return data[idx];
+    return data;
 }

+ 11 - 13
src/piaf/Archive.h

@@ -25,25 +25,23 @@ namespace WalrusRPG
             RLE
         };
 
-        struct FileEntry
+        class File
         {
+        protected:
+            uint8_t *data;
+            bool loaded;
+          public:
+            friend class Archive;
             char filename[9]; // 8 + a \0 in case of printing
             FileType file_type;
             CompressionType compression_type;
             uint32_t file_size;
             uint32_t data_offset;
-        };
-
-        class File
-        {
-          private:
-            uint8_t *data;
             std::size_t size;
 
-          public:
-            File(uint8_t *data, std::size_t size);
+            File();
             ~File();
-            uint8_t &operator[](std::size_t idx);
+            uint8_t* get();
         };
 
         class Archive
@@ -54,16 +52,16 @@ namespace WalrusRPG
             uint32_t nb_files;
             uint32_t data_size;
             // wouldn't be a map easier to handle for file opening?
-            FileEntry *entries;
+            File *entries;
 
           public:
             // RAII stuff
+            // Archive(std::unique_ptr<char> *filepath);
             Archive(const char *filepath);
             Archive(tinystl::string &filepath);
             ~Archive();
+            File& get(char *filename);
 
-            FileEntry get_file_entry(char *filename);
-            File get(char *filename);
         };
     }
 }

+ 1 - 0
src/render/Text.cpp

@@ -48,3 +48,4 @@ void TEXT::print_format(unsigned x, unsigned y, const std::string &format, ...)
 {
     TEXT::print_format(x, y, format.c_str());
 }
+