BP Logo
Home » SIRTECH CLASSICS » Jagged Alliance: Unfinished Business » Vanilla Modding » "Cool" patch...
"Cool" patch...[message #102405] Tue, 06 June 2006 00:07 Go to next message
mtx

 
Messages:10
Registered:December 2005
Location: Lisboa, Portugal
Not exactly a source code project, but didn't know where to put it. The 1.13 forums don't deal with C code, and there isn't a public source code repository available...

I patched gameloop.cpp to avoid having ja2 keeping the CPU at 100%.

Problem: I usually play ja2 in a laptop, and hogging the CPU:

a) drains battery real quick
b) turns the laptop into a toaster. I mean HOT! :welder:

For desktop people: a cooler PC means less noise from the fans.

(btw, background tasks would get done faster if they could use the CPU, but that's not important)

Rationale: this game doesn't really need to have a huge framerate; 100Hz seems a good upper limit.

Solution: at each frame refresh, add a Sleep(SOMEMILLISECONDS) to pace the
refresh rate.

So:

 
--- gameloop.cpp.old    2006-06-02 19:24:15.000000000 +0100
+++ gameloop.cpp        2006-06-02 19:21:55.000000000 +0100
@@ -322,6 +322,8 @@
                ReportMapscreenErrorLock();
        }
        #endif
+       //OK! 12% CPU:
+       Sleep(10);
 
        //DebugMsg (TOPIC_JA2,DBG_LEVEL_3,"GameLoop done");
 } 
 
Yeah, it's a really trivial patch... In fact, this does not change the game at all. Not it is supposed to, it's just to make it easier on the CPU.

In my Asus (Centrino, 1.4GHz), there is no difference in gameplay, only in temperature and fan noise...

Tested with 1.13 (last November release).

If anyone tries it, please post here. Thank you.
Re: "Cool" patch...[message #102406] Tue, 06 June 2006 04:35 Go to previous messageGo to next message
Augustus

 
Messages:7
Registered:June 2004
Location: Spain
It seems a nice addition!

A new release is coming. I hope this can be added. :ok:

Let me suggest you to post this on "V1.13 mod development", to get more and better answers.
Re: "Cool" patch...[message #102407] Tue, 06 June 2006 23:33 Go to previous messageGo to next message
defrog

 
Messages:238
Registered:March 2004
Location: Austria
this could cause problems on slower machines as it does not take into account the current time left to achieve a framerate.

perhaps its better to measure time at the beginning of the function and the end of the function and then subtract from 16 and the remainder would be how much you should sleep that frame, if 0 to negative, then no sleep because you took too long already. (that for 60fps)

for 30fps subtract from 33

so a something like
clock_t startTime = clock();

... Game Code for Loops Etc ...

clock_t endTime = clock();

long sleeptime = 33 - (endTime-startTime)/(CLK_TCK/1000);
if( sleeptime > 0 )
sleep(sleeptime);
Re: "Cool" patch...[message #102408] Wed, 07 June 2006 07:07 Go to previous messageGo to next message
mtx

 
Messages:10
Registered:December 2005
Location: Lisboa, Portugal
Thanks for the suggestions.

Augustus:

I can try "V1.13 mod development". But I'm pleased to see that there are still interested readers here... And this patch works with the original sources (with some offset), not just 1.13.

I would really like to see it added under some form to 1.13, however.

defrog:

Your approach is undoubtedly more robust, but I don't know if it's worth the effort. I have a Pentium 200MMX and will try to install 1.13 to test it (it does play JA2, UB, UC well...)

However, I'm pretty confident that sleeping 10ms between screen updates - only done at the end of current computations - will not impact that much the playability, even in such an ancient CPU.

In my Asus, I can downscale CPU speed to 600MHz... And see no difference in the game.

But to feel more comfortable, let's replace the 10ms by 1. I never experimented with sleep(0), but will try; just yielding the current thread could just do the trick! Assuming windows would do it,
of course...

The whole point is to avoid having thousands of cycles per second doing mostly nothing in a busy loop...

The are better and cleverer ways of turning JA2 into a better windows citizen, no doubt. For example, turn gameloop() into an event-driven loop. This one has, however, simplicity on its side Wink

I would, of course, appreciate to have any feedback from testing in other setups.

PS: it was tested under Linux+Wine, and works perfectly as well.
Re: "Cool" patch...[message #130588] Fri, 23 February 2007 16:44 Go to previous messageGo to next message
mtx

 
Messages:10
Registered:December 2005
Location: Lisboa, Portugal
defrog:

I see your patch is merged in r725. Nice! Can't wait to
test it.

Meanwhile, I made some experiments with that windows Sleep()
call... It's trickier than I imagined, after reading some
docs. Examples (as I understood them):

- Sleep(0) isn't a noop! It essentialy does a thread yield.
Not much difference if you are not running other programs,
it seems.

- Sleep(1) is special! It allows window repainting and
window message processing. In my tests, using this in
the loop, although CPU usage remains high, it seems to
do away with most "motion chopping" and sound stutters
with slow CPUs (or using wine+linux...)

Your patch looks very good (and clever, I should add).
I'm eager to test this new option, especially under Wine/Linux (the most stringent test).

The obvious problem w/mine was that sometimes there are
calculations being done, even if the screen isn't changing
(e.g. enemy moves); in that case, it was just slowing
things down.

Sleep() should really only be invoked when nothing has
to done right now (e.g., user Paused the game), using
some global "workPending" flag or sth. similar... But
your ideia of using clock() is a very good approach to
that, and doesn't imply adding flag checking/toggling
everywhere. Yeah!

Re: "Cool" patch...[message #130589] Fri, 23 February 2007 16:55 Go to previous messageGo to next message
lalienxx

 
Messages:84
Registered:February 2006
After enabling the patch some things are slower (web site opening in laptop for example), but the CPU load isn't at 100% anymore. In any way it's optional now.


Re: "Cool" patch...[message #130606] Fri, 23 February 2007 22:07 Go to previous messageGo to next message
Sergeant_Kolja

 
Messages:42
Registered:July 2003
Location: Hannover, N-Germany

defrog
clock_t startTime = clock();

IMHO, GetTickCount() is a bit more efficient.
But Your idea to only free each remaining part of a 33fps slice is optimal!

MTX
Sleep(0) isn't a noop! It essentialy does a thread yield.
Not much difference if you are not running other programs,
it seems.

Yes. Sleep(0) only 'stops' the current task of the current process (JA2.EXE), while all other tasks of the current process (JA2.EXE) inherit the freed remainder of the assigned time slice. Thus, all other processes (not JA2.EXE) are absolutely unchanged, if one of the remaining JA2 threads is also a time-eating one. Only, and only if, all the remaining JA2 threads are in so called waitable states (i.e. WaitForSingleObject() or more Sleep()), the OS gets the remainder of the time slice back and non JA2-processes or idle process can get this time.

MTX
Sleep(1) is special! It allows window repainting and window message processing. In my tests, using this in the loop, although CPU usage remains high, it seems to do away with most "motion chopping" and sound stutters with slow CPUs (or using wine+linux...)

Not special. It must depend on CPU power. Sleep(x) with x>0 says to the OS: please dont call this thread again before x ms are elapsed. IIRC, all other threads of the current process are running normal. Not inheriting the remainder of the slice.

I already experimented witch Sleep(1) right after the call of GameLoop(); It made my laptop slightly save energy, while Sleep(10) made the sound chopping (have very cheap AC'97 onboard sound in a dell inspiron 1300).

The defrog idea could be the best compromise.

But I'll try sometimes to further optimize it. The amount of Sleep'd time should also depend on BogoMips or CPU clock, because some people still using JA2 on 200 MHZ P5MMX and/or have CPU consuming sound drivers.


Re: "Cool" patch...[message #130952] Mon, 26 February 2007 21:39 Go to previous messageGo to next message
Sergeant_Kolja

 
Messages:42
Registered:July 2003
Location: Hannover, N-Germany

Is anybody still working on the gameloop sleep(1) issue?
I ask because I have an autocalibrating 30fps guaranteed patch in development, which looks fine here (CPU usage worst case 70%).
Would be hard to mix 2 patches together, so please contact me.


Re: "Cool" patch...[message #138756] Mon, 14 May 2007 14:50 Go to previous messageGo to next message
BasiC_101

 
Messages:27
Registered:September 2006
Location: South Africa

Great Idea, lucky I never had this problem
Re: "Cool" patch...[message #138866] Tue, 15 May 2007 21:28 Go to previous message
mtx

 
Messages:10
Registered:December 2005
Location: Lisboa, Portugal
Sergeant_Kolja
Is anybody still working on the gameloop sleep(1) issue?
I ask because I have an autocalibrating 30fps guaranteed patch in development, which looks fine here (CPU usage worst case 70%).

Not sure what you mean here; defrog's patch is in mailine 1.13 for quite some time, and handles this quite well, as far as I could test it.

Sergeant_Kolja

Would be hard to mix 2 patches together, so please contact me.

If you want to save a little more CPU cycles at the expense of a (unnoticeable?) delay in, e.g. "interface swooshes", you CAN mix the two...
That is, add a test after the calculations to always Sleep() at least 1ms (well, not exactly, check MSDN docs for details).

Personally, after defrog's work, I won't give it more thought. If you want to see a really "cool" (ahem, temperature wise Wink ) JA2, check ja2-stracciatella - a port of vanilla JA2 to SDL. W/that, you can play JA2 in your laptop without worrying about batteries! Smile

Previous Topic: More Screen Resolutions needed?
Next Topic: How is the game data stored?
Goto Forum:
  


Current Time: Fri Jun 25 00:44:41 EEST 2021

Total time taken to generate the page: 0.02660 seconds