aboutsummaryrefslogtreecommitdiff
path: root/src/engine
diff options
context:
space:
mode:
Diffstat (limited to 'src/engine')
-rw-r--r--src/engine/bullet.c14
-rw-r--r--src/engine/bullet.h6
-rw-r--r--src/engine/camera.c7
-rw-r--r--src/engine/draw_screen.c91
-rw-r--r--src/engine/draw_screen.h2
-rw-r--r--src/engine/level_const.c23
-rw-r--r--src/engine/level_const.h24
7 files changed, 107 insertions, 60 deletions
diff --git a/src/engine/bullet.c b/src/engine/bullet.c
index 2016a4d..e6ca6df 100644
--- a/src/engine/bullet.c
+++ b/src/engine/bullet.c
@@ -3,25 +3,25 @@
// TODO: use hh_entity as bullet struct
-void shootBullet(vec2 playerPos, vec_cor cam_pos, hh_entity* bullet){
+void hh_shoot_bullet(vec2 player, vec_cor cam_pos, hh_entity* bullet){
vec2 temp;
if(g_hh_controller_p1.button_secondary){
if(bullet->is_grounded){
bullet->is_grounded=false;
- bullet->pos = playerPos;
+ bullet->pos = player;
}
}
else{
if(!bullet->is_grounded){
- updateBullet(bullet , cam_pos, temp);
- drawBullet(*bullet);
+ hh_update_bullet(bullet , cam_pos);
+ hh_draw_bullet(*bullet);
}
}
}
-void collision
-void updateBullet(hh_entity* bullet, vec_cor cam_pos, vec2 start){
+
+void hh_update_bullet(hh_entity* bullet, vec_cor cam_pos){
bullet->pos.x += 1;
// update bullet sprite on ppu
@@ -29,7 +29,7 @@ void updateBullet(hh_entity* bullet, vec_cor cam_pos, vec2 start){
bullet->render.fam.position_y = (bullet->pos.y-cam_pos.y);
}
-void drawBullet(hh_entity bullet){
+void hh_draw_bullet(hh_entity bullet){
hh_s_ppu_loc_fam_entry temp = bullet.render.fam;
hh_ppu_update_foreground(10,temp);
}
diff --git a/src/engine/bullet.h b/src/engine/bullet.h
index 5bbc031..5f07b2e 100644
--- a/src/engine/bullet.h
+++ b/src/engine/bullet.h
@@ -4,8 +4,8 @@
#include "input.h"
-void shootBullet(vec2 playerPos, vec_cor cam_pos, hh_entity*);
+void hh_shoot_bullet(vec2 playerPos, vec_cor cam_pos, hh_entity*);
-void updateBullet(hh_entity* , vec_cor, vec2 );
+void hh_update_bullet(hh_entity* , vec_cor );
-void drawBullet(hh_entity);
+void hh_draw_bullet(hh_entity);
diff --git a/src/engine/camera.c b/src/engine/camera.c
index 2c3e517..6898430 100644
--- a/src/engine/camera.c
+++ b/src/engine/camera.c
@@ -4,12 +4,13 @@
vec_cor hh_update_camera(vec_cen new, vec2 min, vec2 max){
-
//TODO: change floating point math to fix point math
//TODO: remove magic number at y camera offset
// new = vec_cen2cor(new,(vec2){.x=max.x/2,.y=max.y/2});
- new = vec_cen2cor((vec2){.x=new.x+(HH_PPU_SPRITE_WIDTH),.y=new.y+(HH_PPU_SPRITE_HEIGHT*8)},(vec2){.x=max.x/2,.y=max.y/2});
+ new = vec_cen2cor((vec2){.x=new.x+(HH_PPU_SPRITE_WIDTH),.y=new.y+(HH_PPU_SPRITE_HEIGHT*8)},(vec2){.x=(max.x - min.x)/2,.y=(max.y - min.y)/2});
+ // new = vec_cen2cor((vec2){.x=new.x+(HH_PPU_SPRITE_WIDTH),.y=new.y+(HH_PPU_SPRITE_HEIGHT*8)},(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;
@@ -30,6 +31,8 @@ vec_cor hh_update_camera(vec_cen new, vec2 min, vec2 max){
old.x = CLAMP(new.x,min.x,max.x);
old.y = CLAMP(new.y,min.y,max.y);
+ //printf("camera new %d min %d max %d\n",new.y,min.y,max.y);
return old;
}
+
diff --git a/src/engine/draw_screen.c b/src/engine/draw_screen.c
index 4a5ca68..d1d7a04 100644
--- a/src/engine/draw_screen.c
+++ b/src/engine/draw_screen.c
@@ -1,72 +1,101 @@
#include "engine/draw_screen.h"
-//#include "engine/sprite_controller.h"
hh_level_entity level;
-uint64_t offset=0;
+uint64_t offsetY=0;
+uint64_t offsetX=0;
uint8_t hh_world_to_tile(vec2 pos){
- int index = (((pos.y/16)*hh_max_x_size) + (pos.x/16));//TODO: remove magic number(s)
+ int index = (((pos.y/HH_PPU_SPRITE_HEIGHT)*level.size.x) + (pos.x/HH_PPU_SPRITE_WIDTH));//TODO: remove magic number(s)
int tile= level.place[index];
return tile;
}
void hh_update_screen(vec2 view, vec2 player){
- int currentTileY = view.y / 16;
- int offset_px = view.y-(offset / 40 * 16);
+ int current_tile_y = view.y / HH_PPU_SPRITE_HEIGHT;
+ int current_tile_x = view.x / HH_PPU_SPRITE_WIDTH;
+ int offset_py = view.y - (offsetY / 40 * HH_PPU_SPRITE_HEIGHT);
+ int offset_px = view.x - offsetX * HH_PPU_SPRITE_WIDTH;
+ static int prev_ofsset = 0;
+ int size = MIN(HH_PPU_BG_CANVAS_TILES_H,level.size.x) * MIN(HH_PPU_BG_CANVAS_TILES_V,level.size.y);
+ if( (offset_py == 0 || offset_py > 230) && level.size.y > level.size.x && prev_ofsset != offset_py){
+ if(offset_py==0){
+ offsetY = (current_tile_y-14) * level.size.x;
+ }
+ else{
+ offsetY = current_tile_y * level.size.x;
+ }
- // || (offset_px == 0 && lastUpdated != 0 && player.y/16 == currentTileY-5)
- if( (offset_px > 230) ){
- int size = (hh_max_x_size) * hh_max_y_size;
- offset = currentTileY * level.x;
-
// Update the background screen
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 = hh_get_palette(level.place[offset+BAM_index]),
- .tilemap_index = level.place[offset+BAM_index],
+ .palette_index = hh_get_palette(level.place[offsetY + BAM_index]),
+ .tilemap_index = level.place[offsetY + BAM_index],
+ });
+ }
+ prev_ofsset = offset_py;
+ }
+ else if ((offset_px == 0 || offset_px > 310) && level.size.x > level.size.y && prev_ofsset != offset_px)
+ {
+ if(offset_px==0){
+ offsetX = current_tile_x - 14;
+ }
+ else{
+ offsetX = current_tile_x;
+ }
+ // Update the background screen
+ for (int BAM_index = 0; BAM_index < size; BAM_index++) {
+ int var = (BAM_index / HH_PPU_BG_CANVAS_TILES_H) * level.size.x + MIN((BAM_index % HH_PPU_BG_CANVAS_TILES_H + offsetX),level.size.x);
+ hh_ppu_update_background(BAM_index, (hh_s_ppu_loc_bam_entry){
+ .horizontal_flip = false,
+ .vertical_flip = false,
+ .palette_index = hh_get_palette(level.place[var]),
+ .tilemap_index = level.place[var],
});
}
}
+
+ prev_ofsset = offset_px;
+
}
void hh_setup_screen(hh_level_entity currentlevel){
hh_clear_screen();
hh_clear_sprite();
level = currentlevel;
- int size = hh_max_x_size* hh_max_y_size;
- printf("hier\n");
- for(int BAM_index = 0; BAM_index < size; BAM_index++){
+ offsetY=0;
+ offsetX=0;
+ int size = MIN(HH_PPU_BG_CANVAS_TILES_H, level.size.x) * MIN(HH_PPU_BG_CANVAS_TILES_V, level.size.y);
+ for (int BAM_index = 0; BAM_index < size; BAM_index++) {
+ int var = (BAM_index / HH_PPU_BG_CANVAS_TILES_H) * level.size.x + BAM_index % HH_PPU_BG_CANVAS_TILES_H;
hh_ppu_update_background(BAM_index, (hh_s_ppu_loc_bam_entry){
.horizontal_flip = false,
.vertical_flip = false,
- .palette_index = hh_get_palette(currentlevel.place[BAM_index]),
- .tilemap_index = currentlevel.place[BAM_index],
+ .palette_index = hh_get_palette(currentlevel.place[var]),
+ .tilemap_index = currentlevel.place[var],
});
}
}
vec_cor hh_draw_screen(vec2 player) {
static vec_cor previousViewport = {0, 0};
- int offset_px = offset/40*16;
- //printf("%d\n",offset_px%2);
- vec_cor viewport = hh_update_camera(player,(vec2){.x=0,.y=offset_px},(vec2){.x=20*16,.y=30*16+offset_px/4});//TODO: remove magic number(s)
-
- hh_update_screen((vec2){.y=viewport.y,.x=0},player);
+ int offset_py = offsetY / 40 * HH_PPU_SPRITE_HEIGHT;
+
+ int offset_px = (offsetX * HH_PPU_SPRITE_WIDTH) ;
+ vec_cor viewport = hh_update_camera(player,(vec2){.x = offset_px, .y = offset_py},(vec2){.x = HH_PPU_SCREEN_WIDTH + offset_px, .y = 480 + offset_py});//TODO: remove magic number(s)
+ viewport.x = CLAMP(viewport.x, 0, level.size.x * HH_PPU_SPRITE_WIDTH - HH_PPU_SCREEN_WIDTH);
+ viewport.y = CLAMP(viewport.y, 0, level.size.y * HH_PPU_SPRITE_HEIGHT - HH_PPU_SCREEN_HEIGHT);
+
+ hh_update_screen((vec2){.y=viewport.y,.x=viewport.x},player);
if (viewport.x == previousViewport.x && viewport.y == previousViewport.y){}
else{
- // update previous viewport values
-
- //printf("viewport y %d\n",viewport.y-(offset/40*16));
- //printf("offset %d\n",offset);
hh_ppu_update_aux((hh_s_ppu_loc_aux){
- .bg_shift_x = viewport.x - 0,
- .bg_shift_y = viewport.y-(offset/40*16),
+ .bg_shift_x = viewport.x - offset_px,
+ .bg_shift_y = viewport.y - offset_py,
.fg_fetch = 0,
.sysreset = 0,
});
previousViewport = viewport;
-
}
return viewport;
@@ -93,8 +122,8 @@ void hh_clear_screen(){
void hh_clear_sprite(){
for (int i = 0; i < HH_PPU_FG_SPRITE_COUNT; i++) {
hh_ppu_update_foreground(i,(hh_s_ppu_loc_fam_entry){
- .position_x = -16,
- .position_y = -16,
+ .position_x = -HH_PPU_SPRITE_WIDTH,
+ .position_y = -HH_PPU_SPRITE_HEIGHT,
});
}
}
diff --git a/src/engine/draw_screen.h b/src/engine/draw_screen.h
index 0d1b68a..9130842 100644
--- a/src/engine/draw_screen.h
+++ b/src/engine/draw_screen.h
@@ -11,7 +11,7 @@
#include <stdlib.h>
#include "engine/camera.h"
-#define hh_max_x_size 40
+#define hh_max_x_size 40
#define hh_max_y_size 30
/** @brief return a single tile from world binary */
diff --git a/src/engine/level_const.c b/src/engine/level_const.c
index b568e86..5ba0187 100644
--- a/src/engine/level_const.c
+++ b/src/engine/level_const.c
@@ -3,11 +3,16 @@
hh_g_all_levels hh_init_game_levels(){
hh_g_all_levels levels;
- levels.currentLevel=0;
-
- levels.level[0].x=40;
- levels.level[0].y=60;
+ levels.current_level=0;
+
+ levels.level[0].size.x=40;
+ levels.level[0].size.y=100;
levels.level[0].hh_level_completed=false;
+
+ levels.level[1].size.x=100;
+ levels.level[1].size.y=28;
+ levels.level[1].hh_level_completed=false;
+
FILE *fp = fopen("../test/bin/level1_test.bin", "rb");
fseek(fp, 0, SEEK_END);
int size = ftell(fp) / sizeof(int);
@@ -15,7 +20,17 @@ hh_g_all_levels hh_init_game_levels(){
int* hh_game_level1 = malloc(size * sizeof(int));
fread(hh_game_level1, sizeof(int), size, fp);
fclose(fp);
+
+ FILE *lvl2 = fopen("../test/bin/level2_test.bin", "rb");
+ fseek(lvl2, 0, SEEK_END);
+ size = ftell(lvl2) / sizeof(int);
+ fseek(lvl2, (0 * sizeof(int)) + sizeof(int), SEEK_SET);
+ int* hh_game_level2 = malloc(size * sizeof(int));
+ fread(hh_game_level2, sizeof(int), size, lvl2);
+ fclose(lvl2);
+
levels.level[0].place = hh_game_level1;
+ levels.level[1].place = hh_game_level2;
return levels;
}
diff --git a/src/engine/level_const.h b/src/engine/level_const.h
index 7a4aac5..b86ae7b 100644
--- a/src/engine/level_const.h
+++ b/src/engine/level_const.h
@@ -3,27 +3,27 @@
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
-#include <math.h>`
+#include "engine/maths.h"
+#include "engine/entity.h"
typedef enum {
- hh_e_STATE_startingScreen,
- hh_e_STATE_Shop,
- hh_e_STATE_Gameplay,
- hh_e_STATE_GameOver,
- hh_e_STATE_HighScore
-} hh_e_GameState;
-
+ hh_e_state_starting_screen,
+ hh_e_state_shop,
+ hh_e_state_gameplay,
+ hh_e_state_game_over,
+ hh_e_state_high_score
+} hh_e_game_state;
+//entity array met enemeies
typedef struct {
- int x;
- int y;
+ vec2 size;
int hh_total_enemies;
int* place;
bool hh_level_completed;
}hh_level_entity;
typedef struct {
- hh_level_entity level[1];
- int currentLevel;
+ hh_level_entity level[2];
+ int current_level;
}hh_g_all_levels;