Why is my code so heavy?

I am new to processing and new to java and i wrote this code which from what i can see should work properly and be the lightest program ever, however, it does not work properly and it uses 17% of my processor(amd fx-8320) and 400mb of ram. My question is how can i possibly get this working and make it so it would not max out every medium spec quad-core it ran on, its just a button and a walking animation it should be able to run on a computer from the 90's. This code is supposed to load a start image and 2 buttons, with a fully resizable stretch to fit screen(which works) then when you press the "New" button with the mouse, it should have a sprite that goes through a walking animation while you hold w a s or d and walk in the according direction and go through the steps of the direction while moving across the screen. The sprite only displays for a flicker after you click the button(the button is also very picky in that you have to click the button many times in different areas for it to eventually work) and the reverts to a black screen. my code is below, any help would be appreciated thank you. and i am aware that this code is probably ridiculous, that is why i am here.

import processing.opengl.*;
//VARIABLES
float state = 1;
boolean wPressed=false;
boolean sPressed=false;
boolean aPressed=false;
boolean dPressed=false;
int dir = 1;
int step = 1;
int locx = 500;
int locy = 300;
PImage char1;
PImage char12;
PImage char13;
PImage char2;
PImage char22;
PImage char23;
PImage char3;
PImage char32;
PImage char33;
PImage char4;
PImage char42;
PImage char43;


//Button Shit
//End Button Shit
//Code
void setup() {
  size(1024, 768);
  frame.setResizable(true);
  imageMode(CORNER);
  noStroke();
  smooth();
}

void menu() {
  PImage start;
  PImage buttonnew;
  PImage buttoncontinue;
  float x1 = width/5;
  float y1 = height/3;
  float x2 = width/5*3.5;
  float y2 = height/3;
  int w = 120;
  int h = 50;
  start = loadImage("data/startpage.png");
  buttonnew = loadImage("data/buttonnew.png");
  buttoncontinue = loadImage("data/buttoncontinue.png");
  image(start, 0, 0, width, height);
  image(buttonnew, x1, y1);
  image(buttoncontinue, x2, y2);
  if (mousePressed && (mouseButton == LEFT)) {
    if (mouseX>x1 && mouseX<x1+w && mouseY>y1 && mouseY<y1+h) {
      state = 2;
    }
    if (mouseX>x2 && mouseX<x2+w && mouseY>y2 && mouseY<y2+h) {
      state = 2.1;
    }
  }
}

//CHARACTER STUFF
void sprite() {
  char1 = loadImage("data/char1.png");
  char12 = loadImage("data/char1-2.png");
  char13 = loadImage("data/char1-3.png");
  char2 = loadImage("data/char2.png");
  char22 = loadImage("data/char2-2.png");
  char23 = loadImage("data/char2-3.png");
  char3 = loadImage("data/char3.png");
  char32 = loadImage("data/char3-2.png");
  char33 = loadImage("data/char3-3.png");
  char4 = loadImage("data/char4.png");
  char42 = loadImage("data/char4-2.png");
  char43 = loadImage("data/char4-3.png");
  keyPressed();
  if (wPressed == true) {
    dir = 4;
    locy ++;
  }
  if (sPressed == true) {
    dir = 1;
    locy --;
  }
  if (aPressed == true) {
    dir = 2;
    locx --;
  }
  if (dPressed == true) {
    dir = 3;
    locx ++;
  }
  keyReleased();
  charimage();
}

void keyPressed() {
  if (key=='w') {
    wPressed=true;
  }
  if (key=='s') {
    sPressed=true;
  }
  if (key=='a') {
    aPressed=true;
  }
  if (key=='d') {
    dPressed=true;
  }
}


void keyReleased() {
  if (key=='w') {
    wPressed=false;
  }
  if (key=='s') {
    sPressed=false;
  }
  if (key=='a') {
    aPressed=false;
  }
  if (key=='d') {
    dPressed=false;
  }
}


void charimage() {
  if (dir == 1) {
    if (step == 1) {
      image(char1, locx, locy);
      step ++;
    }
    if (step == 2) {
      image(char12, locx, locy);
      step ++;
    }
    if (step == 3) {
      image(char13, locx, locy);
      step ++;
    }
  }  
  if (dir == 2) {    
    if (step == 1) {
      image(char2, locx, locy);
      step ++;
    }
    if (step == 2) {
      image(char22, locx, locy);
      step ++;
    }
    if (step == 3) {
      image(char23, locx, locy);
      step ++;
    }
  }
  if (dir == 3) {    
    if (step == 1) {
      image(char3, locx, locy);
      step ++;
    }
    if (step == 2) {
      image(char32, locx, locy);
      step ++;
    }
    if (step == 3) {
      image(char33, locx, locy);
      step ++;
    }
  }
  if (dir == 4) {    
    if (step == 1) {
      image(char4, locx, locy);
      step ++;
    }
    if (step == 2) {
      image(char42, locx, locy);
      step ++;
    }
    if (step == 3) {
      image(char43, locx, locy);
      step = 1;
    }
  }
}


void draw() {
  background(0);
  if (state == 1) {
    menu();
  }
  if (state == 2) {
    sprite();
  }
}
Tagged:

Answers

  • also how in heck do i make that readable?

  • edited November 2013

    Please use ctrl + k or Select all text (ctrl +A) and use tab once in your processing sketch before copying it here and then paste it.

  • Answer ✓

    (i have formatted the code)

    move the loadImage() calls out of sprite() and into setup(). they are (indirectly) in your draw() loop so it's loading them 60 times a second.

  • Answer ✓

    For a start take all the code to load images out of the menu (lines 47-49 incl.) and sprite (lines 65-76 incl.) functions and put them inside setup.

    Since the draw method is executed ~60 times a second and this calls either the menu or sprite functions, it means you are loading the images sixty times a second. No wonder you are using huge amounts of RAM Java's garbage collector has no chance of keeping up with you.

  • You should also consider using arrays.

Sign In or Register to comment.