From 557cbed3ab2dea1dafd61ccdc3521aeed9b63a43 Mon Sep 17 00:00:00 2001 From: NielsCoding <101340368+NielsCoding@users.noreply.github.com> Date: Sat, 11 Mar 2023 19:46:57 +0100 Subject: drawing map en read from binary complete. shift to background. --- src/engine/draw_screen.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ src/engine/draw_screen.h | 18 +++++++++++--- src/makefile | 3 ++- 3 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 src/engine/draw_screen.c diff --git a/src/engine/draw_screen.c b/src/engine/draw_screen.c new file mode 100644 index 0000000..3e4d8c4 --- /dev/null +++ b/src/engine/draw_screen.c @@ -0,0 +1,61 @@ +#include "draw_screen.h" + +uint8_t hh_world_to_tile(vec2 pos){ + + FILE* level = fopen("../test/bin/test_map.bin", "rb"); /* open binary file */ + if (!level) { /* check if file opened successfully */ + fprintf(stderr, "Error: Failed to open file.\n"); + return 0; + } + int index = (pos.y + pos.x); + fseek(level, (index * sizeof(int)) + sizeof(int), SEEK_SET); + int* tile = (int*)malloc(sizeof(int)); + fread(tile, sizeof(int), 1, level); // read 1 tile from binary + + fclose(level); + int val = *tile; + free(tile); + return val; +} + + +// remeber old value to know which part to update. +vec2 previousViewport = { .x = 0, .y = 0 }; +void hh_draw_screen(vec2 viewport){ + if (viewport.x == previousViewport.x && viewport.y == previousViewport.y) return; + + hh_ppu_update_aux((hh_s_ppu_loc_aux){ + .bg_shift_x = viewport.x*HH_PPU_SPRITE_WIDTH, + .bg_shift_y = viewport.y*HH_PPU_SPRITE_HEIGHT, + .fg_fetch = 0, + .sysreset = 0, + }); + + // update previous viewport values + previousViewport = viewport; +} + +void hh_setup_screen(){ + //(HH_map_size_X*HH_map_size_Y) + int size = 3200; // max X = 40 en max Y = 80 + FILE* level = fopen("../test/bin/test_map.bin", "rb"); /* open binary file */ + if (!level) { /* check if file opened successfully */ + fprintf(stderr, "Error: Failed to open file.\n"); + return; + } + fseek(level, (0* sizeof(int)) + sizeof(int), SEEK_SET); + int* tile = (int*)malloc(size*sizeof(int)); + fread(tile, sizeof(int), size, level); // read 1 tile from binary + + fclose(level); + + for(int BAM_index = 0; BAM_index < size; BAM_index++){ + hh_ppu_update_background(BAM_index, (hh_s_ppu_loc_bam_entry){ + .horizontal_flip = false, + .vertical_flip = false, + .palette_index = tile[BAM_index]+1, + .tilemap_index = tile[BAM_index], + }); + } + free(tile); +} diff --git a/src/engine/draw_screen.h b/src/engine/draw_screen.h index 4af5865..8d7df47 100644 --- a/src/engine/draw_screen.h +++ b/src/engine/draw_screen.h @@ -2,8 +2,20 @@ // every function call for drawing the screen goes here. -#include "engine/maths.h" +#include "../engine/maths.h" +#include "ppu/ppu.h" +#include #include -uint16_t hh_world_to_tile(vec2 pos); -void hh_draw_screen(vec2 viewport); \ No newline at end of file +#include + + +#define HH_map_size_X 80 +#define HH_map_size_Y 60 + +/** @brief return a single tile from world binary */ +uint8_t hh_world_to_tile(vec2 pos); +/** @brief shift to level if viewport changed position */ +void hh_draw_screen(vec2 viewport); +/** @brief send data to BAM memory from binary level */ +void hh_setup_screen(); diff --git a/src/makefile b/src/makefile index 96751fb..96f3108 100644 --- a/src/makefile +++ b/src/makefile @@ -31,7 +31,8 @@ LOCAL_SRCS += main.c \ ppu/internals.c \ ppu/ppu.c \ demo.c \ - engine/engine.c + engine/engine.c \ + engine/draw_screen.c CFLAGS += $(SHARED_FLAGS) LFLAGS += $(SHARED_FLAGS) -- cgit v1.2.3 From c74b9b2ff60db2ddc7fe50686b6b654ceea3896e Mon Sep 17 00:00:00 2001 From: UnavailableDev <69792062+UnavailableDev@users.noreply.github.com> Date: Sun, 12 Mar 2023 11:38:48 +0100 Subject: syntax update --- src/engine/draw_screen.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/engine/draw_screen.c b/src/engine/draw_screen.c index 3e4d8c4..acf5b41 100644 --- a/src/engine/draw_screen.c +++ b/src/engine/draw_screen.c @@ -21,7 +21,7 @@ uint8_t hh_world_to_tile(vec2 pos){ // remeber old value to know which part to update. vec2 previousViewport = { .x = 0, .y = 0 }; -void hh_draw_screen(vec2 viewport){ +void hh_draw_screen(vec_cor viewport){ if (viewport.x == previousViewport.x && viewport.y == previousViewport.y) return; hh_ppu_update_aux((hh_s_ppu_loc_aux){ -- cgit v1.2.3