-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.c
71 lines (57 loc) · 2.08 KB
/
display.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
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
//SDL Display file for Chip8 Emulator Project
//Contains functions to do with the 64x32 pixel display
//Written by Jasper Grant 2023-05-01
#include "main.h"
//Declare globals for window, renderer, and texture
//Global to be used in execution functions
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
SDL_Texture* texture = NULL;
SDL_Rect rect;
//All imported globals explained in main.h
unsigned char registers[16];
unsigned char display[64][32];
void display_init(void){
//Attempt to init SDL
if(SDL_Init(SDL_INIT_VIDEO) < 0){
printf("Error: Failed to initialized SDL2\n");
exit(-1);
}
//Initialize window variable
window = SDL_CreateWindow("Chip-8",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
SCREEN_WIDTH, SCREEN_HEIGHT, 0);
//Initialize renderer that draws shapes
renderer = SDL_CreateRenderer(window, -1, 0);
//Set height and width of pixels on screen
rect.w = PIXEL_WIDTH;
rect.h = PIXEL_HEIGHT;
//Set color of pixels
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
}
unsigned char draw_pixel(unsigned char x, unsigned char y){
//Assign values from coordinate variables to rectangle representing pixel
//Remainders are there to handle wrapping around screen
rect.x = x * PIXEL_WIDTH;
rect.y = y * PIXEL_WIDTH;
//If this pixel is already drawn
if(display[x][y] == 1){
//Set color to undraw pixel
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
//Set pixel to 0 in array
display[x][y] = 0;
}
else{
//Mark that a pixel has been set
display[x][y] = 1;
}
SDL_RenderFillRect(renderer, &rect);
SDL_RenderPresent(renderer);
//Set color back to default regardless
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
//This return statement will only give 1 if a drawn pixel is now undrawn
//In cases like CLS where we do not care about this,
//simply do not catch return value
return !display[x][y];
}