# Immediate Mode GUI - Theory and Example - Slides

Tue

24

Oct 2017

Today I gave a talk at Warsaw GameDev Meetup. Topic of my presentation was: "Immediate Mode GUI - Theory and Example". You can download slides here:

Comments | #gui #teaching #events Share

# Lost clicks and key presses on low FPS

Sun

22

Oct 2017

There is a problem with handling input from mouse and keyboard in games and other interactive applications that I just solved. I would like to share my code for the solution. When your app uses a loop that constantly calculates and renders frames, like games usually do, it may seem natural to just read current state of every mouse and keyboard key (whether it's down or up) on each frame. You may then caculate derived information, like whether a button has just been pressed on released, by comparing new state to the state from previous frame. This is how Dear ImGui library works. So first solution could look like this:

void UpdateFrame() { // Fill ImGui::GetIO().DeltaTime, KeyCtrl, KeyShift, KeyAlt etc. ImGui::GetIO().MouseDown[0] = (GetKeyState(VK_LBUTTON) & 0x8000) != 0; ImGui::GetIO().MouseDown[1] = (GetKeyState(VK_RBUTTON) & 0x8000) != 0; ImGui::GetIO().MouseDown[2] = (GetKeyState(VK_MBUTTON) & 0x8000) != 0; for(uint32_t i = 0; i < 512; ++i) ImGui::GetIO().KeysDown[i] = (GetKeyState(i) & 0x8000) != 0; ImGui::NewFrame(); if(ImGui::IsKeyPressed('A')) // Do something... }

There is one problem with this approach. If user presses and releases a key for a very short time, so that both press and release happens between two frame, it will go unnoticed. This is very annoying. It happens especially when:

- Framerate in your program is low.
- Clicks are very fast because they are generated programatically e.g. automatic double-click generated by X-Mouse Button Control or other mouse software.

First step towards solving this is to react to "real" events that are sent by the operating system:

Read full entry | Comments | #gui #winapi #windows Share

# VK_KHR_dedicated_allocation unofficial manual

Mon

16

Oct 2017

I wrote a short article that explains how to use Vulkan extenion "VK_KHR_dedicated_allocation". It may be interesting to you if you are a programmer and you code graphics using Vulkan.

Go to article: **VK_KHR_dedicated_allocation unofficial manual**

Comments | #graphics #vulkan Share

# How to check if an integer number is a power of 10?

Wed

04

Oct 2017

I was recently asked a test question to write a function that would check whether a given integer number is a power of ten. I didn't know the answer immediately because I never solved this puzzle before, so I needed to think about a solution. I quickly came to a conclusion that there are many possible solutions, so I later decided to write them down and share with you.

Number is a power of 10 if it's equal to 10, 100, 1000 etc. 1 is also 0-th power of 10. Other numbers like 2, 3, 11, 12 etc. are not powers of 10. 0 and all negative numbers should also be rejected.

Below you can find 5 different solutions - all of them correct, but some of them more efficient than others. Example code is in C, but you can easily implement the same algorithms in Java or any other programming language.

First thing that came to my mind was to convert the number to a string and check whether it contains '1' followed by '0'-s.

int IsLog10_v1(int32_t x)

{

// Convert x to string.

char buf[12];

itoa(x, buf, 10);

const size_t bufLen = strlen(buf);

// Check if string contains '1' followed by '0'-s.

if(buf[0] != '1')

return 0;

for(size_t i = 1; i < bufLen; ++i)

{

if(buf[i] != '0')

return 0;

}

return 1;

}

Another option is to convert the number to floating-point format, use "real" log10 function, and check if the result is an integer. Note that double must be used here, because float has too small precision and would incorrectly return 1 for inputs like: 999999999, 1000000001.

int IsLog10_v2(int32_t x)

{

// Convert x to double. Calculate log10 of it.

double x_d = (double)x;

double x_log10 = log10(x_d);

// Check if result is integer number - has zero fractional part.

return x_log10 - floor(x_log10) == 0.0;

}

If we want to operate on integer numbers only, we may come up with a recursive algorithm that checks whether the number is greater than zero, divisible by 10 and then calls the same function for x/10.

int IsLog10_v3(int32_t x)

{

if(x <= 0)

return 0;

if(x == 1)

return 1;

if(x % 10 != 0)

return 0;

// Recursion.

return IsLog10_v3(x / 10);

}

Because it's a tail recursion, it's easy to convert it to iterative algorithm (use loop instead of recursive call).

int IsLog10_v4(int32_t x)

{

if(x <= 0)

return 0;

// Same algorithm as v3, but converted from recursion to loop.

for(;;)

{

if(x == 1)

return 1;

if(x % 10 != 0)

return 0;

x /= 10;

}

}

Finally, the most efficient solution is to notice that there are only few possible powers of 10 in the range of 32-bit integers, so we can just enumerate them all.

int IsLog10_v5(int32_t x)

{

// Just enumerate all possible results.

return

x == 1 ||

x == 10 ||

x == 100 ||

x == 1000 ||

x == 10000 ||

x == 100000 ||

x == 1000000 ||

x == 10000000 ||

x == 100000000 ||

x == 1000000000;

}

You can find full source code as a simple, complete C program together with tests on my GitHub, as file: **IsLog10.c**.

Copyright © 2004-2020