c_cpp oF迷你的例子

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp oF迷你的例子相关的知识,希望对你有一定的参考价值。

bool ofxTCPManager::Bind(unsigned short usPort)
{
    /*
	struct sockaddr_in local;
	memset(&local, 0, sizeof(sockaddr_in));

	local.sin_family = AF_INET;
	local.sin_addr.s_addr = INADDR_ANY;
	//Port MUST be in Network Byte Order
	local.sin_port = htons(usPort);

	if (::bind(m_hSocket,(struct sockaddr*)&local,sizeof(local))){
		ofxNetworkCheckError();
		return false;
	}
	return true;*/
    
    struct sockaddr_in local;
    memset(&local, 0, sizeof(sockaddr_in));
    
    local.sin_family = AF_INET;
    local.sin_addr.s_addr = INADDR_ANY;
    //Port MUST be in Network Byte Order
    local.sin_port = htons(usPort);
    
    int yes=1;
    
    // lose the pesky "Address already in use" error message
    if (setsockopt(m_hSocket,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
        perror("setsockopt");
        ::exit(1);
    }
    
    if (::bind(m_hSocket,(struct sockaddr*)&local,sizeof(local))){
        ofxNetworkCheckError();
        return false;  
    }  
    
    return true;
}


//------------------------------------------------------------------------------
// Acces custom C header from C++
// This is C++ code
//------------------------------------------------------------------------------
extern "C" {
  // Get declaration for f(int i, char c, float x)
  #include "my-C-code.h"
}
int main()
{
  f(7, 'x', 3.14);   // Note: nothing unusual in the call
  // ...
}

//------------------------------------------------------------------------------
Use ifdef with or condition
#if defined (TARGET_OF_IOS)
#if defined(TARGET_OSX) || (TARGET_WIN32)
#endif
//------------------------------------------------------------------------------

// Generate mesh with GL line
mesh.setMode(OF_PRIMITIVE_LINES);
mesh.enableColors();
mesh.enableIndices();

ofVec3f top(100.0, 50.0, 0.0);
ofVec3f left(50.0, 150.0, 0.0);
ofVec3f right(150.0, 150.0, 0.0);
ofVec3f next(250,100,0);
ofVec3f next2(150,200,0);

mesh.addVertex(top);
mesh.addColor(ofFloatColor(1.0, 0.0, 0.0));

mesh.addVertex(left);
mesh.addColor(ofFloatColor(0.0, 1.0, 0.0));

mesh.addVertex(right);
mesh.addColor(ofFloatColor(1.0, 1.0, 0.0));

mesh.addVertex(next);
mesh.addColor(ofFloatColor(0.0, 1.0, 0.0));
mesh.addVertex(next2);
mesh.addColor(ofFloatColor(0.0, 1.0, 0.0));

mesh.addIndex(0);
mesh.addIndex(1);
mesh.addIndex(1);
mesh.addIndex(2);


mesh.addIndex(3);
mesh.addIndex(4);

mesh.addIndex(4);

mesh.addIndex(5);


//------------------------------------------------------------------------------
// Draw line with opengl
//------------------------------------------------------------------------------

glBegin(GL_LINE_STRIP);
    glColor3f(1.0, 1.0, 1.0);
    lastPoint = ofVec2f(ofGetMouseX(),ofGetMouseY());
        
    
    
    for (int i= 0; i< last_point.size(); i++) {
        
        glVertex3f(last_point[i].x, last_point[i].y,0);
    }
    
    glEnd();

    glColor3f(0.75, 0.75, 0.75);
    ofCircle(ofGetMouseX(),ofGetMouseY(), 10);

    if(last_point.size() > 1000) {
        last_point.clear();
    }else{
        last_point.push_back(ofVec2f(ofGetMouseX(),ofGetMouseY()));
    }
    
    
    
    
//------------------------------------------------------------------------------
// ACCESS extened class Constructer
//------------------------------------------------------------------------------
class base
{
  public:
  base (int arg)
  {
  }
};

class derived : public base
{
  public:
  derived () : base (number)
  {
  }
};


//------------------------------------------------------------------------------
// Snippet : OFXSCENE TEMPLATE
//------------------------------------------------------------------------------
#include "ofxScene.h"

class Scene1 : public ofxScene {
public:
//------------------------------------------------------------------------------
// VARIABLES
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// METHODS
//------------------------------------------------------------------------------
    Scene1() {};
    ~Scene1() {};
    
    void setup() {}
    void update(float dt) {}
    void draw() {}
    void exit() {};
    
    //scene notifications
    void sceneWillAppear( ofxScene * fromScreen ) {}
    void sceneWillDisappear( ofxScene * toScreen ) {}
    
    void sceneDidAppear( ofxScene * toScreen) { };
    void sceneDidDisappear( ofxScene * fromScreen ) {  };

};


in main class h->
#include "ofxSceneManager.h"
#include "Scene1.h"

enum Scenes{ SCENE_1 = 1, SCENE_2, SCENE_INIT }; // inside class decleration


// Scene Manager
ofxSceneManager * sceneManager;
Scene1 * myScene1;


in main class .cpp->setup
sceneManager = ofxSceneManager::instance();
    
myScene1 = new Scene1(this);

sceneManager->addScene( myScene1, SCENE_1);

sceneManager->setDrawDebug(false);
sceneManager->setCurtainDropTime(0.1);
sceneManager->setCurtainStayTime(0.0);
sceneManager->setCurtainRiseTime(0.1);


in main class .cpp->update
float dt = 0.016666666;
sceneManager->update( dt );

in main class .cpp->draw
sceneManager->draw();

////////////////////////////////////////////////////////////////////////////////
///// REMOVE EMPTY SPACES and line break
////////////////////////////////////////////////////////////////////////////////
string SceneContent::breakLines(string src) {
    
    vector<string> lo = ofSplitString(src, " ");
    
    for (int i = 0; i < lo.size(); i++) {
        
        if(i != 0) {
            src += "\n" + lo[i];
        }else{
            src = lo[i];
        }
        
    }
    
    return  src;
}



////////////////////////////////////////////////////////////////////////////////
// WINDOWLESS APPLICATION
////////////////////////////////////////////////////////////////////////////////
#include "ofMain.h"
#include "ofAppNoWindow.h"

//========================================================================
class ofApp : public ofBaseApp{
public:
    
void setup(){
        
        
}
    
void update(){
    
}
    
void draw(){
        
}
};

int main( ){
    ofAppNoWindow window;
    ofSetupOpenGL(&window, 1024,768, OF_WINDOW);
    ofRunApp( new ofApp());
}
string utf8_substr2(const string &str,int start, int length=INT_MAX)
{
    int i,ix,j,realstart,reallength;
    if (length==0) return "";
    if (start<0 || length <0)
    {
        //find j=utf8_strlen(str);
        for(j=0,i=0,ix=str.length(); i<ix; i+=1, j++)
        {
            unsigned char c= str[i];
            if      (c>=0   && c<=127) i+=0;
            else if (c>=192 && c<=223) i+=1;
            else if (c>=224 && c<=239) i+=2;
            else if (c>=240 && c<=247) i+=3;
            else if (c>=248 && c<=255) return "";//invalid utf8
        }
        if (length !=INT_MAX && j+length-start<=0) return "";
        if (start  < 0 ) start+=j;
        if (length < 0 ) length=j+length-start;
    }
    
    j=0,realstart=0,reallength=0;
    for(i=0,ix=str.length(); i<ix; i+=1, j++)
    {
        if (j==start) { realstart=i; }
        if (j>=start && (length==INT_MAX || j<=start+length)) { reallength=i-realstart; }
        unsigned char c= str[i];
        if      (c>=0   && c<=127) i+=0;
        else if (c>=192 && c<=223) i+=1;
        else if (c>=224 && c<=239) i+=2;
        else if (c>=240 && c<=247) i+=3;
        else if (c>=248 && c<=255) return "";//invalid utf8
    }
    if (j==start) { realstart=i; }
    if (j>=start && (length==INT_MAX || j<=start+length)) { reallength=i-realstart; }
    
    return str.substr(realstart,reallength);
}


// USAGE
for (int i = 0 ; i < _word.length(); i++) {
    string trimmed = utf8_substr2(_word,i,1);
    trimmedWord[i] = trimmed;
}
if(key == 351)
    clientTyping = clientTyping + "ş";
else if(key == 350)
    clientTyping = clientTyping + "Ş";
else if(key == 287)
    clientTyping = clientTyping + "ğ";
else if(key == 286)
    clientTyping = clientTyping + "Ğ";
else if(key == 252)
    clientTyping = clientTyping + "ü";
else if(key == 220)
    clientTyping = clientTyping + "Ü";
else if(key == 246)
    clientTyping = clientTyping + "ö";
else if(key == 214)
    clientTyping = clientTyping + "Ö";
else if(key == 231)
    clientTyping = clientTyping + "ç";
else if(key == 199)
    clientTyping = clientTyping + "Ç";
else if(key == 305)
    clientTyping = clientTyping + "ı";
else if(key == 304)
    clientTyping = clientTyping + "İ";
    
else if(key == 2306) {
    // Shift key do nothing
}
std::vector<float> data;
float min = *min_element(data.begin(), data.end());
float max = *max_element(data.begin(), data.end());
//------------------------------------------------------------------------------
// String to *Char
//------------------------------------------------------------------------------
char *host = (char*)"127.0.0.1";

string tr []= {"ı","İ","ş","Ş","ğ","Ğ","ç","Ç","ö","Ö","ü","Ü"," "};
string en []= {"i","i","s","s","g","g","c","c","o","o","u","u","_"};
    
int ko = sizeof(tr)/sizeof(string);

args.oscMessage = args.btnName;
for (int i = 0; i < ko; i++) {
    ofStringReplace(args.oscMessage, tr[i], en[i]);
}
// 1 - Move the Data folder inside to compiled Application
// Add this line into ofApp.cpp
ofSetDataPathRoot("../Resources/data/");

// 2- Click project file (blue icon at top-left)->Build Phases->Run Script 
// Paste the following line at the end
cp -r bin/data "$TARGET_BUILD_DIR/$PRODUCT_NAME.app/Contents/Resources";
////////////////////////////////////////////////////////////////////////////////
///// SET OPEN GL VERSION
////////////////////////////////////////////////////////////////////////////////
#include "ofApp.h"
#include "ofAppGlutWindow.h"

int main(){
    
    ofGLFWWindowSettings gl_settings;
    //gl_settings.setGLVersion( 3, 2 );
    gl_settings.setSize(1024, 768);
    gl_settings.visible = true;
    gl_settings.decorated = false;
    gl_settings.numSamples = 2;
    
    
    /*ofGLWindowSettings settings;
    settings.setSize(1024, 768);
    settings.setGLVersion(3, 2);
    settings.windowMode = OF_WINDOW;*/

    ofCreateWindow(gl_settings);

    ofRunApp(new ofApp());
}

以上是关于c_cpp oF迷你的例子的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp read_ports_of_microcontroller.ino

c_cpp Prime_Numbers-Sieve_of_Eratosthenes

c_cpp http://www.lintcode.com/en/problem/count-of-smaller-number/

c_cpp Binary Treehttp的最小深度://oj.leetcode.com/problems/minimum-depth-of-binary-tree/

c_cpp 羊群的例子

c_cpp LZ4的例子