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
|
#include "screen.h"
SCREEN_DATA Screen;
void ScreenInit(int width, int height){
// Init emulated screen:
Screen.width=width;
Screen.height=height;
int px_width=width/64;
int px_height=height/32;
Screen.pixel=(px_width < px_height) ? px_width: px_height;
Screen.originX=(width-64*Screen.pixel)/2;
Screen.originY=(height-32*Screen.pixel)/2;
ScreenClear();
SetTraceLogLevel(LOG_ERROR); // Disable anoying raylib logs
InitWindow(width, height, "Chip-8 Emulator by Loïc Guégan");
}
void ScreenClear() {
for(int i=0;i<64*32;i++){
Screen.pixels[i]=0;
}
}
void ScreenUpdate(){
BeginDrawing();
ClearBackground(RAYWHITE);
for(int x=0;x<64;x++){
for(int y=0;y<32;y++){
if(Screen.pixels[x+y*64] == 0)
DrawRectangle(Screen.originX+Screen.pixel*x,Screen.originY+Screen.pixel*y,Screen.pixel,Screen.pixel,BLACK);
else
DrawRectangle(Screen.originX+Screen.pixel*x,Screen.originY+Screen.pixel*y,Screen.pixel,Screen.pixel,WHITE);
}
}
EndDrawing();
}
char ScreenPixelApply(int x, int y, unsigned char state){
char flag=0;
// Toggle pixel if state is on
if(state){
if(Screen.pixels[x+y*64]){
Screen.pixels[x+y*64]=0;
flag=1;
}
else{
Screen.pixels[x+y*64]=1;
}
}
return flag;
}
void ScreenWH(int *width, int *height){
*width=64;
*height=32;
}
void ScreenClose(){
CloseWindow(); // Close window and OpenGL context
}
|