Thursday, July 26, 2007

C's Static Keyword


After this post you shouldn't find the static keyword confusing in C anymore! Let's get started...
The first context we find static variables is in functions. First, it's important to understand that there are two types of "storage classes" in C. The first is auto, short for automatic, which is the default if no storage class is specified. This means that a variable is automatically destroyed upon exit from the code block inside a function or function itself that it's defined in. The other storage class in a function is static. This means that a variable maintains the static memory allocation that is defined in it's first execution through the respective line of code. This static variable stays around until program termination. It doesn't even get reinitialized on a second pass through it's respective line of code, so it's great for counts and other things internal to the function that shouldn't be divulged to the outside world.
At this point you may be wondering about static variables in a global scope or, put another way, at the file level. Either these static variables would be in a header or implementation file. In the implementation file, they wouldn't be visible to clients of the implementation because they would only have access to the header file, in the first place. In the header file, that is included in other files, the static variable definations would also be included, and since included they would be statically visible to their respective header file and all files that use that header. Like static variables, global variables at the file level, retain their values from program execution to termination. Thus, there is absolutely no reason to make global variables static.
In the second context, we find static functions. Say we want to include a private function in a header for consistancy and completness. By doing this, we would normally make the function public to everything else that includes its respective header file. However, if we prepend the function prototype and definition with the static keyword we can still keep it private as originally intended. Now you may be thinking, "Wait just a minute, Samuel! In the previous paragraph, you stated that static variables at the global scope were still public thanks to being able to include them, along with the rest of their header, in the same code that wouldn't be able to access static functions in the same included header file. Pardon me, but this seems a bit inconsistent!" It is and that's all there is to know about the static keyword in C.

Wednesday, July 25, 2007

Rebooting One's Brain

Once in a while I find myself working on a task that requires infinite effort to complete in my current state of mind. That's to say that no matter how hard I work, it's not hard enough and I serve only to frustrate myself by attempting it further, no matter how much I want to get it done.
In these situations the best thing I can do is walk away. This illustrates one of the reasons it's such a bad idea to leave a task for the last minute. By working on things early there is a lot less stress because there is time to screw up and try again. Numerous other people also recommend walking away from the situation temporarily. Often the answer comes when a calmer state of mind is reached.
Any given task that's impossible to complete in the current state of mind serves to prevent other, completable, tasks from being given attention. On several occasions I have focused on one thing only to become depressed and waste an entire day accomplishing nothing. What does one do when he or she realizes that nothing is being accomplished? Procrastinate! This downward spiral can only be broken by working on something else before the impossible task consumes the worker. Best of all if the impossible task is given a rest soon enough, one's moral doesn't have to suffer through other situations that are otherwise unrelated.

Saturday, July 21, 2007

An Officially Productive Week

I am so tired and fulfilled! You see, I just completed my officially productive week. I read, programmed, exercised, volunteered, and taught. Now that I look back, I was nearly constantly working during this last week! Wahoo! During the last few weeks I have felt somewhat down about not fulfilling my calling to make something of my time. I used this feeling of defeat to entice myself to victory for a week by stating that I would officially call myself productive should I complete the above set of tasks. That's not to say that I didn't change any tasks half way through. Instead, I had a clear idea of the amount of effort I wanted to put into my week and a conscious default, which helped steer clear of random web surfing and starring off into space. Work hard and play hard, right? I finished off with reading one of Steve's posts on productivity. He concluded by saying that the most productive thing someone can do is find his or her essence if he or she hasn't already. Otherwise, one could go on and on working hard without accomplishing anything. This makes sense because seeking out one's essence is like seeking out a conscious direction to take one's entire life.

What is my essence? Hmm. I haven't thought of this question for a long time. My essence is to have fun, love others, love myself, work hard both physically and mentally, not care about what others think or be proper, live with a free conscience, and so on. How about some simplification? If I factor out laziness, my essence would subconsciously be understanding and exploiting that understanding up to this point. Just for fun let's take a look at my accomplishments this past week and see what goes with understanding. I read The Prince and some more details from the C++ Programming Language. These books are filled with understanding both in terms of leadership and technology. Programming was putting my understanding to use. Exercising increased my ability to understand through an increase in blood flow to my mind. Teaching is giving back knowledge. The odd one out was volunteering. That was for feeling worthwhile and doing something nice for another person without expecting anything back. Fun fun.

I suspect that maintaining this week's level of productivity is very possible in the coming weeks even though I am so exhausted right now. I just have to be more of a hard-ass when it comes to managing my time. That means no watching Hannibal Lecture eat living people's faces off... again, last night. That's trash anyway so I won't be missing much more than a bit of suspense. I should have gone to bed earlier. This all falls back to the three pillars of sustained productivity as I see them: eating healthy, sleeping minimally and regularly, and exercising until one can't exercise anymore. It's also very important to consciously plan relaxing time. By relaxing I don't mean sitting on the couch watching TV. I am referring to doing something very exciting. If I were married, that would probably be dancing with my girlfriend. I say girlfriend so as to denote a consciously fed desire for continued romance. If I were single, which I am (yay), it could be something as simple as throwing a flying disc with a friend without any fear of rejection. The beautiful thing about using sports to unwind is it brings life to a whole new and exciting level. This level requires hard work all the time but it is so much more alive then the lethargic alternative of drifting through life without any plan. When one doesn't have a plan, he or she is swept up by laziness or another person's "optimal" plan. Not so optimal for one's self now is it?

Saturday, July 07, 2007

Updating a Game World

Picture a game world with terrain, ground level sprites, air-born sprites, and weather. From the water tiles to the movements of the clouds high above, nearly everything in this world requires updates to their states. For example, the water may need to jump by 15 frames in the span of a second or a cloud may need to move by 20 pixels in 1 minute. This brings one to the question of what is the best way to update the various parts of the game world. How should updates be handled at the higher levels of the game world? Specifically, how should updates be stored and when should various parts of the world be updated?
Updates can be stored in one of two ways. First updates can use ticks, the smallest amount of measurable time which increments once with each processor tick. As a benefit, updates can be applied at different intervals to different parts of the game world without storing any extra information. One could update a space ship 100 ticks into the future once in a second and simultaneously update a snail the same number of ticks once in a minute. Extraneous information is minimized in this implementation as both the snail and space ship updates could take into account their last tick count when considering the magnitude of the current update. Compare this with the second way of representing updates: storing updates as the time since the last update. If we update our game world by one second, we either need to update each object in it by the same amount of time or store the update time accumulated for items that aren't updated and update them by the sum of accumulated time when the time finally comes. The first method may seem neater so far, but consider a sprite with a complex AI and movement. If we update it by providing a new tick count, the sprite would need to recalculate all of it's actions from the beginning again and again, wasting valuable time.

Another way to approach the way updates are stored is by mixing the two approaches. Updates can be represented as tick counts at the higher levels and then lower level implementations may process the updates by differentiating them with the last tick count. This would allow for not having to update everything at the same time while still only doing the processing of what is necessary internally at the lower levels.

One issue still remains, what should be done when the tick count reaches it's maximum and has to wrap back to the beginning? If the last tick stored is greater than the current tick we know that the count has been wrapped and we could simply sum the difference between the old count and the max and the new count to find out the current amount of ticks that have past since the last update.