Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
The SoundEffect class represents a sound effect that can be played at any time. The FlatRedBall Editor supports the automatic creation of SoundEffect objects by adding a WAV file to a Screen, Entity, or Global Content. For reference on the SoundEffect object, see the MonoGame SoundEffect page. For more information on using SoundEffect through the FlatRedBall Editor, see the tutorial page on playing sounds.
The following example assumes that you have "mySound.wav" added to your content project. If not using Glue, you will need to pass the .WAV file through the MonoGame content pipeline. Glue will automatically process .WAV files added to Screens, Entities, and Global Content Files.
The SoundEffectInstance is a copy of a SoundEffect which can be played on its own. Each SoundEffectInstance can only be played once at a time. Therefore if two sounds must overlap, then two SoundEffectInstances are needed. By contrast, calling SoundEffect.Play multiple times will result in overlapping sounds. Although the SoundEffectInstance requires multiple instances for simultaneous plays, it provides the number of benefits:
It can be stopped at any time
It provides parameters for modifying how the sound effect is played (such as pitch)
You can inspect if a SoundEffectInstance is playing
For information on using SoundEffectInstances in the FlatRedBall Editor, see the FlatRedBall Editor's SoundEffectInstance page. For a more detail on the SoundEffectInstance class, see MonoGame's SoundEffectInstance reference.
If you are running the iOS simulator on a Mac then you may experience an error at runtime. A work-around for this error has not yet been discovered but it is occurs in MonoGame and not FlatRedBall. More information on the issue can be found here: http://community.monogame.net/t/error-while-playing-soundeffect/1605.
Vector3 is used throughout FlatRedBall to store position, velocity, and acceleration values. Also, the Vector3 struct is used in many places to perform common math operations such as to determine the distance or angle between two objects. Vector3 is located in the Microsoft.Xna.Framework namespace and is a native part of MonoGame. FlatRedBall offers additional extension methods to the Vector3 type through the Vector3ExtensionMethods class. To use this, add the following using statement to your code:
XNA's Matrix class. FlatRedBall uses Matrix in for the Rotation property.
The Color struct is a common struct used in FlatRedBall to control the color of objects. To use the Color class, add the following using statement:
Base class for class used in default XNA Template and FlatRedBall XNA Template.
The Game class holds many useful methods and properties which are often needed deep inside game logic code where the Game reference is not available. The FlatRedBallServices class provides a reference to the game class:
For example, to view the mouse cursor (as shown below) outside of the game class, you would use the following code:
Since FlatRedBallServices is static this can be used anywhere in code.
The resolution of FlatRedBall is controlled through the GraphicsOptions object. For more informatin, see the GraphicsOptions page.
FlatRedBall will attempt to react to the window changing as a result of the user grabbing and dragging the corners or edges. The following code can be called in the Game class to enable resizing:
FlatRedBall applications can run in full screen mode. The following code (which must be called after FlatRedBall is initialized) will set the FlatRedBall application to run at full screen.
When FlatRedBall is in fullscreen mode, it "owns" the graphics device. This means that nothing else will be drawing underneath the Window. This can improve performance, but also makes alt-tabbing slower, and now other Windows can be drawn on top of the Window. For this code to work, you must include a reference to the System.Windows.Forms library in your project's References folder. You can sacrifice performance for this convenience by simply maximizing the game as follows: Add the following using statement:
Add the following to Initialize after initializing FlatRedBall:
The Exit method ends the application. This can be called to exit the game as follows: If inside the Game class:
If the Game class is not in scope:
This can be called at any point and your game will exit properly.
For more information, see the IsFixedTimeStep page.
Game Window as a Control - FlatRedBallService's Owner property returns the game window as a Control which provides more functionality.
Did this article leave any questions unanswered? Post any question in our forums for a rapid response.
IsMouseVisible can be set to true to display the mouse cursor. This value defaults to false which means the mouse will be invisible when it is over the game window.
The following code shows how to make the mouse cursor visible. It assumes the code is written in Game1.cs, so it uses "this":
If not in the Game1.cs, this can be accessed through FlatRedBallServices:
The IsActive property can tell you if the Game window has focus. This is useful if you are building applications or games which you do not want to receive input if the window is not in focus. For example, if a Game is behind another Window, but the game responds to mouse clicks, the game will receive the mouse clicks by default. To prevent this, an IsActive check can be made.
The follow code can prevent a particular method from running by performing an "early out" on the code.
The IsFixedTimeStep property controls whether the game attempts to run at a fixed frame rate. If the game runs especially slow, then the frame rate may be reduced or frames may be dropped (this may vary from platform to platform). This property is true by default. If your game is capable of running at a frame rate faster than specified by the TargetElapedTime then the Game class will delay calling Update and Draw to attempt to match the TargetElapsedTime. If your game is not able to run as quickly, the Game class will reduce the number of Draw calls to attempt to allow the Update call to be called more frequently.
By default the XNA template attempts to run the game at a fixed frame rate. To accomplish this, it may reduce the number of times its Draw method is called so that it can keep its Update method called at a fixed frequency. You can turn this behavior off if you are interested in measuring the performance of your game without any artificial throttling. The following code will enable the game to run as fast as possible rather than being capped to 60 fps or the refresh rate of the monitor: Add the following to the Game Constructor
If you are going to change graphics.SynchronizeWithVerticalRetrace after FlatRedBall is initialized, then after changing the property you must call
If IsFixedTimeStep is false, then it is possible that your game may run at a very high frame rate. If your game's frame rate is sufficiently high, it may exceed the sampling rate of the mouse on the PC. In this situation, you may receive updates where multiple frames in a row report the same position values. This can be problematic if you are using any of the Cursor's Velocity values. You may have many frames where the reported velocity is 0 (due to the position being the same for mutliple frames) even if the user is moving the cursor. This is mostly problematic in situations where the Velocity of the Cursor can impact game behavior - such as with logic that performs kinetic scrolling.
AllowUserResizing controls whether the user can resize the game window by dragging the edges or corners. This value is false by default.
The TargetElapsedTime member allows you to control the update frame rate of your game. This value controls how often the Update method is called. It does not control how frequently your game is rendered to the screen.. In other words, if your game is rendering at 60 frames per second, but you modify this value so that your game runs at 120 frames per second, that will result in the Game's Update call being executed 120 frames per second, but your game will still render at 60 frames per second. In short, your game's Update and Draw methods can run at different rates.
The following code will tell the game logic to run at 10 frames per second:
As explained in this article, tunneling occurs when the speed of a moving object is large relative to objects it is colliding against. More accurately, tunneling occurs when the amount of distance an object travels in one frame is large relative to the objects it is colliding against. By increasing the frame rate of your game, you can reduce the distance covered by an object per-frame. The following example presents a situation where a lot of tunneling is occurring, then shows how increasing the frame rate can solve this problem:
Add the following using statements:
Add the following at class scope:
Add the following to Initialize after initializing FlatRedBall:
Add the following to Update:
While adjusting this value may seem like an effective way to solve tunneling, doing so can be dangerous. The reason for this is because reducing the TargetElapsedTime makes the game attempt to run at a higher frame rate. This means your entire update logic (all live Screens and Entities and FlatRedBall objects) will be run more frequently.
The default value for TargetElapsedTime is approximately 16 milliseconds (60 frames per second). Reducing it to 2 as we do above essentially makes your game require 8X as much processing power per frame. Or another way to look at it is that your game is 1/8 as efficient per draw call (excluding the draw calls themselves). As you can probably imagine, this is a very expensive approach to solving tunneling. But we present it here as it may be a requirement if your game needs a high degree of accuracy in its physics.
With the frame rate adjustment code commented (not used): With the frame rate adjustment code used:
ClientSizeChanged is an event which is raised whenever the window is resized. Unfortunately this event is only raised when the window is resized by dragging the corners/edges, or when the window is maximized. Once the window is maximized, clicking the maximize button to restore the window will not raise this event. Therefore, it is recommended that Windows Desktop games the CornerGrabbingResize event. For more information on this event, see the this page.
The GraphicsDevice is the object which FlatRedBall interacts with. Since FlatRedBall does rendering automatically, most users will not need to interact with this object.
You may want to work with the GraphicsDevice in the following scenarios:
When using DrawableBatches.
When using render targets.
Microsoft.Xna.Framework.Graphics.GraphicsDevice.SetRenderTarget
Did this article leave any questions unanswered? Post any question in our forums for a rapid response.
The GameWindow class represents the window that the game is rendering at. This is typically accessed through the XNA Game's Window property.
The resolution of the game window can depend on the presence of splash screen files. For more information, see this discussion: http://community.monogame.net/t/not-correct-bounds-size-on-iphone5-always-480x320/591
The RenderState contains a variety of settings which can change now rendering is performed. FlatRedBall automatically sets RenderState values appropriately when drawing different types of objects, but users may need to modify RenderStates when using DrawableBatches.
The SetRenderTarget function can be used to specify whether rendering will happen on a RenderTarget (if a non-null value is passed) or directly to the screen (if null is passed). By default no render target is set, which means all rendering will appear on-screen. Setting render targets can be used for a number of reasons:
To use a screen shot of the game at a later time
To save a screen shot to disk
To perform post processing or screen distortion
Note that SetRenderTarget must be called prior to performing any rendering. Typically this means setting the render target and either:
Using FlatRedBallServices.Draw to render everything currently in FlatRedBall to the RenderTarget
Explicitly calling Renderer.DrawCamera to render a camera to the render target.
Rendering to a RenderTarget enables you to use the resulting RenderTarget as a Texture2D, which can then be re-rendered to the screen. This can be done for a number of reasons:
To perform post-processing (modifications on the scene after it has been rendered such as bloom or blur)
To render to a portion of the screen then scale it up to make your game run more efficiently
Add the following at Game1's Class scope:
Add the following to Game1's Initialize:
Add the following to Game1's Draw:
This example shows how to render to a render target, then how to save it to disk when pressing the space bar:
Add the at class scope:
Add the following to CustomInitialize:
Add the following to Draw:
Alternatively, RenderTarget rendering in FlatRedBall can be performed using Layers with RenderTargets. For more information, see the page.
Xna's Model class is the class that's used to draw 3D geometry. Usually in FlatRedBall this object is contained within a PositionedModel and is accessed by the PositionedModel's XnaModel property.
To set individual effects on a model, set the effects within the model's meshparts, like so:
Note that if you use the same effect in multiple places, the parameters may be changed on every use. You may want to Clone() the effect after loading it to ensure it is unique, then set the parameters.
Models store the Texture that they reference in their effect. Therefore, the Texture used on individual mesh parts can be retrieved and set through effects.
Did this article leave any questions unanswered? Post any question in our forums for a rapid response.
SpriteBatch allows games to render 2D graphics to screen. Unlike FlatRedBall's Sprite class, SpriteBatch performs immediate rendering - drawing with SpriteBatch results in graphics appearing for only one frame. To keep SpriteBatch graphics on screen for an extended amount of time, Draw must be called every frame. By contrast, FlatRedBall Sprites can be created one time and they will continue to appear on screen until removed from the engine. Three calls are required to render graphics using SpriteBatch:
SpriteBatch.Begin - sets initial values to be used for all Draw calls until End is called
SpriteBatch.Draw - draws a single sprite to the screen. Draw can be called multiple times between Begin and End, and performing multiple Draw calls between a Begin and End call can improve performance when compared to repeatedly calling Begin and End.
SpriteBatch.End - ends the drawing and presents the visuals on-screen or on the current RenderTarget2D.
The SpriteBatch.Draw method is used to render a Texture2D to screen. The Texture2D used can come from anywhere, including Texture2D instances loaded through Glue. The following code shows how to draw an image on screen. FINISH HERE after Entities support IDrawableBatch
As of XNA4 shared parameters are no longer set by the game engine.
Did this article leave any questions unanswered? Post any question in our forums for a rapid response.
SupportedOrientation controls available orientations on the app. This property is only used on devices which can be used in portrait and landscape modes (not available on desktop platforms, Xbox One).
The following code can be used to set the game to run in portrait mode:
Note that additional code is required for each platform, as shown below.
SupportedOrientations property must be assigned in the Game's constructor, after the graphics instance has been initialized. Assigning the orientation later, such as in initialize methods, may result in unexpected behavior.
Setting the orientation through SupportedOrientation also requires additional changes per-platform.
The orientations set on SupportedOrientation should match the orientations set in Activity1.cs on the Activity attribute, as shown in the following code:
The ReferenceAlpha is a value that is used to test whether a pixel should be drawn according to its alpha value. The default value for this is 0. This is used in combination with the AlphaFunction property.
The following code results in all pixels being drawn:
In the code above, any alpha value greater than 0 will be drawn. In the following code, only alpha values greater than 128 (out of 255) will be drawn:
FlatRedBall XNA uses an alpha range of 0 to 1 (inclusive). This gets converted to a 0 to 255 range, so you may need to perform some math to convert between the two systems.
The RenderState contains a variety of settings which can change now rendering is performed. FlatRedBall automatically sets RenderState values appropriately when drawing different types of objects, but users may need to modify RenderStates when using DrawableBatches.
A Texture2D is a 2D array of pixel information which can be used by Sprites, Text objects, and PositionedModels when drawn. All FlatRedBall platforms support textures up to a max size of 2048x2048. FlatRedBall PC uses the Reach profile by default, but if switched to HiDef, textures up to 4096x4096 can be used.
A Texture2D by itself cannot be displayed in FlatRedBall. The Texutre2D must be drawn by a FlatRedBall object. The most common object for displaying Texture2Ds are Sprite. The following code shows how to render a Texture2D using a Sprite.
The following code loads a Texture2D from a file: Add the following using statement:
Call the following sometime after FlatRedBall is initialized
For more information on the Texture2D class, see the MSDN entry.
Each pixel in a Texture2D can be fully opaque, completely transparent, or have partial transparency. There are two ways to set the transparency of pixels in a Texture:
Save your image in a format that supports transparency, such as .tga or .png. When you save your image, make sure that you have transparent pixels set as transparent in the program that you are using to create or modify the image.
Use a color key for your image. You can specify a color that should be transparent when the engine creates a Texture2D out of it. The default value is black. For more information on a color key, see the TextureLoadingColorKey page.
The Bitmap object is an object commonly used in Windows programming. It is an object that represents an image loaded in RAM. It does not necessarily need to come from a .bmp image. Its flexibility can at times make it a useful "middle-man" for loading Texture2Ds. Assuming myBitmap is a valid Bitmap
When a texture is loaded into FlatRedBall, the source image width and height should be a power of two. Non-power of two image can be loaded, but they may be up-scaled which can result in blurry textures and wasted memory. For a list of power of two values, see this table.
Filtering - Explains why textures become blurry when displayed larger than actual size.
FlatRedBall.FlatRedBallServices.AddDisposable - Can be used to add a Texture2D to a FlatRedBall Content Manager.
Did this article leave any questions unanswered? Post any question in our forums for a rapid response.
The easiest way to create a Texture2D is to load a graphical file and create a Texture2D using FlatRedBallServices as follows:
While this code addresses many common scenarios, you may want to create your own textures programatically. This section discusses various topics on how to create Texture2Ds in code.
The following code creates a Texture2D which is all red, then displays the Texture2D using a Sprite.
Add the following using statement:
The MediaPlayer class is an XNA class which controls the playing of music. For full documentation, see Microsoft's MediaPlayer reference page.
To have your game loop its music, add the following line of code to your Game1.cs file:
The MediaPlayer offers an overall volume value. This volume value applies to all music. In other words, if you set this value, then change the song that is being played, the same volume value will apply to the new song.
This makes Music play at full volume:
This makes Music play at half volume:
This makes Music play at 0 volume (effectively mutes the music):
Did this article leave any questions unanswered? Post any question in our forums for a rapid response.
The Song class can be used to play music in a game. The easiest way to play a song is through Glue, which requires no code. For more information on Songs in Glue, see this page.
WAV files are not supported in FlatRedBall for music files. Instead, they are used by the SoundEffect and SoundEffectInstance classes.
To play a song in Glue, drag+drop a file from disk into your Glue project on a Screen. It will play automatically when the screen loads. For more information about playing songs in Glue, see the Glue MP3 page.
To play a song:
Add the following code:
Add the following using statements:
Add the following to Initialize after initializing FlatRedBall:
This code uses the Content Pipeline: Notice that when this file is loaded you do not include the extension. This is because the file needs to be added to your project using the Content Pipeline. For more information on what the Content Pipeline is, see this link.
This error can occur in a number of cases:
If you're using a WMA/MP3 with DRM
If Windows Media Player is not installed on the computer running the game
Open Control Panel
Select Programs and Features
Turn windows features on or off
Expand Media Features
Make sure Windows Media Player is selected
OGG
This is dependent on the Android phone. Just because it plays on one phone doesn't mean it will play on another. If the phone doesn't have the codec, it will return a
Did this article leave any questions unanswered? Post any question in our forums for a rapid response.
Drag a music file (MP3 or WMA) into your project's Content folder through Visual Studio.
If attempting to play an .ogg file on the PC, you need to have the proper "DirectShow Filters" installed. To verify if you have them installed, attempt to play the .ogg file in Windows Media Player. If Windows Media Player does not recognize the file then you need the filters: If the song does not play, you can install the DirectShow Filders, which can be found here.
Format
PC Desktop
Android
MP3
X
X
WMA
X
OGG
X
Introduction The GraphicsDeviceManager class provides high-level methods, properties, and events related to MonoGame's graphics device. It is automatically created in the Game1 class.
The FromUri method games to create songs from a regular song file (such as .mp3) rather than relying on the Content Pipeline. If you are simply interested in playing a song, the easiest way to load and play a song is through Glue, as explained on this page. The FromUri method requires writing code.
The FromUri method provides the following benefits:
Does not require the usage of the Content Pipeline when loading songs in code.
Does not require adding content to the Visual Studio project, which is important if your project does not ship with all of the content it will use. Games which allow downloading DLC or modding will want to use the FromUri method. Furthermore, audio may be downloaded after a game is installed on Android to fit within the current 50 megabyte .apk limit.
Using the FromUri code requires loading a Song in custom code (as oppose to using Glue code to load the file). Song files which are loaded with FromUri can be added as follows:
Glue files can be loaded, but they must have their LoadedAtRuntime value set to false. For more information, see the LoadedAtRuntime page.
Files can be added manually to the Visual Studio/Xamarin Studio project manually.
Files can be added by the project itself, such as by unzipping a file or downloading a file from the Internet.
Once the file is available by the project, it can be loaded as follows:
The FromUri method as implemented in MonoGame 3.4 includes a bug requiring a workaround: