This commit is contained in:
Tobias Berger 2021-11-18 13:25:50 +01:00
parent 18f2b62f37
commit c6308cc532
3 changed files with 78 additions and 45 deletions

5
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,5 @@
{
"C_Cpp.default.defines": [
"__DEBUG"
]
}

36
.vscode/tasks.json vendored
View file

@ -12,7 +12,7 @@
{ "value": "-ooutput.exe", "quoting": "strong" },
{ "value": "-bench", "quoting": "strong" },
{ "value": "-v", "quoting": "strong" },
{ "value": "-g", "quoting": "strong" },
{ "value": "-b", "quoting": "strong" },
{ "value": "-Iinclude", "quoting": "strong" },
{ "value": "-Llib", "quoting": "strong" },
{ "value": "-stdc99", "quoting": "strong" },
@ -25,6 +25,7 @@
{ "value": "-luser32", "quoting": "strong" },
{ "value": "-lshell32", "quoting": "strong" },
{ "value": "-lwinmm", "quoting": "strong" },
{ "value": "-D__DEBUG", "quoting": "strong" },
],
"group": {
"kind": "build",
@ -36,22 +37,23 @@
"type": "shell",
"command": "tcc",
"args": [
{ "value": "src/**.c", "quoting": "weak" },
{ "value": "-ooutput.exe", "quoting": "strong" },
{ "value": "-bench", "quoting": "strong" },
{ "value": "-v", "quoting": "strong" },
{ "value": "-Iinclude", "quoting": "strong" },
{ "value": "-Llib", "quoting": "strong" },
{ "value": "-stdc99", "quoting": "strong" },
{ "value": "-Wall", "quoting": "strong" },
{ "value": "-Werror", "quoting": "strong" },
{ "value": "-Wwrite-strings", "quoting": "strong" },
{ "value": "-lraylib", "quoting": "strong" },
{ "value": "-lopengl32", "quoting": "strong" },
{ "value": "-lgdi32", "quoting": "strong" },
{ "value": "-luser32", "quoting": "strong" },
{ "value": "-lshell32", "quoting": "strong" },
{ "value": "-lwinmm", "quoting": "strong" },
{ "value": "src/**.c", "quoting": "weak" },
{ "value": "-ooutput.exe", "quoting": "strong" },
{ "value": "-bench", "quoting": "strong" },
{ "value": "-v", "quoting": "strong" },
{ "value": "-Iinclude", "quoting": "strong" },
{ "value": "-Llib", "quoting": "strong" },
{ "value": "-stdc99", "quoting": "strong" },
{ "value": "-Wall", "quoting": "strong" },
{ "value": "-Werror", "quoting": "strong" },
{ "value": "-Wwrite-strings", "quoting": "strong" },
{ "value": "-lraylib", "quoting": "strong" },
{ "value": "-lopengl32", "quoting": "strong" },
{ "value": "-lgdi32", "quoting": "strong" },
{ "value": "-luser32", "quoting": "strong" },
{ "value": "-lshell32", "quoting": "strong" },
{ "value": "-lwinmm", "quoting": "strong" },
{ "value": "-Wl,--subsystem,windows", "quoting": "strong" },
],
"group": "build",
}

View file

@ -7,7 +7,15 @@
#define INITIAL_SCREEN_HEIGHT 900
#define SCREEN_SCALE 8
// #define NOISE_PERIOD 4
#define CAMERA_SPEED (SCREEN_SCALE * 3.0f)
/* Controls
UP/DOWN - Change octaves (Min 1)
LEFT/RIGHT - Change period (Min 1)
SPACE - Change noise type
R - Reset
WASD - Move camera
*/
int main(void)
{
@ -15,18 +23,21 @@ int main(void)
//--------------------------------------------------------------------------------------
int noisePeriod = 4;
#if defined(__DEBUG)
SetTraceLogLevel(LOG_ALL);
#else
SetTraceLogLevel(LOG_NONE);
#endif
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
InitWindow(INITIAL_SCREEN_WIDTH, INITIAL_SCREEN_HEIGHT, "raylib [core] example - basic window");
// SetTargetFPS(5);
fnl_state state = fnlCreateState();
state.seed = 0;
state.frequency = 1.0f / (float)noisePeriod;
state.fractal_type = FNL_FRACTAL_FBM;
state.octaves = 1;
state.noise_type = 0;
fnl_state fnlState = fnlCreateState();
fnlState.seed = 0;
fnlState.frequency = 1.0f / (float)noisePeriod;
fnlState.fractal_type = FNL_FRACTAL_FBM;
fnlState.octaves = 1;
fnlState.noise_type = 0;
int virtualScreenWidth = GetScreenWidth() / SCREEN_SCALE;
int virtualScreenHeight = GetScreenHeight() / SCREEN_SCALE;
@ -35,8 +46,7 @@ int main(void)
*target = LoadRenderTexture(virtualScreenWidth, virtualScreenHeight);
Camera2D camera = {0};
camera.zoom = 1.0f;
Vector2 offset = {0.0f, 0.0f};
Rectangle sourceRec = {0.0f,
0.0f,
@ -49,8 +59,6 @@ int main(void)
//--------------------------------------------------------------------------------------
double offset = 0.0;
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
@ -58,9 +66,9 @@ int main(void)
//----------------------------------------------------------------------------------
if (IsKeyPressed(KEY_SPACE))
{
state.noise_type++;
state.noise_type %= (FNL_NOISE_VALUE + 1);
TraceLog(LOG_INFO, TextFormat("Noisetype: %i", state.noise_type));
fnlState.noise_type++;
fnlState.noise_type %= (FNL_NOISE_VALUE + 1);
TraceLog(LOG_INFO, TextFormat("Noisetype: %i", fnlState.noise_type));
}
if (IsWindowResized())
@ -85,17 +93,17 @@ int main(void)
if (IsKeyPressed(KEY_UP))
{
state.octaves++;
fnlState.octaves++;
}
if (IsKeyPressed(KEY_DOWN))
{
state.octaves--;
fnlState.octaves--;
}
if (IsKeyPressed(KEY_UP) || IsKeyPressed(KEY_DOWN))
{
state.octaves = state.octaves <= 0 ? 1 : state.octaves;
fnlState.octaves = fnlState.octaves <= 0 ? 1 : fnlState.octaves;
TraceLog(LOG_INFO, TextFormat("Octaves: %i", state.octaves));
TraceLog(LOG_INFO, TextFormat("Octaves: %i", fnlState.octaves));
}
if (IsKeyPressed(KEY_LEFT))
@ -109,7 +117,7 @@ int main(void)
if (IsKeyPressed(KEY_LEFT) || IsKeyPressed(KEY_RIGHT))
{
noisePeriod = noisePeriod <= 0 ? 1 : noisePeriod;
state.frequency = 1.0f / (float)noisePeriod;
fnlState.frequency = 1.0f / (float)noisePeriod;
TraceLog(LOG_INFO, TextFormat("Noise period: %i", noisePeriod));
}
@ -117,13 +125,27 @@ int main(void)
if (IsKeyPressed(KEY_R))
{
noisePeriod = 4;
state.frequency = 1.0f / (float)noisePeriod;
fnlState.frequency = 1.0f / (float)noisePeriod;
state.octaves = 1;
fnlState.octaves = 1;
}
float frameTime = GetFrameTime();
offset += (double)frameTime * 10.0 * 0;
if (IsKeyDown(KEY_W))
{
offset.y -= GetFrameTime() * CAMERA_SPEED;
}
if (IsKeyDown(KEY_S))
{
offset.y += GetFrameTime() * CAMERA_SPEED;
}
if (IsKeyDown(KEY_A))
{
offset.x -= GetFrameTime() * CAMERA_SPEED;
}
if (IsKeyDown(KEY_D))
{
offset.x += GetFrameTime() * CAMERA_SPEED;
}
//----------------------------------------------------------------------------------
@ -138,9 +160,13 @@ int main(void)
{
for (int y = 0; y < virtualScreenHeight; y++)
{
int noise = (fnlGetNoise2D(&state, ((double)x) + (int)offset, ((double)y) + (int)offset / 2.0) + 1.0) / 2.0 * 255.0;
float raw_noise = fnlGetNoise2D(&fnlState, (float)x + floorf(offset.x), (float)y + floorf(offset.y));
float noise = (raw_noise + 1.0f) / 2.0f * 255.0f;
DrawPixel(x, y, (Color){255 - noise, noise, noise, 255});
const unsigned char r = noise >= 170 ? 255 : (unsigned char)(noise * 1.5f);
const unsigned char g = 255;
const unsigned char b = 255;
DrawPixel(x, y, (Color){r, g, b, 255});
}
}
@ -150,7 +176,7 @@ int main(void)
{
ClearBackground(RAYWHITE);
DrawTexturePro(target->texture, sourceRec, destRec, (Vector2){0.0f, 0.0f}, 0.0f, WHITE);
char const *drawText = TextFormat("Press [SPACEBAR] to cycle through different noise algorithms, arrow keys to control variables\n%i FPS\nNoise Period (Frequency): %i (%f)\nNoise Octaves: %i", GetFPS(), noisePeriod, state.frequency, state.octaves);
char const *drawText = TextFormat("Press [SPACEBAR] to cycle through different noise algorithms, arrow keys to control variables\n%i FPS\nNoise Period (Frequency): %i (%f)\nNoise Octaves: %i", GetFPS(), noisePeriod, fnlState.frequency, fnlState.octaves);
DrawRectangle(0, 0, MeasureText(drawText, 20), (int)(20 * 5.5), WHITE);
DrawText(drawText, 0, 0, 20, BLACK);
}