Skip to content

Commit

Permalink
Add some toggles.
Browse files Browse the repository at this point in the history
  • Loading branch information
dtugend committed Sep 15, 2021
1 parent 909ff39 commit 1477aa4
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 102 deletions.
113 changes: 60 additions & 53 deletions afx-cefhud-interop/assets/examples/default/drawing.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ <h2>Last game events:</h2>
this.connected = false;
this.messages = {};

this.indexSettings = {};

this.shaderData = {
"afx_drawtexture_vs20": this.compileShader(this.stringToAfxData(`
matrix World;
Expand Down Expand Up @@ -135,8 +137,10 @@ <h2>Last game events:</h2>
"disconnect": async function(senderId) {
await Utils.toPromise(this, "disconnect");
},
"onRenderViewBegin": async function(renderInfo){

"onRenderViewBegin": async function(renderInfo,settings){

this.indexSettings = Object.assign({}, this.indexSettings, settings);

this.elIndicator2.classList.toggle("white");

this.renderInfo = renderInfo;
Expand Down Expand Up @@ -752,15 +756,16 @@ <h2>Last game events:</h2>
try {
// Ensure resources:

await this.render({
"sync": true, // change this according to if you need perfect sync (performance boost if false)
let renderSettings = {
"sync": this.indexSettings.sync,
"x": x,
"y": y,
"width": width,
"height": height,
"frameCount": frameCount,
"pass": pass,
"retry": false,
// optional: // "onAnimationFrame": async function(timestamp) {}
"onBegin": async function() {
let queued = false;

Expand Down Expand Up @@ -812,8 +817,54 @@ <h2>Last game events:</h2>

return queued;
},
// comment out or rename this to s.th. else if you don't need background of cs:go in cef for special background effects.
"onClear": async function(rootTextureHandle) {
"onPaint": async function(cefTextureHandle) {

if(this.retry) return;

//console.log("doPaintCefToGame BEGIN");

if(cefTextureHandle.invalid) throw Utils.toSoftError(new Error("cefTextureHandle.invalid"));

var cefTexture = self.getCefTexture(cefTextureHandle);

if(!cefTexture) {
console.log("Creating new texture for cefTextureHandle: ("+cefTextureHandle.hi+", "+cefTextureHandle.lo+")");

var refTexture = [undefined];
const D3DFMT_A8R8G8B8 = 21;
var hr = await Utils.toPromise(self.interop, "d3d9CreateTexture", this.width, this.height, 1, D3d9.D3DUSAGE.D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3d9.D3DPOOL.D3DPOOL_DEFAULT, refTexture, [cefTextureHandle]);
if(Utils.FAILED(hr)) throw Utils.toSoftError(Utils.failedHResultToError(hr, "CreateTexture failed for cefTextureHandle: ("+cefTextureHandle.hi+", "+cefTextureHandle.lo+")"));

cefTexture = refTexture[0];

self.setCefTexture(cefTextureHandle, cefTexture);
}

if(!cefTexture) throw Utils.toSoftError(new Error("!cefTexture"));

//console.log("doPaintCefToGame BEGIN B");

await self.drawRect2d(cefTexture, this.x, this.y, this.width, this.height, 0, 0, 1, 1);

//console.log("doPaintCefToGame BEGIN D");

await Utils.toPromise(self.interop, "waitForClientGpu");

//console.log("doPaintCefToGame END");

//

return true;
},
"onEnd": async function() {
if(this.retry)
await Utils.toPromise(self.interop, "pumpRetry");
else
await Utils.toPromise(self.interop, "pumpFinish");
}
};

if(this.indexSettings.bg) renderSettings["onClear"] = async function(rootTextureHandle) {

if(this.retry) return;

Expand Down Expand Up @@ -886,53 +937,9 @@ <h2>Last game events:</h2>
//console.log("doPaintGameToCef END");

return true;
},
"onPaint": async function(cefTextureHandle) {

if(this.retry) return;

//console.log("doPaintCefToGame BEGIN");

if(cefTextureHandle.invalid) throw Utils.toSoftError(new Error("cefTextureHandle.invalid"));

var cefTexture = self.getCefTexture(cefTextureHandle);

if(!cefTexture) {
console.log("Creating new texture for cefTextureHandle: ("+cefTextureHandle.hi+", "+cefTextureHandle.lo+")");

var refTexture = [undefined];
const D3DFMT_A8R8G8B8 = 21;
var hr = await Utils.toPromise(self.interop, "d3d9CreateTexture", this.width, this.height, 1, D3d9.D3DUSAGE.D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3d9.D3DPOOL.D3DPOOL_DEFAULT, refTexture, [cefTextureHandle]);
if(Utils.FAILED(hr)) throw Utils.toSoftError(Utils.failedHResultToError(hr, "CreateTexture failed for cefTextureHandle: ("+cefTextureHandle.hi+", "+cefTextureHandle.lo+")"));

cefTexture = refTexture[0];

self.setCefTexture(cefTextureHandle, cefTexture);
}

if(!cefTexture) throw Utils.toSoftError(new Error("!cefTexture"));

//console.log("doPaintCefToGame BEGIN B");

await self.drawRect2d(cefTexture, this.x, this.y, this.width, this.height, 0, 0, 1, 1);

//console.log("doPaintCefToGame BEGIN D");

await Utils.toPromise(self.interop, "waitForClientGpu");

//console.log("doPaintCefToGame END");

//

return true;
},
"onEnd": async function() {
if(this.retry)
await Utils.toPromise(self.interop, "pumpRetry");
else
await Utils.toPromise(self.interop, "pumpFinish");
}
});
};

await this.render(renderSettings);
} catch(e) {
console.log(e);
if(e && e.soft) {
Expand Down
95 changes: 46 additions & 49 deletions afx-cefhud-interop/assets/examples/default/engine.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@

this.drawingConnectRequested = false;

this.indexSettings = {};

this.messageHandlers = {
"settings": async function (settings) {
this.indexSettings = Object.assign({}, this.indexSettings, settings);
},
"connect": async function(senderId) {
await this.connect();
},
Expand All @@ -72,6 +77,25 @@
console.log("drawingDisconnected: "+senderId);
this.drawingConnectRequested = false;
await Utils.toPromise(this, "disconnect");
},
"pump": async function() {

try {
await Utils.toPromise(this.interop, "pump", this.pumpEvents);
}
catch(e) {
Utils.logError(e);

if(!(e && e.soft)) {
await Utils.toPromise(this, "disconnect");
return;
}
}

if(this.connected) this.interop.onMessage(this.interop.id, JSON.stringify({
"id": "pump",
"args": []
}));
}
};

Expand Down Expand Up @@ -290,7 +314,7 @@

await Utils.toPromise(this.interop, "sendMessage", this.args.drawingInteropId, JSON.stringify({
"id": "onRenderViewBegin",
"args": [renderInfo]
"args": [renderInfo,this.indexSettings]
}));
},
/*
Expand Down Expand Up @@ -320,7 +344,7 @@
},*/
// optional.
"onRenderViewHudEnd": async function(){
console.log("onRenderViewHudEnd");
//console.log("onRenderViewHudEnd");
await Utils.toPromise(this.interop, "sendMessage", this.args.drawingInteropId, JSON.stringify({
"id": "onHudEnd",
"args": [this.renderInfo.frameCount, this.pass]
Expand Down Expand Up @@ -350,7 +374,7 @@
// required if you want to do queued rendering optimizations,
// ohterwise optional.
"onForceEndQueue": async function() {
console.log("SEND onForceEndQueue");
//console.log("SEND onForceEndQueue");
await Utils.toPromise(this.interop, "sendMessage", this.args.drawingInteropId, JSON.stringify({
"id": "onForceEndQueue",
"args": []
Expand All @@ -370,45 +394,26 @@

var self = this;

await new Promise(function(resolve,reject){
self.connected = true;
self.newConnection = true;
self.drawingConnectedEvent = [resolve,reject];

async function doConnect() {
self.drawingConnectRequested = true;

await Utils.toPromise(self.interop, "sendMessage", self.args.drawingInteropId, JSON.stringify({
"id": "connect",
"args": [self.interop.id]
}));

await Utils.toPromise(self.interop, "connect");

await Utils.toPromise(self.interop, "sendMessage", self.args.indexInteropId, JSON.stringify({
"id": "engineConnected",
"args": [self.interop.id]
}));
}

doConnect()
.then(()=>{
function pumper() {
Utils.toPromise(self.interop, "pump", self.pumpEvents).then(()=>{
queueMicrotask(pumper);
}).catch((e) => {
Utils.logError(e);

if(!(e && e.soft))
return Utils.toPromise(self, "disconnect");

queueMicrotask(pumper);
});
}
self.connected = true;
self.newConnection = true;
self.drawingConnectRequested = true;

await Utils.toPromise(self.interop, "sendMessage", self.args.drawingInteropId, JSON.stringify({
"id": "connect",
"args": [self.interop.id]
}));

await Utils.toPromise(self.interop, "connect");

await Utils.toPromise(self.interop, "sendMessage", self.args.indexInteropId, JSON.stringify({
"id": "engineConnected",
"args": [self.interop.id]
}));

pumper();
});
});
await self.interop.onMessage(self.interop.id, JSON.stringify({
"id": "pump",
"args": []
}));
}

AfxEngineInterop.prototype.disconnect = async function() {
Expand All @@ -417,14 +422,6 @@
this.messages = {};
this.pumpPromise = Promise.resolve();

if(this.drawingConnectedEvent)
{
var e = this.drawingConnectedEvent;
this.drawingConnectedEvent = null;
e[1]();
return;
}

if(this.drawingConnectRequested)
{
await Utils.toPromise(this.interop, "sendMessage", this.args.drawingInteropId, JSON.stringify({
Expand Down
49 changes: 49 additions & 0 deletions afx-cefhud-interop/assets/examples/default/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ <h1>Default example</h1>
<p>Number of reconnects: <span id="reconnects">0</span></p>
<p>Engine connected: <span id="engineStatus">n/a</span></p>
<p>Drawing connected: <span id="drawingStatus">n/a</span></p>
<h2>Settings</h2>
<form>
<div>
<input type="checkbox" id="sync" name="sync" value="1" checked="checked">
<label for="sync"> <b>Enable perfect sync</b> (lower FPS).</label>
</div>
<div>
<input type="checkbox" id="bg" name="bg" value="1" checked="checked">
<label for="bg"> <b>Draw game background</b> (for background effects, lower FPS).</label>
</div>
</form>
<script type="module">

import * as Utils from '../../js/modules/utils.js';
Expand All @@ -43,6 +54,8 @@ <h1>Default example</h1>

this.drawingInteropId = null;
this.engineInteropId = null;

this.engineConnected = false;

this.messages = {};

Expand All @@ -52,6 +65,30 @@ <h1>Default example</h1>
this.elEngineStatus = document.getElementById('engineStatus');
this.elDrawingStatus = document.getElementById('drawingStatus');

function updateSettings(e) {
if(e.target == self.elSync && !e.target.checked) self.elBg.checked = false;
else if(e.target == self.elBg && e.target.checked) self.elSync.checked = true;

if(!self.engineConnected) return;

let settings = {};

//settings[e.target.name.toString()] = e.target.checked;
settings = {
"sync": self.elSync.checked,
"bg": self.elBg.checked
};

Utils.toPromise(self.interop, "sendMessage", self.engineInteropId, JSON.stringify({
"id": "settings",
"args": [settings]
}));
}
this.elSync = document.getElementById('sync');
this.elBg = document.getElementById('bg');
this.elSync.addEventListener("change", updateSettings);
this.elBg.addEventListener("change", updateSettings);

this.messageHandlers = {
"drawingCreated": async function(drawingInteropId) {
console.log("Created drawing interop #"+drawingInteropId);
Expand All @@ -70,13 +107,25 @@ <h1>Default example</h1>
"engineCreated": async function(engineInteropId) {
console.log("Created engine interop #"+engineInteropId+", connecting ...");
this.engineInteropId = engineInteropId;

let settings = {
"sync": this.elSync.checked,
"bg": this.elBg.checked
};

await Utils.toPromise(this.interop, "sendMessage", this.engineInteropId, JSON.stringify({
"id": "settings",
"args": [settings]
}));

await this.reconnect();
},
"engineConnected": async function(engineInteropId) {
this.elEngineStatus.innerText = "connected";
this.engineConnected = true;
},
"engineDisconnected": async function(engineInteropId) {
this.engineConnected = false;
this.elEngineStatus.innerText = "DISCONNECTED";

await Utils.sleepPromise(1000);
Expand Down

0 comments on commit 1477aa4

Please sign in to comment.