cntrl/src/main.cpp

520 lines
22 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"
#include "render/profiler.hpp"
#include "render/graphics_settings.hpp"
namespace {
constexpr float kFixedDt = 1.0f / 60.0f;
// 1400x900 fits under the monitor chrome on a 1080p display without the
// window clamp scaling it down. 720p/1080p stay as explicit test resolutions.
constexpr int kDevWidth = 1400;
constexpr int kDevHeight = 900;
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 = kDevWidth; // dev default; --720p/--1080p for testing
int height = kDevHeight;
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 uncapped = false; // --uncapped: remove the 60 FPS target for headroom tests
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 floor_overview = false; // --floor-overview: whole-floor dev camera
bool plaza_test = false; // --plaza-test: start in the central plaza
bool office_test = false; // --office-test: start in the office suite
bool fullscreen = false; // --fullscreen: borderless fullscreen
int aniso = 0; // --aniso 0|4|8: concrete texture filtering
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], "--dev") == 0) {
opts.width = kDevWidth;
opts.height = kDevHeight;
} 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], "--uncapped") == 0) {
opts.uncapped = 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], "--floor-overview") == 0) {
opts.floor_overview = true;
} else if (std::strcmp(argv[i], "--plaza-test") == 0) {
opts.plaza_test = true;
} else if (std::strcmp(argv[i], "--office-test") == 0) {
opts.office_test = true;
} else if (std::strcmp(argv[i], "--fullscreen") == 0) {
opts.fullscreen = true;
} else if (std::strcmp(argv[i], "--aniso") == 0 && i + 1 < argc) {
opts.aniso = std::atoi(argv[i + 1]);
} 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;
}
struct InputFrame {
int pad = -1;
bool dash = false;
bool restart = false;
};
bool pad_has_input(int pad) {
for (int b = GAMEPAD_BUTTON_LEFT_FACE_UP; b <= GAMEPAD_BUTTON_RIGHT_THUMB; ++b) {
if (IsGamepadButtonDown(pad, b)) return true;
}
const float lx = GetGamepadAxisMovement(pad, GAMEPAD_AXIS_LEFT_X);
const float ly = GetGamepadAxisMovement(pad, GAMEPAD_AXIS_LEFT_Y);
const float rx = GetGamepadAxisMovement(pad, GAMEPAD_AXIS_RIGHT_X);
const float ry = GetGamepadAxisMovement(pad, GAMEPAD_AXIS_RIGHT_Y);
return lx * lx + ly * ly > 0.09f || rx * rx + ry * ry > 0.09f;
}
int select_gamepad(int current) {
if (current >= 0 && IsGamepadAvailable(current) && pad_has_input(current)) return current;
for (int i = 0; i < 4; ++i) {
if (IsGamepadAvailable(i) && pad_has_input(i)) return i;
}
if (current >= 0 && IsGamepadAvailable(current)) return current;
for (int i = 0; i < 4; ++i) if (IsGamepadAvailable(i)) return i;
return -1;
}
void poll_input(InputFrame& frame) {
frame.pad = select_gamepad(frame.pad);
frame.dash = frame.dash || IsKeyPressed(KEY_LEFT_SHIFT) || IsKeyPressed(KEY_SPACE);
frame.restart = frame.restart || IsKeyPressed(KEY_R);
if (frame.pad >= 0) {
const bool confirm = IsGamepadButtonPressed(frame.pad, GAMEPAD_BUTTON_RIGHT_FACE_DOWN);
frame.dash = frame.dash || confirm
|| IsGamepadButtonPressed(frame.pad, GAMEPAD_BUTTON_RIGHT_TRIGGER_1);
frame.restart = frame.restart || confirm
|| IsGamepadButtonPressed(frame.pad, GAMEPAD_BUTTON_MIDDLE_RIGHT);
}
}
InputState read_input(const Game& game, bool bot, const InputFrame& frame) {
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);
// Gamepad / controller (including DualSense via raylib's generic gamepad API).
if (frame.pad >= 0) {
float gx = GetGamepadAxisMovement(frame.pad, GAMEPAD_AXIS_LEFT_X);
float gy = GetGamepadAxisMovement(frame.pad, 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(frame.pad, GAMEPAD_BUTTON_LEFT_FACE_LEFT)) gx -= 1.0f;
if (IsGamepadButtonDown(frame.pad, GAMEPAD_BUTTON_LEFT_FACE_RIGHT)) gx += 1.0f;
if (IsGamepadButtonDown(frame.pad, GAMEPAD_BUTTON_LEFT_FACE_UP)) gy -= 1.0f;
if (IsGamepadButtonDown(frame.pad, 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);
}
in.dash = frame.dash;
in.restart = frame.restart;
return in;
}
} // namespace
int main(int argc, char** argv) {
SetTraceLogLevel(LOG_WARNING);
const Options opts = parse_options(argc, argv);
// The future graphics menu writes this same setting; --aniso lets us
// exercise it from the CLI until that menu exists.
gfx::graphics_settings().concrete_filter = gfx::filtering_from_aniso(opts.aniso);
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));
}
}
SetTargetFPS(opts.uncapped ? 0 : 60);
PROF_INIT();
PROF_SET_UNCAPPED(opts.uncapped);
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
|| opts.floor_overview || opts.plaza_test || opts.office_test)
? GameState::Playing : GameState::Title;
game.config.no_player_damage = opts.no_damage;
Presenter presenter;
presenter.init(game.config.arena_half, game.floor);
TraceLog(LOG_INFO, "GFX: concrete filtering = %s",
gfx::filtering_name(gfx::graphics_settings().concrete_filter));
presenter.set_floor_overview(opts.floor_overview);
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.plaza_test) {
for (const FloorRoom& r : game.floor.rooms()) {
if (r.kind == RoomKind::Plaza) {
game.player.pos = {(r.rect.x0 + r.rect.x1) * 0.5f,
(r.rect.z0 + r.rect.z1) * 0.5f};
break;
}
}
game.enemy_system.clear();
}
if (opts.office_test) {
for (const FloorRoom& r : game.floor.rooms()) {
if (r.kind == RoomKind::OfficeSuite) {
game.player.pos = {(r.rect.x0 + r.rect.x1) * 0.5f,
(r.rect.z0 + r.rect.z1) * 0.5f};
break;
}
}
game.enemy_system.clear();
}
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;
InputFrame input;
while (!WindowShouldClose()) {
PROF_BEGIN_FRAME();
poll_input(input);
accumulator += GetFrameTime();
if (accumulator > 0.25f) accumulator = 0.25f; // avoid spiral of death
PROF_BEGIN_CPU(profiler::Sec_Sim);
while (accumulator >= kFixedDt) {
if (!opts.hiss_test && !opts.room_test && !opts.aoe_test) {
const InputState in = read_input(game, opts.bot, input);
if (in.restart) presenter.reset();
game.update(in, kFixedDt);
input.dash = false;
input.restart = false;
}
accumulator -= kFixedDt;
}
PROF_END_CPU(profiler::Sec_Sim);
if (opts.aoe_test) {
aoe_clock += GetFrameTime();
if (aoe_clock >= 1.8f) {
aoe_clock = 0.0f;
struct Blast { Vec2 pos; float radius; unsigned flags; };
static const Blast kBlasts[] = {
{{-82.0f, 46.0f}, 4.5f, 0u},
{{4.0f, -8.0f}, 3.2f, kDetonateConcrete},
{{-70.0f, 34.0f}, 4.0f, kDetonateConcrete},
};
const Blast& b = kBlasts[aoe_seq++ % 3];
game.player.pos = b.pos;
game.events.push(EventType::AreaDetonation, b.pos, b.radius,
b.flags);
}
}
PROF_BEGIN_GPU();
// Only the scene renders into the post-FX target: bloom, corruption and
// the damage flash must never tint the HUD. The default raw palette
// draws straight to the backbuffer — no full-res render target or blit.
if (use_post) {
PROF_BEGIN_CPU(profiler::Sec_Scene);
BeginTextureMode(post_target);
ClearBackground(Color{24, 24, 27, 255});
presenter.draw_scene(game);
EndTextureMode();
PROF_END_CPU(profiler::Sec_Scene);
}
PROF_BEGIN_CPU(profiler::Sec_Post);
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);
PROF_DRAW_CALL();
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);
PROF_DRAW_CALL();
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);
PROF_DRAW_CALL();
DrawTexturePro(bloom_b.texture, half_src, half_dst,
Vector2{0.0f, 0.0f}, 0.0f, WHITE);
EndShaderMode();
EndTextureMode();
}
PROF_END_CPU(profiler::Sec_Post);
PROF_BEGIN_CPU(profiler::Sec_UI);
BeginDrawing();
if (use_post) {
BeginShaderMode(post_shader);
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);
PROF_DRAW_CALL();
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);
EndShaderMode();
} else {
ClearBackground(Color{24, 24, 27, 255});
PROF_BEGIN_CPU(profiler::Sec_Scene);
presenter.draw_scene(game);
PROF_END_CPU(profiler::Sec_Scene);
}
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, input.pad);
clay_ui::end_frame(GetFrameTime());
ui::draw_all(game);
if (game.state != GameState::Title) ui::draw_camera_dev(presenter.camera_tuning());
PROF_DRAW_OVERLAY();
PROF_END_CPU(profiler::Sec_UI);
PROF_BEGIN_CPU(profiler::Sec_Present);
EndDrawing();
PROF_END_CPU(profiler::Sec_Present);
PROF_END_GPU();
PROF_END_FRAME();
PROF_SET_STATS(static_cast<int>(game.enemy_system.enemies.size()),
static_cast<int>(game.weapon_system.projectiles.size()),
static_cast<int>(game.pickup_system.shards.size()),
presenter.debris_count(), presenter.dressing_count());
// 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_F10)) PROF_TOGGLE();
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);
PROF_SHUTDOWN();
CloseWindow();
return 0;
}