You are currently browsing the CodeBot @ Work weblog archives for July, 2007.
- 3D Stuff (12)
- AI (1)
- BumpTop (5)
- C/C++ (21)
- Open Source (8)
- Radiant (11)
- Seneca (6)
- Stuff (17)
- The Mortal Realm (2)
- Uncategorized (14)
- win32 (11)
- December 22, 2009: Efficient Rendering, A La Mark.
- December 3, 2009: A Simple Opponent
- December 3, 2009: Blog++
- September 7, 2009: Food Budgeting
- July 3, 2009: Too Busy...
- March 26, 2009: How Do Patents Apply To Me?
- February 27, 2009: U.S. And Human Rights
- February 7, 2009: I've Been Busy...
- November 10, 2008: Radiant Update
- October 28, 2008: Primary Export: Pain
Archive for July 2007
Optimizing CPU Intensive Applications
July 13, 2007 by Mark.
Writing a Win32 application that only acts on events and messages does not take much thought. The messages are streamed to the message processing function, they are acted upon, and then they are discarded. But what happens when your program needs to render or process something critical as fast the computer can go? Well, that also is not hard to accomplish but at the cost of the CPU. A loop such as that will take up the full power of the CPU whenever it gets a chance; essentially a power hungry monster. An application such as a game should take this into account. Take the following code as an example of what NOT to do (I apologize for the usage of PasteBin for this. Apparently WordPress sucks for code):
This loop does do exactly what you want. It takes messages, hands them off to the message processing function and calls the Render function. But what happens when the program does not need to render, or worse, if the scene has not changed enough to merit a complete redraw. In that case, there are several things you can check for. One of which is to see if the window is minimized. Another check is to see if the scene needs to be redrawn. Take this example into account:
A bit better, but it still will take up the majority of the CPU because the PeekMessage function is rather hefty and requires some time to process. In order to speed up the message processing, there is a way to do all your message processing all in one go rather then processing one message per loop. Further more, you can add in a Sleep() call into the loop for when the rendering is not necessary:
Its a start, but there you have it. This Loop will only render when it has to and will yield CPU power to other applications when it cannot render. There are further optimizations that can be done to this loop, such as switching form PeekMessage to GetMessage when background processing is not required. That type of logic should only be considered for applications other then games because games will always have some type of calculations to do in the background that are not user dependent.
Also, there is a neat little function called TranslateAccelerator(). It speeds up translations based on a range of message types. I do not recommend using it on a large group of messages, rather for something like keyboard input or mouse input, it can add several FPS to a game. Furthermore, there have been some talks on GameDev.net about single thread versus multi-thread games, where all input is done on one thread and all rendering on another. I do not believe that using multiple threads to manage something as complex as rendering or input simultaneously would merit any kind of performance boost.
Either way, there have been numerous posts on a number of programming forums on how to write the perfect loop, and my own implementation is not the final say in its design. It is merely an example of how to begin writing a smarter loop. At the end of the day, a decent main loop will decide if your application takes up 99% CPU power or less then 5%. Happy Coding.
Posted in win32 | No Comments »
My Cup of Tea.
July 10, 2007 by Mark.
From the sounds of the title of this post, there should be something about tea. But rather its more of my reflection on my own education up to this point and how it applies to this job. I have been working at my current job for the last 3 months and I have noticed that some of the courses that I have taken (that I thought were just a waste of time) have started to crop up in my daily routine. For example, the information that I have learned in the User Interface course has been almost invaluable in my line of work. The tasks that have been put in front of me within the last few weeks have forced me to review the material covered in that course. Everything about icon design, readability and even text position are actually on my list of things to do. I am delighted to see that my education is being put to the test.
But, I am not going to lie, there are subjects that I wish were available at Seneca. In essence, there are some aspects of this job that grind me squishy brain to a halt. Things like physics and vector calculus take semesters to learn but instead are forced into me in a matter of hours. But like any other lab, this place is full of people with higher levels of education then yours truly, so its not as bad as it might seem.
Posted in Seneca | 1 Comment »
Yet Another Guest Lecture!
July 5, 2007 by Mark.
I was just told that Richard Stallman is going to be in Mississauga today to give a talk about free software. The talk, which is being co-sponsored by the Department of Mathematical and Computational Sciences and U of T’s Knowledge Media Design Institute, will be non-technical, and members of the general public, along with computer scientists and engineers, are encouraged to attend. Unfortunately, The talk is right in the middle of my working hours. But regardless, I’m sure that he will be in town again.
If anyone is interested in going, Here is the info:
Date: Thursday, July 5, 2007
Place: Matthews Auditorium (Room 137), Kaneff Centre, University of Toronto Mississauga, 3359 Mississauga Rd. N.
Time: 5:00 pm
Posted in Stuff | No Comments »