XMLpitstop.com   |  VBnetexpert.com   |  Community Credit  
 
 
Pitstop Search:  
in
 
Sign in | Join | Help
 
 
  Blog
    Home  
 
  Entries By Date
 
<June 2008>
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345
 
 
  Blog Categories
   
 
  Archives
    August 2008 (1)  
    June 2008 (4)  
    May 2008 (2)  
    April 2008 (3)  
    March 2008 (3)  
    February 2008 (5)  
    December 2007 (4)  
    November 2007 (1)  
    October 2007 (3)  
 
  Syndication
    RSS  
    Atom  
    Comments RSS  

June 2008 - Posts

  .NET Flea Market  
 

A Totally Non-Random Post

I am sure this is covered somewhere on the great big Internet, but it's new to me.  And it sucked until I discovered what the problem was.  Basically, I had random values being generated that were anything but random.  "Oh, you forgot to set a seed value to something pseudo-random like now.millisecond."  No.  That didn't help.  Check out this code and see what the results are:

    Private Sub UnRandom()
        Dim r As New Random(Now.Millisecond)

        Dim winCount, winCount2 As Integer

        For i As Integer = 1 To 100
            If Rndm() > 50 Then winCount += 1
        Next

        For i As Integer = 1 To 100
            If r.Next(1, 100) > 50 Then winCount2 += 1
        Next

        MsgBox(winCount & ":" & winCount2)

    End Sub

    Private Function Rndm() As Integer
        Dim r As New Random(Now.Millisecond)

        Return r.Next(1, 100)
    End Function

Assuming a 50/50 chance of win, you would expect that a Random.Next would return a random value.  Seemingly not when it gets continually instantiated like in the Rndm function.  So this apparently is not a case where you want all of your variables to be contained in their own methods.  You'd probably want to have one single random number generator for your whole application.

 
 
 
 

Using GDI+ To Create Depth Perspective

This is a quick little bit of code that I made up after unsuccessfully finding anyone else that had done it.  What I wanted to do is take an original image, and skew it so it looked like it was going off into the distance.  That gives it perspective.  Obviously something like that is going to be used in this game I'm working and blogging on.  More precisely, it'll be part of the map-building tools.

So you start with an image like:   and you end up with an image like: .  That's a 50% skew.  The image should be half as tall at the end and is half as wide as the original.  I didn't see any built-in way in GDI+ to transform the image, so I did it by brute force.  Here's a reusable function to do the magic:

Private Function PerspectiveImage(ByVal sourceImage As Image, ByVal perspective As Single) As Image
    Dim destImage As Image
    Dim destGr As Graphics
    Dim sourceSection As RectangleF
    Dim destSection As RectangleF
    Dim factor As Double

    destImage = New Bitmap(CInt(sourceImage.Width * perspective), _ 
        sourceImage.Height, Imaging.PixelFormat.Format32bppArgb)

    destGr = Graphics.FromImage(destImage)

    For i As Integer = 0 To sourceImage.Width
        With sourceSection
            .Height = sourceImage.Height
            .Width = 1
            .X = i
            .Y = 0
        End With

        factor = perspective * (i / sourceImage.Width)

        With destSection
            .Height = CSng(sourceSection.Height - (sourceSection.Height * factor))
            .Width = 1
            .X = CSng(i * perspective)
            .Y = CSng((sourceSection.Height - .Height) / 2)
        End With

        destGr.DrawImage(sourceImage, destSection, sourceSection, GraphicsUnit.Pixel)
    Next

    destGr.Dispose()

    Return destImage

End Function

Basically it runs through the source image copying column by column of the image, then pasting it into the destination image at a calculated scale.  You can add the value checking yourself to make sure the perspective parameter is a positive number and should be between .1 and .9.  Well, you can do values greater than 1, but it's kinda weird looking.  Enjoy.

 
 
 
 

XNA Zune Games in VB

I've been spending a lot of time working on the game-play for the dungeon-type game for the Zune. Along the way, I've learned a lot about XNA. It's not as bad as I thought. It's just a bunch of new classes to understand. The programming concepts are still the same. I was initially worried that XNA would be so limited as to require a more procedural approach to coding, but I'm glad to see that's not true. I have classes, inheritance, and events. That's all I need to start.

I'd been doing pretty well testing the game out in a winform container, but I really wanted to see how it would run on the Zune. Unfortunately, every time I started the game on the Zune, it would reboot. I knew that the Zune XNA didn't support VB, so I researched getting rid of the reference to the VB runtime. I found the magic switch and recompiled. The VB runtime was gone. I thought that was it, but I still had more to go. Every few days, I'd try again and see if it would dawn on me. Tonight it did. I still had a reference to the 2.0 mscorlib in addition to the 3.5 version. After setting another magic switch, I was able to run the game on the Zune. Happiness!

So, here's what I did. I created a batch file, build.bat. I built the project in VS2005 and copied the vbc.exe command line out of the output window. I pasted this into the batch file. Then I made the following changes.

Original:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Vbc.exe /noconfig /imports:System,System.Collections,System.Collections.Generic /nowarn:42016,41999,42017,42018,42019,42032,42036,42020,42021,42022 /optionstrict+ /rootnamespace:DungeonXNAGame /doc:obj\Release\DungeonXNAGame.xml /define:"CONFIG=\"Release\",TRACE=-1,_MyType=\"WindowsFormsWithCustomSubMain\",PLATFORM=\"AnyCPU\"" /reference:D:\Projects\cbDungeon\DungeonObjects\bin\Release\DungeonObjects.dll,"C:\Program Files\Microsoft XNA\XNA Game Studio\v3.0\References\Windows\x86\Microsoft.Xna.Framework.dll","C:\Program Files\Microsoft XNA\XNA Game Studio\v3.0\References\Windows\x86\Microsoft.Xna.Framework.Game.dll",C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Deployment.dll, C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Drawing.dll,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Windows.Forms.dll, C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.XML.dll /debug- /optimize+ /out:obj\Release\DungeonXNAGame.exe /target:winexe Components\CompassComponent.vb Components\MapComponent.vb DungeonXNAGame.vb GameStart.vb "My Project\AssemblyInfo.vb"

Changed:

C:\WINDOWS\Microsoft.NET\Framework\v3.5\Vbc.exe /sdkpath:"C:\Program Files\Microsoft XNA\XNA Game Studio\v3.0\References\Zune" /noconfig /verbose /nostdlib /novbruntimeref /imports:System,System.Collections,System.Collections.Generic /nowarn:42016,41999,42017,42018,42019,42032,42036,42020,42021,42022 /optionstrict+ /rootnamespace:DungeonXNAGame /define:"_MyType=\"Empty\"" /reference:D:\Projects\cbDungeon\DungeonObjects\bin\Release\DungeonObjects.dll,"C:\Program Files\Microsoft XNA\XNA Game Studio\v3.0\References\Zune\Microsoft.Xna.Framework.dll","C:\Program Files\Microsoft XNA\XNA Game Studio\v3.0\References\Zune\Microsoft.Xna.Framework.Game.dll","c:\Program Files\Microsoft XNA\XNA Game Studio\v3.0\References\Zune\System.Xml.dll" /debug- /optimize+ /out:bin\Release\DungeonXNAGame.dll /target:library Components\CompassComponent.vb Components\MapComponent.vb DungeonXNAGame.vb

Can you spot the differences? Here they are:

  • Path to vbc.exe changed from version v2.0.50727 to v3.0
  • Added /sdkpath switch. This points to the XNA framework root for the Zune files. This gets rid of the mscorlib 2.0 reference
  • Added /verbose (so I can see what's going on)
  • Added /nostdlib (gets rid of unneeded references)
  • Added /novbruntimeref (gets rid of Microsoft.VisualBasic reference)
  • Modified references to point to Zune versions of DLLs
  • Changed /define switch to use \"Empty\" This makes sure that the "My" namespace is not created. Creating the My namespace results in a reference to the VB runtime.
  • Changed output type and destination with /out and /target
  • Removed reference to assemblyinfo.vb. That might not be required, but just to be minimal...

So the main switches you need to work with are: /sdkpath, /novbruntimeref and /define and also make sure you use the right vbc version (3.0). Just use that build.bat from the command line to create your libraries.

Now that I've seen the game run on the Zune, I'm going to drop my reservations about coding the game in VB. All the C# snobs who think they're the only ones that matter can go suck a semicolon.

 
 
 
 

Now a little less than fun project

A few very productive days with lots of progress... lots of momentum.  That's always trouble when you come up to "the wall."  The momentum you have gained certainly works against you.

So in the few days, I created a usable map editor for the game:

I created a test application to walk through the created maps:

And then I created the graphical version using XNA:

So I'm thinking I'm good to go.  Even though I've written everything in VB, I should still be able to make it work.  It's all .NET, right?

First, I make the C# XNA game project and reference my VB.NET DLL.  Point the code to the proper methods in the DLL as needed and now compile.  But it doesn't compile.  Some error about needing a reference to Xna.Framework and Xna.Framework.Game.  Well, of course I have those.  After deeper investigation, there are two different Xna.Framework (and.Game) DLLs.  One for Windows and one for the Zune.  Guess which one I was referencing?  The other one.  That's why it worked so well in the Windows build of the game.

More juggling of code, DLLs and references and eventually rewriting all my XNA code in C# (Gah.), I got a good compile and deployment to the Zune.  However, now it just doesn't start.  The Zune reboots before starting the game.  How do you troubleshoot something like that?

I've hit the wall and it's time to walk away.  Just walk away.

 
 
 

 
Copyright © . All Rights Reserved.
Powered by Community Server (Commercial Edition), by Telligent Systems