Render player with animated sprite
This commit is contained in:
parent
fa7315d6b3
commit
39e8002caf
79
app/main.c
79
app/main.c
@ -10,9 +10,11 @@
|
||||
#include <libxml/parser.h>
|
||||
#include <string.h>
|
||||
|
||||
#define SCALE 4
|
||||
|
||||
#define TILESIZE 32
|
||||
#define VIEWWIDTH 30
|
||||
#define VIEWHEIGHT 20
|
||||
#define VIEWWIDTH 8
|
||||
#define VIEWHEIGHT 6
|
||||
|
||||
#define MAX_PATH_LEN 128
|
||||
|
||||
@ -25,10 +27,17 @@
|
||||
#define TSASSET "/tileset.png"
|
||||
#define TSCOLS 56
|
||||
|
||||
#define PIASSET "/player/idle/down.png"
|
||||
#define PIWIDTH 48
|
||||
#define PIHEIGHT 64
|
||||
#define PIANIMLEN 8
|
||||
|
||||
#define BASEANIMPERIOD 200
|
||||
|
||||
static SDL_Window *window;
|
||||
static SDL_Renderer *renderer;
|
||||
static unsigned map[MAPWIDTH][MAPHEIGHT];
|
||||
static SDL_Texture *tstex;
|
||||
static SDL_Texture *tstex, *pitex;
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
@ -44,7 +53,7 @@ int main(int argc, char *argv[])
|
||||
assert(0 == err);
|
||||
window = SDL_CreateWindow(
|
||||
"2D Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
|
||||
TILESIZE * VIEWWIDTH, TILESIZE * VIEWHEIGHT, 0);
|
||||
SCALE * TILESIZE * VIEWWIDTH, SCALE * TILESIZE * VIEWHEIGHT, 0);
|
||||
assert(NULL != window);
|
||||
renderer = SDL_CreateRenderer(window, -1, 0);
|
||||
assert(NULL != renderer);
|
||||
@ -127,9 +136,36 @@ int main(int argc, char *argv[])
|
||||
tstex = IMG_LoadTexture(renderer, path);
|
||||
assert(NULL != tstex);
|
||||
|
||||
// Render map -- no motion yet so just needs to be done once
|
||||
int offx = 0, offy = 0;
|
||||
// Load player idle spritesheet
|
||||
assert(strlen(argv[1]) + strlen(PIASSET) < MAX_PATH_LEN);
|
||||
strcpy(path, argv[1]);
|
||||
strcat(path, PIASSET);
|
||||
pitex = IMG_LoadTexture(renderer, path);
|
||||
assert(NULL != pitex);
|
||||
|
||||
int offx = 16, offy = 0;
|
||||
const SDL_Rect pdest = {
|
||||
.x = SCALE * 80,
|
||||
.y = SCALE * 80,
|
||||
.w = SCALE * PIWIDTH,
|
||||
.h = SCALE * PIHEIGHT,
|
||||
};
|
||||
|
||||
SDL_Event event;
|
||||
while (1) {
|
||||
// Handle events
|
||||
SDL_PollEvent(&event);
|
||||
switch (event.type) {
|
||||
case SDL_QUIT:
|
||||
goto quit;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
// Draw map
|
||||
for (int y = 0; y < VIEWHEIGHT; ++y) {
|
||||
for (int x = 0; x < VIEWWIDTH; ++x) {
|
||||
const unsigned tileid
|
||||
@ -143,26 +179,27 @@ int main(int argc, char *argv[])
|
||||
.h = TILESIZE,
|
||||
};
|
||||
const SDL_Rect dest = {
|
||||
.x = TILESIZE * x,
|
||||
.y = TILESIZE * y,
|
||||
.w = TILESIZE,
|
||||
.h = TILESIZE,
|
||||
.x = SCALE * TILESIZE * x,
|
||||
.y = SCALE * TILESIZE * y,
|
||||
.w = SCALE * TILESIZE,
|
||||
.h = SCALE * TILESIZE,
|
||||
};
|
||||
SDL_RenderCopy(renderer, tstex, &src, &dest);
|
||||
}
|
||||
}
|
||||
|
||||
// Draw player
|
||||
const unsigned piframe
|
||||
= (SDL_GetTicks64() / BASEANIMPERIOD) % PIANIMLEN;
|
||||
const SDL_Rect psrc = {
|
||||
.x = PIWIDTH * piframe,
|
||||
.y = 0,
|
||||
.w = PIWIDTH,
|
||||
.h = PIHEIGHT,
|
||||
};
|
||||
SDL_RenderCopy(renderer, pitex, &psrc, &pdest);
|
||||
|
||||
SDL_RenderPresent(renderer);
|
||||
|
||||
SDL_Event event;
|
||||
while (1) {
|
||||
SDL_PollEvent(&event);
|
||||
switch (event.type) {
|
||||
case SDL_QUIT:
|
||||
goto quit;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
quit:
|
||||
|
Loading…
x
Reference in New Issue
Block a user