Skip to content

Instantly share code, notes, and snippets.

@dallinbeutler
Created July 9, 2019 18:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dallinbeutler/a2bafdea67a8ae1f02822470f9d9c022 to your computer and use it in GitHub Desktop.
Save dallinbeutler/a2bafdea67a8ae1f02822470f9d9c022 to your computer and use it in GitHub Desktop.
a simple example to get a graphical or game window and GUI going in F# with Veldrid and ImGUI
module ElmEye
open Veldrid
open Veldrid.StartupUtilities
let CreateApp windowWidth windowHeight name =
let windowCI = WindowCreateInfo(
X = 400,
Y =400,
WindowWidth = windowWidth,
WindowHeight = windowHeight,
WindowTitle = name
)
let window = VeldridStartup.CreateWindow(windowCI)
window.add_Closed (fun () -> exit 0 |>ignore )
window.Resizable <- true
window.BorderVisible <-true
window
open System.Diagnostics
open System.Numerics
open ImGuiNET
[<EntryPoint>]
let main argv =
let window = CreateApp 640 480 "ElmEye"
let gdo = GraphicsDeviceOptions()
let gd = VeldridStartup.CreateGraphicsDevice(window)
let gui = new ImGuiRenderer(gd, (gd.MainSwapchain.Framebuffer.OutputDescription),window.Width, window.Height)
let sw = Stopwatch.StartNew()
let mutable lastFrame = sw.ElapsedMilliseconds
window.add_Resized ( fun ()-> gd.ResizeMainWindow(uint32 window.Width,uint32 window.Height)
gui.WindowResized(window.Width, window.Height)
)
while window.Exists do
let cl = gd.ResourceFactory.CreateCommandList()
let events = window.PumpEvents()
gui.Update( float32(sw.ElapsedMilliseconds - lastFrame) ,events)
//If the font is too small
//ImGui.SetWindowFontScale (float32 1.8)
//Use these commands to create an anchor-style panel
ImGui.SetNextWindowSize(Vector2(float32 (window.Width/2), float32 window.Height))
ImGui.SetNextWindowPos( Vector2(float32 0,float32 0), ImGuiCond.Always)
ImGui.Begin("main", ImGuiWindowFlags.NoMove
||| ImGuiWindowFlags.NoCollapse
||| ImGuiWindowFlags.NoTitleBar
||| ImGuiWindowFlags.NoResize)|>ignore
ImGui.Text "Hello Veldrid"
ImGui.End()
//the actual Veldrid drawing part
cl.Begin()
cl.SetFramebuffer(gd.MainSwapchain.Framebuffer)
//A simple interaction to show you how to capture mouse events in GUI vs your game
if ImGui.GetIO().WantCaptureMouse then
cl.ClearColorTarget(uint32 0 , RgbaFloat.Black)
else
cl.ClearColorTarget(uint32 0 , RgbaFloat.CornflowerBlue)
gui.Render (gd, cl)
cl.End()
gd.SubmitCommands cl
gd.SwapBuffers()
printfn "%A" argv
0 // return an integer exit code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment