Salomonsson.se
Sep 24 2019

Starting out with C++ and SDL

I’ve spent some time trying to port my text engine to linux and mac, with the following results:

  • On Linux (also valid on Mac) I cannot detect keyUp. “Fixed” it by gimping the game a bit on those platforms…
  • On OSX I cannot set colors correctly! The exact same sourcecode that works on Linux turns into dark blue text on semi dark blue background. Unreadalbe.

So I have abandoned that path for now. Instead started looking into the SDL library instead… and it is really smooth to work with! (at least on Linux). I have currently managed to get it to work on both Linux and Windows!

I can even work with this code on my old, very beloved, Asus EEE 1005PE netbook. A ten year old 10.1" computer that freezes if it tries to open any random webpage (Gmail? Just forget about even trying), however it has a whooping 250gb harddrive, a keyboard like a dream and a battery that lasts for an eternity. Writing C++ code in VIM (in a terminal window) works like a charm… like in the 70’s ;-) Compilation is around 1-2 seconds, which is not only acceptable, but also faster than Unity compiles on my powerhorse workstation.

This looks very promising for the upcoming Ludum Dare competition!

Sep 16 2018

Fight The System

This evening I managed to render tiles from an old Ripple Tilesheet to a runtime generated mesh, using a custom shader to map the tile values.

It does not scroll yet, but supports rescaling the viewport (although only to even pixels in height).

Apr 19 2018

Cpp text console application, part 3

Now writing to bufferes and writing those to the output. Writer class that prints text character by character and interprets simple commands (right now only newlines and changes in speed and pauses). Two days left to Ludum Dare.

Tutorial that describes pretty much exactly what I’m playing around with

http://cecilsunkure.blogspot.se/2011/11/windows-console-game-writing-to-console.html

Good to keep as reference!

https://github.com/Tommislav/cppTextEngine/commit/1110c6c53b7abb51c5c5a34364d7f450046d8c35

Mar 15 2018

Cpp text console application, part 2

More fun with your terminal:

  • Colors
  • Write character at position

So far I’m surprised by the speed (the delay you see is actually me pausing the thread)! The last time I tried stuff like this directly in the terminal it flickered like hell. But that time I was using conio.h, this time I write stuff myself using only windows.h.

I’m not using buffers yet, only moving cursor and writing to that position. Guess writing to buffers will make it even faster. But thats for another day…

Feb 11 2018

Cpp text console application

Ok, for the first time in a long while I got two hours that I could spend on programming! Need to refresh my c++ knowledge quite a bit (the metro siberia part below uses OpenFrameworks to do all the heavy work for me). But in these two hours I still managed to:

  1. Create a makefile (my very first one)
  2. Code a small console application in VIM (and compile it using :make)
  3. Have the program print out text letter-by-letter
  4. Read the content of the text from an external text file
  5. Store config flags, such as speed or pause in the text document

Not very exciting, and now the time is up. Time to hit the bed. Felt good to do some programming anyways.

Sep 14 2017

Metro Siberia - part 2

Matrices!

Small progress. Started porting over my matrix implementations from haxe. At the moment it actually looks more like C-code than C++.

Aug 24 2017

Metro Siberia - part 1

intersecting triangles

Started a re-make of my old game Metro Siberia in C++. This is the big plan:

  • Custom rendering methods in it’s own header file
  • Game code is platform independent
  • To start with, only draw lines and simple fills
  • Currently using OpenFrameworks as “backend”, because it is almost zero hassle to get working
  • Plan is to change from OpenFrameworks later on… but we’ll see. At least the structure will allow it.

Currently nothing more than a window that renders the picture above.

Mar 30 2017

Ripple Haxe Port, 1

Finally started on something I’ve been wanting to do for a very long time. A serious try to port Ripple Dot Zero to haxe, so I can compile it to c++, run it on Windows with full gamepad support and full, smooth framerate!

This is after a few hours of experimenting with openFLs new Tilemap API, and one day of porting (well… mostly rewriting) the tile rendering part of my old engine. Now it runs on a solid 60 fps after compiling to C++, even though there are tons of optimizations to be done.

Right now it renders all static tiles (no animated tiles, and no sprites) from an existing level, and lets the camera move in a huge sine wave over it.

A very promising first step!

Jan 15 2017

Kingsmountain, Part 2

“Don’t worry about the dog, he’s very nice and only wants to play!”

Dec 15 2016

Kingsmountain, Part 1

A while back I found all the raw material for a game me and Simon were working on a long time ago (actually before both Ripple and Metro Siberia, so somewhere around 2006 – whoa, thats TEN YEARS ago!!) called Kingsmountain, and I’ve been playing with the idea to try and recreate one of the minigames.

My first attempt was with Haxe, and a 2d skeletal animation tool called Spine. However, the Haxe runtime implementation for Spine was buggy as hell, and I spent many hours trying to animate the running little dude, but finally the bugs made me give up.

Instead I restarted the project in Unity, and here you can see the result after just about 2-3 hours.

Oct 31 2016

Math - Triangle Rasterizer

Warning: lengthy post ahead! But lots of really cool stuff!

Been a while since I posted now. There are a couple of reasons for this:

  • First of all, I decided to write my own software triangle rasterizer (I’ll get to explain what that is in a bit)
  • It’s a lot of math involved. And a lot of diagrams with pen and paper. I has spent several hours to get it to its current working state
  • My parental leave is getting close to the end. My kids are now almost 16 months and require a lot of attention! Have not gotten any computer time at all for the past three weeks

But now I finally have something to show, so lets show it!

READ MORE >>
Oct 05 2016

Math - Backface Culling And Directional Lighting

Up to this point I’ve only drawn wireframes. To create colored 3d objects I need two things. Backface Culling and Z Sorting. Time to start with the first: Backface Culling.

  • First of all I changed to using triangles instead of just points/lines.
  • Then I calculate the cross product on two of the sides to get a normal vector.
  • Compare the normal with your viewing vector (a line from the camera to the middle of the triangle) to see if the triangle is facing towards you or away from you
  • Don’t draw if the triangle is facing away from you

Now you no longer see any lines “behind” the cubes. I also draw the normal vectors for each triangle.

Spinning cube with normals

From this point I just filled the triangles with a single color.

Red spinning cub

With the normal vectors already calculated on each triangle, I tried to implement some directional lighting as well. It went much smoother than I would ever have thought.

Yay, lighting!

Pretty interesting! I made a spinning cube with lighting almost exactly ten years ago. However I did not understand much of it, and going back to that tutorial it seems to be full of weird tricks. This time however I did it all by myself =D.

Next up is Z-Sorting so we can have more than just one cube at once

Sep 22 2016

Math - The Camera Matrix

We can move around in our 3d world!

I’m doing serious progress! Today I managed to get the camera matrix working!

The coolest thing about this is that just a few months ago I would have thought that this was too much math for me to ever be able to understand! Remember that I have written everything from scratch - except for the line render code, which is basically just a lineTo(x,y) function. Even the matrix implementation.

There’s no frustum culling in there yet (except for any z-values behind the camera) so pretty much everything gets drawn all the time.

Resources for camera matrix: Camera View Transform Matrix Faster Matrix Inversions

Sep 15 2016

Math - 3D and Perspective

In our last blog post we used matrices to translate, rotate and scale vectors, but so far only in 2D.

Transforming vectors in 3D is pretty much the same thing. The only difference is that we cannot directly plot those vectors out on the screen since each vector will come in a triplet of {x,y,z} and the screen only consists of {x,y}.

We could just ignore the Z-coordinate, but that would look weird!

No Perspective

To get it to look right we need to apply perspective. Perspective means that an object that is further away from our eye will appear smaller than an object that is closer!

Perspective

Applying perspective to 3D-points is something I’ve been able to do for a long time, but now we’re working with matrices, and of course there is something called a perspective projection matrix!

Another great resource I found on the subject is scratchpixel.com, here on Perspective Matrix Projection.

In the past when I’ve been playing with 3D, I’ve used trigonometry for all transformations. It works, but has several drawbacks!

  • More computation heavy! Needs to do Sine and Cosine lookup for each point!
  • Much more complex to nest parent/child-relationship!!
  • Viewing from a camera… I don’t think so!
3D with trigonometry (and fancy blur), made 2006

Using matrices is superior by far. The image below is just a couple of matrices combined, using vectors forming a cube and a pyramid. This would not have been possible using my old 2006 methods!

3D with matrices

If you want to read up on this too, then check out:

Math for game developers youtube channel And http://www.scratchapixel.com/index.php?nocategory

Sep 04 2016

Math - Matrices and Vectors

After my Shader Week I decided to try to re-wake the math part of my brain that feels like it has numbed of a bit in recent years of Android app development. I have actually been doing this for several weeks now, but have not had anything to show until now.

So I started reading up on Linear Algebra, and is currently writing my own custom implementation of Vector and Matrix-classes from scratch in Haxe. And it’s super interesting!

Rotation Matrix

If you have a 2x2 matrix you can put an up-vector in the first column and a right-vector in the second column. When multiplying a vector with this rotation-matrix it will transform the vector into that coordinate space! If you rotate the up and right-vector clockwise each frame you get a rotation matrix!

Matrix rotation

In this first image I multiply four 2d-vectors through a rotation matrix. The unmodified vectors are shown to the left.


TRS Matrix (Translate Rotate Scale)

Another interesting thing with matrices is that you can combine them! If you have one matrix for translation, one for rotation and one matrix for scaling, you can get a single matrix containing all those values by multiplying them! Just remember to multiply them in the correct order!

Translate, Rotate, Scale
In this image I changed the four vectors to form a square instead of that cross. Then just multiplying each vector through my single TRS-matrix.


Parenting

If you have a TRS-matrix named M1, and another TRS-matrix named M2, you can move the transformation of M2 into the local coordinate space of M1 by multiplying M2 with M1! By doing this M2 will be “parented” to M1! It’s so simple!

Parenting

The big square is rotating and the second, smaller square is parented to the big one, therefore inheriting its rotation. Same with the third square (it inherits both the rotation and scale of it’s parent before applying its own rotation).

The vectors (points) for all three squares are just a single unmodified array. All the transformations are done though a single matrix multiplication (for each point).


I will keep working through the math, so expect more posts on this. In the mean time, these two youtube playlists are really great resources:

Math for game developers Khan academy: Linear Algebra