Add extensive tests for animation, input handling, and map rendering
- Enhanced animation archive tests to validate frame metadata and start frame selection logic. - Introduced new tests for entity dispatch entry handling, including key event queuing and processing. - Added tests for fixed map item interactions, including glob egg hatching and item movement. - Expanded flex archive tests to cover teleport egg spawning and usecode event selection. - Implemented isometric controls tests to verify directional key handling and animation state changes. - Improved map renderer tests to ensure correct rendering of map previews and collision detection.
This commit is contained in:
parent
2e16b2d524
commit
e757bb33d0
20 changed files with 1247 additions and 142 deletions
|
|
@ -18,6 +18,7 @@
|
|||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <condition_variable>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <iomanip>
|
||||
|
|
@ -43,10 +44,13 @@ constexpr remorse::s32 diagnosticItemRange = 48;
|
|||
struct AppState {
|
||||
remorse::GameDataRoot root;
|
||||
remorse::u32 mapIndex = 1;
|
||||
bool diagnostics = false;
|
||||
remorse::ProcessKernel diagnosticKernel;
|
||||
remorse::u16 diagnosticGroupCounts[32]{};
|
||||
remorse::StandProcessEntry* diagnosticStand = nullptr;
|
||||
remorse::KeyboardProcessEntry* diagnosticKeyboard = nullptr;
|
||||
remorse::ControllerMovementState controllerMovement;
|
||||
remorse::u16 animationProcessId = 0;
|
||||
std::optional<remorse::LiveFixedMapItems> liveItems;
|
||||
std::optional<remorse::StagedFixedMapItem> diagnosticItem;
|
||||
remorse::u16 diagnosticItemProcessId = 0;
|
||||
|
|
@ -57,7 +61,11 @@ struct AppState {
|
|||
remorse::u32 standWakeCount = 0;
|
||||
remorse::u32 diagnosticKeyEvents = 0;
|
||||
std::mutex sceneMutex;
|
||||
std::condition_variable renderWake;
|
||||
bool renderRequested = false;
|
||||
bool renderStop = false;
|
||||
std::optional<remorse::RenderedMapImage> scene;
|
||||
std::optional<remorse::MapSceneRenderer> renderer;
|
||||
std::optional<remorse::AnimationClip> standAnimation;
|
||||
std::optional<remorse::AnimationClip> walkAnimation;
|
||||
std::optional<remorse::AnimationClip> runAnimation;
|
||||
|
|
@ -71,7 +79,6 @@ struct AppState {
|
|||
std::vector<std::uint8_t> dibPixels;
|
||||
std::vector<std::uint8_t> viewPixels;
|
||||
remorse::WorldPoint spawn{};
|
||||
remorse::WorldPoint playerWorld{};
|
||||
remorse::FastAreaRange fastArea{};
|
||||
remorse::s32 minWorldX = 0;
|
||||
remorse::s32 minWorldY = 0;
|
||||
|
|
@ -86,6 +93,7 @@ struct AppState {
|
|||
std::wstring loadError;
|
||||
bool sceneLoaded = false;
|
||||
std::thread sceneThread;
|
||||
std::thread renderThread;
|
||||
HDC backBufferDc = nullptr;
|
||||
HBITMAP backBufferBitmap = nullptr;
|
||||
HBITMAP previousBackBufferBitmap = nullptr;
|
||||
|
|
@ -124,6 +132,16 @@ void setLoadFailure(AppState& state, const std::wstring& message)
|
|||
state.sceneLoaded = true;
|
||||
}
|
||||
|
||||
remorse::WorldPoint avatarWorld(const AppState& state)
|
||||
{
|
||||
const auto* avatar = state.liveItems ? state.liveItems->findItem(1) : nullptr;
|
||||
return avatar ? remorse::WorldPoint{avatar->data.x * 2, avatar->data.y * 2, avatar->data.z}
|
||||
: state.spawn;
|
||||
}
|
||||
|
||||
bool hatchGlobsInFastArea(remorse::LiveFixedMapItems& items,
|
||||
const remorse::MapSceneRenderer& renderer, const remorse::FastAreaRange& range);
|
||||
|
||||
void loadSceneAsync(HWND window, AppState& state)
|
||||
{
|
||||
state.sceneThread = std::thread([window, &state]() {
|
||||
|
|
@ -153,6 +171,11 @@ void loadSceneAsync(HWND window, AppState& state)
|
|||
return;
|
||||
}
|
||||
const auto stagedItems = remorse::stageFixedMapItems(*map);
|
||||
if (!stagedItems) {
|
||||
setLoadFailure(state, L"The fixed-map item cache could not be initialized.");
|
||||
PostMessageW(window, sceneReadyMessage, 0, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
const auto shapeTypes = remorse::loadTypeFlags(*typeFlagsPath);
|
||||
const auto globObjects = remorse::loadGlobObjects(*globPath);
|
||||
|
|
@ -249,13 +272,38 @@ void loadSceneAsync(HWND window, AppState& state)
|
|||
maxY = std::max(maxY, static_cast<remorse::s32>(entry.y));
|
||||
}
|
||||
|
||||
const remorse::WorldPoint spawn{(minX + maxX) / 2, (minY + maxY) / 2, 0};
|
||||
const auto startingEgg = remorse::findTeleportEgg(*stagedItems, *shapeTypes, 0x1e);
|
||||
if (state.mapIndex == 1 && !startingEgg) {
|
||||
setLoadFailure(state, L"The original map 1 starting teleporter could not be found uniquely.");
|
||||
PostMessageW(window, sceneReadyMessage, 0, 0);
|
||||
return;
|
||||
}
|
||||
if (state.mapIndex != 1 && !state.diagnostics) {
|
||||
setLoadFailure(state, L"Only map 1 has a source-backed start. Use --diagnostics for other maps.");
|
||||
PostMessageW(window, sceneReadyMessage, 0, 0);
|
||||
return;
|
||||
}
|
||||
const remorse::WorldPoint spawn = startingEgg
|
||||
? remorse::WorldPoint{startingEgg->data.x * 2, startingEgg->data.y * 2, startingEgg->data.z}
|
||||
: remorse::WorldPoint{(minX + maxX) / 2, (minY + maxY) / 2, 0};
|
||||
remorse::MapRenderOptions options;
|
||||
options.mapIndex = state.mapIndex;
|
||||
options.maxWidth = backingWidth;
|
||||
options.maxHeight = backingHeight;
|
||||
options.viewCenter = spawn;
|
||||
auto scene = remorse::renderMapPreview(state.root, options);
|
||||
remorse::LiveFixedMapItems liveItems(std::move(*stagedItems), std::move(containedItems));
|
||||
liveItems.initializeAvatar();
|
||||
remorse::StagedFixedMapItem diagnosticStart;
|
||||
diagnosticStart.data.x = static_cast<remorse::u16>(spawn.x / 2);
|
||||
diagnosticStart.data.y = static_cast<remorse::u16>(spawn.y / 2);
|
||||
diagnosticStart.data.z = static_cast<remorse::u8>(spawn.z);
|
||||
liveItems.teleportAvatar(startingEgg ? *startingEgg : diagnosticStart,
|
||||
static_cast<remorse::u8>(state.mapIndex));
|
||||
auto renderer = remorse::loadMapSceneRenderer(state.root, state.mapIndex);
|
||||
if (renderer) {
|
||||
hatchGlobsInFastArea(liveItems, *renderer, remorse::calculateFastAreaRange(spawn));
|
||||
}
|
||||
auto scene = renderer ? renderer->render(options, &liveItems) : std::nullopt;
|
||||
if (!scene) {
|
||||
setLoadFailure(state, L"Map graphics or palette data could not be loaded.");
|
||||
PostMessageW(window, sceneReadyMessage, 0, 0);
|
||||
|
|
@ -272,7 +320,6 @@ void loadSceneAsync(HWND window, AppState& state)
|
|||
{
|
||||
std::lock_guard<std::mutex> lock(state.sceneMutex);
|
||||
state.spawn = spawn;
|
||||
state.playerWorld = spawn;
|
||||
state.fastArea = remorse::calculateFastAreaRange(spawn);
|
||||
state.playerCollisionBody = remorse::makeCollisionBody(spawn, (*shapeTypes)[1]);
|
||||
state.collisionBodies = std::move(collisionBodies);
|
||||
|
|
@ -286,15 +333,14 @@ void loadSceneAsync(HWND window, AppState& state)
|
|||
state.maxWorldX = maxX;
|
||||
state.maxWorldY = maxY;
|
||||
state.scene = std::move(scene);
|
||||
state.renderer = std::move(renderer);
|
||||
state.dibPixels = std::move(dibPixels);
|
||||
state.standAnimation = *standAnimation;
|
||||
state.walkAnimation = *walkAnimation;
|
||||
state.runAnimation = *runAnimation;
|
||||
state.playerFrames = std::move(playerFrames);
|
||||
state.playerPalette = *palette;
|
||||
if (stagedItems) {
|
||||
state.liveItems.emplace(std::move(*stagedItems), std::move(containedItems));
|
||||
}
|
||||
state.liveItems.emplace(std::move(liveItems));
|
||||
state.sceneLoaded = true;
|
||||
}
|
||||
} catch (const std::exception& error) {
|
||||
|
|
@ -306,6 +352,78 @@ void loadSceneAsync(HWND window, AppState& state)
|
|||
});
|
||||
}
|
||||
|
||||
bool hatchGlobsInFastArea(remorse::LiveFixedMapItems& items,
|
||||
const remorse::MapSceneRenderer& renderer, const remorse::FastAreaRange& range)
|
||||
{
|
||||
std::vector<remorse::u16> eggIds;
|
||||
for (const auto& item : items.mapItems()) {
|
||||
if (item.data.shape < renderer.typeFlags.size() &&
|
||||
renderer.typeFlags[item.data.shape].family == 3 &&
|
||||
!items.isGlobHatched(item.itemNumber) && remorse::isWorldPointInFastArea(
|
||||
item.data.x * 2, item.data.y * 2, range)) {
|
||||
eggIds.push_back(item.itemNumber);
|
||||
}
|
||||
}
|
||||
bool changed = false;
|
||||
for (const remorse::u16 eggId : eggIds) {
|
||||
const auto* egg = items.findItem(eggId);
|
||||
const auto glob = std::find_if(renderer.globs.begin(), renderer.globs.end(),
|
||||
[egg](const auto& object) { return object.index == egg->data.quality; });
|
||||
if (glob != renderer.globs.end()) {
|
||||
changed = items.hatchGlobEgg(eggId, *glob, renderer.typeFlags).has_value() || changed;
|
||||
}
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
|
||||
void startLiveRenderer(HWND window, AppState& state)
|
||||
{
|
||||
state.renderThread = std::thread([window, &state]() {
|
||||
for (;;) {
|
||||
std::optional<remorse::LiveFixedMapItems> snapshot;
|
||||
remorse::WorldPoint center{};
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(state.sceneMutex);
|
||||
state.renderWake.wait(lock, [&state]() { return state.renderStop || state.renderRequested; });
|
||||
if (state.renderStop) {
|
||||
return;
|
||||
}
|
||||
state.renderRequested = false;
|
||||
snapshot.emplace(*state.liveItems);
|
||||
center = avatarWorld(state);
|
||||
}
|
||||
remorse::MapRenderOptions options;
|
||||
options.mapIndex = state.mapIndex;
|
||||
options.maxWidth = backingWidth;
|
||||
options.maxHeight = backingHeight;
|
||||
options.viewCenter = center;
|
||||
const remorse::s32 radius = static_cast<remorse::s32>(
|
||||
backingWidth + backingHeight * 2 + 512);
|
||||
options.worldRect = remorse::WorldRect{
|
||||
center.x - radius, center.y - radius, center.x + radius, center.y + radius};
|
||||
auto scene = state.renderer->render(options, &*snapshot);
|
||||
if (!scene) {
|
||||
continue;
|
||||
}
|
||||
std::vector<std::uint8_t> pixels(scene->pixels.size() * 3);
|
||||
for (std::size_t index = 0; index < scene->pixels.size(); ++index) {
|
||||
pixels[index * 3] = scene->pixels[index].blue;
|
||||
pixels[index * 3 + 1] = scene->pixels[index].green;
|
||||
pixels[index * 3 + 2] = scene->pixels[index].red;
|
||||
}
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(state.sceneMutex);
|
||||
if (state.renderStop) {
|
||||
return;
|
||||
}
|
||||
state.scene = std::move(scene);
|
||||
state.dibPixels = std::move(pixels);
|
||||
}
|
||||
PostMessageW(window, sceneReadyMessage, 0, 0);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void drawCenteredText(HDC device, const RECT& bounds, const wchar_t* text, COLORREF color, HFONT font)
|
||||
{
|
||||
const int saved = SaveDC(device);
|
||||
|
|
@ -316,41 +434,6 @@ void drawCenteredText(HDC device, const RECT& bounds, const wchar_t* text, COLOR
|
|||
RestoreDC(device, saved);
|
||||
}
|
||||
|
||||
bool isKeyDown(int key)
|
||||
{
|
||||
return (GetAsyncKeyState(key) & 0x8000) != 0;
|
||||
}
|
||||
|
||||
std::optional<remorse::u8> readMovementDirection()
|
||||
{
|
||||
if (isKeyDown(VK_HOME) || isKeyDown(VK_NUMPAD7) || isKeyDown('Q')) return remorse::controllerDirectionFromScan(0x47);
|
||||
if (isKeyDown(VK_PRIOR) || isKeyDown(VK_NUMPAD9) || isKeyDown('E')) return remorse::controllerDirectionFromScan(0x49);
|
||||
if (isKeyDown(VK_END) || isKeyDown(VK_NUMPAD1) || isKeyDown('Z')) return remorse::controllerDirectionFromScan(0x4f);
|
||||
if (isKeyDown(VK_NEXT) || isKeyDown(VK_NUMPAD3) || isKeyDown('C')) return remorse::controllerDirectionFromScan(0x51);
|
||||
|
||||
const bool up = isKeyDown(VK_UP) || isKeyDown('W') || isKeyDown(VK_NUMPAD8);
|
||||
const bool right = isKeyDown(VK_RIGHT) || isKeyDown('D') || isKeyDown(VK_NUMPAD6);
|
||||
const bool down = isKeyDown(VK_DOWN) || isKeyDown('S') || isKeyDown(VK_NUMPAD2);
|
||||
const bool left = isKeyDown(VK_LEFT) || isKeyDown('A') || isKeyDown(VK_NUMPAD4);
|
||||
if (up && left) return remorse::controllerDirectionFromScan(0x47);
|
||||
if (up && right) return remorse::controllerDirectionFromScan(0x49);
|
||||
if (down && left) return remorse::controllerDirectionFromScan(0x4f);
|
||||
if (down && right) return remorse::controllerDirectionFromScan(0x51);
|
||||
if (up) return remorse::controllerDirectionFromScan(0x48);
|
||||
if (right) return remorse::controllerDirectionFromScan(0x4d);
|
||||
if (down) return remorse::controllerDirectionFromScan(0x50);
|
||||
if (left) return remorse::controllerDirectionFromScan(0x4b);
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
bool isWalkRequested()
|
||||
{
|
||||
remorse::u16 modifiers = (GetKeyState(VK_CAPITAL) & 1) != 0 ? 0x40 : 0;
|
||||
if (isKeyDown(VK_LSHIFT)) modifiers |= 0x01;
|
||||
if (isKeyDown(VK_RSHIFT)) modifiers |= 0x02;
|
||||
return remorse::controllerWalkRequested(modifiers);
|
||||
}
|
||||
|
||||
void compositePlayer(HDC device, AppState& state, int viewWidth, int viewHeight,
|
||||
int sourceLeft, int sourceTop, std::size_t viewStride)
|
||||
{
|
||||
|
|
@ -377,10 +460,9 @@ void compositePlayer(HDC device, AppState& state, int viewWidth, int viewHeight,
|
|||
}
|
||||
|
||||
const remorse::DecodedShapeFrame& sprite = *state.playerFrames[shapeFrame];
|
||||
const int actorX = static_cast<int>(state.scene->width / 2) +
|
||||
static_cast<int>(std::lround(state.playerOffsetX)) - sourceLeft;
|
||||
const int actorY = static_cast<int>(state.scene->height / 2) +
|
||||
static_cast<int>(std::lround(state.playerOffsetY)) - sourceTop;
|
||||
const auto avatarScreen = remorse::projectToScreen(avatarWorld(state));
|
||||
const int actorX = avatarScreen.x - state.scene->sourceLeft - sourceLeft;
|
||||
const int actorY = avatarScreen.y - state.scene->sourceTop - sourceTop;
|
||||
const int spriteLeft = actorX + sprite.frame.xOffset;
|
||||
const int spriteTop = actorY + sprite.frame.yOffset;
|
||||
const bool flipped = remorse::isAnimationFrameFlipped(*metadata);
|
||||
|
|
@ -507,10 +589,9 @@ void paintWindow(HWND window, AppState& state)
|
|||
|
||||
const int viewWidth = std::min(clientWidth, static_cast<int>(state.scene->width));
|
||||
const int viewHeight = std::min(clientHeight, static_cast<int>(state.scene->height));
|
||||
const int centeredLeft = static_cast<int>(state.scene->width / 2) +
|
||||
static_cast<int>(std::lround(state.playerOffsetX)) - viewWidth / 2;
|
||||
const int centeredTop = static_cast<int>(state.scene->height / 2) +
|
||||
static_cast<int>(std::lround(state.playerOffsetY)) - viewHeight / 2;
|
||||
const auto avatarScreen = remorse::projectToScreen(avatarWorld(state));
|
||||
const int centeredLeft = avatarScreen.x - state.scene->sourceLeft - viewWidth / 2;
|
||||
const int centeredTop = avatarScreen.y - state.scene->sourceTop - viewHeight / 2;
|
||||
const int sourceLeft = std::clamp(centeredLeft, 0, static_cast<int>(state.scene->width) - viewWidth);
|
||||
const int sourceTop = std::clamp(centeredTop, 0, static_cast<int>(state.scene->height) - viewHeight);
|
||||
|
||||
|
|
@ -525,6 +606,10 @@ void paintWindow(HWND window, AppState& state)
|
|||
static_cast<std::size_t>(viewWidth) * 3);
|
||||
}
|
||||
compositePlayer(device, state, viewWidth, viewHeight, sourceLeft, sourceTop, viewStride);
|
||||
if (!state.diagnostics) {
|
||||
finishPaint();
|
||||
return;
|
||||
}
|
||||
|
||||
RECT hud{0, 0, viewWidth, 80};
|
||||
HBRUSH hudBrush = CreateSolidBrush(RGB(14, 24, 29));
|
||||
|
|
@ -538,10 +623,11 @@ void paintWindow(HWND window, AppState& state)
|
|||
DeleteObject(hudLine);
|
||||
|
||||
std::wostringstream label;
|
||||
const remorse::WorldPoint playerWorld = avatarWorld(state);
|
||||
label << L"MAP " << std::setw(3) << std::setfill(L'0') << state.mapIndex
|
||||
<< L" X " << state.playerWorld.x
|
||||
<< L" Y " << state.playerWorld.y
|
||||
<< L" Z " << static_cast<int>(state.playerWorld.z)
|
||||
<< L" X " << playerWorld.x
|
||||
<< L" Y " << playerWorld.y
|
||||
<< L" Z " << static_cast<int>(playerWorld.z)
|
||||
<< L" DIR " << std::setw(2) << static_cast<int>(state.playerDirection)
|
||||
<< L" " << (state.playerAnimation == 3 ? L"RUN" : state.playerAnimation == 1 ? L"WALK" : L"IDLE")
|
||||
<< L" WASD / KEYPAD SHIFT RUN";
|
||||
|
|
@ -570,7 +656,7 @@ void paintWindow(HWND window, AppState& state)
|
|||
SetTextColor(device, RGB(74, 207, 184));
|
||||
DrawTextW(device, diagnostics.str().c_str(), -1, &diagnosticsBounds, DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS);
|
||||
const auto nearbyItem = state.liveItems ? state.liveItems->nearestItem(
|
||||
state.playerWorld.x, state.playerWorld.y, diagnosticItemRange) : std::nullopt;
|
||||
playerWorld.x, playerWorld.y, diagnosticItemRange) : std::nullopt;
|
||||
if (state.diagnosticItem || nearbyItem) {
|
||||
const auto& displayItem = state.diagnosticItem ? state.diagnosticItem : nearbyItem;
|
||||
std::wostringstream itemStatus;
|
||||
|
|
@ -603,10 +689,6 @@ void updatePlayer(HWND window, AppState& state)
|
|||
0.0, 0.25);
|
||||
state.lastTick = now;
|
||||
|
||||
if (GetAsyncKeyState(VK_ESCAPE) & 0x8000) {
|
||||
DestroyWindow(window);
|
||||
return;
|
||||
}
|
||||
if (GetForegroundWindow() != window) {
|
||||
return;
|
||||
}
|
||||
|
|
@ -616,11 +698,19 @@ void updatePlayer(HWND window, AppState& state)
|
|||
}
|
||||
|
||||
state.simulationAccumulator += elapsed;
|
||||
const auto direction = readMovementDirection();
|
||||
bool changed = false;
|
||||
while (state.simulationAccumulator >= simulationTickSeconds) {
|
||||
state.simulationAccumulator -= simulationTickSeconds;
|
||||
state.diagnosticKernel.dispatchPendingKeyEvents([](remorse::EntityDispatchEntryBase& entry,
|
||||
remorse::u16 selector, remorse::u16 filter, remorse::KeyEvent& event, void* context) {
|
||||
auto& app = *static_cast<AppState*>(context);
|
||||
if (entry.bytes[0] == 0xa6 && entry.bytes[1] == 0x3a && selector == 0x10 && filter == 0) {
|
||||
app.controllerMovement.handleDirectionalKey(event.scanCode, event.modifiers, event.field8 != 0);
|
||||
}
|
||||
}, &state);
|
||||
const auto movement = state.controllerMovement.request();
|
||||
state.diagnosticKernel.deliverSystemTimerEvent();
|
||||
if (state.diagnostics) {
|
||||
const bool wasSleeping = (state.diagnosticStand->base.bytes[0x19] & 0x10) != 0;
|
||||
const remorse::u32 previousSleepStart = standSleepStart(*state.diagnosticStand);
|
||||
state.diagnosticKernel.runTimedSchedulerPass(state.diagnosticKernel.activeGroup(), [](remorse::EntityDispatchEntryBase& entry, void* context) {
|
||||
|
|
@ -664,53 +754,69 @@ void updatePlayer(HWND window, AppState& state)
|
|||
if (wasSleeping && standSleepStart(*state.diagnosticStand) != previousSleepStart) {
|
||||
++state.standWakeCount;
|
||||
}
|
||||
changed = true;
|
||||
if (!direction) {
|
||||
}
|
||||
changed = changed || state.diagnostics;
|
||||
if (movement.animation == 0) {
|
||||
if (state.animationProcessId != 0) {
|
||||
auto* process = state.diagnosticKernel.processAt(state.animationProcessId);
|
||||
process->bytes[0x18] |= 4;
|
||||
state.diagnosticKernel.terminateProcess(state.animationProcessId, state.diagnosticGroupCounts);
|
||||
state.animationProcessId = 0;
|
||||
}
|
||||
changed = changed || state.playerAnimation != 0 || state.playerFrame != 0;
|
||||
state.playerAnimation = 0;
|
||||
state.playerFrame = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
const remorse::u8 requestedAnimation = isWalkRequested() ? 1 : 3;
|
||||
const remorse::u8 requestedAnimation = movement.animation;
|
||||
const remorse::AnimationClip& clip = requestedAnimation == 1
|
||||
? *state.walkAnimation : *state.runAnimation;
|
||||
if (state.playerAnimation != requestedAnimation) {
|
||||
state.playerAnimation = requestedAnimation;
|
||||
state.playerFrame = 0;
|
||||
} else {
|
||||
state.playerFrame = static_cast<remorse::u8>(
|
||||
(state.playerFrame + 1) % clip.frameCount);
|
||||
}
|
||||
state.playerDirection = *direction;
|
||||
|
||||
const auto metadata = remorse::getAnimationFrame(clip, state.playerDirection, state.playerFrame);
|
||||
if (metadata) {
|
||||
const auto step = remorse::hostMovementStep(
|
||||
*metadata, state.playerDirection, (clip.header[2] & 0x10) != 0);
|
||||
if (step) {
|
||||
const remorse::WorldPoint destination{
|
||||
state.playerWorld.x + step->x,
|
||||
state.playerWorld.y + step->y,
|
||||
state.playerWorld.z + step->z};
|
||||
const remorse::ScreenPoint destinationScreen = remorse::projectToScreen(destination);
|
||||
const remorse::ScreenPoint spawnScreen = remorse::projectToScreen(state.spawn);
|
||||
const double nextOffsetX = destinationScreen.x - spawnScreen.x;
|
||||
const double nextOffsetY = destinationScreen.y - spawnScreen.y;
|
||||
const bool withinMapBounds = destination.x >= state.minWorldX &&
|
||||
destination.x <= state.maxWorldX && destination.y >= state.minWorldY &&
|
||||
destination.y <= state.maxWorldY;
|
||||
const bool withinSceneBounds = std::abs(nextOffsetX) <= backingWidth / 2 - 16 &&
|
||||
std::abs(nextOffsetY) <= backingHeight / 2 - 16;
|
||||
if (withinMapBounds && withinSceneBounds && remorse::canMoveCollisionBody(
|
||||
state.playerCollisionBody, destination, state.collisionBodies)) {
|
||||
state.playerWorld = destination;
|
||||
state.fastArea = remorse::calculateFastAreaRange(destination);
|
||||
state.playerCollisionBody.location = destination;
|
||||
state.playerOffsetX = nextOffsetX;
|
||||
state.playerOffsetY = nextOffsetY;
|
||||
}
|
||||
if (state.animationProcessId == 0 || state.playerAnimation != requestedAnimation ||
|
||||
state.playerDirection != movement.direction) {
|
||||
if (state.animationProcessId != 0) {
|
||||
auto* process = state.diagnosticKernel.processAt(state.animationProcessId);
|
||||
process->bytes[0x18] |= 4;
|
||||
state.diagnosticKernel.terminateProcess(state.animationProcessId, state.diagnosticGroupCounts);
|
||||
}
|
||||
auto* process = state.diagnosticKernel.createAnimPrimitiveProcess(
|
||||
state.diagnosticKernel.activeGroup(), 1, requestedAnimation, movement.direction,
|
||||
clip, state.diagnosticGroupCounts);
|
||||
state.animationProcessId = static_cast<remorse::u16>(process->base.bytes[2] |
|
||||
(static_cast<remorse::u16>(process->base.bytes[3]) << 8));
|
||||
state.diagnosticKernel.startAnimPrimitiveProcess(state.animationProcessId, *state.liveItems, clip);
|
||||
state.playerAnimation = requestedAnimation;
|
||||
}
|
||||
state.playerDirection = movement.direction;
|
||||
const auto* avatar = state.liveItems->findItem(1);
|
||||
const remorse::WorldPoint beforeMove{avatar->data.x, avatar->data.y, avatar->data.z};
|
||||
const auto fastArea = remorse::calculateFastAreaRange(
|
||||
{avatar->data.x, avatar->data.y, avatar->data.z});
|
||||
state.diagnosticKernel.runTimedSchedulerPass(state.diagnosticKernel.activeGroup(),
|
||||
[](remorse::EntityDispatchEntryBase& entry, void* context) {
|
||||
auto& app = *static_cast<AppState*>(context);
|
||||
if (&entry == app.diagnosticKernel.processAt(app.animationProcessId)) {
|
||||
const remorse::AnimationClip& activeClip = app.playerAnimation == 1
|
||||
? *app.walkAnimation : *app.runAnimation;
|
||||
const auto* item = app.liveItems->findItem(1);
|
||||
app.diagnosticKernel.advanceGroundedAnimPrimitive(app.animationProcessId,
|
||||
*app.liveItems, activeClip, app.shapeTypes,
|
||||
remorse::calculateFastAreaRange({item->data.x, item->data.y, item->data.z}));
|
||||
}
|
||||
}, &state);
|
||||
state.fastArea = fastArea;
|
||||
state.playerFrame = state.liveItems->findNpcAnimation(1)->frame;
|
||||
const remorse::ScreenPoint destinationScreen = remorse::projectToScreen(avatarWorld(state));
|
||||
const remorse::ScreenPoint spawnScreen = remorse::projectToScreen(state.spawn);
|
||||
state.playerOffsetX = destinationScreen.x - spawnScreen.x;
|
||||
state.playerOffsetY = destinationScreen.y - spawnScreen.y;
|
||||
const bool moved = avatar->data.x != beforeMove.x || avatar->data.y != beforeMove.y ||
|
||||
avatar->data.z != beforeMove.z;
|
||||
const bool hatched = moved && hatchGlobsInFastArea(
|
||||
*state.liveItems, *state.renderer, remorse::calculateFastAreaRange(avatarWorld(state)));
|
||||
if (state.renderThread.joinable() && (moved || hatched)) {
|
||||
state.renderRequested = true;
|
||||
state.renderWake.notify_one();
|
||||
}
|
||||
changed = true;
|
||||
}
|
||||
|
|
@ -743,13 +849,47 @@ LRESULT CALLBACK windowProcedure(HWND window, UINT message, WPARAM wParam, LPARA
|
|||
updatePlayer(window, *state);
|
||||
}
|
||||
return 0;
|
||||
case WM_KILLFOCUS:
|
||||
state->controllerMovement.clear();
|
||||
while (state->diagnosticKernel.popPendingKeyEvent()) {}
|
||||
return 0;
|
||||
case WM_KEYDOWN:
|
||||
if (wParam == 'F') {
|
||||
case WM_KEYUP:
|
||||
if (wParam == VK_ESCAPE && message == WM_KEYDOWN) {
|
||||
DestroyWindow(window);
|
||||
return 0;
|
||||
}
|
||||
{
|
||||
remorse::KeyEvent event{};
|
||||
event.scanCode = static_cast<remorse::u8>((lParam >> 16) & 0xff);
|
||||
switch (wParam) {
|
||||
case 'W': event.scanCode = 0x48; break;
|
||||
case 'A': event.scanCode = 0x4b; break;
|
||||
case 'S': event.scanCode = 0x50; break;
|
||||
case 'D': event.scanCode = 0x4d; break;
|
||||
case 'Q': event.scanCode = 0x47; break;
|
||||
case 'E': event.scanCode = 0x49; break;
|
||||
case 'Z': event.scanCode = 0x4f; break;
|
||||
case 'C': event.scanCode = 0x51; break;
|
||||
}
|
||||
event.modifiers = (GetKeyState(VK_CAPITAL) & 1) != 0 ? 0x40 : 0;
|
||||
if (GetKeyState(VK_NUMLOCK) & 1) event.modifiers |= 0x20;
|
||||
if ((lParam & (1L << 24)) != 0 || wParam == 'W' || wParam == 'A' ||
|
||||
wParam == 'S' || wParam == 'D' || wParam == 'Q' || wParam == 'E' ||
|
||||
wParam == 'Z' || wParam == 'C') event.modifiers &= ~0x20;
|
||||
if (GetKeyState(VK_LSHIFT) & 0x8000) event.modifiers |= 1;
|
||||
if (GetKeyState(VK_RSHIFT) & 0x8000) event.modifiers |= 2;
|
||||
event.field8 = message == WM_KEYDOWN ? 1 : 0;
|
||||
state->diagnosticKernel.queuePendingKeyEvent(event);
|
||||
}
|
||||
if (message == WM_KEYUP) return 0;
|
||||
if (state->diagnostics && wParam == 'F') {
|
||||
std::lock_guard<std::mutex> lock(state->sceneMutex);
|
||||
if (state->sceneLoaded && state->scene && !state->diagnosticItemCompleted &&
|
||||
state->diagnosticItemProcessId == 0) {
|
||||
const remorse::WorldPoint playerWorld = avatarWorld(*state);
|
||||
state->diagnosticItem = state->liveItems ? state->liveItems->nearestItem(
|
||||
state->playerWorld.x, state->playerWorld.y, diagnosticItemRange) : std::nullopt;
|
||||
playerWorld.x, playerWorld.y, diagnosticItemRange) : std::nullopt;
|
||||
}
|
||||
if (state->diagnosticItem && state->diagnosticItemProcessId == 0 &&
|
||||
state->diagnosticItem->itemNumber <= remorse::ProcessKernel::maxTrackedItemNumber &&
|
||||
|
|
@ -764,7 +904,7 @@ LRESULT CALLBACK windowProcedure(HWND window, UINT message, WPARAM wParam, LPARA
|
|||
}
|
||||
return 0;
|
||||
case WM_CHAR:
|
||||
if (wParam > 0 && wParam <= 0xff) {
|
||||
if (state->diagnostics && wParam > 0 && wParam <= 0xff) {
|
||||
remorse::KeyEvent lastEvent{};
|
||||
lastEvent.character = static_cast<remorse::u8>(wParam);
|
||||
lastEvent.scanCode = static_cast<remorse::u8>((lParam >> 16) & 0xff);
|
||||
|
|
@ -785,6 +925,12 @@ LRESULT CALLBACK windowProcedure(HWND window, UINT message, WPARAM wParam, LPARA
|
|||
paintWindow(window, *state);
|
||||
return 0;
|
||||
case sceneReadyMessage: {
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(state->sceneMutex);
|
||||
if (state->sceneLoaded && state->renderer && !state->renderThread.joinable()) {
|
||||
startLiveRenderer(window, *state);
|
||||
}
|
||||
}
|
||||
InvalidateRect(window, nullptr, FALSE);
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -793,6 +939,14 @@ LRESULT CALLBACK windowProcedure(HWND window, UINT message, WPARAM wParam, LPARA
|
|||
if (state->sceneThread.joinable()) {
|
||||
state->sceneThread.join();
|
||||
}
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(state->sceneMutex);
|
||||
state->renderStop = true;
|
||||
}
|
||||
state->renderWake.notify_one();
|
||||
if (state->renderThread.joinable()) {
|
||||
state->renderThread.join();
|
||||
}
|
||||
if (state->backBufferDc) {
|
||||
SelectObject(state->backBufferDc, state->previousBackBufferBitmap);
|
||||
DeleteObject(state->backBufferBitmap);
|
||||
|
|
@ -811,11 +965,18 @@ int WINAPI wWinMain(HINSTANCE instance, HINSTANCE, PWSTR commandLine, int showCo
|
|||
{
|
||||
remorse::installCrashDiagnostics();
|
||||
remorse::u32 mapIndex = 1;
|
||||
if (commandLine && *commandLine) {
|
||||
wchar_t* end = nullptr;
|
||||
const unsigned long parsed = std::wcstoul(commandLine, &end, 0);
|
||||
if (end != commandLine && parsed <= 255) {
|
||||
mapIndex = static_cast<remorse::u32>(parsed);
|
||||
bool diagnostics = false;
|
||||
std::wistringstream arguments(commandLine ? commandLine : L"");
|
||||
std::wstring argument;
|
||||
while (arguments >> argument) {
|
||||
if (argument == L"--diagnostics") {
|
||||
diagnostics = true;
|
||||
} else {
|
||||
wchar_t* end = nullptr;
|
||||
const unsigned long parsed = std::wcstoul(argument.c_str(), &end, 0);
|
||||
if (end != argument.c_str() && *end == L'\0' && parsed <= 255) {
|
||||
mapIndex = static_cast<remorse::u32>(parsed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -835,13 +996,17 @@ int WINAPI wWinMain(HINSTANCE instance, HINSTANCE, PWSTR commandLine, int showCo
|
|||
AppState state;
|
||||
state.root = *root;
|
||||
state.mapIndex = mapIndex;
|
||||
state.diagnostics = diagnostics;
|
||||
for (int index = 0; index < 3; ++index) {
|
||||
state.diagnosticKernel.createProcess(1, state.diagnosticGroupCounts);
|
||||
}
|
||||
state.diagnosticKernel.createSystemTimerProcess(1, state.diagnosticGroupCounts);
|
||||
state.diagnosticKeyboard = state.diagnosticKernel.createKeyboardProcess(1, state.diagnosticGroupCounts);
|
||||
state.diagnosticKernel.createKeyDaemonProcess(1, state.diagnosticGroupCounts);
|
||||
state.diagnosticStand = state.diagnosticKernel.createStandProcess(1, 1, state.diagnosticGroupCounts);
|
||||
state.diagnosticKernel.createControllerProcess(1, state.diagnosticGroupCounts);
|
||||
if (diagnostics) {
|
||||
state.diagnosticKernel.createKeyDaemonProcess(1, state.diagnosticGroupCounts);
|
||||
state.diagnosticStand = state.diagnosticKernel.createStandProcess(1, 1, state.diagnosticGroupCounts);
|
||||
}
|
||||
|
||||
WNDCLASSEXW windowClass{};
|
||||
windowClass.cbSize = sizeof(windowClass);
|
||||
|
|
@ -859,7 +1024,9 @@ int WINAPI wWinMain(HINSTANCE instance, HINSTANCE, PWSTR commandLine, int showCo
|
|||
|
||||
RECT bounds{0, 0, initialClientWidth, initialClientHeight};
|
||||
AdjustWindowRect(&bounds, WS_OVERLAPPEDWINDOW, FALSE);
|
||||
HWND window = CreateWindowExW(0, windowClassName, L"Crusader: No Remorse | Rebuild", WS_OVERLAPPEDWINDOW,
|
||||
HWND window = CreateWindowExW(0, windowClassName,
|
||||
diagnostics ? L"Crusader: No Remorse | Diagnostics" : L"Crusader: No Remorse | Traversal Test",
|
||||
WS_OVERLAPPEDWINDOW,
|
||||
CW_USEDEFAULT, CW_USEDEFAULT, bounds.right - bounds.left, bounds.bottom - bounds.top,
|
||||
nullptr, nullptr, instance, &state);
|
||||
if (!window) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue