Sunday, December 11, 2016

Vala Revisited

The last time I looked into Vala, I became a bit discouraged. I couldn't find good tooling. The documentation could be better. I wasn't getting good performance. I think a full GObect is too expensive for game components.

I have found better tooling. There is a Vala plugin for VSCode https://marketplace.visualstudio.com/items?itemName=thiagoabreu.vala. I like vscode. It's fast, easy to use and flexible. I can focus on my code and stop worrying.

But whan I say Vala, I really mean Genie. Most of my code for valac is written using Genie. So I've started a plugin for Genie https://github.com/darkoverlordofdata/vscode-genie. Along with the  LLDB, CMake and C/C++ plugins, I have all I need for quick iterative development.

Now about GObject. The documentation is unclear. All the examples extend it as a base class. Why do I need it? There are dire warnings that language features like interfaces and properties won't work without it. And then there is a disclaimer that 'In a more recent Vala compiler' this is relaxed. But there are no specifics. Has that version been released? I think I'm better off not reading the documentation.

So I've factored out GLib.Object to 'try it and see'. All my classes now extend a custom, empty object. In one posting, Gnome calls these 'Non-Object' classes. But, as it turns out, it makes no difference. When I removed GObject, no features were impacted. My performance problems went away, but adding GObject back in doesn't re-introduce them... So where did they come from? What changed?

Time passes - new computer. New OS. Newer SDL libraries. Newer Vala runtime. Take your pick. But the good news is that it works pretty good now. Comparable to Nim.

Benchmarking can be tricky. So I've tracked the average time per frame taken up by the update method on my game object. 

    time in ms

            nim           vala     
    min     0.00015     0.00019     
    max     0.00202     0.0035     
    avg     0.00082     0.0009     


At 60 fps, each frame is .0167ms, and these are all very acceptable times. Vala and Nim seem pretty well tied for performance. 

Monday, November 28, 2016

Giving the Devil it's Due

That 'devil' bing Microsoft. I know, https://blog.darkoverlordofdata.com/2016/03/just-say-no-to-microsoft.html. But I have to move on, and what's more, I had to buy a new computer.

My beautiful new ASUS Zenbook came with Windows 10 pre-installed. I of course dual booted it with ElementaryOS, but since then, I'm a bit embarassed to say, most of my time has been spent using Windows 10.

It used to be when asked why I choose Linux, the answer was easy. But my reasons are mostly technical, and Windows 10 really addresses many of them. There are still many good reasons to choose Linux -


  • Windows 10 Desktop is very distracting. I Love ElementaryOS.
  • Linux fonts are still easier for me to read.
  • I prefer Geary to MS Mail
  • Build processes like gradle run faster on Linux - this impacts iterative testing.
  • The Windows Subsystem for Linux isn't well suited for gui programming.


But Windows has Visual Studio 2015 and Xamarin with excellent support for MonoGame and FSharp, and a path to put FSharp on Android. This seems preferable to my current path of Android Studio, Scala, and libGDX. And it's a jvm free solution - did I ever mention that I hate the jvm?

Saturday, September 10, 2016

ElementaryOS Loki - The Trickster

After installing the latest ElementaryOS Loki, I was left sadly unimpressed. It seemed bugy for a release. But I needed a break, and I had found my old Creative ZEN all covered in dust. I was able to pull off some old music videos of The Small Faces that I wanted to see (I'll get to the point...) I hadn't looked at these in years. So I fired up Videos, surely THAT would be non stressful. The video started, but there was no sound. Check - yep sounds are turned on. I must be missing some codecs. I run

sudo apt-get install ubuntu-restricted-extras

And then I have music. And the light came on. During install, I had connected to the internet and selected to


  • load updates while installing
  • install 3rd party software wich includes MP3


But apparently it didn't happen. So I clicked on AppCenter and selected Updates, and sure enough, there was a whole slew of updates waiting. The moral of this story is always double check the install.

After running those updates and rebooting, I found that most of the bugy stuff was gone.

And now I am suitabley impressed with Loki!

Friday, September 9, 2016

After Installing ElementaryOS Loki 4

I upgraded to ElementaryOS Loki Stable Release today. Installing a new OS is often stressful, but this was worst I've experienced in a few years. Was it worth it? Yep. I'm in love!
I've recorded the steps I went thru to 'get back to normal'

1) Elementary has hobbled the command line package manager. To restore:
sudo dpkg --configure -a
sudo apt-get update
sudo apt-get install gdebi
apt-get install software-properties-common

2) There is an upgraded version of Elementary Tweaks! Bright desktops give me a headache, I need the dark theme to function:
sudo add-apt-repository ppa:philip.scott/elementary-tweaks && sudo apt-get update
sudo apt-get install elementary-tweaks

3) Elementary doesn't allow desktop icons. I don't get why.
sudo apt-get install -–no-install-recommends nautilus dconf-tools
Here are the instructions I follow to configure the desktop:
http://mylinuxideas.blogspot.com/2015/04/enable-desktop-icons-and-right-click-on.html

4) And I want a show desktop icon. This puts it right on the plank:
sudo apt-get install wmctrl
cd /tmp && wget https://github.com/png2378/showdesktop/archive/master.zip
unzip master.zip && cd showdesktop-master
sudo mv showdesktop /usr/local/bin/ && sudo mv showdesktop.desktop /usr/share/applications/ && sudo mv showdesktop.svg /usr/share/icons/elementary/apps/48/
mv showdesktop.dockitem ~/.config/plank/dock1/launchers/
http://elementaryos.stackexchange.com/questions/2001/add-icon-to-show-desktop


Finally, I found that Loki still has a couple of quirks.
1) My mouse cursor disappears. To prevent this, I set the power setting 'Turn off display when inactive' to Never.
2) The top bar keeps freezing up. I created a simple desktop icon to fix that when I click on it:

[Desktop Entry]
Version=1.0
Terminal=false
Type=Application
Name=Fix Wingpanel
Exec=pkill wingpanel
Icon=/usr/share/icons/elementary/apps/64/utilities-terminal.svg
Comment[en_US]=Fix Frozen Top Bar
GenericName[en_US]=Fix Frozen Top Bar
Save this to ~/Desktop/Fix.desktop then start a terminal and enter:
$ chmod +x  Desktop/Fix.desktop
And now I click on it about 50 times a day...

Thursday, June 23, 2016

Using ES6 Modules in CoffeeScript

As we saw in my last post, https://blog.darkoverlordofdata.com/2016/06/mixing-globls-with-es6-module.html, we can use the TypeScript compiler to transpile import/export statements in our ES6 files. But were not really limited to ES6.

We can also use CoffeeScript as a pre-processor for the TypeScript compiler. To use modules in CoffeeScript is pretty easy, we can use the tick (`) to escape javascipt. Hopefully this will fully supported soon at the syntax level.

Suppose I create a module like this:

`import Batch from 'gdx/graphics/g2d/Batch'`

class SpriteBatch extends Batch 


`export default SpriteBatch`

and then I compile like this, where my jsconfig.json file references the output from coffee:

coffee -o src/js -bc src/coffee && tsc -p jsconfig.json 

And something like this is emitted by typescript into the outpur file:

define("gdx/graphics/g2d/SpriteBatch", ["require", "exports", "gdx/graphics/g2d/Batch"], function (require, exports, Batch_1) {
    "use strict";
    SpriteBatch = (function (superClass) {
        extend(SpriteBatch, superClass);
        function SpriteBatch() {
            return SpriteBatch.__super__.constructor.apply(this, arguments);
        }
        return SpriteBatch;
    })(Batch_1.default);
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.default = SpriteBatch;
});
```

Using typescript to build my coffescript. Why does that seem so wrong?

Tuesday, June 21, 2016

Mixing Globals with ES6 Module Implementation

Typescript 1.8 adds the new jsconfig.json format. So now, when the compilerOptions.module field is set to 'system' or 'amd' and compilerOptions.outFile is also set, typescript will bundle all of my modules, transpiling import/export polyfills. But it incudes no implementation for those polyfills. I could use SystemJs, but that is another external, and it's overkill - I only need to use module syntax internally, to see other modules in the same bundle.

My project consumes globals, such as PIXI.js. And to expose my API to scala.js, my library needs to be a global. But within my library, I want to use all the new es6 features, including import and export.

So my jsconfig.json looks like this:

{
    "compilerOptions": {
        "target": "es6",
        "module": "amd",
        "rootDir": "src",
        "outFile": "js/app.js"
    },
    "files": [
        "src/define.js",
        "src/lib/lib.js",
        "src/lib/shapes/Circle.js",
        "src/lib/utils/functions.js"
    ]
}

Typescript normalizes the interface, so no parameter checking is needed. For amd modules, the define
function is less than 10 lines of code:
After running tsc, the outFile js/app.js:

var define = (function (modules) {
    return (name, deps, callback) => {
        modules[name] = { id: name, exports: {} };
        let args = [(name) => modules[name].exports, modules[name].exports];
        for (let i = 2; i < deps.length; i++)
            args.push(modules[deps[i]].exports);
        callback.apply(modules[name].exports, args);
    };
}({}));
define("lib/shapes/Circle", ["require", "exports"], function (require, exports) {
    "use strict";
    //------ lib.js ------
    class Circle {
        constructor(radius) {
            this.radius = radius;
        }
    }
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.default = Circle;
});
define("lib/utils/functions", ["require", "exports"], function (require, exports) {
    "use strict";
    //------ lib2.js ------
    exports.sqrt = Math.sqrt;
    function square(x) {
        return x * x;
    }
    exports.square = square;
    function diag(x, y) {
        return exports.sqrt(square(x) + square(y));
    }
    exports.diag = diag;
});
define("lib/lib", ["require", "exports", "lib/shapes/Circle", "lib/utils/functions"], function (require, exports, Circle_1, functions) {
    "use strict";
    /**
     * Re-export it all to a namespace-like object hierarchy
     */
    window.lib = {
        shapes: {
            Circle: Circle_1.default
        },
        utils: {
            functions: functions
        }
    };
});


Friday, May 27, 2016

Using an ECS with Scala.JS

I've created an implementation of Entitas ECS in Scala. The source is over on github: https://github.com/darkoverlordofdata/entitas-scala.

I think the big drawback to using an entity component system is that it takes a lot of boilerplate to get a project up and running. So, I also have a command line tool to help me with that, entitas-cli. Entitas-cli requires nodejs, and uses Liquid templates to generate code from a json configuration file.

To install entitas-cli: 'npm install -g entitas-cli'

I will need an existing project, so using the tutorial at https://www.scala-js.org/tutorial/basic/, I've created a basic HelloWorld application.  I only followed the first three steps, and then I add my dependencies, so now my build.sbt looks like this:

scalaJSUseRhino in Global := false
enablePlugins(ScalaJSPlugin)
name := "Scala.js Tutorial"
scalaVersion := "2.11.7" // or any other Scala version >= 2.10.2
libraryDependencies += "org.scala-js" %%% "scalajs-dom" % "0.9.0"

libraryDependencies += "com.darkoverlordofdata" %%% "entitas" % "0.0.0-SNAPSHOT"
resolvers += "Artifactory" at "https://oss.jfrog.org/artifactory/oss-snapshot-local/"
libraryDependencies += "co.technius.scalajs-pixi" %%% "core" % "0.0.1-SNAPSHOT"
jsDependencies += "org.webjars" % "pixi.js" % "3.0.7" / "pixi.js"
jsDependencies += RuntimeDOM
skip in packageJSDependencies := false
persistLauncher in Compile := true

I'll have to reload my sbt configuration. So while it's doing that, I open another terminal window in the project root, and type the following commands :

entitas init helloworld 
entitas create -c Destroy
entitas create -c Player 
entitas create -c Position x:Float y:Float
entitas create -c Velocity x:Float y:Float
entitas create -c View sprite:co.technius.scalajs.pixi.Sprite
entitas create -s Destroy IExecuteSystem
entitas create -s Physics IExecuteSystem
entitas create -s Render IInitializeSystem IExecuteSystem

Now I have an ./entitas.json file in my project root.


    {
      "namespace": "helloworld",
      "src": "lib/src",
      "output": {
        "javascript": "web/src/helloworld/generatedExtensions.js",
        "typescript": "lib/src/generatedComponents.ts",
        "declaration": "lib/ext/helloworld.d.ts"
      },
    ...}

These defaults are for a typescript project, so I make the following changes for scala:

      "namespace": "tutorial.webapp",
      "src": "src/main/scala",
      "output": {
        "generated": "components.scala"
      },

and generate the output:

entitas generate -p scala mutable=var

This creates all of my components and system classes. Components are regenerated every time I run the tool. Systems are not overwritten.

I also need some assets.  I've downloaded Kenney's SpaceShooterRedux package from http://opengameart.org/content/space-shooter-redux

The main TutorialApp class should load the assets and start the game loop:

package tutorial.webapp

import co.technius.scalajs.pixi.Container
import co.technius.scalajs.pixi.Pixi
import co.technius.scalajs.pixi.loaders.{ResourceDictionary, Loader}
import com.darkoverlordofdata.entitas.{Systems, Pool}
import tutorial.webapp.systems._
import org.scalajs.dom.document
import org.scalajs.dom.window
import scala.scalajs.js.JSApp

object TutorialApp extends JSApp {
  lazy val width = window.innerWidth
  lazy val height = window.innerHeight
  lazy val renderer = Pixi.autoDetectRenderer(width, height)
  lazy val stage = new Container

  lazy val pool: Pool = { new Pool(Component.TotalComponents.id) }
  lazy val systems: Systems = { new Systems() }

  def main(): Unit = {
    Pixi.loader
      .add("black",   "images/black.png")
      .add("ship",    "images/ship.png")
      .load(loaded)
    document.body.appendChild(renderer.view)
    window.requestAnimationFrame(render)
  }

  lazy val loaded = (loader:Loader, res:ResourceDictionary) => {
    systems
      .add(pool.createSystem(new RenderSystem(pool)))
      .add(pool.createSystem(new PhysicsSystem(pool)))
      .add(pool.createSystem(new DestroySystem(pool)))
    systems.initialize()
  }

  lazy val render: (Double) => Unit = { time =>
    systems.execute()
    window.requestAnimationFrame(render)
  }

}


Now I just need to fill in the systems and create entities. The full version is here
https://github.com/darkoverlordofdata/invaders-scala-js

Try it  live here, at https://darkoverlordofdata.com/invaders-scala-js. Be kind, it is just started.


Wednesday, May 4, 2016

You Will Test the Way We Want You to Test

That sounds like a line from Hogan's Heroes. But really it's JUinit. I can't believe the lack of alternatives in the java-sphere. Actually, this extends to Kotlin, too. I've implemented the Entitas api in Kotlin, and it needs to be tested. And all I get is JUint? Actually, there is Spek, but it requires JUnit...

I suppose that would be OK if I were only calling my Kotlin code from Java. But I'm not, I'm calling from Kotlin, and JUnit can't test idomatic usage. Plus, btw - have I mentioned lately that I hate java? That's why I'm using Kotlin.

So far, Kotlin's been doing a good job of insulating me from java, and I'd like to keep it that way. So I've written a unit test framework before - I can write one in Kotlin. In fact, I just did, it doesn't take long.
~ here is an example of usage:

fun main(args: Array<String>) {
    var k1 = 0
    var k2 = 0
    var k3 = 0

    TestIt("this is the test suite")
    .beforeEach {
        k1 += 1
    }.afterEach {
        k2 += 1
    }.onDone {
        k3 = k1 + k2
    }.run {

        it("this is the test") {
            it.equals(1, 1)
        }

        it("this is another test") {
            it.equals("fred", 1)
        }

    }

}

And, here is the TestIt code, the thing about kotlin is that it's so easy and succinct. The entire framework fits in on file, and is about 80 lines of code.


class TestIt(name:String) {

    private var name = name
    private var beforeProc = {}
    private var afterProc = {}
    private var doneProc = {}
    private val tests:MutableList = mutableListOf()

    data class Test(val name:String, val proc:(assert) -> Unit) {}

    object assert {
        var result = false
        var actual:Any? = null
        var expected:Any? = null
        fun equals(actual:Any, expected:Any):Boolean {
            if (actual.equals(expected)) {
                this.result = true
            } else {
                this.result = false
                this.actual = actual
                this.expected = expected
            }
            return this.result
        }
    }

    fun beforeEach(proc:() -> Unit):TestIt {
        beforeProc = proc
        return this
    }

    fun afterEach(proc:() -> Unit):TestIt {
        afterProc = proc
        return this
    }

    fun onDone(proc:() -> Unit):TestIt {
        doneProc = proc
        return this
    }

    fun run(main:(func:(name:String, proc:(assert) -> Unit) -> Unit) -> Unit) {

        val fail = "\u001b[31m" /* VT100 RED */
        val pass = "\u001b[32m" /* VT100 GREEN */
        val reset = "\u001b[0m" /* VT100 RESET */
        var passed = 0
        var failed = 0

        main {name:String, proc:(assert) -> Unit ->
            val ignore  = tests.add(Test(name, proc))
        }

        println("\t$name\n---------------------------------")
        for (test in tests) {
            assert.result = true
            beforeProc()
            test.proc(assert)
            afterProc()
            if (assert.result) {
                passed++
                println("${pass}PASS${reset} <=> ${test.name}")
            } else {
                failed++
                println("${fail}FAIL${reset} <=> ${test.name}")
                println("     expected ${fail}[${reset}${assert.expected}${fail}]${reset}")
                println("       actual ${fail}[${reset}${assert.actual}${fail}]${reset}")
            }
        }
        doneProc()
        println("---------------------------------")
        println("    <====> Pass: $passed")
        println("    <====> Fail: $failed\n\n")
    }
}


Wednesday, April 20, 2016

Is Kotlin Really a Better Scala?

Disclaimer - I'm not a java coder. In fact, I hate java. Maybe that's too stong. I don't prefer java. But I want to try libGDX, and that means using the jvm. I like programming with FSharp, so I'm checking out scala, which is also based on ocaml, and kotlin, a purported 'better scala'.

So, to compare them, I ported a simple FSharp shmup game to both scala and kotlin. I found that on Android, the Kotlin version starts stuttering while the Scala version continues fast and steady for as long as I fire bullets. So, for the first round, Scala is the clear winner. Kotlin just does not have the raw performance.

Next, I upgraded each to include using Overlap2D. This is more realistic, not to mention it's the reason I'm looking into libGDX. Here, I'm using Box2D physics to move objects, and the results are not conclusive, there is not a better performing version. So let's look at the language.

Kotlin touts interop with java. But I didn't find scala to be any slouch in that area. I think their perspective must be calling Kotlin vs Scala from Java, neither of which I plan on doing.

Kotlin compiles faster? Only if you don't use SBT (scala build tool).

Kotlin's syntax is almost identical to Scala's.
It uses the keyword 'fun' rather than 'def' to define methods.
Kotlin uses 'data class' where scala calls it 'case class'
Kotlin changes 'match' to 'when' and removes some functionality.
Kotlin has no 'new' operator. New objects and function calls look the same.
Kotlin exposes synthetic members - getWidth() and setWidth() are replace with a field reference to width.
Kotlin's weak pattern matching has to be replaced with complex if statements and manual destructuring.

Those may seems like change just to be different, but they actually have a negative impact on readability. New objects dont' stand out. Properties don't match the documetation. Pattern matching is spaghetti.

It seems to me that the main benefit of Kotlin is the one stated by IntelliJ - it helps them sell their IDE. But it doen't help me write my code. I've decided to go with scala.

Wednesday, March 23, 2016

Look, Ma, No Framework! (my first Kotlin project)

I like the way that an entity component system (ecs) organizes my code. But I don't always want to use a big framework. Functional languages such as Kotlin can give me much of the benefits of ecs without the overhead.

Let's start with components:

data class Bounds(val radius: Float)
data class Expires(val value: Float)
data class Health(val current: Int, val maximum: Int)
data class Position(val x: Float, val y: Float)
data class Resource(val path: String)
data class Scale(val x: Float, val y:Float)
data class Size(val w: Float, val h:Float)
data class Sprite(val texture: TextureRegion)
data class Tween(val min:Float, val max:Float, val speed:Float, val repeat:Boolean, val active:Boolean)
data class Velocity(val x: Float, val y: Float)

That's it. TextureRegion is defined in LibGDX, and I defined a couple of enums - EntityType and SpriteLayer (https://github.com/darkoverlordofdata/shmupwarz-libgdx-kotlin) so that we can define our entity:




data class Entity(
    val id:         Int,           /* Unique id */
    val name:       String,        /* Display name */
    val active:     Boolean,       /* In use? */
    val entityType: EntityType,    /* EntityType Enum */
    val layer:      SpriteLayer,   /* Display Layer Enum 
         /* C O M P O N E N T S * */
    val bounds:     Bounds?,       /* Radius */
    val expires:    Expires?,      /* Entity expiration timer */
    val health:     Health?,       /* Health counter */
    val position:   Position?,     /* Screen x,y */
    val resource:   Resource?,     /* Sprite asset */
    val scale:      Scale?,        /* Size scale */
    val size:       Size?,         /* Display size */
    val sprite:     Sprite?,       /* Texture */
    val tween:      Tween?,        /* Tweener */
    val velocity:   Velocity?    /* Speed x,y */
)
var uniqueId: Int = 0

fun createEntity(entityType: EntityType, layer : SpriteLayer, name : String):Entity {
    uniqueId += 1
    return Entity(uniqueId, name, false, entityType, layer, null, null, null, null, null, null, null, null, null, null)
}



Components are initialized to null. I use a factory functions to create a full entities, such as Player:


fun createPlayer(entity:Entity, width: Int, height: Int):Entity {
    val name = entity.name
    val path = "images/$name.png"
    return entity.copy(
        active    = true,
        bounds    = Bounds(43.0f),
        health    = Health(100, 100),
        resource  = Resource(path),
        position  = Position(width.toFloat()/2.0f, 100.0f),
        sprite    = Sprite(TextureRegion(Texture(Gdx.files.internal(path)))),
        velocity  = Velocity(0.0f, 0.0f)
    )
}


Systems use pattern matching to determine which entities they act upon.


fun movementSystem(entity: Entity, delta: Float): Entity =
    when {
        (entity.position != null
        && entity.velocity != null) -> {

            val position = entity.position
            val velocity = entity.velocity
            val x = position.x + velocity.x * delta
            val y = position.y + velocity.y * delta
            entity.copy(position = Position(x, y))
        }
        else -> entity
    }


From a functional viewpoint, Kotlin's implementation of pattern matching seems a bit lame. Better destucturing would help. I should be able to say:

val (position, velocity) = entity

But I can't do that either, Kotlin doesn't support adhoc destructuring. Instead I would need to use

val (id, name, active, entityType, layer, bounds, expires, health, position, resource, scale, size, sprite, tween, velocity) = entity

Who thought that was a good idea? You can see why I'm not using it. Oh well. The thing to remember is that pattern matching does work. I think that compared to all of the good features of Kotlin, this is something I can live with, and hope that JetBrains will improve it - after all, this is only version 1.0.1!

Those are the pieces. How do they fit together? There is a joke-
Q How many Haskell programmers does it take to change a lightbulb
A None. They build a new house around the new lightbulb.

Well, that's what we're going to do, only in Kotlin. Notice that all of the fields of our components and entities are immutable (val). We cannot change them, only create new ones. Notice in the movement system, where the function returns the original entity or with entity.copy(position=Position(x,y)).

First, we create an array of all our entities, active or not:

fun createLevel(): List = arrayListOf(
    createEntity(EntityType.Enemy, SpriteLayer.ENEMY1, "enemy1"),
    ...
    createEntity(EntityType.Bullet, SpriteLayer.BULLET, "bullet"),
    createEntity(EntityType.Bullet, SpriteLayer.BULLET, "bullet"),
    createEntity(EntityType.Bullet, SpriteLayer.BULLET, "bullet"),
    ...
    createEntity(EntityType.Player, SpriteLayer.PLAYER, "fighter"),
    createEntity(EntityType.Explosion, SpriteLayer.EXPLOSION, "explosion"),
    ...
    )


Then our main update loop will create a pipeline out of our systems. This is where it all happens. Once each frame, we recalculate our game state, and then display it:


fun update(delta: Float, mainBatch: SpriteBatch) {
    level = collisionSystem(
        enemySpawningSystem(level, delta)
            .map {inputSystem(it, delta) }
            .map {entitySystem(it, delta) }
            .map {movementSystem(it, delta) }
            .map {tweenSystem(it, delta) }
            .map {expiringSystem(it, delta) }
            .map {removeOffscreenSystem(it, delta) }, delta)

    level.filter { it.active }
        .sortedBy { it.layer }
        .map {spriteRenderSystem(it, mainBatch)}

}


You might be tempted to just add your active entities as needed and remove them when you are done. I tried that. After about a minute, the game will grind to a halt due to memory fragmentation.

I've uploaded the full working project in AndroidStudio to github. It should run on both desktop and android. I generated the ios version also, but I am unable to test it. 

Friday, March 18, 2016

Kotlin and FSharp and Scala! Ocaml!

After re-writing ShmupWars using idiomatic FSharp, I had my ML epiphany. Immutable is good. But FSharp syntax seems a bit rough, and it's hard to be original in Unity, so I'd like to find an alternative. Another strong cross platform game environment is LibGDX, and the jvm has functional languages. But I have limited experience using the jvm, and find the whole java ecosystem intimidating and confusing.

So, first up Scala 2.9. Scala is easy to install, and there is a template for LibGDX, it's all here: http://raintomorrow.cc/post/70000607238/develop-games-in-scala-with-libgdx-getting

Both Scala and FSharp were influenced by OCAML. I can do just about the same things in Scala as in FSharp, but with a cleaner syntax. Moving from FSharp to Scala is straightforward, and in about 3 days I had a working example of ShmupWars. I found that sbt, the Scala Build Tool, does a good job of hiding the jvm from me, and I appreciate that. There are Atom plugins for both Scala and SBT, so I can avoid using a big bloated java ide. SBT also allows quick iterative compiles. I like to test continually while I code, and SBT keeps up with me.

Second, Kotlin 1.0.0. Speaking of big bloated java ide's, Kotlin installs with IntelliJ Community Edition 15. I used Obviam's template http://obviam.net/index.php/libgdx-and-kotlin/ to get started. I couldn't get it to compile from the ide. LibGDX includes command line build scripts, so I installed the kotlinc command line compiler.  IntelliJ keeps trying to reconfigure kotlin, and when it succeeds, the compile script is then broken. So, I'm using Atom with the kotlin-language plugin. Compile iteration is slow. I probably don't fit Kotlin's demographic. They are targeting existing java progamers who would know the secret to fix this snafu. Did I mention that I hate java?

Some Kotlin features I ran into:
* Destructured assignment won't compile unless I list all properties. That's not very convenient if I want just 2 out of 11 properties.
* Pattern matching is 2nd class. No tuples, no destructuring. It's like the Visual Basic Select statement.
* Rather than use an Option wrapper, fields are marked as nullable, and then must checked for null before each use.
* No native immutable linked list.

In the end, Scala is a more mature ML language. Coverting ML idiom from FSharp to Scala was like falling off a log. I was almost disappointed; I thought this would be more of a challenge. I'm sure I just haven't gotten to that part yet.

Kotlin, however does not have first class support for ML idiom. Nor does it promise to - Kotlin has it's own idiom. My original FSharp version uses OOP idiom, would that be a better candidate for Kotlin? I'll give it a try.



Friday, March 11, 2016

Just Say No To Microsoft

I never thought I'd hear myself say that. After all, as some of you know, I worked there for almost 20 years. But they've crossed the line.

Today, I woke up, and my Windows7 machine had upgraded to Windows10. No warnings. No permission. It just did it. And yes, my files are still there, but none of my dev tools work. While I love Linux, I've always tried to make sure my projects work on Microsoft products as well. No more - Microsoft has seen to that. No CygWin, no Babun, no MinGW. Even stuff made for Microsoft - Unity3d, Cocos2D - none of it is working after the upgrade to Windows 10.

It would be different, if I has initiated the upgrade. If I had been prepared. If I had reserved the time to deal with it. This isn't about Open Source vs Closed Source. This is about customer service, and Microsoft obviously doesn't give 2 scraps about their customers needs and wants.

I have a solution. Already had it. I'm selecting a new distro now - I've been meaning to try Fedora. Now is the time. No dual boot needed. There is nothing I need Microsoft for.

I think the next computer I purchase will probably be an OS/X machine.  Thank you Microsoft, for kicking me out of your nest - I can learn to fly without you!

Tuesday, March 8, 2016

A Functional ECS

I recently changed my distro from Mint Cinnamon to ElementaryOS. My research had reached the conclusion that the issues I was having with Unity3D were due to old kernal. I'm happy to report that it's a success - I can now access the Unity Store. And with the latest 5.3.3 version, some breaking changes have been resolved. All of this allows me to get back to FSharp.

And so, I've finished up my very basic port of Entitas to FSharp. While converting the demo game, ShmupWarz, I started wondering if my systems could just be functions. And why not use FSharp's built in pattern matching instead of calling a Match api? Could I juse express my ECS directly in FSharp?

So, for example, I could write the movement system like this:

let MovementSystem (delta:float32) entity =

    match entity.Velocity, entity.Position with

    | Some(velocity), Some(position) ->
        let x = position.X + velocity.X * delta
        let y = position.Y + velocity.Y * delta
        { entity with Position = Vector2(float32 x, float32 y)}

    | _ -> entity

Where the oop version looks like this:

type MovementSystem(world:World) =

    let group = world.GetGroup(Matcher.AllOf(Matcher.Position, Matcher.Velocity))

    interface IExecuteSystem with
        member this.Execute() =

            let delta = Time.deltaTime/100.0f

            for e in (group.GetEntities()) do
                e.position.x <- e.position.x + (e.velocity.x * delta)
                e.position.y <- e.position.y + (e.velocity.y * delta)
                e.position.z <- e.position.z + (e.velocity.z * delta)



While reading this post https://bruinbrown.wordpress.com/2013/10/06/making-a-platformer-in-f-with-monogame/ I noticed that the author was using a pattern very similar to en ECS. So I played with it a bit, tweaked the naming convention, and got it working.

I went ahead and created a full game for a poc. You can see it at https://github.com/darkoverlordofdata/mono-fsharp-shmupwarz

Performance seems just as good, although I'm comparing MonoGame to Unity3D. So my next step is to try to integrate this technique with Unity.

Wednesday, February 24, 2016

You Ain't Seen Nothing Like the Mighty Nim

Not if you are used to class based oop. Instead, Nim is type based and uses multi-methods. This takes some getting used to. So after a couple of weeks, what are my impressions?

My last attempt at creating  a native game engine using Vala was disappointing - great language, slow run time. I actually considered Nim prior to trying Vala, but I was put off by the 'partial style insensitivity' - this means that 'setValue', 'set_value', and 'setvalue' all refer to the same identifier. I don't see any good reasons for that, just another way to get bugs. Between this and the original name Nimrod I was convinced that it was done to prevent wide spread acceptance and keep Nim as a hobby language.

But this isn't a rant. I can live with 'partial insensitivity', and the name was shortened. Nim has a pragmatic style. Nim doesn't have classes, so I don't end up with a complex class hierarchy - this is a good thing. I can use inheritance where it is needed, and composition is easy. With multi methods, I can define a 'class' in more than one file - this is handy if I want to extend an existing class like you can in CSharp. Compared to languages like Java which provide a 'place for everything, everything in it's place', Nim gives you a lot more freedom in organizing your code and files. And definitely requires less boilerplate. Of course, since there is not much structure, you'll need to provide your own.

After 2 weeks of coding, my proof of concept is running on both Linux and Windows7. Not only that, the performance is better than I anticipated, even exciting. I've done no performance tuning. My entity object uses an array for components, but all other arrays are just sequences, tables and queues - all plain Nim dynamic structures. When I play my test demo, ShmupWarz, I can shoot bullets continuously for minutes, blasting enemy ships, and only once did I notice my frame rate drop below 60 fps.

I'm sold. I'm continuing on with my project, using Nim

Tuesday, February 9, 2016

Six Weeks with Vala

Update: https://blog.darkoverlordofdata.com/2016/12/vala-revisited.html

One concept discussed in functional progamming is 'Reasonablness', which focuses alot on how side effects make it hard to predict what your code is actually doing. In case you're not familiar, here is a great post on the topic http://fsharpforfunandprofit.com/posts/is-your-language-unreasonable/

One thing I like about that post is that the author points out that by their nature, we cannot expect much of this 'reasonableness' from your typical oop language. But I think Vala fares worse on than most.

Part of it is the ecosystem - it's hard to reasom without information. Documentation is sparse, conflicting, or just missing. Granted that all technology products have this issue to some degree, but it's particularily bad in the case of Vala. I can't get the Gnome toolset working because I'm not using a gnome desktop. If this were Microsoft or Oracle, we'd call it vendor lockin. I'll give Gnome the benefit of doubt and chalk it up to tunnel-vision or not-invented-here-syndrome. But the result is that I am using Atom and command line make, and scratching my head alot while I seach the web for clues.

But that can be fixed. The real issue I belive is the dependancy on GObject. Remember, GObject was created to compete with Microsoft COM. COM was the object system that backed VB6. It was also the cause of much of VB6 unreasonable behavior. DotNET was created to replace COM. Microsoft wanted a 'pure' object system that would met the requirements of any language. Vala follows the VB6 model of shoe horning the langage to fit the existing object system. And this leaks through the abstracton and results in arbitrary constraints on the language. I continually get compilation errors - things like interfaces and properties sometimes don't compile until I base the class on GObject, resulting in other side effects, requiring more than one ground up redesign. And I still find myself surprised by the results, and not in a good way.

So, what was I actually able to accomplish in those six weeks of vala? First, I discovered Genie, Vala's whitespaces language. This is a great alternative to Vala's curly braces, and I credit it with speeding up the process. It seems to be designed to provide intelligent defaults, and the overall effect is much less typing, and an easy to read grammer. It is missing a couple of features present in Vala - namely lambda's and multidimemsional arrays - though I can declare them in Vala and then comsume them from Geanie. I will miss Geanie.

So, after six weeks, I have a prototype shmup game using SLD2 and Bosco.ECS (My ecs inspred by Entitas). It's a little dissapointing. Performance is not better than Unity. And with Unity, 6 weeks later I had a full game enviromment, with menu, scores, leaderboard, running on Windows, Linux and Android. I still can't get a Vala game to compile on or for Windows 7. Nor have I yet addressed sound or a UI. In case you're interested in Vala, my project is https://github.com/darkoverlordofdata/shmupwarz-vala

I really like Vala and wanted it to work, but its mostly frustrating. And I found that I Genie, the 'other' language compiled by valac, is vastly underrated. Genie could be the next big thing, if Gnome lets it. But I'm moving on to something more fruitful.


Friday, January 29, 2016

Avoiding Unsafe Code in Vala

CSharp uses the 'unsafe' keyword to mark sections of code that ignore type safety. Vala doesn't have this,  but if it did, if would apply to variable length argument lists.

What's wrong with variable length arguments? The tutorial shows a great use case:

actor.animate (AnimationMode.EASE_OUT_BOUNCE, 3000, x: 100.0, y: 200.0, rotation_angle_z: 500.0, opacity: 0);

Coming from a scripting world, this looks like a friendly idiom. In fact, this example has a bug that causes unpredictable results. See that last parameter 'opacity: 0'? That '0' should be a '0.0', because the receiving method:

double val = l.arg();

is requesting a double. Now the stack is out of alignment. If you are lucky, your program will crash with a segmentation fault. Lucky? Yes, because any other result is unpredictable. In my case, the values of all 4 variables became changed to 0, and the program continued running. But the other part of the example seemed to work correctly:

actor.animate (AnimationMode.EASE_OUT_BOUNCE, 3000, "x", 100.0, "y", 200.0, "rotation-angle-z", 500.0, "opacity", 0);

This was giving me the correct results. So I assumed that the problem was with the alternate syntax, which uses a colon. Then I swapped the two, expecting the error to move with it. But it didn't. Now the alternate syntax was working. That should have been my first clue that there was a memory corruption. Instead, seeing that I was using an older version of Vala libraries, I upgraded to the newest. And the problem went away. I thought I'd fixed it. But it wasn't fixed.

I was wrong. Later the next day, it started doing it again. Sorry stackoverflow, you were right. It's been a long time since I've coded using c.

That's when I noticed the '0'. I changed that to '0.0' and it started working. This is how a memory bug presents in C - it wants you to pull your hair out. My advice - don't use variable length argument lists. This 'wolf in sheeps clothing' masquerades as a friendly looking idiom but it's not.

Wednesday, January 27, 2016

What's up with Vala?

The vala landscape is strewn with abandonware. The Gnomite wiki has more dead links than a Gnecrominicon. Vala 1.0 , the next version afer 0.4 never materialized, and the roadmap this link refers to is now itself just another dead link.

But something is happening - Geary is built with Vala, as is the ElementaryOS desktop that I currently love. Version 0.30 was release last fall (still no version 1.0). So there must be something to this. Right?

Maybe - if there were some viable tooling. I spent about a week trying to get various dev tools from Gnome to work. Only 1 would actually install and run - Valama - and it is very unfinished. There is always Geany, but that is so last millenia.

So I've decided that it's best for me to ignore all of that Gnomite stuff. I'm starting over using Atom. I've installed the language-vala plugin. I'm using my own simple make script, and run it using atom-buil plugin. I modified autocomplete-clang to include vala - (You're welcome to try it, but I'll warn you that it, too, is unfinished. But then, the bar is pretty low :)) - And I'm just now starting to get productive.

Now I'm trying to find a unit test that actually works. Honestly - is there any other language whose ecosystem is in such shabby state? It is said that there are 2 kinds of computer languages. The ones that everyone complains about, and the ones that no one uses. Maybe there just haven't been enough complaints about  Vala.

Wednesday, January 20, 2016

Order Out Of Chaos - Finding My Desktop in Elementary OS / Freya

I'm becoming a fan of the Elementary design aesthetic. At first I fought it. I replaced Plank with Docky, only to realize that this added no functionality, just distractions. So I've gone back to the minimalistic, focused desktop that Freya delivers.

In the end, I have only 1 thing I need to change - I need to be able to put files on my desktop. Some 3rd party software will bork without this - try to 'Add to Desktop' in Chrome or create a desktop shortcut in any IntelliJ IDE.

So, to start with, I just created a ~/Desktop folder. This solves the issues with applications that expect a Desktop folder. But it's still not convenient. I found this excellent article https://elementaryforums.com/index.php?threads/enable-desktop-icons-and-right-click-in-elementaryos-freya.198/ which gives me back my standard desktop.

But now I have a new problem. When I click on my Home icon, is opens in nautilis, not the native pantheon-files. I end up using 2 different file managers at the same time, and this is the road back to the typical Linux chaos that I'm trying to avoid.

So, first, lets go back into dconf-editor and roll back one change. Under org->gnome->nautilis->desktop and set the home-icon-visible, network-icon-visible, and trash-icon-visible properties off.

Next, open scratch, and enter:


#!/usr/bin/env xdg-open
[Desktop Entry]
Version=1.0
Terminal=false
Type=Application
Name=Bruce
Exec=pantheon-files /home/bruce
Icon=/usr/share/icons/elementary/places/64/user-home.svg

Instead of 'bruce', use your own account name. Save it as ~/Desktop/Home.desktop
Make it executable:

$ chmod +x ~/Desktop/Home.desktop

Now we have our own custom Home icon. When we click on it, it will open our home folder in the native file manager. We have restored our working desktop and minimized the chaos.

Tuesday, January 19, 2016

Using Gmail Inbox Tabs in Geary

I'm one of those odd people that likes the Gmail inbox tabs. I don't even customize them, they work just great for me out of the box. Mainly, I just don't like to think about my email. Which is why I'm liking Geary. But it doesn't see my Gmail tabs. Actually, any client will have this problem, not just Geary. Gmail doesn't export the property. But there is a way around this by using filters.

I'm not going to show you how to create filters - Google has better help than I can give you - just search for 'gmail filters'  https://support.google.com/mail/answer/3055016?hl=en.

So, a filter is basically a saved search, and we can set some rules. By selecting the tab as our search criteria, we can create a filter based on a tab. To create a label for the Forums tab, I set these options:



WARNING: This is destructive. When this is working, your tabs will always be empty. But you'll need to leave them active for this to continue working. So Gmail won't look as pretty, cause you'll always have those empty tabs sitting there.

Do the same for each tab. When you are done, and you click on the tab, it will now be empty. The contents will be auto archived and labeled with the same name as the tab. And now, you can view them as Labels in Geary.


Sunday, January 10, 2016

Using Coffee Script for Gnome Desktop Scripting

The Cinnamon desktop uses javascript for scripting applets and desklets. Specifially, it uses an older version of spidermonkey. I'm not sure which, but it includes only 2 ES6 features - let and const. No other new, more useful ES6 features seem to work. This is troubling, since the semantics for let and const have changed - only slightly, but enough to break some plugins.

So basically, desktop scripting in Gnome, and therefore Cinnamon, is stuck with a deprecated version of javascript. Since most of my javascript is for the browser, I don't really want to learn how to use it the wrong way. But we know a fix for this - Coffee Script!

Applet = imports.ui.applet
GLib = imports.gi.GLib
Util = imports.misc.util
Gettext = imports.gettext.domain('cinnamon-applets')
_ = Gettext.gettext

class MyApplet
    __proto__: base = Applet.IconApplet.prototype
  
    name: 'Gracie'

    constructor:(orientation)->
        base._init.call(this, orientation);
        try 
            @set_applet_icon_name("gnome-info")
            @set_applet_tooltip(_("Say Hello, #{@name}"))
            return
        
        catch e
            global.logError(e)
            return
            
    on_applet_clicked:(evtdata) =>
        notification = "notify-send \"Hello #{@name}\"  -a TEST -t 10 -u low"
        Util.spawnCommandLine(notification)
        return
        

main = (metadata, orientation)-> new MyApplet(orientation)


Note there is now no need for Lang.Class and Lang.Bind. Insted of coffee's extend, I've set the __proto__ property. You can still use extends for your own class hierarchy, but for a GObject based hierarchy, you'll need to use __proto__.

Compile with the bare flag:

$ coffee -bc applet.coffee

And breath some new life into that stale interface!