SWY1985 / CivOne

An open source implementation of Sid Meier's Civilization.
http://www.civone.org/
Creative Commons Zero v1.0 Universal
244 stars 49 forks source link

Annoying lag #375

Closed axx0 closed 6 years ago

axx0 commented 7 years ago

In one game clicking on a city or moving resources in the city menu causes a small but annoying (~0,5 s) lag.

SWY1985 commented 7 years ago

That's unacceptable, as the lag wasn't there before. I've touched a lot of city logic yesterday so I probably made a mistake somewhere...

SWY1985 commented 7 years ago

I cannot reproduce this. Do you get this lag from the start of the game, or later on?

axx0 commented 7 years ago

It was at the beginning. My capital was 6 or something. I'll try to reproduce it later today.

SWY1985 commented 7 years ago

Thank you, I will keep playing the game and see if I notice some lag. I'll also try it on some other computers later today.

axx0 commented 7 years ago

OK I just played a couple of games. It seems (in the second game for sure!) that building Colossus introduces this lag.

AlexFolland commented 7 years ago

There are definitely sources of lag that aren't present in the original game, and the maxed-out single-thread CPU usage should be addressed at some point. Busy-waiting is not an efficient use of CPU time.

SWY1985 commented 7 years ago

@AlexFolland

I have tried adding Thread.Sleep(1) to several places in the game but it doesn't help a bit. It only causes more lag, and the CPU usage remains the same.

The 100% CPU-core usage is just the way OpenTK (and probably most other modern games) work. Please read more about it (and why it's not an issue) here: https://github.com/opentk/opentk/issues/137

Some more info here (first answer): http://stackoverflow.com/questions/5829881/avoid-waiting-on-swapbuffers

Basically, if another application wants to share the CPU-core, CivOne should just reduce it's CPU usage automatically. There's not a lot that I can do about the CPU usage at the moment, sorry, but I am sure that this is not the source of the lagging issues:

CivOne does many things differently than the original game, often in a different order or more things at the same time. Any lag issue in the game currently is caused by too many tasks trying to finish in the same frame. I am moving task #350 forward to version 0.1.0-alpha.1. I am hopeful that, by moving several tasks to background threads, the lagging issues will vanish. I need to plan this very carefully though, so please be patient while I think up the perfect solution.

Edit: I have managed to get the CPU usage down by adding Thread.Sleep(1) before SwapBuffers in the Render function, but it lowers the framerate considerable and I don't think that's a good idea.

axx0 commented 7 years ago

Played another round, Colossus is definitely the culprit.

EDIT: Another thing - after Colossus was built I saved the game, quit and reloaded. Instead of colossus now Hanging gardens appear in the city.

SWY1985 commented 7 years ago

Thank you for testing again. I will see if I can find what causes it. Nice find on the wonders being saved wrong, I'll fix that too.

mcmonkey4eva commented 6 years ago

I have managed to get the CPU usage down by adding Thread.Sleep(1) before SwapBuffers in the Render function, but it lowers the framerate considerable and I don't think that's a good idea. I'd recommend making it an optional feature, and when enabled making it run specifically when frame time is low (and thus FPS is high) Alternatively, you can take advantage of my OpenTK PR here: https://github.com/opentk/opentk/pull/472 or any equivalent modification of your own design.

The CPU pegging is unacceptable and I'm rather disappointed that OpenTK has never taken steps to fix it, and they as well reject attempts to fix it with bad reasoning like "The game SHOULD peg the CPU, it's a game!" which makes sense until even the main menu of a game pegs a CPU, and all the peg time is it doing literally nothing but spinwaiting...

SWY1985 commented 6 years ago

I have implemented an SDL runtime to replace the OpenTK runtime. #419 The CPU usage is significantly lower. Could someone test the lag with the SDL version? I will post binaries to the Civfanatics forums later this week.

SWY1985 commented 6 years ago

New binary is available, but only the 64-bit Windows version for now. The 32-bit version crashed when I tried it: https://www.civone.com/Download

This new version is a lot faster, drawing code has been optimized. When the game is idle, it only uses 0.5 to 3.5% CPU on my computer.

axx0 commented 6 years ago

Yes so far no problems with lag. I'll just close the issue and open it in case of such problems.