Create entity_t struct and define entityupdate() and entitydraw()
This commit is contained in:
parent
e7a19b7d91
commit
b39189b6d1
161
app/main.c
161
app/main.c
@ -62,14 +62,32 @@ typedef struct {
|
|||||||
bool left, right, up, down;
|
bool left, right, up, down;
|
||||||
} input_state_t;
|
} input_state_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int x, y;
|
||||||
|
} ivec_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
double x, y;
|
double x, y;
|
||||||
} dvec_t;
|
} dvec_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
dvec_t off, ext;
|
||||||
|
} dbox_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int svar, animlen;
|
||||||
|
double speed;
|
||||||
|
dvec_t pos, dir;
|
||||||
|
ivec_t animstep, svarstep;
|
||||||
|
dbox_t fbox;
|
||||||
|
SDL_Texture *tex;
|
||||||
|
SDL_Rect src;
|
||||||
|
} entity_t;
|
||||||
|
|
||||||
static SDL_Window *window;
|
static SDL_Window *window;
|
||||||
static SDL_Renderer *renderer;
|
static SDL_Renderer *renderer;
|
||||||
static unsigned map[MAPWIDTH][MAPHEIGHT];
|
static unsigned map[MAPWIDTH][MAPHEIGHT];
|
||||||
static SDL_Texture *tstex, *pidle, *pwalk, *ptex;
|
static SDL_Texture *tstex, *pidle, *pwalk;
|
||||||
static input_state_t input;
|
static input_state_t input;
|
||||||
static dvec_t vpos = { -128, -96 };
|
static dvec_t vpos = { -128, -96 };
|
||||||
static const unsigned impassable[] = { 284 };
|
static const unsigned impassable[] = { 284 };
|
||||||
@ -104,6 +122,58 @@ static inline bool tilepassable(int x, int y)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void entityupdate(entity_t *e, double dt)
|
||||||
|
{
|
||||||
|
if (0 == e->speed)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Update sprite variant
|
||||||
|
if (e->dir.y >= 0) {
|
||||||
|
if (e->dir.x > 0)
|
||||||
|
e->svar = SPRITE_DIR_RIGHT_DOWN;
|
||||||
|
else if (e->dir.x < 0)
|
||||||
|
e->svar = SPRITE_DIR_LEFT_DOWN;
|
||||||
|
else
|
||||||
|
e->svar = SPRITE_DIR_DOWN;
|
||||||
|
} else if (e->dir.y < 0) {
|
||||||
|
if (e->dir.x > 0)
|
||||||
|
e->svar = SPRITE_DIR_RIGHT_UP;
|
||||||
|
else if (e->dir.x < 0)
|
||||||
|
e->svar = SPRITE_DIR_LEFT_UP;
|
||||||
|
else
|
||||||
|
e->svar = SPRITE_DIR_UP;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply velocity (handling map collisions)
|
||||||
|
const double nextx = e->pos.x + dt * e->speed * e->dir.x;
|
||||||
|
const double nexty = e->pos.y + dt * e->speed * e->dir.y;
|
||||||
|
const dvec_t pfb
|
||||||
|
= { .x = nextx + e->fbox.off.x, .y = nexty + e->fbox.off.y };
|
||||||
|
bool valid = true;
|
||||||
|
valid &= tilepassable(pfb.x, pfb.y);
|
||||||
|
valid &= tilepassable(pfb.x + e->fbox.ext.x, pfb.y);
|
||||||
|
valid &= tilepassable(pfb.x, pfb.y + e->fbox.ext.y);
|
||||||
|
valid &= tilepassable(pfb.x + e->fbox.ext.x, pfb.y + e->fbox.ext.y);
|
||||||
|
if (valid) {
|
||||||
|
e->pos.x = nextx;
|
||||||
|
e->pos.y = nexty;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void entitydraw(entity_t *e, uint64_t t)
|
||||||
|
{
|
||||||
|
const unsigned frame = (t / BASEANIMPERIOD) % e->animlen;
|
||||||
|
e->src.x = e->animstep.x * frame + e->svarstep.x * e->svar;
|
||||||
|
e->src.y = e->animstep.y * frame + e->svarstep.y * e->svar;
|
||||||
|
SDL_Rect dest = {
|
||||||
|
.x = SCALE * (int)(e->pos.x - vpos.x - e->src.w / 2),
|
||||||
|
.y = SCALE * (int)(e->pos.y - vpos.y - e->src.h / 2),
|
||||||
|
.w = SCALE * e->src.w,
|
||||||
|
.h = SCALE * e->src.h,
|
||||||
|
};
|
||||||
|
SDL_RenderCopy(renderer, e->tex, &e->src, &dest);
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
char path[MAX_PATH_LEN];
|
char path[MAX_PATH_LEN];
|
||||||
@ -214,18 +284,26 @@ int main(int argc, char *argv[])
|
|||||||
assert(NULL != pwalk);
|
assert(NULL != pwalk);
|
||||||
|
|
||||||
// Initialize player
|
// Initialize player
|
||||||
SDL_Rect psrc = { .y = 0, .w = PWIDTH, .h = PHEIGHT };
|
entity_t p = {
|
||||||
SDL_Rect pdest = { .w = SCALE * PWIDTH, .h = SCALE * PHEIGHT };
|
.svar = SPRITE_DIR_DOWN,
|
||||||
dvec_t pvel = { 0, 0 }, ppos = { 0, 0 };
|
.animlen = PANIMLEN,
|
||||||
sprite_dir_t pdir = SPRITE_DIR_DOWN;
|
.tex = pidle,
|
||||||
ptex = pidle;
|
.animstep = { .x = PWIDTH },
|
||||||
|
.svarstep = { .y = PHEIGHT },
|
||||||
|
.fbox = {
|
||||||
|
.off = { .x = PFBOFFX, .y = PFBOFFY },
|
||||||
|
.ext = { .x = PFBWIDTH, .y = PFBHEIGHT },
|
||||||
|
},
|
||||||
|
.src = { .w = PWIDTH, .h = PHEIGHT },
|
||||||
|
};
|
||||||
|
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
uint64_t prevt = SDL_GetTicks64();
|
uint64_t prevt = SDL_GetTicks64();
|
||||||
while (1) {
|
while (1) {
|
||||||
// Calculate dt
|
// Calculate dt
|
||||||
const uint64_t t = SDL_GetTicks64();
|
const uint64_t t = SDL_GetTicks64();
|
||||||
const uint64_t dt = t - prevt;
|
const uint64_t dt_ms = t - prevt;
|
||||||
|
const double dt = dt_ms / 1000.0;
|
||||||
prevt = t;
|
prevt = t;
|
||||||
|
|
||||||
// Handle events
|
// Handle events
|
||||||
@ -277,58 +355,28 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Calculate and apply velocity
|
// Calculate and apply velocity
|
||||||
pvel.x = (input.left ? -1 : 0) + (input.right ? 1 : 0);
|
p.dir.x = (input.left ? -1 : 0) + (input.right ? 1 : 0);
|
||||||
pvel.y = (input.up ? -1 : 0) + (input.down ? 1 : 0);
|
p.dir.y = (input.up ? -1 : 0) + (input.down ? 1 : 0);
|
||||||
const double pspeed = mag(pvel);
|
const double dmag = mag(p.dir);
|
||||||
if (pspeed != 0) {
|
if (dmag != 0) {
|
||||||
pvel.x *= WALKSPEED / pspeed;
|
p.dir.x /= dmag;
|
||||||
pvel.y *= WALKSPEED / pspeed;
|
p.dir.y /= dmag;
|
||||||
|
p.tex = pwalk;
|
||||||
// Check for terrain collision, update position if none
|
p.speed = WALKSPEED;
|
||||||
const double nextx = ppos.x + (double)dt / 1000.0 * pvel.x;
|
|
||||||
const double nexty = ppos.y + (double)dt / 1000.0 * pvel.y;
|
|
||||||
const dvec_t pfb
|
|
||||||
= { .x = nextx + PFBOFFX, .y = nexty + PFBOFFY };
|
|
||||||
bool valid = true;
|
|
||||||
valid &= tilepassable(pfb.x, pfb.y);
|
|
||||||
valid &= tilepassable(pfb.x + PFBWIDTH, pfb.y);
|
|
||||||
valid &= tilepassable(pfb.x, pfb.y + PFBHEIGHT);
|
|
||||||
valid &= tilepassable(pfb.x + PFBWIDTH, pfb.y + PFBHEIGHT);
|
|
||||||
if (valid) {
|
|
||||||
ppos.x = nextx;
|
|
||||||
ppos.y = nexty;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
ptex = pwalk;
|
|
||||||
} else {
|
} else {
|
||||||
ptex = pidle;
|
p.tex = pidle;
|
||||||
|
p.speed = 0;
|
||||||
}
|
}
|
||||||
|
entityupdate(&p, dt);
|
||||||
|
|
||||||
// Update view
|
// Update view
|
||||||
const dvec_t pvdisp = {
|
const dvec_t pvdisp = {
|
||||||
.x = ppos.x - (vpos.x + VIEWWIDTH / 2),
|
.x = p.pos.x - (vpos.x + VIEWWIDTH / 2),
|
||||||
.y = ppos.y - (vpos.y + VIEWHEIGHT / 2),
|
.y = p.pos.y - (vpos.y + VIEWHEIGHT / 2),
|
||||||
};
|
};
|
||||||
if (mag(pvdisp) > 72 && dot(pvdisp, pvel) > 0) {
|
if (mag(pvdisp) > 72 && dot(pvdisp, p.dir) > 0) {
|
||||||
const double nextx = vpos.x + (double)dt / 1000.0 * pvel.x;
|
const double nextx = vpos.x + dt * p.speed * p.dir.x;
|
||||||
const double nexty = vpos.y + (double)dt / 1000.0 * pvel.y;
|
const double nexty = vpos.y + dt * p.speed * p.dir.y;
|
||||||
const bool validx = nextx >= MAPMINX && nextx < MAPMAXX;
|
const bool validx = nextx >= MAPMINX && nextx < MAPMAXX;
|
||||||
const bool validy = nexty >= MAPMINY && nexty < MAPMAXY;
|
const bool validy = nexty >= MAPMINY && nexty < MAPMAXY;
|
||||||
if (validx && validy) {
|
if (validx && validy) {
|
||||||
@ -364,12 +412,7 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Draw player
|
// Draw player
|
||||||
const unsigned piframe = (t / BASEANIMPERIOD) % PANIMLEN;
|
entitydraw(&p, t);
|
||||||
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, ptex, &psrc, &pdest);
|
|
||||||
|
|
||||||
SDL_RenderPresent(renderer);
|
SDL_RenderPresent(renderer);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user