We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › why doesn't screen.width and screen.height work
Page Index Toggle Pages: 1
why doesn't screen.width and screen.height work (Read 755 times)
why doesn't screen.width and screen.height work
Mar 18th, 2007, 9:52am
 
hi,  
I've been trying to learn processing and like most people I'm waiting for Shiffman's book to come out but until then I'm trying to work out as much as possible on my own. I found this code on the net that someone posted. Its a water simulation. I tried to get it to go fullscreen and add a background image to get a handle on the syntax. The code is fairly simple to understand but why my additions will not work is not.I thought I inserted screen.width and screen.height into the maps and size command. can someone help me fix this code to go full screen and display a background image?

color col;
int i,j,g;
float osc=0;
int x=0;
int y=0;
float c=0;

float newmap[]=new float[width*height];
float currentmap[]=new float[width*height];
float prevmap[]=new float[width*height];
//my addition-changed 500*500 on the maps to width*height  




void setup(){
 size(screen.width, screen.height,P3D);
 loadPixels();
 //colorMode(RGB,2.0,1.0,1.0);
//commented out colormode for PImage
 noCursor();
PImage w;  
w = loadImage("Worn wall.jpg");  
 image(w, 0, 0);  
//My addition- changed 500*500 to screen.width and  
screen.height
//added PImage code
//get a null pointer exception on image(w, 0, 0) why?
 
 
 
 for(g=0;g<width*height;g++){
   currentmap[g]=0;
   newmap[g]=0;
 }
}

float lu(int x, int y, float array[]){ // look up a table
 return(array[(y*width)+x]);
}


void draw(){
 x=mouseX;
 y=mouseY;
 osc+=.2;
 splash(x,y,.6);
 
if(mousePressed);
   line(mouseX, mouseY, pmouseX, pmouseY);
 for(i=155;i>100;i--){
for(j=100;j<155;j++){
 //     currentmap[(width*j)+i]=.7;//
}
 }
//splash(160,100,100.,1)
 for(j=2;j<height-1;j++){
 for(i=1;i<width-1;i++){

   newmap[i+(j*width)]=.99*
   (((
lu(i+1, j,   currentmap)+
lu(i-1, j,   currentmap)+
lu(i,   j+1, currentmap)+
lu(i,   j-1, currentmap)+
lu(i+1, j+1, currentmap)+
lu(i+1, j-1, currentmap)+
lu(i-1, j+1, currentmap)+
lu(i-1, j-1, currentmap))/4.)-lu(i,j,prevmap));
  // newmap[i+(j*width)]=    newmap[i+(j*width)]-    (newmap[i+(j*width)]*16);
   }
 }

for(g=400;g<(width*height)-2222;g++) {

 //   c=(newmap[g+width+1]-newmap[g-width-1]);
 //  pixels[g]=color(0,0,atan2(.15,c));

   pixels[g]=color((.1)*(1.-abs((newmap[g]))),(.7)*(.6-abs(newmap[g])), 1.-abs(newmap[g]));
 }

arraycopy(currentmap,prevmap);

arraycopy(newmap,currentmap);
// currentmap=newmap;


 updatePixels();
}




void splash(int x, int y, float power){
int sqsize=3; //intensity
float a,b;

if(x>=sqsize && y>=sqsize && x<width-sqsize && y<height-sqsize){
  for(int xx=-sqsize;xx<sqsize;xx++){
 for(int yy=-sqsize;yy<sqsize;yy++){
     a=xx;
     b=sqsize;
currentmap[((y+yy)*width)+x+xx]-=power;//(power-((a/b)*power));

 }
  }
}
}




Re: why doesn't screen.width and screen.height wor
Reply #1 - Mar 18th, 2007, 12:26pm
 
first, please don't double post!
it might seem like a good idea to get your answer faster, but actually it's doing the opposite: people trying to help not only have to browse through more posts, in addition ben or casey have to delete the duplicates by hand ...

i'm not sure what the problem with the image is .. i guess either it's in the wrong place ( needs to be inside a folder called "data" inside your sketchs folder) or corrupted.

the problem with width and heigth is that you are using them before they have been set. when you call size( .. ) these are changed to the new size, therefore the earlier variable initializations have to be wrong. a way to prevent this is to use setup() for initialization of variables (which in general is the safest practice for global variables):

Code:

// declare here ...
float newmap[];  
float currentmap[];  
float prevmap[];

void setup(){  
 size(screen.width, screen.height,P3D);
 // init here ...
 newmap=new float[width*height];  
 currentmap=new float[width*height];  
 prevmap=new float[width*height];

......


screen.width is not width ! totally different things. width is the width of the sketchs frame (window), screen.width is what it says ... so i don't really get why you called your post that way, what "does not work" with those variables?

you might want to look into variable scope:
http://processing.org/learning/examples/variablescope.html

F
Re: why doesn't screen.width and screen.height wor
Reply #2 - Mar 19th, 2007, 7:37pm
 
FYI The code you have there is some of my really old code for a  water effect.  I have a newer version up that is much cleaner and probably 5-10 times faster.  

http://viz.nu/programming/processing/splish
Re: why doesn't screen.width and screen.height wor
Reply #3 - Mar 20th, 2007, 3:13am
 
sorry about the double posts. Did not know and thanks for the tips. I think that cleared up the problem.
Re: why doesn't screen.width and screen.height wor
Reply #4 - Mar 20th, 2007, 5:12am
 
wow movax, great code! I checked out your new simulation and it is really awesome! Thanks for posting that and the code with it! I thought your new code was much easier to read with the notes and very helpful. It certainly clarifies alot more of processing's syntax for a beginner like me. I wish everyone put such well organized commentary into their code. Until Daniel shiffmans book comes out I think alot of beginners like myself are relying on other processing artists code to learn the basics of structure, making their comments critical to the novices learning experience. I know every artist codes differently but it helps to see how things were organized and simplified if you are a beginner without a background in code. anyway, thanks again.
Page Index Toggle Pages: 1