diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/.gitignore | 1 | ||||
| -rw-r--r-- | src/demo.c | 207 | ||||
| -rw-r--r-- | src/engine/TODO/entity.c | 41 | ||||
| -rw-r--r-- | src/engine/TODO/player_controller.h | 4 | ||||
| -rw-r--r-- | src/engine/TODO/sprite_controller.h | 6 | ||||
| -rw-r--r-- | src/engine/camera.c | 34 | ||||
| -rw-r--r-- | src/engine/camera.h | 6 | ||||
| -rw-r--r-- | src/engine/draw_screen.c | 27 | ||||
| -rw-r--r-- | src/engine/draw_screen.h | 2 | ||||
| -rw-r--r-- | src/engine/engine.c | 1 | ||||
| -rw-r--r-- | src/engine/entity.c | 46 | ||||
| -rw-r--r-- | src/engine/entity.h (renamed from src/engine/TODO/entity.h) | 8 | ||||
| -rw-r--r-- | src/engine/maths.c | 19 | ||||
| -rw-r--r-- | src/engine/maths.h | 7 | ||||
| -rw-r--r-- | src/engine/player_controller.c | 1 | ||||
| -rw-r--r-- | src/engine/player_controller.h | 7 | ||||
| -rw-r--r-- | src/engine/sprite_controller.c | 22 | ||||
| -rw-r--r-- | src/engine/sprite_controller.h | 106 | ||||
| -rw-r--r-- | src/makefile | 7 | ||||
| -rw-r--r-- | src/ppusim/input.c | 1 | ||||
| -rw-r--r-- | src/ppusim/sim.c | 11 | 
21 files changed, 466 insertions, 98 deletions
| diff --git a/src/.gitignore b/src/.gitignore index d8325cf..504b995 100644 --- a/src/.gitignore +++ b/src/.gitignore @@ -4,3 +4,4 @@ main.bin  main  main.exe  static/ +*.bin @@ -1,10 +1,17 @@  #include <math.h>  #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) { diff --git a/src/engine/TODO/entity.c b/src/engine/TODO/entity.c deleted file mode 100644 index fa550d5..0000000 --- a/src/engine/TODO/entity.c +++ /dev/null @@ -1,41 +0,0 @@ -#include <stdbool.h> - -#include "hh_entity.h" -#include "maths.h" - -/* -    PLAYER: (pos on X) -    ,___, -    |   | -    | X | -    |___| - -*/ - -bool hh_collision(vec2* pos1, vec2* pos2){ -    if (pos2->x == CLAMP(pos2->x,pos1->x,pos1->x+1.0f)){// hit x -        return true; -    } - -    if (pos2->y == CLAMP(pos2->y,pos1->y,pos1->y+0.99f)){// 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; -    } -} - 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/TODO/sprite_controller.h b/src/engine/TODO/sprite_controller.h deleted file mode 100644 index c1fadff..0000000 --- a/src/engine/TODO/sprite_controller.h +++ /dev/null @@ -1,6 +0,0 @@ -// handles sprites - -// Bg sprites - - -// Fg or entity sprites diff --git a/src/engine/camera.c b/src/engine/camera.c new file mode 100644 index 0000000..e756bd4 --- /dev/null +++ b/src/engine/camera.c @@ -0,0 +1,34 @@ +#include "engine/camera.h" + +#include "ppu/consts.h" + + +vec_cor hh_update_camera(vec_cen new, vec2 min, vec2 max){ + +	//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); + +	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..c4f3389 100644 --- a/src/engine/draw_screen.c +++ b/src/engine/draw_screen.c @@ -1,21 +1,22 @@ -#include "draw_screen.h" +#include "engine/draw_screen.h" +#include "engine/sprite_controller.h"  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;  } @@ -25,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,  	}); @@ -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 <stdio.h> diff --git a/src/engine/engine.c b/src/engine/engine.c index f3410a4..799ee7c 100644 --- a/src/engine/engine.c +++ b/src/engine/engine.c @@ -1,3 +1,4 @@  #include "engine/draw_screen.h"  #include "engine/level.h"  #include "engine/maths.h" +#include "engine/sprite_controller.h" diff --git a/src/engine/entity.c b/src/engine/entity.c new file mode 100644 index 0000000..152cf1d --- /dev/null +++ b/src/engine/entity.c @@ -0,0 +1,46 @@ +#include <stdbool.h> + +#include "engine/entity.h" +#include "engine/maths.h" + +/* +    PLAYER: (pos on X) +    ,___, +    |   | +    | 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+16)){// hit y +        return true; +    } +    return false; +} + +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/TODO/entity.h b/src/engine/entity.h index fdbeb8a..dee4aed 100644 --- a/src/engine/TODO/entity.h +++ b/src/engine/entity.h @@ -1,13 +1,15 @@  #pragma once  #include <stdint.h> +#include <stdbool.h> -#include "maths.h" +#include "engine/maths.h"  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 new file mode 100644 index 0000000..475bba2 --- /dev/null +++ b/src/engine/maths.c @@ -0,0 +1,19 @@ +#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, +		.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..bef287e 100644 --- a/src/engine/maths.h +++ b/src/engine/maths.h @@ -3,12 +3,17 @@  // #include <math.h>  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); +  //fixed point at decimal 7lsb (world positions in pixels (with fixed decimal point))  #define HH_MATH_FIXED_POINT 7 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 new file mode 100644 index 0000000..5d93cf8 --- /dev/null +++ b/src/engine/sprite_controller.c @@ -0,0 +1,22 @@ +#include <stdint.h> + +#include "engine/sprite_controller.h" +#include "ppu/types.h" +#include "ppu/consts.h" +#include "ppu/ppu.h" + +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]); +		} +	}	 +} + +bool hh_colidable(uint8_t tile_idx){ +	return (hh_get_palette(tile_idx) != 0); +} diff --git a/src/engine/sprite_controller.h b/src/engine/sprite_controller.h new file mode 100644 index 0000000..001a459 --- /dev/null +++ b/src/engine/sprite_controller.h @@ -0,0 +1,106 @@ +#pragma once +#include <stdint.h> + +#include "ppu/types.h" + +// handles sprites + +// Bg sprites + +// Fg or entity sprites + +//TODO: pack data inside of sprite_palette LUT +//HH_PPU_PALETTE_COUNT +#define HH_SPRITE_COUNT 40 +#define HH_PAL_IDX_SKY 0 +#define HH_PAL_IDX_BRICK 1 +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 +	7,7,7,2,7,7,1,2,7 +	//other palettes here: +}; + + +const static hh_ppu_loc_palette_table_t hh_g_palette = { +	{//palette info here +		{0x1,0x2,0x3}, +		{0x0,0x0,0x0}, +		{0x0,0x0,0x0}, +		{0x0,0x0,0x0}, +		{0x0,0x0,0x0}, +		{0x0,0x0,0x0}, +		{0x0,0x0,0x0}, +		{0x0,0x0,0x0}}, +	{//Bricks +		{0x1,0x2,0x3},//01 +		{0xd,0x8,0xa},//24 +		{0x0,0x0,0x1},//25 +		{0x1,0x1,0x1},//26 +		{0x1,0x1,0x2},//27 +		{0x2,0x2,0x3},//28 +		{0x3,0x4,0x5},//29 +		{0x5,0x1,0x7}}, +	{//slime +		{0x1,0x2,0x3}, +		{0x1,0x3,0x2}, +		{0x4,0x8,0x3}, +		{0x7,0xa,0x4}, +		{0x0,0x0,0x0}, +		{0x0,0x0,0x0}, +		{0x0,0x0,0x0}, +		{0x0,0x0,0x0}}, +	{ +		{0x0,0x0,0x0}, +		{0x0,0x0,0x0}, +		{0x0,0x0,0x0}, +		{0x0,0x0,0x0}, +		{0x0,0x0,0x0}, +		{0x0,0x0,0x0}, +		{0x0,0x0,0x0}, +		{0x0,0x0,0x0}}, +	{ +		{0x0,0x0,0x0}, +		{0x0,0x0,0x0}, +		{0x0,0x0,0x0}, +		{0x0,0x0,0x0}, +		{0x0,0x0,0x0}, +		{0x0,0x0,0x0}, +		{0x0,0x0,0x0}, +		{0x0,0x0,0x0}}, +	{ +		{0x0,0x0,0x0}, +		{0x0,0x0,0x0}, +		{0x0,0x0,0x0}, +		{0x0,0x0,0x0}, +		{0x0,0x0,0x0}, +		{0x0,0x0,0x0}, +		{0x0,0x0,0x0}, +		{0x0,0x0,0x0}}, +	{ +		{0x0,0x0,0x0}, +		{0x0,0x0,0x0}, +		{0x0,0x0,0x0}, +		{0x0,0x0,0x0}, +		{0x0,0x0,0x0}, +		{0x0,0x0,0x0}, +		{0x0,0x0,0x0}, +		{0x0,0x0,0x0}}, +	{ +		{0x0,0xf,0xf}, +		{0xf,0xf,0xf}, +		{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(uint8_t tile_idx); + +bool hh_colidable(uint8_t tile_idx); diff --git a/src/makefile b/src/makefile index 96f3108..d7d9087 100644 --- a/src/makefile +++ b/src/makefile @@ -32,7 +32,12 @@ LOCAL_SRCS += main.c \  							ppu/ppu.c \  							demo.c \  							engine/engine.c \ -							engine/draw_screen.c +							engine/sprite_controller.c \ +							engine/player_controller.c \ +							engine/draw_screen.c \ +							engine/camera.c \ +							engine/maths.c \ +							engine/entity.c  CFLAGS += $(SHARED_FLAGS)  LFLAGS += $(SHARED_FLAGS) 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 1fceb82..a5fec45 100644 --- a/src/ppusim/sim.c +++ b/src/ppusim/sim.c @@ -29,20 +29,21 @@ void hh_ppu_init() {  }  void hh_ppu_load_tilemap() { -	char* filename = "tiles.bin"; +	char* filename = "../test/bin/tiles.bin";  	FILE* fp = fopen(filename,"rb");  	if (!fp){ +		fprintf(stderr,"Error: Failed to load tiles.");  		return;//error  	} - +	int sprite_size = (HH_PPU_SPRITE_WIDTH * HH_PPU_SPRITE_HEIGHT);  	fseek(fp, 0, SEEK_END);//goto EOF -	int _size = ftell(fp)/HH_PPU_VRAM_TMM_SPRITE_SIZE; +	int _size = ftell(fp)/sprite_size;  	fseek(fp, 0, 0);//goto start of file  	for (int i = 0; i < _size; i++) { -		uint8_t data[HH_PPU_VRAM_TMM_SPRITE_SIZE]; +		uint8_t data[sprite_size]; -		fread(data,HH_PPU_VRAM_TMM_SPRITE_SIZE,1,fp); +		fread(data,sizeof(uint8_t),sprite_size,fp);  		hh_s_ppu_vram_data sprite = hh_ppu_2nat_sprite(data);  		sprite.offset = i*HH_PPU_VRAM_TMM_SPRITE_SIZE; |