-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreen.c
83 lines (63 loc) · 1.62 KB
/
screen.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
72
73
74
75
76
77
78
79
80
81
82
83
#include "screen.h"
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int read_screen(int i, int j, unsigned char screen[HEIGHT * WIDTH])
{
return screen[i * WIDTH+j];
}
void write_screen(int i, int j, unsigned char screen[HEIGHT * WIDTH], unsigned char val)
{
screen[i * WIDTH+j] = val;
}
unsigned char write_xor_screen(int i, int j, unsigned char screen[HEIGHT * WIDTH], unsigned char val)
{
unsigned char prev = screen[i * WIDTH+j];
screen[i * WIDTH+j] ^= val;
unsigned char actual = screen[i * WIDTH+j];
if (prev == 1 && actual == 0){
// there was a collition!
return 1;
}
// no collition
return 0;
}
void init_clear_screen(unsigned char screen[HEIGHT * WIDTH])
{
for (int i = 0; i < HEIGHT; ++i){
for (int j = 0; j < WIDTH; ++j){
write_screen(i, j, screen, 0);
}
}
}
void flick_screen(unsigned char screen[HEIGHT * WIDTH])
{
for (int i = 0; i < HEIGHT; ++i){
for (int j = 0; j < WIDTH; ++j){
unsigned char val = ! read_screen(i, j, screen);
write_screen(i, j, screen, val);
}
}
}
// SDL!
void draw_point(SDL_Renderer *renderer, int i, int j){
SDL_Rect point = {
.x = 0,
.y = 0,
.w = CELL_SIZE,
.h = CELL_SIZE,
};
point.x += CELL_SIZE * j;
point.y += CELL_SIZE * i;
SDL_SetRenderDrawColor(renderer, 48, 54, 25, 0);
SDL_RenderFillRect(renderer, &point);
}
void load_screen(SDL_Renderer *renderer, unsigned char screen[HEIGHT * WIDTH]){
for (int i = 0; i < HEIGHT; ++i){
for (int j = 0; j < WIDTH; ++j){
if (read_screen(i, j, screen) == 1){
draw_point(renderer, i, j);
}
}
}
}