From 7d60741c1393ebc0c572891cfeb19a25c4579ed6 Mon Sep 17 00:00:00 2001 From: Daniel Jones Date: Mon, 17 Aug 2020 15:16:02 +0930 Subject: sprite drawing basic implementation begin implementing sprite drawing, currently draws the chip8 fontset well, no idead about other sprites yet --- chip8.c | 38 +++++++++++++++++++++++++++++++++++++- chip8.h | 3 ++- main.c | 25 +++++++++++++++++++++++-- 3 files changed, 62 insertions(+), 4 deletions(-) diff --git a/chip8.c b/chip8.c index 2ce911f..db2efd2 100644 --- a/chip8.c +++ b/chip8.c @@ -71,7 +71,7 @@ chip8_init() draw_flag = 1; // draw by default i guess delay_timer = 0; sound_timer = 0; - srand(time(NULL)); // seed rng + srand(time(NULL)); /* load font into memory at 0x00: we have 0x00 to 0x1FF free for anything we want */ //TODO: add whole alphabet, then can write a rom to write strings or something @@ -80,3 +80,39 @@ chip8_init() memory[0x0 + i] = chip8_fontset[i]; } } + +void +chip8_draw_sprite(int startx, int starty, uint8_t mem, uint8_t size) +{ + /* + * draw sprite located at loc of height size at startx,starty + */ + + uint8_t byte = 0; + uint8_t mask = 0x1; + uint8_t destbit = 0x0; + for (uint8_t byteoffset = 0; byteoffset < size; byteoffset++) + { + /* loop through each byte from mem to mem+size */ + byte = memory[mem+byteoffset]; + int bit = 0; + for (mask = 0x80; mask != 0; mask >>= 1) + { + if (byte&mask) + { + destbit = 1; + video[WIDTH*starty+(startx+bit)] = 0xFFFFFF; + } + else + { + destbit = 0; + video[WIDTH*starty+(startx+bit)] = 0x0; + } + printf("%d", destbit); + bit++; + } + starty++; + puts(""); + } + puts("---"); +} diff --git a/chip8.h b/chip8.h index 4faa6e4..6018d40 100644 --- a/chip8.h +++ b/chip8.h @@ -13,9 +13,10 @@ #define KEY_SIZE 15 #define MEMORY_SIZE 4096 #define STACK_SIZE 16 -#define MAX_ROM_SIZE 0x1000 - 0x200 // memory size - reseved memory +#define MAX_ROM_SIZE 0x1000 - 0x200 // memory size - reserved memory int load_rom(); void chip8_init(); +void chip8_draw_sprite(int startx, int starty, uint8_t mem, uint8_t size); #endif diff --git a/main.c b/main.c index a3b9539..d549b25 100644 --- a/main.c +++ b/main.c @@ -9,6 +9,7 @@ * http://www.multigesture.net/articles/how-to-write-an-emulator-chip-8-interpreter/ * http://mattmik.com/files/chip8/mastering/chip8.html (lots of info) * http://devernay.free.fr/hacks/chip8/C8TECH10.HTM#0.1 + * http://www.emulator101.com/chip-8-sprites.html */ #define VIDEO_SCALE 10 @@ -24,6 +25,7 @@ void quit(); void usage(char *); extern uint32_t video[WIDTH*HEIGHT]; +extern int draw_flag; void usage(char *program) @@ -54,7 +56,7 @@ void update_video() { static int count = 0; - printf("frame %d\n", count++); + //printf("frame %d\n", count++); /* for (uint32_t i = 0 ;i < WIDTH*HEIGHT; i++) { @@ -66,7 +68,6 @@ update_video() video[2] = (255<<16); */ - toggle_pixel(0, 0); SDL_UpdateTexture(texture, NULL, video, sizeof(video[0])*WIDTH); SDL_RenderClear(renderer); SDL_RenderCopy(renderer, texture, NULL, NULL); @@ -106,7 +107,27 @@ int main(int argc, char *argv[]) const int frame_delay = 1000/fps; uint32_t frame_start; uint32_t frame_time; + int do_quit = 0; + chip8_draw_sprite(0, 0, 0xD*5, 0x5); + chip8_draw_sprite(5, 0, 0xE*5, 0x5); + chip8_draw_sprite(10, 0, 0xA*5, 0x5); + chip8_draw_sprite(15, 0, 0xD*5, 0x5); + chip8_draw_sprite(20, 0, 0xB*5, 0x5); + chip8_draw_sprite(25, 0, 0xE*5, 0x5); + chip8_draw_sprite(30, 0, 0xE*5, 0x5); + chip8_draw_sprite(35, 0, 0xf*5, 0x5); + + chip8_draw_sprite(0, 9, 0xD*5, 0x5); + chip8_draw_sprite(5, 9, 0xE*5, 0x5); + chip8_draw_sprite(10, 9, 0xA*5, 0x5); + chip8_draw_sprite(15, 9, 0xD*5, 0x5); + chip8_draw_sprite(20, 9, 0xB*5, 0x5); + chip8_draw_sprite(25, 9, 0xE*5, 0x5); + chip8_draw_sprite(30, 9, 0xE*5, 0x5); + chip8_draw_sprite(35, 9, 0xf*5, 0x5); + + while(!do_quit) { frame_start = SDL_GetTicks(); -- cgit v1.2.3