Java - Slick - Deleting Images

Apprentice
Posts: 7
Joined: 2011.09
Post: #1
Hey there, good people of iDevGames.

I'm various_names__ and I've been reading the forums for a while, and I couldn't find one thing;

I wanted to know how (or if) you could remove (read: delete, get totally rid of) and image in slick, after a certain event happens.

Like in this pseudo code:

Code:
...Blahblah,other-stuff
public Image nyancat = null;
nyancat = new Image("Desktop/pictures");

putOnScreen(nyancat);

//This is where I want to remove the image, for example
remove.nyancat();
...andotherstuff

I have a Graphics component (or whatever its called), which I use for drawing the image, and i use an Image data type from the
Code:
org.newdawn.slick.Image
library to load, and keep the image in.

I wanted to know this because I'm a bit of a newbie to Slick/Java, and so I'm learning by making a simple space shooter game, and I would like to keep it in top quality (framerate-wise Wink), and I'm worried that if I start loading too many bullets the game might lag.


Oh, btw, one last thing; what is the optimal way of loading the bullets? Should I load them individually, or off a list, or what? Any help is appreciated.

Thanks!
various_names__
Quote this message in a reply
Moderator
Posts: 453
Joined: 2008.04
Post: #2
I'm not quite sure what you mean when you say you want to remove an image.

Probably you just want to stop drawing the image on the screen, so don't call nyancat.draw() in your render loop (or graphics.draw(nyancat); )

I expect slick to handle any number of bullets you can manage. I render several hundred objects with slick each frame and it's no problem. Just try it. If you do run into performance problems, come back and we can discuss more solutions.

If you actually do need to destroy images, the Image class does have a destroy method- but you almost certainly should not be calling destroy unless you are dynamically creating images in your game using render textures or something like that.

You should be doing something like:
Code:
    public void render(GameContainer container, Graphics g) throws SlickException {
        boolean drawImage = true; // or some other condition....
        
        if(drawImage){
            g.drawImage(image, 300,270);
        }
}

Also a side note, I'm the only one here who ever uses Slick.. you'll find a more active slick community here:

http://slick.javaunlimited.net/

Howling Moon Software - CrayonBall for Mac and iPhone, Contract Game Dev Work
Quote this message in a reply
Apprentice
Posts: 7
Joined: 2011.09
Post: #3
Hey Andy, thanks alot for the very useful (and polite! Smile) reply.

Your post answered all my questions, except for one;

How can I draw several images of the same type?
For example, let's pretend I have this code (Using nyancat as an example again Wink):

Code:
Image nyancat = null;

public void drawNyan(Graphics g){

    nyancat = new Image("Desktop/picture.png");

    g.drawImage(nyancat, 200, 300);

}

Then I would run drawNyan in my update loop. However, that would only draw one nyancat to the screen. How could I draw several nyans, without going too overboard with the code? Is there some way to run the drawNyan function several times and make it draw different nyans to the screen (while using the same "Image nyancat" data)? Because from my knowledge, for as long as I run the drawNyan code is for how long the nyancat will stay displayed, when the drawNyan function stops being used the nyancat goes invisible, while I need it to generate a new nyancat.

Thanks for all your help,
various__
Quote this message in a reply
Moderator
Posts: 453
Joined: 2008.04
Post: #4
As you probably suspected, you can just:

Code:
g.drawImage(nyancat, 200, 300);
    g.drawImage(nyancat, 250, 300);
    g.drawImage(nyancat, 300, 300);

etc. But you probably will want to do something like:

Code:
LinkedList<Sprite> allCats = new LinkedList<Sprite>();

--- snip ----
public void drawAllCats(Graphics g){
for(Sprite sprite : allCats){
    g.drawImage(sprite.image, sprite.x, sprite.y);
}
}

So you'll need to make a sprite class (or call it entity, or whatever). The plus here is that each cat can have it's own x and y position. It can even be used for different cats- just give one cat the Nyan cat image, and another cat the NEDM cat image, etc. Then by changing the position of the sprites, it will draw at the new position.

You can then add or remove sprites to this list at will- add new cats, remove old cats, etc.

Good luck!

Howling Moon Software - CrayonBall for Mac and iPhone, Contract Game Dev Work
Quote this message in a reply
Apprentice
Posts: 7
Joined: 2011.09
Post: #5
*nod* Yeah, that works just fine, and fixed my problem for allocation of the (nyan)cats. But now I have another problem: How do I create nyancats dynamically during run-time?

For example, let's pretend my game is running, and when the player presses the spacebar, I want to grab a cat from that list and render it to the screen. Yeah, that works just great, but how do I populate the LinkedList with cats for the game to use to start with?

I mean, surely I could do this:
Code:
...

public void init(GameContainer container) throws SlickException {

...

    static nyanEntity nyan001 = new nyanEntity("data/nyan.png", 200, 300);

...

}


With nyanEntity having a constructor of (location of cat image, x position, y position), but that's generating the nyans manually. How could I make it so that a function of some sort creates nyans, names them individually (e.g. nyan001, nyan002, nyan003) and inserts them into the LinkedList? I mean, I'm not sure of any way of naming variables dynamically, is there a way?

So I need to:
  1. Create dynamically named nyans (nyan001, nyan002, etc.)
  2. Insert those nyans into the list (I think its: "nyanList.add(nyan001);", right?)
  3. During run-time access those nyans (Which I could probably do by running a loop through, and checking the nyan's position, etc.)



The only thing I really need help with is the first thing (naming them dynamically). But anyway, thanks alot for your help Andy, I really appreciate it Smile
Quote this message in a reply
Moderator
Posts: 453
Joined: 2008.04
Post: #6
Well the point of having a list is that you don't have a separate variable for everything you add- obviously that won't really work when you have a game with 500 cats, 2000 bullets, etc.

You can just add the cats within a loop also. You should just be creating them as local variables, it sounds like you might want to brush up on how variables and stuff works. You don't even need to assign them to variables if you are immediately putting them in a list.

Code:
for(int i = 0; i < 10; i++){
     nyanList.add(new nyanEntity("data/nyan.png", 200, 300));
}

That will add 10 nyans, they don't need to be individually named. Accessing that list of nyans will probably be done by a loop for most circumstances.

Howling Moon Software - CrayonBall for Mac and iPhone, Contract Game Dev Work
Quote this message in a reply
Apprentice
Posts: 7
Joined: 2011.09
Post: #7
(Sep 5, 2011 10:19 AM)AndyKorth Wrote:  Well the point of having a list is that you don't have a separate variable for everything you add- obviously that won't really work when you have a game with 500 cats, 2000 bullets, etc.

Agreed.

(Sep 5, 2011 10:19 AM)AndyKorth Wrote:  it sounds like you might want to brush up on how variables and stuff works.

Yeah, my bad, it was late at night when I posted, and my head was all over the place; I had an idea of assigning each nyan in the list with an individual ID, so that I could reuse them, and for debugging. But a while later of reading your post (and re-reading mine to make sure I hadn't ingested any illegal substances that had acted as retardants/made me trip on ballz at the time of posting), I realized my ignorance at the time.


(Sep 5, 2011 10:19 AM)AndyKorth Wrote:  Accessing that list of nyans will probably be done by a loop for most circumstances.

*nod* With a (enhanced) for loop, I could probably do something like run through the list, and check each nyan's position to see if he is outside of the screen, and if so to reuse him to save space, etc. (us C++ programmers like doing that Wink (Saving space that is, not reusing cats LOL ))

Well, I think that's most of my problems are fixed (until I get to implementing the code Rasp ), thank you SO much for all your help Andy; most people would have told me to "f*cking Google it!" or to "Use the search bar again, please, we don't want trolls messing up the forum *hurr durr hurr*" So, you know, thanks Smile .

See ya!
various__


[EDIT]

Hehe, I'm back.
One last question, not nyanList related. I'm loading a large png file for my game's background, but I keep getting the error:

"class org.newdawn.slick.opengl.PNGImageData failed to read the data
java.io.IOException: Not a valid PNG file"

followed by numerous references to where this error occurs. I've checked my code and I'm sure I'm loading the image correctly. Is there some kind of maximum size for an image in slick, or is this related to my file's encoding or something?

Heh, thanks again Andy!


[EDIT 2]

Sorry, didn't want to triple post, but...
Reclaimed looks amazing! Wow! It looks so good!
The server isn't still up, is it? I'd have loved to have played it...

But, yeah, good job! Smile
Quote this message in a reply
Moderator
Posts: 453
Joined: 2008.04
Post: #8
How large is large? if it's over 2500 pixels on a side, then it's probably waaaaay too large anyway, and you'd need to think about what you're doing with it and consider some other technique.

I guess you can check the encoding, but if Preview opens the png, it should probably be opened by slick.

Glad you liked how Reclaimed looks- nah the server usually isn't up but I can turn it on.

Howling Moon Software - CrayonBall for Mac and iPhone, Contract Game Dev Work
Quote this message in a reply
Apprentice
Posts: 7
Joined: 2011.09
Post: #9
It's about 900x480; the size of my game screen (weird dimensions, i know) and it opens properly in any paint/picture program; I'll have to check the encoding.

Another question; what is the most supported script that Slick/Java supports? I need one that can access Java objects, create variables, change variables in a Java object, etc. All the good stuff.

I found Rhino, but Google didn't wanna give me any tutorials except for some (really) outdated ones.

Thanks Andy!
Quote this message in a reply
Moderator
Posts: 453
Joined: 2008.04
Post: #10
A scripting language to include? Unless you know of a language you like better than Java, I would recommend using Java for the whole project. A lot of people will integrate something like Lua into their game, because they like Lua a lot more than the C/C++ code the rest of their engine is written in. But y ou don't have to deal with the problems of C/C++, and you don't seem to have a favorite scripting language already learned and picked out.

Perhaps others here will have different opinions though.

Howling Moon Software - CrayonBall for Mac and iPhone, Contract Game Dev Work
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #11
I see no particular benefit to a scripting language over Java itself unless there is some feature (perhaps keybind macros?) that's more easily implemented in the scripting language.

Java's fast, has a good GC, etc. and it's not particularly hard to write...
Quote this message in a reply
Apprentice
Posts: 7
Joined: 2011.09
Post: #12
It's not that I'm stuck or having problem with something, it's just that I wanted to have enemy AI/weapon AI in scripts; to manage them better, etc.

That way, I could have the AI in a script, and then in my enemy class, instead of extending it for each new enemy type/etc., I could just load a different script.

But, whatever, if you guys think that's a bad idea then I'll just write it all in Java.

Thanks!
Quote this message in a reply
Post Reply