diff --git a/app/main.c b/app/main.c index 427d4ed..2b60e71 100644 --- a/app/main.c +++ b/app/main.c @@ -10,9 +10,11 @@ #include #include +#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,34 +136,24 @@ 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; - SDL_RenderClear(renderer); - for (int y = 0; y < VIEWHEIGHT; ++y) { - for (int x = 0; x < VIEWWIDTH; ++x) { - const unsigned tileid - = map[x + offx + MAPSHIFTX][y + offy + MAPSHIFTY]; - if (0 == tileid) - continue; - const SDL_Rect src = { - .x = TILESIZE * ((tileid - 1) % TSCOLS), - .y = TILESIZE * ((tileid - 1) / TSCOLS), - .w = TILESIZE, - .h = TILESIZE, - }; - const SDL_Rect dest = { - .x = TILESIZE * x, - .y = TILESIZE * y, - .w = TILESIZE, - .h = TILESIZE, - }; - SDL_RenderCopy(renderer, tstex, &src, &dest); - } - } - SDL_RenderPresent(renderer); + // 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: @@ -163,6 +162,44 @@ int main(int argc, char *argv[]) default: break; } + + SDL_RenderClear(renderer); + + // Draw map + for (int y = 0; y < VIEWHEIGHT; ++y) { + for (int x = 0; x < VIEWWIDTH; ++x) { + const unsigned tileid + = map[x + offx + MAPSHIFTX][y + offy + MAPSHIFTY]; + if (0 == tileid) + continue; + const SDL_Rect src = { + .x = TILESIZE * ((tileid - 1) % TSCOLS), + .y = TILESIZE * ((tileid - 1) / TSCOLS), + .w = TILESIZE, + .h = TILESIZE, + }; + const SDL_Rect dest = { + .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); } quit: