Skip to content

Commit deddab0

Browse files
committed
Update readme
1 parent 63e3bde commit deddab0

File tree

3 files changed

+71
-27
lines changed

3 files changed

+71
-27
lines changed

README.md

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,42 +46,77 @@ step during the `npm i` command.
4646
## GLFW
4747

4848
This is a low-level interface, where most of the stuff is directly reflecting
49-
GLFW API. It **does NOT EXPOSE** OpenGL commands.
50-
See [GLFW Docs](http://www.glfw.org/docs/latest/group__window.html)
51-
for useful info on what it does and doesn't.
52-
53-
```js
54-
const glfw = require('glfw-raub');
49+
GLFW API. GLFW **does NOT EXPOSE** OpenGL commands, it only [controls the window-related
50+
setup and resources](http://www.glfw.org/docs/latest/group__window.html).
51+
Aside from several additional features, this addon directly exposes the GLFW API to JS. E.g.:
52+
53+
```cpp
54+
DBG_EXPORT JS_METHOD(pollEvents) {
55+
glfwPollEvents();
56+
RET_GLFW_VOID;
57+
}
5558
```
5659
57-
Here `glfw` is an API container, where all `glfw*` functions are accessible as
58-
`glfw.*`. E.g. `glfwSetWindowTitle` -> `glfw.setWindowTitle`.
60+
Nothing is added between you and GLFW, unless necessary or explicitly mentioned.
61+
62+
* All `glfw*` functions are accessible as
63+
`glfw.*`. E.g. `glfwPollEvents` -> `glfw.pollEvents`.
64+
* All `GLFW_*` constants are accessible as
65+
`glfw.*`. E.g. `GLFW_TRUE` -> `glfw.TRUE`.
5966
60-
See [TypeScript definitions](/index.d.ts) for more details.
67+
68+
See [this example](/examples/vulkan.mjs) for raw GLFW calls.
69+
70+
See [TS declarations](/index.d.ts) for more details.
6171
6272
----------
6373
6474
6575
### class Window
6676
6777
```js
68-
const { Window } = require('glfw-raub');
78+
const { Window } = glfw;
79+
const wnd = new Window({ title: 'GLFW Test', vsync: true });
6980
```
7081

7182
This class helps managing window objects and their events. It can also switch between
7283
fullscreen, borderless and windowed modes.
7384

74-
See [TypeScript definitions](/index.d.ts) for more details.
85+
The first window creates an additional invisible root-window for context sharing.
86+
(so that you can also close any window and still keep the root context).
87+
The platform context (pointers/handles) for sharing may be obtained when necessary.
88+
89+
See [TS declarations](/index.d.ts) for more details.
7590

7691
----------
7792

7893
### class Document
7994

8095
```js
81-
const { Document } = require('glfw-raub');
96+
const { Document } = glfw;
97+
const doc = new Document({ title: 'GLFW Test', vsync: true });
8298
```
8399

84-
It can be used to facilitate the environment for other
85-
JS libraries, such as [three.js](https://threejs.org/).
100+
Document inherits from `Window` and has the same features in general.
101+
It exposes additional APIs to mimic the content of web `document`.
102+
There are some tricks to provide WebGL libraries with necessary environment.
103+
Document is specifically designed for compatibility with [three.js](https://threejs.org/).
104+
Other web libraries may work too, but may require additional tweaking.
105+
106+
See [TS declarations](/index.d.ts) for more details.
107+
108+
----------
86109

87-
See [TypeScript definitions](/index.d.ts) for more details.
110+
### Extras
111+
112+
* `glfw.hideConsole(): void` - tries to hide the console window on Windows.
113+
* `glfw.showConsole(): void` - shows the console window if it has been hidden.
114+
* `glfw.drawWindow(w: number, cb: (dateNow: number) => void): void` - this is a shortcut
115+
to call `pollEvents`, then `cb`, and then `swapBuffers`, where you only supply `cb`
116+
and C++ side does the rest.
117+
* `glfw.platformDevice(w: number): number` - returns the window HDC on Windows,
118+
or whatever is similar on other systems.
119+
* `glfw.platformWindow(w: number): number` - returns the window HWND on Windows,
120+
or whatever is similar on other systems.
121+
* `glfw.platformContext(w: number): number` - returns the window WGL Context on Windows,
122+
or whatever is similar on other systems.

src/cpp/glfw-common.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@
3636
#define RET_GLFW_VOID \
3737
return glfw::undefined;
3838

39+
inline Napi::Number NewInt64(napi_env env, int64_t val) {
40+
napi_value value;
41+
napi_status status = napi_create_double(env, val, &value);
42+
NAPI_THROW_IF_FAILED(env, status, Napi::Number());
43+
return Napi::Number(env, value);
44+
}
45+
46+
#define RET_PTR(VAL) return NewInt64(env, reinterpret_cast<int64_t>(VAL))
47+
3948
namespace glfw {
4049
Napi::Value undefined;
4150
}

src/cpp/glfw-platform.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,46 +11,46 @@ namespace glfw {
1111

1212
DBG_EXPORT JS_METHOD(platformDevice) { NAPI_ENV;
1313
#ifdef _WIN32
14-
RET_NUM(reinterpret_cast<uint64_t>(wglGetCurrentDC()));
14+
RET_PTR(wglGetCurrentDC());
1515
#elif __linux__
1616
if (glfwGetPlatform() == GLFW_PLATFORM_WAYLAND) {
17-
RET_NUM(reinterpret_cast<uint64_t>(glfwGetWaylandDisplay()));
17+
RET_PTR(glfwGetWaylandDisplay());
1818
} else {
19-
RET_NUM(reinterpret_cast<uint64_t>(glfwGetX11Display()));
19+
RET_PTR(glfwGetX11Display());
2020
}
2121
#elif __APPLE__
2222
CGLContextObj kCGLContext = CGLGetCurrentContext();
23-
RET_NUM(reinterpret_cast<uint64_t>(CGLGetShareGroup(kCGLContext)));
23+
RET_PTR(CGLGetShareGroup(kCGLContext));
2424
#endif
2525
}
2626

2727

2828
DBG_EXPORT JS_METHOD(platformWindow) { NAPI_ENV; THIS_WINDOW;
2929
#ifdef _WIN32
30-
RET_NUM(reinterpret_cast<uint64_t>(glfwGetWin32Window(window)));
30+
RET_PTR(glfwGetWin32Window(window));
3131
#elif __linux__
3232
if (glfwGetPlatform() == GLFW_PLATFORM_WAYLAND) {
33-
RET_NUM(reinterpret_cast<uint64_t>(glfwGetWaylandWindow(window)));
33+
RET_PTR(glfwGetWaylandWindow(window));
3434
} else {
35-
RET_NUM(reinterpret_cast<uint64_t>(glfwGetX11Window(window)));
35+
RET_PTR(glfwGetX11Window(window));
3636
}
3737
#elif __APPLE__
38-
RET_NUM(reinterpret_cast<uint64_t>(glfwGetCocoaWindow(window)));
38+
RET_PTR(glfwGetCocoaWindow(window));
3939
#endif
4040
}
4141

4242

4343
DBG_EXPORT JS_METHOD(platformContext) { NAPI_ENV; THIS_WINDOW;
4444
#ifdef _WIN32
45-
RET_NUM(reinterpret_cast<uint64_t>(glfwGetWGLContext(window)));
45+
RET_PTR(glfwGetWGLContext(window));
4646
#elif __linux__
4747
if (glfwGetPlatform() == GLFW_PLATFORM_WAYLAND) {
48-
RET_NUM(reinterpret_cast<uint64_t>(glfwGetEGLContext(window)));
48+
RET_PTR(glfwGetEGLContext(window));
4949
} else {
50-
RET_NUM(reinterpret_cast<uint64_t>(glfwGetGLXContext(window)));
50+
RET_PTR(glfwGetGLXContext(window));
5151
}
5252
#elif __APPLE__
53-
RET_NUM(reinterpret_cast<uint64_t>(glfwGetNSGLContext(window)));
53+
RET_PTR(glfwGetNSGLContext(window));
5454
#endif
5555
}
5656

0 commit comments

Comments
 (0)