From a0fa9b70af859f4b9cc768a48d2c0c760c13985f Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Fri, 31 Oct 2025 13:59:45 +0100 Subject: [PATCH] =?UTF-8?q?-=20[NEW]=20C=C3=A0rrega=20d'habitacions=20-=20?= =?UTF-8?q?[NEW]=20C=C3=A0rrega=20unificada=20d'imatges?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/images.cpp | 17 ++++ source/images.h | 8 ++ source/rooms.cpp | 234 +++++++++++++++++++++++++++++++++++++++++++++- source/rooms.h | 28 ++++-- 4 files changed, 274 insertions(+), 13 deletions(-) create mode 100644 source/images.cpp create mode 100644 source/images.h diff --git a/source/images.cpp b/source/images.cpp new file mode 100644 index 0000000..ad5697d --- /dev/null +++ b/source/images.cpp @@ -0,0 +1,17 @@ +#include "images.h" +#include + +namespace images +{ + std::map images; + + draw::surface *getImage(std::string filename) + { + auto item = images.find(filename); + if (item != images.end()) return item->second; + + draw::surface *image = draw::loadSurface(filename.c_str()); + images[filename] = image; + return image; + } +} diff --git a/source/images.h b/source/images.h new file mode 100644 index 0000000..37dcc74 --- /dev/null +++ b/source/images.h @@ -0,0 +1,8 @@ +#pragma once +#include "japi/draw.h" +#include + +namespace images +{ + draw::surface *getImage(std::string filename); +} diff --git a/source/rooms.cpp b/source/rooms.cpp index 415ff62..a42e561 100644 --- a/source/rooms.cpp +++ b/source/rooms.cpp @@ -1,21 +1,249 @@ #include "rooms.h" -#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "images.h" + +namespace fs = std::filesystem; namespace rooms { + int num_total_rooms = 0; + std::string current_room = ""; + std::map rooms; + static const char *paletteMap[] = { "black", "bright_black", "blue", "bright_blue", "red", "bright_red", "magenta", "bright_magenta", "green", "bright_green", "cyan", "bright_cyan", "yellow", "bright_yellow", "white", "bright_white" }; + const uint8_t colorToNum(std::string value) + { + for (int i=0; i<16; ++i) if (value == paletteMap[i]) return i; + return 0; + } + + // Trim from start (in-place) + inline void ltrim(std::string& s) { + s.erase(s.begin(), std::find_if(s.begin(), s.end(), + [](unsigned char ch) { return !std::isspace(ch); })); + } + + // Trim from end (in-place) + inline void rtrim(std::string& s) { + s.erase(std::find_if(s.rbegin(), s.rend(), + [](unsigned char ch) { return !std::isspace(ch); }).base(), s.end()); + } + + // Trim both ends (in-place) + inline void trim(std::string& s) { + ltrim(s); + rtrim(s); + } + + void processKeyValue(const std::string& room_name, const std::string& section, const std::string& key, const std::string& value) + { + room_t& room = rooms[room_name]; + + if (section == "global") { + if (key == "name") { + room.name = value; + } else if (key == "bgColor") { + room.bgColor = colorToNum(value); + } else if (key == "border") { + room.border = colorToNum(value); + //} else if (key == "tileMapFile") { + // room.tileMapFile = value; + } else if (key == "tileSetFile") { + room.tileSetFile = images::getImage("./data/tilesets/"+value); + } else if (key == "roomUp") { + room.roomUp = value; + } else if (key == "roomDown") { + room.roomDown = value; + } else if (key == "roomLeft") { + room.roomLeft = value; + } else if (key == "roomRight") { + room.roomRight = value; + } else if (key == "itemColor1") { + room.itemColor1 = colorToNum(value); + } else if (key == "itemColor2") { + room.itemColor2 = colorToNum(value); + } + } else if (section == "enemy") { + if (key == "animation") { + room.enemies[room.enemies.size()-1].animation = value; + } else if (key == "x") { + room.enemies[room.enemies.size()-1].x = std::stoi(value); + } else if (key == "y") { + room.enemies[room.enemies.size()-1].y = std::stoi(value); + } else if (key == "vx") { + room.enemies[room.enemies.size()-1].vx = std::stof(value); + } else if (key == "vy") { + room.enemies[room.enemies.size()-1].vy = std::stof(value); + } else if (key == "x1") { + room.enemies[room.enemies.size()-1].x1 = std::stoi(value); + } else if (key == "y1") { + room.enemies[room.enemies.size()-1].y1 = std::stoi(value); + } else if (key == "x2") { + room.enemies[room.enemies.size()-1].x2 = std::stoi(value); + } else if (key == "y2") { + room.enemies[room.enemies.size()-1].y2 = std::stoi(value); + } else if (key == "color") { + room.enemies[room.enemies.size()-1].color = colorToNum(value); + } else if (key == "flip") { + room.enemies[room.enemies.size()-1].flip = value=="true" ? true : false; + } else if (key == "mirror") { + room.enemies[room.enemies.size()-1].mirror = value=="true" ? true : false; + } + } else if (section == "item") { + if (key == "tile") { + room.items[room.items.size()-1].tile = std::stoi(value); + } else if (key == "x") { + room.items[room.items.size()-1].x = std::stoi(value); + } else if (key == "y") { + room.items[room.items.size()-1].y = std::stoi(value); + } else if (key == "counter") { + room.items[room.items.size()-1].counter = std::stof(value); + } + } + } + + void loadRoom(fs::path filename) + { + std::string room_name = filename.stem().string(); + + std::ifstream file(filename); + if (!file) { + std::cerr << "Failed to open file.\n"; + return; + } + + std::string current_section = "global"; + + std::string line; + while (std::getline(file, line)) + { + std::cout << "'" << line << "'\n"; + // Trim and clean line, ignore empty lines and comments + trim(line); + if (line.empty() || line[0] == '#') continue; + + // Remove inline comments + std::size_t comment_pos = line.find('#'); + if (comment_pos != std::string::npos) { + line = line.substr(0, comment_pos); + trim(line); + } + + // Check if entering or exiting a section + if (line[0] == '[') + { + std::size_t closing_pos = line.find(']'); + if (closing_pos != std::string::npos && closing_pos > 1) + { + std::string section = line.substr(1,closing_pos-1); + if (section[0] == '/') { + current_section = "global"; + } else { + current_section = section; + if (section == "enemy") rooms[room_name].enemies.emplace_back(); + else if (section == "item") rooms[room_name] .items.emplace_back(); + } + } + } + + // Get and process key/value + std::size_t eq_pos = line.find('='); + if (eq_pos != std::string::npos) { + std::string key = line.substr(0, eq_pos); + std::string value = line.substr(eq_pos + 1); + trim(key); + trim(value); + processKeyValue(room_name, current_section, key, value); + } + } + } + + void loadTiles(fs::path filename) + { + filename.replace_extension(".tmx"); + std::string room_name = filename.stem().string(); + std::string line; + bool inData = false; + int row = 0, col = 0; + + std::ifstream file(filename); + if (!file) { + std::cerr << "Failed to open file.\n"; + return; + } + + room_t& room = rooms[room_name]; + + while (std::getline(file, line)) { + if (!inData) { + if (line.find("") != std::string::npos) inData = true; + continue; + } + + if (line.find("") != std::string::npos) break; + + std::stringstream ss(line); + std::string value; + while (std::getline(ss, value, ',')) { + if (!value.empty()) { + room.tiles[col][row] = static_cast(std::stoi(value)); + ++col; + if (col == 32) { + col = 0; + ++row; + } + } + } + } + } void load() { - //fs::path target_dir = "./data/room"; + // Get all room files + fs::path target_dir = "./data/room"; + std::vector room_files; - int room_num = 1; + try { + for (const auto& entry : fs::directory_iterator(target_dir)) { + if (entry.is_regular_file() && entry.path().extension() == ".room") { + room_files.push_back(entry.path()); + } + } + std::sort(room_files.begin(), room_files.end()); + for (const auto& path : room_files) { + std::cout << "Processing " << path.filename() << '\n'; + loadRoom(path); + loadTiles(path); + } + + } catch (const fs::filesystem_error& e) { + std::cerr << "Filesystem error: " << e.what() << '\n'; + } + + for (auto &item : rooms) + { + room_t &room = item.second; + std::cout << room.name << std::endl; + + for (int y=0; y<16; ++y) { + for (int x=0; x<32; ++x) + std::cout << std::to_string(room.tiles[x][y]) << ","; + std::cout << std::endl; + } + } } } diff --git a/source/rooms.h b/source/rooms.h index ce03350..f4073cb 100644 --- a/source/rooms.h +++ b/source/rooms.h @@ -1,4 +1,8 @@ #pragma once +#include +#include +#include +#include "japi/draw.h" #define COLOR_BLACK 0 #define COLOR_BRIGHT_BLACK 1 @@ -19,7 +23,7 @@ struct enemy_t { - char *animation {nullptr}; + std::string animation {""}; uint8_t x {0}; uint8_t y {0}; float vx {0.0f}; @@ -29,6 +33,8 @@ struct enemy_t uint8_t x2 {0}; uint8_t y2 {0}; uint8_t color {COLOR_WHITE}; + bool flip {false}; + bool mirror {false}; }; struct item_t @@ -41,17 +47,19 @@ struct item_t struct room_t { - char *name {nullptr}; + std::string name {""}; uint8_t bgColor {COLOR_BLACK}; uint8_t border {COLOR_BLACK}; - char *tileMapFile {nullptr}; - char *tileSetFile {nullptr}; - uint8_t roomUp {0}; - uint8_t roomDown {0}; - uint8_t roomLeft {0}; - uint8_t roomRight {0}; - enemy_t *enemies; - item_t *items; + draw::surface *tileSetFile {nullptr}; + std::string roomUp {0}; + std::string roomDown {0}; + std::string roomLeft {0}; + std::string roomRight {0}; + uint8_t itemColor1 {0}; + uint8_t itemColor2 {0}; + std::vector enemies; + std::vector items; + uint8_t tiles[32][16]; }; namespace rooms