PDA

View Full Version : CPU usage and general



Wizard
23-02-2007, 09:02 AM
Hi all, I'm busy with a game made with Delphi and DelphiX. It works fine and the frames per second is around 250. Problem is (maybe it's not a problem at all) that when the app runs my PC'S CPU fan goes on and I can see from the windows task manager that it uses around 90 to 98 % CPU. Is this normal? My code is lengthy so I won't put everything here.

A portion of my code:

procedure TForm1.Man1Create;
begin
Man1 := TMan1.Create(DXSpriteEngine1.Engine);
Man1.Image := DXImageList1.Items.Find('Man1');
Man1.X := 30;
Man1.Y := -500;
Man1.Speed := 1;
Man1.Width := Man1.Image.Width;
Man1.Height := Man1.Image.Height;
Man1.PixelCheck := False;
end;

procedure TMan1.DoMove(MoveCount: Integer);
begin
if road.Moved = true then
y := y+1;
if y > Form1.DXDraw1.Height - 100 then
dead;
if (Movement = 0) Then
Movement := Random(2);
if X > 560 Then Movement := 1;
if X < 50 Then Movement := 2;

Case Movement of
1:
begin
If Image = Form1.DXImageList1.Items.Find('Man1') Then
X := X - Speed
end;
2:
begin
if Image = Form1.DXImageList1.Items.Find('Man1') Then

X := X + Speed
end;
end;
end;

I have 19 men with each one more or less the same code as above, is it possible to do it in one procedure? The x and y parameters and the image is different for all 19.


Thanks for your time :-)

AthenaOfDelphi
23-02-2007, 10:40 AM
Hi Wizard,

When you say 'more or less' the same as the code you provide.... whats different for each of the 19?

Do you understand object orientated programming?

The reason I ask is because there is a nice way and a nasty way to do this.

The nasty way is as you've done.... the nice way is object orientation.

If we assume that EVERY one of the 'men' do exactly the same thing, then you can declare TMan to handle everything thats done in TForm1.Manxcreate.


constructor TMan.create(xPos,yPos:integer;imgName:string;sEngi ne:TDXSpriteEngine;sImageList:TDXImageList);
begin
inherited create(sEngine);

self.x:=xPos;
self.y:=yPos;
self.image:=sImageList.items.find(imgName);

fImgList:=sImageList;

self.speed:=1;
self.width:=self.image.width;
self.height:=self.image.height;
self.pixelcheck:=false;
end;


Once you do this... you can create them like this....


myMen.add(TMan.create(30,-500,'Man1',DXSpriteEngine1,DXImageList1));
myMen.add(TMan.create(200,200,'Man2',DXSpriteEngin e1,DXImageList1));


myMen is a TList. Assuming that the move code is identical for them all, then you only need to declare it once. I'm guessing from what you've said, you have TMan1.doMove; TMan2.doMove; etc.

The thing to do there is to identify common elements.... so of TMan1, TMan2, TMan3 all do the same thing, then move that to a common object TMySimpleMan for example. Then when you create them, create 3 instances of TMySimpleMan. The thing to do, is to identify elements that are common to ALL of them and declare those in a base class TBaseMan. Then extend that to include the bits that aren't common to all.

So you could end up with TBaseMan (this actually does nothing on its own, but provides core functionality that is common to all TMan sprite instances). Then say TManLeftRight which is descended from TBaseMan, but that has the doMove overriden to move left and right across the screen. Its a little tricky to know whether what I'm saying is applicable, without seeing the code.

I'm making the assumption that this isn't for the competition. If it isn't, I'd be happy to have a look at the code and send you back some more detailed suggestions... if you want to do that, email me (athena at outer hyphen reaches dot com) a ZIP file of the code so I can build the EXE here and I'll have a look at it.

With regards to 100% cpu usage.... based on my own prgrams using unDelphiX, I think its pretty normal.

Wizard
23-02-2007, 10:56 AM
Thanks again for your time AthenaOfDelphi, I appreciate it. Well I won't say I understand OOP but what I will say is that I'm learning and getting to know OOP. You have 16 years of programming experience and I have 2 years of trying to figure things out :-)

I will send you a zip of my project :-)

Thanks!

seiferalmasy
23-02-2007, 12:11 PM
I have 100% cpu depending on fps, which is normal. I have put limits on fps, from 60-200. I don't use realtime for vsync either.

I give the user the choice as to what fps he wants. Most games I know of, however always try to take as much cpu as they can to ensure max framerate

With 60 fps, cpu usage is as little as 10%, and with 200 (interval of 5), it goes to around 40-60% [pentium 4 2.8 GHz]

I also use threaded timer but I aint the expert around here by a long shot :oops: but in my own opinion, super high framerates/realtime are not necessary most of the time. For example, even with 200 and vsync (the monitor will obviously refresh at its max framerate, to me that is 85 Hz) I get a totally smooth game.

Experiment with the FPS and make it suitable to YOUR game:)

Wizard
23-02-2007, 12:20 PM
Thanks for your reply seiferalmasy. I'm not going to worry about the cpu usage anymore...it seems normal :-)

My program works fine as I've coded it but hopefully AthenaOfDelphi can show me a few few things I didn't know before :oops:

AthenaOfDelphi
23-02-2007, 04:23 PM
Hi Wizard,

I sent you a mail containing my roughed up version of your game. Its not complete, and in some respects its different to how you've got yours implemented (mainly there are no drums and the men move more randomly, plus they start in random positions, but I've covered this in the mail).

The key thing is that your code extends beyond 1700 lines (not including the restart form), by using objects in a more OOP way, I've got that down to around 650 lines (including the restart form which is now rendered by the main form although as I've said, its roughed up... no drums, no sound and no messages... but the rest is roughly the same).

Anyhow, as I've said in the mail.... if you have any questions and such like about my sample code, drop me a mail.