Skip to content

Commit

Permalink
fixed: close event related stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
ankushlohiya001 committed May 8, 2024
1 parent 026878f commit 151de93
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 70 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const Window = require("./lib/window.js");
const App = require("./lib/app.js");

module.exports = Window;
module.exports = App;
62 changes: 40 additions & 22 deletions lib/app.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,65 @@
const EventEmitter = require("events");
const sdl = require("./binding");
const InitFlag = require("./types").InitFlag;
const eventWatcher = require("./events");

class ApplicationContext extends EventEmitter {
static mainContext = null;
static loopId = null;
static mainLoop(delayMs = 17) {
if (ApplicationContext.loopId) return;
const Window = require("./window");

const Status = {
Loaded: 0x10,
Idle: 0x11,
Started: 0x12,
Stopped: 0x13,
};

class App {
static status = Status.Loaded;

static mainLoop(eventWatcher, delayMs = 17) {
if (App.status == Status.Started) {
console.log("->Main Loop already active!!");
} else if (App.status != Status.Idle) {
App.initSDL();
}
App.status = Status.Started;
(function loop() {
if (App.status == Status.Stopped) return;
eventWatcher.pollEvent();
ApplicationContext.loopId = setTimeout(loop, delayMs);
setTimeout(loop, delayMs);
})();
}

constructor(windowList) {
super();
this.windowList = windowList;
static initSDL() {
if (sdl.init(InitFlag.VIDEO) !== 0) {
console.log("->Unable to initalise SDL");
this.exit();
App.status = Status.Stopped;
App.exit();
return;
} else {
App.status = Status.Idle;
console.log("->Success of initalised SDL");
}

// handle interrupt signal ( Ctrl + C )
// destroy all window, then exits

process.on("SIGINT", () => {
for (let [id, win] in this.windowList) {
for (let [, win] in Window.list) {
win.close();
}
});
}

console.log("->Success of initalised SDL");
static exit() {
if (App.status == Status.Stopped) {
console.log("->Unable, already exited!!");
} else if (Window.list.size == 0) {
App.status = Status.Stopped;
sdl.quit();
console.log("->Success to exit SDL");
}
}

exit() {
if (this.windowList.size > 0) return;
clearTimeout(ApplicationContext.loopId);
sdl.quit();
console.log("->Success to exit SDL");
process.exit(0);
static createWindow(opts) {
return Window.create(App, opts);
}
}

module.exports = ApplicationContext;
module.exports = App;
51 changes: 24 additions & 27 deletions lib/events/index.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,48 @@
const windowList = require("../window").windowList;
const EventType = require("../types").EventType;
const WindowEvent = require("./window_event");
const MouseEvent = require("./mouse_event");
const KeyboardEvent = require("./keyboard_event");
// const DropEvent = require("./drop_event");
const EventWatcher = require("../binding").EventWatcher;

const eventWatcher = new EventWatcher();

let lastEventWindowId;

eventWatcher.setCallback(function(eventType, winId) {

let win = windowList.get(winId);

if (!win) {
winId = lastEventWindowId;
win = windowList.get(winId);
return;
}

function eventHandler(eventWatcher, eventType, window) {
switch (eventType) {
case EventType.WINDOWEVENT:
WindowEvent.initWindowEvents(eventWatcher, win);
WindowEvent.initWindowEvents(eventWatcher, window);
break;
case EventType.KEYDOWN:
case EventType.KEYUP:
KeyboardEvent.initKeyboardEvents(eventWatcher, win);
KeyboardEvent.initKeyboardEvents(eventWatcher, window);
break;
case EventType.MOUSEMOTION:
case EventType.MOUSEBUTTONDOWN:
case EventType.MOUSEBUTTONUP:
case EventType.MOUSEWHEEL:
MouseEvent.initMouseEvents(eventWatcher, win);
break;
// case EventType.DROPFILE:
// case EventType.DROPTEXT:
// case EventType.DROPBEGIN:
// case EventType.DROPCOMPLETE:
// DropEvent.initDropEvents(eventWatcher.getWindowEvent);
MouseEvent.initMouseEvents(eventWatcher, window);
break;
// case EventType.DROPFILE:
// case EventType.DROPTEXT:
// case EventType.DROPBEGIN:
// case EventType.DROPCOMPLETE:
// DropEvent.initDropEvents(eventWatcher.getWindowEvent);
// break;
case EventType.QUIT:
//win.close();
break;
}
lastEventWindowId = winId;
}

function setupEventWatcher(windowList) {
const eventWatcher = new EventWatcher();

eventWatcher.setCallback((eventType, winId) => {
let window = windowList.get(winId);

if (!window) return;

});
eventHandler(eventWatcher, eventType, window);
});
return eventWatcher;
}

module.exports = eventWatcher;
module.exports = { setupEventWatcher };
40 changes: 21 additions & 19 deletions lib/window.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
const types = require("./types");
const WindowFlag = types.WindowFlag;

const sdl = require("./binding");

const EventEmitter = require("events");
const { setupEventWatcher } = require("./events");

const performance = require("perf_hooks").performance;
const ApplicationContext = require("./app");

class Window extends sdl.Window {
static windowList = new Map(); // stroing all window by their IDs

static createWindow(opt) {
if (!ApplicationContext.mainContext) {
ApplicationContext.mainContext = new ApplicationContext(
Window.windowList,
);
ApplicationContext.mainLoop();
static list = new Map(); // stroing all window by their IDs

static create(app, opts) {
const window = new Window(opts);
const winId = window.id; // required storing as destroying window cause id loss
Window.list.set(winId, window);

if (Window.list.size == 1) {
const eventWatcher = setupEventWatcher(Window.list);
app.mainLoop(eventWatcher);
}
const window = new Window(opt);

window.on("exit", () => {
ApplicationContext.mainContext.exit();
Window.list.delete(winId);
app.exit();
});
return window;
}
Expand Down Expand Up @@ -87,14 +93,14 @@ class Window extends sdl.Window {
this._opengl = options.opengl; // required for 3d render support
this._fps = options.fps;

this._closed = false;

this._lastPerformance = performance.now();
this._loop = null;
this._deltaTime = 0;

this.canvasList = new Set(); // list of canvas to render

Window.windowList.set(this.id, this);

this.eventEmitter = new EventEmitter();
}

Expand Down Expand Up @@ -273,6 +279,7 @@ class Window extends sdl.Window {
}

requestAnimationFrame(cb) {
if (this._closed) return;
const crnt = performance.now();
this._deltaTime = crnt - this._lastPerformance;
this._lastPerformance = crnt;
Expand All @@ -281,8 +288,6 @@ class Window extends sdl.Window {
let delayForNext = 1000 / this._fps - timeConsumed;
delayForNext = delayForNext < 0 ? Window.EPSILON : delayForNext;

const window = this;

return setTimeout(async () => {
await cb(crnt);
}, Math.ceil(delayForNext));
Expand All @@ -292,11 +297,8 @@ class Window extends sdl.Window {
if (!this.closable) {
return false;
}
const id = this.id; // required storing as destroying window cause id loss
super.destroy();

Window.windowList.delete(id);

this._closed = true;
this.emit("exit"); // exit event for closing app context to fully exit program.
return true;
}
Expand Down

0 comments on commit 151de93

Please sign in to comment.