afegit resource::cache

normalitzat Audio
This commit is contained in:
2026-04-18 11:41:34 +02:00
parent 7409c799c3
commit 94aa69cffe
29 changed files with 1420 additions and 134 deletions

View File

@@ -1,7 +1,9 @@
#include "core/jail/jdraw8.hpp"
#include <fstream>
#include <string>
#include "core/resources/resource_cache.hpp"
#include "core/resources/resource_helper.hpp"
#if defined(__clang__)
#pragma clang diagnostic push
@@ -43,25 +45,57 @@ JD8_Surface JD8_NewSurface() {
return surface;
}
JD8_Surface JD8_LoadSurface(const char* file) {
auto buffer = ResourceHelper::loadFile(file);
// Helper intern: deriva el basename d'una ruta per a buscar al Cache.
static std::string jd8_basename(const char* file) {
std::string s = file;
auto pos = s.find_last_of("/\\");
return pos == std::string::npos ? s : s.substr(pos + 1);
}
JD8_Surface JD8_LoadSurface(const char* file) {
// Prova primer el Resource::Cache. Si l'asset és precarregat, copiem
// els 64KB des del cache (microsegons) i ens estalviem la decodificació
// GIF. Mantenim el contracte de la funció: el caller rep un buffer
// fresc que ha d'alliberar amb JD8_FreeSurface.
if (Resource::Cache::get() != nullptr) {
try {
const auto& cached = Resource::Cache::get()->getSurfacePixels(jd8_basename(file));
JD8_Surface image = JD8_NewSurface();
memcpy(image, cached.data(), 64000);
return image;
} catch (const std::exception&) {
// No està al cache (asset no llistat al manifest). Fallback.
}
}
auto buffer = ResourceHelper::loadFile(file);
unsigned short w, h;
Uint8* pixels = LoadGif(buffer.data(), &w, &h);
if (pixels == NULL) {
printf("Unable to load bitmap: %s\n", SDL_GetError());
exit(1);
}
JD8_Surface image = JD8_NewSurface();
memcpy(image, pixels, 64000);
free(pixels);
return image;
}
JD8_Palette JD8_LoadPalette(const char* file) {
if (Resource::Cache::get() != nullptr) {
try {
const auto& cached = Resource::Cache::get()->getPaletteBytes(jd8_basename(file));
// Reservem un buffer 768 bytes (256 * RGB) que el caller ha
// d'alliberar amb free() — mateixa convenció que el LoadPalette
// original (retornava un malloc).
JD8_Palette palette = (JD8_Palette)malloc(768);
memcpy(palette, cached.data(), 768);
return palette;
} catch (const std::exception&) {
// No està al cache.
}
}
auto buffer = ResourceHelper::loadFile(file);
return (JD8_Palette)LoadPalette(buffer.data());
}
@@ -78,6 +112,23 @@ void JD8_FillSquare(int ini, int height, Uint8 color) {
memset(&screen[offset], color, size);
}
void JD8_FillRect(int x, int y, int w, int h, Uint8 color) {
if (x < 0) {
w += x;
x = 0;
}
if (y < 0) {
h += y;
y = 0;
}
if (x + w > 320) w = 320 - x;
if (y + h > 200) h = 200 - y;
if (w <= 0 || h <= 0) return;
for (int row = y; row < y + h; ++row) {
memset(&screen[x + (row * 320)], color, w);
}
}
void JD8_Blit(JD8_Surface surface) {
memcpy(screen, surface, 64000);
}