Should I check if sprite is in screen?

Just starting out? Need help? Post your questions and find answers here.
Lebostein
Addict
Addict
Posts: 807
Joined: Fri Jun 11, 2004 7:07 am

Should I check if sprite is in screen?

Post by Lebostein »

Hi,

for a game I have a huge landscape with many sprites. The player sees only a small part of this world.
My question to the developers is: Have the functions DisplaySprite() and DisplayTransparentSprite() already built-in a test, whether the sprite is visible on screen?

Or do I have to check that (to speed-up the program for example)?

Code: Select all

ForEach allsprites()
  ...
  If x < ScreenWidth() And y < ScreenHeight() And x > -SpriteWidth(sprite) And y > -SpriteHeight(sprite)
  
    DisplaySprite(sprite, x, y)
  
  EndIf
  ...
Next
Fred
Administrator
Administrator
Posts: 16690
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Should I check if sprite is in screen?

Post by Fred »

It's already done for you.
Lebostein
Addict
Addict
Posts: 807
Joined: Fri Jun 11, 2004 7:07 am

Re: Should I check if sprite is in screen?

Post by Lebostein »

One less worry. Thank you! :D

By the way: Some 2DDrawing()-Tools have a problem with that. For example Plot(). PB crashes when the point is outside of the screen or outside of an image...
User avatar
STARGÅTE
Addict
Addict
Posts: 2090
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Should I check if sprite is in screen?

Post by STARGÅTE »

Plot have no check, it write directly to the memory, but it is very fast!
Last edited by STARGÅTE on Mon Aug 19, 2013 11:32 am, edited 1 time in total.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
eesau
Enthusiast
Enthusiast
Posts: 589
Joined: Fri Apr 27, 2007 12:38 pm
Location: Finland

Re: Should I check if sprite is in screen?

Post by eesau »

Fred wrote:It's already done for you.
I think adding this information to the help file would be good.
Lebostein
Addict
Addict
Posts: 807
Joined: Fri Jun 11, 2004 7:07 am

Re: Should I check if sprite is in screen?

Post by Lebostein »

STARGÅTE wrote:Plot have no check, it write directly to the memory, but it is very fast!
Such information must be written in the manual!

PS: I think a line like "if x < width" will not slow down the program... or I am wrong?
Fred
Administrator
Administrator
Posts: 16690
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Should I check if sprite is in screen?

Post by Fred »

Lebostein wrote:
STARGÅTE wrote:Plot have no check, it write directly to the memory, but it is very fast!
Such information must be written in the manual!

PS: I think a line like "if x < width" will not slow down the program... or I am wrong?
http://www.purebasic.com/documentation/ ... /plot.html
For performance reasons there are no bounds checks performed on these coordinates, the specified coordinates must be inside the current drawing area. OutputWidth() and OutputHeight() can be used to verify that.
User avatar
STARGÅTE
Addict
Addict
Posts: 2090
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Should I check if sprite is in screen?

Post by STARGÅTE »

a "If X >= 0 and X<Width and Y>=0 and Y<Height" in each Plot()-call
is slow instead of only one ASM-Call like:
MOV buffer, color
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Post Reply