I’ve read the PShape tutorial article, specifically about Retained Mode with a Custom PShape Class, and looked at the accompanying examples, but I can’t wrap my head around any of it. I’ve avoided Object-Oriented Programming for quite some time, and I think it’s time for me to finally put it to use. In my game, I currently use a function to draw my walls. It looks something like this:
void drawWall(float x, float y, float w, float h, PImage wallTexture) {
beginShape();
texture(wallTexture);
vertex(x,y,0,0);
vertex(x+w,y,1,0);
vertex(x+w,y+h,1,1);
vertex(x,y+h,0,1);
endShape();
}
The real function I’m using is for 3D walls in P3D, but this mockup has the essentials: parameters and execution. My problem is “converting” this into an object, specifically one that uses retained mode to save on performance, which is pretty low for some of the game’s players. My primary struggle is wrapping my head around having the correct number of objects with the parameters that I need when I need them. With the function, I can just tell the game to draw a set of walls in a certain pattern by using a few drawWall() functions. With objects, however, I don’t know how to change the parameters of the walls or how to avoid constantly creating new objects any time I want a wall.
I gave it a couple of hours of trial and error, and this is all I could come up with:
class wall {
PShape s;
float x;
float y;
float w;
float h;
wall(float tempX, float tempY, float tempW, float tempH) {
// First create the shape
s = createShape();
s.beginShape();
// You can set fill and stroke
s.fill(255);
s.stroke(0);
// Here, we are hardcoding a series of vertices
s.vertex(0,0,0,0);
s.vertex(5,0,1,0);
s.vertex(5,5,1,1);
s.vertex(0,5,0,1);
s.endShape(CLOSE);
x=tempX;
y=tempY;
w=tempW;
h=tempH;
}
void display() {
// Locating and drawing the shape
pushMatrix();
translate(x,y);
scale(w,h);
shape(s);
popMatrix();
}
}
So really, I’m looking for help on making a leap from functions to objects, or at least tips on saving performance in P3D if objects aren’t the only way.