You Are Here:

Community: Developer Discussion Boards

#1 Old Unhappy Canvas.blit problem - 2009-04-18, 12:38

Join Date: Jul 2007
Posts: 11
Raylman89
Offline
Registered User
Hi all guys!
I'm making a game in phyton and have done all the hard part of the game. For test i've used canvas.rectangle instead of real image. Now i'm switching these rectangles to images with canvas.blit(image,target=(x,y),mask=testmask).
I have to draw 5 5x5 pixel image and 1 10x10 image. This is into a while loop with a sleep timer at the end. I've inserted the drawing code just after the canvas.clear(), but when i'm running the game it seems that the draw function is too slow and the screen is cleared before it finish to draw my images. i post some part of code:


automask code for images (called once at the beggining of program just after the image loading)
Code:
def automask(im):
    width, height = im.size
    mask = Image.new(im.size, '1') # black and white
    tran = im.getpixel((0,0))[0]   # transparent top-left
    for y in range(height):
        line = im.getpixel([(x, y) for x in range(width)])
        for x in range(width):
            if line[x] == tran:
                mask.point((x,y), 0)  # mask on the point
    return mask
drawing code (executed every cycle)
Code:
canvas.clear(0x000000)
canvas.rectangle((ostacolox, ostacolo, (ostacolox + 10), (ostacolo + 30)), fill=color)
canvas.rectangle((ostacolo2x, ostacolo2, (ostacolo2x + 10), (ostacolo2 + 30)), fill=color)
spunti= unicode(int(punti))
slivello= unicode(livello)
canvas.text((290,18), spunti ,fill=0xffff00)
canvas.text((2,18), "Livello " + slivello, fill=0xffff00)  
	
canvas.blit(corpo, target=(x1,y1))
canvas.blit(corpo, target=(x3,y3))
canvas.blit(corpo, target=(x5,y5))
canvas.blit(corpo, target=(x7,y7))
canvas.blit(corpo, target=(x9,y9))
canvas.blit(testa, target=(x11,y11),mask=masktesta)
Am i doing something wrong? is there a way for speed up things? timer is already at the lowest sleep possibile and if i delete it i only obtain a faster game but with the same problem.

If you want you can watch my game working with canvas.rectagle here -> http://www.youtube.com/watch?v=OTqyHoNIlOw

Sorry for my bad english! I hope in some reply!!

Thank you

Michele
Reply With Quote

#2 Old Re: Canvas.blit problem - 2009-04-19, 01:38

Join Date: Mar 2003
Posts: 125
Location: UK
aya42
Offline
Regular Contributor
Quote:
Originally Posted by Raylman89 View Post
when i'm running the game it seems that the draw function is too slow and the screen is cleared before it finish to draw my images.
If you're not using an offscreen buffer, you'll get a noticeable flickering effect. Try something like this:

Code:
# Build offscreen buffer
buffer = graphics.Image.new(canvas.size, 'RGB')

# Main loop
while 1:

    # Draw next frame to buffer
    buffer.clear(0x000000)
    buffer.rectangle((ostacolox, ostacolo, (ostacolox + 10), (ostacolo + 30)), fill=color)
    buffer.rectangle((ostacolo2x, ostacolo2, (ostacolo2x + 10), (ostacolo2 + 30)), fill=color)
    spunti= unicode(int(punti))
    slivello= unicode(livello)
    buffer.text((290,18), spunti ,fill=0xffff00)
    buffer.text((2,18), "Livello " + slivello, fill=0xffff00)  
    buffer.blit(corpo, target=(x1,y1))
    buffer.blit(corpo, target=(x3,y3))
    buffer.blit(corpo, target=(x5,y5))
    buffer.blit(corpo, target=(x7,y7))
    buffer.blit(corpo, target=(x9,y9))
    buffer.blit(testa, target=(x11,y11),mask=masktesta)

    # Now blit buffer onto canvas
    canvas.blit(buffer)

    # Sleep for a bit
    e32.ao_sleep(0.1)
Reply With Quote

#3 Old Re: Canvas.blit problem - 2009-04-19, 11:21

Join Date: Jul 2007
Posts: 11
Raylman89
Offline
Registered User
Great Job man! Now it's working fine. Sorry for my stupid question but i'm very newbie to phyton and i didn't know about the possibility to create an external buffer
Now the game runs a little slower but there is not flickering at all!!! I'm running this game on a N95 8gb.. do you think there will be speed problem with other s60 3rd edition? i'm asking this because i know there are other 3rd edition with lower cpu speed..
Reply With Quote

#4 Old Re: Canvas.blit problem - 2009-04-19, 12:52

Join Date: Jul 2007
Posts: 11
Raylman89
Offline
Registered User
Ok, now i have solved the flickering problem, but i'm having another problem. With the buffer method i have draw 2 moving 320x240 png that are the background of the game. I need to import 2 times the png because when the first reach x=-320 and it's totally out of the screen i need the second one to be at x=0 and then set the first image to be x=320 se they do a infinite cycle giving the look of an infitine moving background. The problem is that this is very heavy: the game fps fall down and it's near to unplayable. Someone have ideas on how to speed up this code or make the same effect with a lighte code?

I post the code i use to draw the background:
Code:
bx1=0
bx2=320
back1 = Image.open("E:\\back2.png")
res_x, res_y = canvas.size
buffer = graphics.Image.new(canvas.size, 'RGB')

#on the while loop:

buffer.blit(back1, target=(bx1,0))
buffer.blit(back1, target=(bx2,0))
...
...
canv.blit(buffer)
...
...
bx1 = bx1-2
if bx1<= -320:
        bx1=320
bx2 = bx2-2
if bx2<= -320:
	bx2=320
Thank you for your help!

Michele
Reply With Quote

#5 Old Re: Canvas.blit problem - 2009-04-19, 19:47

Join Date: Mar 2003
Posts: 125
Location: UK
aya42
Offline
Regular Contributor
Quote:
Originally Posted by Raylman89 View Post
Someone have ideas on how to speed up this code or make the same effect with a lighte code?
Well, you could cut it down to a single blit by pre-compositing a double-sized version.

Code:
back = Image.open("E:\\back2.png")
w, h = back.size
dblBack = Image.new((w*2, h), 'RGB')
dblBack.blit(back, target=(0, 0))
dblBack.blit(back, target=(w, 0))
x = 0
while 1:
    buffer.blit(dblBack, target=(-x,0))
    canvas.blit(buffer)
    x = (x + 2) % w 
    e32.ao_sleep(0.1)
Reply With Quote

#6 Old Re: Canvas.blit problem - 2009-04-19, 20:27

Join Date: Jul 2007
Posts: 11
Raylman89
Offline
Registered User
Quote:
Originally Posted by aya42 View Post
Well, you could cut it down to a single blit by pre-compositing a double-sized version.

Code:
back = Image.open("E:\\back2.png")
w, h = back.size
dblBack = Image.new((w*2, h), 'RGB')
dblBack.blit(back, target=(0, 0))
dblBack.blit(back, target=(w, 0))
x = 0
while 1:
    buffer.blit(dblBack, target=(-x,0))
    canvas.blit(buffer)
    x = (x + 2) % w 
    e32.ao_sleep(0.1)
Awsome!! But i have understand that now with this code you take the 2 images and merge it on a new image. But why this cose is so faster? in this code and in mine the image has to be "printed" on the screen so i can't justify so different performance. Sorry if i'm getting boring man, but i'm so happy to talk with someone that is so skilled i'm very newbie with python and i want to learn it better as i can!!
Reply With Quote

#7 Old Re: Canvas.blit problem - 2009-04-19, 21:00

Join Date: Mar 2003
Posts: 125
Location: UK
aya42
Offline
Regular Contributor
Quote:
Originally Posted by Raylman89 View Post
But why this cose is so faster? in this code and in mine the image has to be "printed" on the screen so i can't justify so different performance.
These are just general game design principles, not specific to Python. When you want to render a game with a high framerate, you need to ensure that each frame can be generated as quickly as possible, so try to factor out as much as you can from the main loop, by pre-computing as much as you can before you start. Remember, if you want to generate, say, 25 frames per second, that only gives you 40ms to generate each frame.

When you start getting performance issues, you need to profile your existing code, by calculating how many milliseconds each line of code is taking to execute (time.clock() can help there), working out which lines are taking the longest to run, and trying to optimize those lines.

For example, for the scrolling background, you may find that using the 'source' parameter for 'blit' to reduce the source rectangle size may yield a performance advantage. If that's still too slow, you might be able to (given sufficient RAM) pre-compute all 160 possible background states, and use them in sequence.

However, before you start optimizing that section, make sure that it's definately that bit which is taking up most of the time, otherwise your time would be better spent optimizing the part that is.
Reply With Quote

#8 Old Re: Canvas.blit problem - 2009-04-19, 21:57

Join Date: Jul 2007
Posts: 11
Raylman89
Offline
Registered User
thank you very much man. I can't say enough thanks to you because i'm not very good with english language however i will post there if i need more help!! when my game will be ready i will send you a copy and i would like you to tell me your opinion..
if someone is interested this is the latest video i've recorded...unluckely the framerate is slow cause i've registered it on the pc screen and framerate is very low there.

http://theray.altervista.org/wordpre...a-una-grafica/
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump
Similar Threads
Thread Thread Starter Forum Replies Last Post
canvas.blit() problem - scaling does not go together with picture masks Zamoth_bg Python 2 2008-06-29 21:40
canvas.blit() problem of scaling Zamoth_bg Python 3 2008-06-29 13:19
Problem with eglSwapBuffers and heap corruption greatape Symbian Media (Graphics & Sounds) 2 2007-05-24 04:35
netcards - Problem with opening adapter kernj Symbian Tools & SDKs 5 2007-01-10 09:56
Problem: S60 SDK for CW in PC with .NET anttij Carbide.c++ IDE and plug-ins 1 2005-02-28 12:36

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia