Skip to content

Commit

Permalink
GIF: Prerender mode - draw all frames once upon launch and store for …
Browse files Browse the repository at this point in the history
…later

Bump version number to 0.4.1
Some cleanup
  • Loading branch information
fuelsoft committed Sep 10, 2020
1 parent d6cd0de commit 35fd73b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 18 deletions.
2 changes: 1 addition & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace IVC {
const std::string BUILD_DATE = __DATE__;
const std::string BUILD_TIME = __TIME__;
std::string VERSION_ABOUT;
const std::string IVVERSION = "0.4.0";
const std::string IVVERSION = "0.4.1";

/* WINDOW MANAGEMENT */
const int WIN_DEFAULT_W = 1600;
Expand Down
8 changes: 4 additions & 4 deletions meta/meta.rc
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
1 VERSIONINFO
FILEVERSION 0,4,0,0
PRODUCTVERSION 0,4,0,0
FILEVERSION 0,4,1,0
PRODUCTVERSION 0,4,1,0
BEGIN
BLOCK "StringFileInfo"
BEGIN
BLOCK "040904E4"
BEGIN
VALUE "CompanyName", "Fuelsoft Development"
VALUE "FileDescription", "Image Viewer"
VALUE "FileVersion", "0.4.0"
VALUE "FileVersion", "0.4.1"
VALUE "InternalName", "viewer"
VALUE "LegalCopyright", "Fuelsoft Development"
VALUE "OriginalFilename", "Viewer.exe"
VALUE "ProductName", "Image Viewer"
VALUE "ProductVersion", "0.4.0"
VALUE "ProductVersion", "0.4.1"
END
END
BLOCK "VarFileInfo"
Expand Down
52 changes: 41 additions & 11 deletions subclasses/IVAnimatedImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ void IVAnimatedImage::animate() {
return;
}

void IVAnimatedImage::prerender() {
for (int i = 0; i < this->frame_count; i++) {
prepare(i);
this->frames.push_back(this->texture);
this->texture = nullptr; //remove reference so next call to prepare doesn't free it
}
return;
}

/* PUBLIC */

IVAnimatedImage::IVAnimatedImage(SDL_Renderer* renderer, std::filesystem::path path) {
Expand Down Expand Up @@ -152,7 +161,10 @@ IVAnimatedImage::IVAnimatedImage(SDL_Renderer* renderer, std::filesystem::path p

this->texture = SDL_CreateTextureFromSurface(this->renderer, this->surface);

if (this->animated) animationThread = std::thread(&IVAnimatedImage::animate, this);
if (this->animated) {
if (this->prerendered) prerender();
animationThread = std::thread(&IVAnimatedImage::animate, this);
}
}

IVAnimatedImage::~IVAnimatedImage() {
Expand All @@ -162,18 +174,23 @@ IVAnimatedImage::~IVAnimatedImage() {
}
DGifCloseFile(this->gif_data, nullptr);
SDL_FreeSurface(this->surface);
SDL_DestroyTexture(this->texture);
if (prerendered) {
for (uint32_t i = 0; i < this->frames.size(); i++) {
SDL_DestroyTexture(this->frames[i]);
}
}
else {
SDL_DestroyTexture(this->texture);
}
}

/**
* prepare - Create surface from current index, apply palette and create texture, keying as required.
* prepare - Create surface from specified index, apply palette and create texture, keying as required.
* This should be called as infrequently as possible - static images don't need refreshing.
*/
void IVAnimatedImage::prepare() {
int local_index = frame_index;

void IVAnimatedImage::prepare(uint16_t index) {
// shortened for simplicity
GifImageDesc* im_desc = &this->gif_data->SavedImages[local_index].ImageDesc;
GifImageDesc* im_desc = &this->gif_data->SavedImages[index].ImageDesc;

// destination for copy - if only a region is being updated, this will not cover the whole image
SDL_Rect dest;
Expand All @@ -182,19 +199,19 @@ void IVAnimatedImage::prepare() {
dest.w = im_desc->Width;
dest.h = im_desc->Height;

SDL_Surface* temp = SDL_CreateRGBSurfaceFrom((void *) this->gif_data->SavedImages[local_index].RasterBits, im_desc->Width, im_desc->Height, this->depth, im_desc->Width * (this->depth >> 3), 0, 0, 0, 0);
SDL_Surface* temp = SDL_CreateRGBSurfaceFrom((void *) this->gif_data->SavedImages[index].RasterBits, im_desc->Width, im_desc->Height, this->depth, im_desc->Width * (this->depth >> 3), 0, 0, 0, 0);

if (gif_data->SavedImages[local_index].ImageDesc.ColorMap) { // local colour palette
if (gif_data->SavedImages[index].ImageDesc.ColorMap) { // local colour palette
// convert from local giflib colour to SDL colour and populate palette
setPalette(gif_data->SavedImages[local_index].ImageDesc.ColorMap, temp);
setPalette(gif_data->SavedImages[index].ImageDesc.ColorMap, temp);
}
else if (gif_data->SColorMap) { // if global colour palette defined
// convert from global giflib colour to SDL colour and populate palette
setPalette(gif_data->SColorMap, temp);
}

//get gfx extension block to find transparent palette index
ExtensionBlock* gfx = getGraphicsBlock(local_index);
ExtensionBlock* gfx = getGraphicsBlock(index);

//if gfx block exists and transparency flag is set, set colour key
if (gfx && (gfx->Bytes[0] & 0x01)) {
Expand All @@ -212,6 +229,19 @@ void IVAnimatedImage::prepare() {
this->ready = false; // mark current frame as already requested
}

/**
* prepare - If in prerender mode, update index. Otherwise, prepare current index frame.
*/
void IVAnimatedImage::prepare() {
if (prerendered) {
this->texture = frames[frame_index];
this->ready = false;
}
else {
prepare(frame_index);
}
}

/**
* set_status - Set image state - play or pause (or toggle to swap)
* s > New IVImage::state state
Expand Down
14 changes: 12 additions & 2 deletions subclasses/IVAnimatedImage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ class IVAnimatedImage : public IVImage{
uint16_t frame_index = 0;
uint16_t delay_val = 0;

#ifdef MAKE_NO_PRERENDER
bool prerendered = false;
#else
bool prerendered = true;
#endif

std::vector<SDL_Texture*> frames;

bool play = true;
bool quit = false;

Expand All @@ -34,8 +42,6 @@ class IVAnimatedImage : public IVImage{

void setIndex(uint16_t index);

void next();

uint16_t getDelay(uint16_t index);

uint16_t getDelay();
Expand All @@ -44,6 +50,8 @@ class IVAnimatedImage : public IVImage{

void animate();

void prerender();

public:
uint16_t frame_count = 0;
bool playable;
Expand All @@ -54,6 +62,8 @@ class IVAnimatedImage : public IVImage{

~IVAnimatedImage();

void prepare(uint16_t index);

void prepare();

void set_status(IVImage::state s);
Expand Down

0 comments on commit 35fd73b

Please sign in to comment.