Skip to main content
gamedev.net
🔒 Locked

CODE OPTIMISING

Started by KingMaximus Jan 2, 2001 at 10:36 AM 18 replies 850+ views
Original Post
KingMaximus
KingMaximus
Im writin a isometric game (a rts) in Delphi. On my development machine i''m getting about 200fps - this is just drawing the tiles on the screen not units etc.. yet, however my friends get around 100fps because their machines specs. are lower than my own machine. I''m therefore looking to optimise my code, and wondered if some of you pros could comment on the downsides to the following code which works out where to place the isometric tiles. This code is run every frame, and comments would be greatly appreicatated as im a newbie in all this isometric stuff for loopy := 0 to maxmapheight do begin; for loopx := 0 to maxmapwidth do begin; x:=(loopx * isowidth) + ((loopy mod 2) * (isowidth div 2)) - map.posonmap.x; y:=(loopy * (isoheight div 2)) -map.posonmap.y; if (x>140) and (x<680) and (y>-50) and (y<550) then begin; TileList.Items[map.tiles[loopx,loopy].tileimage].Draw(DXDraw.Surface,x,y,0); end; end; end; Thanks again, KingMaximus
Dino
Dino
Here''s some common things you can do (I''ll try and get the syntax right):


y:=-map.posonmap.y;
dY:= isoheight div 2;
for loopy := 0 to maxmapheight do
begin;
x:= -map.posonmap.x;

for loopx := 0 to maxmapwidth do
begin;
x:= x + isowidth;
y:= y + dY;
if (x>140) and (x<680) and (y>-50) and (y<550) then
begin;
TileList.Items[map.tiles[loopx,loopy].tileimage].Draw(DXDraw.Surface,x,y,0);
end;
end;
end;


I don''t know if the syntax is right, but you should get the idea. Take out the constant multiplication in there and switch it with addition only. That should speed it up.

Dino M. Gambone
Good judgement is gained through experience. Experience, however, is gained through bad judgement.
Dino M. Gambone
Good judgment is gained through experience. Experience, however, is gained through bad judgment. Currently working on Rise of Praxis MUD: http://www.riseofpraxis.net/

 
KingMaximus
KingMaximus
cheers for that Dino i owe ya one! i got about an extra 5fps using your method, but I know have a even bigger probbie my units. The game I''m developing is a red alert sort-of clone - just as my first real isometric game - and I added 3 units and the frame rate gets halfed :S The units get drawn every frame just after the code Dino just optimised gets executed... wat i can''t understand is why the units are slowing the whole system down, maybe im over looking something anyway if anyone could help me please do i''ll be externally gratefull!

DrawAllUnits() is called from the main loop that executes all the other functions

procedure DrawAllUnits(DX: TDXDraw; UnitImg: TDXImageList; var Map: TTileMap);
var
i: integer;
begin;
for i:=1 to map.unitcount do
begin;
if (map.units.created) and (map.units[i].visible) and (map.units[i].inplayersviewablearea) then
begin;
drawunit(map.units[i],dx,unitimg,map);
end;
end;
end;

And this draws the unit itself
procedure DrawUnit(UnitObj: tisounit; DX: TDXDraw; UnitImg: TDXImageList; Map: TTileMap);
var
frame: integer;
begin;
if not(unitobj.inplayersviewablearea) then exit;
if (unitobj.anim.frame<0) or (unitobj.anim.frame>32) then
frame:=0
else
frame:=unitobj.anim.frame;
unitimg.items[frame].draw(dx.surface,unitobj.screenpos.x,unitobj.screenpos.y,0);
end;

Thanx again folks!!!

KingMaximus
KingMaximus
well 1st off mr anonymous my pc is:-

athlon 700mhz
gigabyte m/board
256mb ram
voodoo 3 2000 AGP gfx

resolution - 640x480 in 16 bit

draw method - directx component draws it for me

no alpha channels or transparency yet


GalaxyQuest
GalaxyQuest
First of all, you should be ashamed of yourself for going 640x480 resolution...even me, with my p1 233 am doing my project in 800x600

Anyways, you may think about some sorting methods on your units. Then as you draw your map, you also draw your units. I dont know how your units relate to the map, for me, my units "will" be located in specific map squares(tiles) so when i am drawing my screen i then draw all objects also on the screen (which happens to be all the map squares currently viewable).
Im not suggesting my route but just showing you an idea of how you might use it when you draw your screen. There are lots of guys who keep units seperate from the map and they probably can answer your question better. Good luck and i was only playing around about the resolution stuff so dont sweat it.



aka John M.
Never give up. Never surrender!


morfe
morfe
quote:
Original post by KingMaximus
for loopy := 0 to maxmapheight do
begin;
for loopx := 0 to maxmapwidth do
begin;
x:=(loopx * isowidth) + ((loopy mod 2) * (isowidth div 2)) - map.posonmap.x;
y:=(loopy * (isoheight div 2)) -map.posonmap.y;
if (x>140) and (x<680) and (y>-50) and (y<550) then
begin;
TileList.Items[map.tiles[loopx,loopy].tileimage].Draw(DXDraw.Surface,x,y,0);
end;
end;
end;


To start with, change it to this

for loopy := 0 to maxmapheight do
begin
for loopx := 0 to maxmapwidth do
begin
x:=(loopx * isowidth) + ((loopy and 1) * (isowidth shr 1)) - map.posonmap.x;
y:=(loopy * (isoheight shr 1)) -map.posonmap.y;
if (x>140) and (x<680) and (y>-50) and (y<550) then
begin
TileList.Items[map.tiles[loopx,loopy].tileimage].Draw(DXDraw.Surface,x,y,0);
end;
end;
end;

Now, if your tiles measure 16, 32 or 64 you can use shifts instead of multiplies. This is THIRTEEN TIMES FASTER. Delphi usually does it for you, but sometimes misses. Say your isowidth = 64 and your isoheight = 32. You can do this:

for loopy := 0 to maxmapheight do
for loopx := 0 to maxmapwidth do
begin
x:=(loopx shl 6) + ((loopy and 1) shl 4) - map.posonmap.x;
y:= (loopy shl 4) - map.posonmap.y;
if (x>140) and (x<680) and (y>-50) and (y<550) then

TileList.Items[map.tiles[loopx,loopy].tileimage].Draw(DXDraw.Surface,x,y,0);
end;

Last optimisation you can make is to replace for loops with while loops which decrease the number of jumps in your code.

Next one ...

procedure DrawAllUnits(DX: TDXDraw; UnitImg: TDXImageList; var Map: TTileMap);
var
i: integer;
begin
for i:=1 to map.unitcount do
with map.units[ i] do
if (created) and (visible) and (inplayersviewablearea) then
drawunit(map.units[ i],dx,unitimg,map);
end;

procedure DrawUnit(UnitObj: tisounit; DX: TDXDraw; UnitImg: TDXImageList; Map: TTileMap);
var
frame: integer;
begin;
with unitobj do
begin
if (inplayersviewablearea) then
if (anim.frame<0) or (anim.frame>32) then
frame := 0
else
frame := anim.frame;
unitimg.items[frame].draw(dx.surface,screenpos.x,screenpos.y,0);
end;
end;

I'll add a couple more later... Note I put a space in the [ i] to prevent italics being activated.

Edited by - morfe on January 2, 2001 4:55:25 PM
"NPCs will be inherited from the basic Entity class. They will be fully independent, and carry out their own lives oblivious to the world around them ... that is, until you set them on fire ..." -- Merrick
KingMaximus
KingMaximus
Thanks to everyone that has helped me so far, especially Dino and Morfe - between them a extra 15fps have been added to my frame rate.

The fps in now around 80fps on mine - this is with the AI routine i wrote which is really really dodgy (just a collection of ifs at the mo!) and with the drawing of the units.

As for GalaxyQuests question about how I''m locating the units each one has a x and y co-ordinate which I translate to the position on the screen (for example x:=x-screenposx) and then I simply draw the units frame at this co-ordinate. Is this the ebst method or do you think I should locate it in a tile???

I apreciate everyones help with this matter, I''m young (15) but I''m slowly getting to grips with all this Isometric stuff thanks to all you guys (...wipes tears from eyes.... )

Anyway thanks again,

MattyB
(KingMaximus)
KingMaximus
KingMaximus
ALSO - I know this aint really isometric related but does any one know how i''d go about modelling the physics for a missile, I know I''d need at least three forces air resistance, gravity and pulpusion but I don''t know how I''d put it into code - if anyone could quickly knock together a routine in any routine please do so! Thanx again every one!
morfe
morfe
I would give the missiles these properties:

Missile = class // record if you don't want to use lists
{
Target
Position
Speed
};

Use a linked list (or a TList) for the missile list. If you need some example code to write one yourself, I'll post it.

Every time the missile should be moved the speed is added to the position, and then the speed is accelerated towards the target. If the missile misses the target it will take some time before it is able to reverse its speed, which is quite realistic without imposing to much physics into it.




"NPCs will be inherited from the basic Entity class. They will be fully independent, and carry out their own lives oblivious to the world around them ... that is, until you set them on fire ..."
"When you are willing to do that which others are ashamed to do, therein lies an advantage."


Edited by - morfe on January 3, 2001 6:23:22 PM
"NPCs will be inherited from the basic Entity class. They will be fully independent, and carry out their own lives oblivious to the world around them ... that is, until you set them on fire ..." -- Merrick
Fred Stumakas
Fred Stumakas
Hello fellow Isometriclanders.

I have noticed that KingMaximus has been making claims of 200 frames in one second in delphi. I do not believe this wild claim.

From what planet was the idea that this was even possible first launched?

Delphi executes its code in a virtual machine. Everyone knows VCL is just long for VM.

Another problem that I have is that everyone knows if you draw faster than 1000 frames in one second your monitor will be exploded (very messy - trust me).

Thus I have to believe that perhaps KingMaximus should count his frames more carefully. Frankly I do not see how it is possible for a human being to be able to perceive, yet alone count, 200 frames drawn in one second. That idea is simply perposterous.

Truly yours,


---
Fred Stumakas,
Professional Programmar
---Fred Stumakas,Professional Programmar
KingMaximus
KingMaximus
Well I''m so sorry mr I can''t spell programmer.....

200fps whats wrong with that?? I get 145fps on Unreal Tornament in 640x480 mode - then again that must be wrong as well cos ur a Professional Programmar aint ya

Well never mind, maybe you should email the creators of Unreal Tornament and tell them that the code is a pile of wank then cos obvisouly you know best.... maybe its best for the health of people on Earth - we don''t want any monitors exploding now do we especially on the higher end athlons......

Anyway on a brighter note cheers again morfe for your help regarding the missile matter!

Mattyb
morfe
morfe
quote:
Original post by Fred Stumakas
I have noticed that KingMaximus has been making claims of 200 frames in one second in delphi. I do not believe this wild claim.



I do, since I get 333fps (and occasionally higher).




"NPCs will be inherited from the basic Entity class. They will be fully independent, and carry out their own lives oblivious to the world around them ... that is, until you set them on fire ..."
"When you are willing to do that which others are ashamed to do, therein lies an advantage."
"NPCs will be inherited from the basic Entity class. They will be fully independent, and carry out their own lives oblivious to the world around them ... that is, until you set them on fire ..." -- Merrick
KingMaximus
KingMaximus
Ok Morfe regarding this missile matter - the movement is ok as you explained but what about the height aspect?

Wouldn't I have to model gravity as well to get the missile to climb depending on its angle and speed, and then change angle, because of lack of speed, and finally fall back to Earth?

If you could give me some sample code I would be extremly grateful.......

Thanx again,

Mattyb

Edited by - KingMaximus on January 4, 2001 5:40:04 AM
morfe
morfe
You simply weight the effect of gravity based on its speed. When it''s going fast enough, gravity it negligible, but on launch (and if it runs out of fuel) gravity acts more directly on it.

Is that what you mean?



"NPCs will be inherited from the basic Entity class. They will be fully independent, and carry out their own lives oblivious to the world around them ... that is, until you set them on fire ..."
"When you are willing to do that which others are ashamed to do, therein lies an advantage."
"NPCs will be inherited from the basic Entity class. They will be fully independent, and carry out their own lives oblivious to the world around them ... that is, until you set them on fire ..." -- Merrick
KingMaximus
KingMaximus
Yep thats wat I meant, so in terms of code something like the following (in Pascal as usual - )

var
missilespeed, gravity, missileheight: integer;
missile: tpoint;
begin;
dec(gravity,missilespeed);
dec(missile.y,gravity);
inc(missile.x,missilespeed);
end;

or something? or am i completly off track????

thanx gain

matt
morfe
morfe
quote:
Original post by KingMaximus

var
missilespeed, gravity, missileheight: integer;
missile: tpoint;
begin;
dec(gravity,missilespeed);
dec(missile.y,gravity);
inc(missile.x,missilespeed);
end;



Well, I''d say more like

with missile do
..
dec(gravity, missilespeed);
inc(y_velocity, gravity);
inc(y, y_velocity);
inc(x, missilespeed);
..




"NPCs will be inherited from the basic Entity class. They will be fully independent, and carry out their own lives oblivious to the world around them ... that is, until you set them on fire ..."
"When you are willing to do that which others are ashamed to do, therein lies an advantage."
"NPCs will be inherited from the basic Entity class. They will be fully independent, and carry out their own lives oblivious to the world around them ... that is, until you set them on fire ..." -- Merrick
KingMaximus
KingMaximus
Silly Me :S Thanks Morfe!! Are you making any games at the moment? If so what language you using??

Thanks again matey,

Matt

morfe
morfe
Yep, two main ones: one in Delphi and assembler, one mainly in C++. I use Delphi a lot for other things too (apps, editors, etc.)



"NPCs will be inherited from the basic Entity class. They will be fully independent, and carry out their own lives oblivious to the world around them ... that is, until you set them on fire ..."
"When you are willing to do that which others are ashamed to do, therein lies an advantage."
"NPCs will be inherited from the basic Entity class. They will be fully independent, and carry out their own lives oblivious to the world around them ... that is, until you set them on fire ..." -- Merrick

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.