Simple but useful: on screen

Game Editor comments and discussion.

Simple but useful: on screen

Postby lcl » Tue Mar 08, 2011 12:26 pm

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
}

:)
Last edited by lcl on Tue Mar 08, 2011 1:16 pm, edited 4 times in total.
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Simple but useful: on screen

Postby Game A Gogo » Tue Mar 08, 2011 12:44 pm

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 :)
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: Simple but useful: on screen

Postby lcl » Tue Mar 08, 2011 1:05 pm

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!
Last edited by lcl on Tue Mar 08, 2011 1:15 pm, edited 1 time in total.
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Simple but useful: on screen

Postby Game A Gogo » Tue Mar 08, 2011 1:12 pm

Beleive it or not, but your CPU will take less time multiplying, even with decimals than doing divisions, even perfect ones like 4/2
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: Simple but useful: on screen

Postby lcl » Tue Mar 08, 2011 1:17 pm

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! :)
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Simple but useful: on screen

Postby AnarchCassius » Tue Mar 08, 2011 5:01 pm

Huh, I knew division was bad but I thought decimal multiplication was equivalent. Good job both of you.
AnarchCassius
 
Posts: 55
Joined: Sat Jan 29, 2011 1:33 am
Score: 6 Give a positive score

Re: Simple but useful: on screen

Postby sonicfire » Tue Mar 08, 2011 5:27 pm

Yup always try to use * instead of / !
sonicfire
 
Posts: 425
Joined: Wed Nov 01, 2006 9:34 pm
Location: berlin germany
Score: 16 Give a positive score

Re: Simple but useful: on screen

Postby Game A Gogo » Tue Mar 08, 2011 6:51 pm

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 :)
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: Simple but useful: on screen

Postby Hblade » Tue Mar 08, 2011 9:32 pm

Niiice ;)
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: Simple but useful: on screen

Postby lcl » Tue Mar 08, 2011 9:59 pm

Nice addition GAG. :)
Simple, but useful, just as it should be. :D
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score


Return to GE - General

Who is online

Users browsing this forum: No registered users and 1 guest

cron