Repository navigation
Expand file tree
/
Copy pathmain.c
More file actions
208 lines (171 loc) · 4.9 KB
/
Copy pathmain.c
File metadata and controls
208 lines (171 loc) · 4.9 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
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
#include "main.h"
#include <SDL3/SDL_audio.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
uint32_t framebuffer[WIDTH * HEIGHT];
Uint8* wav_data = NULL;
SDL_AudioStream* stream;
Uint32 wav_position = 0;
Uint32 wav_data_len = 0;
int main(int argc, char* argv[]) {
UI ui;
initSDL();
initUI(&ui);
initAudio();
bool hasQuit = false;
playAudio();
while (!hasQuit) {
hasQuit = getQuit();
drawBrazil();
playAudio();
updateUI(&ui);
}
destroyAudio();
destroyUI(&ui);
return EXIT_SUCCESS;
}
void drawBrazil() {
const int32_t midPx = WIDTH / 2;
const int32_t midPy = HEIGHT / 2;
uint32_t randPx = arc4random() % WIDTH;
uint32_t randPy = arc4random() % HEIGHT;
// Calculate blue circle
int32_t x = pow(midPx - (int32_t)randPx, 2);
int32_t y = pow(midPy - (int32_t)randPy, 2);
if (sqrt(x + y) <= 50) // Is pixel inside blue circle?
{
putpix(randPx, randPy, BLUE); // Draw blue circle
} else {
putpix(randPx, randPy, GREEN); // Draw green square
// Calculate Yellow dime
if (randPy > midPy) {
x = -((int32_t)randPy - (midPy * 0.4) - midPx) / 0.75;
} else {
x = ((int32_t)randPy - HEIGHT + (midPy * 0.4) + midPx) / 0.75;
}
int minDimeX = midPx - x;
int maxDimeX = midPx + x;
if (randPx > minDimeX && randPx < maxDimeX) {
putpix(randPx, randPy, YELLOW); // Draw yellow dime
}
}
}
void initUI(UI* ui) {
ui->window = SDL_CreateWindow("SDL Framebuffer", WIDTH * 4, HEIGHT * 4, 0);
if (ui->window == NULL) {
fprintf(stderr, "SDL CreateWindow failed: %s \n", SDL_GetError());
SDL_Quit();
exit(EXIT_FAILURE);
}
ui->renderer = SDL_CreateRenderer(ui->window, NULL);
if (ui->renderer == NULL) {
fprintf(stderr, "SDL CreateRenderer failed: %s \n", SDL_GetError());
SDL_DestroyWindow(ui->window);
SDL_Quit();
exit(EXIT_FAILURE);
}
ui->texture = SDL_CreateTexture(
ui->renderer,
SDL_PIXELFORMAT_XRGB8888,
SDL_TEXTUREACCESS_STREAMING,
WIDTH,
HEIGHT);
if (ui->texture == NULL) {
fprintf(stderr, "SDL CreateTexture failed: %s \n", SDL_GetError());
SDL_DestroyRenderer(ui->renderer);
SDL_DestroyWindow(ui->window);
SDL_Quit();
exit(EXIT_FAILURE);
}
SDL_SetTextureScaleMode(ui->texture, SDL_SCALEMODE_NEAREST);
}
void updateUI(UI* ui) {
// Copy the contents of framebuffer to the texture
SDL_UpdateTexture(
ui->texture,
NULL,
framebuffer,
WIDTH * sizeof(uint32_t) // pitch -> the width of the image in bytes
);
// Display the window and the renderer
SDL_RenderClear(ui->renderer);
SDL_RenderTexture(ui->renderer, ui->texture, NULL, NULL);
SDL_RenderPresent(ui->renderer);
}
void destroyUI(UI* ui) {
SDL_DestroyTexture(ui->texture);
SDL_DestroyRenderer(ui->renderer);
SDL_DestroyWindow(ui->window);
SDL_Quit();
}
int putpix(int x, int y, uint32_t clr) {
if (x < 0 || y < 0 || x >= WIDTH || y >= HEIGHT) {
fprintf(stderr, "Pixel outside range (x: %d, y: %d).\n", x, y);
return 1;
}
framebuffer[WIDTH * y + x] = clr;
return 0;
}
bool getQuit() {
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_EVENT_QUIT)
return true;
}
return false;
}
void initSDL() {
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)) {
fprintf(stderr, "SDL Init failed: %s \n", SDL_GetError());
exit(EXIT_FAILURE);
}
}
void initAudio() {
SDL_AudioSpec wav_spec;
if (!SDL_LoadWAV("mihailandrei-praia-de-domingo.wav", &wav_spec, &wav_data, &wav_data_len)) {
fprintf(stderr, "Failed to load WAV file: %s \n", SDL_GetError());
SDL_Quit();
exit(EXIT_FAILURE);
}
stream = SDL_OpenAudioDeviceStream(
SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK,
&wav_spec,
NULL, // Let SDL handle callback-free manual feeding
NULL // No userdata needed
);
if (!stream) {
fprintf(stderr, "Failed to open audio device stream: %s \n", SDL_GetError());
SDL_free(wav_data);
SDL_Quit();
exit(EXIT_FAILURE);
};
SDL_ResumeAudioStreamDevice(stream);
}
void playAudio() {
int queued_bytes = SDL_GetAudioStreamQueued(stream);
// Keep at least two chunks buffered to avoid hardware stutter
if (queued_bytes < (CHUNK_SIZE * 2)) {
Uint32 bytes_remaining = wav_data_len - wav_position;
Uint32 bytes_to_write = CHUNK_SIZE;
if (bytes_to_write > bytes_remaining) {
bytes_to_write = bytes_remaining;
}
// Push a chunk of the WAV memory into the stream
if (bytes_to_write > 0) {
if (!SDL_PutAudioStreamData(stream, &wav_data[wav_position], bytes_to_write)) {
fprintf(stderr, "Failed to push stream data: %s \n", SDL_GetError());
exit(EXIT_FAILURE);
}
wav_position += bytes_to_write;
}
// Loop back to the start if we reached the end of the data file
if (wav_position >= wav_data_len) {
wav_position = 0;
}
}
}
void destroyAudio() {
SDL_DestroyAudioStream(stream);
SDL_free(wav_data);
}