Skip to content

Commit

Permalink
Update GIF code from TWiLight (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
Epicpkmn11 authored Sep 12, 2021
1 parent 4e1296e commit b8c4381
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
19 changes: 12 additions & 7 deletions arm9/source/gif.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,20 @@ void Gif::displayFrame(void) {
}
}

bool Gif::load(bool top) {
bool Gif::load(const char *path, bool top, bool animate, bool forceDecompress) {
_top = top;

FILE *file = fopen((_top ? "sd:/hiya/splashtop.gif" : "sd:/hiya/splashbottom.gif"), "rb");
FILE *file = fopen(path, "rb");
if (!file)
return false;

fseek(file, 0, SEEK_END);
_compressed = ftell(file) > 1 << 20; // Decompress files bigger than 1MiB while drawing
fseek(file, 0, SEEK_SET);
if(forceDecompress) {
_compressed = false;
} else {
fseek(file, 0, SEEK_END);
_compressed = ftell(file) > (/*dsiFeatures()*/true ? 1 << 20 : 1 << 18); // Decompress files bigger than 1MiB (256KiB in DS Mode) while drawing
fseek(file, 0, SEEK_SET);
}

// Reserve space for 2,000 frames
_frames.reserve(2000);
Expand All @@ -108,7 +112,7 @@ bool Gif::load(bool top) {
fread(&header, 1, sizeof(header), file);

// Check that this is a GIF
if (memcmp(header.signature, "GIF89a", sizeof(header.signature)) != 0) {
if (memcmp(header.signature, "GIF87a", sizeof(header.signature)) != 0 && memcmp(header.signature, "GIF89a", sizeof(header.signature)) != 0) {
fclose(file);
return false;
}
Expand Down Expand Up @@ -220,7 +224,8 @@ bool Gif::load(bool top) {
_paused = false;
_finished = loopForever();
_frames.shrink_to_fit();
_animating.push_back(this);
if(animate)
_animating.push_back(this);

return true;
}
12 changes: 9 additions & 3 deletions arm9/source/gif.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <nds/ndstypes.h>
#include <vector>
#include <cstddef>

typedef unsigned int uint;

Expand Down Expand Up @@ -60,7 +61,6 @@ class Gif {
struct Image {
u8 lzwMinimumCodeSize;
std::vector<u8> imageData;
size_t dataSize;
} image;

std::vector<u16> lct; // In DS format
Expand All @@ -72,7 +72,7 @@ class Gif {

std::vector<Frame> _frames;
std::vector<u16> _gct; // In DS format
u16 _loopCount = 0;
u16 _loopCount = 0xFFFF;
bool _top = false;
bool _compressed = false;

Expand All @@ -95,9 +95,13 @@ class Gif {
static void timerHandler(void);

Gif() {}
Gif(const char *path, bool top, bool animate, bool forceDecompress) { load(path, top, animate, forceDecompress); }
~Gif() {}

bool load(bool top);
bool load(const char *path, bool top, bool animate, bool forceDecompress);

Frame &frame(int frame) { return _frames[frame]; }
std::vector<u16> gct() { return _gct; }

bool paused() { return _paused; }
void pause() { _paused = true; }
Expand All @@ -108,6 +112,8 @@ class Gif {
bool waitingForInput(void) { return _waitingForInput; }
void resume(void) { _waitingForInput = false; _currentDelayProgress = _currentDelay; }
bool finished(void) { return _finished; }

int currentFrame(void) { return _currentFrame; }
};

#endif
4 changes: 2 additions & 2 deletions arm9/source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,15 +354,15 @@ int main( int argc, char **argv) {
}

if (splash) {
if (gif[true].load(true)) {
if (gif[true].load("sd:/hiya/splashtop.gif", true, true, false)) {
splashFound[true] = true;
splashBmp[true] = false;
} else if (access("sd:/hiya/splashtop.bmp", F_OK) == 0) {
splashFound[true] = true;
splashBmp[true] = true;
}

if (gif[false].load(false)) {
if (gif[false].load("sd:/hiya/splashbottom.gif", false, true, false)) {
splashFound[false] = true;
splashBmp[false] = false;
} else if (access("sd:/hiya/splashtop.bmp", F_OK) == 0) {
Expand Down

0 comments on commit b8c4381

Please sign in to comment.