Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Standardizing Frame Rates and Handling Objects


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 destructivArts   Members   -  Reputation: 205

Like
0Likes
Like

Posted 17 April 2012 - 08:04 PM

I'm writing a 2D platform game engine in Java using the awt package. (its pretty basic right now)
I have everything set up in a class that extends JPanel, so I'm using the paintComponent method to draw graphics on screen.
The class also implements Runnable, and I'm using a thread, which sleeps for a constant amount of time, to keep frame rate.
My first problem, however, is that the image keeps flickering randomly.
I looked up ways to solve this, the first being to Double Buffer, which I have successfully done. I use a separate method to load all the graphics onto a BufferedImage with Graphics2D. While this helped, it still flickers quite a bit.
I also read on the Java API that the repaint() call will be processed as soon as possible. I guess this means that it won't always happen immediately.
So my first question is: How would I rewrite, or create a new, paint method that I can have executed immediately?

My second question deals with handling my game objects.
Everything that appears on screen that the player can interact with in some way (platforms, enemies, doors, etc.) is a child class of an Entity class. Sometimes, like when the player nears the side of the screen, I need to affect all the objects at once. In this case I need to shift them all. Now, all of these objects are in separate ArrayLists, with one ArrayList for each type of object. Would it be a bad idea to put all the ArrayLists inside a Vector so that I can just loop through the vector and then each ArrayList so that I don't have to call them by name? My problem here is that I don't know how many object types I'm going to need, and it would be easier to write a general, nested for loop that only has to call the vector by name.

Any help would be greatly appreciated,

Thanks,
Peter
-------------------------------------
"Other than that, I have no opinion."
My Blog - Check it Out

Sponsor:

#2 Kyan   Members   -  Reputation: 395

Like
1Likes
Like

Posted 17 April 2012 - 08:36 PM

So my first question is: How would I rewrite, or create a new, paint method that I can have executed immediately?

Use a BufferStrategy. It allows you to "blit" to an off-screen image like you would normally using an API like SDL and doesn't restrict you with Swing's obnoxious threading issues with regards to paint() and repaint(). That is, you can flip() your buffer whenever you like, thus controling the rendering speed yourself (for the most part, but it is an improvement since Swing tends to ignore repaint() requests at random). Furthermore, the surface is almost guaranteed to be hardware accelerated and Java will handle all the details for you. Here's a simple example: http://gpsnippets.bl...gy-in-java.html

Would it be a bad idea to put all the ArrayLists inside a Vector so that I can just loop through the vector and then each ArrayList so that I don't have to call them by name?

First, a Vector is just a synchronized ArrayList so there's not any particular need to use them. If you're having synchronization issues, I'd look at an alternate strategy (synch blocks, copy-on-write, etc.) instead to avoid any potential overhead.

Second, to answer your specific question - not really. I mean, it's not ideal or anything but you aren't going to fail miserably if you do it until you start generating a lot of objects. If it works well enough for you, more power to you.

That said, if you are having issues or want something that's a bit more scalable, I'd look into an alternate partioning scheme such as a basic quadtree or similar. The key issue is that most of those objects don't move - things like doors and static platforms don't actually change their positions. You can track an offset into your level and compute the "drawing" coordinates from that without needing to store those objects, drastically cutting down the objects you need to iterate over on a per-frame basis.

#3 destructivArts   Members   -  Reputation: 205

Like
0Likes
Like

Posted 17 April 2012 - 09:50 PM

Ok, I'll look up the BufferStrategy. Thanks, that sounds exactly like what I was looking for.

On the second issue though, (I'll admit your using a lot of words I am unfamiliar with)
but just to clarify:
- All objects in my game will 'move'. They have an initial x and y position, but when the player moves into a region roughly 100 pixels from the edge of the screen, instead of the player moving, all the objects move in the opposite direction. Because it's not a 3d game, I don't see how I can move a camera that dictates what's on screen. (Although now that I think about it, I can think of several ways I could do just that.)
- The only reason I suggested Vectors is because I know they can hold different data types within one Vector.

I'm relatively new in this area of programming, so I apologize for any misconceptions I have regarding this type of code,
Thank you for your help,
Now... to Google!

Thanks again,
Peter
-------------------------------------
"Other than that, I have no opinion."
My Blog - Check it Out

#4 deepDweller   Members   -  Reputation: 104

Like
1Likes
Like

Posted 18 April 2012 - 02:27 PM

I'll admit that I'm not quite sure what a quadtree is, but I think what Kyan is getting at is that if you have a bunch of objects that don't actively move around the screen (such as doors as were his example), then you may be able to have those objects themselves process, in their own code, the camera offset and apply it to their draw position, saving you the trouble of storing them in a container and looping through them whenever the big camera shift function is called.

#5 destructivArts   Members   -  Reputation: 205

Like
0Likes
Like

Posted 18 April 2012 - 08:21 PM

Ok, I've implemented the idea of using a camera object to get everything thats on screen. Now i only move the player and the camera when the player nears the edge of the screen. Thank you guys so far, now I have to read up on these BufferStrategies.

Thanks again,
Peter
-------------------------------------
"Other than that, I have no opinion."
My Blog - Check it Out




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS