From 008c9e7199c7ee6503e78105aedfbcf5e4ee3612 Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Sat, 1 Nov 2025 10:31:05 +0100 Subject: [PATCH] =?UTF-8?q?-=20[FIX]=20Els=20tiles=20necessiten=20un=20uin?= =?UTF-8?q?t16=5Ft=20-=20[NEW]=20Pintat=20de=20una=20room=20-=20[NEW]=20C?= =?UTF-8?q?=C3=A0rrega=20de=20paletes=20en=20format=20JASC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/images.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ source/images.h | 1 + source/main.cpp | 25 ++++++++++++++++++------- source/rooms.cpp | 18 +++++++++++++++++- source/rooms.h | 3 ++- 5 files changed, 80 insertions(+), 9 deletions(-) diff --git a/source/images.cpp b/source/images.cpp index ad5697d..2031721 100644 --- a/source/images.cpp +++ b/source/images.cpp @@ -1,5 +1,9 @@ #include "images.h" #include +#include +#include +#include +#include namespace images { @@ -14,4 +18,42 @@ namespace images images[filename] = image; return image; } + + int loadPalette(std::string filename) + { + uint32_t palette[16]; + + FILE *file = fopen(filename.c_str(), "r"); + if (!file) return -1; + + char header[32]; + int version, count; + + // Read and validate header + if (!fgets(header, sizeof(header), file) || strncmp(header, "JASC-PAL", 8) != 0) { + fclose(file); + return -2; + } + + if (fscanf(file, "%d\n%d\n", &version, &count) != 2 || count != 16) { + fclose(file); + return -3; + } + + // Read 16 RGB triplets + for (int i = 0; i < 16; ++i) { + int r, g, b; + if (fscanf(file, "%d %d %d\n", &r, &g, &b) != 3) { + fclose(file); + return -4; + } + palette[i] = (r << 16) | (g << 8) | b; + } + + fclose(file); + + draw::setPalette(palette, 16); + + return 0; + } } diff --git a/source/images.h b/source/images.h index 37dcc74..28e0eef 100644 --- a/source/images.h +++ b/source/images.h @@ -5,4 +5,5 @@ namespace images { draw::surface *getImage(std::string filename); + int loadPalette(std::string filename); } diff --git a/source/main.cpp b/source/main.cpp index 20b4d32..55a74bb 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,11 +1,22 @@ #include "rooms.h" +#include "japi/draw.h" +#include "japi/game.h" +#include "images.h" -namespace game +bool loop(); + +void game::init() { - int init() - { - rooms::load(); + draw::init("DILEMMAKER v0.1", 320, 240, 3, false); + game::setState(loop); + rooms::load(); + images::loadPalette("./data/palette/zx-spectrum.pal"); +} - return 0; - } -} \ No newline at end of file +bool loop() +{ + draw::setViewport(32, 24, 256, 128); + rooms::draw(); + draw::render(); + return true; +} diff --git a/source/rooms.cpp b/source/rooms.cpp index 016c307..2bd60b4 100644 --- a/source/rooms.cpp +++ b/source/rooms.cpp @@ -198,7 +198,7 @@ namespace rooms std::string value; while (std::getline(ss, value, ',')) { if (!value.empty()) { - room.tiles[col][row] = static_cast(std::stoi(value)); + room.tiles[col][row] = std::stoi(value); ++col; if (col == 32) { col = 0; @@ -246,4 +246,20 @@ namespace rooms } }*/ } + + void draw() + { + room_t &room = rooms["02"]; + draw::setSource(room.tileSetFile); + + for (int y=0; y<16; ++y) { + for (int x=0; x<32; ++x) { + uint16_t tile = room.tiles[x][y]; + if (tile>0) { + tile--; + draw::draw(x*8, y*8, 8, 8, (tile%24)*8, (tile/24)*8); + } + } + } + } } diff --git a/source/rooms.h b/source/rooms.h index f4073cb..6019b83 100644 --- a/source/rooms.h +++ b/source/rooms.h @@ -59,10 +59,11 @@ struct room_t uint8_t itemColor2 {0}; std::vector enemies; std::vector items; - uint8_t tiles[32][16]; + uint16_t tiles[32][16]; }; namespace rooms { void load(); + void draw(); }