From cab6d3b205173e94fafc9f2fc1e8ac2acad16e0c Mon Sep 17 00:00:00 2001 From: Steve Date: Wed, 31 Jan 2018 21:50:49 +0000 Subject: [PATCH] Render entities. --- common.mk | 2 +- src/defs.h | 52 +++++++++++++-------------- src/entities/blobs/bob.c | 25 +++++++++++++ src/entities/blobs/bob.h | 6 ++++ src/entities/{entities.c => entity.c} | 2 +- src/entities/{entities.h => entity.h} | 0 src/entities/items/item.c | 2 -- src/entities/structures/pushBlock.c | 2 ++ src/entities/structures/pushBlock.h | 1 + src/entities/structures/structures.c | 2 -- src/entities/structures/teleporter.c | 2 ++ src/entities/structures/teleporter.h | 1 + src/entities/unit.c | 4 +-- src/structs.h | 2 +- src/system/sprites.c | 5 ++- src/system/sprites.h | 1 + src/test/atlasTest.c | 4 +++ src/test/atlasTest.h | 2 ++ src/world/entities.c | 42 ++++++++++++++++++++++ src/world/entities.h | 27 ++++++++++++++ src/world/worldLoader.c | 11 ++---- 21 files changed, 150 insertions(+), 45 deletions(-) rename src/entities/{entities.c => entity.c} (99%) rename src/entities/{entities.h => entity.h} (100%) create mode 100644 src/world/entities.c create mode 100644 src/world/entities.h diff --git a/common.mk b/common.mk index d41d001..785d781 100644 --- a/common.mk +++ b/common.mk @@ -33,7 +33,7 @@ OBJS += atlas.o atlasTest.o aquaBlob.o OBJS += battery.o blaze.o bob.o boss.o blobBoss.o OBJS += camera.o cannon.o cardReader.o cell.o cherry.o combat.o consumable.o OBJS += debris.o destructable.o door.o draw.o -OBJS += effects.o entities.o entityFactory.o exit.o explosions.o eyeDroid.o eyeDroidCommander.o evilBlob.o +OBJS += effects.o entities.o entity.o entityFactory.o exit.o explosions.o eyeDroid.o eyeDroidCommander.o evilBlob.o OBJS += fleshChunk.o frost.o OBJS += game.o grenade.o OBJS += heart.o horizontalDoor.o horizontalLaserTrap.o hub.o hud.o diff --git a/src/defs.h b/src/defs.h index d640c7b..bf16c24 100644 --- a/src/defs.h +++ b/src/defs.h @@ -88,29 +88,29 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. enum { ET_NONE, - ET_EFFECT, - ET_TRAP, - ET_BOB, - ET_ENEMY, - ET_TEEKA, - ET_BOSS, - ET_HEART_CELL, - ET_KEY, - ET_ITEM, - ET_CONSUMABLE, - ET_MIA, - ET_DECORATION, - ET_DOOR, - ET_LIFT, - ET_PUSHBLOCK, - ET_DESTRUCTABLE, - ET_POWER_POINT, - ET_CARD_READER, - ET_PRESSURE_PLATE, - ET_TELEPORTER, - ET_ITEM_PAD, - ET_POOL, - ET_EXIT, + ET_EFFECT, + ET_TRAP, + ET_BOB, + ET_ENEMY, + ET_TEEKA, + ET_BOSS, + ET_HEART_CELL, + ET_KEY, + ET_ITEM, + ET_CONSUMABLE, + ET_MIA, + ET_DECORATION, + ET_DOOR, + ET_LIFT, + ET_PUSHBLOCK, + ET_DESTRUCTABLE, + ET_POWER_POINT, + ET_CARD_READER, + ET_PRESSURE_PLATE, + ET_TELEPORTER, + ET_ITEM_PAD, + ET_POOL, + ET_EXIT, ET_INFO_POINT }; @@ -200,9 +200,9 @@ enum enum { MS_LOCKED = -1, - MS_INCOMPLETE, - MS_PARTIAL, - MS_MISSING_HEART_CELL, + MS_INCOMPLETE, + MS_PARTIAL, + MS_MISSING_HEART_CELL, MS_COMPLETE }; diff --git a/src/entities/blobs/bob.c b/src/entities/blobs/bob.c index 61ef481..4ca4c2e 100644 --- a/src/entities/blobs/bob.c +++ b/src/entities/blobs/bob.c @@ -20,6 +20,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "bob.h" +static void load(cJSON *root); +static void save(cJSON *root); + void initBob(void) { Unit *u; @@ -27,6 +30,13 @@ void initBob(void) u = createUnit(); u->type = ET_BOB; + + u->sprite[FACING_LEFT] = getSprite("BobLeft"); + u->sprite[FACING_RIGHT] = getSprite("BobRight"); + u->sprite[FACING_DIE] = getSprite("BobSpin"); + + u->load = load; + u->save = save; } void addBobItem(Item *i) @@ -38,3 +48,18 @@ int numCarriedItems(void) { return 0; } + +static void load(cJSON *root) +{ + world.bob->x = cJSON_GetObjectItem(root, "x")->valueint; + world.bob->y = cJSON_GetObjectItem(root, "y")->valueint; + world.bob->facing = lookup(cJSON_GetObjectItem(root, "facing")->valuestring); +} + +static void save(cJSON *root) +{ + cJSON_AddStringToObject(root, "type", "Bob"); + cJSON_AddNumberToObject(root, "x", world.bob->x); + cJSON_AddNumberToObject(root, "y", world.bob->y); + cJSON_AddStringToObject(root, "facing", getLookupName("FACING_", world.bob->facing)); +} diff --git a/src/entities/blobs/bob.h b/src/entities/blobs/bob.h index 7b4541a..df44bb7 100644 --- a/src/entities/blobs/bob.h +++ b/src/entities/blobs/bob.h @@ -19,5 +19,11 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "../../common.h" +#include "../../json/cJSON.h" extern Unit *createUnit(void); +extern Sprite *getSprite(char *name); +extern char *getLookupName(const char *prefix, long num); +extern long lookup(const char *name); + +extern World world; diff --git a/src/entities/entities.c b/src/entities/entity.c similarity index 99% rename from src/entities/entities.c rename to src/entities/entity.c index 867c5f8..c898072 100644 --- a/src/entities/entities.c +++ b/src/entities/entity.c @@ -18,7 +18,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include "entities.h" +#include "entity.h" void animateEntity(void); static void applyDamage(int damage); diff --git a/src/entities/entities.h b/src/entities/entity.h similarity index 100% rename from src/entities/entities.h rename to src/entities/entity.h diff --git a/src/entities/items/item.c b/src/entities/items/item.c index f02e28d..f1e57fc 100644 --- a/src/entities/items/item.c +++ b/src/entities/items/item.c @@ -37,8 +37,6 @@ Entity *createItem(void) i = malloc(sizeof(Item)); memset(i, 0, sizeof(Item)); - world.entityTail->next = (Entity*)i; - world.entityTail = (Entity*)i; initEntity((Entity*)i); diff --git a/src/entities/structures/pushBlock.c b/src/entities/structures/pushBlock.c index f982e39..c17738d 100644 --- a/src/entities/structures/pushBlock.c +++ b/src/entities/structures/pushBlock.c @@ -35,6 +35,8 @@ Entity *initPushBlock(void) s->isSolid = 1; s->startX = s->startY = -1; + + s->sprite[0] = s->sprite[1] = s->sprite[2] = getSprite("Crate1"); s->flags |= EF_EXPLODES | EF_ALWAYS_PROCESS; diff --git a/src/entities/structures/pushBlock.h b/src/entities/structures/pushBlock.h index 3664f5b..f77ae19 100644 --- a/src/entities/structures/pushBlock.h +++ b/src/entities/structures/pushBlock.h @@ -24,5 +24,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. extern void playSound(int snd, int ch); extern void addTeleportStars(Entity *e); extern Structure *createStructure(void); +extern Sprite *getSprite(char *name); extern Entity *self; diff --git a/src/entities/structures/structures.c b/src/entities/structures/structures.c index c84ff1d..e74af89 100644 --- a/src/entities/structures/structures.c +++ b/src/entities/structures/structures.c @@ -26,8 +26,6 @@ Structure *createStructure(void) s = malloc(sizeof(Structure)); memset(s, 0, sizeof(Structure)); - world.entityTail->next = (Entity*)s; - world.entityTail = (Entity*)s; initEntity((Entity*)s); diff --git a/src/entities/structures/teleporter.c b/src/entities/structures/teleporter.c index 30f4a5d..c69e8ff 100644 --- a/src/entities/structures/teleporter.c +++ b/src/entities/structures/teleporter.c @@ -34,6 +34,8 @@ Entity *initTeleporter(void) s->type = ET_TELEPORTER; + s->sprite[0] = s->sprite[1] = s->sprite[2] = getSprite("TeleporterActive"); + s->flags |= EF_WEIGHTLESS | EF_NO_CLIP | EF_IGNORE_BULLETS | EF_NO_TELEPORT; s->plane = PLANE_FOREGROUND; diff --git a/src/entities/structures/teleporter.h b/src/entities/structures/teleporter.h index 68ee13a..9220a78 100644 --- a/src/entities/structures/teleporter.h +++ b/src/entities/structures/teleporter.h @@ -28,5 +28,6 @@ extern void observeActivation(Entity *e); extern int isOnScreen(Entity *e); extern void setGameplayMessage(int type, char *format, ...); extern Structure *createStructure(void); +extern Sprite *getSprite(char *name); extern Entity *self; diff --git a/src/entities/unit.c b/src/entities/unit.c index 0bce343..24a6bc0 100644 --- a/src/entities/unit.c +++ b/src/entities/unit.c @@ -33,11 +33,11 @@ Unit *createUnit(void) u = malloc(sizeof(Unit)); memset(u, 0, sizeof(Unit)); - world.entityTail->next = (Entity*)u; - world.entityTail = (Entity*)u; initEntity((Entity*)u); + u->type = ET_ENEMY; + u->oxygen = MAX_OXYGEN; u->canCarryItem = rand() % 100 < 85; diff --git a/src/structs.h b/src/structs.h index a48e6ee..6886f2c 100644 --- a/src/structs.h +++ b/src/structs.h @@ -382,7 +382,7 @@ struct Sprite { char name[MAX_NAME_LENGTH]; int *times; char **filenames; - SDL_Rect *frames; + Atlas **frames; int currentFrame; float currentTime; int w; diff --git a/src/system/sprites.c b/src/system/sprites.c index ce9ab45..2003ad2 100644 --- a/src/system/sprites.c +++ b/src/system/sprites.c @@ -80,7 +80,7 @@ static void animateSprite(Sprite *s) SDL_Rect getCurrentFrame(Sprite *s) { - return s->frames[s->currentFrame]; + return s->frames[s->currentFrame]->rect; } static void loadGameSprites(void) @@ -144,6 +144,7 @@ void loadSprite(cJSON *root) s->times = malloc(sizeof(int) * s->numFrames); s->filenames = malloc(sizeof(char*) * s->numFrames); + s->frames = malloc(sizeof(Atlas*) * s->numFrames); i = 0; @@ -156,6 +157,8 @@ void loadSprite(cJSON *root) filename = cJSON_GetObjectItem(frame, "content")->valuestring; s->filenames[i] = malloc(strlen(filename) + 1); STRNCPY(s->filenames[i], filename, strlen(filename)); + + s->frames[i] = getImageFromAtlas(filename); } i++; diff --git a/src/system/sprites.h b/src/system/sprites.h index 81c6351..fca0ddb 100644 --- a/src/system/sprites.h +++ b/src/system/sprites.h @@ -24,3 +24,4 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. extern float wrap(float value, float low, float high); extern char *readFile(const char *filename); char **getFileList(const char *dir, int *count); +extern Atlas *getImageFromAtlas(char *filename); diff --git a/src/test/atlasTest.c b/src/test/atlasTest.c index 9182346..80fa2b7 100644 --- a/src/test/atlasTest.c +++ b/src/test/atlasTest.c @@ -37,6 +37,8 @@ void initAtlasTest(void) initMap(); + initEntities(); + timeout = FPS * 2; cameraTrack((Entity*)world.bob); @@ -53,6 +55,8 @@ static void logic(void) static void draw(void) { drawMap(); + + drawEntities(); } static void trackRandomEntity(void) diff --git a/src/test/atlasTest.h b/src/test/atlasTest.h index e9d6917..c4a69f5 100644 --- a/src/test/atlasTest.h +++ b/src/test/atlasTest.h @@ -27,8 +27,10 @@ extern void loadMapData(char *filename); extern void initMap(void); extern void initHub(void); extern void initGame(void); +extern void initEntities(void); extern void loadWorld(char *filename); extern void drawMap(void); +extern void drawEntities(void); extern void cameraTrack(Entity *e); extern App app; diff --git a/src/world/entities.c b/src/world/entities.c new file mode 100644 index 0000000..08e92ad --- /dev/null +++ b/src/world/entities.c @@ -0,0 +1,42 @@ +/* +Copyright (C) 2018 Parallel Realities + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + +See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +*/ + +#include "entities.h" + +static Texture *atlasTexture; + +void initEntities(void) +{ + atlasTexture = getTexture("gfx/atlas/atlas.png"); +} + +void drawEntities(void) +{ + Entity *e; + int x, y; + + for (e = world.entityHead.next ; e != NULL ; e = e->next) + { + x = (-camera.x + e->x); + y = (-camera.y + e->y); + + blitRect(atlasTexture->texture, x, y, &e->sprite[0]->frames[0]->rect, 0); + } +} diff --git a/src/world/entities.h b/src/world/entities.h new file mode 100644 index 0000000..5493117 --- /dev/null +++ b/src/world/entities.h @@ -0,0 +1,27 @@ +/* +Copyright (C) 2018 Parallel Realities + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + +See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +*/ + +#include "../common.h" + +extern Texture *getTexture(const char *filename); +extern void blitRect(SDL_Texture *texture, int x, int y, SDL_Rect *srcRect, int center); + +extern Camera camera; +extern World world; diff --git a/src/world/worldLoader.c b/src/world/worldLoader.c index 614cc26..f3a0ac7 100644 --- a/src/world/worldLoader.c +++ b/src/world/worldLoader.c @@ -126,16 +126,9 @@ static void loadSprites(cJSON *root) static void loadBob(cJSON *root) { - Bob *b; + world.bob = (Bob*)createEntity("Bob"); - b = (Bob*)createEntity("Bob"); - b->x = cJSON_GetObjectItem(root, "x")->valueint; - b->y = cJSON_GetObjectItem(root, "y")->valueint; - b->facing = lookup(cJSON_GetObjectItem(root, "facing")->valuestring); - - initBob(b); - - world.bob = b; + world.bob->load(root); } static void loadEntities(cJSON *root)