Skip to content

Commit

Permalink
Readability improvements and added commenting to loadsprite2.
Browse files Browse the repository at this point in the history
  • Loading branch information
DCurrent committed Mar 19, 2018
1 parent 3957c97 commit 61ae161
Showing 1 changed file with 39 additions and 10 deletions.
49 changes: 39 additions & 10 deletions engine/openbor.c
Original file line number Diff line number Diff line change
Expand Up @@ -4641,36 +4641,65 @@ s_sprite *loadsprite2(char *filename, int *width, int *height)
size_t size;
s_bitmap *bitmap = NULL;
s_sprite *sprite = NULL;
int clipl, clipr, clipt, clipb;

int clip_left;
int clip_right;
int clip_top;
int clip_bottom;

// Load raw bitmap (image) file from pack. If this
// fails, then we return NULL.
bitmap = loadbitmap(filename, packfile, pixelformat);

if(!bitmap)
{
return NULL;
}
}

// Apply width and height adjustments, if any.
if(width)
{
*width = bitmap->width;
}

if(height)
{
*height = bitmap->height;
}
clipbitmap(bitmap, &clipl, &clipr, &clipt, &clipb);
size = fakey_encodesprite(bitmap);
}

// Trim empty pixels from the bitmap to save memory.
// We will pass the arguments by reference - they will
// be modified by the clipping function to tell us
// exactly how much trim work on each axis was done.
clipbitmap(bitmap, &clip_left, &clip_right, &clip_top, &clip_bottom);

// Get size of trimmed bitmap and allocate memory for
// use as a sprite. If this fails, then free the memory
// bitmap occupies, and return NULL.
size = fakey_encodesprite(bitmap);
sprite = (s_sprite *)malloc(size);

if(!sprite)
{
freebitmap(bitmap);
return NULL;
}
encodesprite(-clipl, -clipt, bitmap, sprite);
sprite->offsetx = clipl;
sprite->offsety = clipt;

// Transpose bitmap to a sprite, using the memory
// we allocated for it above. The trim arguments
// from our trimming function will be used as an
// offset. We'll also store/ the bitmap's trimmed
// dimensions for later use.
encodesprite(-clip_left, -clip_top, bitmap, sprite);
sprite->offsetx = clip_left;
sprite->offsety = clip_top;
sprite->srcwidth = bitmap->width;
sprite->srcheight = bitmap->height;

// Delete the raw bitmap, we don't need it
// any more.
freebitmap(bitmap);


// Return encoded sprite.
return sprite;
}

Expand Down

0 comments on commit 61ae161

Please sign in to comment.