I made this minigame for project (with a lot of stolen gifs lol, ill correct that later) and it works pretty well... except for its incredible cpu usage of 50%, even though the graphics are 8 bit and obviously dont take up a lot of graphics painting space... How the code works is pretty simple:
The code and is attached (not the game because zip files are not allowed). I'm not sure what the problem is, is it the huge amount of arraylist for loops that might be lagging out the cpu? the duration of the timer has to be 5 otherwise the physics would be glitchy so I can't change that.
Also this is the mac version so sorry windows users :P I'm making the windows version later after I fix the copyright
Ash Whin wrote:I'm not sure what the problem is, is it the huge amount of arraylist for loops that might be lagging out the cpu? the duration of the timer has to be 5 otherwise the physics would be glitchy so I can't change that.
That's 5 milliseconds per whatever it is you're doing repeatedly. Or 200 times per second if you want to look at it another way. I'd try doing the repetition a lot less frequently until you get a number where the performance and the physics work satisfactorily.
I edited the thread type and now the cpu usage is at 20%. Much better but not good enough for an 8 bit game :P How can i get it to around 10% and below?
You shouldn't do anything to the UI in any other thread. See Concurrency in Swing for more information. Also, you know that you're sleeping one millisecond each time there, right? That's basically the same as not sleeping at all.
Did you read Paul's comment on the number of repaints per second? 200 is much too high. Trying to get 60 is a lot more realistic. That would give you a delay of 16ms instead of 5.
I tried using some concurrency but it didn't do too much unfortunately, its stuck at 25% CPU with the following code SwingWorker and ActionListener combo:
I'm not getting this very well, can someone give me an explanation on what I'm doing wrong?
there is a difference between updating your game and displaying it. The updating can be done as many times per second as you like, or is feasable, while displaying could be done in another tempo, maybe every three to four updates. To get a decent balance between the two, you should do some measurements how long an update takes. Have you considered some hardware accelleration like JavaFX or OpenGL?
There are three kinds of actuaries: those who can count, and those who can't.
There are all sorts of confusing things about your code, apart from why are you writing while (true) ...?
Ash Whin wrote:
Those aren't doubles on lines 15‑16; they are ints. Why are you mixing doubles and floats? Why are you using floats at all? Just because line 20 compiles and runs, that doesn't make it is correctly typed. What does that bit about nanoTime mean in line 19? Why have you got a loop to reduce delta below 1? What is wrong with the % operator? Why are you trying to deal with sizes < 1?