Use directional sprites for player
This commit is contained in:
parent
10bb223bdd
commit
c5daa136c1
50
app/main.c
50
app/main.c
@ -29,15 +29,24 @@
|
||||
#define TSASSET "/tileset.png"
|
||||
#define TSCOLS 56
|
||||
|
||||
#define PIASSET "/player/idle/down.png"
|
||||
#define PIWIDTH 48
|
||||
#define PIHEIGHT 64
|
||||
#define PIANIMLEN 8
|
||||
#define PIDLE_ASSET "/player/idle.png"
|
||||
#define PWIDTH 48
|
||||
#define PHEIGHT 64
|
||||
#define PANIMLEN 8
|
||||
|
||||
#define WALKSPEED 72 // pixels per second
|
||||
|
||||
#define BASEANIMPERIOD 200
|
||||
|
||||
typedef enum {
|
||||
SPRITE_DIR_DOWN = 0,
|
||||
SPRITE_DIR_LEFT_DOWN = 1,
|
||||
SPRITE_DIR_LEFT_UP = 2,
|
||||
SPRITE_DIR_UP = 3,
|
||||
SPRITE_DIR_RIGHT_UP = 4,
|
||||
SPRITE_DIR_RIGHT_DOWN = 5,
|
||||
} sprite_dir_t;
|
||||
|
||||
typedef struct {
|
||||
bool left, right, up, down;
|
||||
} input_state_t;
|
||||
@ -49,7 +58,7 @@ typedef struct {
|
||||
static SDL_Window *window;
|
||||
static SDL_Renderer *renderer;
|
||||
static unsigned map[MAPWIDTH][MAPHEIGHT];
|
||||
static SDL_Texture *tstex, *pitex;
|
||||
static SDL_Texture *tstex, *pidle;
|
||||
static input_state_t input;
|
||||
|
||||
static inline double mag(dvec_t v)
|
||||
@ -155,17 +164,18 @@ int main(int argc, char *argv[])
|
||||
assert(NULL != tstex);
|
||||
|
||||
// Load player idle spritesheet
|
||||
assert(strlen(argv[1]) + strlen(PIASSET) < MAX_PATH_LEN);
|
||||
assert(strlen(argv[1]) + strlen(PIDLE_ASSET) < MAX_PATH_LEN);
|
||||
strcpy(path, argv[1]);
|
||||
strcat(path, PIASSET);
|
||||
pitex = IMG_LoadTexture(renderer, path);
|
||||
assert(NULL != pitex);
|
||||
strcat(path, PIDLE_ASSET);
|
||||
pidle = IMG_LoadTexture(renderer, path);
|
||||
assert(NULL != pidle);
|
||||
|
||||
// Initialize view and player
|
||||
dvec_t vpos = { 512, 0 };
|
||||
SDL_Rect psrc = { .y = 0, .w = PIWIDTH, .h = PIHEIGHT };
|
||||
SDL_Rect pdest = { .w = SCALE * PIWIDTH, .h = SCALE * PIHEIGHT };
|
||||
SDL_Rect psrc = { .y = 0, .w = PWIDTH, .h = PHEIGHT };
|
||||
SDL_Rect pdest = { .w = SCALE * PWIDTH, .h = SCALE * PHEIGHT };
|
||||
dvec_t pvel = { 0, 0 }, ppos = { 640, 96 };
|
||||
sprite_dir_t pdir = SPRITE_DIR_DOWN;
|
||||
|
||||
SDL_Event event;
|
||||
uint64_t prevt = SDL_GetTicks64();
|
||||
@ -232,6 +242,23 @@ int main(int argc, char *argv[])
|
||||
pvel.y *= WALKSPEED / pspeed;
|
||||
ppos.x += (double)dt / 1000.0 * pvel.x;
|
||||
ppos.y += (double)dt / 1000.0 * pvel.y;
|
||||
|
||||
// Update direction
|
||||
if (pvel.y >= 0) {
|
||||
if (pvel.x > 0)
|
||||
pdir = SPRITE_DIR_RIGHT_DOWN;
|
||||
else if (pvel.x < 0)
|
||||
pdir = SPRITE_DIR_LEFT_DOWN;
|
||||
else
|
||||
pdir = SPRITE_DIR_DOWN;
|
||||
} else if (pvel.y < 0) {
|
||||
if (pvel.x > 0)
|
||||
pdir = SPRITE_DIR_RIGHT_UP;
|
||||
else if (pvel.x < 0)
|
||||
pdir = SPRITE_DIR_LEFT_UP;
|
||||
else
|
||||
pdir = SPRITE_DIR_UP;
|
||||
}
|
||||
}
|
||||
|
||||
// Update view
|
||||
@ -277,6 +304,7 @@ int main(int argc, char *argv[])
|
||||
// Draw player
|
||||
const unsigned piframe = (t / BASEANIMPERIOD) % PANIMLEN;
|
||||
psrc.x = PWIDTH * piframe;
|
||||
psrc.y = PHEIGHT * pdir;
|
||||
pdest.x = SCALE * (int)(ppos.x - vpos.x - PWIDTH / 2);
|
||||
pdest.y = SCALE * (int)(ppos.y - vpos.y - PHEIGHT / 2);
|
||||
SDL_RenderCopy(renderer, pidle, &psrc, &pdest);
|
||||
|
Loading…
x
Reference in New Issue
Block a user