aboutsummaryrefslogtreecommitdiff
path: root/src/engine/draw_screen.c
blob: 0c31bf6ab70b7ef683f31573cdadef88c2461949 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "engine/draw_screen.h"
#include "engine/sprite_controller.h"

uint8_t hh_world_to_tile(vec2 pos){
	//TODO: remove magic file name here
    FILE* level = fopen("static/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/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

	fclose(level);
	// int val = tile;
	// free(tile);
	return tile;
}


// remeber old value to know which part to update.
vec2 previousViewport = { .x = 0, .y = 0 };
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,
		.bg_shift_y = viewport.y,
		.fg_fetch	= 0,
		.sysreset	= 0,
	});

	// update previous viewport values
	previousViewport = viewport;
}

void hh_setup_screen(){
	//(HH_map_size_X*HH_map_size_Y)
	int size = 2400; // max X = 40 en max Y = 80
	//TODO: remove magic file name here
	FILE* level = fopen("static/level1_test.bin", "rb"); /* open binary file */
	if (!level) { /* check if file opened successfully */
		fprintf(stderr, "Error: Failed to open level file.\n");
		return;
	}
	fseek(level, (0* sizeof(int)) + sizeof(int), SEEK_SET);
	int* tile = (int*)malloc(size*sizeof(int));
   fread(tile, sizeof(int), size, level); // read 1 tile from binary

	fclose(level);

	for(int BAM_index = 0; BAM_index < size; BAM_index++){
		hh_ppu_update_background(BAM_index, (hh_s_ppu_loc_bam_entry){
			.horizontal_flip = false,
			.vertical_flip   = false,
			.palette_index   = hh_get_palette(tile[BAM_index]),
			.tilemap_index   = tile[BAM_index],
		});
	}
	free(tile);
}
void hh_clear_screen(){
	// (HH_PPU_SCREEN_HEIGHT*HH_PPU_SCREEN_WIDTH)/(HH_PPU_SPRITE_HEIGHT*HH_PPU_SPRITE_WIDTH)
	for (int i = 0; i < HH_PPU_BG_CANVAS_TILES_H*HH_PPU_BG_CANVAS_TILES_V; i++) {
		hh_s_ppu_loc_bam_entry temp = {
			.vertical_flip=false,.horizontal_flip = false,
			.palette_index = 3,.tilemap_index = 0
		};
		hh_ppu_update_background(i,temp);
		hh_ppu_update_color(3,0,(hh_ppu_rgb_color_t){0x0,0x0,0x0});
	}
	hh_ppu_update_aux((hh_s_ppu_loc_aux){
		.bg_shift_x = 0,
		.bg_shift_y = 0,
		.fg_fetch	= 0,
		.sysreset	= 0,
	});
}

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,
		});
	}
}