• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
destructivArts

Image/Animation Loading

4 posts in this topic

Before I start I'd like to say that this is the first really big game I've worked on. So far my programming experience involves a high school level intro to Java class, and the writing of Snake, Pong, Space Invaders, Pac Man and Tetris. I realize that all of them are relatively simple compared to what I'm trying to do now, but I'm learning so if I'm doing something blatantly wrong please be gentle. I still am stumbling around in the dark.

With that said, I'm writing a 2D sidescroller, and everything (so far) was working fine, until I tried to load my first background. The background is a large image that would scroll as the player moves. (It's an 11000 px x 6000 px .png file that is 6.3 MB)
I have several other animations/images that loaded perfectly fine before this.

The way I'm handling animations and images (if this is a bad idea please let me know so I can fix it.) is to load them all in the constructor of the game into two separate handler objects. For example, I have a DataStreamReader that reads an initial_load.txt file and passes the names, paths, and number of frames from this txt file to an AnimationHandler object as Strings. The animation handler takes these strings and uses them to load each frame into an animation object and then the animation into an ArrayList<Animation>.
[CODE]

for(int i=0;i<numFrames;i++){
ImageIcon img = new ImageIcon(this.getClass().getResource(path+name+i+".png")).getImage();
frames.add(new Frame(img));
}
animations.add(new Animation(frames, name));
[/CODE]
For each object I create, I pass it the animationHandler and it finds the appropriate animations for itself. This way I'm not loading the images for each separate object. (This is how I've done it for my earlier games.)

What I've learned is that Java creates a separate Thread for each image it is trying to load. After too many threads are loaded it will produce the error I'm getting:
[CODE]
Exception in thread "Image Fetcher 0" java.lang.OutOfMemoryError: Java heap space
at java.awt.image.DataBufferInt.<init>(DataBufferInt.java:41)
at java.awt.image.Raster.createPackedRaster(Raster.java:458)
at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1015)
at sun.awt.image.ImageRepresentation.createBufferedImage(ImageRepresentation.java:230)
at sun.awt.image.ImageRepresentation.setPixels(ImageRepresentation.java:535)
at apple.awt.OSXImageRepresentation.setPixels(OSXImageRepresentation.java:58)
at sun.awt.image.ImageDecoder.setPixels(ImageDecoder.java:120)
at sun.awt.image.PNGImageDecoder.sendPixels(PNGImageDecoder.java:531)
at sun.awt.image.PNGImageDecoder.produceImage(PNGImageDecoder.java:452)
at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:246)
at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:172)
at sun.awt.image.ImageFetcher.run(ImageFetcher.java:136)
[/CODE]

Nothing I've read has really explained how to go about fixing this.

That's everything I know. I'm really not sure what to do although I'll keep looking.
Any help is greatly appreciated,
Thanks
Peter
0

Share this post


Link to post
Share on other sites
Your issue is that you ran out of memory, in Java I think the default is set to like 64 or 96 megabytes for your application, if your application takes up more memory then this you get that error. This can be easily remedied by you passing a certain argument when running your java program telling it to allocate more space, whether its 128 megabytes, a gigabyte etc.... I found in game programming it is very easy to go over this initial limit.

The paramater u want to pass is the following.

java -Xmx2g mygame
which will give you 2 gigabytes for your game or
java –Xmx128m mygame
which will give you 128 megabytes for your game.

I think you get the idea here, if you are having any other issues let me know.
0

Share this post


Link to post
Share on other sites
Thanks!
How would I do that from within the game? Because when the game is finished I don't want people to have to go through and do that just to run the game.

I guess I'll go ahead and ask this just to be sure: Is the way I'm handling images and animations ok?

Thanks again
Peter
0

Share this post


Link to post
Share on other sites
You could do it that way, but I believe if you load a sprite sheet instead and and just render certain parts of the sprite sheets (in reference to its current animation), you should see some performance increase.

Unless you plan on the user themselves running the game from console (i.e you tell them to go type in java myGame) then there really shoudln't be an issue, you can obscure (hide) those parameters from the user in many ways, so they basically just click on something to run the game, you might want to look at packaging it all into a jar and looking up making executable jar files. That can be an alternative.


EDIT!!! I just noticed you wrote that the background image is a 11000 px x 6000 px, at first i understood this as a 1100 x 600 px. this is going to take a HUGE chunk of memory and i dont recommend directly loading it, split this image into many pieces and load one up at a time. Even though the image is PNG and depending how you are doing things this image can take up to 264megabytes of DATA!!!. Edited by The_Neverending_Loop
1

Share this post


Link to post
Share on other sites
Thanks again for the advice. I'm not to the point yet where I am doing stand alone builds of the game yet, and I'm running it from within Netbeans. Is it possible to alter the amount of memory available to Netbeans?

Also I split the background apart into pieces. They're now 2000 x 1500 px and as I'm loading them I'm getting the same error as before.
If the problem is the game is loading too many images at once, is there a way to check if an image is finished loading before continuing?

Thanks again,
Peter
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0