×

Discussion Board

Results 1 to 12 of 12
  1. #1
    Regular Contributor
    Join Date
    Jul 2003
    Posts
    63

    Series 40 - draw only a part of the image

    I'm developing a game for Series 40. Using MIDP1.0(or nokia UI api) how can I get only a part of a large image ? I want to get a rectangular image from a source image and being able to specify the coordinates in the large image and how big this new image will be (from the source image).

    Practicly, I want the same effect as available in MIDP2.0 with :

    createImage(Image image, int x, int y, int width, int height, int transform) method from Image class

    or

    drawRegion(Image src, int x_src, int y_src, int width, int height, int transform, int x_dest, int y_dest, int anchor) method from Graphics

  2. #2
    Super Contributor
    Join Date
    Mar 2003
    Location
    Israel
    Posts
    2,280
    Set the clipping rect to the size of the desired result image, and draw the image at an offset of the negative value of the location of the result in the big image:
    Code:
    // drawing the rectangle (30,40) to (40,60) from the big image
    // on coordinate x,y
    g.setClip(x,y,10,20); // set the clip rect to the size and location of the desired result
    g.drawImage(theBigImage,x - 30,y - 40,Graphics.TOP|Graphics.LEFT); // draw the big image at an offset of (30,40) from the destination coordinates (x,y)
    shmoove

    [edit]
    You might also want to save the previous clipping rect (with getClipX(), getClipY(), getClipWidth(), and getClipHeight() from the Graphics class) before setting the new one.
    [/edit]
    Last edited by shmoove; 2003-07-09 at 08:41.

  3. #3
    Regular Contributor
    Join Date
    Jul 2003
    Posts
    63
    It worked!

    Thank you very much!

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    12
    I'm trying to understand how setclip works. Can you explain why do we need to draw from the -ve position of the original image ?

    Thanks

  5. #5
    Super Contributor
    Join Date
    Mar 2003
    Location
    Israel
    Posts
    2,280
    SetClip() sets the clipping region for any subsequent drawing on the Graphics context.
    Now in english:
    What it does is make it so that only what is drawn inside the clipping rectangle is actually drawn. If you draw a pixel outside of the clipping rect then that won't show up in the final result.
    So in this method, I set the clipping rectangle to the size and location of the picture I need, and then draw the big image(the one that contains what I want to draw), at an offset so that the part of the big Image that I want to draw will fall exactly in the clipping rectangle. That way only that part will actually show up since the rest of what I draw is outside the clipping rectangle.
    Hope I made myself clear. Let me know if you need any more clarifications.

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    12
    Thanks a lot for the explanation! its clear now.

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    15
    does calling clipping slow down at all?

    at the moment, i have an array of Image's and if i were to use 1 big image then i can do this clipping method to draw sprites from this big image to the screen?

    will this be SLOW??

  8. #8
    Super Contributor
    Join Date
    Mar 2003
    Location
    Israel
    Posts
    2,280
    Yes it does slow things down.
    How slow? I remember someone on these forums tested it and cam up with the fact that it was around 33% slower.
    Best way to see is test it out yourself.
    But unless you are having memory shortages then there's really no need for it.

  9. #9
    Super Contributor
    Join Date
    Mar 2003
    Location
    Israel
    Posts
    2,280
    Here's the post I was talking about:
    http://nkn.forum.nokia.com/forum/sho...=%2Asetclip%2A

  10. #10
    Regular Contributor
    Join Date
    Mar 2003
    Location
    Kluang
    Posts
    143
    33% ??? That's quite alot.

    There was a bug which I encountered using the Setclip technique.

    My character's animation is done using Setclip, where I have a rectangular image, in which all the pose for jump, go left, go right, die are drawn in.

    I have 5 poses, so there'll be one portion of the image that is left blank, marked "?" in the diagram below.

    |-----------------------|
    | | | |
    | x | x | w |
    | | | |
    | | | |
    | x | x | ? |
    |-----------------------|

    When I run the game, the character will suddenly disappear when I move it at a certain direction, say "w". My current solution is to copy the pose "w" to the "?", and it showed up after that. Is there any other way around this problem? Or is there something I missed out in the SetClip function?

    Thanks,
    YC

  11. #11
    Regular Contributor
    Join Date
    May 2003
    Posts
    72
    Just something to keep in mind: the clip from repaint(x,y,widht,height) is a differnt beast alltogether, it still allows changes made during that call of paint outside of the clipping rectange. They are shown the next time you call a repaint(x,y,w,h) that contains these regions or regualr repaint().

  12. #12
    Registered User
    Join Date
    Sep 2003
    Posts
    5

    Had to split tileset!

    Hi,

    When testing on a real device - S55,
    I found out that the phone had problems loading large
    images and couln'd load the tileset which was 120x270.

    After much tedious testing, I've found that the phone
    will open a 120x90 png and so my solution was to split
    the tileset into 3 images.

    At first I was going to load in each part and cut
    out the individual tiles.... however after making the game
    for MIDP2 and only recently discovering that most phones
    are MIDP1... I can't use the MIDP2 functions to cut out
    the tiles.

    Then I remembered looking at this forum topic yesterday..

    I rebuilt the tileset from the 3 images BUT when testing
    on a real device... for some strange reason, the rebuilt
    image is mainly black - although it works fine on the emulator.
    (I think the S55 implementation is fairly buggy?).

    So at this stage i'm getting fairly pissed off, my current solution
    is to keep the three tileset images and i have wrote a function
    for drawing the tiles... it looks something like this.

    ------------
    public void drawTile(int tileOffset,int x,int y)
    {
    g.setClip(x,y,TILESIZE,TILESIZE);
    if( tileOffset <= 107 )
    {
    int tileX = tileOffset%mapSizeX;
    int tileY = (tileOffset/mapSizeX)%mapSizeY;
    int tileXPos = tileX * TILESIZE;
    int tileYPos = tileY * TILESIZE;
    g.drawImage(tileset1, x-tileXPos,y-tileYPos, Graphics.TOP|Graphics.LEFT);
    }
    else if( tileOffset <= 215 )
    {
    tileOffset-=107;
    int tileX = tileOffset%mapSizeX;
    int tileY = (tileOffset/mapSizeX)%mapSizeY;
    int tileXPos = tileX * TILESIZE;
    int tileYPos = tileY * TILESIZE;
    g.drawImage(tileset2, x-tileXPos,y-tileYPos, Graphics.TOP|Graphics.LEFT);
    }
    else
    {
    tileOffset-=215;
    int tileX = tileOffset%mapSizeX;
    int tileY = (tileOffset/mapSizeX)%mapSizeY;
    int tileXPos = tileX * TILESIZE;
    int tileYPos = tileY * TILESIZE;
    g.drawImage(tileset3, x-tileXPos,y-tileYPos, Graphics.TOP|Graphics.LEFT);
    }
    }
    ------------

    1. Any suggestions on how to improve the situation are welcome. Surely theres a better way???
    2. Have any of you encountered such fustrating problems
    when trying to get things working on a real device.

Posting Permissions

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