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 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/engine/draw_screen.c (limited to '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); +} -- 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(-) (limited to 'src/engine/draw_screen.c') 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 From 43951373604173c70bb2423dd56988b60a6704db Mon Sep 17 00:00:00 2001 From: UnavailableDev Date: Sun, 12 Mar 2023 12:58:51 +0100 Subject: camera movement --- src/engine/camera.c | 16 ++++++++++++++++ src/engine/camera.h | 6 ++++++ src/engine/draw_screen.c | 9 +++++---- src/engine/draw_screen.h | 2 +- src/engine/maths.c | 15 +++++++++++++++ src/engine/maths.h | 3 +++ src/engine/sprite_controller.c | 11 ++++++++++- src/engine/sprite_controller.h | 24 +++++++++++++----------- 8 files changed, 69 insertions(+), 17 deletions(-) create mode 100644 src/engine/camera.c create mode 100644 src/engine/camera.h create mode 100644 src/engine/maths.c (limited to 'src/engine/draw_screen.c') diff --git a/src/engine/camera.c b/src/engine/camera.c new file mode 100644 index 0000000..46c2d93 --- /dev/null +++ b/src/engine/camera.c @@ -0,0 +1,16 @@ +#include "engine/camera.h" + +#include "ppu/consts.h" + + +vec_cor hh_update_camera(vec_cen new, vec2 min, vec2 max){ + + new = vec_cen2cor(new,(vec2){.x=20,.y=30}); + static vec_cor old; + + old.x = CLAMP(new.x,min.x,max.x); + old.y = CLAMP(new.y,min.y,max.y); + + return old; +} + diff --git a/src/engine/camera.h b/src/engine/camera.h new file mode 100644 index 0000000..b3ffb52 --- /dev/null +++ b/src/engine/camera.h @@ -0,0 +1,6 @@ +#pragma once + +#include "engine/maths.h" + +vec_cor hh_update_camera(vec_cor new, vec2 min, vec2 max); + diff --git a/src/engine/draw_screen.c b/src/engine/draw_screen.c index acf5b41..0295241 100644 --- a/src/engine/draw_screen.c +++ b/src/engine/draw_screen.c @@ -1,4 +1,5 @@ -#include "draw_screen.h" +#include "engine/draw_screen.h" +#include "engine/sprite_controller.h" uint8_t hh_world_to_tile(vec2 pos){ @@ -37,8 +38,8 @@ void hh_draw_screen(vec_cor 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 */ + int size = 2400; // max X = 40 en max Y = 80 + FILE* level = fopen("../test/bin/level1_test.bin", "rb"); /* open binary file */ if (!level) { /* check if file opened successfully */ fprintf(stderr, "Error: Failed to open file.\n"); return; @@ -53,7 +54,7 @@ void hh_setup_screen(){ hh_ppu_update_background(BAM_index, (hh_s_ppu_loc_bam_entry){ .horizontal_flip = false, .vertical_flip = false, - .palette_index = tile[BAM_index]+1, + .palette_index = hh_get_palette(tile[BAM_index]), .tilemap_index = tile[BAM_index], }); } diff --git a/src/engine/draw_screen.h b/src/engine/draw_screen.h index 8d7df47..b181108 100644 --- a/src/engine/draw_screen.h +++ b/src/engine/draw_screen.h @@ -2,7 +2,7 @@ // every function call for drawing the screen goes here. -#include "../engine/maths.h" +#include "engine/maths.h" #include "ppu/ppu.h" #include diff --git a/src/engine/maths.c b/src/engine/maths.c new file mode 100644 index 0000000..ebd699c --- /dev/null +++ b/src/engine/maths.c @@ -0,0 +1,15 @@ +#include "engine/maths.h" + +vec_cor vec_cen2cor(vec_cen in, vec2 halfDistance){ + return (vec_cor){ + .x = in.x - halfDistance.x, + .y = in.y - halfDistance.y, + }; +} + +vec_cen vec_cor2cen(vec_cor in, vec2 halfDistance){ + return (vec_cen){ + .x = in.x + halfDistance.x, + .y = in.y + halfDistance.y, + }; +} diff --git a/src/engine/maths.h b/src/engine/maths.h index c7f1b44..bd20202 100644 --- a/src/engine/maths.h +++ b/src/engine/maths.h @@ -9,6 +9,9 @@ typedef struct { typedef vec2 vec_cen;//centered typedef vec2 vec_cor;//left upper corner +vec_cor vec_cen2cor(vec_cen in, vec2 halfDistance); +vec_cor vec_cor2cen(vec_cen in, vec2 halfDistance); + //fixed point at decimal 7lsb (world positions in pixels (with fixed decimal point)) #define HH_MATH_FIXED_POINT 7 diff --git a/src/engine/sprite_controller.c b/src/engine/sprite_controller.c index c224ff7..d4c44ab 100644 --- a/src/engine/sprite_controller.c +++ b/src/engine/sprite_controller.c @@ -4,7 +4,16 @@ // #include "engine/maths.h" #include "ppu/types.h" #include "ppu/consts.h" +#include "ppu/ppu.h" -uint8_t hh_get_palette(uint16_t tile_idx) { +uint8_t hh_get_palette(uint8_t tile_idx) { return hh_g_sprite_palette[tile_idx]; } + +void hh_setup_palettes(){ + for (int idx = 0; idx < HH_PPU_PALETTE_COUNT; idx++) { + for (int col = 0; col < HH_PPU_PALETTE_COLOR_COUNT; col++) { + hh_ppu_update_color(idx,col,hh_g_palette[idx][col]); + } + } +} diff --git a/src/engine/sprite_controller.h b/src/engine/sprite_controller.h index 47ab0af..001a459 100644 --- a/src/engine/sprite_controller.h +++ b/src/engine/sprite_controller.h @@ -11,10 +11,10 @@ //TODO: pack data inside of sprite_palette LUT //HH_PPU_PALETTE_COUNT -#define HH_SPRITE_COUNT 32 +#define HH_SPRITE_COUNT 40 #define HH_PAL_IDX_SKY 0 #define HH_PAL_IDX_BRICK 1 -uint8_t hh_g_sprite_palette[HH_SPRITE_COUNT] = { +const static uint8_t hh_g_sprite_palette[HH_SPRITE_COUNT] = { 0,1,1,1,1,1,1,1,1,1, //1+9 1,1,1,1,1,1,1,1,1,1, //6+4 1,1,1,1,1,1,1,1,1, //9 @@ -23,7 +23,7 @@ uint8_t hh_g_sprite_palette[HH_SPRITE_COUNT] = { }; -hh_ppu_loc_palette_table_t hh_g_palette = { +const static hh_ppu_loc_palette_table_t hh_g_palette = { {//palette info here {0x1,0x2,0x3}, {0x0,0x0,0x0}, @@ -88,17 +88,19 @@ hh_ppu_loc_palette_table_t hh_g_palette = { {0x0,0x0,0x0}, {0x0,0x0,0x0}}, { + {0x0,0xf,0xf}, {0xf,0xf,0xf}, - {0xf,0xf,0xf}, - {0x0,0x0,0x0}, - {0x0,0x0,0x0}, - {0x0,0x0,0x0}, - {0x0,0x0,0x0}, - {0x0,0x0,0x0}, + {0xf,0x0,0xf}, + {0xf,0xf,0x0}, + {0xf,0x0,0x0}, + {0x0,0xf,0x0}, + {0x0,0x0,0xf}, {0x0,0x0,0x0}} }; +void hh_setup_palettes(); + /** @brief return palette index that belongs to tilemap index */ -uint8_t hh_get_palette(uint16_t tile_idx); +uint8_t hh_get_palette(uint8_t tile_idx); -bool hh_colidable(uint16_t tile_idx); +bool hh_colidable(uint8_t tile_idx); -- cgit v1.2.3 From 5c4bf7d451135cd3af3e51976251f01cbb6e6573 Mon Sep 17 00:00:00 2001 From: UnavailableDev Date: Mon, 13 Mar 2023 09:57:34 +0100 Subject: camera, movement, collisions --- src/engine/TODO/player_controller.h | 4 ---- src/engine/camera.c | 20 +++++++++++++++++- src/engine/draw_screen.c | 18 ++++++++-------- src/engine/entity.c | 41 +++++++++++++++++++++---------------- src/engine/entity.h | 6 ++++-- src/engine/maths.c | 4 ++++ src/engine/maths.h | 4 +++- src/engine/player_controller.c | 1 + src/engine/player_controller.h | 7 +++++++ src/engine/sprite_controller.c | 5 ++++- src/makefile | 1 + src/ppusim/input.c | 1 + src/ppusim/sim.c | 2 +- 13 files changed, 77 insertions(+), 37 deletions(-) delete mode 100644 src/engine/TODO/player_controller.h create mode 100644 src/engine/player_controller.c create mode 100644 src/engine/player_controller.h (limited to 'src/engine/draw_screen.c') diff --git a/src/engine/TODO/player_controller.h b/src/engine/TODO/player_controller.h deleted file mode 100644 index 1e9b86c..0000000 --- a/src/engine/TODO/player_controller.h +++ /dev/null @@ -1,4 +0,0 @@ -#include "maths.h" -#include "hh_entity.h" - -// inputs diff --git a/src/engine/camera.c b/src/engine/camera.c index 46c2d93..e756bd4 100644 --- a/src/engine/camera.c +++ b/src/engine/camera.c @@ -5,8 +5,26 @@ vec_cor hh_update_camera(vec_cen new, vec2 min, vec2 max){ - new = vec_cen2cor(new,(vec2){.x=20,.y=30}); + //TODO: change floating point math to fix point math + //TODO: fix buggy y-axis ?? + + // new = vec_cen2cor(new,(vec2){.x=max.x/2,.y=max.y/2}); + new = vec_cen2cor((vec2){.x=new.x+(HH_PPU_SPRITE_WIDTH/2),.y=(new.y+(HH_PPU_SPRITE_HEIGHT/2))*2},(vec2){.x=max.x/2,.y=max.y/2}); + // new.x = new.x << HH_MATH_FIXED_POINT; + // new.y = new.y << HH_MATH_FIXED_POINT; static vec_cor old; + // old.x = old.x << HH_MATH_FIXED_POINT; + // old.y = old.y << HH_MATH_FIXED_POINT; + + // int16_t some = 0; + // some = some <<= HH_MATH_FIXED_POINT-1; + + new.x = (int)((float)new.x*0.1f + (float)old.x*0.9f); + new.y = (int)((float)new.y*0.1f + (float)old.y*0.9f); + + // old.x = old.x >> HH_MATH_FIXED_POINT; + // old.y = old.y >> HH_MATH_FIXED_POINT; + old.x = CLAMP(new.x,min.x,max.x); old.y = CLAMP(new.y,min.y,max.y); diff --git a/src/engine/draw_screen.c b/src/engine/draw_screen.c index 0295241..c4f3389 100644 --- a/src/engine/draw_screen.c +++ b/src/engine/draw_screen.c @@ -3,20 +3,20 @@ uint8_t hh_world_to_tile(vec2 pos){ - FILE* level = fopen("../test/bin/test_map.bin", "rb"); /* open binary file */ + FILE* level = fopen("../test/bin/level1_test.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); + int index = ((pos.y/16)*40 + pos.x/16);//TODO: remove magic number(s) 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 + 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; + // int val = tile; + // free(tile); + return tile; } @@ -26,8 +26,8 @@ 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){ - .bg_shift_x = viewport.x*HH_PPU_SPRITE_WIDTH, - .bg_shift_y = viewport.y*HH_PPU_SPRITE_HEIGHT, + .bg_shift_x = viewport.x, + .bg_shift_y = viewport.y, .fg_fetch = 0, .sysreset = 0, }); diff --git a/src/engine/entity.c b/src/engine/entity.c index 1dc1df8..152cf1d 100644 --- a/src/engine/entity.c +++ b/src/engine/entity.c @@ -1,6 +1,6 @@ #include -#include "engine/hh_entity.h" +#include "engine/entity.h" #include "engine/maths.h" /* @@ -12,30 +12,35 @@ */ -bool hh_collision(vec2* pos1, vec2* pos2){ - if (pos2->x == CLAMP(pos2->x,pos1->x,pos1->x+1.0f)){// hit x +bool hh_collision(vec_cor pos1, vec2 pos2){ + if (pos2.x == CLAMP(pos2.x, pos1.x, pos1.x+16)){// hit x return true; } - if (pos2->y == CLAMP(pos2->y,pos1->y,pos1->y+0.99f)){// hit y + if (pos2.y == CLAMP(pos2.y, pos1.y, pos1.y+16)){// hit y return true; } return false; } -void hh_solve_collision(vec2* pos_environment, hh_entity* entity){ - if (entity->vec.x > 0.0f){ - entity->pos.x = MIN(entity->pos.x,pos_environment->x-1.0f); - entity->vec.x = 0.0f; - } else if (entity->vec.x < 0.0f){ - entity->pos.x = MAX(entity->pos.x,pos_environment->x+1.0f); - entity->vec.x = 0.0f; - } else if (entity->vec.y > 0.0f){ - entity->pos.x = MIN(entity->pos.x,pos_environment->x-1.0f); - entity->vec.x = 0.0f; - } else if (entity->vec.y < 0.0f){ - entity->pos.x = MAX(entity->pos.x,pos_environment->x+1.0f); - entity->vec.x = 0.0f; - } +void hh_solve_collision(vec2 pos_environment, hh_entity* entity){ + if (!hh_collision(pos_environment,entity->pos)) + return; + + printf("BONK!/n"); + // if (entity->vec.y > 0){ + // entity->pos.y = MAX(entity->pos.y,pos_environment.y); + // entity->vec.y = 0; + // } else { + // entity->pos.y = MIN(entity->pos.y,pos_environment.y); + // entity->vec.y = 0; + // } + // if (entity->vec.x <= 0){ + // entity->pos.x = MIN(entity->pos.x,pos_environment.x-16); + // entity->vec.x = 0; + // } else { + // entity->pos.x = MAX(entity->pos.x,pos_environment.x+16); + // entity->vec.x = 0; + // } } diff --git a/src/engine/entity.h b/src/engine/entity.h index b2a0c49..dee4aed 100644 --- a/src/engine/entity.h +++ b/src/engine/entity.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include "engine/maths.h" @@ -8,6 +9,7 @@ typedef struct { vec2 pos, vec; bool is_grounded; int8_t hp; + int8_t speed; //armor/block? }hh_entity; @@ -15,10 +17,10 @@ typedef struct { /// @param pos1 position of environment tile to be checked /// @param pos2 position entity /// @return true if collision between enity and environment -bool hh_collision(vec2* pos1, vec2* pos2); +bool hh_collision(vec2 pos1, vec2 pos2); /// @brief solve collisions /// @param environment position /// @param entity position /// @return solved new entity position -void hh_solve_collision(vec2* pos_environment, hh_entity* entity); +void hh_solve_collision(vec2 pos_environment, hh_entity* entity); diff --git a/src/engine/maths.c b/src/engine/maths.c index ebd699c..475bba2 100644 --- a/src/engine/maths.c +++ b/src/engine/maths.c @@ -1,5 +1,9 @@ #include "engine/maths.h" +vec2 vec_add(vec2 a, vec2 b){ + return (vec2){a.x + b.x, a.y + b.y}; +} + vec_cor vec_cen2cor(vec_cen in, vec2 halfDistance){ return (vec_cor){ .x = in.x - halfDistance.x, diff --git a/src/engine/maths.h b/src/engine/maths.h index bd20202..bef287e 100644 --- a/src/engine/maths.h +++ b/src/engine/maths.h @@ -3,12 +3,14 @@ // #include typedef struct { - uint32_t x,y; + int32_t x,y; } vec2; typedef vec2 vec_cen;//centered typedef vec2 vec_cor;//left upper corner +vec2 vec_add(vec2 a, vec2 b); + vec_cor vec_cen2cor(vec_cen in, vec2 halfDistance); vec_cor vec_cor2cen(vec_cen in, vec2 halfDistance); diff --git a/src/engine/player_controller.c b/src/engine/player_controller.c new file mode 100644 index 0000000..27e522e --- /dev/null +++ b/src/engine/player_controller.c @@ -0,0 +1 @@ +#include "engine/player_controller.h" diff --git a/src/engine/player_controller.h b/src/engine/player_controller.h new file mode 100644 index 0000000..400c18e --- /dev/null +++ b/src/engine/player_controller.h @@ -0,0 +1,7 @@ +#pragma once + +#include "engine/maths.h" +#include "engine/entity.h" +// inputs + +void hh_player_actions(); diff --git a/src/engine/sprite_controller.c b/src/engine/sprite_controller.c index d4c44ab..5d93cf8 100644 --- a/src/engine/sprite_controller.c +++ b/src/engine/sprite_controller.c @@ -1,7 +1,6 @@ #include #include "engine/sprite_controller.h" -// #include "engine/maths.h" #include "ppu/types.h" #include "ppu/consts.h" #include "ppu/ppu.h" @@ -17,3 +16,7 @@ void hh_setup_palettes(){ } } } + +bool hh_colidable(uint8_t tile_idx){ + return (hh_get_palette(tile_idx) != 0); +} diff --git a/src/makefile b/src/makefile index 142d918..d7d9087 100644 --- a/src/makefile +++ b/src/makefile @@ -33,6 +33,7 @@ LOCAL_SRCS += main.c \ demo.c \ engine/engine.c \ engine/sprite_controller.c \ + engine/player_controller.c \ engine/draw_screen.c \ engine/camera.c \ engine/maths.c \ diff --git a/src/ppusim/input.c b/src/ppusim/input.c index 08bc382..5323fb1 100644 --- a/src/ppusim/input.c +++ b/src/ppusim/input.c @@ -12,4 +12,5 @@ void hh_input_read() { g_hh_controller_p1.dpad_down = kb[SDL_SCANCODE_S]; g_hh_controller_p1.dpad_left = kb[SDL_SCANCODE_A]; g_hh_controller_p1.dpad_right = kb[SDL_SCANCODE_D]; + g_hh_controller_p1.button_primary = kb[SDL_SCANCODE_SPACE]; } diff --git a/src/ppusim/sim.c b/src/ppusim/sim.c index 1e7e609..a5fec45 100644 --- a/src/ppusim/sim.c +++ b/src/ppusim/sim.c @@ -32,7 +32,7 @@ void hh_ppu_load_tilemap() { char* filename = "../test/bin/tiles.bin"; FILE* fp = fopen(filename,"rb"); if (!fp){ - fprintf(stderr,"File error!"); + fprintf(stderr,"Error: Failed to load tiles."); return;//error } int sprite_size = (HH_PPU_SPRITE_WIDTH * HH_PPU_SPRITE_HEIGHT); -- cgit v1.2.3