Skip to content

Commit

Permalink
Merge branch 'Lactozilla:dev' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Axanery authored Apr 30, 2024
2 parents d91a0c4 + a8121a5 commit cd0ebc1
Show file tree
Hide file tree
Showing 25 changed files with 204 additions and 88 deletions.
33 changes: 18 additions & 15 deletions source/Engine/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -889,21 +889,24 @@ PRIVATE STATIC void Application::RunFrame(void* p) {
DEBUG_fontSprite = new ISprite();

int cols, rows;
DEBUG_fontSprite->SpritesheetCount = 1;
DEBUG_fontSprite->Spritesheets[0] = DEBUG_fontSprite->AddSpriteSheet("Debug/Font.png");
if (!DEBUG_fontSprite->Spritesheets[0])
DEBUG_fontSprite->Spritesheets[0] = DEBUG_fontSprite->AddSpriteSheet("Sprites/Fonts/Font.png");
cols = DEBUG_fontSprite->Spritesheets[0]->Width / 32;
rows = DEBUG_fontSprite->Spritesheets[0]->Height / 32;

DEBUG_fontSprite->ReserveAnimationCount(1);
DEBUG_fontSprite->AddAnimation("Font?", 0, 0, cols * rows);
for (int i = 0; i < cols * rows; i++) {
DEBUG_fontSprite->AddFrame(0,
(i % cols) * 32,
(i / cols) * 32,
32, 32, 0, 0,
14);
Texture* spriteSheet = DEBUG_fontSprite->AddSpriteSheet("Debug/Font.png");
if (!spriteSheet)
spriteSheet = DEBUG_fontSprite->AddSpriteSheet("Sprites/Fonts/DebugFont.png");
if (spriteSheet) {
DEBUG_fontSprite->Spritesheets.push_back(spriteSheet);

cols = spriteSheet->Width / 32;
rows = spriteSheet->Height / 32;

DEBUG_fontSprite->ReserveAnimationCount(1);
DEBUG_fontSprite->AddAnimation("Font", 0, 0, cols * rows);
for (int i = 0; i < cols * rows; i++) {
DEBUG_fontSprite->AddFrame(0,
(i % cols) * 32,
(i / cols) * 32,
32, 32, 0, 0,
14);
}
}

Graphics::SetTextureInterpolation(original);
Expand Down
10 changes: 5 additions & 5 deletions source/Engine/FontFace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,9 @@ PUBLIC STATIC ISprite* FontFace::SpriteFromFont(Stream* stream, int pixelSize, c
}
}

sprite->Spritesheets[0] = Graphics::CreateTextureFromPixels(package->Width, package->Height, pixelData, package->Width * sizeof(Uint32));
sprite->SpritesheetsBorrowed[0] = false;
sprite->SpritesheetCount = 1;
Texture* spriteSheet = Graphics::CreateTextureFromPixels(package->Width, package->Height, pixelData, package->Width * sizeof(Uint32));
if (spriteSheet)
sprite->Spritesheets.push_back(spriteSheet);

// Add preliminary chars
sprite->AddAnimation("Font", offsetBaseline & 0xFFFF, pixelSize, 0x100);
Expand Down Expand Up @@ -248,9 +248,9 @@ PUBLIC STATIC ISprite* FontFace::SpriteFromFont(Stream* stream, int pixelSize, c
}
}

char testFilename[256];
char testFilename[4096];
snprintf(testFilename, sizeof testFilename, "Fonts/%s_%d.bmp", filenameJustName, pixelSize);
strncpy(sprite->SpritesheetsFilenames[0], testFilename, 32);
sprite->SpritesheetsFilenames.push_back(StringUtils::Duplicate(testFilename));

SDL_Surface* surface = SDL_CreateRGBSurfaceFrom(pixelData, package->Width, package->Height, 32, package->Width * 4,
0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
Expand Down
2 changes: 1 addition & 1 deletion source/Engine/Rendering/GL/GLRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2087,7 +2087,7 @@ PUBLIC STATIC void GLRenderer::MakeFrameBufferID(ISprite* sprite, AnimFrame*
GL_AnimFrameVert vertices[16];
GL_AnimFrameVert* vert = &vertices[0];

if (frame->SheetNumber >= sprite->SpritesheetCount)
if (frame->SheetNumber >= sprite->Spritesheets.size())
return;
if (!sprite->Spritesheets[frame->SheetNumber])
return;
Expand Down
120 changes: 79 additions & 41 deletions source/Engine/ResourceTypes/ISprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,18 @@

class ISprite {
public:
char Filename[256];
char* Filename = nullptr;

bool LoadFailed;
bool LoadFailed = true;

Texture* Spritesheets[32];
bool SpritesheetsBorrowed[32];
char SpritesheetsFilenames[128][32];
int SpritesheetCount = 0;
vector<Texture*> Spritesheets;
vector<char*> SpritesheetsFilenames;
int CollisionBoxCount = 0;

vector<Animation> Animations;
};
#endif

#define MAX_SPRITESHEETS 32

#include <Engine/ResourceTypes/ISprite.h>

#include <Engine/Application.h>
Expand All @@ -40,17 +36,19 @@ class ISprite {
#include <Engine/Utilities/StringUtils.h>

PUBLIC ISprite::ISprite() {
memset(Spritesheets, 0, sizeof(Spritesheets));
memset(SpritesheetsBorrowed, 0, sizeof(SpritesheetsBorrowed));
memset(Filename, 0, 256);
Spritesheets.clear();
Spritesheets.shrink_to_fit();
SpritesheetsFilenames.clear();
SpritesheetsFilenames.shrink_to_fit();
LoadFailed = true;
Filename = nullptr;
}
PUBLIC ISprite::ISprite(const char* filename) {
memset(Spritesheets, 0, sizeof(Spritesheets));
memset(SpritesheetsBorrowed, 0, sizeof(SpritesheetsBorrowed));
memset(Filename, 0, 256);

strncpy(Filename, filename, 255);
Spritesheets.clear();
Spritesheets.shrink_to_fit();
SpritesheetsFilenames.clear();
SpritesheetsFilenames.shrink_to_fit();
Filename = StringUtils::Duplicate(filename);
LoadFailed = !LoadAnimation(Filename);
}

Expand Down Expand Up @@ -213,20 +211,20 @@ PUBLIC void ISprite::RemoveFrames(int animID) {
}

PUBLIC void ISprite::ConvertToRGBA() {
for (int a = 0; a < SpritesheetCount; a++) {
for (int a = 0; a < Spritesheets.size(); a++) {
if (Spritesheets[a])
Graphics::ConvertTextureToRGBA(Spritesheets[a]);
}
}
PUBLIC void ISprite::ConvertToPalette(unsigned paletteNumber) {
for (int a = 0; a < SpritesheetCount; a++) {
for (int a = 0; a < Spritesheets.size(); a++) {
if (Spritesheets[a])
Graphics::ConvertTextureToPalette(Spritesheets[a], paletteNumber);
}
}

PUBLIC bool ISprite::LoadAnimation(const char* filename) {
char* str, altered[4096];
char* str;
int animationCount, previousAnimationCount;

Stream* reader = ResourceStream::New(filename);
Expand All @@ -253,30 +251,58 @@ PUBLIC bool ISprite::LoadAnimation(const char* filename) {
reader->ReadUInt32();

// Get texture count
this->SpritesheetCount = reader->ReadByte();
unsigned spritesheetCount = reader->ReadByte();

// Load textures
for (int i = 0; i < this->SpritesheetCount; i++) {
if (i >= MAX_SPRITESHEETS) {
Log::Print(Log::LOG_ERROR, "Too many spritesheets in sprite %s!", filename);
exit(-1);
}
for (int i = 0; i < spritesheetCount; i++) {
char fullPath[4096];

str = reader->ReadHeaderedString();

// Spritesheet path is relative to where the animation file is
if (StringUtils::StartsWith(str, "./")) {
char *parentPath = StringUtils::GetPath(filename);
if (parentPath) {
snprintf(fullPath, sizeof fullPath, "%s/%s", parentPath, str + 2);
Memory::Free(parentPath);
}
else
snprintf(fullPath, sizeof fullPath, "%s", str + 2);
}
else
StringUtils::Copy(fullPath, str, sizeof fullPath);

Memory::Free(str);

// Replace '\' with '/'
StringUtils::ReplacePathSeparatorsInPlace(fullPath);

char* sheetName = StringUtils::Duplicate(fullPath);

#ifdef ISPRITE_DEBUG
Log::Print(Log::LOG_VERBOSE, " - %s", str);
Log::Print(Log::LOG_VERBOSE, " - %s", sheetName);
#endif

strcpy(SpritesheetsFilenames[i], str);
SpritesheetsFilenames.push_back(sheetName);

snprintf(altered, sizeof altered, "Sprites/%s", str);
Memory::Free(str);
bool shouldConcatSpritesPath = true;
if (StringUtils::StartsWith(sheetName, "Sprites/")) {
// don't need to concat "Sprites/" if the path already begins with that
shouldConcatSpritesPath = false;
}

if (shouldConcatSpritesPath) {
char* altered = StringUtils::ConcatPaths("Sprites", sheetName);
if (!altered)
abort();

if (Graphics::SpriteSheetTextureMap->Exists(altered))
SpritesheetsBorrowed[i] = true;
Spritesheets.push_back(AddSpriteSheet(altered));

Spritesheets[i] = AddSpriteSheet(altered);
// Spritesheets[i] = Image::LoadTextureFromResource(altered);
Memory::Free(altered);
}
else {
Spritesheets.push_back(AddSpriteSheet(sheetName));
}
}

// Get collision group count
Expand Down Expand Up @@ -310,17 +336,18 @@ PUBLIC bool ISprite::LoadAnimation(const char* filename) {
an.Flags = reader->ReadByte();

#ifdef ISPRITE_DEBUG
Log::Print(Log::LOG_VERBOSE, " \"%s\" (%d) (Flags: %02X, FtL: %d, Spd: %d, Frames: %d)", an.Name, a, an.Flags, an.FrameToLoop, an.AnimationSpeed, frameCount);
Log::Print(Log::LOG_VERBOSE, " \"%s\" (%d) (Flags: %02X, FtL: %d, Spd: %d, Frames: %d)", an.Name, a, an.Flags, an.FrameToLoop, an.AnimationSpeed, an.FrameCount);
#endif

an.Frames.resize(an.FrameCount);

for (int i = 0; i < an.FrameCount; i++) {
AnimFrame anfrm;
anfrm.SheetNumber = reader->ReadByte();
frameID++;

if (anfrm.SheetNumber >= SpritesheetCount)
Log::Print(Log::LOG_ERROR, "Sheet number %d outside of range of sheet count %d! (Animation %d, Frame %d)", anfrm.SheetNumber, SpritesheetCount, a, i);
if (anfrm.SheetNumber >= Spritesheets.size())
Log::Print(Log::LOG_ERROR, "Sheet number %d outside of range of sheet count %d! (Animation %d, Frame %d)", anfrm.SheetNumber, Spritesheets.size(), a, i);

anfrm.Duration = reader->ReadInt16();
anfrm.Advance = reader->ReadUInt16();
Expand Down Expand Up @@ -388,10 +415,10 @@ PUBLIC bool ISprite::SaveAnimation(const char* filename) {
stream->WriteUInt32(totalFrameCount);

// Get texture count
stream->WriteByte(this->SpritesheetCount);
stream->WriteByte(this->Spritesheets.size());

// Load textures
for (int i = 0; i < this->SpritesheetCount; i++) {
for (int i = 0; i < this->Spritesheets.size(); i++) {
stream->WriteHeaderedString(SpritesheetsFilenames[i]);
}

Expand Down Expand Up @@ -470,13 +497,24 @@ PUBLIC void ISprite::Dispose() {
Animations.clear();
Animations.shrink_to_fit();

for (int a = 0; a < SpritesheetCount; a++) {
for (int a = 0; a < Spritesheets.size(); a++) {
if (Spritesheets[a]) {
// if (!SpritesheetsBorrowed[a])
// Graphics::DisposeTexture(Spritesheets[a]);
Spritesheets[a] = NULL;
}
}

Spritesheets.clear();
Spritesheets.shrink_to_fit();

for (int a = 0; a < SpritesheetsFilenames.size(); a++) {
Memory::Free(SpritesheetsFilenames[a]);
}

SpritesheetsFilenames.clear();
SpritesheetsFilenames.shrink_to_fit();

Memory::Free(Filename);
Filename = nullptr;
}

PUBLIC ISprite::~ISprite() {
Expand Down
26 changes: 17 additions & 9 deletions source/Engine/ResourceTypes/ResourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ class ResourceManager {
#include <Engine/IO/Stream.h>
#include <Engine/Application.h>

#define KEEP_DATA_PACKS_IN_MEMORY

struct StreamNode {
Stream* Table;
struct StreamNode* Next;
Expand Down Expand Up @@ -75,7 +73,6 @@ PUBLIC STATIC void ResourceManager::Init(const char* filename) {
char modpacksString[1024];
if (Application::Settings->GetString("game", "modpacks", modpacksString, sizeof modpacksString)) {
if (File::Exists(modpacksString)) {
// ResourceManager::UsingDataFolder = false;
ResourceManager::UsingModPack = true;

Log::Print(Log::LOG_IMPORTANT, "Using \"%s\"", modpacksString);
Expand Down Expand Up @@ -134,6 +131,7 @@ PUBLIC STATIC void ResourceManager::Load(const char* filename) {
PUBLIC STATIC bool ResourceManager::LoadResource(const char* filename, Uint8** out, size_t* size) {
Uint8* memory;
char resourcePath[4096];
char *pathToLoad = StringUtils::ReplacePathSeparators(filename);
ResourceRegistryItem item;

if (ResourceManager::UsingDataFolder && !ResourceManager::UsingModPack)
Expand All @@ -142,10 +140,10 @@ PUBLIC STATIC bool ResourceManager::LoadResource(const char* filename, Uint8**
if (!ResourceRegistry)
goto DATA_FOLDER;

if (!ResourceRegistry->Exists(filename))
if (!ResourceRegistry->Exists(pathToLoad))
goto DATA_FOLDER;

item = ResourceRegistry->Get(filename);
item = ResourceRegistry->Get(pathToLoad);

memory = (Uint8*)Memory::Malloc(item.Size + 1);
if (!memory)
Expand All @@ -172,7 +170,7 @@ PUBLIC STATIC bool ResourceManager::LoadResource(const char* filename, Uint8**
if (item.DataFlag == 2) {
Uint8 keyA[16];
Uint8 keyB[16];
Uint32 filenameHash = CRC32::EncryptString(filename);
Uint32 filenameHash = CRC32::EncryptString(pathToLoad);
Uint32 sizeHash = CRC32::EncryptData(&item.Size, sizeof(item.Size));

// Populate Key A
Expand Down Expand Up @@ -231,12 +229,16 @@ PUBLIC STATIC bool ResourceManager::LoadResource(const char* filename, Uint8**
}
}

Memory::Free(pathToLoad);

*out = memory;
*size = (size_t)item.Size;
return true;

DATA_FOLDER:
ResourceManager::PrefixResourcePath(resourcePath, sizeof resourcePath, filename);
ResourceManager::PrefixResourcePath(resourcePath, sizeof resourcePath, pathToLoad);

Memory::Free(pathToLoad);

SDL_RWops* rw = SDL_RWFromFile(resourcePath, "rb");
if (!rw) {
Expand All @@ -263,20 +265,26 @@ PUBLIC STATIC bool ResourceManager::LoadResource(const char* filename, Uint8**
return true;
}
PUBLIC STATIC bool ResourceManager::ResourceExists(const char* filename) {
char *pathToLoad = StringUtils::ReplacePathSeparators(filename);

char resourcePath[4096];
if (ResourceManager::UsingDataFolder && !ResourceManager::UsingModPack)
goto DATA_FOLDER;

if (!ResourceRegistry)
goto DATA_FOLDER;

if (!ResourceRegistry->Exists(filename))
if (!ResourceRegistry->Exists(pathToLoad))
goto DATA_FOLDER;

Memory::Free(pathToLoad);

return true;

DATA_FOLDER:
ResourceManager::PrefixResourcePath(resourcePath, sizeof resourcePath, filename);
ResourceManager::PrefixResourcePath(resourcePath, sizeof resourcePath, pathToLoad);

Memory::Free(pathToLoad);

SDL_RWops* rw = SDL_RWFromFile(resourcePath, "rb");
if (!rw) {
Expand Down
Loading

0 comments on commit cd0ebc1

Please sign in to comment.