Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions src/client/ClientGameRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ import { createCanvas } from "./Utils";
import { WebGLFrameBuilder } from "./WebGLFrameBuilder";
import { createRenderer, GameRenderer } from "./hud/GameRenderer";
import { GameView as WebGLGameView } from "./render/gl";
import { ALL_UNIT_TYPES } from "./render/types";
import { ALL_UNIT_TYPES, UnitState } from "./render/types";
import { SoundManager } from "./sound/SoundManager";

export interface LobbyConfig {
Expand Down Expand Up @@ -372,7 +372,34 @@ function mountWebGLFrameLoop(
};
requestAnimationFrame(driveFrame);

return { builder: new WebGLFrameBuilder(view) };
const builder = new WebGLFrameBuilder(view);

// When context is lost and restored, WebGL loses all textures and geometry.
// Force a full re-upload of the simulation state.
view.on("contextrestored", () => {
builder.clearCaches();

// Full upload of terrain, territory & trail state
const mapSize = mapWidth * mapHeight;
const allRefs = new Array(mapSize);
const allTerrain = new Uint8Array(mapSize);
for (let i = 0; i < mapSize; i++) {
allRefs[i] = i;
allTerrain[i] = gameView.terrainByte(i);
}
view.applyTerrainDelta(allRefs, allTerrain);

const frameData = gameView.frameData();
view.uploadTileAndTrailState(frameData.tileState, frameData.trailState);

// Structures and railroads normally skip GPU upload unless marked dirty, now force
view.updateStructures(frameData.units as Map<number, UnitState>);
view.uploadRailroadState(frameData.railroadState);

builder.update(gameView);
});

return { builder };
}

async function createClientGame(
Expand Down
6 changes: 6 additions & 0 deletions src/client/WebGLFrameBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ export class WebGLFrameBuilder {
this.patternData = new Uint8Array(PALETTE_SIZE * 1024);
}

/** Drop internal caches to force a full re-upload of state on the next update(). */
clearCaches(): void {
this.knownSmallIDs.clear();
this.localPlayerSmallID = 0;
}

update(gameView: GameView): void {
this.syncPlayers(gameView);
this.syncLocalPlayer(gameView);
Expand Down
2 changes: 2 additions & 0 deletions src/client/render/gl/Events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ export interface GameViewEventMap {
altviewpeek: AltViewPeekEvent;
/** Grid-view default toggled (M key). */
gridviewtoggle: GridViewToggleEvent;
/** WebGL Context successfully restored after a loss. (Requires full state re-upload) */
contextrestored: { type: "restored" };
}

/** A single item in the radial context menu. */
Expand Down
Loading
Loading