-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstdlib.c
More file actions
137 lines (108 loc) · 3.42 KB
/
stdlib.c
File metadata and controls
137 lines (108 loc) · 3.42 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
time_t start_time;
void __init_time(void) {
start_time = time(NULL);
}
void _time(long long *address) {
*address = time(NULL) - start_time;
}
void _printi(long long i) {
printf("%lld", i);
}
void _printc(long long c) {
printf("%c", (char) c);
}
void _exit(void) {
exit(0);
}
void _readi(long long *address) {
scanf("%lld", address);
}
void _readc(long long *address) {
scanf("%c", (char *) address);
}
#ifndef HEADLESS
#include <SDL2/SDL.h>
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
#define COLOR_R(c) ((c) >> 16) & 0xFF
#define COLOR_G(c) ((c) >> 8) & 0xFF
#define COLOR_B(c) (c) & 0xFF
SDL_Window* window;
SDL_Renderer* renderer;
void __sdl_init_screen(void) {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
exit(1);
}
window = SDL_CreateWindow("SDL Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (window == NULL) {
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
exit(1);
}
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == NULL) {
printf("Renderer could not be created! SDL_Error: %s\n", SDL_GetError());
exit(1);
}
SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);
SDL_RenderClear(renderer);
}
void __sdl_event_loop(void) {
SDL_RenderPresent(renderer);
SDL_Event event;
int quit = 0;
while (!quit) {
while (SDL_PollEvent(&event) != 0) {
if (event.type == SDL_QUIT) {
quit = 1;
}
}
}
SDL_DestroyWindow(window);
SDL_Quit();
}
void _setPixel(long long x, long long y, long long color) {
SDL_SetRenderDrawColor(renderer, COLOR_R(color), COLOR_G(color), COLOR_B(color), SDL_ALPHA_OPAQUE);
SDL_RenderDrawPoint(renderer, x, y);
}
void _drawLine(long long x1, long long y1, long long x2, long long y2, long long color) {
SDL_SetRenderDrawColor(renderer, COLOR_R(color), COLOR_G(color), COLOR_B(color), SDL_ALPHA_OPAQUE);
SDL_RenderDrawLine(renderer, x1, y1, x2, y2);
}
void _drawCircle(long long x0, long long y0, long long radius, long long color) {
const long long diameter = (radius * 2);
long long x = (radius - 1);
long long y = 0;
long long tx = 1;
long long ty = 1;
long long error = (tx - diameter);
SDL_SetRenderDrawColor(renderer, COLOR_R(color), COLOR_G(color), COLOR_B(color), SDL_ALPHA_OPAQUE);
while (x >= y) {
SDL_RenderDrawPoint(renderer, x0 + x, y0 - y);
SDL_RenderDrawPoint(renderer, x0 + x, y0 + y);
SDL_RenderDrawPoint(renderer, x0 - x, y0 - y);
SDL_RenderDrawPoint(renderer, x0 - x, y0 + y);
SDL_RenderDrawPoint(renderer, x0 + y, y0 - x);
SDL_RenderDrawPoint(renderer, x0 + y, y0 + x);
SDL_RenderDrawPoint(renderer, x0 - y, y0 - x);
SDL_RenderDrawPoint(renderer, x0 - y, y0 + x);
if (error <= 0) {
++y;
error += ty;
ty += 2;
}
if (error > 0) {
--x;
tx += 2;
error += (tx - diameter);
}
}
}
void _clearAll(long long color) {
SDL_SetRenderDrawColor(renderer, COLOR_R(color), COLOR_G(color), COLOR_B(color), SDL_ALPHA_OPAQUE);
SDL_RenderClear(renderer);
}
#endif