직접 코코스2dx 게임 엔진 쓰기 (4) 창 GLView
이 대상을 통해 OpenGL의 보기를 얻을 수 있습니다.
GLview를 정의하기 전에 자주 사용하는 형상 헤더 파일을 정의해야 합니다.
// Geometry.h
4
#ifndef __GEMMETRY_H__
#define __GEMMETRY_H__
class Size {
public:
Size() {}
Size(float width, float height) {
this->width = width;
this->height = height;
}
float width;
float height;
};
class Vec2 {
public:
Vec2() {}
Vec2(float x, float y) {
this->x = x;
this->y = y;
}
float x;
float y;
};
class Rect {
public:
Rect() {}
Rect(float x, float y, float width, float height) {
origin.x = x;
origin.y = y;
size.width = width;
size.height = height;
}
Vec2 origin;
Size size;
};
#endif
이 헤더 파일에 정의된 크기와 Vec2는 모두 직사각형 Rect 서비스를 정의하는 것으로 각각 길이와 원점을 나타낸다.// GLView.h
#ifndef __GLVIEW_H__
#define __GLVIEW_H__
#include "Geometry.h"
class GLView {
public:
virtual ~GLView() {}
virtual void setViewName(const std::string& viewname) {
_viewName = viewname;
}
virtual void setFrameSize(float width, float height) {
_designResolutionSize = _screenSize = Size(width, height);
}
protected:
std::string _viewName;
Size _designResolutionSize;
Size _screenSize;
};
#endif
GLView에서는 창의 제목 이름을 나타내는 세 가지 속성이 정의되어 있으며, 두 치수는 모두 창의 크기이며, 뒤에 화면 크기가 알맞게 조정될 때 사용할 수 있습니다.APPDelegate에서 가져온 것은 GLView의 대상이 아니라 하위 클래스의 대상인 GLViewImpl입니다
#ifndef __GLVIEWIMPL_H__
#define __GLVIEWIMPL_H__
#include "GLView.h"
class GLViewImpl: public GLView {
public:
static GLViewImpl* createWithRect(const std::string& viewName, Rect rect);
bool initWithRect(const std::string& viewName, Rect rect);
};
GLViewImpl* GLViewImpl::createWithRect(const std::string& viewName, Rect rect) {
GLViewImpl* ret = new GLViewImpl;
if (ret && ret->initWithRect(viewName, rect)) {
// ret->autorelease;//
return ret;
}
if (ret != NULL) {
delete ret;
ret = NULL;
}
return NULL;
}
bool GLViewImpl::initWithRect(const std::string& viewName, Rect rect) {
setViewName(viewName);
setFrameSize(rect.size.width, rect.size.height);
}
#endif
그 중에서 자동으로 방출되는 기능이 언급되었는데 메모리 관리에 관한 것이고 다음에 메모리 관리의 내용을 본다.그리고 AppDelegate에서 호출할 때 현재 창이 존재하는지 여부를 판단하고 존재하지 않으면 다음과 같이 만듭니다.
#ifndef __APP_DELEGATE_H__
#define __APP_DELEGATE_H__
#include "Application.h"
#include "Director.h"
#include "GLView.h"
#include "GLViewImpl.h"
#include "Geometry.h"
#include <iostream>
class AppDelegate: private Application {
public:
virtual bool applicationDidFinishLaunching() {
Director* director = Director::getInstance();
GLView* glview = director->getOpenGLView();
if (!glview) {
glview = GLViewImpl::createWithRect("WinName", Rect(0, 0, 960, 640));
director->setOpenGLView(glview);
}
return true;
}
};
#endif
Director에서 현재 뷰 창으로 설정합니다.#ifndef __DIRECTOR_H__
#define __DIRECTOR_H__
#include "GLView.h"
class Director {
public:
Director() {
_openGLView = NULL;
}
virtual ~Director() {}
static Director* getInstance();
GLView* getOpenGLView() { return _openGLView; }
virtual bool init(){ return true; }
void setOpenGLView(GLView *openGLView) {
if (_openGLView != openGLView) {
if (_openGLView) {
// ,
}
_openGLView = openGLView;
}
}
private:
GLView *_openGLView;
};
class DisplayLinkDirector : public Director {
public:
};
static DisplayLinkDirector *s_SharedDirector = NULL;
Director* Director::getInstance()
{
if (!s_SharedDirector) {
s_SharedDirector = new DisplayLinkDirector;
s_SharedDirector->init();
}
return s_SharedDirector;
}
#endif
실제로 이 단계에 이르면 시작 절차가 완료되었습니다.cocos2dx 프로젝트에서 검은색 창을 볼 수 있고 장면과 메시지 순환이 추가되지 않았습니다.첨부 코드: demo4
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Cocos Studio v3.x ListView에 PanelNode를 동적으로 추가해보기Cococs Studio에서 ListView를 설정한 BaseNode와 파트 부분이 되는 PasrtNode를 작성. ListView에 PasrtNode를 추가해 가고, 빨리 GridView적인 것을 만드는 비망록. ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.