직접 코코스2dx 게임 엔진 쓰기 (4) 창 GLView

13114 단어 cocos2d-x게임 엔진
이전 섹션에서 Director 객체를 찾았습니다.
이 대상을 통해 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

좋은 웹페이지 즐겨찾기