Slick Forums

Discuss the Slick 2D Library
It is currently Wed Jun 19, 2013 12:31 pm

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Additive Blending
PostPosted: Fri Feb 24, 2012 9:54 pm 
Offline

Joined: Fri Feb 24, 2012 9:20 pm
Posts: 3
I have a code that's somewhat like this:
Code:
class _ extends BasicGame{
    ...
    public void render(GameContainer gc, Graphics g) throws SlickException{
        myObj.myRender();
    }
}

class myObj{
    Image image;
    ...
    public void myRender() throws SlickException{
        image.getGraphics().setDrawMode(Graphics.MODE_ADD);
        image.draw(x, y);
        image.getGraphics().setDrawMode(Graphics.MODE_NORMAL);
    }
}

When I run the program, nothing gets drawn (and it freezes)... so I'm assuming this code is wrong.

Is there a way to set the draw mode for one image?
I want to avoid changing myRender() to myRender(Graphics g) if possible, because all of my other myRender() methods (the ones that don't change the blend mode) work fine.


Top
 Profile  
 
 Post subject: Re: Additive Blending
PostPosted: Sat Feb 25, 2012 3:20 pm 
Offline
Game Developer
User avatar

Joined: Thu Mar 03, 2011 6:22 pm
Posts: 534
what to you mean it freezes? It's a bit hard to tell where the problem is^^° What does the image look like? Didi you try it with different images? Maybe using TestCases? (Slick has a lot of them)

_________________
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: Re: Additive Blending
PostPosted: Sat Feb 25, 2012 4:16 pm 
Offline
Slick Zombie

Joined: Sat Jan 27, 2007 7:10 pm
Posts: 1477
Few things:
  • Image.getGraphics() uses a hash map lookup, so it's better practice to use the returned variable rather than calling it a whole bunch of times per frame.
  • With image.draw(), you are drawing the image on to itself. Is that intentional?
  • You are forgetting to flush() the image graphics after you're done drawing to the offscreen image.
  • Since you are calling myRender every frame, and you aren't clearing the offscreen image, it is just going to keep drawing on top of itself with MODE_ADD. Eventually the image will turn white or very bright.

All in all, it looks like you aren't sure how to use image graphics. Here's an example:
Code:
Image offscreen;
Graphics offscreenGraphics;
boolean offscreenDirty = true;

public void init(GameContainer c) throws SlickException {
   //memory leak with current Slick build, see here:
   //http://slick.javaunlimited.net/viewtopic.php?f=1&t=4511
   offscreen = new Image(256, 256);
   offscreenGraphics = offscreen.getGraphics();
}

public void render(GameContainer c, Graphics g) throws SlickException {
   //ideally, we only render to our offscreen image
   //when it's dirty and needs updating. this saves
   //us needing to update it every frame
   if (offscreenDirty) {
      offscreenDirty = false;
      updateOffscreen();
   }
   
   //render the image
   g.drawImage(offscreen);
}

public void updateOffscreen() {
   //clear the image so that we start fresh
   offscreenGraphics.clear();
   //draw another image on this offscreen buffer   
   offscreenGraphics.drawImage(myOtherImage, 0, 0);
   //and do whatever else here...
   //...
   
   //finally, flush the graphics
   offscreenGraphics.flush();
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

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