This tutorial is a little bit longer and a little bit more involved than the past tutorials. We'll start down the road to separating game logic from game logistics by creating a new project called XNA 2D Engine and moving the "MyGame" class into it. We'll turn the MyGame into a base class to be used with the 2D engine.
We'll introduce the VBContentManager class, my implementation of a ContentManager extended with our "VB Content Pipeline hack" code (there's lots more on that, below). We'll then go on to making a primitive sprite in Photoshop and drawing it to the screen with a SpriteBatch object.
Here is some companion text to go with the video, that describes in detail some code I'll be importing but not spending a lot of time describing in the video: In a standard C# XNA project, the default game class is created for you with a GraphicsDeviceManager and a ContentManager both instantiated and ready to go. As we saw in Tutorial 3, it's fairly easily make our own class that inherits from the base XNA Game class, create our own GraphicsDeviceManager and quickly reproduce what the C# template generates automatically.
What tutorial 3 did not touch on was the ContentManager, because for us VB'ers, the ContentManager is really the point where the lack of VB support in XNA requires us to do a little bit of legwork, so I held off on the subject until now.
In C#, the ContentManager class loads cross-platform file types (like .xnb files) that MSBuild creates automatically from things like .png or .jpg files that get included in the C# project. The primary issue when using a ContentManager with VB is getting these .xnb files created from our .png or .jpg files, because there is no automatic conversion process built into the IDE.
My approach to solving this problem was to borrow largely what's been documented by Alan Phipps, and from that, create my own ContentManager class called the VBContentManager class. Here's is the VBContentManager class, in it's entirety:
Public Fonts As Hashtable(Of SpriteFont) Public Textures As Hashtable(Of Texture2D) Public SubTextures As Hashtable(Of Hashtable(Of Rectangle)) Public Sounds As Hashtable(Of SoundBank)
Private strFakeCSharpProject As String Private strContentRootFolder As String Private strPipelineRootFolder As String Private objContent As List(Of ContentFile) Private intDupeSound, intDupeTexture, intDupeFont AsInteger
Public ContentType As ContentTypes Public File As System.IO.FileInfo Public Name As String
PublicSubNew(ByVal ContentType As ContentTypes, ByVal ContentFile As System.IO.FileInfo, ByVal Name As String) Me.ContentType = ContentType Me.File = ContentFile Me.Name = Name EndSub
EndStructure
''' <summary> ''' Creates a new ContentManager which has been extended with functionality to support a Visual Basic (or other non-C#) project ''' </summary> ''' <param name="ServiceProvider">Your game has a .Services property; send that in here.</param> ''' <param name="ContentRootFolder">The root of your content folder, relative to your projects bin\Debug folder.</param> ''' <remarks></remarks> PublicSubNew(ByVal ServiceProvider As IServiceProvider, ByVal ContentRootFolder As String)
Me.Fonts = New Hashtable(Of SpriteFont) Me.Textures = New Hashtable(Of Texture2D) Me.SubTextures = New Hashtable(Of Hashtable(Of Rectangle)) Me.Sounds = New Hashtable(Of SoundBank)
EndSub
''' <summary> ''' Load all assets that have been compiled by a VBContentManager. ''' </summary> ''' <param name="Compile">Whether or not to have MSBuild compile your game content before attempting to load it</param> ''' <remarks></remarks> PublicSub LoadPipeline(ByVal Compile AsBoolean)
If Compile Then Me.CompileContent() EndIf
ForEach obj As ContentFile InMe.objContent
Try
SelectCase obj.ContentType
'The pipeline will be compiled into either debug or release, depending upon 'which option is chosen in the IDE. We don't want to use the My namespace 'though, because it does not work with the xbox 360.
'msgBox("While loading the pipeline, a piece of content called " & obj.Name & " could not be found, and was skipped")
'A new piece of content has been added, but the programmer failed 'to instruct the VBContentPipeline to compile it. So the content 'will not be available during this instance of the Game.
EndTry
Next
EndSub
PrivateSub CompileContent()
Dim objProcess As Process
Me.objContent = New List(Of ContentFile)
Me.HarvestContent()
IfMe.objContent.Count > 0 Then
Me.StartTheFakeCSharpFile()
Me.AddContentToTheFakeCSharpFile()
Me.EndTheFakeCSharpFile()
Try 'to write the fake C# project file to disk
Using TempStreamWriter As System.IO.StreamWriter = New System.IO.StreamWriter(Me.strContentRootFolder & "\VBContentPipeline.csproj", False)
With TempStreamWriter
.Write(Me.strFakeCSharpProject) .Close()
EndWith
EndUsing
Try 'to shell out to MSBuild.exe, sending it our fake c# project file
In a nutshell, the VBContentManager class uses a supplied directory as a root folder which it scans for content (texture files, XACT sound projects, and spritefonts) in the HarvestContent method. It also has the ability to produce a C# project file that includes information about the harvested content, which it can pass to MSBuild in order to have the corresponding .xnb (et al) files created on your behalf.
To use the VBContentManager class, all you have to do is call the LoadPipeline method in your game's LoadGraphicsContent sub, indicating whether or not the content is already compiled (false) or whether you want to execute the C# hack process (true).
The last thing the LoadPipeline method does for you is it uses the Load method of the underlying ContentManager to import the texture, sound and font files into corresponding typed hash tables, which reside as public members of the VBContentManager. The resources are keyed by filename (no extension) in the respective hash table.
Here is an example of a simple base game class that makes use of the VBContentManager class (you can also watch the video tutorial to see this class being used by a game project).
PublicClass MyBaseGame Inherits Game
Protected objGDM As GraphicsDeviceManager Protected objCM As VBContentManager Protected objSB As SpriteBatch
''' <summary> ''' Starts a new game for the XNA 2D Engine ''' </summary> ''' <param name="WindowTitle">The window title when the game is not full-screen</param> ''' <param name="MouseIsVisible">Whether or not the mouse is visible by default</param> ''' <param name="ContentRootFolder">The root of your content folder, relative to your projects bin\debug (or release) folder</param> ''' <remarks></remarks> PublicSubNew(ByVal WindowTitle As String, ByVal MouseIsVisible AsBoolean, ByVal ContentRootFolder As String)
Me.objGDM = New GraphicsDeviceManager(Me) Me.objCM = New VBContentManager(Me.Services, ContentRootFolder)
Me.objSB = New SpriteBatch(Me.objGDM.GraphicsDevice)
EndSub
EndClass
Here is a game that inherits from the above base game class. The root content folder for this game is a peer folder of the project's bin folder. Once you've got all the other stuff sitting in an engine somewhere, this is essentially all you need to do to spawn a new game with a vb content manager, effectively simulating VB support for XNA.
PublicClass XNAGameForWindows Inherits MyBaseGame
PublicSubNew() MyBase.New("XNA Game for Windows", True, "../../Content") EndSub
EndClass
And here's the typed hash table. I wouldn't grab textures & the like out of a hash tables directly from a draw routine, but at the top of your program you can pull them out into discrete references or put them into lists, or whatever. The VBContentManager just makes them available in the hash in a way that's easy for humans to find them (ie. by file name), and you can do whatever you want with them from there.
PublicReadOnlyProperty Keys() As ICollection Get ReturnMyBase.Dictionary.Keys EndGet EndProperty
DefaultPublicProperty Item(ByVal key As String) As Type Get ReturnCType(MyBase.Dictionary(key), Type) EndGet Set(ByVal value As Type) MyBase.Dictionary(key) = value EndSet EndProperty
PublicOverloadsSub Add(ByVal key As String, ByVal obj As Type) MyBase.Dictionary.Add(key, obj) EndSub
PublicReadOnlyProperty Values() As ICollection Get Return Dictionary.Values EndGet EndProperty
PublicFunction Contains(ByVal key As String) AsBoolean Return Dictionary.Contains(key) EndFunction
PublicSub Remove(ByVal key As String) Dictionary.Remove(key) EndSub
0 comments: