ASP.NET
  Home arrow ASP.NET arrow Page 14 - Managed DirectX First Steps: Direct 3D Bas...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Managed DirectX First Steps: Direct 3D Basics and DirectX vs. GDI+
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 65
    2005-01-26

    Table of Contents:
  • Managed DirectX First Steps: Direct 3D Basics and DirectX vs. GDI+
  • DirectX Overview
  • Referencing DirectX Libraries
  • Put That in Your Pipeline and Shade It
  • 3-D Coordinate Systems and Projections
  • Drawing Primitives and Texture
  • The Application Proposal
  • The Coding Phase
  • Second Step: Coding Your First Windowed Test
  • Code the Render Procedure
  • Fourth Step: Using Transparent Textures
  • Fifth Step: Changing Diffuse Colors
  • Sixth Step: Testing Matrix Transformations
  • Adding the Final Touches

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Managed DirectX First Steps: Direct 3D Basics and DirectX vs. GDI+ - Adding the Final Touches


    (Page 14 of 14 )

    Because this chapter features no games, there’s no such thing as “polishing the application.” But there’s at least one thing you can improve in the samples that will surely be useful in the next chapters: finding a way to create smooth animations.

    Although it is very interesting seeing the walking man running at 400 steps per second, in a real game this kind of behavior will be, at a minimum, strange. So you’d better define a specific frame rate to improve your graphics animation.

    Including an if command in the loop that calls the Render procedure to check the processor clock and just render a new scene at previously defined intervals will suffice to give the desired effect in your test, and maybe even in some basic games. In more sophisticated ones, where different objects can have different animations running at different speeds, the control of what image must be shown at a given time will be the responsibility of each game object.

    So let’s get into a practical example. Which frame rate would be nice? Well, the best cartoons use a 32 frames-per-second (fps) rate of animation, but usually 16 fps provides a good frame rate. The actual best frame rate must be calculated for each game (or each game object), because different animations require different frame rates. For instance, you can do a walking man with 5, 10, or 20 frames. The more frames, the smoother the final animation will be, and the higher the frame rate must be. For this specific walking man animation, the rate to acquire the best results is only 10 fps. So you’ll use that.

    In the following code sample, you define the frame rate for the animation by setting the number of frames with the DesiredFrameRate variable:

    int desiredFrameRate = 10;
    int lastTick = 0;
    while(!windowTest.EndTest) {
       // Force a Frame rate of 10 frames a second at most.
       if(System.Environment.TickCount - lastTick >= 1000 /
            desiredFrameRate) {
          windowTest.Render();
          // Frame rate calculation.
          windowTest.Text = "Window Test. Frame rate: " +
             DirectXLists.CalcFrameRate().ToString();
          lastTick = System.Environment.TickCount;
       }
      
    Application.DoEvents();
    }

    The result (a screen drawn at a fixed frame rate, and a man walking at normal speed) is shown in Figure 3-32.


    Figure 3-32.  Your walking man, tired of running, now walks at a lazy rate of 10 fps.

    Note that you still continue with the loop running at full speed. In your tests, all the loop does when it’s not rendering is process the application events, but you could use an else clause with this if statement to process any internal calculation only when the screen isn’t being drawn. The basic idea is shown in the following code:

    if(System.Environment.TickCount - lastTick >= 1000 /
             desiredFrameRate) {
       //Do the game scene rendering.
    }
    else {
      
    //Do the game physics.
      
    //Calculate collisions.
      
    // Initialize anything that can help the scene to draw
              faster.
      
    // etc.
    }

    More About DirectX and GDI+

    After learning the basics and seeing the power behind the DirectX world, it’s time to think how GDI+ and DirectX fit together and how to choose either one (or both) as a basic technology for a game project.

    In a general way, you can say that GDI+:

    • Is a technology to draw 2-D graphics

    • Is the “native” library for working in Windows

    • Is more easily ported to other devices (like Pocket PC)

    • Won’t use any extended graphics or acceleration features, even when there’s a hardware accelerator present

    • Is easy to work with

         And you can say that DirectX:

    • Is mainly aimed at working with 3-D graphics, but has many features that can be used in 2-D graphics

    • Has special requirements to run games (needs installation)

    • Is more easily ported to the Xbox console

    • Can use all the power of graphics acceleration devices

    • Needs a little more effort to get started

    Summary

    In many situations, choosing DirectX is a must, such as when you are coding a 3-D game engine, or when you want to code a fast-paced action game that will need to use hardware acceleration features. But there are other situations in which using GDI+ is perfectly fine. Let’s see some examples.

    Imagine again that you are coding a Sid Meyer’s Civilization I clone. Is there really a need to use DirectX? Remember that, although many people have 3-D boards nowadays, not everyone has one, and creating a game that doesn’t require such hardware will broaden your game audience. And because a game like this isn’t graphics intensive (the graphics aren’t very sophisticated, and the frame rate isn’t a problem), it’ll be better to center your efforts on creating more sophisticated algorithms, which can run faster and make better gameplay. No gamer likes to wait for the computer to “think” about a better move.

    When talking about simpler games, like Minesweeper or Solitaire, there’s no need at all to use DirectX. A simpler solution, besides providing the benefits explained in the previous paragraph, will lead to a game that is easier to debug and easier to maintain, maybe resulting in a more sophisticated version.

    Even when talking about arcade games, when you deal with games with few animations (Breakout-like games are a good example), you can stay with GDI+ without fear of choosing the wrong platform.

    Simply put, GDI+ is good for many kinds of games, but DirectX has an incredible number of benefits if you’re willing to invest a little more time in development. So before starting any new game project, think carefully about which platform is the best for your goals.

    And let’s highlight an important point: You can use both techniques in a game. All you need to do is isolate the GDI+ code from the DirectX code by not using any GDI+ code between the BeginScene and EndScene methods. The better approach is to create a separate function for any GDI+ code, which will be called after the call to the Render procedure.

    Acknowledgments

    The walking man drawings used in this chapter were made by Igor Sinkovec, a graphic artist and game programmer. 

    This chapter is from Beginning .NET Game Programming in C# by David Weller et. al.(Apress, 2004, ISBN: 1590593197). Check it out at your favorite bookstore today. Buy this book now.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    ASP.NET ARTICLES

    - Adding Content to a Static ASP.NET Website
    - Building a Static ASP.NET Website in a Basic...
    - Develop Your First ASP.NET Website with Visu...
    - Run ASP.NET in Windows XP Home with Cassini ...
    - How to Test a Web Application
    - How to Add Code and Validation Controls to a...
    - Working in Source and Split Views to Build a...
    - How to Build a Web Form for a One-Page Web A...
    - How to Develop a One-Page Web Application
    - An ASP.NET Web Application in Action
    - Developing ASP.NET Web Applications
    - An Introduction to ASP.NET Web Programming
    - Introduction to the ADO.NET Entity Framework...
    - Completing an In-Text Advertising System und...
    - Programming an In-Text Advertising System un...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek