Rise
The Vieneo Province
Loading...
Searching...
No Matches
Tests/CdiReplayTests.cpp

Source at documentation-generation time. See Test catalog for fixture associations.

1#include "NativeUiTest11.h"
2#include "../Instruments/Instrument.h"
3#include <iostream>
4
5// Only the logging/command services are stubbed. Draw the production A-4
6// attitude indicator, textures and sprites through the native D3D11 renderer.
7struct Logger::CrashState {};
8Logger::Logger() = default;
9Logger::~Logger() = default;
10void Logger::AddToCallStack(const char*) {}
11void Logger::Log(const char* text, Level level, int)
12{ if (level >= Error) throw std::runtime_error(text); }
13Bus::Bus(Logger* value) : logger(value) {}
14void Bus::AddCommand(const Command& command, bool) { commandStream.push_back(command); }
17float mark = 999;
18
19static void Check(bool pass, const char* message)
20{ if (!pass) throw std::runtime_error(message); }
21static bool Inside(int x, int y, const RECT& rect)
22{ return x >= rect.left && x < rect.right && y >= rect.top && y < rect.bottom; }
23
24int main(int argc, char** argv)
25{
26 try {
27 Check(argc == 2, "usage: CdiReplayTests <output-directory>");
28 const std::filesystem::path output = argv[1];
29 std::filesystem::create_directories(output);
30 NativeUiTest11 fixture(137,137); Logger logger; Bus bus(&logger);
31 DeviceObject device(&fixture.device,&logger);
32 // A-4.xml's authored instrument location and scale.
33 AttitudeIndicator attitude(24,24,1,1,&bus,&logger,&device,{});
34 Sprite surround(&logger,&device,"Textures/Instruments/flatdelta.png",0,0,.9f,0,0,137,137,0xffffffff);
35 bus.FlightPathVectorDegrees = RiseMath::Vector2(999,999);
36 bus.TargetBearingDeg = 999;
37 auto render = [&](const char* name, float loc, float gs) {
38 bus.LocalizerDeviationRadians = RiseMath::ToRadian(loc);
39 bus.GlideslopeDeviationRadians = RiseMath::ToRadian(gs);
40 attitude.FrameMove(1);
41 auto& d = fixture.device;
42 NativeUiTest11::HR(d.Clear(0,nullptr,Rendering::ClearFlags::TARGET,0,1,0));
43 NativeUiTest11::HR(d.BeginScene());
44 d.SetRenderState(Rendering::RenderState::ZENABLE,FALSE);
45 d.SetRenderState(Rendering::RenderState::SCISSORTESTENABLE,TRUE);
46 RECT clip{0,0,137,137}; device.SetScissorRect(&clip);
47 NativeUiTest11::HR(device.pSprite->Begin(Rendering::SpriteFlags::AlphaBlend |
49 surround.Draw(); attitude.Render();
50 NativeUiTest11::HR(device.pSprite->End());
51 NativeUiTest11::HR(d.EndScene()); fixture.DebugClean();
52 fixture.Save(output/(std::string(name)+".png"));
53 return fixture.Pixels();
54 };
55 const auto absent = render("a4-cdi-absent",999,999);
56 const auto centered = render("a4-cdi-centered",0,0);
57 const auto negative = render("a4-cdi-negative",-2.49f,-.69f);
58 const auto positive = render("a4-cdi-positive",2.49f,.69f);
59 const auto flagged = render("a4-cdi-flags",3,1);
60 // The original A-4 scales occupy about 29x5 and 5x32 pixels. This
61 // catches sampling beyond the 120x19/19x120 images (a white tail).
62 const RECT localizer{51,105,82,111}, glideslope{94,54,100,87};
63 int locPixels=0, gsPixels=0;
64 for (const auto* pixels : {&centered,&negative,&positive,&flagged}) {
65 for (int y=0;y<137;++y) for (int x=0;x<137;++x) {
66 const size_t i=size_t(y)*137+x;
67 if ((*pixels)[i]==absent[i]) continue;
68 const bool loc=Inside(x,y,localizer), gs=Inside(x,y,glideslope);
69 Check(loc || gs,"CDI rendering extends past its scale (stretched texture border)");
70 locPixels+=loc; gsPixels+=gs;
71 }
72 }
73 Check(locPixels>40 && gsPixels>40,"missing CDI scales");
74 // Deviation bugs must move in both axes and remain inside their scales.
75 for (const RECT& rect : {localizer,glideslope}) {
76 int leftChanges=0,rightChanges=0;
77 for (int y=rect.top;y<rect.bottom;++y) for (int x=rect.left;x<rect.right;++x) {
78 const size_t i=size_t(y)*137+x;
79 leftChanges+=negative[i]!=centered[i];
80 rightChanges+=positive[i]!=centered[i];
81 }
82 Check(leftChanges>3 && rightChanges>3,"CDI bug did not move with deviation");
83 }
84 Check(render("a4-cdi-signal-lost",999,999)==absent,"CDI remains after signal loss");
85 std::cout<<"PASS: A-4 CDI bounds, centered/negative/positive deviation, flags and signal loss; D3D11 debug layer clean\n";
86 return 0;
87 } catch (const std::exception& error) {
88 std::cerr<<"FAIL: "<<error.what()<<'\n'; return 1;
89 }
90}
#define MAX_SCAN
Definition Bus.h:20
Bus(Logger *prmLogger)
Definition Bus.cpp:55
void AddCommand(const Command &command, bool onlyOne)
Definition Bus.cpp:5
std::vector< Command > commandStream
Definition Bus.h:442
void Log(const char *msg, Level level=Info, int errorCode=0)
Definition Logger.cpp:283
void AddToCallStack(const char *msg)
Definition Logger.cpp:355
Logger()
Definition Logger.cpp:392
~Logger()
Definition Logger.cpp:448
@ Error
Definition Logger.h:28
s_network_objects playerships[MAX_SCAN]
Definition globals.cpp:112
float mark
Definition globals.cpp:45
Scockpit ourcockpit
Definition globals.cpp:114
constexpr uint32_t TARGET
constexpr uint32_t SCISSORTESTENABLE
constexpr uint32_t ZENABLE
constexpr DWORD AlphaBlend
constexpr DWORD SortTexture
constexpr DWORD SortBackToFront
bool Inside(Vec p, const Triangle &t)
Definition RigidProp.h:147
auto ToRadian(T value)
Definition ClientMath.h:13