From 28e7cbabd931d08770302bff68bacf9a085e7ae0 Mon Sep 17 00:00:00 2001 From: Steve Date: Sun, 28 Jan 2018 12:01:39 +0000 Subject: [PATCH] Atlas loading and testing. --- common.mk | 11 +++++++---- src/main.c | 2 +- src/main.h | 1 + src/system/atlas.c | 46 ++++++++++++++++++++++++++++++++++--------- src/system/atlas.h | 3 +++ src/system/draw.c | 18 +++++++++++++++++ src/system/init.c | 3 ++- src/system/init.h | 1 + src/system/textures.c | 1 - src/test/atlasTest.c | 46 +++++++++++++++++++++++++++++++++++++++++++ src/test/atlasTest.h | 29 +++++++++++++++++++++++++++ src/util/util.h | 1 - src/world/particles.c | 1 - src/world/player.h | 1 - 14 files changed, 145 insertions(+), 19 deletions(-) create mode 100644 src/test/atlasTest.c create mode 100644 src/test/atlasTest.h diff --git a/common.mk b/common.mk index 389d062..224954d 100644 --- a/common.mk +++ b/common.mk @@ -13,9 +13,11 @@ SEARCHPATH += src/entities/items SEARCHPATH += src/entities/misc SEARCHPATH += src/entities/structures SEARCHPATH += src/entities/traps -SEARCHPATH += src/game -SEARCHPATH += src/hub -SEARCHPATH += src/system +SEARCHPATH += src/game +SEARCHPATH += src/hub +SEARCHPATH += src/json +SEARCHPATH += src/system +SEARCHPATH += src/test SEARCHPATH += src/util SEARCHPATH += src/widgets SEARCHPATH += src/world @@ -25,7 +27,7 @@ vpath %.h $(SEARCHPATH) DEPS += defs.h structs.h -OBJS += atlas.o +OBJS += atlas.o atlasTest.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 @@ -34,6 +36,7 @@ OBJS += fleshChunk.o frost.o OBJS += game.o OBJS += heart.o horizontalDoor.o horizontalLaserTrap.o hub.o hud.o OBJS += init.o infoPoint.o input.o io.o item.o items.o itemPad.o +OBJS += cJSON.o OBJS += key.o keycard.o OBJS += laserTrap.o lift.o lookup.o OBJS += main.o map.o maths.o mia.o diff --git a/src/main.c b/src/main.c index 5e4785b..06518f2 100644 --- a/src/main.c +++ b/src/main.c @@ -91,5 +91,5 @@ static void handleCommandLine(int argc, const char *argv[]) } - initTitle(); + initAtlasTest(); } diff --git a/src/main.h b/src/main.h index 8f89f2e..fd7afdb 100644 --- a/src/main.h +++ b/src/main.h @@ -28,6 +28,7 @@ extern void handleInput(void); extern void prepareScene(void); extern void presentScene(void); extern void initTitle(void); +extern void initAtlasTest(void); App app; Camera camera; diff --git a/src/system/atlas.c b/src/system/atlas.c index a8e89a4..5d7ba13 100644 --- a/src/system/atlas.c +++ b/src/system/atlas.c @@ -22,13 +22,12 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. static void loadAtlasTexture(void); static void loadAtlasData(void); +static void loadImageData(cJSON *root); static Atlas atlasHead; static Atlas *atlasTail; -/* -static Texture atlasTexture; +static Texture *atlasTexture; static int atlasSize; -*/ void initAtlas(void) { @@ -58,21 +57,50 @@ Atlas *getImageFromAtlas(char *filename) static void loadAtlasTexture(void) { + atlasTexture = getTexture("gfx/atlas/atlas.png"); + SDL_QueryTexture(atlasTexture->texture, NULL, NULL, &atlasSize, &atlasSize); } static void loadAtlasData(void) { + cJSON *root, *node; + char *text; + text = readFile("data/atlas/atlas.json"); + + root = cJSON_Parse(text); + + for (node = cJSON_GetObjectItem(root, "images")->child ; node != NULL ; node = node->next) + { + loadImageData(node); + } + + cJSON_Delete(root); + + free(text); } -void loadImageData(cJSON *root) +static void loadImageData(cJSON *root) { + Atlas *atlas; + char *filename; + int x, y, w, h; -} - -void createRectangle(char *filename, int x, int y, int w, int h) -{ + filename = cJSON_GetObjectItem(root, "filename")->valuestring; + x = cJSON_GetObjectItem(root, "x")->valueint; + y = cJSON_GetObjectItem(root, "y")->valueint; + w = cJSON_GetObjectItem(root, "w")->valueint; + h = cJSON_GetObjectItem(root, "h")->valueint; + atlas = malloc(sizeof(Atlas)); + memset(atlas, 0, sizeof(Atlas)); + atlasTail->next = atlas; + atlasTail = atlas; + + STRNCPY(atlas->filename, filename, MAX_FILENAME_LENGTH); + atlas->rect.x = x; + atlas->rect.y = y; + atlas->rect.w = w; + atlas->rect.h = h; } - diff --git a/src/system/atlas.h b/src/system/atlas.h index ce513f6..710798f 100644 --- a/src/system/atlas.h +++ b/src/system/atlas.h @@ -20,3 +20,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "../common.h" #include "../json/cJSON.h" + +extern Texture *getTexture(const char *filename); +extern char *readFile(const char *filename); diff --git a/src/system/draw.c b/src/system/draw.c index 223ac48..4e174ea 100644 --- a/src/system/draw.c +++ b/src/system/draw.c @@ -77,6 +77,24 @@ void blit(SDL_Texture *texture, int x, int y, int center) SDL_RenderCopy(app.renderer, texture, NULL, &dstRect); } +void blitRect(SDL_Texture *texture, int x, int y, SDL_Rect *srcRect, int center) +{ + SDL_Rect dstRect; + + dstRect.x = x; + dstRect.y = y; + dstRect.w = srcRect->w; + dstRect.h = srcRect->h; + + if (center) + { + dstRect.x -= (dstRect.w / 2); + dstRect.y -= (dstRect.h / 2); + } + + SDL_RenderCopy(app.renderer, texture, srcRect, &dstRect); +} + void blitFlip(SDL_Texture *texture, int x, int y, int center, SDL_RendererFlip flip) { SDL_Rect dstRect; diff --git a/src/system/init.c b/src/system/init.c index 86c1763..34cdc4b 100644 --- a/src/system/init.c +++ b/src/system/init.c @@ -69,7 +69,8 @@ void initGameSystem(void) void (*initFuncs[]) (void) = { initLookups, initGraphics, - initFonts + initFonts, + initAtlas }; numInitFuns = sizeof(initFuncs) / sizeof(void*); diff --git a/src/system/init.h b/src/system/init.h index d3ad3af..331f50b 100644 --- a/src/system/init.h +++ b/src/system/init.h @@ -28,6 +28,7 @@ extern void createSaveFolder(void); extern void initLookups(void); extern void initGraphics(void); extern void initFonts(void); +extern void initAtlas(void); extern void destroyLookups(void); extern void destroyFonts(void); extern void destroyTextures(void); diff --git a/src/system/textures.c b/src/system/textures.c index b2ffd36..f85660a 100644 --- a/src/system/textures.c +++ b/src/system/textures.c @@ -67,7 +67,6 @@ static Texture *loadTexture(const char *filename) return addTextureToCache(filename, texture); } - Texture *getTexture(const char *filename) { Texture *t; diff --git a/src/test/atlasTest.c b/src/test/atlasTest.c new file mode 100644 index 0000000..5df292a --- /dev/null +++ b/src/test/atlasTest.c @@ -0,0 +1,46 @@ +/* +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 "atlasTest.h" + +static void logic(void); +static void draw(void); + +static Atlas *testImage; +static Texture *atlasTexture; + +void initAtlasTest(void) +{ + app.delegate.logic = &logic; + app.delegate.draw = &draw; + + testImage = getImageFromAtlas("gfx/sprites/evilblobs/machineGunBlobRight1.png"); + + atlasTexture = getTexture("gfx/atlas/atlas.png"); +} + +static void logic(void) +{ +} + +static void draw(void) +{ + blitRect(atlasTexture->texture, 0, 0, &testImage->rect, 0); +} diff --git a/src/test/atlasTest.h b/src/test/atlasTest.h new file mode 100644 index 0000000..cf76c18 --- /dev/null +++ b/src/test/atlasTest.h @@ -0,0 +1,29 @@ +/* +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 Atlas *getImageFromAtlas(char *filename); +extern Texture *getTexture(const char *filename); +extern void blit(SDL_Texture *texture, int x, int y, int center); +extern void blitRect(SDL_Texture *texture, int x, int y, SDL_Rect *srcRect, int center); +extern void drawLine(int x1, int y1, int x2, int y2, int r, int g, int b, int a); + +extern App app; diff --git a/src/util/util.h b/src/util/util.h index c3b03ff..8ad0dc9 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -19,4 +19,3 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "../common.h" - diff --git a/src/world/particles.c b/src/world/particles.c index 1046dc6..cd4c15d 100644 --- a/src/world/particles.c +++ b/src/world/particles.c @@ -114,7 +114,6 @@ void addFlameParticles(float x, float y) p->destroyAfterAnim = 1; } - void addExplosionParticles(float x, float y, float radius, int amount) { int i; diff --git a/src/world/player.h b/src/world/player.h index c3b03ff..8ad0dc9 100644 --- a/src/world/player.h +++ b/src/world/player.h @@ -19,4 +19,3 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "../common.h" -