FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Programs
(Moderators: fry, REAS)
   transparent gifs and screen wraparound
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: transparent gifs and screen wraparound  (Read 784 times)
pollux

WWW Email
transparent gifs and screen wraparound
« on: Apr 25th, 2003, 5:58am »

i've been using the Sprite class Glen wrote (the one running around some [Learning] examples) but now i have a problem with the screen wraparound result. i need transparent gifs but i need their pixels to dissapear if outside the screen.
 
any help? doesn't seems like too difficult a task. i'm urged, i need it urgently, so please get me out of my misery...
 
i'm including the class code (for at-a-glance feedback?):
 
// Sprite class for transparent GIFs  
class Sprite  
{  
  int wh;  // Size of the sprite (width*height)  
  int[] px;     // Array for storing the sprite  
  int swidth;   // Width of the sprite  
  int sheight;  // Height of the sprite  
  BImage img;   // The image that is the sprite  
   
  Sprite(String imageName) {  
    loadSprite(imageName);  
    swidth = img.width;  
    sheight = img.height;  
    wh = swidth*sheight;  
    px = new int[wh];  
     
    // Load the image into the px[] array  
    for(int i=0; i < wh; i++) {  
 px[i] = img.pixels[i];  
    }  
  }  
   
  void loadSprite(String name) {  
    img = loadImage(name);  
  }  
   
  void display(float x, float y) {  
    int pix;  
    for(int i=0; i < wh; i++) {  
 // If the pixel of the image is not transparent,  
 // draw the pixel into the pixels[] array of the window  
 if(px[i] != 0x00FFFFFF) {  
   pix = (int)x+((int)y*width)+(i%swidth)+((i/swidth)*width);  
   if(pix < width*height && pix > 0) {  
     pixels[pix] = px[i];  
   }  
 }  
    }  
  }  
}  
 
(btw, how do you post quoted code?)
 

pollux | www.frwrd.net
toxi

WWW
Re: transparent gifs and screen wraparound
« Reply #1 on: Apr 27th, 2003, 10:25pm »

hey pollux,
 
hope it's not too late, but simply replace the existing display() method with the one below...
 
Code:

void display(int x, int y) {
  if (-x>=swidth || -y>=sheight) return;
  int tw=(x<0) ? x+swidth : min(width-x,swidth);
  int th=(y<0) ? y+sheight : min(height-y, sheight);
  int offset1=((x<0) ? -x : 0)+((y<0) ? -y : 0)*swidth;
  int offset2=((x<0) ? 0 : x)+((y<0) ? 0 : y)*width;
  int yy=-1;
  while(++yy<th) {
    int xx=-1;
    while(++xx<tw) {
      if(px[offset1] != 0x00FFFFFF) pixels[offset2] = px[offset1];
      offset1++;
      offset2++;
    }
    offset1+=(swidth-tw);
    offset2+=(width-tw);
  }
}

 
also note: the method now expects integer coordinates to avoid further type conversions within the loops.
 
btw. use [ code ] tags (w/o spaces between brackets) to post code samples...
« Last Edit: Apr 27th, 2003, 10:27pm by toxi »  

http://toxi.co.uk/
pollux

WWW Email
Re: transparent gifs and screen wraparound
« Reply #2 on: Apr 28th, 2003, 9:38am »

thanxs a lot, toxi. i am *still* on schedule, and it works like a charm!
 
(btw, i like what you do with director...)
 

pollux | www.frwrd.net
Pages: 1 

« Previous topic | Next topic »