160 lines
8.0 KiB
C++
160 lines
8.0 KiB
C++
#pragma once
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <atomic>
|
|
#include <array>
|
|
#include <cstdio>
|
|
#include <d3d11.h>
|
|
#include <wrl/client.h>
|
|
#include "fonts.h"
|
|
#include "theme.h"
|
|
|
|
namespace startup_intro {
|
|
inline constexpr float duration=2.8f;
|
|
inline constexpr float hold_age=1.85f;
|
|
inline float unit(float value){return std::clamp(value,0.f,1.f);}
|
|
inline float ease(float value){value=unit(value);return 1.f-std::pow(1.f-value,3.f);}
|
|
inline float smooth(float value){value=unit(value);return value*value*(3.f-2.f*value);}
|
|
inline ImU32 ink(ImVec4 color,float alpha){color.w*=unit(alpha);return ImGui::ColorConvertFloat4ToU32(color);}
|
|
|
|
enum class stage {hooks,modules,input,profile,hud,ready};
|
|
inline std::atomic<stage> loading_stage{stage::hooks};
|
|
inline std::atomic<unsigned> modules_done{0},modules_total{0};
|
|
inline float completion(stage phase,unsigned done,unsigned total) {
|
|
switch(phase) {
|
|
case stage::hooks:return .05f;
|
|
case stage::modules:return .35f+.4f*(total ? unit(float(done)/total) : 0);
|
|
case stage::input:return .75f;
|
|
case stage::profile:return .85f;
|
|
case stage::hud:return .95f;
|
|
case stage::ready:return 1.f;
|
|
}
|
|
return 0;
|
|
}
|
|
inline const char* stage_name(stage phase) {
|
|
switch(phase) {
|
|
case stage::hooks:return "loading hooks";
|
|
case stage::modules:return "loading modules";
|
|
case stage::input:return "setting up input";
|
|
case stage::profile:return "loading profile";
|
|
case stage::hud:return "preparing hud";
|
|
case stage::ready:return "ready";
|
|
}
|
|
return "loading";
|
|
}
|
|
inline Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> texture;
|
|
inline void initialize_texture(ID3D11Device* device) {
|
|
if(!device || texture)return;
|
|
// Small repeatable woven/grain tile; generated once and shared by every
|
|
// frame. No image decoding or per-pixel CPU work during the animation.
|
|
std::array<unsigned,128*128> pixels{};
|
|
unsigned seed=0x416d6e65;
|
|
for(unsigned y=0;y<128;++y)for(unsigned x=0;x<128;++x) {
|
|
seed^=seed<<13;seed^=seed>>17;seed^=seed<<5;
|
|
const unsigned weave=((x/2+y/2)&1) ? 9 : 0;
|
|
pixels[y*128+x]=IM_COL32(160,160,160,9+(seed&31)+weave);
|
|
}
|
|
D3D11_TEXTURE2D_DESC desc{};desc.Width=desc.Height=128;desc.MipLevels=desc.ArraySize=1;
|
|
desc.Format=DXGI_FORMAT_R8G8B8A8_UNORM;desc.SampleDesc.Count=1;
|
|
desc.Usage=D3D11_USAGE_IMMUTABLE;desc.BindFlags=D3D11_BIND_SHADER_RESOURCE;
|
|
D3D11_SUBRESOURCE_DATA data{pixels.data(),128*4,0};
|
|
Microsoft::WRL::ComPtr<ID3D11Texture2D> image;
|
|
if(SUCCEEDED(device->CreateTexture2D(&desc,&data,&image)))
|
|
device->CreateShaderResourceView(image.Get(),nullptr,&texture);
|
|
}
|
|
|
|
struct playback {
|
|
double previous=-1;
|
|
float age=0;
|
|
bool finished=false;
|
|
float step(double now,bool ready,bool skip_requested=false) {
|
|
if(finished)return -1;
|
|
const double delta=previous<0 ? 0 : (std::max)(0.0,now-previous);
|
|
previous=now;
|
|
// Start immediately, but keep the fully assembled mark on an opaque
|
|
// field until hooks, profile activation and a module frame are ready.
|
|
if(skip_requested)age=(std::max)(age,hold_age);
|
|
age+=float(delta>.25 ? 1.0/60.0 : delta);
|
|
if(!ready)age=(std::min)(age,hold_age);
|
|
if(age>=duration){finished=true;return -1;}
|
|
return age;
|
|
}
|
|
};
|
|
inline playback animation;
|
|
|
|
inline void render(float age,ImVec2 screen,float progress=0,const char* status="loading hooks",double motion=0) {
|
|
if(age<0 || age>=duration || screen.x<1 || screen.y<1)return;
|
|
auto* draw=ImGui::GetForegroundDrawList();
|
|
const float scale=std::clamp((std::min)(screen.x/900.f,screen.y/650.f),.35f,1.35f);
|
|
const float enter=smooth(age/.18f),exit=smooth((age-2.12f)/.68f);
|
|
const float alpha=enter*(1-smooth((age-2.02f)/.4f));
|
|
const ImVec2 center{screen.x*.5f,screen.y*.5f};
|
|
|
|
// The opaque field clears only after loading has completed.
|
|
const float edge=-screen.y*.2f+exit*(screen.x+screen.y*.4f);
|
|
const float top=std::clamp(edge+screen.y*.12f,0.f,screen.x);
|
|
const float bottom=std::clamp(edge-screen.y*.12f,0.f,screen.x);
|
|
const ImVec2 field[]={{top,0},{screen.x,0},screen,{bottom,screen.y}};
|
|
if(exit<=0)draw->AddRectFilled({0,0},screen,ink(theme::bg(),1.f));
|
|
else draw->AddConvexPolyFilled(field,4,ink(theme::bg(),1.f));
|
|
const float grain=1-smooth((age-1.95f)/.17f);
|
|
if(texture && grain>0) {
|
|
draw->PushClipRect({0,0},screen,true);
|
|
const float drift=float(std::fmod(motion*4,128.0));
|
|
for(float y=-128+drift;y<screen.y;y+=128)
|
|
for(float x=-128+drift*.5f;x<screen.x;x+=128)
|
|
draw->AddImage(reinterpret_cast<ImTextureID>(texture.Get()),{x,y},{x+128,y+128},{0,0},{1,1},ink({1,1,1,1},grain*.22f));
|
|
draw->PopClipRect();
|
|
}
|
|
if(exit>0 && exit<1)
|
|
draw->AddLine({edge+screen.y*.12f,0},{edge-screen.y*.12f,screen.y},ink(theme::accent(),(1-exit)*.5f),scale);
|
|
|
|
ImFont* face=fonts::heading ? fonts::heading : ImGui::GetFont();
|
|
const float title_size=51*scale;
|
|
const auto title=fonts::text_size(face,title_size,"amnesia");
|
|
const float logo_h=70*scale,logo_w=logo_h*174/199,gap=23*scale;
|
|
const float width=logo_w+gap+title.x;
|
|
const float left=center.x-width*.5f;
|
|
const ImVec2 origin{left,center.y-logo_h*.5f-6*scale};
|
|
|
|
// Short, precise face movements lock the familiar cube into place.
|
|
const float assemble=ease((age-.14f)/.48f);
|
|
auto pt=[&](float x,float y,float dx,float dy) {
|
|
return ImVec2{origin.x+(x-43)*logo_h/199+dx*(1-assemble)*scale,
|
|
origin.y+(y-36)*logo_h/199+dy*(1-assemble)*scale};
|
|
};
|
|
const ImVec2 cap[]={pt(130,36,0,-18),pt(217,86,0,-18),pt(130,137,0,-18),pt(43,86,0,-18)};
|
|
const ImVec2 lhs[]={pt(43,86,-16,9),pt(130,137,-16,9),pt(130,235,-16,9),pt(43,184,-16,9)};
|
|
const ImVec2 rhs[]={pt(130,137,16,9),pt(217,86,16,9),pt(217,184,16,9),pt(130,235,16,9)};
|
|
const float mark=alpha*smooth((age-.12f)/.3f);
|
|
draw->AddConvexPolyFilled(cap,4,ink(theme::accent(),mark));
|
|
draw->AddConvexPolyFilled(lhs,4,ink(theme::dim(),mark));
|
|
draw->AddConvexPolyFilled(rhs,4,ink(theme::line(),mark));
|
|
|
|
// The whole word slides out from behind the cube through a fixed mask.
|
|
// Keep the normal letter spacing used by the rest of the client.
|
|
const float reveal=ease((age-.4f)/.6f);
|
|
const float text_x=left+logo_w+gap;
|
|
draw->PushClipRect({text_x,center.y-logo_h},{text_x+title.x*reveal+1,center.y+logo_h},true);
|
|
draw->AddText(face,title_size,{text_x-(1-reveal)*24*scale,center.y-title.y*.5f-6*scale},ink(theme::fg(),alpha*reveal),"amnesia");
|
|
draw->PopClipRect();
|
|
|
|
// Stage completion comes from actual loader checkpoints, not intro age.
|
|
const float bar_alpha=alpha*smooth((age-.55f)/.3f);
|
|
const float y=center.y+logo_h*.5f+13*scale;
|
|
theme::bar(draw,{left,y},{left+width,y+5*scale},progress,theme::accent(),bar_alpha);
|
|
ImFont* body=fonts::body ? fonts::body : ImGui::GetFont();
|
|
draw->AddText(body,11*scale,{left,y+12*scale},ink(theme::muted(),bar_alpha),status);
|
|
char percent[8];snprintf(percent,sizeof(percent),"%d%%",int(unit(progress)*100+.5f));
|
|
const auto size=fonts::text_size(body,11*scale,percent);
|
|
draw->AddText(body,11*scale,{left+width-size.x,y+12*scale},ink(theme::muted(),bar_alpha),percent);
|
|
}
|
|
|
|
inline void draw(bool ready,bool skip_requested=false) {
|
|
const float age=animation.step(ImGui::GetTime(),ready,skip_requested);
|
|
const auto phase=loading_stage.load();
|
|
if(age>=0)render(age,ImGui::GetIO().DisplaySize,
|
|
completion(phase,modules_done.load(),modules_total.load()),stage_name(phase),ImGui::GetTime());
|
|
}
|
|
}
|