วิธีการสร้างโปรแกรมจาก Openframeworks คือ อาศัยการ extends คลาส ofBaseApp ซึ่งเป็น base class สำหรับทุกๆ app ที่สร้างจาก openframeworks โดยในตัวคลาส ofBaseApp จะมี function หลักๆที่ควบคุม event การทำงานของโปรแกรมอยู่ 10 function คือ
3 ฟังก์ชันนี้จะเป็นลูปหลักของโปรแกรมของเรา คือ
setup() | | | | V update() <--| | | | | | | V | draw() - - |
ส่วน ฟังก์ชันที่เหลือคือ event ที่จะเกิดขี้นระหว่างการทำงานของโปรแกรม
เราก็สามารถเขียนโค้ดในฟังก์ชันเหล่านี้เพื่อให้ทำงานถ้าเกิดเหตุการณ์ในแต่ละ event ตามที่กล่าวข้างต้น
โดยตอนเขียนโค้ดให้เราสร้าง header ของคลาส (.h) และส่วน implementation (.cpp) ของคลาส ส่วน main ของโปรแกรมนั้นจะมีแค่โค้ดสร้างหน้าต่างเริ่มต้น และกำหนดค่า object ของ class ที่เป็น application เท่านั้น
#include "ofMain.h"
#include "testApp.h"
#include "ofAppGlutWindow.h"//========================================================================
int main( ){ofAppGlutWindow window;
ofSetupOpenGL(&window, 1024,768, OF_WINDOW); // <-------- setup the GL context// this kicks off the running of my app
// can be OF_WINDOW or OF_FULLSCREEN
// pass in width and height too:
ofRunApp( new testApp());}
โค้ด testApp.h
#ifndef _TEST_APP
#define _TEST_APP#include "ofMain.h"
class testApp : public ofBaseApp{
public:
void setup();
void update();
void draw();void keyPressed (int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);};
#endif
โค้ด testApp.cpp
#include "testApp.h"//--------------------------------------------------------------
void testApp::setup(){}
//--------------------------------------------------------------
void testApp::update(){}
//--------------------------------------------------------------
void testApp::draw(){}
//--------------------------------------------------------------
void testApp::keyPressed(int key){}
//--------------------------------------------------------------
void testApp::keyReleased(int key){}
//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){}
//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){}
//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){}
//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){}
//--------------------------------------------------------------
void testApp::windowResized(int w, int h){}
Recent comments
27 weeks 5 days ago
28 weeks 1 day ago
1 year 12 weeks ago
1 year 19 weeks ago
1 year 26 weeks ago
1 year 27 weeks ago
1 year 34 weeks ago
1 year 36 weeks ago
1 year 44 weeks ago