By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,647 Members | 1,845 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,647 IT Pros & Developers. It's quick & easy.

Animation using pure C/Win32

P: 24
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
  2. {
  3.    /* update all structs (bitmap locations, etc) */
  4.    SleepEx(iSpeed, FALSE);
  5.    InvalidateRect(hwnd, lpRect, TRUE);  
  6. }
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
Share this Question
Share on Google+
8 Replies

Expert 100+
P: 1,764
Probably has to do something with iSpeed, how do you calculate it?
Sep 12 '09 #2

P: 24
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

Expert 10K+
P: 11,448
Timers aren't very accurate on a Microsoft Windows PC. It is not a real time operating system by far.

kind regards,

Sep 12 '09 #4

P: 24
I don't. It's a constant.
Sep 12 '09 #5

P: 24
So how do they make games that run on Windows?
Sep 12 '09 #6

Expert 100+
P: 1,764
They don't use WM_PAINT event, in fact whole message loop has to be improved.

Sep 12 '09 #7

P: 24

Thanks so much! This is a great article. I will rewrite my app accordingly.
Sep 12 '09 #8

Expert Mod 5K+
P: 8,916
Also you don't use Sleep (or SleepEx) which only has ms resolution if that (DOS and therefore Windows 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

Post your reply

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