Powered by Drupal, an open source content management system

โฆษณาโดย Google

Openframeworks เบื้องต้น

วิธีการสร้างโปรแกรมจาก Openframeworks คือ อาศัยการ extends คลาส ofBaseApp ซึ่งเป็น base class สำหรับทุกๆ app ที่สร้างจาก openframeworks โดยในตัวคลาส ofBaseApp จะมี function หลักๆที่ควบคุม event การทำงานของโปรแกรมอยู่ 10 function คือ

  • void setup();
    สำหรับกำหนดค่าเริ่มต้น โดย setup จะถูกเรียกเมื่อเริ่มโปรแกรม
  • void update();
    อัพเดท จะถูกเรียกเมื่อจะมีการทำการ render frame ของหน้าต่างโปรแกรมเราอีกตครั้ง
  • void draw();
    ส่วน draw จะถูกเรียกทุกๆครั้งเพื่อทำการวาดรูปใหม่ในแต่ละการ render ของ frame ของหน้าต่างโปรแกรมของเรา

3 ฟังก์ชันนี้จะเป็นลูปหลักของโปรแกรมของเรา คือ

			setup()
			  | |
			  | |
			   V
			update() <--|
			  | |       |
			  | |       |
			   V        |
			 draw() - - |

ส่วน ฟังก์ชันที่เหลือคือ event ที่จะเกิดขี้นระหว่างการทำงานของโปรแกรม

  • void keyPressed (int key);
    เมื่อมีการกดปุ่มคีย์บอร์ด
  • void keyReleased(int key);
    เมื่อปล่อยปุ่มคีย์บอร์
  • void mouseMoved(int x, int y );
    เมื่อมีการขยับเมาส์
  • void mouseDragged(int x, int y, int button);
    เมื่อมีการ drag
  • void mousePressed(int x, int y, int button);
    เมื่อคลิ๊กเมาส์
  • void mouseReleased(int x, int y, int button);
    เมื่อปล่อยเมาส์
  • void windowResized(int w, int h);
    เมื่อปรับขนาดหน้าต่างของโปรแกรม

เราก็สามารถเขียนโค้ดในฟังก์ชันเหล่านี้เพื่อให้ทำงานถ้าเกิดเหตุการณ์ในแต่ละ 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){

}