diff --git a/CMakeLists.txt b/CMakeLists.txt index 21c3314..6ebb589 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,6 +36,7 @@ add_custom_target(lint find_package(LibXml2 REQUIRED) find_package(SDL2 REQUIRED CONFIG REQUIRED COMPONENTS SDL2 SDL2main) +find_package(SDL2_image REQUIRED) add_subdirectory(engine) add_subdirectory(app) diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 8577634..f75e8a4 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -4,5 +4,9 @@ add_executable(game set_default_target_options(game) target_include_directories(game PUBLIC include) -target_link_libraries(game - PRIVATE LibXml2::LibXml2 SDL2::SDL2 SDL2::SDL2main) +target_link_libraries(game PRIVATE + LibXml2::LibXml2 + SDL2::SDL2 + SDL2::SDL2main + SDL2_image::SDL2_image +) diff --git a/app/main.c b/app/main.c index f562ccf..427d4ed 100644 --- a/app/main.c +++ b/app/main.c @@ -4,14 +4,15 @@ */ #include +#include #include #include #include #include #define TILESIZE 32 -#define WINWIDTH 30 -#define WINHEIGHT 20 +#define VIEWWIDTH 30 +#define VIEWHEIGHT 20 #define MAX_PATH_LEN 128 @@ -21,9 +22,13 @@ #define MAPSHIFTX 48 #define MAPSHIFTY 48 +#define TSASSET "/tileset.png" +#define TSCOLS 56 + static SDL_Window *window; static SDL_Renderer *renderer; static unsigned map[MAPWIDTH][MAPHEIGHT]; +static SDL_Texture *tstex; int main(int argc, char *argv[]) { @@ -39,7 +44,7 @@ int main(int argc, char *argv[]) assert(0 == err); window = SDL_CreateWindow( "2D Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, - TILESIZE * WINWIDTH, TILESIZE * WINHEIGHT, 0); + TILESIZE * VIEWWIDTH, TILESIZE * VIEWHEIGHT, 0); assert(NULL != window); renderer = SDL_CreateRenderer(window, -1, 0); assert(NULL != renderer); @@ -115,8 +120,40 @@ int main(int argc, char *argv[]) } } - SDL_Event event; + // Load tileset + assert(strlen(argv[1]) + strlen(TSASSET) < MAX_PATH_LEN); + strcpy(path, argv[1]); + strcat(path, TSASSET); + 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); + + SDL_Event event; while (1) { SDL_PollEvent(&event); switch (event.type) {