• Recent Tutorials

  • Application Planning & Design - The quick 101

    Now thats all fine, but that was a monolithic program AKA it only does one thing at any one time. Lets take a strategy game now, each players AI is in a separate thread, how does it fit together, its now not just a single branch but how the seperate threads interact with each other - this is harder because its not just getting a thread to talk and one to listen it has to happen at the same time!

    For this I would draw rectangles for each thread and take it from there, a bit like this (main loop only):

    (This should be in blocks but the editor does some weird stuff with spacing, sorry).
    Code:
    |---------------------------|   |---------------------------|   |---------------------------|
    | Get human input and   |   | Work out what the      |   | Work out what the      |
    |        wait()                 |   | next action is and wait |   | next action is and wait |
    *------------------------------------------------------------------------------------------*
    | Send to draw queue    |   | Send to draw queue    |   | Send to draw queue    |
    And so on...
    The point is, each thread has an all important thread that breaks it after each crucial instruction - if that third box (thread) is on another computer and it sends player input to the main thread and it is busy drawing and not listening on the port for input - it won't arrive. Anomalies can then occur in the graphics for example if threads 1 and 3 have finished their draw cycles and are waiting for the next cycle, with all those graphics nicely on screen before thread 2 comes and draws itself onto the screen... Flickering can occur here, and in some instances artifacts depending on how the blending is set but that is a different matter.

    The solution - simpler than it seems. Either, have a
    Code:
    ReadyToAdvance   : Boolean;
    and a

    Code:
    while (ThreadType.ReadyToAdvance = False) do delay(10);
    Or something similar to prevent things coming out of sync. This is crucial when dealing with threads - and yes my examples may not be the best out there - a computational example for encryption would have been better: if each thread works on encrypting a block of data and the main thread pieces it back together as it is recieved and one thread lags for a moment due to high CPU usage and doesn't get that block sent to the main thread in time - big disaster: encrypted text out of order, in say AES-256, is pretty much lost forever. Bad.

    Comments 11 Comments
    1. LP's Avatar
      LP -
      Can you please modify my quote in the article to something less cluttered like this:

      Depending on the project type, there are different Software Development Philosophies. If project is done in smaller teams, one of Agile Development techniques can be used. Some common architectural solutions are described as Design Patterns. There is also a book with the same name.

      There are several code refactoring techniques that should be used to improve the overall quality of the code. In this topic, take a look at Quality Attributes and Non-functional Attributes that should be taken into account when developing the project.
    1. paul_nicholls's Avatar
      paul_nicholls -
      Hey Ben, that attachment didn't work in your "What goes where and how?" installment of this article so I can't see the image...
    1. code_glitch's Avatar
      code_glitch -
      @Lifepower: Sure thing
      @Paul: Might be from when I had to transfer it into the right section, I'll see what I can do.
    1. SilverWarior's Avatar
      SilverWarior -
      If you think that you examples of Application planning with the help of paper and pen will convince other in using similar methods I'm afraid you are mistaken. Why? Most pepole who ever created any computer game or have strong logical sence would come up with idea of that complexity in a few minutes and thus won't even bother writing it down.
      But when doing real planning the rough plan would look like something like this:

      1. Check if the game is already running (using mutex or similar method). If it is running notify user about that with a message and later close second one or make sure that the already running game is shown and then close second one. If the game isn't already running continue to step two.
      2. Load the esential libraries needed for graphic interface and sound system. Also if your game has support for joysticks, gamepads, and some other hardware load neccesary libraries for those to.
      3. Load settings
      4. Create a window and initialize a graphical engine.
      5. Initialize sound system.
      6. Initialize control system.
      6.1. Show main menu. For this use a gamesteate property.
      6.1.1. Alow user to browse trough other submenues of the main menu (ingame options, credits, etc.)
      6.1.2. If a user selects a new game start game initialization proces. Show a loading screen here.
      6.1.2.1. Initialize necessary classes and records
      6.1.2.2. Load predesigned map or create a random map
      6.1.2.3. Create necessary ingame objects (characters, buildings, etc.)
      6.1.2.4. Hide the loading screen and draw initial state of the game.
      6.1.2.5. Start the game loop
      6.1.2.5.1. Check fo player input.
      6.1.2.5.2. Proces game changes, based on player input and other variables and game procedures. This proces can be divided into multiple subsections based on the game design.
      6.1.2.5.3. Draw game graphics
      6.1.2.5.4. Play necessary game sounds and music
      6.1.2.5.5. Fire some special effects (force feedback etc.)
      6.1.2.6. When the loop ends free used ingame objects
      6.1.2.7. Unload maps
      6.1.2.8. Free unneded clases and records
      6.1.2.9. Show main menu again. And continue with main menu code.
      6.1.3. If player selects load saved game, then do similar as in new game but instead load initial game data from savegame file
      6.1.4. If player choses Exit start with game closure procedure
      6.1.4.1. Unload input controlls
      6.1.4.2. Unload sound engine
      6.1.4.3. Unload graphical engine
      6.1.4.4. Free rest of the application's resources
      6.1.4.5. Terminate.
    1. code_glitch's Avatar
      code_glitch -
      @SilverWarrior: Thats exactly the problem, I average around 94% on exam scores involving logic as a fundamental (chemistry, physics, biology, maths etc) - I NEVER used to think planning everything like that was needed - I just HAD the idea.

      BUT, consider the number of individual statements in the following:
      Code:
      1. Setup the appropriate sound & video environment for the OS and its specific setup & HW
      2. Load your config file and ensure it all works
      3. Process input, take action and etc
      You get the idea
      Even where you say
      Code:
      6.1.4.3. Unload graphical engine
      You could still be looking at a few dozen lines. Having that plan in my head as well as my code is just a tad unnecessary. Brushing your teeth is not hard, neither is remembering an essay, combine the two and its quite hard - and try it while you drive: now thats a proper challenge for us people with 4 limbs. Kids, please don't try that either at home or on the roads
    1. SilverWarior's Avatar
      SilverWarior -
      That is the most comon problem in newbe game or appliction developers. They have a great general idea but don't have the necessary knowledge about what all needs to be done to make that general idea a reality.
      I myself am full of general ideas and have ended with many unfinished projects, becouse I never figured out what all is needed to make that idea a reality.
      Now I'm slowly beggining to understan all of this. And since I'm a pasionate gamer I learned alot just by observing how games works. While most pepoles see games just as bunch of graphics and sounds I look at them compleetly differently. I don't se graphics I see sprites or 3D objects, I know that there are complex mechanich behind every game, etc.
      Actually I never looked at games or at other applications as most of the pepole does. I always knew in a whay that they are formed from various parts (buttons, functions, etc.). That's why I started programing, becouse I wanted to learn exacly what is going on behind the scenes.
    1. LP's Avatar
      LP -
      Quote Originally Posted by SilverWarior View Post
      I myself am full of general ideas and have ended with many unfinished projects, becouse I never figured out what all is needed to make that idea a reality.
      Actually, this is common especially in the most ambitious projects. Sometimes even trivial projects with minimal requirements end up being very large and time consuming, not because of lack of planning but simply because of time underestimation being very common among even the most advanced developers and engineers.

      This subject is more of philosophical nature and in this context, there is a work Unskilled and Unaware of It. The idea is that the more you know about some subject, the more accurately you can estimate your own knowledge of it and vice-versa: if you know little about some task, you'll most likely overestimate your own skills on this task. In other words, the more you learn about something, the more you realize that you actually know very little about it.

      There has been a related work (unfortunately, I've lost this reference) that mathematically proved that if you consider yourself above the average in certain knowledge, probabilistically it is more likely you *are* the average.

      There was another very interesting research, although I've lost this reference as well. The idea was that you need to dedicate fully at least 10 years into one specific area to become master of it (btw, if someone has reference to this paper, *please* let me know).

      To resume, keep working on projects, even if they are unfinished, learning how they are done and never stop at this task; in ten years you'll most likely succeed.

      There's one quote related to this phenomena of having many unfinished projects. When they asked Edison, what it felt like to fail 1,000 times to invent the bulb, he replied that he did not fail 1,000 times, rather than he found 1,000 ways how it should not be made (or in other contexts, that the development of light bulb involved 1,000 small steps).
    1. code_glitch's Avatar
      code_glitch -
      @LifePower: If you ever get the chance, perhaps take a look at a book called 'Dreaming in Code' or http://en.wikipedia.org/wiki/Dreaming_in_Code for a quick rundown. Quite a good read, follows the development of the Chandler program (time management software). It covers quite a lot of the morals of planning and time management on software development.
    1. LP's Avatar
      LP -
      Quote Originally Posted by code_glitch View Post
      @LifePower: If you ever get the chance, perhaps take a look at a book called 'Dreaming in Code' or http://en.wikipedia.org/wiki/Dreaming_in_Code for a quick rundown. Quite a good read, follows the development of the Chandler program (time management software). It covers quite a lot of the morals of planning and time management on software development.
      This definitely seems like an interesting book, thanks!
    1. LP's Avatar
      LP -
      Just wanted to add something to this discussion, there are some concepts that you may find interesting. Contrary to Design Patterns that I have mentioned earlier, check Anti-patterns. Also, for indications of a problem in the software check for Code smell.

      Other pages that you might find interesting are Programming rules of thumb and Programming principles.
    1. code_glitch's Avatar
      code_glitch -
      Looking quite good, and for those who want to know about my next article, I am working on it - and its a tantilising one! All I will say for now is that its targeting a very popular linux and how to get your libs running on said platform . You'll have to wait and see what its all about , development and writing is on the slow right now courtesy of mock week and me having to sit 6 hours + of exams every day
Comodo SSL