From 9dc27d7e8b80dd9113e809b7ac9bdb9ef1dd2bce Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Wed, 5 Jun 2024 14:23:16 +0200 Subject: [PATCH] =?UTF-8?q?-=20Carrega=20de=20l'habitaci=C3=B3=20des=20de?= =?UTF-8?q?=20arxiu=20-=20Traslladem=20a=20jutil=20funcionetes=20varies=20?= =?UTF-8?q?-=20Comencem=20a=20implementar=20carrega=20de=20actors=20des=20?= =?UTF-8?q?de=20arxiu=20-=20Comencem=20a=20implementar=20els=20templates?= =?UTF-8?q?=20de=20actors?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data/rooms/00.txt | 14 ++-- data/templates.txt | 15 ++++ source/actor.cpp | 24 ++++++ source/actor.h | 2 + source/jutil.cpp | 26 +++++++ source/jutil.h | 9 +++ source/main.cpp | 21 ++++-- source/room.cpp | 182 ++++++++++++++++++++++++++++++++++++++------- source/room.h | 33 +++++++- 9 files changed, 285 insertions(+), 41 deletions(-) create mode 100644 data/templates.txt create mode 100644 source/jutil.cpp create mode 100644 source/jutil.h diff --git a/data/rooms/00.txt b/data/rooms/00.txt index 722d63b..0165d43 100644 --- a/data/rooms/00.txt +++ b/data/rooms/00.txt @@ -1,11 +1,11 @@ width: 2 -height: 2 -door-xp: 0 1 -color: 9 -floor-texture: 0 -wall-texture: 0 -door-texture: 0 -under-door-texture: 0 +height: 3 +door-height-xn: 0 +color: cyan +floor-texture: 1 +wall-texture: 1 +door-texture: 1 +under-door-texture: 1 actor{ template: box diff --git a/data/templates.txt b/data/templates.txt new file mode 100644 index 0000000..17f5126 --- /dev/null +++ b/data/templates.txt @@ -0,0 +1,15 @@ +actor{ + name: BOX + bmp: test.gif + bmp-rect: 32 0 32 32 + bmp-offset: 0 32 + pos: 32 32 8 + size: 8 8 8 + orient: NONE + movement: CW + anim-cycle: WALK + anim-speed: 0 + flags: PUSH GRAV + react-mask: NONE + react-push: NONE +} diff --git a/source/actor.cpp b/source/actor.cpp index 2076a13..2d8f8c9 100644 --- a/source/actor.cpp +++ b/source/actor.cpp @@ -1,6 +1,8 @@ #include "actor.h" #include "jdraw.h" #include "jinput.h" +#include "jfile.h" +#include "jutil.h" #include "room.h" #include @@ -45,6 +47,28 @@ namespace actor return act; } + void createFromFile(char **buffer) + { + actor_t *act = (actor_t*)malloc(sizeof(actor_t)); + if (*buffer) + { + while (**buffer != 0) + { + const char* key = file::readString(buffer); + + if (util::strcomp(key, "pos:")) { + act->pos.x = file::readInt(buffer); + act->pos.y = file::readInt(buffer); + act->pos.z = file::readInt(buffer); + } else if (util::strcomp(key, "height:")) { + const int val = file::readInt(buffer); + inner_h = SDL_clamp(val, 0, 3); + } + } + } + setDirty(act, true); + } + const bool check_2d_collision(actor_t *obj1, actor_t *obj2) { return (obj1->pos.x < obj2->pos.x + obj2->size.x) && diff --git a/source/actor.h b/source/actor.h index b90dc1f..a21c7f6 100644 --- a/source/actor.h +++ b/source/actor.h @@ -86,6 +86,8 @@ namespace actor // Torna un nou actor actor_t *create(std::string name, vec3_t p, vec3_t s, std::string bmp, SDL_Rect r, SDL_Point o); + void createFromFile(char **buffer); + void setDirty(actor_t *act, const bool force=false); void select(actor_t *act); diff --git a/source/jutil.cpp b/source/jutil.cpp new file mode 100644 index 0000000..7abc6a0 --- /dev/null +++ b/source/jutil.cpp @@ -0,0 +1,26 @@ +#include "jutil.h" +#include +#include + +namespace util +{ + int stringToInt(const char *value, std::vector strings, std::vector values) + { + char lowercase[255]; + strcpy(lowercase, value); + for (int i=0; i + +namespace util +{ + int stringToInt(const char *value, std::vector strings, std::vector values); + + const bool strcomp(const char *a, const char* b); +} \ No newline at end of file diff --git a/source/main.cpp b/source/main.cpp index b15e003..122f30b 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -24,10 +24,13 @@ int room_walldoors = 0; std::vector gifs; +bool editing = false; + void restart() { - room::load(room_w, room_h, room_xp, room_xn, room_yp, room_yn, room_color, room_floor, room_walls, room_doors, room_walldoors); + room::load(0); //room_w, room_h, room_xp, room_xn, room_yp, room_yn, room_color, room_floor, room_walls, room_doors, room_walldoors); + /* box = actor::create("ASCENSOR",{16,32,0}, {8,8,4}, {64,0,32,24}, {0,24}); box->flags = FLAG_MOVING; @@ -115,7 +118,7 @@ void btn(const char* label, const int x, const int y, int &var, int min, int max if (result) { var=SDL_max(min, SDL_min(max, var-(result-2))); - restart(); + room::editor::setWidth(var); } } @@ -260,11 +263,17 @@ bool game::loop() { if (input::keyDown(SDL_SCANCODE_ESCAPE)) return false; + // WHILE EDITING... - //editor_move_selected(); - //actor::updateEditor(actor::getFirst()); - - actor::update(actor::getFirst()); + if (editing) + { + editor_move_selected(); + actor::updateEditor(actor::getFirst()); + } + else + { + actor::update(actor::getFirst()); + } actor::reorder(); diff --git a/source/room.cpp b/source/room.cpp index 2db6b28..8480e7a 100644 --- a/source/room.cpp +++ b/source/room.cpp @@ -1,24 +1,31 @@ #include "room.h" #include "jdraw.h" +#include "jfile.h" +#include "jutil.h" #include "actor.h" namespace room { - static vec3_t size = {8,8,8}; - static vec3_t tmin = {0,0,0}; - static vec3_t tmax = {7,7,7}; - static vec3_t min = {0,0,0}; - static vec3_t max = {56,56,56}; + static int inner_w = 2; + static int inner_h = 2; - static uint8_t doors = DOOR_XP /*| DOOR_YP*/ ; - static uint8_t door_height[4]; - static uint8_t color = 5; + static vec3_t size = {8,8,8}; // Tamany de l'habitació en tiles + static vec3_t tmin = {0,0,0}; // primer tile en cada coordenada + static vec3_t tmax = {7,7,7}; // ultim tile en cada coordenada + static vec3_t min = {0,0,0}; // primer "pixel isometric" en cada coordenada + static vec3_t max = {56,56,56}; // ultim "pixel isometric" en cada coordenada - static uint8_t floor_type = 0; - static uint8_t walls_type = 0; - static uint8_t doors_type = 0; - static uint8_t walldoors_type = 0; + static uint8_t doors = NO_DOOR; // Portes obertes + static uint8_t door_height[4]; // Altura de cada porta + static int8_t exits[6]; // Habitació destí a la que du cada porta (piso i sostre inclosos) + static uint8_t color = 5; // Color de l'habitació + static uint8_t floor_type = 0; // Tile per al piso + static uint8_t walls_type = 0; // Tile per a les pareds + static uint8_t doors_type = 0; // Textura per a les portes + static uint8_t walldoors_type = 0; // Textura per a baix de les portes + + // Surface on se guarden els gràfics de cada cosa static draw::surface *floor_surf = nullptr; static draw::surface *walls_surf = nullptr; static draw::surface *doors_surf = nullptr; @@ -28,6 +35,8 @@ namespace room { actor::clear(); + // [TODO][2024-06-05] Açò es una xorrada. Si al final sempre son els mateixos arxius, + // carregar açò al principi del joc i au if (floor_surf) draw::freeSurface(floor_surf); if (walls_surf) draw::freeSurface(walls_surf); if (doors_surf) draw::freeSurface(doors_surf); @@ -39,24 +48,18 @@ namespace room aux_surf = draw::loadSurface("roomaux.gif"); } - void load(int x, int y, int8_t xp, int8_t xn, int8_t yp, int8_t yn, uint8_t col, uint8_t floor, uint8_t walls, uint8_t door, uint8_t walldoor) + void refresh() { - init(); - color = col; - floor_type = floor; - walls_type = walls; - doors_type = door; - walldoors_type = walldoor; - size = {(x+1)*2,(y+1)*2,3}; - tmin = {3-x,3-y,0}; - tmax = {4+x,4+y,3}; + size = {(inner_w+1)*2,(inner_h+1)*2,3}; + tmin = {3-inner_w,3-inner_h,0}; + tmax = {4+inner_w,4+inner_h,3}; min = {tmin.x*8,tmin.y*8,0}; max = {tmax.x*8,tmax.y*8,24}; - doors = (xp>=0?DOOR_XP:0) | (xn>=0?DOOR_XN:0) | (yp>=0?DOOR_YP:0) | (yn>=0?DOOR_YN:0); - door_height[0] = xp; //XP - door_height[1] = xn; //XN - door_height[2] = yp; //YP - door_height[3] = yn; //YN + doors = (door_height[XP]>=0?DOOR_XP:0) | (door_height[XN]>=0?DOOR_XN:0) | (door_height[YP]>=0?DOOR_YP:0) | (door_height[YN]>=0?DOOR_YN:0); + //door_height[0] = inner_xp; //XP + //door_height[1] = inner_xn; //XN + //door_height[2] = inner_yp; //YP + //door_height[3] = inner_yn; //YN if (doors & DOOR_YP) { @@ -82,7 +85,105 @@ namespace room actor::reorder(); } + + //void load(int x, int y, int8_t xp, int8_t xn, int8_t yp, int8_t yn, uint8_t col, uint8_t floor, uint8_t walls, uint8_t door, uint8_t walldoor) + void load(const int room) + { + if (room > 64) { perror("ERROR: Nombre d'habitació massa gran! Eixint..."); exit(1); } + init(); + + // Primer carreguem els valors per defecte + inner_w = inner_h = 2; + for (int i=0;i<4;++i) door_height[i] = -1; + color = 9; + floor_type = walls_type = doors_type = walldoors_type = 0; + for (int i=0;i<6;++i) exits[i] = -1; + + // Després intentem carregar els valors segons el arxiu que toca, si existeix + char filename[] = "rooms/00.txt"; + filename[6] = int(room/10)*48; + filename[7] = (room%10)*48; + int filesize=0; + char *buffer = file::getFileBuffer(filename, filesize, true); + char *original_buffer = buffer; + + if (buffer) + { + while (*buffer != 0) + { + const char* key = file::readString(&buffer); + + if (util::strcomp(key, "width:")) { + const int val = file::readInt(&buffer); + inner_w = SDL_clamp(val, 0, 3); + } else if (util::strcomp(key, "height:")) { + const int val = file::readInt(&buffer); + inner_h = SDL_clamp(val, 0, 3); + + } else if (util::strcomp(key, "door-height-xp:")) { + const int val = file::readInt(&buffer); + door_height[XP] = SDL_clamp(val, -1, 5); + } else if (util::strcomp(key, "door-height-xn:")) { + const int val = file::readInt(&buffer); + door_height[XN] = SDL_clamp(val, -1, 5); + } else if (util::strcomp(key, "door-height-yp:")) { + const int val = file::readInt(&buffer); + door_height[YP] = SDL_clamp(val, -1, 5); + } else if (util::strcomp(key, "door-height-yn:")) { + const int val = file::readInt(&buffer); + door_height[YN] = SDL_clamp(val, -1, 5); + + } else if (util::strcomp(key, "color:")) { + color = util::stringToInt(file::readString(&buffer), {"blue", "red", "purple", "green", "cyan", "yellow", "white"}, {5, 6, 7, 8, 9, 10, 11}); + + } else if (util::strcomp(key, "floor-texture:")) { + floor_type = file::readInt(&buffer); + } else if (util::strcomp(key, "wall-texture:")) { + walls_type = file::readInt(&buffer); + } else if (util::strcomp(key, "door-texture:")) { + doors_type = file::readInt(&buffer); + } else if (util::strcomp(key, "under-door-texture:")) { + walldoors_type = file::readInt(&buffer); + + } else if (util::strcomp(key, "exit-xp:")) { + const int val = file::readInt(&buffer); + exits[0] = SDL_clamp(val, 0, 64); + } else if (util::strcomp(key, "exit-xn:")) { + const int val = file::readInt(&buffer); + exits[1] = SDL_clamp(val, 0, 64); + } else if (util::strcomp(key, "exit-yp:")) { + const int val = file::readInt(&buffer); + exits[2] = SDL_clamp(val, 0, 64); + } else if (util::strcomp(key, "exit-yn:")) { + const int val = file::readInt(&buffer); + exits[3] = SDL_clamp(val, 0, 64); + } else if (util::strcomp(key, "exit-zp:")) { + const int val = file::readInt(&buffer); + exits[4] = SDL_clamp(val, 0, 64); + } else if (util::strcomp(key, "exit-zn:")) { + const int val = file::readInt(&buffer); + exits[5] = SDL_clamp(val, 0, 64); + + } else if (util::strcomp(key, "actor{")) { + actor::createFromFile(&buffer); + } + } + free(original_buffer); + } + + refresh(); + } + void update() + { + actor::remove(actor::find("DOOR_XP1")); + actor::remove(actor::find("DOOR_XP2")); + actor::remove(actor::find("DOOR_YP1")); + actor::remove(actor::find("DOOR_YP2")); + + refresh(); + } + void draw() { draw::pushSource(); @@ -232,4 +333,31 @@ namespace room { return color; } + + namespace editor + { + void setWidth(const int value) { inner_w = value; update(); } + void setHeight(const int value) { inner_h = value; update(); } + void setDoor(const int which, const int value) { door_height[which] = value; update(); } + + void setColor(const int value) { color = value; update(); } + void setFloorTex(const int value) { floor_type = value; update(); } + void setWallTex(const int value) { walls_type = value; update(); } + void setDoorTex(const int value) { doors_type = value; update(); } + void setWallDoorTex(const int value) { walldoors_type = value; update(); } + void setExit(const int which, const int value) { exits[which] = value; update(); } + + const int getWidth() { return inner_w; } + const int getHeight() { return inner_h; } + const int getDoor(const int which) { return door_height[which]; } + + const int getColor() { return color; } + const int getFloorTex() { return floor_type; } + const int getWallTex() { return walls_type; } + const int getDoorTex() { return doors_type; } + const int getWallDoorTex() { return walldoors_type; } + + const int getExit(const int which) { return exits[which]; } + } + } diff --git a/source/room.h b/source/room.h index 3c15779..2d1c68a 100644 --- a/source/room.h +++ b/source/room.h @@ -1,6 +1,7 @@ #pragma once #include "misc.h" +#define NO_DOOR 0 #define DOOR_XP 1 #define DOOR_XN 2 #define DOOR_YP 4 @@ -10,11 +11,14 @@ #define XN 1 #define YP 2 #define YN 3 +#define ZP 4 +#define ZN 5 namespace room { void init(); - void load(int x, int y, int8_t xp, int8_t xn, int8_t yp, int8_t yn, uint8_t col, uint8_t floor, uint8_t walls, uint8_t door, uint8_t doorwall); + void load(const int room); + //void load(int x, int y, int8_t xp, int8_t xn, int8_t yp, int8_t yn, uint8_t col, uint8_t floor, uint8_t walls, uint8_t door, uint8_t doorwall); void draw(); void draw2(); @@ -24,4 +28,31 @@ namespace room uint8_t getDoors(); uint8_t getDoor(const int d); uint8_t getColor(); + + namespace editor + { + void setWidth(const int value); + void setHeight(const int value); + void setDoor(const int which, const int value); + + void setColor(const int value); + void setFloorTex(const int value); + void setWallTex(const int value); + void setDoorTex(const int value); + void setWallDoorTex(const int value); + + void setExit(const int which, const int value); + + const int getWidth(); + const int getHeight(); + const int getDoor(const int which); + + const int getColor(); + const int getFloorTex(); + const int getWallTex(); + const int getDoorTex(); + const int getWallDoorTex(); + + const int getExit(const int which); + } }