Rendering with graphics class (GDI+) (Post #2)

So! For my first post, I’ll be creating a simple drawing code (using a winforms picturebox). Even though non-GPU rendering is far from optimal for creating a game, it can be useful for testing our game algorithms.

 

Drawing on a picturebox with GDI+

We'll be creating a new project (Windows forms app - .Net framework), we need a new form and a new module.

Insert a picturebox and a timer control

Just for reference I have named the following as shown below

  • Starting form - F_SRender
  • Timer Control - RefreshTmr
  • Picture box - RenderBox
  • Module - M_SRender

Let's take a quick look at our module which I will put here with minimal explanation since we won't be using these much (besides algorithm testing)

I think it can be better explained in the drawing below

The process of refreshing, resets then draws everything in a graphics container and at the end it draw the graphics container on screen

 

'Our declarations for Graphics, Bitmaps, Rectangles, Integers and our Brush (using a texture brush in my experience is the best and fastest way to draw with GDI+)
    Public Gs As Graphics
    Public Bs As Graphics
    Public NewBit As Bitmap
    Public QRect As Rectangle
    Public TRect As Rectangle

    Public TestDrawingS As Bitmap
    Public TestDrawingB As TextureBrush

    Dim RWidth As Int16 = F_SRender.RenderBox.Width
    Dim RHeight As Int16 = F_SRender.RenderBox.Height

    Dim Qx, Qy As Integer

'Our drawing sub
    Public Sub SoftRender(ByVal Func As String)


        If Func = "LOAD" Then
'Here we load a *.PNG file (named TestCircle) that I added to my.resources onto TestdrawingS
            TestDrawingS = New Bitmap(My.Resources.TestCircle)
            Qx = RWidth / 2
            Qy = RHeight / 2


'Qx and Qy are the position variables for the image that we'll draw           

            Gs = F_SRender.RenderBox.CreateGraphics
            NewBit = New Bitmap(RWidth, RHeight)

        ElseIf Func = "REFRESH" Then
'This is called by the timer in the form (resets, draws some primitives, draws the image)
            SoftRender("RESET")
            SoftRender("DRAW")
            SoftRender("TEXTURE")
            SoftRender("FINALIZE")

        ElseIf Func = "RESET" Then
'Fill the Renderbox with a color brush

            QRect = New Rectangle(0, 0, RWidth, RHeight)
            Gs.FillRectangle(Brushes.Black, QRect)

        ElseIf Func = "FINALIZE" Then

'Throwing Gs Graphics to Bs and Drawing the buffer
            Gs = Graphics.FromImage(NewBit)
            QRect = New Rectangle(0, 0, RWidth, RHeight)
            Bs = F_SRender.RenderBox.CreateGraphics
            Bs.DrawImage(NewBit, 0, 0, RWidth, RHeight)

        ElseIf Func = "TEXTURE" Then

'Defining a rectangle and filling it with the texturebrush we created
            Dim fontT As New Font("Arial", 12)
            QRect = New Rectangle(0, 0, My.Resources.TestCircle.Width, My.Resources.TestCircle.Height)
            TestDrawingB = New TextureBrush(TestDrawingS, QRect)
            TestDrawingB.TranslateTransform(Qx, Qy)
            TRect = New Rectangle(Qx, Qy, My.Resources.TestCircle.Width, My.Resources.TestCircle.Height)
            TestDrawingB.ScaleTransform(1, 1)
            Gs.FillRectangle(TestDrawingB, TRect)
            TestDrawingB.Dispose()

'Move the image
            Qy -= 2
            If Qy < -My.Resources.TestCircle.Height Then Qy = RHeight + My.Resources.TestCircle.Height

        ElseIf Func = "DRAW" Then

'Draws a string and an ellipse
            Gs.DrawString("GDI+", New Font("Arial", 12), Brushes.White, 110, 40)
            Gs.DrawEllipse(Pens.White, QRect)

        End If

    End Sub

 

And don't forget to add 

on Form_Load 

SoftRender("LOAD")

and on Timer_Tick

SoftRender("REFRESH") 

 

On running the app you should have a window as show below (The gradient circle with the arrow is  the *.PNG image file I used)

Rendering result

That was it. Now we can start doing some more interesting stuff!

Comments