From f3f382fe4f3a6b074be6fead547f149cafc113d9 Mon Sep 17 00:00:00 2001 From: Juan Manuel Tomas Date: Thu, 13 Jan 2022 09:58:22 -0300 Subject: Create tiled background and foreground --- background.bmp | Bin 0 -> 196662 bytes build.sh | 4 ++-- foreground.bmp | Bin 0 -> 196662 bytes main.c | 51 ++++++++++++++++++++++++++++++++++++++++++++------- test-map.bmp | Bin 2764854 -> 0 bytes 5 files changed, 46 insertions(+), 9 deletions(-) create mode 100644 background.bmp create mode 100644 foreground.bmp delete mode 100644 test-map.bmp diff --git a/background.bmp b/background.bmp new file mode 100644 index 0000000..ae47c4b Binary files /dev/null and b/background.bmp differ diff --git a/build.sh b/build.sh index d4db25e..850e2a2 100755 --- a/build.sh +++ b/build.sh @@ -1,3 +1,3 @@ -gcc -c main.c -o main.o -gcc main.o -lSDL2 -o main +gcc -Wall -pedantic -O0 -c main.c -o main.o && \ +gcc main.o -lSDL2 -o main && \ ./main diff --git a/foreground.bmp b/foreground.bmp new file mode 100644 index 0000000..da6b5d7 Binary files /dev/null and b/foreground.bmp differ diff --git a/main.c b/main.c index 0d14b64..0f5f4c7 100644 --- a/main.c +++ b/main.c @@ -1,14 +1,39 @@ #include #include +#define WINDOW_W 1280 +#define WINDOW_H 720 + +SDL_Texture *createTiledTexture(SDL_Renderer *context, const char *filename) { + SDL_Surface *texture_surface = SDL_LoadBMP(filename); + SDL_Texture *texture_tile = SDL_CreateTextureFromSurface(context, texture_surface); + int tile_w = texture_surface->w; + int tile_h = texture_surface->h; + SDL_FreeSurface(texture_surface); + + Uint32 pixelformat; + SDL_QueryTexture(texture_tile, &pixelformat, 0, 0, 0); + SDL_Texture *texture = SDL_CreateTexture(context, pixelformat, SDL_TEXTUREACCESS_STATIC | SDL_TEXTUREACCESS_TARGET, WINDOW_W, WINDOW_H); + SDL_SetRenderTarget(context, texture); + for (int y = 0; y < WINDOW_H; y += tile_h) { + for (int x = 0; x < WINDOW_W; x += tile_w) { + SDL_Rect tile_rect = { x, y, tile_w, tile_h }; + SDL_RenderCopy(context, texture_tile, 0, &tile_rect); + } + } + SDL_DestroyTexture(texture_tile); + SDL_SetRenderTarget(context, 0); + return texture; +} + int main() { SDL_Init(SDL_INIT_VIDEO); SDL_Window *window = SDL_CreateWindow("Cannons", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, - 1280, - 720, + WINDOW_W, + WINDOW_H, 0); SDL_Renderer *context = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); @@ -16,13 +41,25 @@ int main() { SDL_SetRenderDrawColor(context, 40, 40, 40, 255); SDL_RenderClear(context); - SDL_Surface *map_surface = SDL_LoadBMP("test-map.bmp"); - SDL_Texture *map = SDL_CreateTextureFromSurface(context, map_surface); - SDL_RenderCopy(context, map, 0, 0); + SDL_Texture *background = createTiledTexture(context, "background.bmp"); + SDL_Texture *foreground = createTiledTexture(context, "foreground.bmp"); + + // TODO: + // - Map has a mask generated by the game, of where the terrain (foregroud) exists - SDL_RenderPresent(context); + int exit = 0; + while (!exit) { + SDL_Event event = {0}; + while (SDL_PollEvent(&event)) { + if (event.type == SDL_QUIT) { + exit = 1; + } + } + SDL_RenderCopy(context, foreground, 0, 0); - SDL_Delay(1000); + SDL_RenderPresent(context); + SDL_Delay(10); + } SDL_DestroyRenderer(context); SDL_DestroyWindow(window); diff --git a/test-map.bmp b/test-map.bmp deleted file mode 100644 index 7632ee6..0000000 Binary files a/test-map.bmp and /dev/null differ -- cgit v1.2.3