×

Discussion Board

Results 1 to 8 of 8
  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    13

    Unhappy Canvas.blit problem

    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

  2. #2
    Regular Contributor
    Join Date
    Mar 2003
    Location
    UK
    Posts
    125

    Re: Canvas.blit problem

    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)

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    13

    Re: Canvas.blit problem

    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..

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    13

    Re: Canvas.blit problem

    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

  5. #5
    Regular Contributor
    Join Date
    Mar 2003
    Location
    UK
    Posts
    125

    Re: Canvas.blit problem

    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)

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    13

    Re: Canvas.blit problem

    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!!

  7. #7
    Regular Contributor
    Join Date
    Mar 2003
    Location
    UK
    Posts
    125

    Re: Canvas.blit problem

    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.

  8. #8
    Registered User
    Join Date
    Jul 2007
    Posts
    13

    Re: Canvas.blit problem

    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/

Similar Threads

  1. Replies: 2
    Last Post: 2008-06-29, 20:40
  2. canvas.blit() problem of scaling
    By Zamoth_bg in forum Symbian
    Replies: 3
    Last Post: 2008-06-29, 12:19
  3. Problem with eglSwapBuffers and heap corruption
    By greatape in forum Symbian Media (Closed)
    Replies: 2
    Last Post: 2007-05-24, 03:35
  4. netcards - Problem with opening adapter
    By kernj in forum Symbian Tools & SDKs
    Replies: 5
    Last Post: 2007-01-10, 08:56
  5. Problem: S60 SDK for CW in PC with .NET
    By anttij in forum Carbide.c++ IDE and plug-ins (Closed)
    Replies: 1
    Last Post: 2005-02-28, 11:36

Posting Permissions

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