[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] Hardware Accelerated Scrolling Text
Hello Steve,
the 100 % CPU - Issue:
if you have an inner game loop like
quit = 0
while guit == 0:
   check_events()
   do_game_logic()
   paint()
or any other inner loop which runs the whole time, you are telling the 
computer to use every available clock cycle for the next step in your 
program.
This is quite normal and wanted, normally, because you want to run your 
game at the highest framerate possible. Most games do that, that's why 
quake II still uses your cpu to the max, it'll just render more frames 
in the same time than when it was released.
But you can give other processes a window to do stuff somewhere in your 
inner loop. Pygame has a function
pygame.time.wait(time_in_ms) for this.
it returns the actually waited time in ms. It isn't super - precise, but 
a pygame.time.wait(1) in your inner loop will give the os and other 
processes enough time to do their normal stuff (no more system lagging 
because of pygame app).
if you pygame.time.wait() for longer delays i guess you can drop the cpu 
- usage of your app.
but normally it's ok for a game to use the max cpu, as long as it gives 
the rest of the system a little time now and then.
for smooth scrolling you should use double buffering,  add DOUBLEBUF to 
your display init:
surface = pygame.display.set_mode((640,480), FULLSCREEN|DOUBLEBUF)
and use
pygame.display.flip()
to update the screen.
If you don't use double buffering, you should only 
pygame.display.update() the region(s) which changed, so if you know the 
scrolling text is in the middle of the screen, just update the area 
where the screen is changed. If the top and bottom third of the screen 
is black anyways, just update the middle third and triple your framerate.
The last thing, which is a little more complicated, is making the whole 
application timer - based, which leads to the best results on computers 
with different speeds anyways.
Let a timer run in your main loop,  and give the moving stuff (your 
text) a speed of, let's say, 20 pixels per second, that would be one 
pixel each 50 milliseconds.
Calculate the movement by dividing the milliseconds elapsed through the 
scrollingspeed (elapsed / 50) and place the text where it should go 
after that calculation.
Now it moves at the same speed no matter which framerate is used and 
should appear smoooth and nice and everything.
Sorry if you already tried all that and it wasn't useful in some way, 
you should give some example code in that case.
All the best and everything
   Daniel