Low FPS when mouse is not moving over the window

44 views
Skip to first unread message

Thomas Ryabin

unread,
Nov 7, 2019, 3:37:31 AM11/7/19
to pyglet-users
Hi,

I'm trying to run my pyglet program at 60 FPS, but am running into a problem where it runs at considerably slower than 60 FPS when the mouse isn't moving over the window. However when I move the mouse continuously over the window then the animation plays smoothly at 60 FPS. One thing I will note is that when I look at the CPU usage of the python program, the usage increases significantly when I am moving the mouse and the animation is playing smoothly. Could this be an issue with my Windows OS?

Here is a small Python program that reproduces the problem for me. I am using pyglet 1.4.6.

from math import radians, cos, sin
import pyglet
from pyglet.gl import *
from win32api import GetSystemMetrics

class Ball():
    x
= 0
    y
= 0
    color
= (0, 0, 0)
    radius
= 5

class PrimaryWindow(pyglet.window.Window):
    FPS
= 60
    smoothConfig
= Config(sample_buffers=1, samples=4, depth_size=16, double_buffer=True)
    screenWidth
= GetSystemMetrics(0)
    screenHeight
= GetSystemMetrics(1)
    windowWidthPixels
= int(screenWidth/2)
    windowHeightPixels
= int(screenHeight/2)

    ballRadiusPixels
= 20
    ballVelocity
= 10
    ballColor
= (0, 128 / 255, 255 / 255, 1)

   
def __init__(self):
       
super(PrimaryWindow, self).__init__(config=self.smoothConfig)
       
self.set_size(self.windowWidthPixels, self.windowHeightPixels)
       
self.set_location(int(self.screenWidth/2 - self.windowWidthPixels/2), int(self.screenHeight/2 - self.windowHeightPixels/2))

       
self.ball = Ball()
       
self.ball.x = self.windowWidthPixels/2
       
self.ball.y = self.windowHeightPixels/2
       
self.ball.radius = self.ballRadiusPixels
       
self.ball.color = self.ballColor

        pyglet
.clock.schedule_interval(self.update, 1.0 / self.FPS)
        pyglet
.app.run()

   
def on_draw(self):
       
self.clear()

        verts
= []
       
for i in range(100):
            angle
= radians(float(i)/100*360.0)
            x
= self.ball.radius*cos(angle) + self.ball.x
            y
= self.ball.radius*sin(angle) + self.ball.y
            verts
+= [x, y]

        circle
= pyglet.graphics.vertex_list(100, ('v2f', verts))
        glClear
(pyglet.gl.GL_COLOR_BUFFER_BIT)
        glColor3f
(self.ball.color[0], self.ball.color[1], self.ball.color[2])
        circle
.draw(GL_LINE_LOOP)

   
def update(self, dt):
       
self.ball.x += self.ballVelocity
       
if self.ball.x > self.windowWidthPixels or self.ball.x < 0:
           
self.ballVelocity *= -1


if __name__ == "__main__":
   
PrimaryWindow()


-Thomas
Reply all
Reply to author
Forward
0 new messages