Skip to content

Commit

Permalink
some optimisations
Browse files Browse the repository at this point in the history
  • Loading branch information
yostane committed Sep 24, 2024
1 parent 6b25166 commit 92a0278
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 109 deletions.
13 changes: 7 additions & 6 deletions BlazorDoom.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk.WebAssembly">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Nullable>enable</Nullable>
<WarningLevel>0</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Nullable>enable</Nullable>
<WarningLevel>0</WarningLevel>
<DefaultItemExcludes>$(DefaultItemExcludes);docs\**\*;media;slides-src\**\*</DefaultItemExcludes>
</PropertyGroup>
</Project>
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ restore:
publish:
dotnet publish -c Release
custom_profile:
dotnet run -c Release --launch-profile "BlazorDoom" --verbosity normal
dotnet run -c Release --launch-profile "BlazorDoom" --verbosity normal
publush_run:
dotnet publish -c Release && dotnet serve -d:bin/Release/net9.0/publish/wwwroot -p 8080 -S --path-base '/MangedDoom-Blazor'
5 changes: 5 additions & 0 deletions global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"scripts": {
"start": "dotnet publish -c Release && dotnet serve -d:bin/Release/net9.0/publish/wwwroot -p 8080 -S --path-base '/MangedDoom-Blazor'"
}
}
52 changes: 26 additions & 26 deletions wwwroot/index.html
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
<!DOCTYPE html>
<html lang="en">
<head>
<base />
<meta charset="utf-8" />
<head>
<base href="/MangedDoom-Blazor/" />
<meta charset="utf-8" />

<meta
name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"
/>
<meta
name="description"
content="BlazorDoom - A dotnet WASM port of ManagedDoom"
/>
<meta name="keywords" content="Blazor, Doom, Game, WebAssembly, WASM" />
<title>BlazorDoom</title>
<link href="./bootstrap.min.css" rel="stylesheet" />
<link href="./app.css" rel="stylesheet" />
<link rel="manifest" href="./manifest.json" />
<script type="module" src="./main.js"></script>
</head>
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"
/>
<meta
name="description"
content="BlazorDoom - A dotnet WASM port of ManagedDoom"
/>
<meta name="keywords" content="Blazor, Doom, Game, WebAssembly, WASM" />
<title>BlazorDoom</title>
<link href="./bootstrap.min.css" rel="stylesheet" />
<link href="./app.css" rel="stylesheet" />
<link rel="manifest" href="./manifest.json" />
<script type="module" src="./main.js"></script>
</head>

<body>
<div id="install_button"></div>
<div id="fps"></div>
<canvas id="canvas" width="320" height="200"></canvas>
<button onclick="console.log('clicked sound button');">
Click to enable sound APIs
</button>
</body>
<body>
<div id="install_button"></div>
<div id="fps"></div>
<canvas id="canvas" width="320" height="200"></canvas>
<button onclick="console.log('clicked sound button');">
Click to enable sound APIs
</button>
</body>
</html>
174 changes: 98 additions & 76 deletions wwwroot/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,101 +4,123 @@
* @param {number[]} colors: unit array
*/
export function renderWithColorsAndScreenDataUnmarshalled(screenData, colors) {
const width = 320;
const height = 200;
const canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
context.imageSmoothingEnabled = false;
// TODO: create only two imageData and reuse them
const imageData = context.createImageData(width, height);
let y = 0;
let x = 0;
for (let i = 0; i < screenData.length; i += 1) {
const dataIndex = (y * width + x) * 4;
setSinglePixel(imageData, dataIndex, colors, screenData[i]);
if (y >= height - 1) {
y = 0;
x += 1;
} else {
y += 1;
}
const width = 320;
const height = 200;
const canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
context.imageSmoothingEnabled = false;
// TODO: create only two imageData and reuse them
const imageData = context.createImageData(width, height);
let y = 0;
let x = 0;
for (let i = 0; i < screenData.length; i += 1) {
const dataIndex = (y * width + x) * 4;
setSinglePixel(imageData, dataIndex, colors, screenData[i]);
if (y >= height - 1) {
y = 0;
x += 1;
} else {
y += 1;
}
context.putImageData(imageData, 0, 0);
}
context.putImageData(imageData, 0, 0);
}

function setSinglePixel(imageData, dataIndex, colors, colorIndex) {
const color = colors[colorIndex];
imageData.data[dataIndex] = color & 0xff;
imageData.data[dataIndex + 1] = (color >> 8) & 0xff;
imageData.data[dataIndex + 2] = (color >> 16) & 0xff;
imageData.data[dataIndex + 3] = 255;
const color = colors[colorIndex];
imageData.data[dataIndex] = color & 0xff;
imageData.data[dataIndex + 1] = (color >> 8) & 0xff;
imageData.data[dataIndex + 2] = (color >> 16) & 0xff;
imageData.data[dataIndex + 3] = 255;
}

let audioContext;
const numberOfChannels = 8;
let soundSource;
export function playSound(samples, sampleRate, channel) {
if (!audioContext) {
console.log("creating audio context", numberOfChannels, sampleRate);
audioContext = new AudioContext({
numberOfChannels: numberOfChannels,
class AudioManager {
/**
* @type {AudioBuffer[]}
*/
static buffers = [];

/**
* @type {AudioContext}
*/
static #audioContext;
/**
*
* @returns {AudioContext}
*/
static getAudioContext() {
const numberOfChannels = 9;
if (!this.#audioContext) {
try {
this.#audioContext = new AudioContext({
numberOfChannels: numberOfChannels,
});
} catch (e) {
// console.error(e);
return;
}
}
this.buffers.push(this.#audioContext.createBuffer(1, 90_000, 44100));
for (let index = 1; index < numberOfChannels; index++) {
this.buffers.push(this.#audioContext.createBuffer(1, 90_000, 22050));
}
return this.#audioContext;
}
}

const audioBuffer = audioContext.createBuffer(
numberOfChannels,
samples.length,
sampleRate
);
let soundSource;
export function playSound(samples, sampleRate, channel) {
const audioContext = AudioManager.getAudioContext();
if (!audioContext) {
return;
}

var channelData = audioBuffer.getChannelData(channel);
for (let i = 0; i < samples.length; i++) {
// normalize the sample to be between -1 and 1
channelData[i] = samples[i] / 32767;
}
// console.log("sound", channel, samples.length, sampleRate);
const audioBuffer = audioContext.createBuffer(1, samples.length, sampleRate);

var channelData = audioBuffer.getChannelData(0);
for (let i = 0; i < samples.length; i++) {
// normalize the sample to be between -1 and 1
channelData[i] = samples[i] / 32767;
}

// if (soundSource) {
// soundSource.stop();
// }
// if (soundSource) {
// soundSource.stop();
// }

soundSource = audioContext.createBufferSource();
soundSource.buffer = audioBuffer;
soundSource.connect(audioContext.destination);
soundSource.start();
soundSource = audioContext.createBufferSource();
soundSource.buffer = audioBuffer;
soundSource.connect(audioContext.destination);
soundSource.start();
}

// Todo: play music in bigger and sync the playback
let source;
let musicChannelData;
let musicBuffer;
/**
*
* @param {int[]} samples
* @param {int} sampleRate
* @param {int} channel
* @returns
*/
export function playMusic(samples, sampleRate, channel) {
if (!audioContext) {
try {
audioContext = new AudioContext({
numberOfChannels: numberOfChannels,
});
} catch (e) {
console.error(e);
return;
}
}
const audioContext = AudioManager.getAudioContext();
if (!audioContext) {
return;
}

if (!musicBuffer) {
musicBuffer = audioContext.createBuffer(1, samples.length, sampleRate);
musicChannelData = musicBuffer.getChannelData(0);
}

if (source) {
source.stop();
}
source = audioContext.createBufferSource();
source.buffer = musicBuffer;
source.connect(audioContext.destination);

for (let i = 0; i < samples.length; i++) {
// noralize the sample to be between -1 and 1
musicChannelData[i] = samples[i] / 32767;
}
// console.log("music", channel, samples.length, sampleRate);
const musicBuffer = AudioManager.buffers[0];
musicChannelData = musicBuffer.getChannelData(0);
for (let i = 0; i < samples.length; i++) {
// noralize the sample to be between -1 and 1
musicChannelData[i] = samples[i] / 32767;
}

source.start();
const source = audioContext.createBufferSource();
source.buffer = musicBuffer;
source.connect(audioContext.destination);
source.start(audioContext.currentTime + 0.1);
}

0 comments on commit 92a0278

Please sign in to comment.