Norris's picture

Gamewindow app with 2 sprites (non-animated) eat my CPU usage

Hi !
I created a class that inherits from "GameWindow" following the example "game.cs". To stay simple, I use immediate mode.
I try to create a simple 2D environnement with a background sprite(jpg) and a front sprite (png). For loading images and creating textures, I use the "TexLib" class founded on this site.
This works, but When I look at "performances" tab in the tasks manager (windows XP and old computer) my CPU is used at 100% and when I put my mouse over close or minimize buttons of the form, the hover effect is "delayed". Is it normal ?

VB.Net code

Imports OpenTK
Imports OpenTK.Graphics
Imports OpenTK.Graphics.OpenGL
Imports OpenTK.Audio
Imports OpenTK.Audio.OpenAL
Imports OpenTK.Input
 
Imports System.Drawing
Imports System.Drawing.Imaging
 
Public Class OTK2dGameWindow
    Inherits GameWindow
 
#Region "############# MEMBRES ###################"
    Private textureInt As Integer
    Private backTextureInt As Integer
#End Region
 
 
#Region "############# INITIALISATION ###################"
 
    Public Sub New()
        MyBase.New(640, 480, GraphicsMode.Default, "OpenTK 2D", GameWindowFlags.Default)
        VSync = VSyncMode.On
    End Sub
 
    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
        MyBase.OnLoad(e)
 
        InitFor2D()
 
        TexUtil.InitTexturing()
 
        backTextureInt = TexUtil.CreateTextureFromFile("C:\back-decor.jpg")
        textureInt = TexUtil.CreateTextureFromFile("C:\pac-alpha.png")
 
        OnResize(Nothing)
    End Sub
 
    Private Sub InitFor2D()
        GL.ClearColor(Color.White)
        GL.Disable(EnableCap.DepthTest)
        GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest)
        GL.Enable(EnableCap.Texture2D)
        GL.Enable(EnableCap.Blend)
        GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha)
        GL.PixelStore(PixelStoreParameter.UnpackAlignment, 1)
    End Sub
 
#End Region
 
    Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
        MyBase.OnResize(e)
        GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height)
        'Dim projection As Matrix4 = Matrix4.CreatePerspectiveFieldOfView(CSng(Math.PI / 4), Width / CSng(Height), 1.0F, 64.0F)
        Dim projection As Matrix4 = Matrix4.CreateOrthographic(Width, Height, -1.0F, 1.0F)
        GL.MatrixMode(MatrixMode.Projection)
        GL.LoadMatrix(projection)
    End Sub
 
 
#Region "############# RENDU ###################"
 
    Protected Overrides Sub OnRenderFrame(ByVal e As OpenTK.FrameEventArgs)
        MyBase.OnRenderFrame(e)
        GL.Clear(ClearBufferMask.ColorBufferBit)
        'réglage de la matrice pour positionner les objets (model view)
        Dim modelview As Matrix4 = Matrix4.LookAt(Vector3.Zero, Vector3.UnitZ, Vector3.UnitY)
        GL.MatrixMode(MatrixMode.Modelview)
        GL.LoadMatrix(modelview)
 
 
        'background sprite
        GL.BindTexture(TextureTarget.Texture2D, backTextureInt)
        GL.Begin(BeginMode.Quads)
        GL.TexCoord2(0, 0) : GL.Vertex2(-320.0F, 240.0F)
        GL.TexCoord2(1, 0) : GL.Vertex2(320.0F, 240.0F)
        GL.TexCoord2(1, 1) : GL.Vertex2(320.0F, -240.0F)
        GL.TexCoord2(0, 1) : GL.Vertex2(-320.0F, -240.0F)
        GL.End()
 
 
        'front sprite
        GL.BindTexture(TextureTarget.Texture2D, textureInt)
        GL.Begin(BeginMode.Quads)
        GL.TexCoord2(0, 1) : GL.Vertex3(-64.0F, -64.0F, 1.0F)
        GL.TexCoord2(1, 1) : GL.Vertex3(64.0F, -64.0F, 1.0F)
        GL.TexCoord2(1, 0) : GL.Vertex3(64.0F, 64.0F, 1.0F)
        GL.TexCoord2(0, 0) : GL.Vertex3(-64.0F, 64.0F, 1.0F)
        GL.End()
 
        SwapBuffers()
    End Sub
 
#End Region
 
 
#Region "############# UPDATES ###################"
 
    ''' <summary>
    ''' Méthode de base à utiliser pour updater l'état des "objets" du jeu.
    ''' </summary>
    Protected Overrides Sub OnUpdateFrame(ByVal e As OpenTK.FrameEventArgs)
        MyBase.OnUpdateFrame(e)
 
        If MyBase.Keyboard(Key.Escape) Then
            End
        End If
 
    End Sub
 
#End Region
 
End Class

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

To reduce CPU usage, either set VSync = VSyncMode.On or use System.Threading.Thread.Sleep(). The former is preferrable, as it will sync RenderFrame with the refresh rate of your monitor.

Norris's picture

Thanks for your answers the Fiddler, but that change nothing.
I tried whit just a GameWindow and drawing nothing ... my cpu usage is at 50 % !!!
my computer is very old, but I don't think this is the sole cause of the problem. Must I use Threads ? All animations techniques I use (under gdi, SlimDX or OpenTK) eat my cpu and are very slow.

My computer:
AMD Athlon 2400 / 2 GHz / 3 Go RAM / Windows XP Sp3
Graphic card: ATI Radeon 9600 Pro 256Mo (AGP 8X)

the Fiddler's picture

Which approach did you try?

System.Threading.Thread.Sleep(1) will reduce CPU usage, provided you have CPU time to spare. VSync will rely on the graphics drivers - IIRC that very old Ati drivers didn't sleep on vsync but that was fixed years ago.

Typhon's picture

the CPU is probably being consumed in WinGLNative.ProcessEvents, which tight-loops around PeekMessage. But that shouldn't really be causing delays in the window manager.

the Fiddler's picture

Indeed, unless you enable vsync or call Sleep.

@OP: ensure you are calling Run without a renderframe limit, i.e. Run(60) instead of Run(60, 60).

Jase's picture

Just on that last post, I can verify that CPU usage increases when a second paramater to Run(...) is specified, for whatever reason.

Norris's picture

I tried both solutions, but they change nothing.
Indeed, other applications continue to respond normally, but what will append for my slow OpenGL window if I make long calculations ....
With just one rotating png sprite, I just get 3 or 4 FPS !!

the Fiddler's picture
Norris wrote:

I tried both solutions, but they change nothing.
Indeed, other applications continue to respond normally, but what will append for my slow OpenGL window if I make long calculations ....
With just one rotating png sprite, I just get 3 or 4 FPS !!

Something is very wrong, you should be getting at least 100x-1000x better performance.

What does this print?

Console.WriteLine(GL.GetString(StringName.Vendor))
Console.WriteLine(GL.GetString(StringName.Version))
Console.WriteLine(GL.GetString(StringName.Renderer))
Norris's picture

Sorry for the long time to respond.

this is the output:

ATI Technologies Inc.
2.0.5337 WinXP Release
RADEON 9600 x86/MMX/3DNow!/SSE

Yes I know, this is old, but i think i can get better performance even with this ... I hope !