News | Articles | Libraries | Developer Tools | Books | Forum Links | Search   
Sections:
 

Whirl

By Vassili Philippov, October 03, 2001.
Print version

Introduction

Whirl is a demo full screen application that is written using EasyCE library. You can download installation (58 Kb). Here I describe how I created it.

What You Need

Begin

I have decided to write a small demo program - something to show to my friends. About 10 years ago I read in Scientific America about a beautiful program. The algorithm was very simple, I will describe it below. Here is some picture that explains what is my target. We need to see something beautiful in the end it increases motivation. And name - let's call this program Whirl.

That is my target

I think it will be interesting to show program development from the idea to publishing to download sites (like pocketgear.com and handango.com). I will develop this program and write everything I do. One day will be enough to complete it.

First of all I need EasyCE library. I have never used it before but I have used GAPI without wrappers and I don't want to do it again. What do I expect from EasyCE? ... nothing more then using RGB array instead of device depended buffer, the library description says that EasyCE provides much more features but I don't need them in this program. As I remember EasyCE is free for freeware programs - great, I am not going to sale my small application.

Program description

This program will be a funny demo nothing more. It is similar to good known "life". There is a cell plane. Each cell could have one of N colors. There is only one rule:

if a cell with color C has a neighbor with color C+1 then on the next step it change color to C+1 else it doesn't change color

Colors are in circle sequence. It means that next to the color N is color 0.

This algorithm can be written in C++ as:

//for each point for (int x=0; x<240; x++) { for (int y=0; y<320; y++) { //for each neighbor for (int dx=-1; dx<=1; dx++) { for (int dy=-1; dy<=1; dy++) { field2[x][y] = field[x][y]; //the field is a tore int nx = (x+dx+240)%240; int ny = (y+dy+320)%320; int nc = field[nx][ny]; int c = field[x][y]; if ( (c+1)%N == nc ) { field2[x][y] = nc; } }} }} CopyField(field, field2);

Initially the field should be filled with random colors. Another problem in this application is finding good palette. I will use the rainbow palette.

Program development. Step by step

Step 1. Download EasyCE library

It was simple to find EasyCE library because I created a list of all existing libraries for Pocket PC developers. EasyCE was the first in this list. Many thanks to Jacco Bikker. I have also downloaded the sample application because it is more easy to understand a library using samples then reading helps.

I have spent about 30 minutes looking though the sample and the library.

Step 2. Create new project

I have created new project using WCE Pocket PC Application project template. Then copy EasyCE library files to my project and included them into the project (Project\Add To Project\Files).

Step 3. Simple EasyCE application

What I am going to do now is writing very simple application that uses EasyCE. For example it will fill the screen with red color and wait a click.

...done. Here is the code:

#include "stdafx.h" #include "EasyCE.h" unsigned short* buffer; void PutPixel(int x, int y, byte R, byte G, byte B) { unsigned short c = ((R>>3)<<11) + ((G>>2)<<5) + (B>>3); buffer[x+y*240] = c; } void main() { buffer = (unsigned short*)getbuffer(); for (int x=0; x<240; x++) { for (int y=0; y<320; y++) { PutPixel(x, y, 200, 0, 0); } } update(); while (clicked()) { update(); }; while (!clicked()) { update(); }; }

I am trying to compile it... buh! Compile error:

Error EasyCE.cpp(1222) : fatal error C1010: unexpected end of file

After 20 minutes I have solved the problem. Don't using precompiled headers helped in my case (Project Settings\C/C++\Procompiled Headers).

After small linking error (it is described in EasyCE documentation) that can be solved by adding gx.lib library my project is compiled. It is time to run my application.

To run my application I need gx.dll. The simple application works just as I expected.

Step 4. Whirl logic

Now I will add whirl functionality. I think it will take about 1 hour.... done. And it works. But works slow - only 2 FPS. I expected it, now it is time for optimization. Here is my code before optimizations whirlcode1.html. As you see it is very close to my algorithm description above.

Step 5. Optimization 1

Optimizing code is one of the most interesting jobs. First of all the method Copy2To1 can be changed by simple memcpy.

The second think I think take a lot of time is color calculation. I will create an array of EasyCE format colors for each my color.

m_pPalette = new unsigned short[MAX_INDEX]; ... void Draw(unsigned short* pBuffer) { for (int y=0; y<320; y++) { for (int x=0; x<240; x++) { pBuffer[x+y*240] = m_pPalette[m_pData[x+240*y]]; } } }

I think it will be much faster then call BuildColor function for each point.

Running ... 2.6 FPS. Better but is not good enough. I think I need about 4-5 FPS.

Step 5. Optimization 2

I will try to remove the neighbor circle and optimize calculations. The result is not simple. You can found it here: optimized.html.

Running ... 6 FPS. Enough. Optimizations are finished.

Step 6. Icon

To finish the code we need to add an icon. It is enough just to add any icon to the resources and it will be an application icon.

Step 7. Installation

If your program is very simple and consists of one executable file you don't need any installations. In my case I have to insert gx.dll file also. I will not describe how I created the installation because this procedure is described in many articles. I used standard free Cab Wizard.

The End

You can download sources that include the application and the installation. Whirl.zip (386 Kb).

Related resources:

Discuss

Discuss this article. Here you can write your comments and read comments of other developers.
Rate this article:     Poor Excellent    
 12345 
© 2001-2005 Pocket PC Developer Network, a division of Spb Software House