DEV Community

Cover image for OpenGL Game in Clojure CLR (Mono/.Net) - Part2 (The GameWindow)
Pankaj Doharey
Pankaj Doharey

Posted on • Edited on

2 1

OpenGL Game in Clojure CLR (Mono/.Net) - Part2 (The GameWindow)

Importing OpenTK assembly.

We will begin with importing the OpenTK dll files into our source.

Update the core.clj with the following code:

(ns core
(:gen-class))
(assembly-load-from "./extern/OpenTK/lib/net20/OpenTK.dll")
(defn -main[]
(Console/WriteLine "Starting OpenTK Window."))
view raw core-import.clj hosted with ❤ by GitHub

In the above code we just added the relative path of OpenTK.dll, a .net assembly wrapper for OpenGL.

The we import the necessary drawing and input libs and classes.

(ns core
(:gen-class))
(assembly-load-from "./extern/OpenTK/lib/net20/OpenTK.dll")
(import [System]
[System.IO]
[System.Console]
[System.Drawing]
[OpenTK]
[OpenTK.Graphics.ES30 ClearBufferMask]
[OpenTK.Graphics.OpenGL GL]
[OpenTK.Input]
[clojure.lang])
(defn -main []
(Console/WriteLine "Starting OpenTK Window."))

Once the necessary classes are imported we will begin with creating an instance of GameWindow class and àssigning a cornflower blue color to the background.

(ns core
(:gen-class))
(assembly-load-from "./extern/OpenTK/lib/net20/OpenTK.dll")
(import [System]
[System.IO]
[System.Console]
[System.Drawing]
[OpenTK]
[OpenTK.Graphics.ES30 ClearBufferMask]
[OpenTK.Graphics.OpenGL GL]
[OpenTK.Input]
[clojure.lang])
(defonce game (new GameWindow 800 600))
(def CornflowerBlue Color/CornflowerBlue)
(defn -main []
(Console/WriteLine "Starting OpenTK Window.")
(GL/ClearColor CornflowerBlue)
(GL/Clear ClearBufferMask/ColorBufferBit)
(.SwapBuffers game)
(.Run game 30.0))

Important thing to note in the above is (.SwapBuffers game) because OpenGl is double buffered so while one buffer is showing on the screen other can be used to be drawn to.

Once a drawing action has been accomplished we need to instruct the Graphics card to swap the visible buffer with the buffer drawn to this makes the new buffer visible and hides the previous one. Double buffering is essential in creating the illusion of animation by drawing to alternate buffers and swapping them.

Also (.Run game 30.0) is essentially telling OpenGL to refresh the screen at 30 FPS (30 Frames per second)

Now let us compile this program using:

$ ./build.sh c
Compiling core to build/ -- 1796 milliseconds.

And then run it using :

$ ./build.sh r

This should result in a sunflower blue coloured window:

SunFlowerGamewindow

So now we have a simple OpenGL Window Running, but it doesnt handle any inputs as of yet. In the next series lets implement ways to handle keyboard input to close the window.

All Posts:

Part 1 - The Setup
Part 3 - Handling Input

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More