From bf8c5fdb43ae2446a502227a95f17167c3dcda33 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Fri, 10 Mar 2023 13:12:02 +0100 Subject: add input emulation to test collision --- src/demo.c | 153 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 148 insertions(+), 5 deletions(-) (limited to 'src/demo.c') diff --git a/src/demo.c b/src/demo.c index 019750a..c4e4d7a 100644 --- a/src/demo.c +++ b/src/demo.c @@ -2,10 +2,36 @@ #include "demo.h" #include "ppu/ppu.h" +#include "input.h" +#include "entity.h" -#define HH_DEMO_BALL_COUNT 5 +#define HH_DEMO_BALL_COUNT 1 hh_s_ppu_loc_fam_entry g_hh_demo_balls[HH_DEMO_BALL_COUNT]; +hh_s_entity_player g_hh_player_1 = { + .pos_x = 31000, //0b0000 0001 0011 0110 + .pos_y = 21000, + .radius = 8, + .speed = 1, + .direction_x = 1, + .rotation = 8, + .in_air = false, +}; + +void playerMovement(); + +uint16_t pos_x; //0b0000 0001 0011 0110 +uint16_t pos_y; +uint8_t left = 0; +uint8_t right = 0; +uint8_t up = 0; +uint8_t down = 0; +uint8_t pos_x_bit[2]; +uint8_t pos_y_bit[2]; +uint8_t data_send[3]; +int tileX; +int tileY; + void hh_demo_setup() { // load sprites hh_ppu_update_sprite(0, HH_DBG_SPRITE_BALL); @@ -39,6 +65,22 @@ void hh_demo_setup() { } void hh_demo_loop(unsigned long frame) { + playerMovement(); + + //adjust map size + pos_x = g_hh_player_1.pos_x / 100; + pos_y = g_hh_player_1.pos_y / 100; + + // input testing (no hitbox stuff) + // pos_x += (-1 * g_hh_controller_p1.dpad_left) + (1 * g_hh_controller_p1.dpad_right); // -1 = L || 1 == R + // pos_y += (-1 * g_hh_controller_p1.dpad_up) + (1 * g_hh_controller_p1.dpad_down); // -1 = D || 1 == U + + // update player sprite on ppu + g_hh_demo_balls[0].position_x = pos_x; + g_hh_demo_balls[0].position_y = pos_y; + hh_ppu_update_foreground(0, g_hh_demo_balls[0]); + + // set background pattern position hh_ppu_update_aux((hh_s_ppu_loc_aux) { .bg_shift_x = (frame / 2) % HH_PPU_SPRITE_WIDTH, @@ -46,10 +88,111 @@ void hh_demo_loop(unsigned long frame) { .fg_fetch = 0, .sysreset = 0, }); +} + +// void sendData(uint8_t address, uint16_t data) { +// uint8_t bitData[3]; +// bitData[2] = data & 0xff; +// bitData[1] = (data >> 8); +// bitData[0] = address; // first byte is address +// +// HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_RESET); +// HAL_SPI_Transmit(&hspi1, bitData, 3, 100); //2*8 bit data +// HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_SET); +// } + +void playerMovement() { + int8_t directionX = (-1 * g_hh_controller_p1.dpad_left) + (1 * g_hh_controller_p1.dpad_right); // -1 = L || 1 == R + int8_t directionY = (-1 * g_hh_controller_p1.dpad_down) + (1 * g_hh_controller_p1.dpad_up); // -1 = D || 1 == U + + uint8_t i,j; + uint8_t rotation = 0; // 0-7 + + //rotation calc + for(i = -1; i < 2;i++) + { + for(j = -1; j < 2; j++) + { + if(directionX == i) + { + if(directionY == j) + { + if(i != 0 && j != 0) //dont update when player idle + { + g_hh_player_1.rotation = rotation; + } + } + } + rotation++; + } + } + //direction calc + if(directionX != 0) //update direction if player is not idle + { + g_hh_player_1.direction_x = directionX; + } + //collision map x-axis + + //tile calc including radius and direction for background coliision + + uint16_t tileColX; + uint16_t tileColY = ( g_hh_player_1.pos_y / 100) / 16; ; + + // remaining space between grid and exact + uint8_t modTileX; + uint8_t modTileY; + + if(g_hh_player_1.in_air == false && directionX != 0) + { + if(directionX == 1) + { + tileColX = ( ( g_hh_player_1.pos_x / 100) + g_hh_player_1.radius ) / 20; + modTileX = ( g_hh_player_1.pos_x + ( 100 * g_hh_player_1.radius ) ) % 2000; + } + else if(directionX == -1) + { + tileColX = ( ( g_hh_player_1.pos_x / 100) - g_hh_player_1.radius ) / 20; + modTileX = ( g_hh_player_1.pos_x - ( 100 * g_hh_player_1.radius ) ) % 2000; + } + + if(HH_DEMO_HITBOX_TILEMAP[tileColY][tileColX + directionX] != 1) + { + g_hh_player_1.pos_x = g_hh_player_1.pos_x + (directionX * g_hh_player_1.speed); // NEW x set + } + + else if(HH_DEMO_HITBOX_TILEMAP[tileColY][tileColX + directionX] == 1) + { + if(modTileX < g_hh_player_1.speed) + { + g_hh_player_1.pos_x = g_hh_player_1.pos_x + (directionX * modTileX); // NEW x set + } + else + { + g_hh_player_1.pos_x = g_hh_player_1.pos_x + (directionX * g_hh_player_1.speed); // NEW x set + } + } + + } + else //if in air different all borders have to be checked + { - for (unsigned i = 0; i < HH_DEMO_BALL_COUNT; i++) { - g_hh_demo_balls[i].position_x = HH_PPU_SCREEN_WIDTH/2 - HH_PPU_SPRITE_WIDTH/2 + (int)(60 * (double)sin((1*(double)frame / 10) + (double)i * 12)); - g_hh_demo_balls[i].position_y = HH_PPU_SCREEN_HEIGHT/2 - HH_PPU_SPRITE_HEIGHT/2 + (int)(30 * (double)sin((2*(double)frame / 10) + (double)i * 12)); - hh_ppu_update_foreground(i, g_hh_demo_balls[i]); } + + //collision map floor (y-axis) (falling) + // if falling no jump press (implement) + /* + tileColY = (( g_hh_player_1.pos_y / 100) + g_hh_player_1.radius) / 16; //bottom of player box + modTileY = 1; + if(HH_DEMO_HITBOX_TILEMAP[tileColY+1][tileColX] != 1) //rework after jumping + { + g_hh_player_1.pos_y = g_hh_player_1.pos_y + 5 ;// NEW y set //makew var gravity + //playerStat = falling; //for later use of graphics/sound + } + */ + //else if(HH_DEMO_HITBOX_TILEMAP[]) + + + + + } -- cgit v1.2.3 From c37397234534e9888dfeed9b1728c46646c33b58 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Fri, 10 Mar 2023 13:16:16 +0100 Subject: `clang-format` --- src/demo.c | 182 +++++++++++++++++++++++----------------------------- src/ds.c | 2 +- src/main.c | 3 +- src/makefile | 4 +- src/ppu/internals.c | 75 +++++++--------------- src/ppu/ppu.c | 22 +++---- src/ppusim/input.c | 13 ++-- src/ppusim/mem.c | 4 +- src/ppusim/pixel.c | 77 +++++++++++----------- src/ppusim/sim.c | 16 +++-- src/ppusim/work.c | 35 +++++----- 11 files changed, 186 insertions(+), 247 deletions(-) (limited to 'src/demo.c') diff --git a/src/demo.c b/src/demo.c index c4e4d7a..b9bba17 100644 --- a/src/demo.c +++ b/src/demo.c @@ -1,36 +1,36 @@ #include #include "demo.h" -#include "ppu/ppu.h" -#include "input.h" #include "entity.h" +#include "input.h" +#include "ppu/ppu.h" #define HH_DEMO_BALL_COUNT 1 hh_s_ppu_loc_fam_entry g_hh_demo_balls[HH_DEMO_BALL_COUNT]; hh_s_entity_player g_hh_player_1 = { - .pos_x = 31000, //0b0000 0001 0011 0110 - .pos_y = 21000, - .radius = 8, - .speed = 1, + .pos_x = 31000, // 0b0000 0001 0011 0110 + .pos_y = 21000, + .radius = 8, + .speed = 1, .direction_x = 1, - .rotation = 8, - .in_air = false, + .rotation = 8, + .in_air = false, }; -void playerMovement(); +void hh_player_movement(); -uint16_t pos_x; //0b0000 0001 0011 0110 -uint16_t pos_y; -uint8_t left = 0; -uint8_t right = 0; -uint8_t up = 0; -uint8_t down = 0; -uint8_t pos_x_bit[2]; -uint8_t pos_y_bit[2]; -uint8_t data_send[3]; -int tileX; -int tileY; +uint16_t g_hh_pos_x; // 0b0000 0001 0011 0110 +uint16_t g_hh_pos_y; +uint8_t g_hh_left = 0; +uint8_t g_hh_right = 0; +uint8_t g_hh_up = 0; +uint8_t g_hh_down = 0; +uint8_t g_hh_pos_x_bit[2]; +uint8_t g_hh_pos_y_bit[2]; +uint8_t g_hh_data_send[3]; +int g_hh_tile_x; +int g_hh_tile_y; void hh_demo_setup() { // load sprites @@ -38,55 +38,54 @@ void hh_demo_setup() { hh_ppu_update_sprite(1, HH_DBG_SPRITE_CHECKERBOARD); // background pattern - hh_ppu_update_color(0, 1, (hh_ppu_rgb_color_t) {0x4, 0x4, 0x4}); + hh_ppu_update_color(0, 1, (hh_ppu_rgb_color_t){0x4, 0x4, 0x4}); for (unsigned i = 0; i < HH_PPU_BG_CANVAS_TILES_H * HH_PPU_BG_CANVAS_TILES_V; i++) { - hh_ppu_update_background(i, (hh_s_ppu_loc_bam_entry) { - .horizontal_flip = false, - .vertical_flip = false, - .palette_index = 0, - .tilemap_index = 1, - }); + hh_ppu_update_background(i, (hh_s_ppu_loc_bam_entry){ + .horizontal_flip = false, + .vertical_flip = false, + .palette_index = 0, + .tilemap_index = 1, + }); } // cool colors - hh_ppu_update_color(1, 1, (hh_ppu_rgb_color_t) {0xf, 0x0, 0xf}); - hh_ppu_update_color(2, 1, (hh_ppu_rgb_color_t) {0xf, 0xf, 0xf}); - hh_ppu_update_color(3, 1, (hh_ppu_rgb_color_t) {0xf, 0x0, 0x0}); - hh_ppu_update_color(4, 1, (hh_ppu_rgb_color_t) {0x0, 0xf, 0xf}); - hh_ppu_update_color(5, 1, (hh_ppu_rgb_color_t) {0x0, 0x0, 0xf}); + hh_ppu_update_color(1, 1, (hh_ppu_rgb_color_t){0xf, 0x0, 0xf}); + hh_ppu_update_color(2, 1, (hh_ppu_rgb_color_t){0xf, 0xf, 0xf}); + hh_ppu_update_color(3, 1, (hh_ppu_rgb_color_t){0xf, 0x0, 0x0}); + hh_ppu_update_color(4, 1, (hh_ppu_rgb_color_t){0x0, 0xf, 0xf}); + hh_ppu_update_color(5, 1, (hh_ppu_rgb_color_t){0x0, 0x0, 0xf}); // balls for (unsigned i = 0; i < HH_DEMO_BALL_COUNT; i++) { g_hh_demo_balls[i].horizontal_flip = false; - g_hh_demo_balls[i].vertical_flip = false; - g_hh_demo_balls[i].palette_index = i+1; - g_hh_demo_balls[i].tilemap_index = 0; + g_hh_demo_balls[i].vertical_flip = false; + g_hh_demo_balls[i].palette_index = i + 1; + g_hh_demo_balls[i].tilemap_index = 0; } } void hh_demo_loop(unsigned long frame) { - playerMovement(); + hh_player_movement(); + + // adjust map size + g_hh_pos_x = g_hh_player_1.pos_x / 100; + g_hh_pos_y = g_hh_player_1.pos_y / 100; - //adjust map size - pos_x = g_hh_player_1.pos_x / 100; - pos_y = g_hh_player_1.pos_y / 100; - // input testing (no hitbox stuff) // pos_x += (-1 * g_hh_controller_p1.dpad_left) + (1 * g_hh_controller_p1.dpad_right); // -1 = L || 1 == R // pos_y += (-1 * g_hh_controller_p1.dpad_up) + (1 * g_hh_controller_p1.dpad_down); // -1 = D || 1 == U // update player sprite on ppu - g_hh_demo_balls[0].position_x = pos_x; - g_hh_demo_balls[0].position_y = pos_y; + g_hh_demo_balls[0].position_x = g_hh_pos_x; + g_hh_demo_balls[0].position_y = g_hh_pos_y; hh_ppu_update_foreground(0, g_hh_demo_balls[0]); - // set background pattern position - hh_ppu_update_aux((hh_s_ppu_loc_aux) { + hh_ppu_update_aux((hh_s_ppu_loc_aux){ .bg_shift_x = (frame / 2) % HH_PPU_SPRITE_WIDTH, .bg_shift_y = (frame / 8) % HH_PPU_SPRITE_HEIGHT, - .fg_fetch = 0, - .sysreset = 0, + .fg_fetch = 0, + .sysreset = 0, }); } @@ -95,91 +94,77 @@ void hh_demo_loop(unsigned long frame) { // bitData[2] = data & 0xff; // bitData[1] = (data >> 8); // bitData[0] = address; // first byte is address -// +// // HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_RESET); // HAL_SPI_Transmit(&hspi1, bitData, 3, 100); //2*8 bit data // HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_SET); // } -void playerMovement() { +void hh_player_movement() { int8_t directionX = (-1 * g_hh_controller_p1.dpad_left) + (1 * g_hh_controller_p1.dpad_right); // -1 = L || 1 == R - int8_t directionY = (-1 * g_hh_controller_p1.dpad_down) + (1 * g_hh_controller_p1.dpad_up); // -1 = D || 1 == U + int8_t directionY = (-1 * g_hh_controller_p1.dpad_down) + (1 * g_hh_controller_p1.dpad_up); // -1 = D || 1 == U - uint8_t i,j; + uint8_t i, j; uint8_t rotation = 0; // 0-7 - //rotation calc - for(i = -1; i < 2;i++) - { - for(j = -1; j < 2; j++) - { - if(directionX == i) - { - if(directionY == j) - { - if(i != 0 && j != 0) //dont update when player idle + // rotation calc + for (i = -1; i < 2; i++) { + for (j = -1; j < 2; j++) { + if (directionX == i) { + if (directionY == j) { + if (i != 0 && j != 0) // dont update when player idle { g_hh_player_1.rotation = rotation; } } } - rotation++; + rotation++; } } - //direction calc - if(directionX != 0) //update direction if player is not idle + // direction calc + if (directionX != 0) // update direction if player is not idle { g_hh_player_1.direction_x = directionX; } - //collision map x-axis + // collision map x-axis - //tile calc including radius and direction for background coliision + // tile calc including radius and direction for background coliision uint16_t tileColX; - uint16_t tileColY = ( g_hh_player_1.pos_y / 100) / 16; ; + uint16_t tileColY = (g_hh_player_1.pos_y / 100) / 16; + ; // remaining space between grid and exact uint8_t modTileX; uint8_t modTileY; - if(g_hh_player_1.in_air == false && directionX != 0) - { - if(directionX == 1) - { - tileColX = ( ( g_hh_player_1.pos_x / 100) + g_hh_player_1.radius ) / 20; - modTileX = ( g_hh_player_1.pos_x + ( 100 * g_hh_player_1.radius ) ) % 2000; + if (g_hh_player_1.in_air == false && directionX != 0) { + if (directionX == 1) { + tileColX = ((g_hh_player_1.pos_x / 100) + g_hh_player_1.radius) / 20; + modTileX = (g_hh_player_1.pos_x + (100 * g_hh_player_1.radius)) % 2000; + } else if (directionX == -1) { + tileColX = ((g_hh_player_1.pos_x / 100) - g_hh_player_1.radius) / 20; + modTileX = (g_hh_player_1.pos_x - (100 * g_hh_player_1.radius)) % 2000; } - else if(directionX == -1) - { - tileColX = ( ( g_hh_player_1.pos_x / 100) - g_hh_player_1.radius ) / 20; - modTileX = ( g_hh_player_1.pos_x - ( 100 * g_hh_player_1.radius ) ) % 2000; + + if (HH_DEMO_HITBOX_TILEMAP[tileColY][tileColX + directionX] != 1) { + g_hh_player_1.pos_x = g_hh_player_1.pos_x + (directionX * g_hh_player_1.speed); // NEW x set } - if(HH_DEMO_HITBOX_TILEMAP[tileColY][tileColX + directionX] != 1) - { + else if (HH_DEMO_HITBOX_TILEMAP[tileColY][tileColX + directionX] == 1) { + if (modTileX < g_hh_player_1.speed) { + g_hh_player_1.pos_x = g_hh_player_1.pos_x + (directionX * modTileX); // NEW x set + } else { g_hh_player_1.pos_x = g_hh_player_1.pos_x + (directionX * g_hh_player_1.speed); // NEW x set } + } - else if(HH_DEMO_HITBOX_TILEMAP[tileColY][tileColX + directionX] == 1) - { - if(modTileX < g_hh_player_1.speed) - { - g_hh_player_1.pos_x = g_hh_player_1.pos_x + (directionX * modTileX); // NEW x set - } - else - { - g_hh_player_1.pos_x = g_hh_player_1.pos_x + (directionX * g_hh_player_1.speed); // NEW x set - } - } - - } - else //if in air different all borders have to be checked + } else // if in air different all borders have to be checked { - } - //collision map floor (y-axis) (falling) - // if falling no jump press (implement) + // collision map floor (y-axis) (falling) + // if falling no jump press (implement) /* tileColY = (( g_hh_player_1.pos_y / 100) + g_hh_player_1.radius) / 16; //bottom of player box modTileY = 1; @@ -189,10 +174,5 @@ void playerMovement() { //playerStat = falling; //for later use of graphics/sound } */ - //else if(HH_DEMO_HITBOX_TILEMAP[]) - - - - - + // else if(HH_DEMO_HITBOX_TILEMAP[]) } diff --git a/src/ds.c b/src/ds.c index d6d4357..24ef58f 100644 --- a/src/ds.c +++ b/src/ds.c @@ -1,6 +1,6 @@ +#include "demo.h" #include "main.h" #include "ppu/ppu.h" -#include "demo.h" void hh_setup() { hh_ppu_init(); diff --git a/src/main.c b/src/main.c index fa63aa2..191d5d9 100644 --- a/src/main.c +++ b/src/main.c @@ -1,8 +1,8 @@ #include -#include "main.h" #include "demo.h" #include "input.h" +#include "main.h" bool g_hh_run = true; @@ -19,4 +19,3 @@ void hh_ppu_vblank_interrupt() { hh_demo_loop(frame); frame++; } - diff --git a/src/makefile b/src/makefile index 1115874..c69bfb5 100644 --- a/src/makefile +++ b/src/makefile @@ -81,8 +81,8 @@ compile_commands.json: makefile stm32.mk ds.mk PHONY += format format: - clang-format -i $(LOCAL_SRCS) - clang-tidy --fix-errors $(LOCAL_SRCS) + clang-format -i $(LOCAL_SRCS) $(DESKTOP_SRCS) + clang-tidy --fix-errors $(LOCAL_SRCS) $(DESKTOP_SRCS) PHONY += clean clean: diff --git a/src/ppu/internals.c b/src/ppu/internals.c index 650ba3e..b8d3e27 100644 --- a/src/ppu/internals.c +++ b/src/ppu/internals.c @@ -1,8 +1,8 @@ #include -#include "ppu/types.h" -#include "ppu/internals.h" #include "ppu/consts.h" +#include "ppu/internals.h" +#include "ppu/types.h" bool hh_ppu_vram_valid_address(hh_ppu_addr_t addr) { #pragma GCC diagnostic push @@ -17,88 +17,59 @@ bool hh_ppu_vram_valid_address(hh_ppu_addr_t addr) { } void hh_ppu_vram_write(hh_s_ppu_vram_data data) { - for (unsigned i = 0; i < data.size; i++) - hh_ppu_vram_dwrite(data.offset + i, data.data[i]); + for (unsigned i = 0; i < data.size; i++) hh_ppu_vram_dwrite(data.offset + i, data.data[i]); } hh_s_ppu_vram_data hh_ppu_2nat_bam(hh_s_ppu_loc_bam_entry e) { - hh_ppu_data_t* data = malloc(sizeof(hh_ppu_data_t) * HH_PPU_VRAM_BAM_ENTRY_SIZE); + hh_ppu_data_t *data = malloc(sizeof(hh_ppu_data_t) * HH_PPU_VRAM_BAM_ENTRY_SIZE); - data[0] = HH_RESIZE(e.tilemap_index, 9, 0) << 0 | - HH_RESIZE(e.palette_index, 2, 0) << 10 | - e.vertical_flip << 13 | - e.horizontal_flip << 14; + data[0] = HH_RESIZE(e.tilemap_index, 9, 0) << 0 | HH_RESIZE(e.palette_index, 2, 0) << 10 | e.vertical_flip << 13 | e.horizontal_flip << 14; - hh_s_ppu_vram_data out = { - .data = data, - .size = HH_PPU_VRAM_FAM_ENTRY_SIZE - }; + hh_s_ppu_vram_data out = {.data = data, .size = HH_PPU_VRAM_FAM_ENTRY_SIZE}; return out; } hh_s_ppu_vram_data hh_ppu_2nat_fam(hh_s_ppu_loc_fam_entry e) { - hh_ppu_data_t* data = malloc(sizeof(hh_ppu_data_t) * HH_PPU_VRAM_FAM_ENTRY_SIZE); + hh_ppu_data_t *data = malloc(sizeof(hh_ppu_data_t) * HH_PPU_VRAM_FAM_ENTRY_SIZE); e.position_x += 16; e.position_y += 16; - data[0] = HH_RESIZE(e.tilemap_index, 9, 0) << 0 | - HH_RESIZE(e.palette_index, 2, 0) << 10 | - HH_RESIZE(e.position_y, 2, 0) << 13; - data[1] = HH_RESIZE(e.position_y, 7, 3) << 0 | - HH_RESIZE(e.position_x, 8, 0) << 5 | - e.vertical_flip << 14 | - e.horizontal_flip << 15; - - hh_s_ppu_vram_data out = { - .data = data, - .size = HH_PPU_VRAM_FAM_ENTRY_SIZE - }; + data[0] = HH_RESIZE(e.tilemap_index, 9, 0) << 0 | HH_RESIZE(e.palette_index, 2, 0) << 10 | HH_RESIZE(e.position_y, 2, 0) << 13; + data[1] = HH_RESIZE(e.position_y, 7, 3) << 0 | HH_RESIZE(e.position_x, 8, 0) << 5 | e.vertical_flip << 14 | e.horizontal_flip << 15; + + hh_s_ppu_vram_data out = {.data = data, .size = HH_PPU_VRAM_FAM_ENTRY_SIZE}; return out; } hh_s_ppu_vram_data hh_ppu_2nat_aux(hh_s_ppu_loc_aux aux) { - hh_ppu_data_t* data = malloc(sizeof(hh_ppu_data_t) * HH_PPU_VRAM_AUX_SIZE); - - data[0] = HH_RESIZE(aux.bg_shift_y, 7, 0) << 0 | - HH_RESIZE(aux.bg_shift_x, 7, 0) << 8; - data[1] = HH_RESIZE(aux.bg_shift_x, 8, 8) << 0 | - aux.fg_fetch << 1 | - aux.sysreset << 2; - - hh_s_ppu_vram_data out = { - .data = data, - .size = HH_PPU_VRAM_AUX_SIZE - }; + hh_ppu_data_t *data = malloc(sizeof(hh_ppu_data_t) * HH_PPU_VRAM_AUX_SIZE); + + data[0] = HH_RESIZE(aux.bg_shift_y, 7, 0) << 0 | HH_RESIZE(aux.bg_shift_x, 7, 0) << 8; + data[1] = HH_RESIZE(aux.bg_shift_x, 8, 8) << 0 | aux.fg_fetch << 1 | aux.sysreset << 2; + + hh_s_ppu_vram_data out = {.data = data, .size = HH_PPU_VRAM_AUX_SIZE}; return out; } hh_s_ppu_vram_data hh_ppu_2nat_sprite(const hh_ppu_loc_sprite_data_t sprite_data) { - hh_ppu_data_t* data = malloc(sizeof(hh_ppu_data_t) * HH_PPU_VRAM_TMM_SPRITE_SIZE); + hh_ppu_data_t *data = malloc(sizeof(hh_ppu_data_t) * HH_PPU_VRAM_TMM_SPRITE_SIZE); for (unsigned i = 0; i < HH_PPU_SPRITE_WIDTH * HH_PPU_SPRITE_HEIGHT; i++) { - unsigned word = i / 5; + unsigned word = i / 5; unsigned pixel = i % 5; if (pixel == 0) data[word] = 0; data[word] |= HH_RESIZE(sprite_data[i], 2, 0) << pixel * 3; } - hh_s_ppu_vram_data out = { - .data = data, - .size = HH_PPU_VRAM_TMM_SPRITE_SIZE - }; + hh_s_ppu_vram_data out = {.data = data, .size = HH_PPU_VRAM_TMM_SPRITE_SIZE}; return out; } hh_s_ppu_vram_data hh_ppu_2nat_color(hh_ppu_rgb_color_t rgb) { - hh_ppu_data_t* data = malloc(sizeof(hh_ppu_data_t) * HH_PPU_VRAM_PAL_ENTRY_SIZE); + hh_ppu_data_t *data = malloc(sizeof(hh_ppu_data_t) * HH_PPU_VRAM_PAL_ENTRY_SIZE); - data[0] = HH_RESIZE(rgb[0], 3, 0) << 8 | - HH_RESIZE(rgb[1], 3, 0) << 4 | - HH_RESIZE(rgb[2], 3, 0) << 0; + data[0] = HH_RESIZE(rgb[0], 3, 0) << 8 | HH_RESIZE(rgb[1], 3, 0) << 4 | HH_RESIZE(rgb[2], 3, 0) << 0; - hh_s_ppu_vram_data out = { - .data = data, - .size = HH_PPU_VRAM_PAL_ENTRY_SIZE - }; + hh_s_ppu_vram_data out = {.data = data, .size = HH_PPU_VRAM_PAL_ENTRY_SIZE}; return out; } diff --git a/src/ppu/ppu.c b/src/ppu/ppu.c index 5f33c55..df14b9f 100644 --- a/src/ppu/ppu.c +++ b/src/ppu/ppu.c @@ -1,52 +1,48 @@ #include -#include "ppu/ppu.h" -#include "ppu/internals.h" #include "ppu/consts.h" +#include "ppu/internals.h" +#include "ppu/ppu.h" void hh_ppu_update_foreground(unsigned index, hh_s_ppu_loc_fam_entry e) { hh_s_ppu_vram_data s = hh_ppu_2nat_fam(e); - s.offset = HH_PPU_VRAM_FAM_OFFSET + HH_PPU_VRAM_FAM_ENTRY_SIZE * index; + s.offset = HH_PPU_VRAM_FAM_OFFSET + HH_PPU_VRAM_FAM_ENTRY_SIZE * index; hh_ppu_vram_write(s); free(s.data); } void hh_ppu_update_background(unsigned index, hh_s_ppu_loc_bam_entry e) { hh_s_ppu_vram_data s = hh_ppu_2nat_bam(e); - s.offset = HH_PPU_VRAM_BAM_OFFSET + HH_PPU_VRAM_BAM_ENTRY_SIZE * index; + s.offset = HH_PPU_VRAM_BAM_OFFSET + HH_PPU_VRAM_BAM_ENTRY_SIZE * index; hh_ppu_vram_write(s); free(s.data); } void hh_ppu_update_sprite(unsigned tilemap_index, const hh_s_ppu_loc_sprite sprite) { hh_s_ppu_vram_data s = hh_ppu_2nat_sprite(sprite); - s.offset = HH_PPU_VRAM_TMM_OFFSET + HH_PPU_VRAM_TMM_SPRITE_SIZE * tilemap_index; + s.offset = HH_PPU_VRAM_TMM_OFFSET + HH_PPU_VRAM_TMM_SPRITE_SIZE * tilemap_index; hh_ppu_vram_write(s); free(s.data); } void hh_ppu_update_aux(hh_s_ppu_loc_aux aux) { hh_s_ppu_vram_data a = hh_ppu_2nat_aux(aux); - a.offset = HH_PPU_VRAM_AUX_OFFSET; + a.offset = HH_PPU_VRAM_AUX_OFFSET; hh_ppu_vram_write(a); free(a.data); } void hh_ppu_update_palette_table(hh_ppu_loc_palette_table_t table) { - for(unsigned i = 0; i < HH_PPU_PALETTE_COUNT; i++) - hh_ppu_update_palette(i, table[i]); + for (unsigned i = 0; i < HH_PPU_PALETTE_COUNT; i++) hh_ppu_update_palette(i, table[i]); } void hh_ppu_update_palette(unsigned palette_index, hh_ppu_loc_palette_data_t palette) { - for(unsigned i = 0; i < HH_PPU_PALETTE_COLOR_COUNT; i++) - hh_ppu_update_color(palette_index, i, palette[i]); + for (unsigned i = 0; i < HH_PPU_PALETTE_COLOR_COUNT; i++) hh_ppu_update_color(palette_index, i, palette[i]); } void hh_ppu_update_color(unsigned palette_index, unsigned color_index, hh_ppu_rgb_color_t color) { hh_s_ppu_vram_data c = hh_ppu_2nat_color(color); - c.offset = HH_PPU_VRAM_PAL_OFFSET + - palette_index * HH_PPU_VRAM_PAL_ENTRY_SIZE * HH_PPU_PALETTE_COLOR_COUNT + - color_index * HH_PPU_VRAM_PAL_ENTRY_SIZE; + c.offset = HH_PPU_VRAM_PAL_OFFSET + palette_index * HH_PPU_VRAM_PAL_ENTRY_SIZE * HH_PPU_PALETTE_COLOR_COUNT + color_index * HH_PPU_VRAM_PAL_ENTRY_SIZE; hh_ppu_vram_write(c); free(c.data); } diff --git a/src/ppusim/input.c b/src/ppusim/input.c index bdb94ac..08bc382 100644 --- a/src/ppusim/input.c +++ b/src/ppusim/input.c @@ -2,15 +2,14 @@ #include "input.h" -hh_s_gamepad g_hh_controller_p1 = { 0 }; -hh_s_gamepad g_hh_controller_p2 = { 0 }; +hh_s_gamepad g_hh_controller_p1 = {0}; +hh_s_gamepad g_hh_controller_p2 = {0}; void hh_input_read() { // SDL_PumpEvents(); - const Uint8* kb = SDL_GetKeyboardState(NULL); - g_hh_controller_p1.dpad_up = kb[SDL_SCANCODE_W]; - g_hh_controller_p1.dpad_down = kb[SDL_SCANCODE_S]; - g_hh_controller_p1.dpad_left = kb[SDL_SCANCODE_A]; + const Uint8 *kb = SDL_GetKeyboardState(NULL); + g_hh_controller_p1.dpad_up = kb[SDL_SCANCODE_W]; + 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]; } - diff --git a/src/ppusim/mem.c b/src/ppusim/mem.c index e3f41f5..bd8606e 100644 --- a/src/ppusim/mem.c +++ b/src/ppusim/mem.c @@ -1,8 +1,8 @@ -#include #include +#include -#include "ppusim/mem.h" #include "ppu/internals.h" +#include "ppusim/mem.h" hh_ppu_data_t *g_hh_ppusim_vram = NULL; diff --git a/src/ppusim/pixel.c b/src/ppusim/pixel.c index 6e915e6..0457d55 100644 --- a/src/ppusim/pixel.c +++ b/src/ppusim/pixel.c @@ -1,10 +1,10 @@ #include #include -#include "ppusim/work.h" +#include "ppu/consts.h" #include "ppu/internals.h" #include "ppusim/mem.h" -#include "ppu/consts.h" +#include "ppusim/work.h" /* transform xy if tile is flipped */ static uint16_t hh_ppusim_apply_transform(unsigned x, unsigned y, bool fliph, bool flipv) { @@ -15,25 +15,25 @@ static uint16_t hh_ppusim_apply_transform(unsigned x, unsigned y, bool fliph, bo /* @brief get current bg pixel cidx */ static uint8_t hh_ppusim_bg_pixel(unsigned x, unsigned y) { - hh_ppu_data_t* aux = &g_hh_ppusim_vram[HH_PPU_VRAM_AUX_OFFSET]; - unsigned bg_shift_y = HH_RESIZE(aux[0], 7, 0); - unsigned bg_shift_x = HH_RESIZE(aux[0], 15, 8) | HH_RESIZE(aux[1], 0, 0) << 8; - unsigned abs_x = bg_shift_x + x; - unsigned abs_y = bg_shift_y + y; - unsigned grid_x = abs_x / HH_PPU_SPRITE_WIDTH; - unsigned grid_y = abs_y / HH_PPU_SPRITE_HEIGHT; - unsigned loc_x = abs_x - grid_x * HH_PPU_SPRITE_WIDTH; - unsigned loc_y = abs_y - grid_y * HH_PPU_SPRITE_HEIGHT; - unsigned bam_offset = grid_y * HH_PPU_BG_CANVAS_TILES_H + grid_x; - hh_ppu_data_t bam = g_hh_ppusim_vram[HH_PPU_VRAM_BAM_OFFSET + bam_offset]; - uint8_t cidx = 0; + hh_ppu_data_t *aux = &g_hh_ppusim_vram[HH_PPU_VRAM_AUX_OFFSET]; + unsigned bg_shift_y = HH_RESIZE(aux[0], 7, 0); + unsigned bg_shift_x = HH_RESIZE(aux[0], 15, 8) | HH_RESIZE(aux[1], 0, 0) << 8; + unsigned abs_x = bg_shift_x + x; + unsigned abs_y = bg_shift_y + y; + unsigned grid_x = abs_x / HH_PPU_SPRITE_WIDTH; + unsigned grid_y = abs_y / HH_PPU_SPRITE_HEIGHT; + unsigned loc_x = abs_x - grid_x * HH_PPU_SPRITE_WIDTH; + unsigned loc_y = abs_y - grid_y * HH_PPU_SPRITE_HEIGHT; + unsigned bam_offset = grid_y * HH_PPU_BG_CANVAS_TILES_H + grid_x; + hh_ppu_data_t bam = g_hh_ppusim_vram[HH_PPU_VRAM_BAM_OFFSET + bam_offset]; + uint8_t cidx = 0; uint16_t tile_pixel_idx = hh_ppusim_apply_transform(loc_x, loc_y, HH_RESIZE(bam, 14, 14), HH_RESIZE(bam, 13, 13)); - uint16_t tile_idx = HH_RESIZE(bam, 9, 0); - hh_ppu_addr_t ttm_addr = tile_idx * HH_PPU_VRAM_TMM_SPRITE_SIZE + tile_pixel_idx / 5; - uint8_t word_bit_addr = (tile_pixel_idx % 5) * 3; - hh_ppu_data_t tmm = g_hh_ppusim_vram[HH_PPU_VRAM_TMM_OFFSET + ttm_addr]; + uint16_t tile_idx = HH_RESIZE(bam, 9, 0); + hh_ppu_addr_t ttm_addr = tile_idx * HH_PPU_VRAM_TMM_SPRITE_SIZE + tile_pixel_idx / 5; + uint8_t word_bit_addr = (tile_pixel_idx % 5) * 3; + hh_ppu_data_t tmm = g_hh_ppusim_vram[HH_PPU_VRAM_TMM_OFFSET + ttm_addr]; cidx |= HH_RESIZE(bam, 12, 10) << 3; - cidx |= HH_RESIZE(tmm, word_bit_addr+2, word_bit_addr) << 0; + cidx |= HH_RESIZE(tmm, word_bit_addr + 2, word_bit_addr) << 0; return cidx; } @@ -44,42 +44,37 @@ static uint8_t hh_ppusim_fg_pixel(unsigned x, unsigned y) { uint8_t cidx = 0; for (unsigned i = 0; i < HH_PPU_FG_SPRITE_COUNT; i++) { unsigned fam_offset = i * HH_PPU_VRAM_FAM_ENTRY_SIZE; - hh_ppu_data_t* fam = &g_hh_ppusim_vram[HH_PPU_VRAM_FAM_OFFSET + fam_offset]; - unsigned sprite_y = HH_RESIZE(fam[0], 15, 13) | HH_RESIZE(fam[1], 4, 0) << 3; - unsigned sprite_x = HH_RESIZE(fam[1], 13, 5); + hh_ppu_data_t *fam = &g_hh_ppusim_vram[HH_PPU_VRAM_FAM_OFFSET + fam_offset]; + unsigned sprite_y = HH_RESIZE(fam[0], 15, 13) | HH_RESIZE(fam[1], 4, 0) << 3; + unsigned sprite_x = HH_RESIZE(fam[1], 13, 5); if (x < sprite_x) continue; if (x >= sprite_x + HH_PPU_SPRITE_WIDTH) continue; if (y < sprite_y) continue; if (y >= sprite_y + HH_PPU_SPRITE_HEIGHT) continue; - unsigned loc_x = x - sprite_x; - unsigned loc_y = y - sprite_y; + unsigned loc_x = x - sprite_x; + unsigned loc_y = y - sprite_y; uint16_t tile_pixel_idx = hh_ppusim_apply_transform(loc_x, loc_y, HH_RESIZE(fam[1], 15, 15), HH_RESIZE(fam[1], 14, 14)); - uint16_t tile_idx = HH_RESIZE(fam[0], 9, 0); - hh_ppu_addr_t ttm_addr = tile_idx * HH_PPU_VRAM_TMM_SPRITE_SIZE + tile_pixel_idx / 5; - uint8_t word_bit_addr = (tile_pixel_idx % 5) * 3; - hh_ppu_data_t tmm = g_hh_ppusim_vram[HH_PPU_VRAM_TMM_OFFSET + ttm_addr]; - unsigned cidx_col = HH_RESIZE(tmm, word_bit_addr+2, word_bit_addr); + uint16_t tile_idx = HH_RESIZE(fam[0], 9, 0); + hh_ppu_addr_t ttm_addr = tile_idx * HH_PPU_VRAM_TMM_SPRITE_SIZE + tile_pixel_idx / 5; + uint8_t word_bit_addr = (tile_pixel_idx % 5) * 3; + hh_ppu_data_t tmm = g_hh_ppusim_vram[HH_PPU_VRAM_TMM_OFFSET + ttm_addr]; + unsigned cidx_col = HH_RESIZE(tmm, word_bit_addr + 2, word_bit_addr); if (cidx_col == 0) continue; unsigned cidx_pal = HH_RESIZE(fam[0], 12, 10); - cidx = (cidx_col << 0) | (cidx_pal << 3); + cidx = (cidx_col << 0) | (cidx_pal << 3); break; } return cidx; } -void hh_ppusim_pixel(uint8_t* s, unsigned x, unsigned y) { - uint8_t bg_cidx = hh_ppusim_bg_pixel(x, y); - uint8_t fg_cidx = hh_ppusim_fg_pixel(x, y); - uint8_t cidx = (fg_cidx & HH_MASK(3)) == 0 ? bg_cidx : fg_cidx; - hh_ppu_data_t pal_rgb = g_hh_ppusim_vram[HH_PPU_VRAM_PAL_OFFSET + cidx]; - hh_ppu_rgb_color_t rgb = { - HH_RESIZE(pal_rgb, 11, 8), - HH_RESIZE(pal_rgb, 7, 4), - HH_RESIZE(pal_rgb, 3, 0) - }; +void hh_ppusim_pixel(uint8_t *s, unsigned x, unsigned y) { + uint8_t bg_cidx = hh_ppusim_bg_pixel(x, y); + uint8_t fg_cidx = hh_ppusim_fg_pixel(x, y); + uint8_t cidx = (fg_cidx & HH_MASK(3)) == 0 ? bg_cidx : fg_cidx; + hh_ppu_data_t pal_rgb = g_hh_ppusim_vram[HH_PPU_VRAM_PAL_OFFSET + cidx]; + hh_ppu_rgb_color_t rgb = {HH_RESIZE(pal_rgb, 11, 8), HH_RESIZE(pal_rgb, 7, 4), HH_RESIZE(pal_rgb, 3, 0)}; s[0] = rgb[0] << 4; s[1] = rgb[1] << 4; s[2] = rgb[2] << 4; } - diff --git a/src/ppusim/sim.c b/src/ppusim/sim.c index 2816b31..449b78d 100644 --- a/src/ppusim/sim.c +++ b/src/ppusim/sim.c @@ -1,6 +1,6 @@ -#include -#include #include +#include +#include #include "main.h" #include "ppu/ppu.h" @@ -8,16 +8,17 @@ #include "ppusim/sim.h" #include "ppusim/work.h" -SDL_Window *g_hh_window = NULL; +SDL_Window *g_hh_window = NULL; SDL_Renderer *g_hh_renderer = NULL; void hh_ppu_init() { SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS); - g_hh_window = SDL_CreateWindow("ppusim", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, HH_PPU_SCREEN_WIDTH * HH_PPUSIM_UPSCALE_FACTOR, HH_PPU_SCREEN_HEIGHT * HH_PPUSIM_UPSCALE_FACTOR, SDL_WINDOW_SHOWN); + g_hh_window = SDL_CreateWindow("ppusim", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, HH_PPU_SCREEN_WIDTH * HH_PPUSIM_UPSCALE_FACTOR, + HH_PPU_SCREEN_HEIGHT * HH_PPUSIM_UPSCALE_FACTOR, SDL_WINDOW_SHOWN); g_hh_renderer = SDL_CreateRenderer(g_hh_window, -1, SDL_RENDERER_ACCELERATED); g_hh_ppusim_core_count = SDL_GetCPUCount(); - g_hh_ppusim_threads = malloc(sizeof(pthread_t) * g_hh_ppusim_core_count); + g_hh_ppusim_threads = malloc(sizeof(pthread_t) * g_hh_ppusim_core_count); g_hh_ppusim_vram = malloc(sizeof(hh_ppu_data_t) * 0xffff); memset(g_hh_ppusim_vram, 0x0000, 0xffff); @@ -36,8 +37,9 @@ void hh_loop() { SDL_Event e; while (g_hh_run) { uint32_t start = SDL_GetTicks(); - while (SDL_PollEvent(&e)) if (e.type == SDL_QUIT) exit(0); - + while (SDL_PollEvent(&e)) + if (e.type == SDL_QUIT) exit(0); + hh_ppu_vblank_interrupt(); SDL_RenderClear(g_hh_renderer); diff --git a/src/ppusim/work.c b/src/ppusim/work.c index 3b9cee6..96d15aa 100644 --- a/src/ppusim/work.c +++ b/src/ppusim/work.c @@ -2,12 +2,12 @@ #include #include -#include "ppusim/work.h" -#include "ppusim/sim.h" #include "ppu/consts.h" #include "ppusim/pixel.h" +#include "ppusim/sim.h" +#include "ppusim/work.h" -pthread_t* g_hh_ppusim_threads; +pthread_t *g_hh_ppusim_threads; unsigned g_hh_ppusim_core_count; hh_s_ppusim_screen g_hh_ppusim_screen; @@ -15,29 +15,26 @@ hh_s_ppusim_screen g_hh_ppusim_screen; #pragma GCC diagnostic ignored "-Wint-to-pointer-cast" #pragma GCC diagnostic ignored "-Wpointer-to-int-cast" -void* hh_ppusim_draw_thread(void* arg) { - unsigned core = (unsigned long) arg; +void *hh_ppusim_draw_thread(void *arg) { + unsigned core = (unsigned long)arg; for (unsigned y = core; y < HH_PPU_SCREEN_HEIGHT; y += g_hh_ppusim_core_count) - for (unsigned x = 0; x < HH_PPU_SCREEN_WIDTH; x++) - hh_ppusim_pixel(g_hh_ppusim_screen[y][x], x, y); + for (unsigned x = 0; x < HH_PPU_SCREEN_WIDTH; x++) hh_ppusim_pixel(g_hh_ppusim_screen[y][x], x, y); return NULL; } -void hh_ppusim_draw_frame(SDL_Renderer* renderer) { - for (unsigned core = 0; core < g_hh_ppusim_core_count; core++) - pthread_create(&g_hh_ppusim_threads[core], NULL, hh_ppusim_draw_thread, (void*)(unsigned long)core); - for (unsigned core = 0; core < g_hh_ppusim_core_count; core++) - pthread_join(g_hh_ppusim_threads[core], NULL); - +void hh_ppusim_draw_frame(SDL_Renderer *renderer) { + for (unsigned core = 0; core < g_hh_ppusim_core_count; core++) pthread_create(&g_hh_ppusim_threads[core], NULL, hh_ppusim_draw_thread, (void *)(unsigned long)core); + for (unsigned core = 0; core < g_hh_ppusim_core_count; core++) pthread_join(g_hh_ppusim_threads[core], NULL); + for (unsigned x = 0; x < HH_PPU_SCREEN_WIDTH; x++) { for (unsigned y = 0; y < HH_PPU_SCREEN_HEIGHT; y++) { SDL_SetRenderDrawColor(renderer, g_hh_ppusim_screen[y][x][0], g_hh_ppusim_screen[y][x][1], g_hh_ppusim_screen[y][x][2], 255); - SDL_RenderFillRect(renderer, &(SDL_Rect) { - .x = x * HH_PPUSIM_UPSCALE_FACTOR, - .y = y * HH_PPUSIM_UPSCALE_FACTOR, - .w = HH_PPUSIM_UPSCALE_FACTOR, - .h = HH_PPUSIM_UPSCALE_FACTOR, - }); + SDL_RenderFillRect(renderer, &(SDL_Rect){ + .x = x * HH_PPUSIM_UPSCALE_FACTOR, + .y = y * HH_PPUSIM_UPSCALE_FACTOR, + .w = HH_PPUSIM_UPSCALE_FACTOR, + .h = HH_PPUSIM_UPSCALE_FACTOR, + }); } } } -- cgit v1.2.3 From c12ff10740927c6040c83b3399ad35e94117131c Mon Sep 17 00:00:00 2001 From: Flenk008 Date: Sat, 11 Mar 2023 20:56:26 +0100 Subject: Fixed movement Adjusted movement speed otherwise wouldnt show any movement. Also changed input testing lines so it does work --- src/demo.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) (limited to 'src/demo.c') diff --git a/src/demo.c b/src/demo.c index b9bba17..baaf73d 100644 --- a/src/demo.c +++ b/src/demo.c @@ -12,7 +12,7 @@ hh_s_entity_player g_hh_player_1 = { .pos_x = 31000, // 0b0000 0001 0011 0110 .pos_y = 21000, .radius = 8, - .speed = 1, + .speed = 100, .direction_x = 1, .rotation = 8, .in_air = false, @@ -66,14 +66,17 @@ void hh_demo_setup() { void hh_demo_loop(unsigned long frame) { hh_player_movement(); + // input testing (no hitbox stuff) + // g_hh_player_1.pos_x += ((-1 * g_hh_controller_p1.dpad_left) + (1 * g_hh_controller_p1.dpad_right)) * g_hh_player_1.speed; // -1 = L || 1 == R + // g_hh_player_1.pos_y += ((-1 * g_hh_controller_p1.dpad_up) + (1 * g_hh_controller_p1.dpad_down)) * g_hh_player_1.speed; // -1 = D || 1 == U + // adjust map size g_hh_pos_x = g_hh_player_1.pos_x / 100; g_hh_pos_y = g_hh_player_1.pos_y / 100; - // input testing (no hitbox stuff) - // pos_x += (-1 * g_hh_controller_p1.dpad_left) + (1 * g_hh_controller_p1.dpad_right); // -1 = L || 1 == R - // pos_y += (-1 * g_hh_controller_p1.dpad_up) + (1 * g_hh_controller_p1.dpad_down); // -1 = D || 1 == U + + // update player sprite on ppu g_hh_demo_balls[0].position_x = g_hh_pos_x; @@ -102,7 +105,7 @@ void hh_demo_loop(unsigned long frame) { void hh_player_movement() { int8_t directionX = (-1 * g_hh_controller_p1.dpad_left) + (1 * g_hh_controller_p1.dpad_right); // -1 = L || 1 == R - int8_t directionY = (-1 * g_hh_controller_p1.dpad_down) + (1 * g_hh_controller_p1.dpad_up); // -1 = D || 1 == U + int8_t directionY = (-1 * g_hh_controller_p1.dpad_up) + (1 * g_hh_controller_p1.dpad_down); // -1 = D || 1 == U uint8_t i, j; uint8_t rotation = 0; // 0-7 @@ -140,11 +143,11 @@ void hh_player_movement() { if (g_hh_player_1.in_air == false && directionX != 0) { if (directionX == 1) { - tileColX = ((g_hh_player_1.pos_x / 100) + g_hh_player_1.radius) / 20; - modTileX = (g_hh_player_1.pos_x + (100 * g_hh_player_1.radius)) % 2000; + tileColX = ((g_hh_player_1.pos_x / 100) + g_hh_player_1.radius) / 16; + modTileX = (g_hh_player_1.pos_x + (100 * g_hh_player_1.radius)) % 1600; } else if (directionX == -1) { - tileColX = ((g_hh_player_1.pos_x / 100) - g_hh_player_1.radius) / 20; - modTileX = (g_hh_player_1.pos_x - (100 * g_hh_player_1.radius)) % 2000; + tileColX = ((g_hh_player_1.pos_x / 100) - g_hh_player_1.radius) / 16; + modTileX = (g_hh_player_1.pos_x - (100 * g_hh_player_1.radius)) % 1600; } if (HH_DEMO_HITBOX_TILEMAP[tileColY][tileColX + directionX] != 1) { @@ -163,6 +166,11 @@ void hh_player_movement() { { } + + if(directionY != 0) + { + // g_hh_player_1.pos_y = g_hh_player_1.pos_y + (directionY * g_hh_player_1.speed * 2); // NEW x set + } // collision map floor (y-axis) (falling) // if falling no jump press (implement) /* -- cgit v1.2.3 From 506b1d11e6006e44de5b888eb342eb9dbed58d2c Mon Sep 17 00:00:00 2001 From: UnavailableDev Date: Mon, 13 Mar 2023 10:04:37 +0100 Subject: demo.c --- src/demo.c | 207 ++++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 184 insertions(+), 23 deletions(-) (limited to 'src/demo.c') diff --git a/src/demo.c b/src/demo.c index b9bba17..58cb461 100644 --- a/src/demo.c +++ b/src/demo.c @@ -1,10 +1,17 @@ #include #include "demo.h" -#include "entity.h" #include "input.h" +#include "entity.h" #include "ppu/ppu.h" +#include "engine/maths.h" +#include "engine/camera.h" +#include "engine/entity.h" +#include "engine/draw_screen.h" +#include "engine/sprite_controller.h" + + #define HH_DEMO_BALL_COUNT 1 hh_s_ppu_loc_fam_entry g_hh_demo_balls[HH_DEMO_BALL_COUNT]; @@ -32,10 +39,19 @@ uint8_t g_hh_data_send[3]; int g_hh_tile_x; int g_hh_tile_y; +typedef struct { + vec2 pos; + uint8_t idx; +}hh_s_tiles; + + +hh_entity hh_g_player, hh_g_player_new; void hh_demo_setup() { +#if 0 // load sprites - hh_ppu_update_sprite(0, HH_DBG_SPRITE_BALL); - hh_ppu_update_sprite(1, HH_DBG_SPRITE_CHECKERBOARD); + // hh_ppu_update_sprite(0, HH_DBG_SPRITE_BALL); + // hh_ppu_update_sprite(1, HH_DBG_SPRITE_CHECKERBOARD); + hh_ppu_update_sprite(1, HH_SQUARE); // background pattern hh_ppu_update_color(0, 1, (hh_ppu_rgb_color_t){0x4, 0x4, 0x4}); @@ -49,44 +65,189 @@ void hh_demo_setup() { } // cool colors - hh_ppu_update_color(1, 1, (hh_ppu_rgb_color_t){0xf, 0x0, 0xf}); - hh_ppu_update_color(2, 1, (hh_ppu_rgb_color_t){0xf, 0xf, 0xf}); - hh_ppu_update_color(3, 1, (hh_ppu_rgb_color_t){0xf, 0x0, 0x0}); - hh_ppu_update_color(4, 1, (hh_ppu_rgb_color_t){0x0, 0xf, 0xf}); - hh_ppu_update_color(5, 1, (hh_ppu_rgb_color_t){0x0, 0x0, 0xf}); + // hh_ppu_update_color(1, 1, (hh_ppu_rgb_color_t){0xf, 0x0, 0xf}); + // hh_ppu_update_color(2, 1, (hh_ppu_rgb_color_t){0xf, 0xf, 0xf}); + // hh_ppu_update_color(3, 1, (hh_ppu_rgb_color_t){0xf, 0x0, 0x0}); + // hh_ppu_update_color(4, 1, (hh_ppu_rgb_color_t){0x0, 0xf, 0xf}); + // hh_ppu_update_color(5, 1, (hh_ppu_rgb_color_t){0x0, 0x0, 0xf}); + + for (int i = 0; i < 8; i++) + { + hh_ppu_update_color(1,i,HH_SLIME[i]); + } + // balls +#else + hh_g_player = (hh_entity){ + .pos = {32,32}, + .vec = {0,0}, + .hp = 1, + .speed = 1, + .is_grounded = false + }; + for (unsigned i = 0; i < HH_DEMO_BALL_COUNT; i++) { g_hh_demo_balls[i].horizontal_flip = false; g_hh_demo_balls[i].vertical_flip = false; - g_hh_demo_balls[i].palette_index = i + 1; - g_hh_demo_balls[i].tilemap_index = 0; + g_hh_demo_balls[i].palette_index = 7; + g_hh_demo_balls[i].tilemap_index = 20; } + hh_setup_palettes(); + hh_setup_screen(); +#endif } void hh_demo_loop(unsigned long frame) { - hh_player_movement(); + // hh_player_movement(); // adjust map size - g_hh_pos_x = g_hh_player_1.pos_x / 100; - g_hh_pos_y = g_hh_player_1.pos_y / 100; + // g_hh_pos_x = g_hh_player_1.pos_x / 100; + // g_hh_pos_y = g_hh_player_1.pos_y / 100; // input testing (no hitbox stuff) - // pos_x += (-1 * g_hh_controller_p1.dpad_left) + (1 * g_hh_controller_p1.dpad_right); // -1 = L || 1 == R - // pos_y += (-1 * g_hh_controller_p1.dpad_up) + (1 * g_hh_controller_p1.dpad_down); // -1 = D || 1 == U + // g_hh_pos_x += (-1 * g_hh_controller_p1.dpad_left) + (1 * g_hh_controller_p1.dpad_right); // -1 = L || 1 == R + // g_hh_pos_y += (-1 * g_hh_controller_p1.dpad_up) + (1 * g_hh_controller_p1.dpad_down); // -1 = D || 1 == U + + + + hh_g_player.vec = (vec2){.x = (-1 * g_hh_controller_p1.dpad_left) + (1 * g_hh_controller_p1.dpad_right), + .y = (-1 * g_hh_controller_p1.dpad_up) + (1 * g_hh_controller_p1.dpad_down) }; + // const int8_t maa = 3; + // const int8_t mbb = -3; + // if (g_hh_controller_p1.dpad_up) + + // if (g_hh_controller_p1.dpad_down) + + // if (g_hh_controller_p1.dpad_left) { + // hh_g_player.vec.x += mbb; + // // g_hh_demo_balls[0].horizontal_flip = true; + // } + // if (g_hh_controller_p1.dpad_right) { + // hh_g_player.vec.x += maa; + // // g_hh_demo_balls[0].horizontal_flip = true; + // } + if (g_hh_controller_p1.button_primary /*&& hh_g_player.is_grounded*/) //JUMP + hh_g_player.vec.y += -6; + // // if (g_hh_controller_p1.button_secondary) + + + hh_g_player.vec.y += 1; //gravity + + + //END OF VECTOR CHANGES + // hh_g_player.vec.y = CLAMP(hh_g_player.vec.y,-32,32); + // hh_g_player.vec.x = CLAMP(hh_g_player.vec.x,-32,32); + + hh_g_player_new.pos = (vec2){ + .x = hh_g_player.pos.x + hh_g_player.vec.x, + .y = hh_g_player.pos.y + hh_g_player.vec.y, + }; + + + + // const uint8_t empty = 0; + // hh_s_tiles tiles[9]; + // const vec2 tile_offset[9] = { + // (vec2){-16,-16},(vec2){0,-16},(vec2){+16,-16}, + // (vec2){-16,0}, (vec2){0,0}, (vec2){+16,0}, + // (vec2){-16,+16},(vec2){0,+16},(vec2){+16,+16}, + // }; + + // for (int i = 0; i < 9; i++) { + // vec2 temp_pos = vec_add(hh_g_player.pos, tile_offset[i]); + // temp_pos =(vec2){ + // .x = temp_pos.x, + // .y = temp_pos.y, + // }; + // hh_s_tiles tile = { + // .pos = temp_pos, + // .idx = hh_world_to_tile(temp_pos) + // }; + + // if(hh_colidable(tile.idx)) { + // tiles[i]=tile; + // // printf(" collidable near!"); + // } else { + // tiles[i].idx = 0; + // } + // } + /* + 012 + 345 + 678 + */ + + // for (int i = 0; i < 9; i++) + // { + // if (tiles[i].idx != 0){ + // hh_solve_collision(tiles[i].pos, &hh_g_player); + // } + // } + + hh_g_player_new.is_grounded = false; + + // solves x collision + if (hh_g_player.vec.x <= 0) { + if (hh_colidable(hh_world_to_tile((vec2){.x=hh_g_player_new.pos.x + 0, .y=hh_g_player.pos.y + 0})) || + hh_colidable(hh_world_to_tile((vec2){.x=hh_g_player_new.pos.x + 0, .y=hh_g_player.pos.y + 15}))) { + hh_g_player_new.pos.x = (hh_g_player_new.pos.x & ~15) + 16, + hh_g_player_new.vec.x = 0; + } + } else { + if (hh_colidable(hh_world_to_tile((vec2){.x=hh_g_player_new.pos.x + 16, .y=hh_g_player.pos.y + 0})) || + hh_colidable(hh_world_to_tile((vec2){.x=hh_g_player_new.pos.x + 16, .y=hh_g_player.pos.y + 15}))) { + hh_g_player_new.pos.x = hh_g_player_new.pos.x & ~15, // <-- magic comma, NOT TOUCHY + hh_g_player_new.vec.x = 0; + } + } + + //solves y collision + if (hh_g_player.vec.y <= 0) { + if (hh_colidable(hh_world_to_tile((vec2){.x=hh_g_player_new.pos.x + 0, .y=hh_g_player_new.pos.y + 0})) || + hh_colidable(hh_world_to_tile((vec2){.x=hh_g_player_new.pos.x + 0, .y=hh_g_player_new.pos.y + 15}))) { + hh_g_player_new.pos.y = (hh_g_player_new.pos.y & ~15) + 16, + hh_g_player_new.vec.y = 0; + } + } else { + if (hh_colidable(hh_world_to_tile((vec2){.x=hh_g_player_new.pos.x + 0, .y=hh_g_player_new.pos.y + 16})) || + hh_colidable(hh_world_to_tile((vec2){.x=hh_g_player_new.pos.x + 16, .y=hh_g_player_new.pos.y + 15}))) { + hh_g_player_new.pos.y = hh_g_player_new.pos.y & ~15, + hh_g_player_new.vec.y = 0; + hh_g_player_new.is_grounded = true; + } + } + + hh_g_player = hh_g_player_new; + + + + vec_cor cam_pos;//value in tiles + // cam_pos = (vec2){0,0}; + cam_pos = hh_update_camera(hh_g_player.pos,(vec2){0,0},(vec2){.x=20*16,.y=30*16});//TODO: remove magic number(s) + printf("%i, %i:%i, %i\n",hh_g_player.pos.x,hh_g_player.pos.y,cam_pos.x,cam_pos.y); + hh_draw_screen(cam_pos); // update player sprite on ppu - g_hh_demo_balls[0].position_x = g_hh_pos_x; - g_hh_demo_balls[0].position_y = g_hh_pos_y; + g_hh_demo_balls[0].position_x = (hh_g_player.pos.x-cam_pos.x); + g_hh_demo_balls[0].position_y = hh_g_player.pos.y-cam_pos.y; hh_ppu_update_foreground(0, g_hh_demo_balls[0]); + // for (int i = 0; i < HH_DEMO_BALL_COUNT; i++){ + // g_hh_demo_balls[i].position_x = hh_g_player.pos.x +16*i; + // g_hh_demo_balls[i].position_y = hh_g_player.pos.y; + // hh_ppu_update_foreground(i, g_hh_demo_balls[i]); + + // } + + // set background pattern position - hh_ppu_update_aux((hh_s_ppu_loc_aux){ - .bg_shift_x = (frame / 2) % HH_PPU_SPRITE_WIDTH, - .bg_shift_y = (frame / 8) % HH_PPU_SPRITE_HEIGHT, - .fg_fetch = 0, - .sysreset = 0, - }); + // hh_ppu_update_aux((hh_s_ppu_loc_aux){ + // .bg_shift_x = (frame / 2) % HH_PPU_SPRITE_WIDTH, + // .bg_shift_y = (frame / 8) % HH_PPU_SPRITE_HEIGHT, + // .fg_fetch = 0, + // .sysreset = 0, + // }); } // void sendData(uint8_t address, uint16_t data) { -- cgit v1.2.3 From 30eca8d3bd03c1efe52f2432c4b6b0c842fffa2d Mon Sep 17 00:00:00 2001 From: UnavailableDev Date: Mon, 13 Mar 2023 17:10:11 +0100 Subject: demo.c clean up --- src/demo.c | 197 +---------------------------------------- src/engine/entity.c | 12 +-- src/engine/entity.h | 33 ++++++- src/engine/player_controller.c | 166 ++++++++++++++++++++++++++++++++++ 4 files changed, 207 insertions(+), 201 deletions(-) (limited to 'src/demo.c') diff --git a/src/demo.c b/src/demo.c index 58cb461..bb0aecf 100644 --- a/src/demo.c +++ b/src/demo.c @@ -9,11 +9,10 @@ #include "engine/camera.h" #include "engine/entity.h" #include "engine/draw_screen.h" +#include "engine/player_controller.h" #include "engine/sprite_controller.h" -#define HH_DEMO_BALL_COUNT 1 -hh_s_ppu_loc_fam_entry g_hh_demo_balls[HH_DEMO_BALL_COUNT]; hh_s_entity_player g_hh_player_1 = { .pos_x = 31000, // 0b0000 0001 0011 0110 @@ -47,207 +46,17 @@ typedef struct { hh_entity hh_g_player, hh_g_player_new; void hh_demo_setup() { -#if 0 - // load sprites - // hh_ppu_update_sprite(0, HH_DBG_SPRITE_BALL); - // hh_ppu_update_sprite(1, HH_DBG_SPRITE_CHECKERBOARD); - hh_ppu_update_sprite(1, HH_SQUARE); - // background pattern - hh_ppu_update_color(0, 1, (hh_ppu_rgb_color_t){0x4, 0x4, 0x4}); - for (unsigned i = 0; i < HH_PPU_BG_CANVAS_TILES_H * HH_PPU_BG_CANVAS_TILES_V; i++) { - hh_ppu_update_background(i, (hh_s_ppu_loc_bam_entry){ - .horizontal_flip = false, - .vertical_flip = false, - .palette_index = 0, - .tilemap_index = 1, - }); - } - - // cool colors - // hh_ppu_update_color(1, 1, (hh_ppu_rgb_color_t){0xf, 0x0, 0xf}); - // hh_ppu_update_color(2, 1, (hh_ppu_rgb_color_t){0xf, 0xf, 0xf}); - // hh_ppu_update_color(3, 1, (hh_ppu_rgb_color_t){0xf, 0x0, 0x0}); - // hh_ppu_update_color(4, 1, (hh_ppu_rgb_color_t){0x0, 0xf, 0xf}); - // hh_ppu_update_color(5, 1, (hh_ppu_rgb_color_t){0x0, 0x0, 0xf}); - - for (int i = 0; i < 8; i++) - { - hh_ppu_update_color(1,i,HH_SLIME[i]); - } - - - // balls -#else - hh_g_player = (hh_entity){ - .pos = {32,32}, - .vec = {0,0}, - .hp = 1, - .speed = 1, - .is_grounded = false - }; - - for (unsigned i = 0; i < HH_DEMO_BALL_COUNT; i++) { - g_hh_demo_balls[i].horizontal_flip = false; - g_hh_demo_balls[i].vertical_flip = false; - g_hh_demo_balls[i].palette_index = 7; - g_hh_demo_balls[i].tilemap_index = 20; - } hh_setup_palettes(); hh_setup_screen(); -#endif + } void hh_demo_loop(unsigned long frame) { // hh_player_movement(); - // adjust map size - // g_hh_pos_x = g_hh_player_1.pos_x / 100; - // g_hh_pos_y = g_hh_player_1.pos_y / 100; - - // input testing (no hitbox stuff) - // g_hh_pos_x += (-1 * g_hh_controller_p1.dpad_left) + (1 * g_hh_controller_p1.dpad_right); // -1 = L || 1 == R - // g_hh_pos_y += (-1 * g_hh_controller_p1.dpad_up) + (1 * g_hh_controller_p1.dpad_down); // -1 = D || 1 == U - - - - hh_g_player.vec = (vec2){.x = (-1 * g_hh_controller_p1.dpad_left) + (1 * g_hh_controller_p1.dpad_right), - .y = (-1 * g_hh_controller_p1.dpad_up) + (1 * g_hh_controller_p1.dpad_down) }; - // const int8_t maa = 3; - // const int8_t mbb = -3; - // if (g_hh_controller_p1.dpad_up) - - // if (g_hh_controller_p1.dpad_down) - - // if (g_hh_controller_p1.dpad_left) { - // hh_g_player.vec.x += mbb; - // // g_hh_demo_balls[0].horizontal_flip = true; - // } - // if (g_hh_controller_p1.dpad_right) { - // hh_g_player.vec.x += maa; - // // g_hh_demo_balls[0].horizontal_flip = true; - // } - if (g_hh_controller_p1.button_primary /*&& hh_g_player.is_grounded*/) //JUMP - hh_g_player.vec.y += -6; - // // if (g_hh_controller_p1.button_secondary) - - - hh_g_player.vec.y += 1; //gravity - - - //END OF VECTOR CHANGES - // hh_g_player.vec.y = CLAMP(hh_g_player.vec.y,-32,32); - // hh_g_player.vec.x = CLAMP(hh_g_player.vec.x,-32,32); - - hh_g_player_new.pos = (vec2){ - .x = hh_g_player.pos.x + hh_g_player.vec.x, - .y = hh_g_player.pos.y + hh_g_player.vec.y, - }; - - - - - // const uint8_t empty = 0; - // hh_s_tiles tiles[9]; - // const vec2 tile_offset[9] = { - // (vec2){-16,-16},(vec2){0,-16},(vec2){+16,-16}, - // (vec2){-16,0}, (vec2){0,0}, (vec2){+16,0}, - // (vec2){-16,+16},(vec2){0,+16},(vec2){+16,+16}, - // }; - - // for (int i = 0; i < 9; i++) { - // vec2 temp_pos = vec_add(hh_g_player.pos, tile_offset[i]); - // temp_pos =(vec2){ - // .x = temp_pos.x, - // .y = temp_pos.y, - // }; - // hh_s_tiles tile = { - // .pos = temp_pos, - // .idx = hh_world_to_tile(temp_pos) - // }; - - // if(hh_colidable(tile.idx)) { - // tiles[i]=tile; - // // printf(" collidable near!"); - // } else { - // tiles[i].idx = 0; - // } - // } - /* - 012 - 345 - 678 - */ - - // for (int i = 0; i < 9; i++) - // { - // if (tiles[i].idx != 0){ - // hh_solve_collision(tiles[i].pos, &hh_g_player); - // } - // } - - hh_g_player_new.is_grounded = false; - - // solves x collision - if (hh_g_player.vec.x <= 0) { - if (hh_colidable(hh_world_to_tile((vec2){.x=hh_g_player_new.pos.x + 0, .y=hh_g_player.pos.y + 0})) || - hh_colidable(hh_world_to_tile((vec2){.x=hh_g_player_new.pos.x + 0, .y=hh_g_player.pos.y + 15}))) { - hh_g_player_new.pos.x = (hh_g_player_new.pos.x & ~15) + 16, - hh_g_player_new.vec.x = 0; - } - } else { - if (hh_colidable(hh_world_to_tile((vec2){.x=hh_g_player_new.pos.x + 16, .y=hh_g_player.pos.y + 0})) || - hh_colidable(hh_world_to_tile((vec2){.x=hh_g_player_new.pos.x + 16, .y=hh_g_player.pos.y + 15}))) { - hh_g_player_new.pos.x = hh_g_player_new.pos.x & ~15, // <-- magic comma, NOT TOUCHY - hh_g_player_new.vec.x = 0; - } - } - - //solves y collision - if (hh_g_player.vec.y <= 0) { - if (hh_colidable(hh_world_to_tile((vec2){.x=hh_g_player_new.pos.x + 0, .y=hh_g_player_new.pos.y + 0})) || - hh_colidable(hh_world_to_tile((vec2){.x=hh_g_player_new.pos.x + 0, .y=hh_g_player_new.pos.y + 15}))) { - hh_g_player_new.pos.y = (hh_g_player_new.pos.y & ~15) + 16, - hh_g_player_new.vec.y = 0; - } - } else { - if (hh_colidable(hh_world_to_tile((vec2){.x=hh_g_player_new.pos.x + 0, .y=hh_g_player_new.pos.y + 16})) || - hh_colidable(hh_world_to_tile((vec2){.x=hh_g_player_new.pos.x + 16, .y=hh_g_player_new.pos.y + 15}))) { - hh_g_player_new.pos.y = hh_g_player_new.pos.y & ~15, - hh_g_player_new.vec.y = 0; - hh_g_player_new.is_grounded = true; - } - } - - hh_g_player = hh_g_player_new; - - - - vec_cor cam_pos;//value in tiles - // cam_pos = (vec2){0,0}; - cam_pos = hh_update_camera(hh_g_player.pos,(vec2){0,0},(vec2){.x=20*16,.y=30*16});//TODO: remove magic number(s) - printf("%i, %i:%i, %i\n",hh_g_player.pos.x,hh_g_player.pos.y,cam_pos.x,cam_pos.y); - hh_draw_screen(cam_pos); - // update player sprite on ppu - g_hh_demo_balls[0].position_x = (hh_g_player.pos.x-cam_pos.x); - g_hh_demo_balls[0].position_y = hh_g_player.pos.y-cam_pos.y; - hh_ppu_update_foreground(0, g_hh_demo_balls[0]); - - // for (int i = 0; i < HH_DEMO_BALL_COUNT; i++){ - // g_hh_demo_balls[i].position_x = hh_g_player.pos.x +16*i; - // g_hh_demo_balls[i].position_y = hh_g_player.pos.y; - // hh_ppu_update_foreground(i, g_hh_demo_balls[i]); - - // } - + hh_player_actions(); - // set background pattern position - // hh_ppu_update_aux((hh_s_ppu_loc_aux){ - // .bg_shift_x = (frame / 2) % HH_PPU_SPRITE_WIDTH, - // .bg_shift_y = (frame / 8) % HH_PPU_SPRITE_HEIGHT, - // .fg_fetch = 0, - // .sysreset = 0, - // }); } // void sendData(uint8_t address, uint16_t data) { diff --git a/src/engine/entity.c b/src/engine/entity.c index 152cf1d..153e7e1 100644 --- a/src/engine/entity.c +++ b/src/engine/entity.c @@ -28,19 +28,19 @@ void hh_solve_collision(vec2 pos_environment, hh_entity* entity){ return; printf("BONK!/n"); - // if (entity->vec.y > 0){ + // if (entity->vel.y > 0){ // entity->pos.y = MAX(entity->pos.y,pos_environment.y); - // entity->vec.y = 0; + // entity->vel.y = 0; // } else { // entity->pos.y = MIN(entity->pos.y,pos_environment.y); - // entity->vec.y = 0; + // entity->vel.y = 0; // } - // if (entity->vec.x <= 0){ + // if (entity->vel.x <= 0){ // entity->pos.x = MIN(entity->pos.x,pos_environment.x-16); - // entity->vec.x = 0; + // entity->vel.x = 0; // } else { // entity->pos.x = MAX(entity->pos.x,pos_environment.x+16); - // entity->vec.x = 0; + // entity->vel.x = 0; // } } diff --git a/src/engine/entity.h b/src/engine/entity.h index dee4aed..f45dae2 100644 --- a/src/engine/entity.h +++ b/src/engine/entity.h @@ -3,16 +3,47 @@ #include #include +#include "ppu/types.h" + #include "engine/maths.h" +typedef uint8_t hh_idx_t; + +typedef enum { + fire, ice, poison +}hh_e_damage_t; + +typedef struct { + hh_s_ppu_loc_fam_entry fam; //screen + hh_idx_t frame0; + hh_idx_t palette; + +}hh_s_rendering; + +typedef struct { + int8_t hp; + int8_t dmg; + hh_e_damage_t dmg_type; + int8_t speed_x, speed_y; + +} hh_s_atributes; + + typedef struct { - vec2 pos, vec; + vec2 pos, vel, vec; bool is_grounded; int8_t hp; int8_t speed; + hh_s_rendering render; //armor/block? }hh_entity; +typedef struct { + hh_entity p; + hh_s_atributes atr; +}hh_s_player; + + /// @brief detect for collision enity and eviroment /// @param pos1 position of environment tile to be checked /// @param pos2 position entity diff --git a/src/engine/player_controller.c b/src/engine/player_controller.c index 27e522e..6735620 100644 --- a/src/engine/player_controller.c +++ b/src/engine/player_controller.c @@ -1 +1,167 @@ +#include "ppu/types.h" +#include "engine/camera.h" +#include "engine/draw_screen.h" +#include "engine/sprite_controller.h" #include "engine/player_controller.h" + +#include "demo.h" +#include + +#include "input.h" + +void hh_player_actions() { + static hh_entity player={ + .hp = 4, + .speed = 6, + .is_grounded = false, + .pos = (vec2){32,32}, + .vel = (vec2){0,0}, + .vec = (vec2){0,0}, + .render = { + .frame0 = 20, + .palette = 7, + .fam = (hh_s_ppu_loc_fam_entry){ + .horizontal_flip = false, + .vertical_flip = false, + .palette_index = 7, + .tilemap_index = 2, + } + } + }, player_new = {0}; + + // hh_input_read(); + player.vel = (vec2){.x = (-1 * g_hh_controller_p1.dpad_left) + (1 * g_hh_controller_p1.dpad_right), + .y = (-1 * g_hh_controller_p1.dpad_up) + (1 * g_hh_controller_p1.dpad_down) }; + // const int8_t maa = 3; + // const int8_t mbb = -3; + // if (g_hh_controller_p1.dpad_up) + // + // if (g_hh_controller_p1.dpad_down) + // + // if (g_hh_controller_p1.dpad_left) { + // player.vel.x += mbb; + // // g_hh_demo_balls[0].horizontal_flip = true; + // } + // if (g_hh_controller_p1.dpad_right) { + // player.vel.x += maa; + // // g_hh_demo_balls[0].horizontal_flip = true; + // } + // if (g_hh_controller_p1.button_primary /*&& player.is_grounded*/) //JUMP + // player.vel.y += -6; + // // // if (g_hh_controller_p1.button_secondary) + + // player.render.fam.palette_index = 7; + // player.render.fam.tilemap_index = 7; + + // printf("%x ",player.render.fam.palette_index); + + // player.vel.y += 1; //gravity + + + //END OF VECTOR CHANGES + // player.vel.y = CLAMP(player.vel.y,-32,32); + // player.vel.x = CLAMP(player.vel.x,-32,32); + + player_new.pos = (vec2){ + .x = player.pos.x + player.vel.x, + .y = player.pos.y + player.vel.y, + }; + + + + // const uint8_t empty = 0; + // hh_s_tiles tiles[9]; + // const vec2 tile_offset[9] = { + // (vec2){-16,-16},(vec2){0,-16},(vec2){+16,-16}, + // (vec2){-16,0}, (vec2){0,0}, (vec2){+16,0}, + // (vec2){-16,+16},(vec2){0,+16},(vec2){+16,+16}, + // }; + // for (int i = 0; i < 9; i++) { + // vec2 temp_pos = vec_add(player.pos, tile_offset[i]); + // temp_pos =(vec2){ + // .x = temp_pos.x, + // .y = temp_pos.y, + // }; + // hh_s_tiles tile = { + // .pos = temp_pos, + // .idx = hh_world_to_tile(temp_pos) + // }; + // if(hh_colidable(tile.idx)) { + // tiles[i]=tile; + // // printf(" collidable near!"); + // } else { + // tiles[i].idx = 0; + // } + // } + /* + 012 + 345 + 678 + */ + // for (int i = 0; i < 9; i++) + // { + // if (tiles[i].idx != 0){ + // hh_solve_collision(tiles[i].pos, &player); + // } + // } + + player_new.is_grounded = false; + + // solves x collision + if (player.vel.x <= 0) { + if (hh_colidable(hh_world_to_tile((vec2){.x=player_new.pos.x + 0, .y=player.pos.y + 0})) || + hh_colidable(hh_world_to_tile((vec2){.x=player_new.pos.x + 0, .y=player.pos.y + 15}))) { + player_new.pos.x = (player_new.pos.x & ~15) + 16, + player_new.vel.x = 0; + } + } else { + if (hh_colidable(hh_world_to_tile((vec2){.x=player_new.pos.x + 16, .y=player.pos.y + 0})) || + hh_colidable(hh_world_to_tile((vec2){.x=player_new.pos.x + 16, .y=player.pos.y + 15}))) { + player_new.pos.x = player_new.pos.x & ~15, // <-- magic comma, NOT TOUCHY + player_new.vel.x = 0; + } + } + + //solves y collision + if (player.vel.y <= 0) { + if (hh_colidable(hh_world_to_tile((vec2){.x=player_new.pos.x + 0, .y=player_new.pos.y + 0})) || + hh_colidable(hh_world_to_tile((vec2){.x=player_new.pos.x + 0, .y=player_new.pos.y + 15}))) { + player_new.pos.y = (player_new.pos.y & ~15) + 16, + player_new.vel.y = 0; + } + } else { + if (hh_colidable(hh_world_to_tile((vec2){.x=player_new.pos.x + 0, .y=player_new.pos.y + 16})) || + hh_colidable(hh_world_to_tile((vec2){.x=player_new.pos.x + 16, .y=player_new.pos.y + 15}))) { + player_new.pos.y = player_new.pos.y & ~15, + player_new.vel.y = 0; + player_new.is_grounded = true; + } + } + + player = player_new; + + vec_cor cam_pos;//value in tiles + // cam_pos = (vec2){0,0}; + cam_pos = hh_update_camera(player.pos,(vec2){0,0},(vec2){.x=20*16,.y=30*16});//TODO: remove magic number(s) + // printf("%i, %i:%i, %i\n",player.pos.x,player.pos.y,cam_pos.x,cam_pos.y); + hh_draw_screen(cam_pos); + // 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.tilemap_index = 2;//TODO: these two lines should be redundant + player.render.fam.palette_index = 7; + hh_ppu_update_foreground(0, player.render.fam); + + + hh_s_ppu_loc_fam_entry sprite = { + .position_x = 16, + .position_y = 16, + .palette_index = 7, + .tilemap_index = 2, + }; + hh_ppu_update_foreground(1, sprite); + +} + -- cgit v1.2.3