Quantcast
Channel: beginners - openFrameworks
Viewing all articles
Browse latest Browse all 4929

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

$
0
0

@cunabula wrote:

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();

Posts: 3

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 4929

Trending Articles