Skip to content

Commit

Permalink
Merge pull request #36 from zesinger/background_images
Browse files Browse the repository at this point in the history
Background images
  • Loading branch information
zesinger committed Jun 11, 2023
2 parents 0b561e7 + 79a8ea6 commit 828c087
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 10 deletions.
58 changes: 50 additions & 8 deletions src/serum-decode.cpp
Expand Up @@ -48,6 +48,7 @@ UINT16 nframes;
UINT32 nocolors, nccolors;
UINT32 ncompmasks, nmovmasks;
UINT32 nsprites;
UINT16 nbackgrounds;
// data
UINT32* hashcodes = NULL;
UINT8* shapecompmode = NULL;
Expand All @@ -69,6 +70,9 @@ UINT32* spritedetdwords = NULL;
UINT16* spritedetdwordpos = NULL;
UINT32* triggerIDs = NULL;
UINT16* framespriteBB = NULL;
UINT8* backgroundframes = NULL;
UINT16* backgroundIDs = NULL;
UINT16* backgroundBB = NULL;

// variables
bool cromloaded = false; // is there a crom loaded?
Expand Down Expand Up @@ -219,6 +223,21 @@ void Serum_free(void)
free(triggerIDs);
triggerIDs = NULL;
}
if (backgroundframes)
{
free(backgroundframes);
backgroundframes = NULL;
}
if (backgroundIDs)
{
free(backgroundIDs);
backgroundIDs = NULL;
}
if (backgroundBB)
{
free(backgroundBB);
backgroundBB = NULL;
}
cromloaded = false;
}

Expand Down Expand Up @@ -386,6 +405,7 @@ SERUM_API(bool) Serum_LoadFile(const char* const filename, int* pwidth, int* phe
fread(&ncompmasks, 4, 1, pfile);
fread(&nmovmasks, 4, 1, pfile);
fread(&nsprites, 4, 1, pfile);
if (sizeheader >= 13 * sizeof(UINT)) fread(&nbackgrounds, 2, 1, pfile); else nbackgrounds = 0;
// allocate memory for the serum format
hashcodes = (UINT32*)malloc(sizeof(UINT32) * nframes);
shapecompmode = (UINT8*)malloc(nframes);
Expand All @@ -407,10 +427,14 @@ SERUM_API(bool) Serum_LoadFile(const char* const filename, int* pwidth, int* phe
spritedetdwordpos = (UINT16*)malloc(nsprites * sizeof(UINT16) * MAX_SPRITE_DETECT_AREAS);
spritedetareas = (UINT16*)malloc(nsprites * sizeof(UINT16) * MAX_SPRITE_DETECT_AREAS * 4);
triggerIDs = (UINT32*)malloc(nframes * sizeof(UINT32));
backgroundframes = (UINT8*)malloc(nbackgrounds * fwidth * fheight);
backgroundIDs = (UINT16*)malloc(nframes * sizeof(UINT16));
backgroundBB = (UINT16*)malloc(nframes * 4 * sizeof(UINT16));
if (!hashcodes || !shapecompmode || !compmaskID || !movrctID || !cpal || !cframes || !dynamasks || !dyna4cols || !framesprites || !framespriteBB || !activeframes || !colorrotations || !triggerIDs ||
(!compmasks && ncompmasks > 0) ||
(!movrcts && nmovmasks > 0) ||
((nsprites > 0) && (!spritedescriptionso || !spritedescriptionsc || !spritedetdwords || !spritedetdwordpos || !spritedetareas)))
((nsprites > 0) && (!spritedescriptionso || !spritedescriptionsc || !spritedetdwords || !spritedetdwordpos || !spritedetareas)) ||
((nbackgrounds > 0) && (!backgroundframes || !backgroundIDs || !backgroundBB)))
{
Serum_free();
fclose(pfile);
Expand Down Expand Up @@ -455,6 +479,13 @@ SERUM_API(bool) Serum_LoadFile(const char* const filename, int* pwidth, int* phe
}
}
}
if (sizeheader >= 13 * sizeof(UINT))
{
fread(backgroundframes, fwidth * fheight, nbackgrounds, pfile);
fread(backgroundIDs, sizeof(UINT16), nframes, pfile);
fread(backgroundBB, 4 * sizeof(UINT16), nframes, pfile);
}
else memset(backgroundIDs, 0xFF, nframes * sizeof(UINT16));
fclose(pfile);
if (pntriggers)
{
Expand Down Expand Up @@ -688,15 +719,26 @@ bool Check_Sprites(UINT8* Frame, int quelleframe, UINT8* pquelsprites, UINT8* ns

void Colorize_Frame(UINT8* frame, int IDfound)
{
UINT16 ti;
UINT16 tj,ti;
// Generate the colorized version of a frame once identified in the crom frames
for (ti = 0; ti < fwidth * fheight; ti++)
for (tj = 0; tj < fheight; tj++)
{
UINT8 dynacouche = dynamasks[IDfound * fwidth * fheight + ti];
if (dynacouche == 255)
frame[ti] = cframes[IDfound * fwidth * fheight + ti];
else
frame[ti] = dyna4cols[IDfound * MAX_DYNA_4COLS_PER_FRAME * nocolors + dynacouche * nocolors + frame[ti]];
for (ti = 0; ti < fwidth; ti++)
{
UINT16 tk = tj * fwidth + ti;

if ((backgroundIDs[IDfound] < nbackgrounds) && (frame[tk] == 0) && (ti >= backgroundBB[IDfound * 4]) &&
(tj >= backgroundBB[IDfound * 4 + 1]) && (ti <= backgroundBB[IDfound * 4 + 2]) && (tj <= backgroundBB[IDfound * 4 + 3]))
frame[tk] = backgroundframes[backgroundIDs[IDfound] * fwidth * fheight + tk];
else
{
UINT8 dynacouche = dynamasks[IDfound * fwidth * fheight + tk];
if (dynacouche == 255)
frame[tk] = cframes[IDfound * fwidth * fheight + tk];
else
frame[tk] = dyna4cols[IDfound * MAX_DYNA_4COLS_PER_FRAME * nocolors + dynacouche * nocolors + frame[tk]];
}
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/serum-decode.h
Expand Up @@ -23,6 +23,7 @@ const int MAX_SPRITE_DETECT_AREAS = 4; // maximum number of areas to detect the
const int PALETTE_SIZE = 64 * 3; // size of a palette
const int ROTATION_SIZE = 3 * MAX_COLOR_ROTATIONS; // size of a color rotation block
const int MAX_SPRITE_TO_DETECT = 12; // max number of sprites detected in a frame
const int MAX_BACKGROUND_IMAGES = 255; // max number of background images

SERUM_API(bool) Serum_LoadFile(const char* const filename, int* pwidth, int* pheight, unsigned int* pnocolors, unsigned int* pntriggers);
SERUM_API(bool) Serum_Load(const char* const altcolorpath, const char* const romname, int* pwidth, int* pheight, unsigned int* pnocolors, unsigned int* pntriggers);
Expand Down
4 changes: 2 additions & 2 deletions src/serum-version.h
@@ -1,8 +1,8 @@
#pragma once

#define SERUM_VERSION_MAJOR 1 // X Digits
#define SERUM_VERSION_MINOR 5 // Max 2 Digits
#define SERUM_VERSION_PATCH 1 // Max 2 Digits
#define SERUM_VERSION_MINOR 6 // Max 2 Digits
#define SERUM_VERSION_PATCH 0 // Max 2 Digits

#define _SERUM_STR(x) #x
#define SERUM_STR(x) _SERUM_STR(x)
Expand Down

0 comments on commit 828c087

Please sign in to comment.