Skip to content

Instantly share code, notes, and snippets.

@baku89
Forked from satoruhiga/ofApp.cpp
Created October 13, 2016 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baku89/087a9ddcd63a0bfc66390349d5b16e97 to your computer and use it in GitHub Desktop.
Save baku89/087a9ddcd63a0bfc66390349d5b16e97 to your computer and use it in GitHub Desktop.
GenUVLayout
#include "ofApp.h"
const int NUM_LED_PER_RING = 13;
const int NUM_ROW_IN_UV_LAYOUT = 6;
const int PIXEL_SIZE = 4;
const int PIXEL_MARGIN = 4;
const int STRIP_MARGIN = 10;
const int LAND_TEXT_HEADER_MARGIN = 60;
const int LAND_MARGIN = 20;
struct Strip
{
float x, y;
Strip(float x, float y)
: x(x), y(y)
{}
void draw()
{
ofPushMatrix();
ofTranslate(x, y);
int X = 0;
for (int i = 0; i < NUM_LED_PER_RING; i++)
{
ofDrawRectangle(X, 0, PIXEL_SIZE, PIXEL_SIZE);
X += PIXEL_SIZE + PIXEL_MARGIN;
}
ofPopMatrix();
}
static float getWidth()
{
return (PIXEL_SIZE + PIXEL_MARGIN) * NUM_LED_PER_RING - PIXEL_MARGIN;
}
static float getHeight() { return PIXEL_SIZE; }
ofRectangle getRectangle() const { return ofRectangle(x, y, getWidth(), getHeight()); }
};
struct Land
{
int ID;
float x, y;
int num_strips;
std::vector<Strip> strips;
ofRectangle rect;
Land(int ID, float x, float y, int num_strips)
: ID(ID)
, x(x)
, y(y)
, num_strips(num_strips)
{
rect = ofRectangle(0, 0, LAND_TEXT_HEADER_MARGIN, 10);
for (int i = 0; i < num_strips; i++)
{
int X = i % NUM_ROW_IN_UV_LAYOUT;
int Y = i / NUM_ROW_IN_UV_LAYOUT;
X *= (Strip::getWidth() + STRIP_MARGIN);
Y *= STRIP_MARGIN;
strips.emplace_back(X, Y);
rect.growToInclude(strips.back().getRectangle());
}
rect.x = x;
rect.y = y;
}
void draw()
{
ofPushMatrix();
ofTranslate(x, y);
for (auto& it : strips)
{
it.draw();
// ofDrawRectangle(it.getRectangle());
}
ofTranslate(rect.width, 0);
std::stringstream ss;
ss << "#" << ID << " : " << strips.size() << " " << " { " << rect << " }" << endl;
ofDrawBitmapString(ss.str(), 10, 10);
ofPopMatrix();
// ofDrawRectangle(rect);
}
ofRectangle getRectangle() const { return rect; }
};
std::vector<Land> lands;
//--------------------------------------------------------------
void ofApp::setup(){
ofBackground(0);
int offset_x = 10;
int offset_y = 10;
int ID = 0;
int N;
N = 6 * 14;
lands.emplace_back(ID, offset_x, offset_y, N);
offset_y += lands.back().getRectangle().height + LAND_MARGIN;
ID++;
N = 6 * 10;
lands.emplace_back(ID, offset_x, offset_y, N);
offset_y += lands.back().getRectangle().height + LAND_MARGIN;
ID++;
N = 6 * 8;
lands.emplace_back(ID, offset_x, offset_y, N);
offset_y += lands.back().getRectangle().height + LAND_MARGIN;
ID++;
N = 6 * 6;
lands.emplace_back(ID, offset_x, offset_y, N);
offset_y += lands.back().getRectangle().height + LAND_MARGIN;
ID++;
N = 6 * 5;
lands.emplace_back(ID, offset_x, offset_y, N);
offset_y += lands.back().getRectangle().height + LAND_MARGIN;
ID++;
N = 6 * 5;
lands.emplace_back(ID, offset_x, offset_y, N);
offset_y += lands.back().getRectangle().height + LAND_MARGIN;
ID++;
N = 6 * 3;
lands.emplace_back(ID, offset_x, offset_y, N);
offset_y += lands.back().getRectangle().height + LAND_MARGIN;
ID++;
N = 6 * 3;
lands.emplace_back(ID, offset_x, offset_y, N);
offset_y += lands.back().getRectangle().height + LAND_MARGIN;
ID++;
N = 6 * 1;
lands.emplace_back(ID, offset_x, offset_y, N);
offset_y += lands.back().getRectangle().height + LAND_MARGIN;
ID++;
N = 6 * 1;
lands.emplace_back(ID, offset_x, offset_y, N);
offset_y += lands.back().getRectangle().height + LAND_MARGIN;
ID++;
}
//--------------------------------------------------------------
void ofApp::update(){
}
//--------------------------------------------------------------
void ofApp::draw(){
for (auto& it : lands)
{
it.draw();
}
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
if (key == ' ')
{
ofFbo fbo;
fbo.allocate(1024, 768);
fbo.begin();
ofClear(0);
for (auto& it : lands)
{
it.draw();
}
fbo.end();
ofPixels pix;
fbo.readToPixels(pix);
ofSaveImage(pix, "template.png");
ofBeginSaveScreenAsSVG("template.svg");
for (auto& it : lands)
{
it.draw();
}
ofEndSaveScreenAsSVG();
ofBeginSaveScreenAsPDF("template.pdf");
for (auto& it : lands)
{
it.draw();
}
ofEndSaveScreenAsPDF();
}
}
//--------------------------------------------------------------
void ofApp::keyReleased(int key){
}
//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mouseEntered(int x, int y){
}
//--------------------------------------------------------------
void ofApp::mouseExited(int x, int y){
}
//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment