IT tutorials
 
Mobile
 

XNA Game Studio 3.0 : Making a Prettier Clock with 3-D Text (part 1)

11/5/2011 3:43:34 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
1. Drawing Multiple Text Strings

One way to make the display more interesting is to draw different-colored versions of the text at slightly different positions on the screen:

protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

DateTime nowDateTime = DateTime.Now;
string nowString = nowDateTime.ToLongTimeString();
Vector2 nowVector = new Vector2(50, 400);

spriteBatch.Begin();

spriteBatch.DrawString(font, nowString, nowVector, Color.Red);

nowVector.X = nowVector.X + 4;
nowVector.Y = nowVector.Y + 4;

spriteBatch.DrawString(font, nowString, nowVector, Color.Yellow);

spriteBatch.End();

base.Draw(gameTime);
}

This version of the Draw method is very similar to the original, except that DrawString is now called twice, first drawing in red and then in yellow. In between the draw operations, the values of the X and Y properties of the position vector are increased by 4 using the following statements:

nowVector.X = nowVector.X + 4;
nowVector.Y = nowVector.Y + 4;

Figure 1 shows how this works. The thing on the right side of the "gozzinta" is an expression. This generates a result that is then placed in the destination.

Figure 1. A statement that evaluates an expression and updates the value for a variable


The sequence of instructions that the compiler creates to work out the statement is as follows:

  1. Fetch the value of the X property of nowVector.

  2. Add 4 to it.

  3. Store the value back in the X property of nowVector.

The effect of adding 4 to the X and Y properties is to move the drawing position for the text across and down the screen. Figure 2 shows the result of these changes.

Figure 2. A more interesting time display


From this, you can see that when you draw on the screen, the images are laid on top of each other in the order they are drawn. The red version of the time string is overwritten by the yellow one. The nice thing about this approach is that it gives a good 3-D effect. The human eye interprets the darker color as being in the "background," making the letters appear to pop out of the display. However, the 3-D effect is not quite perfect. The image in Figure 3 is an enlargement of part of the text and shows that the red part is not actually "solid"; instead, it’s simply a layer drawn behind the yellow one.

Figure 3. A zoomed-in detail of the overwritten text


If you want the 3-D effect to be perfect, you need to draw lots more red versions to "fill in the gaps." You could do this by simply copying the code four times, but perhaps you remember reading somewhere that computers are supposed to make life easier, and this doesn’t feel very easy at all. What you really want to do is perform a block of statements a given number of times, and it turns out that C# provides a way to do this: it’s called the for loop construction.

2. Repeating Statements with a for Loop

A program can do three things as it runs. It can perform a single action (a statement), it can make a choice of what to do (a condition statement), or it can repeat something (a loop construction). It might surprise you to learn that with these three programming constructions, you could write any program. You’ve seen how to write statements and conditions; now you need to discover how to create a loop. With a loop, you need to write the drawing instructions only once, and the loop construction then performs them as many times as you like:

spriteBatch.Begin();

int layer;
for (layer = 0; layer < 4; layer++)
{
spriteBatch.DrawString(font, nowString, nowVector, Color.Red);
nowVector.X++;
nowVector.Y++;
}

spriteBatch.DrawString(font, nowString, nowVector, Color.Yellow);

spriteBatch.End();

This code performs four drawing operations with the red color. The code in the block controlled by for is repeated a given number of times. When the loop finishes, the final DrawString puts the yellow version on top of all the red ones. Note that the yellow DrawString is not repeated four times because it is not inside the block of code controlled by the for loop.

The loop itself is controlled by the three items in brackets that follow the key word for. These are shown in Figure 4. Each of the three items is used to manage the behavior of the loop.

Figure 4. How a for loop is constructed


  • Begin This is a statement that is obeyed when the loop starts. In this example, you’re using an integer variable called layer to count each of the layers that you’re drawing, and the loop must set this to zero at the beginning.

  • Test Condition The condition controls when the loop finishes. It can be either true (the loop continues) or false (the loop ends). The condition in your loop is layer < 4. You might not have seen the < operator before; it performs a "less-than" comparison between the two operands. If the item on the left is less than the item on the right, the result of the comparison is true. If the item on the left is not less than the item on the right, the result of the comparison is false. C# provides a range of different comparison operators.

  • Change Each time the statements in the loop are completed, the change is performed. In this case, the change statement layer++ makes the value in layer 1 larger each time. After the change has been performed, the test condition is evaluated to see whether the statements controlled by the loop are to be executed again.

The C# compiler has the job of producing the machine instructions that perform the loop when the program runs. The precise sequence that’s followed by the code that the compiler produces is as follows:

  1. Perform the Begin statement to start the loop.

  2. Perform the Test and finish if the test is false.

  3. Perform the statement in the loop body.

  4. Perform the Change statement.

  5. Return to step 2.

 
Others
 
- Android Views (part 3) - CheckBoxes, RadioButtons, and Spinners
- Android Views (part 2) - Button and ImageButton
- Android Views (part 1) - TextView and EditText
- Java ME on Symbian OS : Networking and General Connection Framework (part 2) - Security Policy for Network Connections & NetworkDemo MIDlet
- Java ME on Symbian OS : Networking and General Connection Framework (part 1)
- jQuery 1.3 : Effects - Basic hide and show
- jQuery 1.3 : Effects - Inline CSS modification
- Mobile Web Apps : Events
- Mobile Web Apps : Setting up Shop
- iPhone 3D Programming : Simplify with glDrawTexOES
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us