Page 1 of 1

Simple but useful: on screen

PostPosted: Tue Mar 08, 2011 12:26 pm
by lcl
This is simple but useful function for checking if the actor is on the screen or not.
Return 1 if on screen, else return 0.

Global code:
Code: Select all
int inView()
{
    int temp = (xscreen > - width * 0.5 && xscreen < view.width + width * 0.5 && yscreen > - height * 0.5 && yscreen < view.height + height * 0.5);
    return temp;
}

Usage:
Code: Select all
if (inView())
{
    //Action here
}

:)

Re: Simple but useful: on screen

PostPosted: Tue Mar 08, 2011 12:44 pm
by Game A Gogo
with your code, the actor needs to be halfly visible before it returns true, here is a quick work around:
Code: Select all
int inView()
{
    int temp = (xscreen > width*.5 && xscreen < view.width-width*.5 && yscreen > height*.5 && yscreen < view.height-height*.5);
    return temp;
}


Handy code otherwise :)

Re: Simple but useful: on screen

PostPosted: Tue Mar 08, 2011 1:05 pm
by lcl
Yeah, I knew that...
I just didn't make it work right for some reason... xD
-> *Going to update the code* :)

Btw, is there some reason for using multiplication instead of division?
Thanks for reply, GAG.

EDIT: With your code that actor has to be fully visible... xD
Understandable mistake. :)
Some "-"'s had to change to "+"s and it works!

Re: Simple but useful: on screen

PostPosted: Tue Mar 08, 2011 1:12 pm
by Game A Gogo
Beleive it or not, but your CPU will take less time multiplying, even with decimals than doing divisions, even perfect ones like 4/2

Re: Simple but useful: on screen

PostPosted: Tue Mar 08, 2011 1:17 pm
by lcl
Game A Gogo wrote:Beleive it or not, but your CPU will take less time multiplying, even with decimals than doing divisions, even perfect ones like 4/2

I believe, actually, it makes sense! :)

Re: Simple but useful: on screen

PostPosted: Tue Mar 08, 2011 5:01 pm
by AnarchCassius
Huh, I knew division was bad but I thought decimal multiplication was equivalent. Good job both of you.

Re: Simple but useful: on screen

PostPosted: Tue Mar 08, 2011 5:27 pm
by sonicfire
Yup always try to use * instead of / !

Re: Simple but useful: on screen

PostPosted: Tue Mar 08, 2011 6:51 pm
by Game A Gogo
Code: Select all
int inView(char* actorn)
{
    int temp;
    Actor*TActor=NULL;
    if(strcmp("Event Actor",actorn)==0)temp=(xscreen > width*.5 && xscreen < view.width-width*.5 && yscreen > height*.5 && yscreen < view.height-height*.5);
    else if(strcmp("Collide Actor",actorn)==0)temp=(collide.xscreen > collide.width*.5 && collide.xscreen < view.width-collide.width*.5 && collide.yscreen > collide.height*.5 && collide.yscreen < view.height-collide.height*.5);
    else if(strcmp("Creator Actor",actorn)==0)temp=(creator.xscreen > creator.width*.5 && creator.xscreen < view.width-creator.width*.5 && creator.yscreen > creator.height*.5 && creator.yscreen < view.height-creator.height*.5);
    else {TActor=getclone(actorn);temp=(TActor->xscreen > TActor->width*.5 && TActor->xscreen < view.width-TActor->width*.5 && TActor->yscreen > TActor->height*.5 && TActor->yscreen < view.height-TActor->height*.5);}
    return temp;
}


This code will enable you to pick up the state of other actors, as well as "Event Actor","Collide Actor" and "Creator Actor" for convenience. I will be using this code :)

Re: Simple but useful: on screen

PostPosted: Tue Mar 08, 2011 9:32 pm
by Hblade
Niiice ;)

Re: Simple but useful: on screen

PostPosted: Tue Mar 08, 2011 9:59 pm
by lcl
Nice addition GAG. :)
Simple, but useful, just as it should be. :D