A tutorial by ShadowDust702

This tutorial assumes the following:

  • You have installed Gorgon
  • That you are new to Gorgon
  • That you’re working in C# (but should work for other .NET 3.5 capable managed languages).
  • That you have a working knowledge of how to program (windows forms, basic .NET functionality, etc…)

Basic Objective

The basic Objective of this tutorial will be to teach you how to initialize Gorgon, and create cool Gorgon stuff

Getting Started

Assuming you have MS Visual C# opened and ready to start a new project; If not do so now. For this example I name my project “MyFirstGorgonGame”. But you can name it what ever you like. Note that I changed the title, and name/filename of my form, but left everything else relatively the same. You may wish to do this as well.


Visual Studio

 

Gorgon Library References

So now that you’ve got everything ready, and are ready to get started in dive in to the awesome world of 2D gaming/graphics? Good! Because we’ll now need to include Gorgon in to our project.

Over to the right in your “Solution Explorer” right-click the “References node”, and then click on “Add Reference”. Make sure you’re in the “.Net” tab, and look down the list. You should see Gorgon. Select this and push ‘OK’.

References

Success? Great!

 

Initializing Gorgon

To make our code easier to write we should add the GorgonLibrary and GorgonLibrary.Graphics namespaces in our using list at the very top. Place these below the other “using” statements so that it looks similar to this:

using System;using System.Linq;using System.Collections.Generic;using System.ComponentModel;using System.Drawing;using System.Text;using System.Windows.Forms;using GorgonLibrary;using GorgonLibrary.Graphics;

 Now it’s time to turn that dull grey form in to something of awesomeness! Double click on your form. You should be presented with the Form_Load event in your code:

private void Form1_Load(object sender, EventArgs e){}

Here’s the first line we’re going to put in our Form_Load routine:

Gorgon.Initialize(false, false);

The first parameter is for background rendering, basically, if set to true then Gorgon will keep rendering even if it’s window is not focused. The second will tell Gorgon whether it should block the screensaver/power savings (I don’t recommend setting this to true, it’s a holdover from days gone by –Tape Worm 06:07, 1 June 2009 (UTC)). If we’re making a game we should probably set this parameter to false.

 

This line selects what Gorgon should render on to. In this case we’re selecting our form (this):

Gorgon.SetMode(this);

By passing only the form instance into the SetMode function, Gorgon will automatically set the video mode to match the display format of the desktop and it will match the width and height of the window client area. You can pass any control into this parameter like a TextBox, Label, Button, or Panel (which is the most useful). –Tape Worm 06:07, 1 June 2009 (UTC)

 

Now if you’re like me and like to cheat and let VisualStudio do most of the typing. Paste the following line:

Gorgon.Idle

And now type ‘+=’ immediately after and push the tab key twice. If all goes well then you should end up with the following (if not copy what’s shown below):

private void Form1_Load(object sender, EventArgs e)

{

   Gorgon.Initialize(false, false);

   Gorgon.SetMode(this);

   Gorgon.Idle += new GorgonLibrary.Graphics.FrameEventHandler(Gorgon_Idle);

}

 

void Gorgon_Idle(object sender, GorgonLibrary.Graphics.FrameEventArgs e)

{

   throw new NotImplementedException();

}

So what is the “Idle” event? Well basically this is when Gorgon is wanting us to do our drawing. For now just replace the

throw new NotImplementedException();

with

return;

Now we need to tell Gorgon to start (or rather, go). So now back in our Form_Load void add the following line.

Gorgon.Go();

Now would be a good time to see if we’ve done something silly, so press F5 to start debuging our App.

Nothingness

Not quite as awesome as you were hoping? Well I’m excited, because that blankness means endless possibilities of pixel filling fun!

The Second most fun part

Ironically the fun is also the hardest part. But it’s relatively easy anyways. The first hardest part (possibly harder then drawing stuff to screen) is deciding what to draw to the screen. Hmmmm, how about a spinning resizing sprite!!! This ought to give our graphics card a work-out… Well kinda..

 

We need to create a Sprite object. In a proper game you’ll be having Sprites come and go etc.. so you’d probably want to make an array or list. But for our simple tutorial, there will just be a single Sprite object. To create one you need to add this line to where you declare variables for your form (ensure that the sprite is visible to the scope of the form object, and not just one of the functions in that form. –Tape Worm 06:07, 1 June 2009 (UTC)):

private Sprite _mySprite;

Before our Gorgon.Go() line in our Form_Load method we’ll need to create our Sprite:

_mySprite = new Sprite(“MySprite”, GorgonLibrary.Graphics.Image.FromFile(Environment.CurrentDirectory + “\<your_image_here>”));

Protip: The reason we have to decorate the Image class with the “GorgonLibrary.Graphics” namespace is because we have a conflict with System.Drawing.Image, to get around this you can do a few things. What I like to do is alias System.Drawing like this:

using Drawing = System.Drawing.

Now all the System.Drawing objects just need to be declared like this “Drawing.Color” or “Drawing.Image” –Tape Worm 06:07, 1 June 2009 (UTC)

 

The first parameter is the name of the sprite and the second parameter is somewhat like how we get an image with GDI+.

 

Drawing the Sprite itself

Great! so now we have a Sprite loaded, now what? We Draw!! that’s what!

Add the following lines in to our new idle function the visual studio wrote mostly for us (before the return preferably):

Gorgon.Screen.Clear();

This will ensure the screen is cleared on every frame. If you don’t do this the frame buffer will not be cleaned up from the last frame and you’ll have artifacts from the last frame drawn.

And now add the following:

Vector2D CenterScreen = new Vector2D(Gorgon.Screen.Width * 0.5f, Gorgon.Screen.Height * 0.5f);

This line calculates the center of our Gorgon screen and this is where we’ll draw our Sprite. But at the moment where ever we put our Sprite it’ll be where the top-left point on our Sprites image is, which is not useful for our situation right now. So to correct that we add the following line:

_mySprite.Axis = new Vector2D(_mySprite.Image.Width * 0.5f, _mySprite.Image.Height * 0.5f);

This sets the Axis point on our Sprite to the center of the sprite. Now next thing we need to do is place the Sprite in the center of the screen (where CenterScreen is pointing) add this line:

_mySprite.Position = CenterScreen;

In order to set the rotation just add the following line:

_mySprite.Rotation += (10.0f * e.FrameDeltaTime);

Keep in mind that the rotation is based in degrees, and is just one floating point number, making it super easy! I’ve changed the rotation from 1f to 10.0f * e.FrameDeltaTime. This will keep the rotation speed constant. Using the original code the rotation could be super slow on less powerful machines or ridiculously fast on beastly machines like mine.Basically what the change amounts to is “rotate 10 degrees every second”. –Tape Worm 06:07, 1 June 2009 (UTC)

Now tell the Sprite to draw by adding this line next:

_mySprite.Draw()

Alrite! Now hit F5 and see what happens. If all goes well then you should get a weird spinning image of your picture. Don’t worry your graphics card hasn’t exploded, this is what we want. If the app fails, then ensure your app can find your picture.

 

Last and probably least

So now we have a spinning image. we want it to be spinning and zooming.. awesome..

 

To make the image bigger we need to set the scale of the sprite. Now there are three ways to do this:

  • Scale – Which will scale the sprite relatively (2.0f will make it twice as big, 0.5f will make it half as big and so on)
  • ScaledDimensions – Which will scale using absolute pixel sizes. For example: To scale to 64×64 set ScaledDimensions = new Vector2D(64, 64);
  • UniformScale – Which takes a single relative floating point number for scaling. This will scale both the width and height by the factor provided to the property.

For this tutorial we’re going to let our image go to double size, then reset it back to half. So essentially it’s in an endless loop of getting bigger just to get small again. Add this line to increase the Scale (place it before MySprite.Draw):

_mySprite.Scale += new Vector2D(0.1f, 0.1f) * e.FrameDeltaTime;

Luckily Gorgon lets us use the += to add two Vectors together, thus making our code quicker and easier to read/write. Now add the next two lines to reset the scale once it gets over 2:

if (_mySprite.Scale.X > 2.0f)

   _mySprite.Scale = new Vector2D(0.5f, 0.5f);

 

Done! Now hit F5 and see the crazyness! I don’t blame you if you never look at the image again

What you should be seeing.

11 thoughts on “A tutorial by ShadowDust702

  1. TizzyT

    I seem to be having one single problem, great work by the way. I am working and VB although I do know enough C# to get by this tutorial.
    This line when I converted it to VB didn’t work for me.
    “Gorgon.Idle += new GorgonLibrary.Graphics.FrameEventHandler(Gorgon_Idle);”
    I have it exactly as if in the VB project and it says :
    “Error 1 ‘Public Shared Event Idle(sender As Object, e As GorgonLibrary.Graphics.FrameEventArgs)’ is an event, and cannot be called directly. Use a ‘RaiseEvent’ statement to raise an event.”
    Any help would be great, thanks again.

    1. Tape_Worm Post author

      Assigning events don’t work the same in VB.Net as they do in C# (at least, they didn’t used to, I haven’t used VB.Net in a long time).

      Look at the source code for the Gorgon example applications. There’s an example called TehShadowGn0s that’s written in VB.Net and it has code that assigns the idle event:
      Gorgon VB.Net Example

      Also, look at this MSDN article about assigning events.

  2. Isk

    Hi. I try to install Gorgon but everytime ive got an error: “.net 3.5 SP1 is required by Gorgon but was not found”. I have win 7 x64 and VS 2008 so i guess i have .net 3.5 too. Ive tried copule times to reinstall .net but that was not fun and always ended on reinstall windows ;( Any idea how to fix this?

    1. Tape_Worm Post author

      You may not have .NET 3.5 SP1 installed. Otherwise, there’s something messed up with your .NET 3.5 install.

      This was brought up on the forums before if I recall, but I can’t remember if it was resolved or not. You’ll have to look there to see what the specifics are.

      Beyond that, I don’t know what causes the issue, nor do I know how to fix it as I’ve never been able to reproduce it.

      Alternatively you can use the subversion repository and download the source code for Gorgon and then compile it.

      1. Isk

        I finally managed to install Gorgon – not sure if works;>
        But ur installer doesnt work right.
        Quote from forum:
        “The registry key that the installer looks at is (under HKEY_LOCAL_MACHINE):
        SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5\1033

        And that should have a DWORD entry called “SP” and the value for the entry should be 1 if it’s for .NET 3.5 SP1. ”

        And thats right only for english version of windows. For different lang, keys looking diffrent.
        In polish version its:
        SOFTWARE\Microsoft\NET Framework\Setup\NDP\v3.5\1045

        I guess 1033/1045 its LCID – check here http://msdn.microsoft.com/en-us/goglobal/bb964664.aspx

        Not a big issue. Can be solved by making keys, values in registry, or modifying instaler to work with all lang version 🙂

        Thanks for help – i will check your lib later:)

        1. Tape_Worm Post author

          Yeah, I kind of guessed locale might be the root of the issue. Unfortunately, I don’t have the time to mess with the way the installer does its detection, so it’ll have to stay that way for the time being. For the next version of Gorgon I plan on scrapping the installer outright because of stuff like that and because there’s really nothing that I’m using that needs to be registered system wide (plus it’s a real pain in the ass to maintain the installer scripts).

          Regardless, I’m glad you got it working.

          As for wondering if it’ll actually work: Yeah, it should be fine. I can’t imagine why it wouldn’t (assuming you’ve got the correct SlimDX version installed) since it really doesn’t do anything other than copy the binaries and source to the location you specify and set up the Start Menu folders/icons. The only thing that might go south is the installation into the GAC (assuming you chose that option, I wouldn’t, it’s really not that handy), but again, I can’t really see any reason why that’d fail either.

          Would you be so kind as to post your findings on that forum thread for me? That would help solve confusion in the future for other users.

  3. James Moore

    Sorry delete my previous two duplicate posts. This article contains the following which looks wrong. The second <your_image_here> should be deleted.

    _mySprite = new Sprite(“MySprite”, GorgonLibrary.Graphics.Image.FromFile(Environment.CurrentDirectory + “|</your_image_here>”));</your_image_here>

Leave a Reply