diff options
author | NielsCoding <101340368+NielsCoding@users.noreply.github.com> | 2023-03-11 19:46:57 +0100 |
---|---|---|
committer | NielsCoding <101340368+NielsCoding@users.noreply.github.com> | 2023-03-11 19:46:57 +0100 |
commit | 557cbed3ab2dea1dafd61ccdc3521aeed9b63a43 (patch) | |
tree | c8f6126b849e536c8ff7090205375f37475f192c /src/engine/draw_screen.c | |
parent | f4e4b53fe3ef96a7bd2d4052c5a181f05945a0aa (diff) |
drawing map en read from binary complete. shift to background.
Diffstat (limited to 'src/engine/draw_screen.c')
-rw-r--r-- | src/engine/draw_screen.c | 61 |
1 files changed, 61 insertions, 0 deletions
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); +} |