Explorar o código

Nspire : starting to deal with PIAF and PNG

Eiyeron Fulmincendii %!s(int64=10) %!d(string=hai) anos
pai
achega
96172df8bb
Modificáronse 5 ficheiros con 71 adicións e 15 borrados
  1. BIN=BIN
      data/out.wrf.tns
  2. 33 1
      platform/nspire/Texure.cpp
  3. 2 1
      platform/sfml/Texture.cpp
  4. 9 9
      src/engine/main.cpp
  5. 27 4
      src/piaf/Archive.cpp

BIN=BIN
data/out.wrf.tns


+ 33 - 1
platform/nspire/Texure.cpp

@@ -1,12 +1,16 @@
 #include "Texture.h"
+#include "piaf/Archive.h"
 #include "utility/Rect.h"
 #include "render/Pixel.h"
 #include "utility/misc.h"
-
+#include "lodepng.h"
+#include "Input.h"
+#include "Graphics.h"
 using WalrusRPG::Graphics::Black;
 using WalrusRPG::Graphics::Pixel;
 using WalrusRPG::Graphics::Texture;
 using WalrusRPG::Utils::Rect;
+using WalrusRPG::PIAF::File;
 
 namespace
 {
@@ -20,6 +24,34 @@ namespace
     */
 }
 
+Texture::Texture(File entry)
+{
+    unsigned char* pic;
+    unsigned width, height;
+
+    signed result = lodepng_decode_memory(&pic, &width, &height, (unsigned char*)entry.get(), entry.file_size, LCT_RGBA, 8);
+    UNUSED(result);
+    
+    data = new uint16_t[width * height + 3];
+    data[0] = width;
+    data[1] = height;
+    for(unsigned y = 0; y < height; y++)
+    {
+        for (unsigned x = 0; x < width; x++) {
+            uint16_t color = (pic[(y*width + x)]>>3)<<11;
+            color |= (pic[(y*width + x + 1)]>>2)<<5;
+            color |= (pic[(y*width + x + 2)]>>3);
+            if(pic[(y*width + x +3)] == 0)
+            {
+                data[2] = color;
+            }
+            data[y*width + x] = color;
+        }
+    }
+    delete[] pic;
+}
+
+
 Texture::Texture(char *data) : data((texture_data_t) data)
 {
 }

+ 2 - 1
platform/sfml/Texture.cpp

@@ -3,6 +3,7 @@
 #include <SFML/OpenGL.hpp>
 #include <SFML/Graphics/Rect.hpp>
 #include <cstdint>
+#include <cstdlib>
 #include "utility/misc.h"
 #include "render/Pixel.h"
 
@@ -30,7 +31,7 @@ TEXTURE::Texture(char *data)
     }
     this->data.update(pixels);
 
-    delete[] pixels;
+    free(pixels);
 
 }
 

+ 9 - 9
src/engine/main.cpp

@@ -6,6 +6,7 @@
 #include "map/StateMap.h"
 #include "piaf/Archive.h"
 #include "utility/misc.h"
+#include "sprites.h"
 
 using namespace WalrusRPG;
 using WalrusRPG::PIAF::Archive;
@@ -19,19 +20,18 @@ int main(int argc, char *argv[])
     Timing::init();
     Quirks::init(argv[0]);
 
-    // Archive arc(Quirks::solve_absolute_path("samples/one_file.wrf"));
-    Archive arc(Quirks::solve_absolute_path("data/out.wrf").get());
+    Archive arc("data/out.wrf.tns");
     Texture tex(arc.get("ov.png"));
-    if(arc.has("ov.png"))
-        printf("%s\n", arc.get("test.txt").get());
+    WalrusRPG::PIAF::File f1 = arc.get("l1.bin");
+    WalrusRPG::PIAF::File f2 = arc.get("l2.bin");
 
-    const uint8_t* l1 = arc.get("l1.bin").get();
-    const uint8_t* l2 = arc.get("l2.bin").get();
+    const uint8_t* l1 = f1.get();
+    const uint8_t* l2 = f2.get();
 
-    uint16_t* dungeonTest = new uint16_t[arc.get("l1.bin").file_size/2+1];
-    uint16_t* dungeonTest2 = new uint16_t[arc.get("l1.bin").file_size/2+1];
+    uint16_t* dungeonTest = new uint16_t[f1.file_size/2+1];
+    uint16_t* dungeonTest2 = new uint16_t[f1.file_size/2+1];
 
-    for(unsigned i = 0; i < arc.get("l1.bin").file_size; i+=2)
+    for(unsigned i = 0; i < f1.file_size; i+=2)
     {
         dungeonTest[i/2] = read_big_endian_value<uint16_t>(&l1[i]);
         dungeonTest2[i/2] = read_big_endian_value<uint16_t>(&l2[i]);

+ 27 - 4
src/piaf/Archive.cpp

@@ -2,6 +2,9 @@
 #include <cstdio>
 #include <memory>
 #include <zlib.h>
+#if NSPIRE
+#include "../../platform/nspire/Interrupts.h"
+#endif
 #include "Archive.h"
 #include "Quirks.h"
 #include "utility/misc.h"
@@ -13,6 +16,9 @@ using WalrusRPG::PIAF::File;
 using WalrusRPG::PIAF::FileType;
 using WalrusRPG::PIAF::CompressionType;
 
+#if NSPIRE
+using namespace Nspire;
+#endif
 namespace
 {
 
@@ -57,8 +63,12 @@ Archive::Archive(string &filepath) : Archive(filepath.c_str())
 {
 }
 
+
 Archive::Archive(const char *filepath) : file(nullptr), entries(nullptr), files_data(nullptr), files_loaded(nullptr)
 {
+#if NSPIRE
+    Interrupts::off();   
+#endif
     // Null pointer exception trigger
     if (filepath == nullptr)
     {
@@ -73,7 +83,7 @@ Archive::Archive(const char *filepath) : file(nullptr), entries(nullptr), files_
     // Open the archive
     file = fopen(real_filename.get(), "rb");
     // Again another null pointer trigger
-    if (file == nullptr)
+    if (file == nullptr || file == NULL)
     {
         // TODO : throw Couldn't open
         // fprintf(stderr, "Unable to open %s\n", filepath);
@@ -94,7 +104,7 @@ Archive::Archive(const char *filepath) : file(nullptr), entries(nullptr), files_
     // Tempoary buffer to contain the header.
     char header_container[32] = {0};
     // Read the headers and trigger exceptions on errors
-    if (fread(header_container, sizeof(char), 32, file))
+    if (fread(header_container, sizeof(char), 32, file) != 32)
     {
         // So we coudln't load the whole header.
         // TODO : check flags and return correct exceptions
@@ -177,10 +187,16 @@ Archive::Archive(const char *filepath) : file(nullptr), entries(nullptr), files_
 
 
     }
+#if NSPIRE
+    Interrupts::init();
+#endif
 }
 
 Archive::~Archive()
 {
+#if NSPIRE
+    Interrupts::off();
+#endif
     if (file != nullptr)
         fclose(file);
     if (entries != nullptr)
@@ -198,7 +214,9 @@ Archive::~Archive()
     }
     delete[] files_data;
     delete[] files_loaded;
-
+#if NSPIRE
+    Interrupts::init();
+#endif
 }
 
 bool Archive::has(const char *filename)
@@ -221,6 +239,9 @@ File Archive::get(const char *filename)
             // On demand load
             if(!files_loaded[index])
             {
+ #if NSPIRE
+               Interrupts::off();
+#endif
                 uint8_t *data = new uint8_t[entries[index].file_size];
                 fseek(file, files_data_offset[index] + 32 + 24 * nb_files, SEEK_SET);
                 if (fread(data, sizeof(uint8_t), entries[index].file_size, file) !=
@@ -234,7 +255,9 @@ File Archive::get(const char *filename)
                     entries[index].data = data;
                 }
                 files_loaded[index] = true;
-
+#if NSPIRE
+                Interrupts::init();
+#endif
             }
             return entries[index];
         }