1

So I have an FPS application, Means that I am drawing a lot of "frames" per seconds.

Now basically I have a bitmap object, and it is my screen, it is like my buffer I am drawing a frame on this bitmap then I am drawing the buffer to the user screen, and I am doing this a lot of times in a second. Because the DrawFrame function is used a lot of times, I need to make it as optimal as I can.

I have a graphic object that I am creating from the bitmap.

And the question is, do I need to create a new graphic object every time I draw a frame, Or do I need to make a public graphic object that will be used in the draw function.

This is the two options I have:

Public Class FpsEngine 'Exmaple Object
    Private Buffer As Bitmap
    Public Sub New()
        'initialize the object
    End Sub
    Public Sub DrawFrame() 'This fucntion will be called a lot of times and must be optimal
        Using g As Graphics = Graphics.FromImage(Buffer)
            'Draw the frame on the Buffer
        End Using

        'Draw the Buffer on the user screen
    End Sub
End Class

OR

Public Class FpsEngine 'Exmaple Object
    Private Buffer As Bitmap
    Private g As Graphics
    Public Sub New()
        'initialize the object
        g = Graphics.FromImage(Buffer) 'Create the graphics object
    End Sub
    Public Sub DrawFrame() 'This fucntion will be called a lot of times and must be optimal
        'Draw the frame on the Buffer using the public Graphics object (g)

        'Draw the Buffer on the user screen
    End Sub
End Class

Note - the DrawFrame function is not running on the main GUI thread, thats why I am not drawing directly on the user screen, I am drawing all the frame on a buffer then after everything (the frame) is ready I am interrupting the main GUI thread am make a Fast Image to Screen Draw.

3
  • I would say that since you are copying the same bitmap to the screen each time, that it would be better to make your Graphics object a class level member like your second example. No need for a new one each time. I have no idea if you will be able to get an acceptable framerate this way, however. Apr 1, 2016 at 20:55
  • Well in matter of fact, I can make 2 buffers and run on 2 threads for each buffer, doubling the framerate and lets say I want to get to 30 fps (I need to draw a frame in 33 milisec (it is possible), but the question is still what is better to use, or what is the right way to use the Graphics object. (personally I always try to avoid using public variables because of side effects (unwilling object mutation)).
    – Green Fire
    Apr 1, 2016 at 21:14
  • In this case, it would be a private variable to the class. I think you would incur some overhead newing up a new bitmap and calling the FromImage method. Maybe it's negligible. The only way to know for sure is to measure it. Apr 1, 2016 at 21:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.