Atlas loading and testing.

This commit is contained in:
Steve 2018-01-28 12:01:39 +00:00
parent 0bbff1cb85
commit 28e7cbabd9
14 changed files with 145 additions and 19 deletions

View File

@ -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

View File

@ -91,5 +91,5 @@ static void handleCommandLine(int argc, const char *argv[])
}
initTitle();
initAtlasTest();
}

View File

@ -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;

View File

@ -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;
}

View File

@ -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);

View File

@ -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;

View File

@ -69,7 +69,8 @@ void initGameSystem(void)
void (*initFuncs[]) (void) = {
initLookups,
initGraphics,
initFonts
initFonts,
initAtlas
};
numInitFuns = sizeof(initFuncs) / sizeof(void*);

View File

@ -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);

View File

@ -67,7 +67,6 @@ static Texture *loadTexture(const char *filename)
return addTextureToCache(filename, texture);
}
Texture *getTexture(const char *filename)
{
Texture *t;

46
src/test/atlasTest.c Normal file
View File

@ -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);
}

29
src/test/atlasTest.h Normal file
View File

@ -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;

View File

@ -19,4 +19,3 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "../common.h"

View File

@ -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;

View File

@ -19,4 +19,3 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "../common.h"