Files
AVA2/tests/select_render_test.cpp
shenandshen 281418f351 Fix cross-platform rendering and optimize frame performance
Correct selection orientation, game-entry lifecycle, mounted NPCs, labels and skill panels. Add the native GPU scene path, resident geometry, bounded texture and Python caches, and collision broad phase.

Pin the independently maintained Metin2 SDK through a submodule and document reproducible setup. Include rendering regressions and macOS packaging improvements.

Validation: client Debug build and 12 targeted client tests passed; SDK build and all 14 SDK tests passed.
2026-10-11 17:55:53 -07:00

155 lines
7.8 KiB
C++

#include "python_runtime.h"
#include "python_net.h"
#include "python_event_manager.h"
#include "eter_ui/PythonWindowManager.h"
#include "eter_ui/PythonWindow.h"
#include "eter_image/GrpDevice.h"
#include "eter_image/GrpExpandedImageInstance.h"
#include "eter_resource/ResourceManager.h"
#include <vector>
#include "eter_render/RaceData.h"
#include "eter_render/TextTailManager.h"
#include "eter_net/NetworkActorManager.h"
#include "eter_net/PythonNetworkStream.h"
#include "test_rundir.h"
#include <SDL3/SDL.h>
#include <cassert>
#include <cmath>
#include <filesystem>
#include <iostream>
int main(int argc, char** argv) {
if (argc < 3) return 2;
if (!ava2_test::EnterTestRunDir()) return ava2_test::kSkip;
ava2::PythonRuntime runtime(argv[1], argv[2]);
auto* wm = runtime.window_manager();
wm->SetScreenSize(1024, 768);
wm->SetResolution(1024, 768);
ava2::RaceManager::Instance().LoadPlayerRaces();
const char* names[] = {"WarriorHero", "ShadowBlade", "DarkLord", "MysticHealer"};
const unsigned armor[] = {11299, 11499, 11699, 11899};
for (int slot = 0; slot < 4; ++slot) {
ava2::Character c;
c.id = 1001 + slot; c.name = names[slot]; c.job = slot;
c.level = 75; c.health = 4; c.strength = 6; c.intelligence = 3; c.dexterity = 3;
c.main_part = armor[slot];
ava2::set_mock_character(slot, c);
}
ava2::set_mock_empire(3);
runtime.execute(
"import app, constInfo, networkModule, chr, event\n"
"app.SetHairColorEnable(constInfo.HAIR_COLOR_ENABLE)\n"
"app.SetArmorSpecularEnable(constInfo.ARMOR_SPECULAR_ENABLE)\n"
"app.SetWeaponSpecularEnable(constInfo.WEAPON_SPECULAR_ENABLE)\n"
"stream = networkModule.MainStream()\nstream.Create()\n"
"stream.SetSelectCharacterPhase()\n");
SDL_Surface* surface = SDL_CreateSurface(1024, 768, SDL_PIXELFORMAT_ARGB8888);
SDL_Renderer* renderer = SDL_CreateSoftwareRenderer(surface);
assert(surface && renderer);
CGraphicDevice device;
device.Init(renderer);
// The same pooled object must draw identically after another UI poisons its state.
auto* image = static_cast<CGraphicImage*>(CResourceManager::Instance().GetResourcePointer("locale/en/ui/select/name_warrior.sub"));
assert(image);
auto* instance = CGraphicExpandedImageInstance::New();
instance->SetImagePointer(image); instance->SetPosition(10, 10);
SDL_FillSurfaceRect(surface, nullptr, 0xff123456);
instance->Render(); SDL_RenderPresent(renderer);
const auto* first = static_cast<const Uint32*>(surface->pixels);
std::vector<Uint32> expected(first, first + 1024*768);
instance->SetRenderingRect(0, 0, 10, 22);
instance->SetScale(2, 3); instance->SetRotation(45);
instance->SetDiffuseColor(0.2f, 0.3f, 0.4f, 0.5f);
instance->LeftRightReverse();
instance->SetRenderingMode(CGraphicExpandedImageInstance::RENDERING_MODE_MODULATE);
CGraphicExpandedImageInstance::Delete(instance);
auto* reused = CGraphicExpandedImageInstance::New();
assert(reused == instance);
reused->SetImagePointer(image); reused->SetPosition(10, 10);
SDL_FillSurfaceRect(surface, nullptr, 0xff123456);
reused->Render(); SDL_RenderPresent(renderer);
const auto* second = static_cast<const Uint32*>(surface->pixels);
assert(std::equal(expected.begin(), expected.end(), second));
CGraphicExpandedImageInstance::Delete(reused);
ava2::Gr2SceneRenderer scene(1024, 768);
ava2::TextTailManager::Instance().SetCurrentSceneRenderer(&scene);
ava2::TextTailManager::Instance().SetCurrentRenderer(renderer);
for (int slot = 0; slot < 4; ++slot) {
runtime.execute("stream.curPhaseWindow.SelectSlot(" + std::to_string(slot) + ")\n");
auto* data = ava2::NetworkActorManager::Instance().GetActorData(slot);
assert(data && data->m_pActorInstance);
// An item vnum must resolve to an MSM shape instead of falling back to the novice body.
assert(data->m_pActorInstance->GetModel());
assert(data->m_awPart[ava2::RaceData::PART_MAIN] != armor[slot]);
const float height = data->m_pActorInstance->GetY();
runtime.execute("chr.Update()\n");
const float animTime = data->m_pActorInstance->GetAnimTime();
runtime.execute("chr.Deform()\n");
assert(data->m_pActorInstance->GetAnimTime() == animTime);
assert(std::abs(data->m_pActorInstance->GetY() - height) < 0.001f);
auto draws = data->m_pActorInstance->BuildDrawCommands();
assert(!draws.empty());
SDL_FillSurfaceRect(surface, nullptr, 0xff000000);
runtime.execute("event.Skip(stream.curPhaseWindow.descIndex)\n");
for (int frame = 0; frame < 40; ++frame) {
ava2::CPythonEventManager::Instance().SetElapsedMillisecond(16);
wm->Update(); wm->Render();
runtime.execute("stream.curPhaseWindow.OnUpdate()\n");
}
SDL_RenderPresent(renderer);
float eyeX, eyeY, eyeZ;
scene.GetEyePosition(eyeX, eyeY, eyeZ);
assert(eyeZ < -1400); // native +Y camera position maps to engine -Z
assert(scene.GetViewMatrix()[0] < 0); // Windows RH screen orientation
const auto* pixels = static_cast<const Uint32*>(surface->pixels);
// The list and description backgrounds must be textured, never solid white placeholders.
int white = 0;
for (int y = 220; y < 460; ++y)
for (int x = 805; x < 970; ++x)
if ((pixels[y * 1024 + x] & 0xffffff) == 0xffffff) ++white;
assert(white < 1000);
// Project every visible model vertex using the camera actually installed by the Python UI.
int visible = 0;
for (const auto& draw : draws)
for (size_t i = 0; i < draw.positions.size(); i += 3) {
const auto* p = &draw.positions[i];
float x = p[0]*draw.world[0] + p[1]*draw.world[4] + p[2]*draw.world[8] + draw.world[12];
float y = p[0]*draw.world[1] + p[1]*draw.world[5] + p[2]*draw.world[9] + draw.world[13];
float z = p[0]*draw.world[2] + p[1]*draw.world[6] + p[2]*draw.world[10] + draw.world[14];
const float* view = scene.GetViewMatrix();
const float* proj = scene.GetProjMatrix();
const float vx = x*view[0] + y*view[4] + z*view[8] + view[12];
const float vy = x*view[1] + y*view[5] + z*view[9] + view[13];
const float vz = x*view[2] + y*view[6] + z*view[10] + view[14];
const float sx = 270 + (vx*proj[0]/vz + 1)*754/2;
const float sy = (1 - vy*proj[5]/vz)*768/2;
assert(vz > 1000 && vz < 3000);
assert(sy > 0 && sy < 680); // head and feet must both fit
if (sx > 270 && sx < 805 && sy > 20 && sy < 680) ++visible;
}
assert(visible > 100);
if (argc > 3) {
std::filesystem::create_directories(argv[3]);
SDL_SaveBMP(surface, (std::string(argv[3]) + "/slot" + std::to_string(slot) + ".bmp").c_str());
}
}
// Transparent text cannot introduce white blocks or visible glyphs.
SDL_FillSurfaceRect(surface, nullptr, 0xff123456);
UI::CGraphicTextInstance text;
text.SetText("Transparent"); text.SetColor(0x00ffffff); text.Render();
SDL_RenderPresent(renderer);
for (int i = 0; i < 1024*768; ++i)
assert(static_cast<Uint32*>(surface->pixels)[i] == 0xff123456);
text.SetText("By virtue of their heavy");
int width, height;
text.GetTextSize(&width, &height);
assert(width > 0 && width < 165 && height == 12);
ava2::TextTailManager::Instance().SetCurrentSceneRenderer(nullptr);
ava2::TextTailManager::Instance().SetCurrentRenderer(nullptr);
device.Init(nullptr);
SDL_DestroyRenderer(renderer); SDL_DestroySurface(surface);
std::cout << "AVA2 selection rendering: four races, equipment, camera, text and alpha passed\n";
}