-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchip8screen.c
40 lines (32 loc) · 1.12 KB
/
chip8screen.c
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
//
// Created by Paltrickontpb on 30-11-2020.
//
#include "chip8screen.h"
#include <assert.h>
#include <memory.h>
static void chip8_screen_check_bounds(int x, int y){
assert(x>=0 && x<CHIP8_WIDTH && y>=0 && y<CHIP8_HEIGHT);
}
void chip8_clear_screen(struct chip8_screen *screen){
memset(screen->pixels, 0, sizeof(screen->pixels));
}
void chip8_screen_set(struct chip8_screen *screen, int x, int y){
chip8_screen_check_bounds(x,y);
screen->pixels[y][x] = true;
}
bool chip8_screen_is_set(struct chip8_screen *screen, int x, int y){
chip8_screen_check_bounds(x,y);
return screen->pixels[y][x];
}
bool chip8_screen_draw_sprite(struct chip8_screen *screen, int x, int y, const char *sprite, int num){
bool pixel_collision = false;
for(int ly=0; ly < num; ly++){
char c = sprite[ly];
for(int lx=0; lx<8; lx++){
if((c & (0b10000000 >> lx)) == 0) continue;
if(screen->pixels[(ly+y) % CHIP8_HEIGHT][(lx+x) % CHIP8_WIDTH]) pixel_collision = true;
screen->pixels[(ly+y) % CHIP8_HEIGHT][(lx+x) % CHIP8_WIDTH] ^= true;
}
}
return pixel_collision;
}