-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpixel.c
260 lines (221 loc) · 7.4 KB
/
pixel.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include "pixel.h"
// in milliseconds
const uint32_t sleepTable[10] = {1000, 500, 250, 100, 50, 10, 5, 2, 1, 0};
const size_t sleepTableSize = sizeof(sleepTable) / sizeof(sleepTable[0]);
struct Pixel *pixelInit(const char *windowName, size_t width, size_t height){
struct Pixel *p = malloc(sizeof(struct Pixel));
p->size = width * height * 4;
p->width = width;
p->height = height;
p->run = true;
p->sleepDuration.tv_nsec = 0;
pixelSetIndexSpeed(p, sleepTableSize / 2);
// Initialize SDL2
if(SDL_Init(SDL_INIT_EVERYTHING) != 0){
printf("Error initializing SDL: %s\n", SDL_GetError());
exit(EXIT_FAILURE);
}
// Create a window and renderer
p->window = SDL_CreateWindow(windowName, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, p->width, p->height, SDL_WINDOW_SHOWN);
p->renderer = SDL_CreateRenderer(p->window, -1, SDL_RENDERER_ACCELERATED);
p->texture = SDL_CreateTexture(p->renderer, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STATIC, p->width, p->height);
p->buffer = malloc(sizeof(uint8_t) * p->size);
p->rgba = malloc(sizeof(uint8_t) * 4);
pixelSetColor(p, 0, 0, 0, 255);
return p;
}
void pixelSetIndexSpeed(struct Pixel *p, size_t index){
p->sleepIndex = index;
p->sleepDuration.tv_nsec = sleepTable[p->sleepIndex] * 1000000;
p->sleepDuration.tv_sec = p->sleepDuration.tv_nsec / 1000000000;
p->sleepDuration.tv_nsec %= 1000000000;
}
void pixelSetSpeed(struct Pixel *p, bool speed){
if(speed){
pixelSetIndexSpeed(p, sleepTableSize - 1);
}
else{
pixelSetIndexSpeed(p, 0);
}
}
int waitFor(struct Pixel *p, SDL_Keycode code){
for(ever){
while(SDL_PollEvent(&p->event)){
if(p->event.type == SDL_KEYUP){
SDL_Keycode key = p->event.key.keysym.sym;
if(key == SDLK_q || key == SDLK_ESCAPE){
p->run = false;
return PIXEL_EXIT;
}
else if(key == code){
return PIXEL_CONTINUE;
}
}
else if(p->event.type == SDL_QUIT){
p->run = false;
return PIXEL_EXIT;
}
}
}
return PIXEL_CONTINUE;
}
int pixelEvents(struct Pixel *p){
while(SDL_PollEvent(&p->event)){
if(p->event.type == SDL_KEYDOWN){
SDL_Keycode key = p->event.key.keysym.sym;
if(key == SDLK_q || key == SDLK_ESCAPE){
p->run = false;
return PIXEL_EXIT;
}
else if(key == SDLK_RETURN){
return PIXEL_RUN;
}
else if(key == SDLK_s){
FILE *file;
char fileName[50];
snprintf(fileName, 50, "screenShot%ld.raw", (long)time(NULL));
file = fopen(fileName, "w");
if(file == NULL){
fprintf(stderr, "file \"%s\" couldn't be opened\n", fileName);
}
else{
fwrite(p->buffer, sizeof(uint8_t), p->size, file);
fclose(file);
printf("\"%s\" saved\n", fileName);
}
}
}
else if(p->event.type == SDL_KEYUP){
const SDL_Keycode key = p->event.key.keysym.sym;
if(key == SDLK_0){
pixelSetSpeed(p, 0);
}
else if(key == SDLK_9){
pixelSetSpeed(p, 1);
}
else if(key == SDLK_DOWN){
if(0 < p->sleepIndex){
pixelSetIndexSpeed(p, p->sleepIndex - 1);
}
}
else if(key == SDLK_UP){
if(p->sleepIndex < sleepTableSize - 1){
pixelSetIndexSpeed(p, p->sleepIndex + 1);
}
}
else if(key == SDLK_SPACE){
return waitFor(p, SDLK_SPACE);
}
}
else if(p->event.type == SDL_QUIT){
p->run = false;
return PIXEL_EXIT;
}
}
return PIXEL_CONTINUE;
}
void pixelWait(struct Pixel *p){
nanosleep(&p->sleepDuration, NULL);
}
void pixelWaitExit(struct Pixel *p){
while(pixelEvents(p) != PIXEL_EXIT && p->run){}
}
void pixelFree(struct Pixel *p){
free(p->buffer);
free(p->rgba);
SDL_DestroyRenderer(p->renderer);
SDL_DestroyWindow(p->window);
SDL_Quit();
}
void pixelSetColor(struct Pixel *p, uint8_t r, uint8_t g, uint8_t b, uint8_t a){
p->rgba[0] = r;
p->rgba[1] = g;
p->rgba[2] = b;
p->rgba[3] = a;
}
void pixelUpdate(struct Pixel *p){
SDL_UpdateTexture(p->texture, NULL, p->buffer, 4 * p->width);
// SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_NONE);
SDL_RenderCopy(p->renderer, p->texture, NULL, NULL);
SDL_RenderPresent(p->renderer);
#ifdef CAPTURE_ON
char fileName[6];
const char folderName[11] = "./Capture/";
char filePath[21];
if(capture == 2){
struct stat st = {0};
if(stat(folderName, &st) == -1){
mkdir(folderName, 0777);
printf("Folder was made\n");
}
capture = 1;
}
if(capture == 1){
// TODO: Posible integer overflow
// uint16_t = 2 ^ 16 - 1 => max number of didgets is 5 + \0
snprintf(fileName, 6, "%05hd", captureIndex++);
strncpy(filePath, folderName, 11);
strncat(filePath, fileName, 6);
strncat(filePath, ".png", 5);
// printf("Path: %s\n", filePath);
savePng(filePath, buffer, WINDOWWIDTH, WINDOWHEIGHT);
}
#endif
}
void pixelClear(struct Pixel *p){
for(size_t i = 0; i < p->size; i++){
p->buffer[i] = 0;
}
}
void pixelFill(struct Pixel *p){
for(size_t y = 0; y < p->height; y++){
for(size_t x = 0; x < p->width; x++){
const size_t i = 4 * (y * p->width + x);
p->buffer[i + 0] = p->rgba[0];
p->buffer[i + 1] = p->rgba[1];
p->buffer[i + 2] = p->rgba[2];
p->buffer[i + 3] = p->rgba[3];
}
}
}
void pixelDraw(struct Pixel *p, int x, int y){
if(x < 0 || (int)p->width <= x || y < 0 || (int)p->height <= y){
return;
}
const size_t i = 4 * (y * p->width + x);
p->buffer[i + 0] = p->rgba[0];
p->buffer[i + 1] = p->rgba[1];
p->buffer[i + 2] = p->rgba[2];
p->buffer[i + 3] = p->rgba[3];
}
void drawLine(struct Pixel *p, int x0, int y0, int x1, int y1){
int dx = abs(x1 - x0);
int sx = (x0 < x1) ? 1 : -1;
int dy = -abs(y1 - y0);
int sy = (y0 < y1) ? 1 : -1;
int err = dx + dy, e2; /* error value e_xy */
for(;;){
pixelDraw(p, x0, y0);
if(x0 == x1 && y0 == y1){ break; }
e2 = 2 * err;
if(e2 >= dy){ err += dy; x0 += sx; } /* e_xy+e_x > 0 */
if(e2 <= dx){ err += dx; y0 += sy; } /* e_xy+e_y < 0 */
}
}
double calculateResolution(int radius){
return 360.0 / (2.0 * abs(radius) * M_PI);
}
void drawCircle(struct Pixel *p, int x, int y, int radius){
double resolution = calculateResolution(radius);
for(double i = 0.0; i < 360.0; i += resolution){
int angle = i;
int x1 = radius * cos(angle * M_PI / 180.0);
int y1 = radius * sin(angle * M_PI / 180.0);
pixelDraw(p, x + x1, y + y1);
}
}
void drawRectangle(struct Pixel *p, int x, int y, int width, int height){
for(int i = 0; i < height; i++){
drawLine(p, x, y + i, x + width, y + i);
}
}