473,387 Members | 1,420 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Animation using pure C/Win32

As part of familiarizing myself with the Win32 API, I wrote a simple animation program in C. The program has a struct for each bitmap, holding information such as its location, visibility, mask (for transparency), etc. Then I have a separate thread running that has a WHILE loop that looks like this:

Expand|Select|Wrap|Line Numbers
  1. WHILE (TRUE)
  2. {
  3.    /* update all structs (bitmap locations, etc) */
  4.    SleepEx(iSpeed, FALSE);
  5.    InvalidateRect(hwnd, lpRect, TRUE);  
  6. }
  7.  
In my WM_PAINT case in the window procedure, I'm simply drawing all my bitmaps based on their updated attributes.

The animation on my home computer seemed smooth and reasonable (it looked good), but then I copied the EXE to my work computer, and when I tried it at work, it seems to run in slow motion. It's still smooth, not skipping 'frames' and not showing any graphical problems, but it runs in slow motion. I do notice that every now and then the animation speeds up to its normal pace for a few seconds, but then it slows down again.

I know the problem is not with my work computer. I've run simple games on it before and their animation speeds are normal.

Any advice?
Sep 11 '09 #1
8 5504
Savage
1,764 Expert 1GB
@DelphiCoder
Probably has to do something with iSpeed, how do you calculate it?
Sep 12 '09 #2
I discovered the problem was with SleepEx(). I ran a diagnostics program that I wrote. It takes 157 ms to execute a loop containing 100 ms worth of SleepEx() calls on my work computer. On my home computer, it's exactly 100 ms.
Sep 12 '09 #3
JosAH
11,448 Expert 8TB
@DelphiCoder
Timers aren't very accurate on a Microsoft Windows PC. It is not a real time operating system by far.

kind regards,

Jos
Sep 12 '09 #4
@Savage
I don't. It's a constant.
Sep 12 '09 #5
@JosAH
So how do they make games that run on Windows?
Sep 12 '09 #6
Savage
1,764 Expert 1GB
@DelphiCoder
They don't use WM_PAINT event, in fact whole message loop has to be improved.

Read http://www.mvps.org/directx/articles..._game_loop.htm
Sep 12 '09 #7
@Savage

Thanks so much! This is a great article. I will rewrite my app accordingly.
Sep 12 '09 #8
Banfa
9,065 Expert Mod 8TB
@DelphiCoder
Also you don't use Sleep (or SleepEx) which only has ms resolution if that (DOS and therefore Windows 2.xxx and 3 and 95 had a maximum timer resolution of 50ms IIRC).

Use wait-able timers which are OS objects with much better resolution and accuracy.
Sep 13 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Simena Dinas | last post by:
KToon: 2D Animation Toolkit KToon is a 2D Animation Toolkit designed by animators (Toonka Films) for animators, focused to the Cartoon's industry. This project is covered by the GPL License...
6
by: Darren | last post by:
X-No-Archive Hi all, Can anyone help me with structuring the data in a small tool I have to build? I'm trying to work out if there's a design pattern or data structure that would remove some...
2
by: billrdio | last post by:
I am trying to make a JavaScript animation of real-time images - i.e. images that will periodically change on the server. The problem I am having is that the JavaScript animation I have created is...
5
by: Miyra | last post by:
Hi. I'm working with an app that uses exceptions for control flow. These are code blocks where exceptions are thrown/caught regularly. A couple hundred exceptions occur per hour and they're caught...
3
by: Bob | last post by:
I want to create a GUI using VS .NET. For now I just want to input some parameters used by the algorithms I've written in C++. Maybe later I want to access a database to load the data from and plot...
10
by: Robert Skidmore | last post by:
Take a look at this new JS function I made. It is really simple but very powerful. You can animate any stylesheet numeric value (top left width height have been tested), and works for both % and px...
2
by: rdemyan via AccessMonster.com | last post by:
My application has a lot of complicated SQL statements, calculations, processing that takes time. I've created a custom form to act like a messagebox. It has 10 small rectangles on it that change...
3
by: | last post by:
Can someone point me to some good info on how to do somoe simple animation on a form. Specifically, I want to have some tiles that when they are clicked, they flip over in place and reveal a...
4
by: Sin Jeong-hun | last post by:
Most applications, including Windows Explorer, show some sort of 'wait' dialog with animation when a lengthy operation is going on. For example, When the Windows Explorer is searching for...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.