Mobile Apps
i-phone Apps
Java

3D game Programming

 

The Draw method encompasses all the rendering that is required for the UI screen. Before any drawing is actually done, the BeginSprite method from the base class is called. In this case, simply calling the Begin method on the renderSprite object itself would have given us the same behavior, but encapsulating the call allows us to easily change the behavior later and let all subsequent derived classes get this new, improved behavior.
The base class was designed to handle rendering the background, so to render that, the Draw method is called on the base class. After that, it is a simple matter to call the Draw method on each of the buttons and mark the sprite as finished rendering.
The actual event-handler methods are amazingly simple. Depending on which button is clicked, you fire the appropriate event. However, if you remember from Chapter 5, "Finishing Up the Support Code," when you were designing the button, the only way to get the button to fire its click event was to call the OnMouseClick method on the button. On top of that, you needed to call the OnMouseMove method for the button to highlight correctly when the mouse moved over it. You also want the user to be able to control the UI without using the mouse. To handle these cases, add the three methods in Listing 6.4 to your class.
Listing 6.4. Handling User Input on Screens

///
/// Update the buttons if the mouse is over it
///
public void OnMouseMove(int x, int y)
{
newButton.OnMouseMove(x, y);
exitButton.OnMouseMove(x, y);
}
///
/// See if the user clicked the buttons
///
public void OnMouseClick(int x, int y)
{
newButton.OnMouseClick(x, y);
exitButton.OnMouseClick(x, y);
}
///
/// Called when a key is pressed
///
public void OnKeyPress(System.Windows.Forms.Keys key){
switch(key)
{
case Keys.Escape:
// Escape is the same as pressing quit
OnExitButton(this, EventArgs.Empty);
break;
case Keys.Enter:
// Enter is the same as starting a new game
OnNewButton(this, EventArgs.Empty);
break;
}
}

Nothing overly complicated here: you simply add new public methods to your UI screen that mirror the methods the buttons have. When they are called, you simply pass the data on to the buttons and allow them to do the work they need to do. The keyboard keys are a different matter, though. In this case, when the user presses a key, the code checks whether it is Esc, which is the same as pressing the Quit button, or Enter, which is the same as pressing the New Game button. This code allows the user to navigate the game without ever needing the mouse.

Plugging into the Game Engine

That's all there is to the main menu screen for your new game. Now all you'll need to do is plug it in to your game engine, and you'll be well on your way to having a fully functional game. Go back to your game engine class, and add the following two variables to your class:

// Is the main menu currently being shown
private bool isMainMenuShowing = true;
// The main UI screen
private MainUiScreen mainScreen = null;

These control the main menu UI screen, as well as determine whether this screen is currently being shown. Because the first thing you see when you start the game is the main menu, obviously you want this Boolean variable to default to true, as it does here. Now you actually create an instance of the main menu UI screen, which you do in the OnCreateDevice method. Add the following code to the end of that method:

// Create the main UI Screen
mainScreen = new MainUiScreen(device, desc.Width, desc.Height);
mainScreen.NewGame += new EventHandler(OnNewGame);
mainScreen.Quit += new EventHandler(OnMainQuit);

You won't be able to compile yet because the event-handler methods haven't been declared yet. For now, you can skip them because you will handle them in a few moments. First, because you've created your main menu screen, you want to ensure that it gets cleaned up when the application shuts down. In the OnDestroyDevice method for your game engine, add the following to the end of the method:

if (mainScreen != null)
{
mainScreen.Dispose();
}

This code ensures that the textures and sprite used for rendering this UI screen are cleaned up properly. Speaking of rendering, you probably want to ensure that your screen will get rendered as well. For now, go ahead and remove the code that you used to render the sky box from your OnFrameRender method (don't worry, you'll replace it later) because you don't want the sky box to be rendered while the main menu is being displayed. Replace your OnFrameRender method with the one in Listing 6.5.
Listing 6.5. Rendering Your Main Menu

public void OnFrameRender(Device device, double appTime, float elapsedTime)
{
bool beginSceneCalled = false;

// Clear the render target and the zbuffer
device.Clear(ClearFlags.ZBuffer | ClearFlags.Target, 0, 1.0f, 0);
try
{
device.BeginScene();
beginSceneCalled = true;

// Decide what to render here
if (isMainMenuShowing)
{
mainScreen.Draw();
}
#if (DEBUG)
// Show debug stats (in debug mode)
debugFont.DrawText(null, sampleFramework.FrameStats,
new System.Drawing.Rectangle(2,0,0,0),
DrawTextFormat.NoClip, unchecked((int)0xffffff00));
debugFont.DrawText(null, sampleFramework.DeviceStats,
new System.Drawing.Rectangle(2,15,0,0),
DrawTextFormat.NoClip, unchecked((int)0xffffff00));
#endif
}
finally
{
if (beginSceneCalled)
device.EndScene();
}
}

Obviously, your rendering method got a lot less complex with that. Because the UI screen is encapsulated so well, it's only a single method call to ensure that everything gets rendered correctly. You're not entirely finished yet, though. Remember, for the buttons to work correctly, you need to call into the mouse and keyboard methods of the UI screen. You have hooked the mouse and keyboard user input callbacks from the sample framework. Go ahead and add the code from Listing 6.6 to them now to call into the appropriate UI screen.
Listing 6.6. Handling User Input

private void OnMouseEvent(bool leftDown, bool rightDown, bool middleDown,
bool side1Down, bool side2Down, int wheel, int x, int y)
{
if (!leftDown)
{
if (isMainMenuShowing)
{
mainScreen.OnMouseMove(x, y);
}
}
else if (leftDown)
{
if (isMainMenuShowing)
{
mainScreen.OnMouseClick(x, y);
}
}
}
/// Handle keyboard strokes
private void OnKeyEvent(System.Windows.Forms.Keys key,
bool keyDown, bool altDown)
{
// Only do this when it's down
if (keyDown)
{
if (isMainMenuShowing)
{
mainScreen.OnKeyPress(key);
}
}
}

These methods are automatically called by the sample framework whenever the appropriate event occurs. For example, as you move the mouse cursor across the screen, the OnMouseEvent method is called; similarly, the OnKeyEvent method is called if you press a keyboard button. You've now implemented the main menu screen, except for the two event handlers you need to handle the button clicks on the screen. Go ahead and add those now:

private void OnNewGame(object sender, EventArgs e)
{
}
private void OnMainQuit(object sender, EventArgs e)
{
sampleFramework.CloseWindow();
}

 
 

Our Courses

virtualinfocom on Facebook
animation courses, Professional Animation Courses, 2D Animation, Internet, Cd-Presentation, Web development, Web Design, Multimedia Animation , Best Animation Academy India, Animation center india , Animation institute india , Animation training india, Animation classes india , Animations Classes India, Animation courses india , Animation training india , Animation School, Animation colleges , Animation college india , Animation university india, Animation degree india, 2d animation training india, 2d animation institute india, 2d animation courses india, Animation programs india , game development india, game design institute india, game design gaming india, gamedesign institute kolkata, gaming class india, gaming training india, Best web design Academy India, web design center india , web design School, web design institute india , Animation classes india , Animations Classes India, Animation courses india ,Animation training india , Animation college india , Animation university india, web design india, web design training india, 2d animation institute india, 2d animation courses india, web design institute india, Animation degress india , Animation training india, Best animation institute in India, Electronics and Embedded training at Kolkata, Hands on Training on Industrial Electronics and Embedded systems, For ITI, Diploma and Degree Engineering students, Real time projects and Industry interaction, Final year project guidance, Electronics and Embedded training at Kolkata, 1st Electronics and Embedded training in Eastern India
Designed by virtualinfocom