Skip to content

Instantly share code, notes, and snippets.

@turbo
Created February 7, 2017 22:30
Show Gist options
  • Save turbo/afb9055f174a2d6041d13c862a2c31bc to your computer and use it in GitHub Desktop.
Save turbo/afb9055f174a2d6041d13c862a2c31bc to your computer and use it in GitHub Desktop.
#define seconds * 1e3
#define bpm 60000 /
#define frames -1
#define GLExt(a) a(wglGetProcAddress(#a))
#define native(t) __declspec(dllimport) t __stdcall
#define MIDINote(hout, base, note, volume, channel) midiOutShortMsg(hout, 0x90 + ((base + note) * 0x100) + (volume * 0x10000) + channel)
// ## FRAMEWORK SETTINGS ##
//~ #define USEWIN // Use a window (not recommended)
#define MODERN_GLSL // One stop shader compiling
#define SOUND // Midi?
// ########################
// ####### DEMO SETTINGS #######
#define XRES 1366
#define YRES 768
#define RUNTIME 15 seconds
#define SONGSPEED bpm 120
#define BEAT_SUSTAIN 10 frames
// #############################
// Compile: cl /c /GS- /O2 /Ob1 /Oi /Os /FAs blah.cpp
/* Link: crinkler.exe
/OUT:".\demo.exe"
opengl32.lib glu32.lib winmm.lib kernel32.lib user32.lib gdi32.lib
winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib
uuid.lib odbc32.lib odbccp32.lib msvcrt.lib libcmt.lib
/SUBSYSTEM:WINDOWS
/ENTRY:"entrypoint"
/CRINKLER
/HASHTRIES:8192
/COMPMODE:SLOW
/ORDERTRIES:32000
/TINYHEADER
/TINYIMPORT
/UNALIGNCODE
/REPORT:.\demo.html
/SATURATE
/UNSAFEIMPORT
/LIBPATH:"C:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\x86"
/RANGE:opengl32
.\blah.obj
*/
// Bootstrap your own windows.h. Using the stock one incurs 10+ bytes
extern "C" {
struct BUTTER_PFDC { int a, b, c; };
typedef void(__stdcall*glUseProgram)(int);
typedef int(__stdcall*glCreateProgram)(void);
typedef int(__stdcall*glCreateShader)(int);
typedef void(__stdcall*glLinkProgram)(int);
typedef void(__stdcall*glCompileShader)(int);
typedef void(__stdcall*glAttachShader)(int,int);
typedef void(__stdcall*glShaderSource)(unsigned shader, int count, const char** string, const int *length);
typedef int(__stdcall*glCreateShaderProgramv)(int, int, const char**);
native(void) glRects(short, short, short, short);
native(int) GetDC(void*);
native(int) GetAsyncKeyState(int);
native(int) SetCursorPos(int, int);
native(int) GetTickCount();
native(int) wglGetProcAddress(const char*);
native(int) wglMakeCurrent(int, int);
native(int) ChoosePixelFormat(int, const BUTTER_PFDC*);
native(int) SetPixelFormat(int, int, const BUTTER_PFDC*);
native(int) wglCreateContext(int);
native(void) SwapBuffers(int);
native(void) ExitProcess(int);
native(int) wsprintfA(char*, char*, ...);
native(int) Beep(int,int);
native(int) midiOutClose(int*);
native(int) midiOutShortMsg(int, long);
native(int) midiOutOpen(int*, int, int, int, int);
}
void entrypoint( void ) {
const static BUTTER_PFDC pfd = {0,0,0x00000020|0x00000001};
#ifdef SOUND
int hMidi = 0;
midiOutClose(&hMidi);
if(midiOutOpen(&hMidi, 0, 0, 0, 0)) goto graceful;
#endif
#ifdef USEWIN
const auto hDC = GetDC(CreateWindow("edit", 0, WS_POPUP|WS_VISIBLE|WS_MAXIMIZE, 0,0,0,0,0,0,0,0));
ShowCursor( 0 );
#else
const auto hDC = GetDC(nullptr);
SetCursorPos(0,YRES);
#endif
SetPixelFormat(hDC, ChoosePixelFormat(hDC, &pfd), &pfd);
wglMakeCurrent(hDC, wglCreateContext(hDC));
#ifndef MODERN_GLSL
const auto p = GLExt(glCreateProgram)();
const auto s = GLExt(glCreateShader)(0x8B30);
#endif
const auto starttime = GetTickCount();
auto beattime = 0;
MIDINote(hMidi, 23, 0, 127, 15);
int frame_sustain = 0;
loop:
char pflog[1024];
const auto elapsed = GetTickCount() - starttime;
const int onbeat = elapsed - beattime > SONGSPEED;
if(onbeat) {
frame_sustain = 1;
Beep(1e3, 10);
beattime = elapsed;
}
if(frame_sustain && frame_sustain < BEAT_SUSTAIN)
frame_sustain++;
else
frame_sustain = 0;
wsprintfA(pflog, "void main(void) {"
"float time = %d. / 1000.;"
"float onbeat = %d.;"
"float t = time, p;"
"vec2 g = gl_FragCoord.xy, s = vec2(1366.,768.), u = (g+g-s)/s.y, ar = vec2(atan(u.x, u.y) * 3.18 + t*2., length(u)*3. + fract(t*.5)*4.);"
"p = floor(ar.y)/5.;"
"ar = abs(fract(ar)-.5);"
"gl_FragColor = mix(vec4(1,.3,0,1), vec4(.3,.2,.5,1), vec4(fract((p)))) * .1/dot(ar,ar);"
"gl_FragColor.a = 1.;"
"if(onbeat > 0.) gl_FragColor += 1. / onbeat;"
"}", elapsed, frame_sustain);
const char *blorg = pflog;
#ifdef MODERN_GLSL
GLExt(glUseProgram)(GLExt(glCreateShaderProgramv)(0x8B30, 1, &blorg));
#else
GLExt(glShaderSource)(s, 1, &blorg, 0);
GLExt(glCompileShader)(s);
GLExt(glAttachShader)(p,s);
GLExt(glLinkProgram)(p);
GLExt(glUseProgram)(p);
#endif
glRects(-1,-1,1,1);
SwapBuffers(hDC);
if(GetAsyncKeyState(27) || elapsed > RUNTIME) goto graceful;
goto loop;
graceful:
ExitProcess(
#ifdef SOUND
midiOutClose(&hMidi)
#else
0
#endif
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment