Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (804)
Games in Android Showcase (239)
games submitted by our members
Games in WIP (868)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Brick Breaker game running slow  (Read 4447 times)
0 Members and 1 Guest are viewing this topic.
Offline cookiecompiler

Junior Newbie





« Posted 2015-06-11 15:45:26 »

Hi everyone! This is my first post on this forum. I am on the path of learning java for making games. I have close to intermediate level of java when it comes to making normal swing applications and such but i am new to gaming. I made a brick breaker game to get started but it is skipping frames and running slow. It even stops in the middle while i am trying to play. It shoots up the CPU usage to 15 to 20% as well. Please have a look at the code. Thank you!



http://pastebin.java-gaming.org/6dced309e201c
Offline DarkCart

JGO Kernel


Medals: 124
Projects: 9
Exp: 50 years


It's all in the mind, y'know.


« Reply #1 - Posted 2015-06-11 16:07:13 »

Well, first off, you're using Java2D. Which isn't really good. I know, it's hypocritical for me to be criticizing the tool I use. Anyway, also, you're making 14 instances of Brick here:

Quote
Brick b1 = new Brick();
    Brick b2 = new Brick();
    Brick b3 = new Brick();
    Brick b4 = new Brick();
    Brick b5 = new Brick();
    Brick b6 = new Brick();
    Brick b7 = new Brick();
    Brick b8 = new Brick();
    Brick b9 = new Brick();
    Brick b10 = new Brick();
    Brick b11 = new Brick();
    Brick b12 = new Brick();
    Brick b13 = new Brick();
    Brick b14 = new Brick();

And right after that, you make an ArrayList of Bricks. The key to this is to just use the array list and do brickList.add(new Brick()); 14 times instead of creating 14 instances. Something like this:

1  
2  
3  
for (int i = 0; i < 14; i++) {
brickList.add(new Brick(...));
}


That takes care of those instances. Then, to render, just do this:

1  
2  
3  
for (int i = 0; i < brickList.size(); i++) {
brickList.get(i).drawBrick(g2d, brickList.get(i).getColor(), brickList.get(i).getX(), brickList.get(i).getY());
}


You may have to add some getters to your Brick class, but I'm pretty sure this should take care of some of the  CPU usage

The darkest of carts.
Offline SHC
« Reply #2 - Posted 2015-06-11 16:28:12 »

The key to this is to just use the array list and do brickList.add(new Brick()); 14 times instead of creating 14 instances.

Absolutely wrong. Even when you inlined the new statement into the add method call, you are still creating an instance. However this is clean, as this is short and easy to read.

@cookiecompiler

The actual issue is your game loop, the swing timer is not reliable for game programming, as it is never guaranteed to be accurate. Instead, use System.nanoTime() and write your own game loop instead. See this post for more information on the topic.

http://gameprogrammingpatterns.com/game-loop.html

The next issue is that you are using Java2D. I won't say Java2D is very bad, but it isn't the right choice for programming games, though it can be used to get high frame rates (~120) if used correctly. For starters, I still recommend Java2D. Once you start feeling the limitations, you can move to Mercury (similar to Java2D's Graphics interface, but uses hardware acceleration).

Good luck.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
Administrator

« JGO Overlord »


Medals: 1371
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2015-06-11 16:31:34 »

Stop plugging Mercury to newbies. Seriously.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline SHC
« Reply #4 - Posted 2015-06-11 16:35:41 »

Stop plugging Mercury to newbies. Seriously.

I really like to ask you why, because as I know, it is in a usable state, and not my own (I'm a contributor though). I really don't want to point newbies to un-finished or heavy WIP engines or libraries, but Mercury is usable as I know it.

Offline Slyth2727
« Reply #5 - Posted 2015-06-11 17:27:45 »

Because there are many libraries out there with better structure, performance, community, and support. To put it simply.
Offline ags1

JGO Kernel


Medals: 367
Projects: 7


Make code not war!


« Reply #6 - Posted 2015-06-12 10:23:30 »

...you can move to Mercury (similar to Java2D's Graphics interface, but uses hardware acceleration).

Good luck.

To be fair to Java2D, it does use hardware acceleration.

Offline cookiecompiler

Junior Newbie





« Reply #7 - Posted 2015-06-13 12:47:48 »

Thank you so much @DarkCart and @SHC for your helpful replies. I have i good experience with Swing and not a lot of experience with Java2D. Do you suggest that i move on to a gaming engine? I read a little bit about LibGDX but i dont know a good way to start learning the engine. Can you please help me in this regard? Because ultimately i will have to move on to a gaming engine. Thanks again
Offline DarkCart

JGO Kernel


Medals: 124
Projects: 9
Exp: 50 years


It's all in the mind, y'know.


« Reply #8 - Posted 2015-06-13 12:54:48 »

Thank you so much @DarkCart and @SHC for your helpful replies. I have i good experience with Swing and not a lot of experience with Java2D. Do you suggest that i move on to a gaming engine? I read a little bit about LibGDX but i dont know a good way to start learning the engine. Can you please help me in this regard? Because ultimately i will have to move on to a gaming engine. Thanks again

You're welcome Smiley I say stick with Java2D for a little longer, then maybe switch to something like Slick2D (I know, it's outdated) so you can get a feel for OpenGL, then move on to LWJGL. I'd honestly would not use LibGDX because the Gradle setup is a nightmare. I know from experience.

The darkest of carts.
Offline SauronWatchesYou

JGO Ninja


Medals: 33
Projects: 4
Exp: 2 years


Hi there! :)


« Reply #9 - Posted 2015-06-13 13:21:42 »

The Gradle setup is a nightmare. I know from experience.

You think Huh I think it's extremely simple, especially importing projects into Eclipse. Gradle seems to make it much simpler. 

I wouldn't advise against LibGDX just because of the setup/Gradle. It's a powerful tool, great for creating games for different platforms and requires way less knowledge than core LWJGL. They are both great tools and I would advise looking into both and making a decision. I would suggest ignoring Slick2D as it's pretty much dead. Might as well use something up to date Pointing

If CookieCompiler is just interested in making games fast I would suggest LibGDX, If he/she wants to really have complete control over the project and use lots of OpenGL then LWJGL is the way to go. Could even learn both Smiley

Hey, you! Back to work
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline SHC
« Reply #10 - Posted 2015-06-13 13:32:54 »

What DarkCart said, I agree with him. Use Java2D for some time, and move out of it once you start feeling it's limitations (Maybe slow performance when lots of entities, or you might want to use effects such as lights in your game). Then, it is entirely up to your interest, if you want to make games fast, then use a framework like LibGDX, but if you want to learn what happens under the hood, LWJGL is the way to go.

I would suggest ignoring Slick2D too, because, it's author is now using LibGDX himself, and the project is not appearing to be actively maintained. Compared to that, LibGDX is better I think (I never actually used it, I went the LWJGL way).

Offline cookiecompiler

Junior Newbie





« Reply #11 - Posted 2015-06-13 14:11:27 »

Thanks. This community is really helpful. I would then go with Java2D for sometime since i feel that it teaches me a lot of things that i need to know. I gives me all the information about what is occurring under the hood. Would you guys please point me to good tutorials or good books that i can learn from? I tried using LibGDX with IntelliJ and yes it was some sort of a mess that i didnt want to deal with at this point. Even though it might be easier to make games with it, i think its not for me at this point. I have decided to continue with Java2D. Please guide me regarding the books thanks!
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (581 views)
2019-09-04 15:33:17

hadezbladez (5510 views)
2018-11-16 13:46:03

hadezbladez (2402 views)
2018-11-16 13:41:33

hadezbladez (5772 views)
2018-11-16 13:35:35

hadezbladez (1223 views)
2018-11-16 13:32:03

EgonOlsen (4661 views)
2018-06-10 19:43:48

EgonOlsen (5682 views)
2018-06-10 19:43:44

EgonOlsen (3198 views)
2018-06-10 19:43:20

DesertCoockie (4095 views)
2018-05-13 18:23:11

nelsongames (5115 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!