Loading...
Logo
Processing Forum
I'm just drawing a simple sprite on the screen. It seems to be flickering on and off. Not sure what to do about it... I've tried double buffering and not, it happens on both. Even though I've heard double buffering is already provided. I'm also getting a decent framerate... Well here's my code if anyone has any ideas.

Copy code
  1. Sprite box;
  2. PGraphics screen;

  3. void initScreen(){
  4.   size(800, 450);
  5.   frameRate(30);
  6.   rectMode(CENTER);
  7.   screen = createGraphics(800, 450);
  8.   
  9.   smooth();
  10.   noStroke();
  11.   fill(255);
  12. }

  13. void setup() {
  14.   initScreen();
  15.   
  16.   box = new Sprite("spritesheet.png", 300/5, 360/6);
  17.   
  18. };

  19. void draw() {
  20.   screen.beginDraw();
  21.   screen.background(#999999);
  22.   
  23.   
  24.   screen.text(frameRate, width-50, 15);
  25.   
  26.   box._draw();
  27.   screen.endDraw();
  28.   image(screen, 0, 0);
  29. }
  30. class Sprite {
  31.   private PImage img;
  32.   private int x = 0;
  33.   private int y = 0;
  34.   private int w;//sprite width
  35.   private int h;//sprite height
  36.   
  37.   Sprite(String img_path, int wc, int hc){
  38.     img = loadImage(img_path);
  39.     w = wc;
  40.     h = hc;
  41.   }
  42.   
  43.   void _draw(){
  44.     screen.image(img.get(x*w, y*h, w, h), 100, 100);
  45.     
  46.     //move ahead a frame
  47.     x += 1;
  48.     //see if we should move down a row
  49.     if(x*w > img.width){
  50.       x = 0;
  51.       //see if we should go back to the beginning
  52.       if(y*h >= img.height){
  53.         y = 0;
  54.       } else {
  55.         y += 1;
  56.       }
  57.     }
  58.     
  59.   }
  60. }


If anyone can help, thanks a lot!

Replies(3)

Okay I figured that out. Turns out PImage.get is a LOT slower than I expected! So now here's a new Sprite class I made that caches the individual frames. It's much simpler anyways.

Copy code
  1. class Sprite {
  2.   private PImage img;
  3.   private int frame = 0;
  4.   private int length = 0;
  5.   PImage[] cache;
  6.   
  7.   Sprite(String img_path, int w, int h){
  8.     img = loadImage(img_path);
  9.     int xcount = img.width/w;
  10.     length = xcount*(img.height/h);
  11.     
  12.     cache = new PImage[length];
  13.     for(int i = 0; i < length; i++){
  14.       int xc = i%xcount;
  15.       int yc = floor(i/xcount); 
  16.       cache[i] = img.get(xc*w, yc*h, w, h);
  17.     }
  18.   }
  19.   
  20.   void _draw(){
  21.     screen.image(cache[frame], 100, 100);
  22.     //move ahead a frame
  23.     frame += 1;
  24.     //see if we should restart
  25.     if(frame >= length)
  26.       frame = 0;
  27.     
  28.   }
  29. }
If you are planning on developing software that uses sprites can I suggest you look at the Sprite for Processing library

 
If you are planning on developing software that uses sprites can I suggest you look at the  Sprite for Processing  library
Thanks I checked it out, but I don't think it matches my needs. I like to separate between my resources and actors. Helps when you have to manage hundreds of each. Nice work though!