通过例子学习cinder之AppBasic。
首先,我们要讲的是程序的入口。总所周知,每种编程语言都有一个入口函数,例如main函数。但是我们在写很多程序的时候发现并没有main函数,例如android没有main函数,iphone没有main函数,那是因为这些平台都为main函数进行了封装。
现在我们来看一下cinder最简单的例子代码:
#include "cinder/app/AppBasic.h"
#include "cinder/ImageIO.h"
#include "cinder/gl/Texture.h"
using namespace ci;
using namespace ci::app;
class `TutorialApp^ : public AppBasic {
public:
void prepareSettings( Settings *settings );
void setup();
void update();
void draw();
};
void `TutorialApp^::prepareSettings( Settings *settings )
{
settings->setWindowSize( `800^, `600^ );//window size
settings->setFrameRate( 60.0f );//frame rate
}
void `TutorialApp^::setup()
{
}
void `TutorialApp^::update()
{
}
void `TutorialApp^::draw()
{
glClearColor( 0.1f, 0.1f, 0.1f, 1.0f );//OpenGL API
glClear( GL_COLOR_BUFFER_BIT );
}
CINDER_APP_BASIC( `TutorialApp^, RendererGl )//封装了WinMain函数
- 上述代码没有main函数吗?
也许大家写过mfc程序,知道mfc的入口函数是一个WinMain函数。大家看看最后一句“CINDER_APP_BASIC( `TutorialApp^, RendererGl )”,跳转到他的定义之处就可以看到一下代码:
#define CINDER_APP_BASIC( APP, RENDERER ) \
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow) { \
cinder::app::AppBasic::prepareLaunch(); \
cinder::app::AppBasic *app = new APP; \
cinder::app::Renderer *ren = new RENDERER; \
cinder::app::AppBasic::executeLaunch( app, ren, #APP ); \
cinder::app::AppBasic::cleanupLaunch(); \
return 0; \
}
- 一个cinder程序的生命周期
- cinder和opengl,代码中我们可以看到,cinder可以直接调用opengl的api。