cntrl/src/main.cpp

403 lines
17 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/layout.hpp"
#include "render/presenter.hpp"
#include "render/ui.hpp"
#include "render/clay_ui.hpp"
#include "render/ui_font.hpp"
namespace {
constexpr float kFixedDt = 1.0f / 60.0f;
constexpr int k1080pWidth = 1920;
constexpr int k1080pHeight = 1080;
constexpr int k720pWidth = 1280;
constexpr int k720pHeight = 720;
struct Options {
std::uint64_t seed = 0xC0FFEEULL;
int width = k1080pWidth; // 1080p by default; --720p for the old size
int height = k1080pHeight;
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
bool post_fx = false; // --post-fx: opt into graded/bloomed look
bool hiss_test = false; // --hiss-test: close-up of one Hiss Brute
bool room_test = false; // --room-test: frozen look at the open-plan room
bool aoe_test = false; // --aoe-test: repeating area blast for VFX QA
bool fullscreen = false; // --fullscreen: borderless fullscreen
float fov = 0.0f; // --fov <deg>: initial camera FOV
float cam_height = 0.0f; // --cam-height <m>: initial camera height
float cam_forward = 0.0f; // --cam-forward <m>: initial forward offset
};
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;
} else if (std::strcmp(argv[i], "--1080p") == 0) {
opts.width = k1080pWidth;
opts.height = k1080pHeight;
} else if (std::strcmp(argv[i], "--720p") == 0) {
opts.width = k720pWidth;
opts.height = k720pHeight;
} else if (std::strcmp(argv[i], "--post-fx") == 0) {
opts.post_fx = true;
} else if (std::strcmp(argv[i], "--hiss-test") == 0) {
opts.hiss_test = true;
} else if (std::strcmp(argv[i], "--room-test") == 0) {
opts.room_test = true;
} else if (std::strcmp(argv[i], "--aoe-test") == 0) {
opts.aoe_test = true;
} else if (std::strcmp(argv[i], "--fullscreen") == 0) {
opts.fullscreen = true;
} else if (std::strcmp(argv[i], "--fov") == 0 && i + 1 < argc) {
opts.fov = static_cast<float>(std::atof(argv[i + 1]));
} else if (std::strcmp(argv[i], "--cam-height") == 0 && i + 1 < argc) {
opts.cam_height = static_cast<float>(std::atof(argv[i + 1]));
} else if (std::strcmp(argv[i], "--cam-forward") == 0 && i + 1 < argc) {
opts.cam_forward = static_cast<float>(std::atof(argv[i + 1]));
}
}
return opts;
}
int first_gamepad() {
for (int i = 0; i < 4; ++i) if (IsGamepadAvailable(i)) return i;
return -1;
}
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);
// Gamepad / controller (including DualSense via raylib's generic gamepad API).
const int gp = first_gamepad();
if (gp >= 0) {
float gx = GetGamepadAxisMovement(gp, GAMEPAD_AXIS_LEFT_X);
float gy = GetGamepadAxisMovement(gp, GAMEPAD_AXIS_LEFT_Y);
const float dz = 0.18f;
const float glen = std::sqrt(gx * gx + gy * gy);
if (glen < dz) { gx = 0.0f; gy = 0.0f; }
else {
const float scaled = (glen - dz) / (1.0f - dz);
gx = (gx / glen) * scaled;
gy = (gy / glen) * scaled;
}
if (IsGamepadButtonDown(gp, GAMEPAD_BUTTON_LEFT_FACE_LEFT)) gx -= 1.0f;
if (IsGamepadButtonDown(gp, GAMEPAD_BUTTON_LEFT_FACE_RIGHT)) gx += 1.0f;
if (IsGamepadButtonDown(gp, GAMEPAD_BUTTON_LEFT_FACE_UP)) gy -= 1.0f;
if (IsGamepadButtonDown(gp, GAMEPAD_BUTTON_LEFT_FACE_DOWN)) gy += 1.0f;
in.move_x = std::clamp(in.move_x + gx, -1.0f, 1.0f);
in.move_y = std::clamp(in.move_y + gy, -1.0f, 1.0f);
const bool pad_dash = IsGamepadButtonPressed(gp, GAMEPAD_BUTTON_RIGHT_FACE_DOWN)
|| IsGamepadButtonPressed(gp, GAMEPAD_BUTTON_RIGHT_TRIGGER_1);
if (pad_dash) SetGamepadVibration(gp, 0.25f, 0.25f, 0.08f);
in.dash = in.dash || pad_dash;
in.restart = in.restart || IsGamepadButtonPressed(gp, GAMEPAD_BUTTON_RIGHT_FACE_DOWN)
|| IsGamepadButtonPressed(gp, GAMEPAD_BUTTON_MIDDLE_RIGHT);
}
return in;
}
} // namespace
int main(int argc, char** argv) {
SetTraceLogLevel(LOG_WARNING);
const Options opts = parse_options(argc, argv);
InitWindow(opts.width, opts.height, "FBC: NIGHT SHIFT — pre-alpha");
const int monitor = GetCurrentMonitor();
if (opts.fullscreen) {
SetWindowSize(GetMonitorWidth(monitor), GetMonitorHeight(monitor));
ToggleFullscreen();
} else {
const int max_w = GetMonitorWidth(monitor);
const int max_h = GetMonitorHeight(monitor) - 48;
if (opts.width > max_w || opts.height > max_h) {
const float scale = std::min(static_cast<float>(max_w) / opts.width,
static_cast<float>(max_h) / opts.height);
SetWindowSize(static_cast<int>(opts.width * scale),
static_cast<int>(opts.height * scale));
}
}
// Best-effort SDL GameControllerDB mappings for hotplug/DualSense detection.
const char* db_candidates[] = {
"assets/gamecontrollerdb.txt",
"../assets/gamecontrollerdb.txt",
"gamecontrollerdb.txt",
};
for (const char* c : db_candidates) {
if (!FileExists(c)) continue;
char* text = LoadFileText(c);
if (text != nullptr) { SetGamepadMappings(text); UnloadFileText(text); break; }
}
SetTargetFPS(60);
ui_font::init();
ui_font::init_gui_style();
clay_ui::init();
Game game(opts.seed);
game.state = (opts.bot || opts.hiss_test || opts.room_test || opts.aoe_test)
? GameState::Playing : GameState::Title;
game.config.no_player_damage = opts.no_damage;
Presenter presenter;
presenter.init(game.config.arena_half);
if (opts.fov > 0.0f) presenter.camera_tuning().fov = opts.fov;
if (opts.cam_height > 0.0f) presenter.camera_tuning().height = opts.cam_height;
if (opts.cam_forward > 0.0f) presenter.camera_tuning().forward = opts.cam_forward;
if (opts.aoe_test) {
game.player.pos = {-70.0f, 46.0f};
game.enemy_system.clear();
}
if (opts.hiss_test) {
presenter.set_hiss_test(true);
game.player.pos = {12.0f, 12.0f};
game.enemy_system.clear();
game.enemy_system.spawn(EnemyType::HissBrute, Vec2{0.0f, -4.0f},
game.corruption);
Enemy& brute = game.enemy_system.enemies[0];
brute.speed = 0.0f;
brute.bob_phase = 0.0f;
brute.mods.burn_dps = 0.4f;
brute.mods.speed_mult = 0.6f;
brute.mods.damage_taken_mult = 1.3f;
}
if (opts.room_test) {
game.player.pos = {layout::kOpenRoom.cx, layout::kOpenRoom.cz - 2.0f};
game.enemy_system.clear();
}
// The scene renders to a texture and is blitted raw by default. --post-fx
// opts into the corruption grade, bloom, VHS noise and damage flash from
// assets/shaders/post_fx.fs (docs/ART_DIRECTION.md "Post FX").
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;
const bool use_post = opts.post_fx && post_ok;
const bool use_bloom = use_post && bloom_ok;
float accumulator = 0.0f;
float aoe_clock = 0.0f;
int aoe_seq = 0;
bool screenshot_taken = false;
while (!WindowShouldClose()) {
accumulator += GetFrameTime();
if (accumulator > 0.25f) accumulator = 0.25f; // avoid spiral of death
while (accumulator >= kFixedDt) {
if (!opts.hiss_test && !opts.room_test && !opts.aoe_test) {
const InputState in = read_input(game, opts.bot);
if (in.restart) presenter.reset();
game.update(in, kFixedDt);
}
accumulator -= kFixedDt;
}
if (opts.aoe_test) {
aoe_clock += GetFrameTime();
if (aoe_clock >= 1.6f) {
aoe_clock = 0.0f;
const Vec2 spots[] = {{-82.0f, 46.0f}, {-70.0f, 34.0f},
{-76.0f, 58.0f}, {-64.0f, 46.0f}};
game.events.push(EventType::AreaDetonation,
spots[aoe_seq++ % 4], 4.5f);
}
}
// Only the scene renders into the post-FX target: bloom, corruption and
// the damage flash must never tint the HUD.
BeginTextureMode(post_target);
ClearBackground(Color{24, 24, 27, 255});
presenter.draw_scene(game);
EndTextureMode();
if (use_bloom) {
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 (use_post) BeginShaderMode(post_shader);
if (use_post) {
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 (use_post) EndShaderMode();
const Vector2 mouse = GetMousePosition();
clay_ui::begin_frame(mouse.x, mouse.y, IsMouseButtonDown(MOUSE_LEFT_BUTTON), GetFrameTime());
if (game.state == GameState::Title) ui::draw_menu(game);
clay_ui::end_frame(GetFrameTime());
ui::draw_all(game);
if (game.state != GameState::Title) ui::draw_camera_dev(presenter.camera_tuning());
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();
clay_ui::shutdown();
ui_font::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;
}