- [FIX] Els tiles necessiten un uint16_t

- [NEW] Pintat de una room
- [NEW] Càrrega de paletes en format JASC
This commit is contained in:
2025-11-01 10:31:05 +01:00
parent 76f4ae31c8
commit 008c9e7199
5 changed files with 80 additions and 9 deletions

View File

@@ -1,5 +1,9 @@
#include "images.h"
#include <map>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
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;
}
}

View File

@@ -5,4 +5,5 @@
namespace images
{
draw::surface *getImage(std::string filename);
int loadPalette(std::string filename);
}

View File

@@ -1,11 +1,22 @@
#include "rooms.h"
#include "japi/draw.h"
#include "japi/game.h"
#include "images.h"
namespace game
{
int init()
bool loop();
void game::init()
{
draw::init("DILEMMAKER v0.1", 320, 240, 3, false);
game::setState(loop);
rooms::load();
images::loadPalette("./data/palette/zx-spectrum.pal");
}
return 0;
}
bool loop()
{
draw::setViewport(32, 24, 256, 128);
rooms::draw();
draw::render();
return true;
}

View File

@@ -198,7 +198,7 @@ namespace rooms
std::string value;
while (std::getline(ss, value, ',')) {
if (!value.empty()) {
room.tiles[col][row] = static_cast<uint8_t>(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);
}
}
}
}
}

View File

@@ -59,10 +59,11 @@ struct room_t
uint8_t itemColor2 {0};
std::vector<enemy_t> enemies;
std::vector<item_t> items;
uint8_t tiles[32][16];
uint16_t tiles[32][16];
};
namespace rooms
{
void load();
void draw();
}