<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Pankaj Doharey</title>
    <description>The latest articles on DEV Community by Pankaj Doharey (@metacritical).</description>
    <link>https://dev.to/metacritical</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2459%2F071f0ec7-a320-41aa-bbbc-e15119552ed7.jpg</url>
      <title>DEV Community: Pankaj Doharey</title>
      <link>https://dev.to/metacritical</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/metacritical"/>
    <language>en</language>
    <item>
      <title>OpenGL Game in Clojure CLR (Mono/.Net) - Part3 (Handling Input)</title>
      <dc:creator>Pankaj Doharey</dc:creator>
      <pubDate>Tue, 30 Jul 2019 18:39:48 +0000</pubDate>
      <link>https://dev.to/metacritical/create-an-opengl-game-in-clojure-clr-mono-net-part3-handling-input-2leo</link>
      <guid>https://dev.to/metacritical/create-an-opengl-game-in-clojure-clr-mono-net-part3-handling-input-2leo</guid>
      <description>&lt;h2&gt;
  
  
  Handling Keyboard input and Game loop.
&lt;/h2&gt;

&lt;p&gt;Most 3D applications like games run in a game loop where are series of events modify the game and OpenGL state to draw things on screen. One such important event is user input event. OpenTK &lt;code&gt;GameWindow&lt;/code&gt; class has&lt;br&gt;
one such method called &lt;code&gt;OnUpdateFrame&lt;/code&gt; which as the name suggest gets triggered every time the frame is rendered. &lt;/p&gt;

&lt;p&gt;Right now it is getting it isnt doing anything since it is in the parent class and we are dealing with an instance of &lt;code&gt;GameWindow&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;What we really need to do is inherit the &lt;code&gt;GameWindow&lt;/code&gt; and override the &lt;code&gt;OnUpdateFrame&lt;/code&gt; methods and capture the window events as they happen.&lt;/p&gt;

&lt;p&gt;In order to do so there are primarily 3 mechanisms in in Clojure to deal with Inheritance. &lt;code&gt;gen-class&lt;/code&gt;, &lt;code&gt;proxy&lt;/code&gt; and &lt;code&gt;reify&lt;/code&gt;. But since we are dealing with the instance of the inherited class &lt;code&gt;reify&lt;/code&gt; cannot be used.&lt;/p&gt;

&lt;p&gt;Since i aim to keep the tutorial largely functional my tool of choice is &lt;code&gt;proxy&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So let us modify our &lt;code&gt;core&lt;/code&gt; namespace and create a proxy object and override the &lt;code&gt;OnUpdateFrame&lt;/code&gt; method like so:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;As you can see &lt;code&gt;OpenTK.GameWindow&lt;/code&gt; constructor has one more overloading function which can take four parameters, &lt;code&gt;width&lt;/code&gt;,&lt;code&gt;height&lt;/code&gt;, &lt;code&gt;GraphicsMode&lt;/code&gt; and &lt;code&gt;Title&lt;/code&gt; of the window. For now we are sticking with default graphics mode thus a &lt;code&gt;nil&lt;/code&gt; is passed.&lt;/p&gt;

&lt;p&gt;If you compile and run this program it will print the event object to the terminal with every refresh.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4tDoGVTV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/yfappdk2x1lx31ynomw9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4tDoGVTV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/yfappdk2x1lx31ynomw9.png" alt="Print Event Object" width="800" height="526"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since, much of our game logic will go inside the &lt;code&gt;OnUpdateFrame&lt;/code&gt; method it will be better to dispatch the events to a separate on-update-frame function to handle the inputs and keep only the override definition in the proxy object like so:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Lets us compile and run this application:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jiY0clBq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/wvg9qzy167oxy5pnh69x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jiY0clBq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/wvg9qzy167oxy5pnh69x.png" alt="Handle Keypress" width="800" height="529"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You see every time we press the escape key it would print &lt;code&gt;Escape Pressed&lt;/code&gt; on the terminal multiple times because the refresh rate of the window is so fast that it ends up capturing the escape key press event multiple times.&lt;/p&gt;

&lt;p&gt;For now we want the Escape key to exit the current window. But the &lt;code&gt;on-update-frame&lt;/code&gt; does not have a handle of the &lt;code&gt;proxy&lt;/code&gt; instance.&lt;/p&gt;

&lt;p&gt;There is a way though &lt;code&gt;C#&lt;/code&gt; has a special keyword &lt;code&gt;this&lt;/code&gt; which gives a handle on the current execution context. Thus we would modify our &lt;code&gt;OnUpdateFrame&lt;/code&gt; override and &lt;code&gt;on-update-frame&lt;/code&gt; function and pass this as a parameter. Then we can simply call &lt;code&gt;(.Exit this)&lt;/code&gt; in the input &lt;code&gt;let&lt;/code&gt; block. &lt;/p&gt;

&lt;p&gt;Let us modify our code like so:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;This exits the window when you press escape.&lt;/p&gt;

&lt;p&gt;Next we will refactor and properly organise the code, and learn how to draw shapes on the screen ...&lt;/p&gt;

&lt;p&gt;To be continued ...&lt;/p&gt;

&lt;p&gt;Previous Posts:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/metacritical/create-an-opengl-game-in-clojure-clr-mono-net-part1-the-setup-59ai"&gt;Part 1 - The Setup&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/metacritical/create-an-opengl-game-in-clojure-clr-mono-net-part2-the-gamewindow-2gf"&gt;Part 2 - The Game Window&lt;/a&gt;&lt;/p&gt;

</description>
      <category>clojure</category>
      <category>mono</category>
      <category>opengl</category>
      <category>opentk</category>
    </item>
    <item>
      <title>OpenGL Game in Clojure CLR (Mono/.Net) - Part2 (The GameWindow)</title>
      <dc:creator>Pankaj Doharey</dc:creator>
      <pubDate>Mon, 29 Jul 2019 18:06:31 +0000</pubDate>
      <link>https://dev.to/metacritical/create-an-opengl-game-in-clojure-clr-mono-net-part2-the-gamewindow-2gf</link>
      <guid>https://dev.to/metacritical/create-an-opengl-game-in-clojure-clr-mono-net-part2-the-gamewindow-2gf</guid>
      <description>&lt;h2&gt;
  
  
  Importing OpenTK assembly.
&lt;/h2&gt;

&lt;p&gt;We will begin with importing the OpenTK &lt;code&gt;dll&lt;/code&gt; files into our source.&lt;/p&gt;

&lt;p&gt;Update the &lt;code&gt;core.clj&lt;/code&gt; with the following code:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In the above code we just added the relative path of &lt;code&gt;OpenTK.dll&lt;/code&gt;, a .net assembly wrapper for OpenGL.&lt;/p&gt;

&lt;p&gt;The we import the necessary drawing and input libs and classes.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Once the necessary classes are imported we will begin with creating an instance of &lt;code&gt;GameWindow&lt;/code&gt; class and àssigning a cornflower blue color to the background.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


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

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Also &lt;code&gt;(.Run game 30.0)&lt;/code&gt; is essentially telling OpenGL to refresh the screen at &lt;code&gt;30 FPS&lt;/code&gt; (30 Frames per second)&lt;/p&gt;

&lt;p&gt;Now let us compile this program using:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ ./build.sh c&lt;/code&gt;&lt;br&gt;
&lt;code&gt;Compiling core to build/ -- 1796 milliseconds.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And then run it using :&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ ./build.sh r&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This should result in a sunflower blue coloured window:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F1slmg1w7acydybj63lpe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F1slmg1w7acydybj63lpe.png" alt="SunFlowerGamewindow"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;All Posts:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/metacritical/create-an-opengl-game-in-clojure-clr-mono-net-part1-the-setup-59ai"&gt;Part 1 - The Setup&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/metacritical/create-an-opengl-game-in-clojure-clr-mono-net-part3-handling-input-2leo"&gt;Part 3 - Handling Input&lt;/a&gt;&lt;/p&gt;

</description>
      <category>clojure</category>
      <category>mono</category>
      <category>opengl</category>
      <category>opentk</category>
    </item>
    <item>
      <title>OpenGL Game in Clojure CLR (Mono/.Net) - Part1 (The Setup)</title>
      <dc:creator>Pankaj Doharey</dc:creator>
      <pubDate>Mon, 29 Jul 2019 15:00:01 +0000</pubDate>
      <link>https://dev.to/metacritical/create-an-opengl-game-in-clojure-clr-mono-net-part1-the-setup-59ai</link>
      <guid>https://dev.to/metacritical/create-an-opengl-game-in-clojure-clr-mono-net-part1-the-setup-59ai</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;I gave been thinking about writing 2D/3D OpenGL games in &lt;code&gt;clojure&lt;/code&gt; for sometime. When it comes to developing games you have many choices with &lt;code&gt;clojure&lt;/code&gt;. You can develop games for webgl using clojurescript + Webgl/Canvas Api or any 2D/3D javascript library of your choice. You can also use JVM and its many OpenGL libraries like LWJGL.&lt;/p&gt;

&lt;p&gt;There is a third choice if you want your games to be highly portable you can choose a game engine with C# bindings. Or better yet use &lt;code&gt;Clojure-CLR&lt;/code&gt; to develop on the &lt;code&gt;.Net&lt;/code&gt; framework. This is a first tutorial in hopefully a series of tutorials targeting the third option. &lt;/p&gt;

&lt;p&gt;Since C# on .Net is the most popular choice of game development for a large number of Indie and Small studios i think it would be an excellent choice to use Clojure CLR to develop cross platform games on the Mono/.Net Framework. &lt;/p&gt;

&lt;p&gt;The tutorial is geared towards development on a macOS with mono/.Net but the it should be directly applicable on Ubuntu/Linux or Windows.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setup.
&lt;/h2&gt;

&lt;p&gt;In order to start developing Clojure CLR application on a mac we need to install &lt;code&gt;Mono&lt;/code&gt; which is an open-source implementation on Microsoft .Net Framework on Linux and Mac.&lt;/p&gt;

&lt;p&gt;The simplest way to install mono on a mac is using the mac universal package from here &lt;a href="https://www.mono-project.com/download/stable/" rel="noopener noreferrer"&gt;https://www.mono-project.com/download/stable/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This will also install &lt;code&gt;nuget&lt;/code&gt;, which is a package manager to fetch &lt;code&gt;.Net&lt;/code&gt; packages.&lt;/p&gt;

&lt;p&gt;Once &lt;code&gt;Mono&lt;/code&gt; is installed, which you can check by typing the following on the terminal.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ mono --version&lt;/code&gt;&lt;br&gt;
&lt;code&gt;=&amp;gt; Mono JIT compiler version 6.0.0.313 (tarball Fri Jul 19 16:02:10 BST 2019)&lt;br&gt;
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Install Clojure-CLR
&lt;/h3&gt;

&lt;p&gt;Now let's install &lt;code&gt;clojure-clr&lt;/code&gt;, though you are free to try official &lt;code&gt;Clojure-CLR&lt;/code&gt; this tutorial uses a particular fork of &lt;code&gt;Clojure-CLR&lt;/code&gt; maintained by &lt;a href="https://twitter.com/ra" rel="noopener noreferrer"&gt;Ramsey Nasser&lt;/a&gt; and co of &lt;a href="http://arcadia.technology/" rel="noopener noreferrer"&gt;Arcadia Technologies&lt;/a&gt;, since that is the &lt;code&gt;Clojure-CLR&lt;/code&gt; i found most success with instead of the official binaries.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ git clone git@github.com:arcadia-unity/clojure-clr.git&lt;/code&gt;&lt;br&gt;
&lt;code&gt;$ cd clojure-clr/&lt;/code&gt;&lt;br&gt;
&lt;code&gt;$ git checkout unity&lt;/code&gt;&lt;br&gt;
&lt;code&gt;$ make&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If the compilation is successful this should produce a &lt;code&gt;Release&lt;/code&gt; directory in &lt;code&gt;bin/4.0/Release/&lt;/code&gt;. Now you should add this directory as an entry to &lt;code&gt;CLOJURE_LOAD_PATH&lt;/code&gt; environment variable in &lt;code&gt;~/.bash_profile&lt;/code&gt; so that we can write build scripts to find and use &lt;code&gt;clojure-clr&lt;/code&gt; binaries.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;export CLOJURE_LOAD_PATH=$HOME/Projects/clojure-clr/bin/4.0/Release/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It would also be useful to add the following aliases to &lt;code&gt;~/.bash_profile&lt;/code&gt;&lt;br&gt;
&lt;code&gt;#Clojure clr repl.&lt;/code&gt;&lt;br&gt;
&lt;code&gt;alias cljclr="mono $CLOJURE_LOAD_PATH/Clojure.Main.exe"&lt;/code&gt;&lt;br&gt;
&lt;code&gt;#Clojure clr compiler.&lt;/code&gt;&lt;br&gt;
&lt;code&gt;alias cljcomp="mono $CLOJURE_LOAD_PATH/Clojure.Compile.exe"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Also do remember to source the bash profile: &lt;code&gt;$ source ~/.bash_profile&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The above aliases are clojure-clr repl and compiler respectively.&lt;/p&gt;

&lt;p&gt;Invoking &lt;code&gt;$ cljclr&lt;/code&gt; on the terminal and you would be greeted with a clojure repl.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Clojure 1.10.0-master-SNAPSHOT&lt;br&gt;
user=&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You can run clojure commands on the repl now.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;user =&amp;gt; (+ 1 2 3)&lt;br&gt;
6&lt;/code&gt;  you can press &lt;code&gt;Ctrl+c&lt;/code&gt; to exit the repl.&lt;/p&gt;

&lt;p&gt;The last thing we need to install is a &lt;code&gt;C#&lt;/code&gt; bindings for OpenGL, one such wrapper in &lt;a href="https://opentk.net" rel="noopener noreferrer"&gt;OpenTK&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;we will begin by creating a directory structure for our app.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ mkdir -p ~/Projects/cljopentk/extern&lt;/code&gt;&lt;br&gt;
&lt;code&gt;$ mkdir -p ~/Projects/cljopentk/build&lt;/code&gt;&lt;br&gt;
&lt;code&gt;$ cd ~/Projects/cljopentk/extern&lt;/code&gt;&lt;br&gt;
&lt;code&gt;nuget install OpenTK -ExcludeVersion&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;this should download the latest OpenTK bindings into the extern directory. An ls into the directory should show the following dlls.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ ls extern/OpenTK/lib/net20/&lt;/code&gt;&lt;br&gt;
&lt;code&gt;&amp;gt;&amp;gt; OpenTK.dll  OpenTK.pdb  OpenTK.xml&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;now lets create a core.clj file in the parent directory &lt;code&gt;cljopentk&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ cd ..&lt;/code&gt;&lt;br&gt;
&lt;code&gt;$ touch core.clj build.sh&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Build Script.
&lt;/h2&gt;

&lt;p&gt;Make the &lt;code&gt;build.sh&lt;/code&gt; as executable,&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ chmod +x build.sh&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;and paste the following code in it.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  Create DLL softlinks for program execution.
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;$ ./build.sh link&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Hi World!
&lt;/h3&gt;

&lt;p&gt;Now let us write a small clojure "hi-world" program.&lt;br&gt;
Paste the following code in the newly created &lt;code&gt;core.clj&lt;/code&gt;.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Compile the program using the build script.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ ./build.sh c&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Run the program with.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ ./build.sh r&lt;/code&gt;&lt;br&gt;
&lt;code&gt;Hi World&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now that we have a basic setup to compile clojure-clr programs we can continue with developing the OpenTK/OpenGL game. &lt;/p&gt;

&lt;p&gt;Since you are still reading perhaps you want to checkout the next post in this series :&lt;/p&gt;

&lt;p&gt;All Posts:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/metacritical/create-an-opengl-game-in-clojure-clr-mono-net-part2-the-gamewindow-2gf"&gt;Part 2 - The Game Window&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/metacritical/create-an-opengl-game-in-clojure-clr-mono-net-part3-handling-input-2leo"&gt;Part 3 - Handling Input&lt;/a&gt;&lt;/p&gt;

</description>
      <category>clojure</category>
      <category>mono</category>
      <category>opengl</category>
      <category>opentk</category>
    </item>
  </channel>
</rss>
