chip-8/src/screen.c

51 lines
1.2 KiB
C
Raw Normal View History

2023-12-24 18:46:21 +01:00
#include "screen.h"
2023-12-24 19:59:31 +01:00
SCREEN_DATA Screen;
2023-12-24 18:53:13 +01:00
void ScreenInit(int width, int height){
2023-12-24 19:59:31 +01:00
// 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();
2023-12-24 18:53:13 +01:00
InitWindow(width, height, "Chip-8 Emulator");
2023-12-24 18:46:21 +01:00
SetTargetFPS(60); // Set game to run at 60 frames-per-second
}
2023-12-24 19:59:31 +01:00
void ScreenClear() {
for(int i=0;i<64*32;i++){
Screen.pixels[i]=0;
}
}
2023-12-24 18:46:21 +01:00
void ScreenUpdate(){
BeginDrawing();
ClearBackground(RAYWHITE);
2023-12-24 19:59:31 +01:00
// DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);
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);
}
}
2023-12-24 18:46:21 +01:00
EndDrawing();
}
2023-12-25 07:24:17 +01:00
void ScreenSetPixel(int x, int y, unsigned char state){
2023-12-24 19:59:31 +01:00
Screen.pixels[x+y*64]=(state==0) ? 0: 1;
}
2023-12-24 22:06:22 +01:00
void ScreenClose(){
2023-12-24 18:46:21 +01:00
CloseWindow(); // Close window and OpenGL context
}