Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ux Additions to GUI mode #17

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 149 additions & 5 deletions src/randomart.c
Original file line number Diff line number Diff line change
Expand Up @@ -775,14 +775,20 @@ bool compile_node_func_into_fragment_shader(String_Builder *sb, Node *f)
sb_append_cstr(sb, "in vec2 fragTexCoord;\n");
sb_append_cstr(sb, "out vec4 finalColor;\n");
sb_append_cstr(sb, "uniform float time;\n");

sb_append_cstr(sb, "uniform float amp;\n");
sb_append_cstr(sb, "uniform float freq;\n");

sb_append_cstr(sb, "uniform float x_offset;\n");
sb_append_cstr(sb, "uniform float y_offset;\n");
sb_append_cstr(sb, "vec4 map_color(vec3 rgb) {\n");
sb_append_cstr(sb, " return vec4((rgb + 1)/2.0, 1.0);\n");
sb_append_cstr(sb, "}\n");
sb_append_cstr(sb, "void main()\n");
sb_append_cstr(sb, "{\n");
sb_append_cstr(sb, " float x = fragTexCoord.x*2.0 - 1.0;\n");
sb_append_cstr(sb, " float y = fragTexCoord.y*2.0 - 1.0;\n");
sb_append_cstr(sb, " float t = sin(time);\n");
sb_append_cstr(sb, " float x = fragTexCoord.x*2.0 - x_offset;\n");
sb_append_cstr(sb, " float y = fragTexCoord.y*2.0 - y_offset;\n");
sb_append_cstr(sb, " float t = amp*sin(freq*time);\n");
sb_append_cstr(sb, " finalColor = map_color(");
if (!compile_node_into_fragment_expression(sb, f, 0)) return false;
sb_append_cstr(sb, ");\n");
Expand Down Expand Up @@ -1077,6 +1083,7 @@ int main(int argc, char **argv)

const char *input_path = shift(argv, argc);

GUI:
String_Builder src = {0};
if (!read_entire_file(input_path, &src)) return 1;

Expand Down Expand Up @@ -1114,6 +1121,11 @@ int main(int argc, char **argv)
RenderTexture2D screen = LoadRenderTexture(width, height);
Shader shader = LoadShaderFromMemory(NULL, sb.items);
int time_loc = GetShaderLocation(shader, "time");
int x_offset_loc = GetShaderLocation(shader, "x_offset");
int y_offset_loc = GetShaderLocation(shader, "y_offset");
int amp_loc = GetShaderLocation(shader, "amp");
int freq_loc = GetShaderLocation(shader, "freq");

SetTargetFPS(fps);
SetExitKey(KEY_NULL);
Texture default_texture = {
Expand All @@ -1124,24 +1136,59 @@ int main(int argc, char **argv)
.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8,
};
float time = 0.0f;

float max_render_length = (2*PI)*2;
bool pause = false;
float x_coord = 0;
float y_coord = 0;
float x_zoom = 1;
float y_zoom = 1;
float amp = 1;
float freq = 1;
bool restart = false;

printf("==============================================================================================\n"
"\tUse <↑←↓→> key to MOVE around\n"
"\tUse <WASD> key to ZOOM around\n"
"\tUse <GH> key to ZOOM uniformly\n"
"\tUse <IK> key to adjust AMPLITUDE (t variable)\n"
"\tUse <JL> key to adjust FREQUENCY (t variable)\n"
"\tUse <O> key to set everything to default\n"
"\tUse <Space> key to PAUSE\n"
"\tUse <QE> key to adjust the TIME (only works in pause)\n"
"\tUse <?=> key to get INFORMATION\n"
"\tUse <N> key to start a new run\n"
"==============================================================================================\n");
while (!WindowShouldClose()) {
float w = GetScreenWidth();
float h = GetScreenHeight();
float dt = GetFrameTime();
BeginDrawing();
if (ffmpeg == NULL) {
SetShaderValue(shader, time_loc, &time, SHADER_UNIFORM_FLOAT);
SetShaderValue(shader, x_offset_loc, &x_zoom, SHADER_UNIFORM_FLOAT);
SetShaderValue(shader, y_offset_loc, &y_zoom, SHADER_UNIFORM_FLOAT);
SetShaderValue(shader, amp_loc, &amp, SHADER_UNIFORM_FLOAT);
SetShaderValue(shader, freq_loc, &freq, SHADER_UNIFORM_FLOAT);

BeginShaderMode(shader);
DrawTexturePro(
default_texture,
(Rectangle){0, 0, 1, 1},
(Rectangle){x_coord, y_coord, x_coord+x_zoom, y_coord+y_zoom},
(Rectangle){0, 0, w, h},
(Vector2){0}, 0, WHITE);
EndShaderMode();
if (!pause) time += dt;

if ( pause && IsKeyPressed(KEY_Q)) {
time -= 0.5 * dt;
}
if (pause && IsKeyPressed(KEY_E)) {
time += 0.5 * dt;
}
if (IsKeyPressed(KEY_N)) {
restart = true;
break;
}
if (IsKeyPressed(KEY_R)) {
ffmpeg = ffmpeg_start_rendering(width, height, fps);
time = 0;
Expand All @@ -1150,6 +1197,99 @@ int main(int argc, char **argv)
if (IsKeyPressed(KEY_SPACE)) {
pause = !pause;
}
if (IsKeyPressed(KEY_O)) {
x_zoom = 1;
y_zoom = 1;
x_coord = 0;
y_coord = 0;
amp = 1;
freq = 1;
}
if (IsKeyPressed(KEY_EQUAL) || GetCharPressed() == '?') {
printf("==============================================================================================\n");

printf("pause : %s\n",pause ? "true" : "false");
printf("time : %5.2lf\n",time);

printf("rect : (%5.2lf,\t%5.2lf)\n\n",x_coord,y_coord);

printf(" (%5.2lf,\t%5.2lf)\n\n",x_coord+x_zoom,y_coord+y_zoom);

printf("zoom : (%5.2lf,\t%5.2lf)\n\n",x_zoom,y_zoom);
printf("amp : %5.2lf,\n",amp);
printf("freq : %5.2lf\n",freq);

printf("seed : %d\n\n\n",seed);

printf(
"\tUse <↑←↓→> key to MOVE around\n"
"\tUse <WASD> key to ZOOM around\n"
"\tUse <GH> key to ZOOM uniformly\n"
"\tUse <IK> key to adjust AMPLITUDE (t variable)\n"
"\tUse <JL> key to adjust FREQUENCY (t variable)\n"
"\tUse <O> key to set everything to default\n"
"\tUse <Space> key to PAUSE\n"
"\tUse <QE> key to adjust the TIME (only works in pause)\n"
"\tUse <?=> key to get INFORMATION\n"
"\tUse <N> key to start a new run\n"
"==============================================================================================\n");

}




if (IsKeyPressed(KEY_UP)) {
y_coord += 0.01;
}
if (IsKeyPressed(KEY_DOWN)) {
y_coord -= 0.01;
}

if (IsKeyPressed(KEY_LEFT)) {
x_coord += 0.01;
}
if (IsKeyPressed(KEY_RIGHT)) {
x_coord -= 0.01;
}

if (IsKeyPressed(KEY_W)) {
y_zoom /= 1.2;
}
if (IsKeyPressed(KEY_S)) {
y_zoom *= 1.2;
}

if (IsKeyPressed(KEY_A)) {
x_zoom /= 1.2;
}
if (IsKeyPressed(KEY_D)) {
x_zoom *= 1.2;
}


if (IsKeyPressed(KEY_I) ) {
amp *= 1.2;
}
if (IsKeyPressed(KEY_K)) {
amp /= 1.2;
}

if (IsKeyPressed(KEY_J)) {
freq /= 1.2;
}
if (IsKeyPressed(KEY_L)) {
freq *= 1.2;
}

if (IsKeyPressed(KEY_G)) {
x_zoom /= 1.2;
y_zoom /= 1.2;
}
if (IsKeyPressed(KEY_H)) {
x_zoom *= 1.2;
y_zoom *= 1.2;
}
} else {
if (time < max_render_length) {
BeginTextureMode(screen);
Expand Down Expand Up @@ -1210,6 +1350,10 @@ int main(int argc, char **argv)
EndDrawing();
}
CloseWindow();
if (restart){
seed ^= seed >> 3 ;
goto GUI;
}
return 0;
}

Expand Down