Best way to move code out of ofApp::setup()


#1

Apologies if this is a really stupid question, I’m very new to C++/openFrameworks/coding in general.

I have the following two pieces of code in ofApp::setup() which create two vectors, one of 63 vec3 points and the second of 48 ofRectangles based on the first. I’m using them to draw with later.

In ofApp.h

vector <glm::vec3> coords_100;
vector <ofRectangle> squares;

In ofApp.cpp setup

for (int y = 0; y < ofGetHeight() + 1; y = y + 100)
	{
		for (int x = 0; x < ofGetWidth() + 1; x = x + 100)
		{
			glm::vec3 temp_pos(x - ofGetWidth() / 2, y - ofGetHeight() / 2, 0.f);
			coords_100.push_back(temp_pos);
		}
	}

for (int i = 0; i < coords_100.size() - (ofGetWidth() / 100 + 2); i++)
	{
		if ((i + 1) % ((ofGetWidth() / 100) + 1) != 0)
		{
			ofRectangle a(coords_100[i], coords_100[i + 2 + (ofGetWidth() / 100)]);
			squares.push_back(a);
		}
	}

They work fine as they are but I’d like to take them out of setup to keep my main code as clean as possible.

I’ve done this as a pair of functions in their own cpp file and also as a struct, I’m just a bit worried that my lack of experience is going to bite me if I’m using a pattern that’s really messy as both these solutions to the problem have four extra vectors. I expect it doesn’t matter as they’re all small in this instance but when I’m working with more data this sort of thing will be a problem won’t it?

Any advice you could give me would be brilliant.

The struct version hpp

struct Set
{
	vector <glm::vec3> a;
	vector <ofRectangle> b;
	vector <glm::vec3> coords();
	vector <ofRectangle> squares();
};

cpp

#include "Set.hpp"

vector <glm::vec3> Set::coords()
{
	for (int y = 0; y < ofGetHeight() + 1; y = y + 100)
	{
		for (int x = 0; x < ofGetWidth() + 1; x = x + 100)
		{
			glm::vec3 temp_pos(x - ofGetWidth() / 2, y - ofGetHeight() / 2, 0.f);
			a.push_back(temp_pos);
		}
	}
	return a;
}

vector <ofRectangle> Set::squares()
{
	for (int i = 0; i < a.size() - (ofGetWidth() / 100 + 2); i++)	
	{
		if ((i + 1) % ((ofGetWidth() / 100) + 1) != 0)
		{
			ofRectangle c(a[i], a[i + 2 + (ofGetWidth() / 100)]);
			b.push_back(c);
		}
	}
	return b;
}

and ofApp.cpp setup

coords_100 = set.coords();
squares = set.squares();

#2

The solution looks fine for now. Its hard to tell if it will not fit future uses if those are not yet defined. Most important things you will have to think of are:

  • when will you call coords() and squares()?. those are filling a vector but not clearing before build.
  • will you read a and b while rebuilding their contents? in that case you will have to be very careful not to do both at the same time.
  • are those two members (a and b) really forced to live in the same class (set)?
  • do you really need to store ofRectangles or should you use ofVec4f?

These just came up right now… But you really don’t need to worry about those things if your program is working well for what you need. Try to worry only when problems arise.


#3

Thanks that’s really helpful advice. At the moment I’m just using them to draw a bunch of crosses and have random squares flash on the screen. The vectors won’t grow (they’re just to have all the positions to hand for other uses).

Plan is to have around twenty random squares fire at once and fade over time triggered by a keypress. Once that’s working swap the keypress trigger for midi and add spout so I can use the oF sketch as a mask in my VJing software that reacts to my bandmate’s midi drum patterns.


#4

Your proposed structure seems fine. For my taste a function of that few calls would be a little overkill to place in a struct. I would probably just make a function within ofApp and call it from Setup & windowResized etc.

However, if you are happy with your struct route it is fine, I guess the only add I would suggest is if this is going to be expanded on later, maybe consider making it a Class rather than a Struct? Sometimes a class’s extra features end up being handy down the road.

Anyways good luck!


#5

Thank you, it’s all baby steps for me at the moment but I’m enjoying the challenge. Picking when something should be a function/class/struct I expect I’ll just get better at over time.

The window isn’t every going to be resized (while the program is running and the code is only going to be run once so I expect all the faffing around with it is a bit pointless and it really does just belong in setup().

Was quite pleased I managed to work out how to make sure if I need the program to run in a different window size or shape further down the line it’ll still work regardless of what dimensions are set in ofSetupOpenGL()