Slick Forums

Discuss the Slick 2D Library
It is currently Thu Apr 18, 2019 2:18 pm

All times are UTC




Post new topic Reply to topic  [ 22 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: Sun Jul 10, 2011 5:10 am 
Offline

Joined: Thu Nov 20, 2008 5:16 am
Posts: 76
I'm doing some render texture work and I noticed a few apparent problems with the copyArea call in the Graphics class.

Firstly, copyArea seems to flip the destination vertically:

Image

Secondly, there are some strange inconsistencies. When you remove the green drawRect I have, the area copied by the copyArea becomes completely black in the destination. I played around with the GL_RGBA and GL_RGB texture format flags and that didn't seem to fix it, so I'm not sure what the issue is there.

I'm not sure if it's some state that isn't set properly at some point or what.... some combinations of flush() also seem to cause the problem.

Anyway, here's a replication class set up as one of the slick tests:

Code:
package org.newdawn.slick.tests;

import org.newdawn.slick.*;
import org.newdawn.slick.util.Log;

public class ImageCopyAreaTest extends BasicGame {
   private Image logo;

   private Graphics renderGraphics;
   private Image renderImage;

   // in my real app, we have many of these...
   private Image copiedImage;

   /**
    * Create a new image rendering test
    */
   public ImageCopyAreaTest() {
      super("ImageCopyAreaTest");
   }

   public void init(GameContainer container) throws SlickException {
      logo = new Image("testdata/logo.png");

      try {
         renderImage = new Image(256, 256);
         renderGraphics = renderImage.getGraphics();
      } catch (SlickException e) {
         Log.error("creating local image or graphics context failed: " + e.getMessage());
      }

      copiedImage = new Image(256, 256);

      generate();
   }

   public void generate() {

      Graphics g = renderGraphics;

      g.setColor(Color.pink);
      g.fillRoundRect(0, 0, 256, 256, 15);

      g.drawImage(logo, 0, 0);

      g.flush();
      try {
         copiedImage = new Image(256, 256);

         // add outline. On my machine removing these lines causes the copyArea call to become entirely black.
         // Perhaps some openGL state that is set by the drawRect? No idea.
         g.setColor(new Color(0, 255, 0, 128));
         g.drawRect(0, 0, copiedImage.getWidth() - 1, copiedImage.getHeight() - 1);

         //copy to local image.
         g.copyArea(copiedImage, 0, 0);
         g.flush();
      } catch (SlickException e) {
         e.printStackTrace();
      }

   }

   public void render(GameContainer container, Graphics g) throws SlickException {
      // Draw the one we rendered to, then the copy in a new image.
      g.drawImage(renderImage, 100, 172);
      g.drawImage(copiedImage, 444, 172);
   }

   public static void main(String[] argv) {
      try {
         AppGameContainer container = new AppGameContainer(new ImageCopyAreaTest());
         container.setDisplayMode(800, 600, false);
         container.start();
      } catch (SlickException e) {
         e.printStackTrace();
      }
   }

   public void update(GameContainer container, int delta) throws SlickException {

   }
}


Thanks for any insights/workarounds/fixes!

-Andy


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 10, 2011 9:36 pm 
Offline

Joined: Thu Nov 20, 2008 5:16 am
Posts: 76
It seems I accidentally put this in the Resolved Bugs forum... I meant for it to be a bug report. Seems like I can't move it myself... could I ask a moderator to move it for me?

Sorry and thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 11, 2011 1:27 am 
Offline
Regular
User avatar

Joined: Wed Apr 27, 2011 3:29 pm
Posts: 197
Location: United State of California
I only have a suitable workaround: copyArea() seems to only work reliably with the default graphics object. You can get this with container.getGraphics(). There is no consequence for using this anywhere besides inside the render() method. Additionally, this eliminates having to create a back buffer so the benefit is two-fold. I can post some example code if you really need it.

Otherwise, I can confirm that copyArea() is just plain weird in other circumstances. It was very frustrating when I experimented with it myself.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 11, 2011 11:00 pm 
Offline

Joined: Thu Nov 20, 2008 5:16 am
Posts: 76
Interesting info, thanks. I can give that a shot.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 12, 2011 10:40 am 
Offline
Game Developer
User avatar

Joined: Thu Mar 03, 2011 6:22 pm
Posts: 534
And the funny thing is, the copyArea method is really just a simple glCopyTexImage2D...

you should really have the Graphics parameter in your generate method, and use the method from this Graphics. Graphics does not work exactly like the normal Java version :)

_________________
Current Projects:
Image Mr. Hat I
Image Vegan Vs. Zombies
Projects:
RadicalFish Engine - Build on top of Slick2D, Ideas, Bugs? Open an Issue ticket!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 12, 2011 8:01 pm 
Offline

Joined: Thu Nov 20, 2008 5:16 am
Posts: 76
R.D. wrote:
you should really have the Graphics parameter in your generate method, and use the method from this Graphics. Graphics does not work exactly like the normal Java version :)


I'm rendering to an offscreen image because I'm doing some expensive drawing that I'm saving to image to display later.

Per:

http://slick.cokeandcode.com/wiki/doku. ... o_an_image


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 13, 2011 4:16 am 
Offline
Regular
User avatar

Joined: Wed Apr 27, 2011 3:29 pm
Posts: 197
Location: United State of California
I used that guide as well. Using the default graphics object instead of image.getGraphics() also avoids that delay the author complains about at the end. Have any luck with that?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 13, 2011 1:55 pm 
Offline

Joined: Thu Nov 20, 2008 5:16 am
Posts: 76
Yeah, using the default graphics context does work quite well. Thanks everyone for the suggestions.

It feels a little weird, since I don't want this immediately on the screen, but as long as you clear after you are done doing your copyAreas, it's all the same anyway.

It's spiffy, since things aren't even upside down, and it doesn't exhibit that strange behavior where it only works if you do the line drawing.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 13, 2011 2:38 pm 
Offline
Game Developer
User avatar

Joined: Thu Mar 03, 2011 6:22 pm
Posts: 534
what do you mean "right on the screen?" copyArea just copies into the image... nothing else^^

_________________
Current Projects:
Image Mr. Hat I
Image Vegan Vs. Zombies
Projects:
RadicalFish Engine - Build on top of Slick2D, Ideas, Bugs? Open an Issue ticket!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 13, 2011 3:47 pm 
Offline

Joined: Thu Nov 20, 2008 5:16 am
Posts: 76
Not the copyArea call, but drawing to the graphics context for the screen, instead of drawing to an offscreen graphics context.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 13, 2011 6:27 pm 
Offline
Regular
User avatar

Joined: Wed Apr 27, 2011 3:29 pm
Posts: 197
Location: United State of California
AndyKorth wrote:
...doesn't exhibit that strange behavior where it only works if you do the line drawing.


Yeah, I gave up trying to do it the normal way when everything I did was translated, flipped, and colored the wrong way. I'm glad it's working out for you now!


Top
 Profile  
 
PostPosted: Sun Nov 13, 2011 4:58 am 
Offline
User avatar

Joined: Fri Oct 07, 2011 11:05 pm
Posts: 97
Location: France, Nantes
Sorry to dig out this topic, but does someone has an explanation for that bug? I tried to understand it, but it is too much low level for me :/

_________________
"The Keeper is aware. The Keeper understands. The Keeper has seen the enemy."


Top
 Profile  
 
PostPosted: Sun Nov 13, 2011 5:28 am 
Offline
Regular
User avatar

Joined: Wed Apr 27, 2011 3:29 pm
Posts: 197
Location: United State of California
I believe it's been brought to Kevin's attention. I linked to it in the status thread that he started. Hopefully it will be explained or fixed by his next bug run.

_________________
Current Testing Machine:
Windows 7 x64, 4GB RAM DDR3, 3.3ghz AMD Athlon II X3 455, AMD Radeon HD 6800 series.


Top
 Profile  
 
PostPosted: Sun Nov 13, 2011 5:54 am 
Offline
User avatar

Joined: Fri Oct 07, 2011 11:05 pm
Posts: 97
Location: France, Nantes
raeleus wrote:
I believe it's been brought to Kevin's attention. I linked to it in the status thread that he started. Hopefully it will be explained or fixed by his next bug run.

Oh, good news then ;)

_________________
"The Keeper is aware. The Keeper understands. The Keeper has seen the enemy."


Top
 Profile  
 
PostPosted: Mon Nov 14, 2011 1:32 pm 
Offline
User avatar

Joined: Mon Jun 20, 2011 4:28 pm
Posts: 71
IgoR wrote:
Sorry to dig out this topic, but does someone has an explanation for that bug? I tried to understand it, but it is too much low level for me :/


no OpenGL expert or anything, but could it be a problem with inverse coordinates ?
As I recall OpenGL actually uses a coordinate system which has high Y value at the top and low at the bottom, just like you normally have on a math graph, unlike the standard in computer graphics

so its kinda like a reverse rectangle then

but then again why does it work with the GC - G object, hmm...

anyway here is the line that is being used

Code:
target.bind();
GL.glCopyTexImage2D(SGL.GL_TEXTURE_2D, 0, format, x, screenHeight - (y + target.getHeight()), target.getTexture().getTextureWidth(), target.getTexture().getTextureHeight(), 0);
target.ensureInverted();


Image class method
Code:
public void ensureInverted()
{
    if (textureHeight > 0) {
        textureOffsetY = textureOffsetY + textureHeight;
        textureHeight = -textureHeight;
    }
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 22 posts ]  Go to page 1, 2  Next

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group