Category Archives: C++

How to create parallax effect using accelerometer

This is short guide how to create parallax effect using accelerometer on mobile platforms. Some code is related to Android, but all concept is applicable to iOS, etc.

What is parallax effect? There are some more complex definitions but i would define as simple as the following – you move your phone/device in space and some objects inside your application are shifting accordingly to compensate this movement. This allows to create some strong feeling of 3d interface as well as nice interaction effect.

As good example you can check out my live wallpaper (link on market), which is using this effect while rendering particle system of moving objects. More information about this application can be found here.

To create parallax effect we need to grab data from accelerometer sensor (as i found out gyroscope is not present at majority of phones while accelerometer gives enough of data to be happy with it), convert sensor data to relative rotation angles and shift some parts of application interface accordingly. 3 steps:

Continue reading

Stackblur – C++ multi-threaded version of fast blur algorithm

I found nice algorithm for blurring images – Stackblur by Mario Klingemann. It could do the job relatively fast and gives decent quality. You can check it here – web demonstration. As you can see it can be usable even in web projects.

stackblursample

As i wanted to include it to my cross-platform engine i found two c++ implementations: 

First is SSE friendly, second contains some division optimization via static tables. However, both are not using all cpu cores. I took second one as foundation for my implementation, as i expected my code to work on mobile devices with no SSE support. Single-core processing of 1920×1200 rgba image with 100 px radius took only 219 ms (Intel Q9550, Windows 7).

I improved stackblur code to multi-threaded version – on my quad-core cpu speed results as expected showed 4x improvement – 63ms for the same task. You can download the part my lib below and use it as a foundation to your needs.

Download multi-threaded 32bit-color (RGBA) version of StackBlur:  stackblur.cpp

I believe algorithm can be optimized even further – any suggestions are welcome.

Compact lib for JSON parsing (cross-platform C++)

Recently i was in search of compact C++ source code for parsing  of JSON format. This is very simple minimalistic format and its very popular – but i was surprised when found a lot of complex parsers and no simple implementation inside one cpp-h pair. So i wrote one.

mvjsonlogo

Here it is – tiny C++ cross-platform solution for JSON decoding without any dependencies like boost or something: MVJSON.cpp MVJSON.h (version 1.0)

Continue reading

Alive Numbers 2 – android release of my minimalistic live wallpaper with embedded widgets

Brand new version of live wallpaper –  Alive Numbers 2 for Android devices. Enjoy smooth OpenGl ES 2.0 animation of minimalistic animated background in couple with customisable embedded widgets. New version has adjustable color schemes and ability to hand-tune widget layout. You can choose from set of base animations and play with a lot of options for every widget.

Alive Numbers 2

It can be found by name ‘Alive numbers 2′ or here: https://play.google.com/store/apps/details?id=com.calibvr.minimal

This topic contains screens and details…

Continue reading

Perfomance of complex GL ES 2.0 shaders on mobile devices

There is very nice site http://glsl.heroku.com/, where you can find the gallery of complex GLSL shaders (from very simple gradients to very complex rendering systems). You can modify their code at real-time using provided editor:

heroku

Current implementation of WebGL is using GL ES 2.0 – the same as all mordern android / iOS phones/tablets. So i decided to test if i can use these shaders at mobile applications – and tested their performance on Sumsung Galaxy Note II. Of course i tested only relatively simple shaders expecting them to run slow…

Continue reading

Live Wallpaper under Android can be powered by my cross-platform engine now

After my recent modifications of android part of my cross-platform engine, it is possible to make application also run as android live background. I plan to implement the same feature as well for iOS 7, but a bit later. Here is working example (https://play.google.com/store/apps/details?id=com.calibvr.synctimer):

Synctimer as live background

Android live wallpaper is not a general Activity application – it is special WallpaperService. And you cant implement it in pure C++ using NativeActivity. All my core is cross-platform C++ so i implemented two-way communication between C++ and JAVA. From C++ NativeActivity you can call JAVA classes through JNI and from java service you can call native (c++) methods of engine core.

The tricky part that all this communication involves a lot of different threads. Wallpaper service has its own thread, but rendering should be performed in another one. My native C++ core is launched as third thread and spawns other async threads which could call some java methods. But i got through all this nightmare using mutexed queues of events. As result i got full functionality of my engine at the background of android launcher.

When i only started working with android i wrote small live wallpaper in pure java (with no OpenGL). Wasted only couple of evenings and even lost the source code. But recently i was surprised when discovered that it has more than 100.000 downloads (https://play.google.com/store/apps/details?id=back.livenumbers). It does not even work properly on my Note 2 now.

But the point is that now i have smooth OpenGl ES 2.0 animations/effects in couple with engine functionality. Probably, i will create couple of stylish backs fused with some in-code widgets (like battery indicator, weather forecast, clock, calendar date or something else) as implementation will be relatively easy now using my engine. Any suggestions on this matter are welcome.

Manual decoding of ICO file format – small c++ cross-platform decoder lib

This post is related to one simple task – decode ICO file format from C++ manually (I needed this for one of my projects where i wanted to display fav icons related to web sites and decoder has to be part of my cross-platform framework). As a result you can download small c++ code below (ico.cpp). The description of ICO format you can find here – http://www.daubnet.com/en/file-format-ico. Image data is stored uncompressed so we don’t need to implement some smart decompression algorithms – just read some headers, get data and apply bit mask to fill alpha channel.

icons

As solution i wanted a function like this:  

bool IcoDecoder::decode(unsigned charbuffer,///< input buffer data

    int size,///< size of buffer

    unsigned intwidth,///< output – width

    unsigned intheight,///< output – height

    std::vector<unsigned char>&image///< output – image data

) 

and this function should extract the largest possible image from multiresolution icon.

Continue reading

Beal prize – first approach

May be you already heard that so called Beal Prize was increased to 1.000.000$ (http://ns3.ams.org/bealprize.html). So if you can find positive integers A^x+B^y=C^z where x,y,z > 2 and A,B,C dont have common factor, you can get a lot of cash. As you can see its not a complication to write a piece of code to check some range of numbers, or generator to generate some lucky numbers. Its officially checked only in range where all numbers are less than 1000.

So i decided to try my luck.

As first approach i decided to generate a lot of prime numbers (15.000.000 for start). This is C++ code for relatively fast generation of prime numbers:

// Primes

int MAXPRIME = 15000000;

unsigned long * primeNumbers = new unsigned long[MAXPRIME];

LOG << “Computing “ << MAXPRIME << ” prime numbers…” << NL;

LOG.startProfile();

{

primeNumbers[0] = 2;

primeNumbers[1] = 3;

long x = 3;

long sqrtIndex = 0;

for (int i = 2; i < MAXPRIME; i++)

{

bool ok = false;

while (!ok)

{

x += 2;

if ((x % 6 == 1) || (x % 6 == 5))

{

ok = true;

while (primeNumbers[sqrtIndex] * primeNumbers[sqrtIndex] < x)

sqrtIndex++;

 

for (int j = 0; j <= sqrtIndex; j++)

if (x % primeNumbers[j] == 0)

{ ok = false; break; }

}

}

primeNumbers[i] = x;

if (i % 100000 == 0)

LOG << i << ” [ “ << primeNumbers[i] << ” ] “ << NL;

}

}

LOG.profile(“Computing “ + SS::toString(MAXPRIME) + ” prime numbers was completed”);

To check Beal conjecture we need support of very long integers for C++. I have my own implementation, but you can use any decent library.As first straight-forward approach i decided to do in endless cycle the following (until i get lucky numbers):

  • Get two random prime numbers from pregenerated array.
  • Generate random x,y,z within reasonable range.
  • Compute S=A^x+B^Y
  • Compute the most close C – which gives (2*C)^z ~ S (i have used simple method of bisection).
  • If (2*C)^z == S break and report success.

I got yet no success, and some mathematicians tell that conjecture is probably true. But still why not to try to search if it is so easy.

SyncTimer – stylish multiplatform timer with synchronization between devices

My newly developped cross-platform engine finally goes to markets. Current platfroms are iOS, Android and Windows desktop.

SyncTimer icon

Download stylish multiplatform timer with synchronization between devices. 
You can control your timer from your PC, your iPhone or iPad, your android tablet and more. 
And even get more fun sharing timer with your friends.

http://synctimer.calibvr.com/