From 9942429dbb83fed5532c070f8afe41d6ddcd66d2 Mon Sep 17 00:00:00 2001 From: zlago Date: Wed, 25 Sep 2024 14:02:49 +0200 Subject: parallax --- src/main.c | 47 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 9 deletions(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index a304797..bf23ee0 100644 --- a/src/main.c +++ b/src/main.c @@ -9,6 +9,9 @@ #include "tilemap.h" +#include "incbin.h" +#include "libplum.h" + SDL_Window *window = NULL; SDL_Renderer *renderer = NULL; @@ -37,7 +40,7 @@ int main(int argc, char **argv) { if (window == NULL) { goto end; } - renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE| SDL_RENDERER_TARGETTEXTURE | SDL_RENDERER_PRESENTVSYNC); + renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_TARGETTEXTURE | SDL_RENDERER_PRESENTVSYNC); if (renderer == NULL) { goto end; } @@ -46,7 +49,7 @@ int main(int argc, char **argv) { { res_init_texture(); res_init_map(); - init_tilemap(); + //init_tilemap(); void *a = util_executableRelativePath("assets.res", *argv, 0); puts(a); if (loadResources(a)) { @@ -55,10 +58,28 @@ int main(int argc, char **argv) { } free(a); struct blob map = res_get_map("out"); - printf("load_tilemap %u\n", load_tilemap(map.data, map.size)); + tilemap = load_tilemap(map.data, map.size); + printf("load_tilemap %p\n", tilemap); SDL_SetRenderTarget(renderer, NULL); } + unsigned error; + struct plum_image *image = plum_load_image(game_icon, game_icon_end - game_icon, PLUM_COLOR_32 | PLUM_ALPHA_INVERT, &error); + if (image == NULL) { + fprintf(stderr, "error: libplum: %s\n", plum_get_error_text(error)); + goto icon_done; + } + SDL_Surface *surface = SDL_CreateRGBSurfaceFrom(image->data, image->width, image->height, 32, image->width * 4, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000); + if (surface == NULL) { + plum_destroy_image(image); + fprintf(stderr, "error: SDL2: %s\n", SDL_GetError()); + goto icon_done; + } + SDL_SetWindowIcon(window, surface); + SDL_FreeSurface(surface); + plum_destroy_image(image); + icon_done: + SDL_ShowWindow(window); int x = 0, y = 0; @@ -118,8 +139,11 @@ int main(int argc, char **argv) { break; case SDL_WINDOWEVENT_MOVED: // a window is moved //fprintf(stderr, "window %u moved %4i %4i\n", evt.window.windowID, evt.window.data1, evt.window.data2); - //wx = evt.window.data1; - //wy = evt.window.data2; + break; + case SDL_WINDOWEVENT_RESIZED: // a window is resized + //fprintf(stderr, "window %u resized %4i %4i\n", evt.window.windowID, evt.window.data1, evt.window.data2); + break; + case SDL_WINDOWEVENT_SIZE_CHANGED: // ? break; case SDL_WINDOWEVENT_MINIMIZED: //fprintf(stderr, "window %u minimized\n", evt.window.windowID); @@ -159,18 +183,23 @@ int main(int argc, char **argv) { SDL_SetRenderDrawColor(renderer, 0, 128, 255, 0); SDL_RenderFillRect(renderer, &(SDL_Rect) {0, 0, WINDOW_WIDTH, WINDOW_HEIGHT}); SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE); - //SDL_RenderCopy(renderer, tilemap_tileset, &(SDL_Rect) {0, 0, 128, 90}, &(SDL_Rect) {0, 0, 128, 90}); - SDL_RenderCopy(renderer, tilemap_tilemap, &(SDL_Rect) {x, y, WINDOW_WIDTH, WINDOW_HEIGHT}, &(SDL_Rect) {0, 0, WINDOW_WIDTH, WINDOW_HEIGHT}); + x += input_right(input_now) - input_left(input_now); y += input_down(input_now) - input_up(input_now); - if (x < 0) {x = 0;} else if (x + WINDOW_WIDTH > 29 * 8) {x = 29 * 8 - WINDOW_WIDTH;} - if (y < 0) {y = 0;} else if (y + WINDOW_HEIGHT > 19 * 8) {y = 19 * 8 - WINDOW_HEIGHT;} + if (x < 0) {x = 0;} else if (x + WINDOW_WIDTH > tilemap->width * 8) {x = tilemap->width * 8 - WINDOW_WIDTH;} + if (y < 0) {y = 0;} else if (y + WINDOW_HEIGHT > tilemap->height * 8) {y = tilemap->height * 8 - WINDOW_HEIGHT;} + + //SDL_RenderCopy(renderer, tilemap->wang_tileset, &(SDL_Rect) {0, 0, 128, 90}, &(SDL_Rect) {0, 0, 128, 90}); + for (int i = 0; i < tilemap->layers; i++) { + SDL_RenderCopy(renderer, tilemap->tilemaps[i], &(SDL_Rect) {x * tilemap->parallax[i].x, y * tilemap->parallax[i].y, WINDOW_WIDTH, WINDOW_HEIGHT}, &(SDL_Rect) {0, 0, WINDOW_WIDTH, WINDOW_HEIGHT}); + } //SDL_RenderCopy(renderer, res_get_texture("meow").data, &(SDL_Rect) {0, 0, 128, 90}, &(SDL_Rect) {0, 0, 128, 90}); // then we wait for the next video frame SDL_RenderPresent(renderer); } end: + free_tilemap(tilemap), tilemap = NULL; SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); //SDL_QuitSubSystem(INIT_SUBSYSTEMS); -- cgit 1.4.1-2-gfad0