Showing posts with label Code Snippets. Show all posts
Showing posts with label Code Snippets. Show all posts

Wednesday, February 21, 2007

GAMEY LITTLE HACKER'S 'RPG ZERO'

On previous posts I commented that GLH had published some handy source code and tips for data-driven games, in particular, RPG ones.

Well, GLH is back with an awesome demo video of a starting RPG project:

From GLH's site: "When I started writing about game programming here last month, I really did want to practice a little altruism and share some stuff with the community. I'm afraid the vanity levels on this project however, make it wholly unsuitable to share as good practice. The sheer self indulgence in the code should warn people away. I really can't in good conscience recommend anyone learn anything from this... the scripting language is an own rolled, interpreted, prefix notated, reflection driven nightmare for pete's sake!

I would like to share though. So if anyone is interested in watching, I'll post the goings on in my RPG land."

So, what do you think? Should we be interested?

C'mon! Pay a visit to this site and let GLH know that you are interested ... I did ... ;)

Wednesday, January 31, 2007

TUTORIAL: A DATA DRIVEN SAMPLE ("SHOOTER ZERO")

Continuing with the "XNA Community Pimpage" tradition, I recommend reading this post on Gamey Little Hacker's Blog about data-driven game architecture and implementation.

Not only is the second part of the series on the matter but also you will find an interesting demo sample code which will bring some light for those of us who need tips and examples (and I say "of us" because I'm starting to deal with the concept of "Entity" within my engine so this example comes to me in the proper moment).

From GLH's blog: "Questions are welcome, and if there's enough interest I could write a few more posts that break out the details."

All in favor of more posts just raise your hands, or ... well, say, let GLH know that you're interested in more posts on this regard ... I am ...

Tuesday, January 30, 2007

TUTORIAL: XNA COLLISION DETECTION FOR 3D MODELS (PARTS 2 & 3)

Sharky's has published parts 2 and 3 of his formerly 2-part tutorial series called "XNA Collision Detection for 3D models".


For those of you who didn't read part 1, please go and check it now!

Also there's some sample code you can download and read through the code.

Enjoy!

Friday, January 26, 2007

OOB INTERSECTION TEST USING XNA

As we wait for the second part of Sharky's tutorial "XNA Collision Detection for 3D models", there is an interesting source code to study, which was programmed and uploaded by Minh at Channel 9 site.

Too many links for one sentence, don't you think?

Anyway, just dowload the code, build the assembly and play around with the test demo to see how the test performs.

See ya!

Friday, December 29, 2006

PROBLEMS WITH FULLSCREEN MODE IN XNA?

Some members of the XNA community (including me) have experienced problems with XNA games when running on fullscreen mode -in particular, I have a GeForce 6600 GT card.

The problem: when a XNA game tries to get/set the presentation parameters for accepted fullscreen modes, it takes the list of rates accepted by the gfx card and in some cases uses the higher one -for instance, like 240 Hz on 800x600. Phewww! So you will see nothing more than a black screen and your monitor trying to warn you that something is attempting to set an invalid refresh rate.

The workaround: if the user forgot to (or cannot) forbid any rate above the maximum accepted vertical retrace on the gfx card, just catch the "PreparingDeviceSettings" event and then set a common rate like 60 or 72 Hz. How? In the class that derives from "Microsoft.Xna.Framework.Game" class, add the following line (say, within the constructor):

graphics.PreparingDeviceSettings += new EventHandler<PreparingDeviceSettingsEventArgs>(MyMethod); // "graphics" is a reference previously instanced as "GraphicsDeviceManager(this)" usually in the same constructor

Then add the following method in the same class:

private void MyMethod (object sender, PreparingDeviceSettingsEventArgs args)
{
args.GraphicsDeviceInformation.PresentationParameters.FullScreenRefreshRateInHz = 60; // for instance, or 70, 72, 75, etc.
}

I've modified the code of games like Rocket Commander and Wildboarders and it all works great!

Let's hope Sharky modifies Air Legends soon so that I can finally play the game.

Enjoy!

Wednesday, September 13, 2006

FRAMERATE COMPONENT FOR XNA

Just a quick note: I have posted the source code for a simple component that calculates and display the framerate of your XNA-game project.

Considerations:

  • You might change the namespace in line to your game's namespace.
  • Currently, the game window's title will be used to display the framerate (this will change when XNA supports fonts). As a workaround, you can use garykac's bitmap fonts example to modify this behavior.
  • If "Enabled" is set to false, framerates are not calculated (Current is set to 0).
  • If "Visible" is set to false, the current framerate value is not displayed (but yet it can be calculated if enabled).
  • When you want to show the decimal part of the framerate value (that is, "ShowDecimals" is set to true), you can set the display format to the fixed one (like "0.00").
  • You can also set whether the time step must be fixed or not.
  • You are free to modify/optimize the source code as desired. Please share your optimizations with the community.

Enjoy!!!

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Components;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;

namespace WindowsGame1
{
/// <summary>
/// Simple class to calculate the frame-per-seconds rate of your game.
/// </summary>
public sealed partial class Framerate : GameComponent
{
#region Instance Fields

private float deltaFPSTime;
private double currentFramerate;
private string windowTitle, displayFormat;
private bool enabled, visible, canDraw, showDecimals;

#endregion

#region Instance Properties

/// <summary>
/// Gets the current framerate.
/// </summary>
/// <remarks>
/// The 'Enabled' property must have been set to true to retrieve values greater than zero.
/// </remarks>
public double Current
{
get{ return this.currentFramerate; }

}

/// <summary>
///
Gets or Sets a value to enable framerate calculation.
/// </summary>
public bool
Enabled
{
get { return this.enabled; }
set
{
this.enabled = value;
this
.currentFramerate = 0;

if (this.Game != null && this.windowTitle != null && !this
.enabled)
this.Game.Window.Title = this.windowTitle;
}
}

/// <summary>
///
Gets or Sets a value to display framerate on screen.
/// </summary>
/// <remarks>
///
Currently, the framerate is shown in the window's title of the game.
/// </remarks>
public bool Visible{

get { return this.visible; }
set
{
this.visible = value;

if (this.Game != null && this.windowTitle != null && !this
.visible)
this.Game.Window.Title = this.windowTitle;
}
}

/// <summary>
///
Gets or sets a value indicating whether the time step must be fixed or not.
/// </summary>
/// <remarks>
///
If set to true, the game will target the desired constant framerate set in your main class ('Game1', by default).
/// </remarks>
public bool
IsFixedTimeStep
{
get { return this.Game.IsFixedTimeStep; }
set
{
if(this.Game != null)
this.Game.IsFixedTimeStep = value;
}
}

/// <summary>
///
Gets or sets a value indicating whether the framerate will display decimals on screen or not.
/// </summary>
public bool
ShowDecimals
{
get { return this.showDecimals; }
set { this.showDecimals = value; }
}

/// <summary>
///
Gets or sets a value indicating whether the decimal part of the framerate value must be display as fixed format (or as double format, otherwise).
/// </summary>
/// <remarks>
///
The 'ShowDecimals' property must be set to true in order to set the proper format.
/// </remarks>
public bool
FixedFormatDisplay
{
get { return this.displayFormat == "F"; }
set { this.displayFormat = value == true ? "F" : "R"; }
}

#endregion

#region Constructors

/// <summary>
/// Parameterless constructor for this class.

/// </summary>
public
Framerate()
{
InitializeComponent();
}

#endregion

#region Instance Methods

/// <summary>
/// Called after game initialization but before the first frame of the game.
/// </summary>
public override void Start()
{
this
.canDraw = false
;
this.currentFramerate = 0;

this.windowTitle = this.Game != null ? this.Game.Window.Title : String.Empty;
}

/// <summary>
///
Called when the gamecomponent needs to be updated.
/// </summary>
public override void
Update()
{
if (this.enabled)
{
// The time since Update() method was last called.
float elapsed = (float)this
.Game.ElapsedTime.TotalMilliseconds;

// Ads the elapsed time to the cumulative delta time.
this
.deltaFPSTime += elapsed;

// If delta time is greater than a second: (a) the framerate is calculated, (b) it is marked to be drawn, and (c) the delta time is adjusted, accordingly.
if (this.deltaFPSTime > 1000)
{
this.currentFramerate = 1000 / elapsed;
this.deltaFPSTime -= 1000;
this.canDraw = true;
}
}
}

/// <summary>

///
Called when the gamecomponent needs to be drawn.
/// </summary>
/// <remarks>
///
Currently, the framerate is shown in the window's title of the game.
/// </remarks>
public override void
Draw()
{
// If the framerate can be drawn, it is shown in the window's title of
the game.

if (this.visible && this.canDraw)
{
string currentFramerateString = this.showDecimals ? this.currentFramerate.ToString(this.displayFormat) : ((int)this.currentFramerate).ToString("D");

this.Game.Window.Title = "FPS: " + currentFramerateString;

this.canDraw = false;
}
}

#endregion
}
}

[We will discuss and focus on XNA GSE later.]