Why you should never cast floats to ints

So a few weeks ago I wrote some code that would align my text in a rectangle. The alignment would be either top left, top center, top right, middle left, etc… After finishing that up, I noticed that my text looks bad and I figured that it was due to floating point rounding errors. This happens when you set a sprite position to something like

Vector2 position = Vector2(100.5f, 50.2f);

The problem here is that the smallest controllable element of a picture represented on the screen is a single pixel, and what you are trying to do is set your image, sprite, text or whatever to one hundered pixels and a half. There is no such thing as half a pixel. So your graphics card tries to correct that and in doing so you get artifacts like these



Notice how there are lines in between each sprite, that happens because the sprite position is not exact. The sprite position is not set to Vector2(100, 50). It is set to something in between like Vector2(100.5f, 50.5f) pixel. That causes artifacts like the lines you see above. So, how do you solve this? Either cast your floating point positions to ints then back to floats or use a function like Round or Floor to round your numbers up or down to whole numbers.


So let’s take a look at my text

As you can see, the text above is a lot cleaner than the text below. The text below looks like it has artifacts (which it does). Notice the letters “u”, “F”, “m”, “i”, and “e”. They all look bad. That is because these letters position is not exact. So how did I fix that? I cast my floats into ints, did my division calculations to align the text inside an imaginary rectangle and I cast those ints back to floats.

The day I wrote that code, I knew it was temporary and I would change it later on. I did not like the fact that I had to cast my numbers not once, but twice. At the time I didn’t think it had any impact on my engine performance, but oh boy was I wrong.

So a few days ago I was going through my YouTube subscription box and I found a video from CppCon about how unsigned integers are much slower than signed integers. After hours of research, I came across this article. Why you should never cast a Floats to Ints. After reading that I remembered the block I wrote that would cast my text position from float to an int back to float again. Here it is.


Math::Vector2 Text::GetTextAlignmentOffset(TextAlignment textAlignment)
    Vector2 textSize = Vector2((float)rectangle.width, (float)rectangle.height);
    Vector2 scissorRectangleSize = Vector2((float)scissorRectangle.width, (float)scissorRectangle.height);
    return Vector2((float)((int)(scissorRectangleSize.Center().x - textSize.Center().x)), (float)((int)(scissorRectangleSize.Center().y - textSize.Center().y)));

This horribly looking code made me want to test what the above article said about how casting floats to ints is bad. So here is what I did. I created three tests like so.

int main()
    BF::System::Timer t;
    BF::Math::Rectangle rectangle(10, 10, 100, 200);
    BF::Math::Rectangle scissorRectangle(0, 0, 200, 300);
    for (size_t i = 0; i < 10000000; i++)
    	Vector2 textSize = Vector2((float)rectangle.width, (float)rectangle.height);
    	Vector2 scissorRectangleSize = Vector2((float)scissorRectangle.width, (float)scissorRectangle.height);
    	Vector2 pos1 = Vector2((float)((int)(scissorRectangleSize.x - textSize.x)), (float)scissorRectangle.y);
    std::cout << t.GetElapsedTimeInMilliseconds() << std::endl;

    for (size_t i = 0; i < 10000000; i++)
    	int p = scissorRectangle.width - rectangle.width;
    	Vector2 pos2 = Vector2((float)p, (float)scissorRectangle.height);
    std::cout << t.GetElapsedTimeInMilliseconds() << std::endl;

    for (size_t i = 0; i < 10000000; i++)
    	Vector2i pos3 = Vector2i(scissorRectangle.width - rectangle.width, scissorRectangle.height);
    std::cout << t.GetElapsedTimeInMilliseconds() << std::endl;
    return  0;

The first test casts “rectangle.width” and “rectangle.height” from integers to floats and stores them inside “textSize”. The same thing is done to “scissorRectangle” and it is stored inside “scissorRectangleSize “. After that, I subtract “scissorRectangleSize.x” from “textSize.x” and cast them from float to int and back to float again. Running this ten million times took 86.4108 milliseconds. I did 7 total casts in this test.

The second test I only did two casts. Running that also ten million times took 25.1934 milliseconds.

In the third and last test, I’ve done a total of zero casts since Vector2i is a vector with two ints instead of two floats for the x and y. Running this ten million times took 25.2048 milliseconds which is around the same amount of time the second test took.


Keep in mind all these tests were done on release build on x64.

So, there you have it. Doing this many casts in three lines of code is very bad.

[Blue Flame Engine] Adding C# scripting support

Alright, so I started thinking of adding scripting support to my engine. There is a laundry list of things that I feel like should be done before adding scripting support, but I feel like adding scripting to the engine at this stage will motivate me to finish up some of the things that I should have finished a long time ago. Like the editor -.-

C# has been extremely popular as a scripting language for a lot of game engines like Godot, Unity, Unreal, Frostbite and more. Personally, I love C# as much as C++. So, C# will be the first official and maybe the only scripting language to be added to the engine.

So you might wonder, how will this be done. Well, this is exactly what I asked myself at the beginning of the day. A couple of hours of research later, it turns out that I could use C++/CLI to convert my C++ unmanaged code to C++ managed code and use it in C#. This process will be tedious if I do it on my own. I have a little over 10,000 lines of code in my engine thus far and I don’t feel like converting every single line to managed C++. That would drive me crazy. So I’m thinking of using or creating a tool to do that for me.


Well, wish me luck. I’m going to need it.

[Blue Flame Engine] Node based system

Alright, it’s has been a while since I posted anything here. But here it goes.


For the past couple of weeks, I have been thinking of changing the engine architecture. Instead of creating a class for each object in the game, (for example, to add a player in the game you would create a player class and call it inside the scene) now you would add a node to a scene. A node can be anything from a Camera, Sprite, Mesh or a Light node, etc… Each node will be automatically initialized, loaded, updated and drawn in the scene without you the programmer having to go and call each function for each class.

Each node can have n number of nodes as child nodes.

So basically it will kind of follow the same architecture as Godot, Unity, Oger3D engines. Eventually, when I add script support, you will also be able to add a script node.


I haven’t fully settled on this architecture but to me, it seems the best way to go if this is going to be an editor based engine. I’m not sure yet.


My main goal for this engine is speed. Unity takes a billion years to open or create a new project. It also takes a billion years to compile anything. Editing code, while the game is running, is also not great at all. I don’t want to wait around for 20 seconds for a project to compile and run. I want it to run instantly. Unity takes forever to even load a new scene, and I’m running a 16 core 1950X. Godot surprised me with how fast it builds and runs a project. If I can make my engine anywhere near as fast Godot, I will be very happy. Honestly, I would contribute to the Godot team, but I don’t think I’m good enough. I don’t think I’m at the point where I can contribute to something that big.


Anyways, those are my thoughts.



Another game engine update !

Hey everyone,

it’s been a while since I posted an update. I have been working super hard on my game engine. I have committed a bunch of code on GitHub these past few months.

Here is a full list of what my engine supports so far and here is a link to my engine at GitHub.


  • Support for Windows, Linux, WebGL and Android
  • Support for DirectX 11 and OpenGL 4+.
  • Support for PNG, JPEG, BMP, DDS, TIFF, WBMP, WebP and more using the FreeImage library.
  • Support for FBX model format that then gets converted to a custom model loading format called BFX.
  • Support for custom file formats like BFA for Animation and BFM for tile maps.
  • Support for 16bit, 32bit and IEEE float WAV file format loading.
  • Support for OpenAL-Soft 1.17.2 for 3D audio.
  • Support for ttf font rendering using FreeType library.
  • Rendering 3D models with basic Phong shading.
  • Rendering 2D sprites with sprite batching.
  • Rendering 2D textures.
  • Rendering Texture Cubes and Skybox.
  • Rendering Tile maps
  • Support for playing sprite animation
  • Support GUI system
  • Math library that has Vector2, 3 and 4, 4×4 Matrix and Rectangle.
  • Support for both Perspective and Orthographic projection.
  • Support for Camera system.
  • Controller support on Windows using Xinput v1.4.


This is the sponza scene running ony my game engine with basic diffuse lighting.


Blue Flame Engine Update !

Hi everyone,


Alright, so here what I have been up to. I have added a sprite batch system called “SpriteRenderer”. Now you can draw a ton of sprites in one draw call. It’s still needs more work.

I also added some nice textures to test out. In my 3D scene there are two nicely textures cubes.

Resources for studying computer graphics

Hi everyone,

For the past year or so I have been studying computer graphics on my own and during this journey I have come across a lot of useful links and resources that helped me understand computer graphics and I thought I should defiantly share these links with all of you.

I will keep adding and updating this list for as long as I possibly can and I encourage all of you to help me out add more links to this list.


Resources for computer graphics and engine programming:-




OpenGL tutorials:-






CPU Design:-


Last update:- 15/Dec/2016

Blue Flame Engine now supports WebGL and Model loading and texturing

Hi everyone,

So I have been working super hard on my game engine, and it is looking great so far.

Now my engine supports WebGL using emscripten to compile GLES2 / GLES3 and C++ source code into JavaScript. Which is Awesome 😀
Also it now supports texturing and model loading with multiple meshes.

Here is a video of a textured obj model with multiple meshes being rendered in my engine. I know the texture work looks absolutely horrible but it works and that’s what matters. It’s just a test for now. I will upload another video with a proper model with proper texturing.

What you see right now works on Windows, Linux and WebGL using OpenGL 4.5, DirectX 11 and GLES2 and 3.

As always, here is the source code.

Blue Flame Engine is now on Linux !

Hi everyone !

It’s been a while. Well I have been working super hard on getting my engine to work on Linux, and just today I finally did it. Now my engine works on both Windows and Linux using OpenGL 4.5 !!

I had to disable and restructure a lot of my code though, and I had to disable DirectX 11. I will enable it in the next build with hopefully Android support !!! 😀

As always if you want to check out the source code you can find it here on GitHub.

Blue Flame Engine first look

Hello everyone,

So I decided to extend the Black Engine to support DirectX 11 with OpenGL 4.5. I also thought of a better name for the engine, “Blue Flame”.

So, moving forward, the “Black Engine” is now called “Blue Flame Engine” and it will support both OpenGL 4.5 and DirectX 11.

Here is a first look at what I have so far.
blue flame engine

It doesn’t look like much, but this triangle is being rendered by both OpenGL 4.5 and DirectX 11 using a single unified code. So basically you write once, and the engine will port things to both OpenGL and DirectX. One thing I still haven’t gotten around to doing is that the engine still requires you to write two sets of shaders, HLSL and GLSL. In the future there will be a single shader language you write to and the engine will convert it to HLSL and GLSL automatically.

As always, the source code for this engine is up on github if anyone is interested to look at.

Black Engine

ooh man, I haven’t posted anything in ages. Well here is what I have been doing the past few months.

I have been working on a new game engine written in C++ and OpenGL 4.5. This is a 2D/3D game engine that will support my upcoming games.

Here is a list of the engine features so far:-
1. Window system.
2. Obj model loading.
3. Phong lighting model.
4. Math library.

Here is a small demo of the engine so far. I will also post the source code for anyone to check it.

In the future I would like to extend this engine to support DirectX 11, SIMD support for the math library, Shadows, Textures and more !