Implement basic map drawing from tileset

This commit is contained in:
Camden Dixie O'Brien 2024-12-27 16:13:21 +00:00
parent 4151ca5717
commit fa7315d6b3
3 changed files with 48 additions and 6 deletions

View File

@ -36,6 +36,7 @@ add_custom_target(lint
find_package(LibXml2 REQUIRED) find_package(LibXml2 REQUIRED)
find_package(SDL2 REQUIRED CONFIG REQUIRED COMPONENTS SDL2 SDL2main) find_package(SDL2 REQUIRED CONFIG REQUIRED COMPONENTS SDL2 SDL2main)
find_package(SDL2_image REQUIRED)
add_subdirectory(engine) add_subdirectory(engine)
add_subdirectory(app) add_subdirectory(app)

View File

@ -4,5 +4,9 @@ add_executable(game
set_default_target_options(game) set_default_target_options(game)
target_include_directories(game PUBLIC include) target_include_directories(game PUBLIC include)
target_link_libraries(game target_link_libraries(game PRIVATE
PRIVATE LibXml2::LibXml2 SDL2::SDL2 SDL2::SDL2main) LibXml2::LibXml2
SDL2::SDL2
SDL2::SDL2main
SDL2_image::SDL2_image
)

View File

@ -4,14 +4,15 @@
*/ */
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <assert.h> #include <assert.h>
#include <ctype.h> #include <ctype.h>
#include <libxml/parser.h> #include <libxml/parser.h>
#include <string.h> #include <string.h>
#define TILESIZE 32 #define TILESIZE 32
#define WINWIDTH 30 #define VIEWWIDTH 30
#define WINHEIGHT 20 #define VIEWHEIGHT 20
#define MAX_PATH_LEN 128 #define MAX_PATH_LEN 128
@ -21,9 +22,13 @@
#define MAPSHIFTX 48 #define MAPSHIFTX 48
#define MAPSHIFTY 48 #define MAPSHIFTY 48
#define TSASSET "/tileset.png"
#define TSCOLS 56
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;
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
@ -39,7 +44,7 @@ int main(int argc, char *argv[])
assert(0 == err); assert(0 == err);
window = SDL_CreateWindow( window = SDL_CreateWindow(
"2D Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, "2D Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
TILESIZE * WINWIDTH, TILESIZE * WINHEIGHT, 0); TILESIZE * VIEWWIDTH, TILESIZE * VIEWHEIGHT, 0);
assert(NULL != window); assert(NULL != window);
renderer = SDL_CreateRenderer(window, -1, 0); renderer = SDL_CreateRenderer(window, -1, 0);
assert(NULL != renderer); 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); 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) { while (1) {
SDL_PollEvent(&event); SDL_PollEvent(&event);
switch (event.type) { switch (event.type) {