From cd9d3141626e7be29b69da15b989d23eb63e46f0 Mon Sep 17 00:00:00 2001 From: UnavailableDev Date: Mon, 3 Apr 2023 10:06:00 +0200 Subject: sprite rendering, collision fix --- src/engine/animator.c | 35 ++++++++++++++++++++++++++++---- src/engine/animator.h | 5 ++--- src/engine/entity.c | 12 +++++------ src/engine/maths.h | 1 + src/engine/player_controller.c | 45 +++++++++++++++++++++++++++--------------- 5 files changed, 69 insertions(+), 29 deletions(-) (limited to 'src/engine') diff --git a/src/engine/animator.c b/src/engine/animator.c index 887493c..58c50d8 100644 --- a/src/engine/animator.c +++ b/src/engine/animator.c @@ -1,6 +1,8 @@ #include "engine/animator.h" #include "engine/entity.h" #include "engine/maths.h" +#include "ppu/consts.h" +#include "ppu/ppu.h" #define hh_white_palette 6 @@ -13,7 +15,7 @@ void hh_animate_hit(hh_s_rendering* in, bool hit) { } } -void hh_animate(hh_s_rendering* in, hh_idx_t start, hh_idx_t end, uint8_t step) { +void hh_animate(hh_s_rendering* in, uint16_t start, uint16_t end, uint8_t step) { if (in->fam.palette_index >= start && in->fam.palette_index < end) { in->fam.palette_index += step; } else {// rollover @@ -21,7 +23,32 @@ void hh_animate(hh_s_rendering* in, hh_idx_t start, hh_idx_t end, uint8_t step) } } -void hh_update_sprite(hh_entity* in) { - hh_animate_hit(&in->render, in->is_hit); - +uint16_t hh_update_sprite(uint16_t idx, hh_entity* in, vec_cor cam) { + hh_animate_hit(&in->render, in->is_hit); + hh_s_ppu_loc_fam_entry temp = in->render.fam; + for (int y = 0; y < CEILI(in->size.y,16); y++) { + temp.position_y = CLAMP(in->pos.y - cam.y + y*HH_PPU_SPRITE_HEIGHT, -16, HH_PPU_SCREEN_HEIGHT); + for (int x = 0; x < CEILI(in->size.x,16); x++) { + temp.position_x = CLAMP(in->pos.x - cam.x + x*HH_PPU_SPRITE_WIDTH, -16, HH_PPU_SCREEN_WIDTH); + hh_ppu_update_foreground(++idx, temp); + temp.tilemap_index++; + } + } + return idx; } + + +// void hh_update_sprite(hh_entity* in, vec_cor cam) { +// hh_animate_hit(&in->render, in->is_hit); +// // uint16_t idx = in->render.ppu_foreground_index; +// uint16_t idx = 0; +// hh_s_ppu_loc_fam_entry temp = in->render.fam; +// for (int y = 0; y < CEILI(in->size.y,16); y++) { +// temp.position_y = CLAMP(in->pos.y - cam.y + y*HH_PPU_SPRITE_HEIGHT, -16, HH_PPU_SCREEN_HEIGHT); +// for (int x = 0; x < CEILI(in->size.x,16); x++) { +// temp.position_x = CLAMP(in->pos.x - cam.x + x*HH_PPU_SPRITE_WIDTH, -16, HH_PPU_SCREEN_WIDTH); +// hh_ppu_update_foreground(idx++, temp); +// temp.tilemap_index++; +// } +// } +// } diff --git a/src/engine/animator.h b/src/engine/animator.h index 3b015a6..10fa321 100644 --- a/src/engine/animator.h +++ b/src/engine/animator.h @@ -8,8 +8,7 @@ /** @brief flashes sprite white, also needs to be called next frame */ void hh_animate_hit(hh_s_rendering*, bool hit); /** @brief updates current animation frame */ -void hh_animate(hh_s_rendering*, hh_idx_t start, hh_idx_t end, uint8_t step); +void hh_animate(hh_s_rendering*, uint16_t start, uint16_t end, uint8_t step); /** @brief passively updates sprite*/ -void hh_update_sprite(hh_entity* in); - +uint16_t hh_update_sprite(uint16_t idx, hh_entity* in, vec_cor cam); diff --git a/src/engine/entity.c b/src/engine/entity.c index b374f8c..58b62c9 100644 --- a/src/engine/entity.c +++ b/src/engine/entity.c @@ -50,13 +50,13 @@ hh_entity hh_background_collision (hh_entity temp_old_entity,hh_entity temp_new_ // solves x collision if (temp_old_entity.vel.x <= 0) { if (hh_colidable(hh_world_to_tile((vec2){.x=temp_new_entity.pos.x + 0, .y=temp_old_entity.pos.y + 0})) || - hh_colidable(hh_world_to_tile((vec2){.x=temp_new_entity.pos.x + 0, .y=temp_old_entity.pos.y + 15}))) { + hh_colidable(hh_world_to_tile((vec2){.x=temp_new_entity.pos.x + 0, .y=temp_old_entity.pos.y + (temp_old_entity.size.y-1)}))) { temp_new_entity.pos.x = (temp_new_entity.pos.x & ~15) + 16, temp_new_entity.vel.x = 0; } } else { - if (hh_colidable(hh_world_to_tile((vec2){.x=temp_new_entity.pos.x + 16, .y=temp_old_entity.pos.y + 0})) || - hh_colidable(hh_world_to_tile((vec2){.x=temp_new_entity.pos.x + 16, .y=temp_old_entity.pos.y + 15}))) { + if (hh_colidable(hh_world_to_tile((vec2){.x=temp_new_entity.pos.x + temp_old_entity.size.x, .y=temp_old_entity.pos.y + 0})) || + hh_colidable(hh_world_to_tile((vec2){.x=temp_new_entity.pos.x + temp_old_entity.size.x, .y=temp_old_entity.pos.y + (temp_old_entity.size.y-1)}))) { temp_new_entity.pos.x = temp_new_entity.pos.x & ~15, // <-- magic comma, NOT TOUCHY temp_new_entity.vel.x = 0; } @@ -65,13 +65,13 @@ hh_entity hh_background_collision (hh_entity temp_old_entity,hh_entity temp_new_ //solves y collision if (temp_old_entity.vel.y <= 0) { if (hh_colidable(hh_world_to_tile((vec2){.x=temp_new_entity.pos.x + 0, .y=temp_new_entity.pos.y + 0})) || - hh_colidable(hh_world_to_tile((vec2){.x=temp_new_entity.pos.x + 15, .y=temp_new_entity.pos.y + 0}))) { + hh_colidable(hh_world_to_tile((vec2){.x=temp_new_entity.pos.x + (temp_new_entity.size.x-1), .y=temp_new_entity.pos.y + 0}))) { temp_new_entity.pos.y = (temp_new_entity.pos.y & ~15) + 16, temp_new_entity.vel.y = 0; } } else { - if (hh_colidable(hh_world_to_tile((vec2){.x=temp_new_entity.pos.x + 0, .y=temp_new_entity.pos.y + 15})) || - hh_colidable(hh_world_to_tile((vec2){.x=temp_new_entity.pos.x + 15, .y=temp_new_entity.pos.y + 15}))) { + if (hh_colidable(hh_world_to_tile((vec2){.x=temp_new_entity.pos.x + 0, .y=temp_new_entity.pos.y + (temp_new_entity.size.y-1)})) || + hh_colidable(hh_world_to_tile((vec2){.x=temp_new_entity.pos.x + (temp_new_entity.size.x-1), .y=temp_new_entity.pos.y + (temp_new_entity.size.y-1)}))) { temp_new_entity.pos.y = temp_new_entity.pos.y & ~15, temp_new_entity.vel.y = 0; temp_new_entity.is_grounded = true; diff --git a/src/engine/maths.h b/src/engine/maths.h index bef287e..6f66042 100644 --- a/src/engine/maths.h +++ b/src/engine/maths.h @@ -20,3 +20,4 @@ vec_cor vec_cor2cen(vec_cen in, vec2 halfDistance); #define MIN(a,b) (((a)<(b))?(a):(b)) #define MAX(a,b) (((a)>(b))?(a):(b)) #define CLAMP(N,LOWER,UPPER) (MIN(MAX(LOWER, N), UPPER)) +#define CEILI(numerator, denominator) (numerator / denominator + (numerator % denominator != 0)) diff --git a/src/engine/player_controller.c b/src/engine/player_controller.c index 5027655..7d2847e 100644 --- a/src/engine/player_controller.c +++ b/src/engine/player_controller.c @@ -17,11 +17,13 @@ void hh_player_actions() { .is_grounded = false, .is_hit = false, .radius = 8, - .pos = (vec2){32,32}, + .pos = (vec2){128+16,32}, .vel = (vec2){0,0}, + .size = (vec2){32,32}, .render = { .frame0 = 80, .palette = 3, + .ppu_foreground_index = 0, .fam = (hh_s_ppu_loc_fam_entry){ .horizontal_flip = false, .vertical_flip = false, @@ -40,9 +42,11 @@ void hh_player_actions() { .radius = 8, .pos = (vec2){128,48}, .vel = (vec2){0,0}, + .size = (vec2){16,16}, .render = { .frame0 = 20, .palette = 7, + .ppu_foreground_index = 16, .fam = (hh_s_ppu_loc_fam_entry){ .horizontal_flip = false, .vertical_flip = false, @@ -155,28 +159,37 @@ void hh_player_actions() { vec_cor cam_pos;//value in tiles cam_pos = hh_update_camera(player.pos,(vec2){0,0},(vec2){.x=20*16,.y=30*16});//TODO: remove magic number(s) - hh_update_sprite(&player); + uint16_t idx = 16; + idx = hh_update_sprite(idx, &player, cam_pos); + idx = hh_update_sprite(idx, &enemy, cam_pos); hh_draw_screen(cam_pos); + idx =16; + // TODO: make this a function call // update player sprite on ppu - player.render.fam.position_x = (player.pos.x-cam_pos.x); - player.render.fam.position_y = (player.pos.y-cam_pos.y); + // player.render.fam.position_x = (player.pos.x-cam_pos.x); + // player.render.fam.position_y = (player.pos.y-cam_pos.y); + + // enemy.render.fam.position_x = (enemy.pos.x-cam_pos.x); + // enemy.render.fam.position_y = (enemy.pos.y-cam_pos.y); - enemy.render.fam.position_x = (enemy.pos.x-cam_pos.x); - enemy.render.fam.position_y = (enemy.pos.y-cam_pos.y); // TODO: make this loop a function call - for (int i = 0; i < 4; i++) - { - hh_s_ppu_loc_fam_entry temp = player.render.fam; - temp.position_x = player.render.fam.position_x+(!(player.vel.x>0)?-1:1)*(i%2?8:-8); - temp.position_y = player.render.fam.position_y+(i>1?0:-16); - temp.tilemap_index = player.render.fam.tilemap_index + i; - temp.palette_index = player.render.fam.palette_index; - temp.horizontal_flip = !(player.vel.x>0); - hh_ppu_update_foreground(i,temp); - } + // for (int i = 0; i < 4; i++) + // { + // hh_s_ppu_loc_fam_entry temp = player.render.fam; + // temp.position_x = player.render.fam.position_x+(!(player.vel.x>0)?-1:1)*(i%2?8:-8); + // temp.position_y = player.render.fam.position_y+(i>1?0:-16); + // temp.tilemap_index = player.render.fam.tilemap_index + i; + // temp.horizontal_flip = !(player.vel.x>0); + // hh_ppu_update_foreground(i,temp); + + // // hh_s_ppu_loc_fam_entry temp = { + // // .position_x = player.render.fam.position_x+(!(player.vel.x>0)?-1:1)*(i%2?8:-8) + // // }; + + // } hh_ppu_update_foreground(4, enemy.render.fam); -- cgit v1.2.3