Skip to content

Commit

Permalink
Merge pull request #6 from djfos/feature/detailed-test
Browse files Browse the repository at this point in the history
update readme
  • Loading branch information
djfos authored Mar 11, 2023
2 parents 2672c4f + 770c229 commit 74edeac
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 2 deletions.
103 changes: 102 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,108 @@
# Usage

```typescript
// TODO
import {
createWindow,
getPrimaryMonitor,
getProcAddress,
mainloop,
pollEvents,
} from "https://deno.land/x/[email protected]/mod.ts";
import * as gl from "https://deno.land/x/[email protected]/api/gles23.2.ts";
import * as imgui from "https://deno.land/x/[email protected]/mod.ts";
import {
Bool,
ImGuiConfigFlagBits,
showWidgetDemoWindow,
} from "https://deno.land/x/[email protected]/mod.ts";

function queryWindowSizeAndFontSize() {
const aspectRatio = 16 / 9;
const lines = 40;
const monitor = getPrimaryMonitor();
const height = Math.ceil(monitor.workArea.height * 0.7);
const width = Math.ceil(height * aspectRatio);
const fontSize = Math.min(32, Math.ceil(height / lines));
return { width, height, fontSize };
}
const windowInfo = queryWindowSizeAndFontSize();
console.info(windowInfo);

const window = createWindow({
title: "IMGUI DWM",
width: windowInfo.width,
height: windowInfo.height,
resizable: true,
glVersion: "v3.2",
gles: false,
});

// load opengl functions
gl.load(getProcAddress);

// create imgui context and init imgui backends
const imguiContext = imgui.createContext();
imgui.implGlfwInitForOpenGL(window.nativeHandle);
imgui.implOpenGL3Init("#version 130");

// set io
const io = imgui.getIO();
io.ConfigFlags |= ImGuiConfigFlagBits.DockingEnable;

// set font
const fonts = io.Fonts;
if (Deno.build.os == "windows") {
const fontFile = "C:/Windows/Fonts/consola.ttf";
try {
const fontData = Deno.readFileSync(fontFile);
fonts.addFontFromMemoryTTF(fontData, windowInfo.fontSize);
} catch (error) {
console.error(error);
}
}

// states, primitive wrappers
const showDImGuiDemoWindow = Bool.of(true);
const showCppDemoWindow = Bool.of(true);

await mainloop(() => {
// new frame
imgui.implOpenGL3NewFrame();
imgui.implGlfwNewFrame();
imgui.newFrame();

// draw widgets
imgui.begin("controls");
imgui.checkbox("dimgui demo", showDImGuiDemoWindow.buffer);
imgui.checkbox("c++ demo", showCppDemoWindow.buffer);
imgui.showStyleSelector("style selector");
imgui.showFontSelector("font selector");
imgui.end();

if (showDImGuiDemoWindow.value) {
showWidgetDemoWindow();
}
if (showCppDemoWindow.value) {
imgui.showDemoWindow(showCppDemoWindow.buffer);
}
// submit
imgui.render();

// clear screen with default color
gl.Clear(gl.COLOR_BUFFER_BIT);
// render by imgui backend
const drawData = imgui.getDrawData();
imgui.implOpenGL3RenderDrawData(drawData);

// swap render buffer and handle window events
window.swapBuffers();
pollEvents();
});

// clean up
imgui.implOpenGL3Shutdown();
imgui.implGlfwShutdown();
imgui.destroyContext(imguiContext);
```

# Contribute
Expand Down
2 changes: 1 addition & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
]
}
}
}
}
22 changes: 22 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

98 changes: 98 additions & 0 deletions test/test_example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import {
createWindow,
getPrimaryMonitor,
getProcAddress,
mainloop,
pollEvents,
} from "https://deno.land/x/[email protected]/mod.ts";
import * as gl from "https://deno.land/x/[email protected]/api/gles23.2.ts";
import * as imgui from "https://deno.land/x/[email protected]/mod.ts";
import { Bool, ImGuiConfigFlagBits, showWidgetDemoWindow } from "https://deno.land/x/[email protected]/mod.ts";

function queryWindowSizeAndFontSize() {
const aspectRatio = 16 / 9;
const lines = 40;
const monitor = getPrimaryMonitor();
const height = Math.ceil(monitor.workArea.height * 0.7);
const width = Math.ceil(height * aspectRatio);
const fontSize = Math.min(32, Math.ceil(height / lines));
return { width, height, fontSize };
}
const windowInfo = queryWindowSizeAndFontSize();
console.info(windowInfo);

const window = createWindow({
title: "IMGUI DWM",
width: windowInfo.width,
height: windowInfo.height,
resizable: true,
glVersion: "v3.2",
gles: false,
});

// load opengl functions
gl.load(getProcAddress);

// create imgui context and init imgui backends
const imguiContext = imgui.createContext();
imgui.implGlfwInitForOpenGL(window.nativeHandle);
imgui.implOpenGL3Init("#version 130");

// set io
const io = imgui.getIO();
io.ConfigFlags |= ImGuiConfigFlagBits.DockingEnable;

// set font
const fonts = io.Fonts;
if (Deno.build.os == "windows") {
const fontFile = "C:/Windows/Fonts/consola.ttf";
try {
const fontData = Deno.readFileSync(fontFile);
fonts.addFontFromMemoryTTF(fontData, windowInfo.fontSize);
} catch (error) {
console.error(error);
}
}

// states, primitive wrappers
const showDImGuiDemoWindow = Bool.of(true);
const showCppDemoWindow = Bool.of(true);

await mainloop(() => {
// new frame
imgui.implOpenGL3NewFrame();
imgui.implGlfwNewFrame();
imgui.newFrame();

// draw widgets
imgui.begin("controls");
imgui.checkbox("dimgui demo", showDImGuiDemoWindow.buffer);
imgui.checkbox("c++ demo", showCppDemoWindow.buffer);
imgui.showStyleSelector("style selector");
imgui.showFontSelector("font selector");
imgui.end();

if (showDImGuiDemoWindow.value) {
showWidgetDemoWindow();
}
if (showCppDemoWindow.value) {
imgui.showDemoWindow(showCppDemoWindow.buffer);
}
// submit
imgui.render();

// clear screen with default color
gl.Clear(gl.COLOR_BUFFER_BIT);
// render by imgui backend
const drawData = imgui.getDrawData();
imgui.implOpenGL3RenderDrawData(drawData);

// swap render buffer and handle window events
window.swapBuffers();
pollEvents();
});

// clean up
imgui.implOpenGL3Shutdown();
imgui.implGlfwShutdown();
imgui.destroyContext(imguiContext);

0 comments on commit 74edeac

Please sign in to comment.