Skip to content

Commit

Permalink
Commonizes IconSet functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsons26 committed Jun 27, 2024
1 parent 8000d0a commit fbecc6f
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 455 deletions.
1 change: 1 addition & 0 deletions common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ set(COMMON_SRC
getshape.cpp
graphicsviewport.cpp
hsv.cpp
iconset.cpp
iff.cpp
ini.cpp
int.cpp
Expand Down
95 changes: 74 additions & 21 deletions tiberiandawn/iconset.cpp → common/iconset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,15 @@

#include <stdlib.h>
#include <stdio.h>
#include "common/wwstd.h"
#include "common/file.h"
#include "wwstd.h"
#include "file.h"
#include "tile.h"
#include "common/iff.h"
#include "iff.h"

// Misc? ST - 1/3/2019 10:40AM
// extern int Misc;
int Misc;

int Get_Icon_Set_Size(void const* iconset);
int Get_Icon_Set_Width(void const* iconset);
int Get_Icon_Set_Height(void const* iconset);
void* Get_Icon_Set_Icondata(void const* iconset);
void* Get_Icon_Set_Trans(void const* iconset);
void* Get_Icon_Set_Remapdata(void const* iconset);
void* Get_Icon_Set_Palettedata(void const* iconset);
int Get_Icon_Set_Count(void const* iconset);
void* Get_Icon_Set_Map(void const* iconset);

//#define ICON_PALETTE_BYTES 16
//#define ICON_MAX 256

Expand All @@ -76,7 +66,11 @@ int Get_Icon_Set_Size(void const* iconset)

icontrol = (IControl_Type*)iconset;
if (icontrol) {
size = le32toh(icontrol->Size);
if (icontrol->Is_CC_IconSet()) {
size = le32toh(icontrol->CC.Size);
} else {
size = le32toh(icontrol->RA.Size);
}
}
return (size);
}
Expand Down Expand Up @@ -110,7 +104,11 @@ void* Get_Icon_Set_Icondata(void const* iconset)
IControl_Type* icontrol;
icontrol = (IControl_Type*)iconset;
if (icontrol)
return (Add_Long_To_Pointer(iconset, le32toh(icontrol->Icons)));
if (icontrol->Is_CC_IconSet()) {
return (Add_Long_To_Pointer(iconset, le32toh(icontrol->CC.Icons)));
} else {
return (Add_Long_To_Pointer(iconset, le32toh(icontrol->RA.Icons)));
}
return (NULL);
}

Expand All @@ -121,7 +119,11 @@ void* Get_Icon_Set_Trans(void const* iconset)

icontrol = (IControl_Type*)iconset;
if (icontrol) {
ptr = Add_Long_To_Pointer((void*)iconset, le32toh(icontrol->TransFlag));
if (icontrol->Is_CC_IconSet()) {
ptr = Add_Long_To_Pointer((void*)iconset, le32toh(icontrol->CC.TransFlag));
} else {
ptr = Add_Long_To_Pointer((void*)iconset, le32toh(icontrol->RA.TransFlag));
}
}
return (ptr);
}
Expand All @@ -140,11 +142,62 @@ int Get_Icon_Set_Count(void const* iconset)

void* Get_Icon_Set_Map(void const* iconset)
{
char* icontrol = (char*)iconset;
IControl_Type* icontrol;
int32_t icontrol_map;

icontrol = (IControl_Type*)iconset;
if (icontrol) {
uint32_t icontrol_map;
memcpy(&icontrol_map, icontrol + offsetof(IControl_Type, Map), sizeof(uint32_t));
return icontrol + le32toh(icontrol_map);
if (icontrol->Is_CC_IconSet()) {
memcpy(&icontrol_map, (char *)iconset + offsetof(IControl_Type, CC.Map), sizeof(int32_t));
} else {
memcpy(&icontrol_map, (char *)iconset + offsetof(IControl_Type, RA.Map), sizeof(int32_t));
}
return (char *)iconset + le32toh(icontrol_map);
}
return (NULL);
}
}

int Get_Icon_Set_MapWidth(void const* iconset)
{
IControl_Type* icontrol;

icontrol = (IControl_Type*)iconset;
if (iconset) {
if (icontrol->Is_CC_IconSet()) {
//CC doesn't have this
} else {
return le16toh((((IControl_Type*)iconset)->RA.MapWidth));
}
}
return (0);
}

int Get_Icon_Set_MapHeight(void const* iconset)
{
IControl_Type* icontrol;

icontrol = (IControl_Type*)iconset;
if (iconset) {
if (icontrol->Is_CC_IconSet()) {
//CC doesn't have this
} else {
return le16toh((((IControl_Type*)iconset)->RA.MapHeight));
}
}
return (0);
}

unsigned char const* Get_Icon_Set_ControlMap(void const* iconset)
{
IControl_Type* icontrol;

icontrol = (IControl_Type*)iconset;
if (iconset) {
if (icontrol->Is_CC_IconSet()) {
//CC doesn't have this
} else {
return ((unsigned char const*)((char*)iconset + ((IControl_Type*)iconset)->RA.ColorMap));
}
}
return (NULL);
}
84 changes: 10 additions & 74 deletions common/stamp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,79 +9,28 @@
// distributed with this program. You should have received a copy of the
// GNU General Public License along with permitted additional restrictions
// with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
#include "tile.h"
#include "endianness.h"
#include "graphicsviewport.h"
#include <string.h>
#include <stdint.h>

#define TD_TILESET_CHECK 0x20

#pragma pack(push, 1)
struct IconControlType
{
uint8_t* Get_Icon_Data()
{
if (TD.Icons == TD_TILESET_CHECK) {
return reinterpret_cast<uint8_t*>(this) + TD.Icons;
} else {
return reinterpret_cast<uint8_t*>(this) + RA.Icons;
}
}

uint8_t* Get_Icon_Map()
{
if (TD.Icons == TD_TILESET_CHECK) {
return reinterpret_cast<uint8_t*>(this) + TD.Map;
} else {
return reinterpret_cast<uint8_t*>(this) + RA.Map;
}
}

int16_t Width; // always 24 (ICON_WIDTH)
int16_t Height; // always 24 (ICON_HEIGHT)
int16_t Count; // count of cells in set, not same as images
int16_t Allocated; // is treated like a bool, always 0 in the file?

union
{
struct
{
int32_t Size; // filesize
int32_t Icons; // always 0x00000020
int32_t Palettes; // seems to always be 0x00000000
int32_t Remaps; // unknown, bitfield?
int32_t TransFlag; // array of images length, unknown
int32_t Map; // image index for each cell
} TD;

struct
{
int16_t MapWidth; // tile width in cells
int16_t MapHeight; // tile height in cells
int32_t Size; // filesize
int32_t Icons; // always 0x00000028
int32_t Palettes; // seems to always be 0x00000000
int32_t Remaps; // unknown, bitfield?
int32_t TransFlag; // array of images length, unknown
int32_t ColorMap; // terrain type index, ra only
int32_t Map; // image index for each cell
} RA;
};
};
#pragma pack(pop)

int IconEntry;
void* IconData;
const IconControlType* LastIconset;
const IControl_Type* LastIconset;
const uint8_t* StampPtr;
const uint8_t* TransFlagPtr;
const uint8_t* MapPtr;
int IconWidth;
int IconHeight;
int IconSize;
int IconCount;
int TDIcons;

void Init_Stamps(const IconControlType* iconset)
void Init_Stamps(const IControl_Type* iconset)
{
if (iconset && LastIconset != iconset) {
IconCount = le16toh(iconset->Count);
Expand All @@ -91,10 +40,10 @@ void Init_Stamps(const IconControlType* iconset)
IconSize = IconWidth * IconHeight;

// TD and RA tileset headers are slightly different, so check a constant that only exists in one type.
if (le32toh(iconset->TD.Icons) == TD_TILESET_CHECK) {
MapPtr = reinterpret_cast<const uint8_t*>(iconset) + le32toh(iconset->TD.Map);
StampPtr = reinterpret_cast<const uint8_t*>(iconset) + le32toh(iconset->TD.Icons);
TransFlagPtr = reinterpret_cast<const uint8_t*>(iconset) + le32toh(iconset->TD.TransFlag);
if (iconset->Is_CC_IconSet()) {
MapPtr = reinterpret_cast<const uint8_t*>(iconset) + le32toh(iconset->CC.Map);
StampPtr = reinterpret_cast<const uint8_t*>(iconset) + le32toh(iconset->CC.Icons);
TransFlagPtr = reinterpret_cast<const uint8_t*>(iconset) + le32toh(iconset->CC.TransFlag);
} else {
MapPtr = reinterpret_cast<const uint8_t*>(iconset) + le32toh(iconset->RA.Map);
StampPtr = reinterpret_cast<const uint8_t*>(iconset) + le32toh(iconset->RA.Icons);
Expand All @@ -106,7 +55,7 @@ void Init_Stamps(const IconControlType* iconset)
void Buffer_Draw_Stamp(void* thisptr, void* icondata, int icon, int x, int y, const void* remapper)
{
GraphicViewPortClass& viewport = *static_cast<GraphicViewPortClass*>(thisptr);
IconControlType* tileset = static_cast<IconControlType*>(icondata);
IControl_Type* tileset = static_cast<IControl_Type*>(icondata);

if (!tileset) {
return;
Expand Down Expand Up @@ -177,7 +126,7 @@ void Buffer_Draw_Stamp_Clip(void const* thisptr,
int bottom)
{
const GraphicViewPortClass& viewport = *static_cast<const GraphicViewPortClass*>(thisptr);
const IconControlType* tileset = static_cast<const IconControlType*>(icondata);
const IControl_Type* tileset = static_cast<const IControl_Type*>(icondata);

if (!tileset) {
return;
Expand Down Expand Up @@ -264,16 +213,3 @@ void Buffer_Draw_Stamp_Clip(void const* thisptr,
}
}
}

uint8_t* Get_Icon_Set_Map(void* temp)
{
if (temp != nullptr) {
if (le32toh(static_cast<IconControlType*>(temp)->TD.Icons) == TD_TILESET_CHECK) {
return static_cast<uint8_t*>(temp) + le32toh(static_cast<IconControlType*>(temp)->TD.Icons);
} else {
return static_cast<uint8_t*>(temp) + le32toh(static_cast<IconControlType*>(temp)->RA.Icons);
}
}

return nullptr;
}
51 changes: 43 additions & 8 deletions tiberiandawn/tile.h → common/tile.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
#ifndef TILE_H
#define TILE_H

#include "endianness.h"
#include <stdint.h>

/*=========================================================================*/
/* The following prototypes are for the file: ICONSET.CPP */
/*=========================================================================*/
Expand All @@ -47,25 +50,57 @@ void* Get_Icon_Set_Palettedata(void const* iconset);
int Get_Icon_Set_Count(void const* iconset);
void* Get_Icon_Set_Map(void const* iconset);

int Get_Icon_Set_MapWidth(void const* iconset);
int Get_Icon_Set_MapHeight(void const* iconset);

unsigned char const* Get_Icon_Set_ControlMap(void const* iconset);

#define CC_ICON_OFFSET 0x20

/*
** This is the control structure at the start of a loaded icon set. It must match
** the structure in WWLIB.I! This structure MUST be a multiple of 16 bytes long.
*/

// C&C version of struct
#pragma pack(push, 2)
typedef struct
{
bool Is_CC_IconSet(void) const
{
return le32toh(CC.Icons) == CC_ICON_OFFSET;
}

int16_t Width; // Width of icons (pixels).
int16_t Height; // Height of icons (pixels).
int16_t Count; // Number of (logical) icons in this set.
int16_t Allocated; // Was this iconset allocated?
int32_t Size; // Size of entire iconset memory block.
int32_t Icons; // Offset from buffer start to icon data.
int32_t Palettes; // Offset from buffer start to palette data.
int32_t Remaps; // Offset from buffer start to remap index data.
int32_t TransFlag; // Offset for transparency flag table.
int32_t Map; // Icon map offset (if present).

union
{
// C&C version of struct
struct
{
int32_t Size; // Size of entire iconset memory block.
int32_t Icons; // Offset from buffer start to icon data.
int32_t Palettes; // Offset from buffer start to palette data.
int32_t Remaps; // Offset from buffer start to remap index data.
int32_t TransFlag; // Offset for transparency flag table.
int32_t Map; // Icon map offset (if present).
} CC;

// RA version of struct
struct
{
int16_t MapWidth; // Width of map (in icons).
int16_t MapHeight; // Height of map (in icons).
int32_t Size; // Size of entire iconset memory block.
int32_t Icons; // Offset from buffer start to icon data.
int32_t Palettes; // Offset from buffer start to palette data.
int32_t Remaps; // Offset from buffer start to remap index data.
int32_t TransFlag; // Offset for transparency flag table.
int32_t ColorMap; // Offset for color control value table.
int32_t Map; // Icon map offset (if present).
} RA;
};
} IControl_Type;
#pragma pack(pop)

Expand Down
1 change: 0 additions & 1 deletion redalert/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ set(REDALERT_SRC
help.cpp
house.cpp
iconlist.cpp
iconset.cpp
idata.cpp
infantry.cpp
init.cpp
Expand Down
Loading

0 comments on commit fbecc6f

Please sign in to comment.