-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.c
345 lines (305 loc) · 9.89 KB
/
main.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include <OpenGL/OpenGL.h>
#include <ApplicationServices/ApplicationServices.h>
#include <OpenGL/glu.h>
#include <OpenGL/CGLTypes.h>
#include <OpenGL/CGLContext.h>
#include <Carbon/Carbon.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
#include <OpenAL/al.h>
#include "alut.h"
#include "4klang/4klang.inh"
#include "shader.minified.frag"
const char* ansiEscapeClearScreen = "\033[2J";
const char* ansiEscapeCursorReset = "\033[0;0H";
const char* ansiEscapeQueryCursor = "\033[6n";
const char* iEmojiAtlas[]={
/* 0 */ "🔴", // rainbow
/* 1 */ "😡", // rainbow
/* 2 */ "🤗", // rainbow
/* 3 */ "🌕", // rainbow
/* 4 */ "🎾", // rainbow
/* 5 */ "🤢", // rainbow
/* 6 */ "🌎", // rainbow
/* 7 */ "🔵", // rainbow
/* 8 */ "😈", // rainbow
/* 9 */ "🍇", // rainbow
/* 10 */ "⚪", // fire plasma
/* 11 */ "🌕", // fire plasma
/* 12 */ "😡", // fire plasma
/* 13 */ "🍇", // fire plasma
/* 14 */ "😈", // fire plasma
/* 15 */ "🌑", // fire plasma
/* 16 */ "🏿", // skintone cube
/* 17 */ "🏾", // skintone cube
/* 18 */ "🏽", // skintone cube
/* 19 */ "🏼", // skintone cube
/* 20 */ "🏻", // skintone cube
/* 21 */ "🌑", // moons
/* 22 */ "🌒", // moons
/* 23 */ "🌓", // moons
/* 24 */ "🌔", // moons
/* 25 */ "🌕", // moons
/* 26 */ "🌖", // moons
/* 27 */ "🌗", // moons
/* 28 */ "🌘", // moons
/* 29 */ "⚫", // grey square tunnel / fizzer
/* 30 */ "🔘", // grey square tunnel / fizzer
/* 31 */ "⚪", // grey square tunnel / fizzer
/* 32 */ " ", // red sdf / constant black
/* 33 */ "💮", // red sdf
/* 34 */ "⭕", // red sdf
/* 35 */ "🔴", // red sdf
/* 36 */ " ", // opening
/* 37 */ "🔸", // opening
/* 38 */ "🌕", // opening
/* 39 */ "⬜", // credits char
/* 40 */ "📕", // rainbow square tunnel
/* 41 */ "📙", // rainbow square tunnel
/* 42 */ "📒", // rainbow square tunnel
/* 43 */ "📗", // rainbow square tunnel
/* 44 */ "📘", // rainbow square tunnel
/* 45 */ " ", // free ?
/* 46 */ " ", // free ?
/* 47 */ " ", // free ?
/* 48 */ " ", // free ?
/* 49 */ " ", // free ?
/* 50 */ " ", // free ?
/* 51 */ " ", // free
/* 52 */ " ", // outrun grid
/* 53 */ "😈", // outrun grid
/* 54 */ "💖", // outrun grid
/* 55 */ "🌸", // outrun grid
/* 56 */ "🍀", // road
/* 57 */ "🥦", // road
/* 58 */ "🎛", // road
/* 59 */ "🗑", // road
/* 60 */ "🌐", // road
/* 61 */ "🎽", // road
/* 62 */ "🔵", // road
/* 63 */ "🌞", // road
};
#define ATLAS_SIZE (sizeof(iEmojiAtlas)/sizeof(iEmojiAtlas[0]))
#if defined(DEBUG)
static_assert(ATLAS_SIZE==64,"Atlas must be 64 entries");
#endif
char* iFixedEmojiAtlas[ATLAS_SIZE];
GLint shaderCompile(const char* fragmentSource)
{
#if defined(DEBUG)
if(!strstr(fragmentSource, "void main()"))
{
puts("could not find a main() function in shader - did you lazily paste this from shadertoy?");
exit(1);
}
#endif
// compile shader
GLuint shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(shader, 1, &fragmentSource, 0);
glCompileShader(shader);
// shader compiler errors
#if defined(DEBUG)
GLint isCompiled = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &isCompiled);
if (isCompiled == GL_FALSE)
{
const int maxLength = 2048;
GLchar errorLog[maxLength];
glGetShaderInfoLog(shader, maxLength, 0, errorLog);
puts(errorLog);
glDeleteShader(shader);
exit(1);
}
#endif
// link shader
GLuint program = glCreateProgram();
glAttachShader(program, shader);
glLinkProgram(program);
return program;
}
// need to disable line-buffering for the size measuring to work
struct termios old_tio, new_tio;
void disableLineBuffering(){
// http://shtrom.ssji.net/skb/getc.html
unsigned char c;
tcgetattr(STDIN_FILENO,&old_tio);
new_tio=old_tio;
new_tio.c_lflag &=(~ICANON & ~ECHO);
tcsetattr(STDIN_FILENO,TCSANOW,&new_tio);
}
void restoreLineBuffering(){
// http://shtrom.ssji.net/skb/getc.html
tcsetattr(STDIN_FILENO,TCSANOW,&old_tio);
}
int queryRenderedWidth(const char* str){
// awful hack to special-case for U+3000 Ideographic Space
if(*(uint32_t*)str == 0x008080E3)
return 2;
printf("%s%s%s\n",ansiEscapeCursorReset,str,ansiEscapeQueryCursor);
int x, y;
scanf("\033[%d;%1dR", &y, &x);
return x - 1;
}
void unpack1bit(const uint8_t* src, uint8_t* dst, int length)
{
for(int i=0;i<length;++i){
dst[i] = ((src[i/8]>>(i%8))&1) ? 255 : 0;
}
}
int main(){
const int kWidth = 40;
const int kHeight = 24;
#if defined(DEBUG) && 0
fputs(ansiEscapeClearScreen,stdout); // clear full screen
fputs(ansiEscapeCursorReset,stdout); // reset cursor
printf("atlas is %lu chars\n", ATLAS_SIZE);
printf("display is %d x %d\n", kWidth, kHeight);
printf("press enter to continue...\n");
getchar();
#endif
#if defined(HAS_AUDIO)
static SAMPLE_TYPE buffer4kl[MAX_SAMPLES*2];
#if defined(LOAD_AUDIO)
FILE* fh = fopen("audio.bin", "rb");
if(fh){
fread(buffer4kl, sizeof(SAMPLE_TYPE), MAX_SAMPLES*2, fh);
fclose(fh);
}else{
puts("failed to load precalc'd audio");
exit(1);
}
#elif defined(THREADED_AUDIO)
// this doesn't work
static pthread_t synthRenderThread;
if (pthread_create(&synthRenderThread, NULL, (threadfunc_t)_4klang_render, buffer4kl)) {
fprintf(stderr, "pthread_create() failed\n");
exit(1);
}
#else
puts("I still didn't figure out how to thread the audio, sorry for the precalc~~~~~~~~");
_4klang_render(buffer4kl);
#endif
#if defined(SAVE_AUDIO)
FILE* fh = fopen("audio.bin", "wb");
if(fh){
fwrite(buffer4kl, sizeof(SAMPLE_TYPE), MAX_SAMPLES*2, fh);
fclose(fh);
}
#endif
#endif
// fix up emoji widths
disableLineBuffering();
for(int i=0;i<ATLAS_SIZE;++i){
int w = queryRenderedWidth(iEmojiAtlas[i]);
//printf("%s|%d\n", iEmojiAtlas[i], w);
int length = strlen(iEmojiAtlas[i]) + (2-w);
iFixedEmojiAtlas[i] = (char*)malloc(length + 1);
memset(iFixedEmojiAtlas[i], ' ', length);
memcpy(iFixedEmojiAtlas[i], iEmojiAtlas[i], strlen(iEmojiAtlas[i]));
iFixedEmojiAtlas[i][length] = 0;
}
restoreLineBuffering();
const CGLPixelFormatAttribute attribs[]={(CGLPixelFormatAttribute)0}; // anything goes.
CGLPixelFormatObj formats;
GLint num_pix;
CGLChoosePixelFormat(attribs,&formats,&num_pix);
CGLContextObj ctx;
CGLCreateContext(formats,0,&ctx); // first hit is good enough for us.
CGLSetCurrentContext(ctx);
GLint shaderProgram;
uint8_t cpuFramebuffer[kWidth*kHeight];
// create framebuffer - can't remember which tutorial i stole this from lmao
GLuint FramebufferName = 0;
GLuint renderedTexture;
{
// The framebuffer, which regroups 0, 1, or more textures, and 0 or 1 depth buffer.
glGenFramebuffers(1, &FramebufferName);
glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName);
// The texture we're going to render to
glGenTextures(1, &renderedTexture);
// "Bind" the newly created texture : all future texture functions will modify this texture
glBindTexture(GL_TEXTURE_2D, renderedTexture);
// Give an empty image to OpenGL ( the last "0" )
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, kWidth, kHeight, 0, GL_RED, GL_UNSIGNED_BYTE, 0);
// Poor filtering. Needed !
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// Set "renderedTexture" as our colour attachement #0
glFramebufferTextureEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, renderedTexture, 0);
// Set the list of draw buffers.
GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0};
glDrawBuffers(1, DrawBuffers); // "1" is the size of DrawBuffers
}
// create credits texture
{
const uint8_t packedCredits[]={
#include "creds.txt"
};
GLuint texid;
glGenTextures(1,&texid);
const int w=40,h=24;
glBindTexture(GL_TEXTURE_2D, texid);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
uint8_t pixels[w*h];
unpack1bit(packedCredits, pixels, w*h);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, w, h, 0, GL_RED, GL_UNSIGNED_BYTE, pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texid);
}
shaderProgram = shaderCompile(shader_frag);
ALuint source,buffer;
alutInit(0,0);
alGenSources(1,&source);
alGenBuffers(1,&buffer);
alBufferData(buffer, AL_FORMAT_STEREO16, buffer4kl, sizeof(SAMPLE_TYPE)*MAX_SAMPLES*2, 44100);
alSourcei(source, AL_BUFFER, buffer);
// set background black - this chokes performance massively on vscode for some reason
//fputs(ansiEscapeBackgroundBlack,stdout);
fputs(ansiEscapeClearScreen,stdout); // clear full screen
fputs(ansiEscapeCursorReset,stdout); // reset cursor
alSourcePlay(source);
float lastBeat;
for(int iFrame=0;;++iFrame)
{
float iTime;
int samplePosition;
alGetSourcef(source,AL_SEC_OFFSET,&iTime);
alGetSourcei(source, AL_SAMPLE_OFFSET, &samplePosition);
float iBeat = (float)((double)samplePosition / SAMPLES_PER_BEAT);
glUseProgram(shaderProgram);
glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName);
glViewport(0,0,kWidth,kHeight);
glTexCoord4f(kWidth, kHeight, iTime, iBeat);
glRecti(-1,-1,1,1);
glSwapAPPLE();
glReadPixels(0,0,kWidth,kHeight,GL_RED,GL_UNSIGNED_BYTE,cpuFramebuffer);
char framebuffer[1048576]; // 1MB should be enough for anyone..
char* writePtr = framebuffer;
memcpy(writePtr, ansiEscapeCursorReset, strlen(ansiEscapeCursorReset)); // reset cursor position
writePtr += strlen(ansiEscapeCursorReset);
for(int y=kHeight;y-->0;){ // OpenGL-style inverted y axis
for(int x=0;x<kWidth;++x){
uint8_t value = cpuFramebuffer[y*kWidth+x];
const char* pixel = iFixedEmojiAtlas[value >> 2];
memcpy(writePtr, pixel, strlen(pixel));
writePtr += strlen(pixel);
}
*writePtr++ = '\n';
}
*writePtr = 0;
fputs(framebuffer,stdout);
fflush(stdout);
usleep(10000);
lastBeat = iBeat;
int state;
alGetSourcei(source, AL_SOURCE_STATE, &state);
if (state!=AL_PLAYING)
break;
}
}