cntrl/src/main.cpp

249 lines
10 KiB
C++

// FBC: NIGHT SHIFT — entry point.
// Owns the window, the fixed 60 Hz tick, and the raylib<->sim translation.
// Everything else lives in src/game (simulation) and src/render (presentation).
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include "raylib.h"
#include "game/game.hpp"
#include "render/presenter.hpp"
#include "render/ui.hpp"
namespace {
constexpr float kFixedDt = 1.0f / 60.0f;
struct Options {
std::uint64_t seed = 0xC0FFEEULL;
const char* screenshot_path = nullptr; // --screenshot <path>: capture the framebuffer
float screenshot_at = 3.0f; // --screenshot-at <sec>: when to capture
bool bot = false; // --bot: kiting autopilot (M6 balance-bot seed)
bool no_damage = false; // --no-damage: stress-test mode
};
Options parse_options(int argc, char** argv) {
Options opts;
for (int i = 1; i < argc; ++i) {
if (std::strcmp(argv[i], "--seed") == 0 && i + 1 < argc) {
opts.seed = std::strtoull(argv[i + 1], nullptr, 10);
} else if (std::strcmp(argv[i], "--screenshot") == 0 && i + 1 < argc) {
opts.screenshot_path = argv[i + 1];
} else if (std::strcmp(argv[i], "--screenshot-at") == 0 && i + 1 < argc) {
opts.screenshot_at = static_cast<float>(std::atof(argv[i + 1]));
} else if (std::strcmp(argv[i], "--bot") == 0) {
opts.bot = true;
} else if (std::strcmp(argv[i], "--no-damage") == 0) {
opts.no_damage = true;
}
}
return opts;
}
InputState read_input(const Game& game, bool bot) {
InputState in;
if (bot) {
// Kiting autopilot: seek the nearest shard, repel enemies that get close,
// dash when cornered. Drives itself.
Vec2 goal = game.player.pos;
float best = 1e9f;
for (const XpShard& s : game.pickup_system.shards) {
const float d = distance(s.pos, game.player.pos);
if (d < best) { best = d; goal = s.pos; }
}
Vec2 dir = normalized(goal - game.player.pos);
float nearest_enemy = 1e9f;
for (const Enemy& e : game.enemy_system.enemies) {
const float d = distance(e.pos, game.player.pos);
nearest_enemy = std::min(nearest_enemy, d);
if (d < 3.5f) dir += normalized(game.player.pos - e.pos) * (3.5f - d) * 0.8f;
}
if (length_sq(dir) > 1.0f) dir = normalized(dir);
in.move_x = dir.x;
in.move_y = dir.y;
in.dash = nearest_enemy < 2.0f;
return in;
}
in.move_x = (IsKeyDown(KEY_D) ? 1.0f : 0.0f) - (IsKeyDown(KEY_A) ? 1.0f : 0.0f);
in.move_y = (IsKeyDown(KEY_S) ? 1.0f : 0.0f) - (IsKeyDown(KEY_W) ? 1.0f : 0.0f);
in.dash = IsKeyPressed(KEY_LEFT_SHIFT) || IsKeyPressed(KEY_SPACE);
in.restart = IsKeyPressed(KEY_R);
return in;
}
} // namespace
int main(int argc, char** argv) {
SetTraceLogLevel(LOG_WARNING);
InitWindow(1280, 720, "FBC: NIGHT SHIFT — pre-alpha");
SetTargetFPS(60);
const Options opts = parse_options(argc, argv);
Game game(opts.seed);
game.config.no_player_damage = opts.no_damage;
Presenter presenter;
presenter.init(game.config.arena_half);
// Full-screen post FX keyed to corruption (docs/ART_DIRECTION.md "Post FX"):
// the frame renders to a texture, then assets/shaders/post_fx.fs draws it
// with VHS noise, chromatic aberration and a red Hiss tint that intensifies
// as the House decays. Missing shader degrades to a plain blit.
RenderTexture2D post_target =
LoadRenderTexture(GetScreenWidth(), GetScreenHeight());
// S12 bloom: half-res bright pass + separable blur, composited in post_fx.
const int bloom_w = GetScreenWidth() / 2;
const int bloom_h = GetScreenHeight() / 2;
RenderTexture2D bloom_a = LoadRenderTexture(bloom_w, bloom_h);
RenderTexture2D bloom_b = LoadRenderTexture(bloom_w, bloom_h);
auto shader_path_for = [](const char* rel) -> std::string {
if (FileExists(rel)) return rel;
char path[512];
std::snprintf(path, sizeof(path), "%s../%s", GetApplicationDirectory(), rel);
return FileExists(path) ? path : std::string();
};
Shader bright_shader{};
Shader blur_shader{};
bool bloom_ok = false;
if (const std::string p = shader_path_for("assets/shaders/bloom_bright.fs"); !p.empty()) {
bright_shader = LoadShader(nullptr, p.c_str());
bloom_ok = bright_shader.id != 0;
}
if (const std::string p = shader_path_for("assets/shaders/bloom_blur.fs"); !p.empty()) {
blur_shader = LoadShader(nullptr, p.c_str());
bloom_ok = bloom_ok && blur_shader.id != 0;
}
const int loc_blur_dir = bloom_ok
? GetShaderLocation(blur_shader, "uDirection") : -1;
const std::string shader_path = FileExists("assets/shaders/post_fx.fs")
? "assets/shaders/post_fx.fs"
: std::string(GetApplicationDirectory()) + "../assets/shaders/post_fx.fs";
Shader post_shader{};
bool post_ok = FileExists(shader_path.c_str());
if (post_ok) {
post_shader = LoadShader(nullptr, shader_path.c_str());
post_ok = post_shader.id != 0;
}
if (!post_ok) {
TraceLog(LOG_WARNING, "POST: %s missing, rendering without post FX",
shader_path.c_str());
}
const int loc_time = GetShaderLocation(post_shader, "uTime");
const int loc_corruption = GetShaderLocation(post_shader, "uCorruption");
const int loc_damage = GetShaderLocation(post_shader, "uDamage");
const int loc_bloom = post_ok
? GetShaderLocation(post_shader, "texture1") : -1;
float accumulator = 0.0f;
bool screenshot_taken = false;
while (!WindowShouldClose()) {
accumulator += GetFrameTime();
if (accumulator > 0.25f) accumulator = 0.25f; // avoid spiral of death
while (accumulator >= kFixedDt) {
game.update(read_input(game, opts.bot), kFixedDt);
accumulator -= kFixedDt;
}
// Scene + UI render into the post-FX target, then the corruption shader
// paints the final frame.
BeginTextureMode(post_target);
ClearBackground(Color{24, 24, 27, 255});
presenter.draw_scene(game);
ui::draw_all(game);
EndTextureMode();
if (post_ok && bloom_ok) {
const Rectangle full = {
0.0f, 0.0f, static_cast<float>(post_target.texture.width),
-static_cast<float>(post_target.texture.height)};
const Rectangle half_src = {
0.0f, 0.0f, static_cast<float>(bloom_w),
-static_cast<float>(bloom_h)};
const Rectangle half_dst = {
0.0f, 0.0f, static_cast<float>(bloom_w),
static_cast<float>(bloom_h)};
BeginTextureMode(bloom_a);
BeginShaderMode(bright_shader);
DrawTexturePro(post_target.texture, full, half_dst,
Vector2{0.0f, 0.0f}, 0.0f, WHITE);
EndShaderMode();
EndTextureMode();
const float dir_h[2] = {1.0f / bloom_w, 0.0f};
const float dir_v[2] = {0.0f, 1.0f / bloom_h};
BeginTextureMode(bloom_b);
BeginShaderMode(blur_shader);
SetShaderValue(blur_shader, loc_blur_dir, dir_h,
SHADER_UNIFORM_VEC2);
DrawTexturePro(bloom_a.texture, half_src, half_dst,
Vector2{0.0f, 0.0f}, 0.0f, WHITE);
EndShaderMode();
EndTextureMode();
BeginTextureMode(bloom_a);
BeginShaderMode(blur_shader);
SetShaderValue(blur_shader, loc_blur_dir, dir_v,
SHADER_UNIFORM_VEC2);
DrawTexturePro(bloom_b.texture, half_src, half_dst,
Vector2{0.0f, 0.0f}, 0.0f, WHITE);
EndShaderMode();
EndTextureMode();
}
BeginDrawing();
if (post_ok) BeginShaderMode(post_shader);
if (post_ok) {
if (bloom_ok && loc_bloom >= 0) {
SetShaderValueTexture(post_shader, loc_bloom,
bloom_a.texture);
}
const float t = static_cast<float>(GetTime());
const float corruption = game.corruption.value();
const float damage = presenter.damage_flash();
SetShaderValue(post_shader, loc_time, &t, SHADER_UNIFORM_FLOAT);
SetShaderValue(post_shader, loc_corruption, &corruption,
SHADER_UNIFORM_FLOAT);
SetShaderValue(post_shader, loc_damage, &damage, SHADER_UNIFORM_FLOAT);
}
DrawTextureRec(post_target.texture,
Rectangle{0.0f, 0.0f,
static_cast<float>(post_target.texture.width),
-static_cast<float>(post_target.texture.height)},
Vector2{0.0f, 0.0f}, WHITE);
if (post_ok) EndShaderMode();
EndDrawing();
// Dev tools: --screenshot <path> [--screenshot-at <sec>] captures the
// framebuffer (works regardless of compositor/window visibility);
// F12 saves one on demand.
if (opts.screenshot_path != nullptr && !screenshot_taken
&& static_cast<float>(GetTime()) >= opts.screenshot_at) {
TakeScreenshot(opts.screenshot_path);
screenshot_taken = true;
}
if (IsKeyPressed(KEY_F12)) TakeScreenshot("screenshot.png");
// The presenter has consumed this frame's sim events.
game.events.clear();
}
presenter.shutdown();
if (post_ok) UnloadShader(post_shader);
UnloadRenderTexture(post_target);
if (bloom_ok) {
UnloadShader(bright_shader);
UnloadShader(blur_shader);
}
UnloadRenderTexture(bloom_a);
UnloadRenderTexture(bloom_b);
CloseWindow();
return 0;
}