axmol/cocos/2d/CCDirector.h

538 lines
15 KiB
C
Raw Normal View History

/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2010-2013 cocos2d-x.org
Copyright (c) 2011 Zynga Inc.
Copyright (c) 2013-2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __CCDIRECTOR_H__
#define __CCDIRECTOR_H__
#include "CCPlatformMacros.h"
2013-07-19 13:42:45 +08:00
#include "CCObject.h"
#include "ccTypes.h"
#include "CCGeometry.h"
2013-12-07 10:48:02 +08:00
#include "CCVector.h"
#include "CCGL.h"
#include "kazmath/mat4.h"
#include "CCLabelAtlas.h"
2012-08-28 12:04:51 +08:00
2011-06-20 17:31:38 +08:00
2012-04-18 18:43:45 +08:00
NS_CC_BEGIN
2012-06-20 18:09:11 +08:00
/**
* @addtogroup base_nodes
* @{
*/
/* Forward declarations. */
class LabelAtlas;
class Scene;
class EGLView;
class DirectorDelegate;
class Node;
class Scheduler;
class ActionManager;
class EventDispatcher;
2013-12-21 16:56:28 +08:00
class EventCustom;
class EventListenerCustom;
class TextureCache;
2013-11-27 10:35:12 +08:00
class Frustum;
class Renderer;
2010-09-28 18:27:33 +08:00
/**
2013-09-23 02:39:42 +08:00
@brief Class that creates and handles the main Window and manages how
and when to execute the Scenes.
The Director is also responsible for:
2010-12-29 18:01:37 +08:00
- initializing the OpenGL context
- setting the OpenGL pixel format (default on is RGB565)
- setting the OpenGL buffer depth (default one is 0-bit)
- setting the projection (default one is 3D)
- setting the orientation (default one is Portrait)
Since the Director is a singleton, the standard way to use it is by calling:
_ Director::getInstance()->methodName();
The Director also sets the default OpenGL context:
- GL_TEXTURE_2D is enabled
- GL_VERTEX_ARRAY is enabled
- GL_COLOR_ARRAY is enabled
- GL_TEXTURE_COORD_ARRAY is enabled
*/
class CC_DLL Director : public Object
{
public:
2013-12-21 16:56:28 +08:00
static const char *EVENT_PROJECTION_CHANGED;
static const char* EVENT_AFTER_UPDATE;
static const char* EVENT_AFTER_VISIT;
static const char* EVENT_AFTER_DRAW;
/** @typedef ccDirectorProjection
Possible OpenGL projections used by director
*/
enum class Projection
{
/// sets a 2D projection (orthogonal projection)
_2D,
/// sets a 3D projection with a fovy=60, znear=0.5f and zfar=1500.
_3D,
/// it calls "updateProjection" on the projection delegate.
CUSTOM,
/// Default projection is 3D projection
DEFAULT = _3D,
};
/** returns a shared instance of the director */
static Director* getInstance();
/** @deprecated Use getInstance() instead */
CC_DEPRECATED_ATTRIBUTE static Director* sharedDirector() { return Director::getInstance(); }
/**
* @js ctor
*/
Director(void);
/**
* @js NA
* @lua NA
*/
virtual ~Director(void);
virtual bool init(void);
// attribute
2013-09-23 02:39:42 +08:00
/** Get current running Scene. Director can only run one Scene at a time */
2013-08-08 16:31:44 +08:00
inline Scene* getRunningScene() { return _runningScene; }
/** Get the FPS value */
2013-08-08 16:31:44 +08:00
inline double getAnimationInterval() { return _animationInterval; }
/** Set the FPS value. */
2013-11-16 21:08:00 +08:00
virtual void setAnimationInterval(double interval) = 0;
/** Whether or not to display the FPS on the bottom-left corner */
2013-08-08 16:31:44 +08:00
inline bool isDisplayStats() { return _displayStats; }
/** Display the FPS on the bottom-left corner */
inline void setDisplayStats(bool displayStats) { _displayStats = displayStats; }
/** seconds per frame */
inline float getSecondsPerFrame() { return _secondsPerFrame; }
/** Get the EGLView, where everything is rendered
* @js NA
* @lua NA
*/
2013-08-08 16:31:44 +08:00
inline EGLView* getOpenGLView() { return _openGLView; }
2013-11-16 21:08:00 +08:00
void setOpenGLView(EGLView *openGLView);
TextureCache* getTextureCache() const;
2013-08-08 16:31:44 +08:00
inline bool isNextDeltaTimeZero() { return _nextDeltaTimeZero; }
void setNextDeltaTimeZero(bool nextDeltaTimeZero);
/** Whether or not the Director is paused */
2013-08-08 16:31:44 +08:00
inline bool isPaused() { return _paused; }
2011-07-08 14:48:25 +08:00
/** How many frames were called since the director started */
2013-08-08 16:31:44 +08:00
inline unsigned int getTotalFrames() { return _totalFrames; }
2011-07-08 14:48:25 +08:00
/** Sets an OpenGL projection
@since v0.8.2
* @js NA
* @lua NA
*/
2013-08-08 16:31:44 +08:00
inline Projection getProjection() { return _projection; }
void setProjection(Projection projection);
/** Sets the glViewport*/
void setViewport();
2011-07-08 14:48:25 +08:00
/** How many frames were called since the director started */
/** Whether or not the replaced scene will receive the cleanup message.
If the new scene is pushed, then the old scene won't receive the "cleanup" message.
If the new scene replaces the old one, the it will receive the "cleanup" message.
@since v0.99.0
*/
2013-08-08 16:31:44 +08:00
inline bool isSendCleanupToScene() { return _sendCleanupToScene; }
/** This object will be visited after the main scene is visited.
This object MUST implement the "visit" selector.
Useful to hook a notification object, like Notifications (http://github.com/manucorporat/CCNotifications)
@since v0.99.5
*/
Node* getNotificationNode();
void setNotificationNode(Node *node);
2013-09-23 02:39:42 +08:00
/** Director delegate. It shall implement the DirectorDelegate protocol
@since v0.99.5
* @js NA
* @lua NA
*/
DirectorDelegate* getDelegate() const;
/**
* @js NA
* @lua NA
*/
2013-08-08 16:31:44 +08:00
void setDelegate(DirectorDelegate* delegate);
// window size
/** returns the size of the OpenGL view in points.
*/
2013-08-08 16:31:44 +08:00
const Size& getWinSize() const;
/** returns the size of the OpenGL view in pixels.
*/
2013-08-08 16:31:44 +08:00
Size getWinSizeInPixels() const;
/** returns visible size of the OpenGL view in points.
* the value is equal to getWinSize if don't invoke
* EGLView::setDesignResolutionSize()
*/
Size getVisibleSize() const;
/** returns visible origin of the OpenGL view in points.
*/
Point getVisibleOrigin() const;
/** converts a UIKit coordinate to an OpenGL coordinate
Useful to convert (multi) touch coordinates to the current layout (portrait or landscape)
*/
Point convertToGL(const Point& point);
/** converts an OpenGL coordinate to a UIKit coordinate
Useful to convert node points to window points for calls such as glScissor
*/
Point convertToUI(const Point& point);
/// XXX: missing description
2013-08-08 16:31:44 +08:00
float getZEye() const;
// Scene Management
/** Enters the Director's main loop with the given Scene.
* Call it to run only your FIRST scene.
* Don't call it if there is already a running scene.
*
* It will call pushScene: and then it will call startAnimation
*/
void runWithScene(Scene *scene);
/** Suspends the execution of the running scene, pushing it on the stack of suspended scenes.
* The new scene will be executed.
* Try to avoid big stacks of pushed scenes to reduce memory allocation.
* ONLY call it if there is a running scene.
*/
void pushScene(Scene *scene);
2013-09-23 02:39:42 +08:00
/** Pops out a scene from the stack.
* This scene will replace the running one.
* The running scene will be deleted. If there are no more scenes in the stack the execution is terminated.
* ONLY call it if there is a running scene.
*/
2013-08-08 16:31:44 +08:00
void popScene();
2013-09-23 02:39:42 +08:00
/** Pops out all scenes from the stack until the root scene in the queue.
2012-06-12 02:50:32 +08:00
* This scene will replace the running one.
* Internally it will call `popToSceneStackLevel(1)`
2012-06-12 02:50:32 +08:00
*/
2013-08-08 16:31:44 +08:00
void popToRootScene();
2013-09-23 02:39:42 +08:00
/** Pops out all scenes from the stack until it reaches `level`.
If level is 0, it will end the director.
If level is 1, it will pop all scenes until it reaches to root scene.
If level is <= than the current stack level, it won't do anything.
*/
void popToSceneStackLevel(int level);
/** Replaces the running scene with a new one. The running scene is terminated.
* ONLY call it if there is a running scene.
*/
void replaceScene(Scene *scene);
/** Ends the execution, releases the running scene.
It doesn't remove the OpenGL view from its parent. You have to do it manually.
* @lua endToLua
*/
2013-08-08 16:31:44 +08:00
void end();
/** Pauses the running scene.
The running scene will be _drawed_ but all scheduled timers will be paused
While paused, the draw rate will be 4 FPS to reduce CPU consumption
*/
2013-08-08 16:31:44 +08:00
void pause();
/** Resumes the paused scene
The scheduled timers will be activated again.
The "delta time" will be 0 (as if the game wasn't paused)
*/
2013-08-08 16:31:44 +08:00
void resume();
/** Stops the animation. Nothing will be drawn. The main loop won't be triggered anymore.
If you don't want to pause your animation call [pause] instead.
*/
2013-08-08 16:31:44 +08:00
virtual void stopAnimation() = 0;
/** The main loop is triggered again.
Call this function only if [stopAnimation] was called earlier
@warning Don't call this function to start the main loop. To run the main loop call runWithScene
*/
2013-08-08 16:31:44 +08:00
virtual void startAnimation() = 0;
/** Draw the scene.
This method is called every frame. Don't call it manually.
*/
2013-08-08 16:31:44 +08:00
void drawScene();
// Memory Helper
2013-09-23 02:39:42 +08:00
/** Removes all cocos2d cached data.
It will purge the TextureCache, SpriteFrameCache, LabelBMFont cache
@since v0.99.3
*/
2013-08-08 16:31:44 +08:00
void purgeCachedData();
/** sets the default values based on the Configuration info */
2013-08-08 16:31:44 +08:00
void setDefaultValues();
// OpenGL Helper
/** sets the OpenGL default values */
2013-08-08 16:31:44 +08:00
void setGLDefaultValues();
/** enables/disables OpenGL alpha blending */
2013-08-08 16:31:44 +08:00
void setAlphaBlending(bool on);
/** enables/disables OpenGL depth test */
2013-08-08 16:31:44 +08:00
void setDepthTest(bool on);
2013-08-08 16:31:44 +08:00
virtual void mainLoop() = 0;
/** The size in pixels of the surface. It could be different than the screen size.
High-res devices might have a higher surface size than the screen size.
Only available when compiled using SDK >= 4.0.
@since v0.99.4
*/
void setContentScaleFactor(float scaleFactor);
2013-08-08 16:31:44 +08:00
float getContentScaleFactor() const;
2013-11-27 10:35:12 +08:00
/**
Get the Culling Frustum
*/
Frustum* getFrustum() const { return _cullingFrustum; }
2010-12-29 18:01:37 +08:00
/** Gets the Scheduler associated with this director
@since v2.0
*/
Scheduler* getScheduler() const;
/** Sets the Scheduler associated with this director
@since v2.0
*/
void setScheduler(Scheduler* scheduler);
/** Gets the ActionManager associated with this director
@since v2.0
*/
ActionManager* getActionManager() const;
/** Sets the ActionManager associated with this director
@since v2.0
*/
void setActionManager(ActionManager* actionManager);
/** Gets the EventDispatcher associated with this director
@since v3.0
*/
EventDispatcher* getEventDispatcher() const;
/** Sets the EventDispatcher associated with this director
@since v3.0
*/
void setEventDispatcher(EventDispatcher* dispatcher);
/** Returns the Renderer
@since v3.0
*/
Renderer* getRenderer() const;
/* Gets delta time since last tick to main loop */
float getDeltaTime() const;
/**
* get Frame Rate
*/
float getFrameRate() const { return _frameRate; }
protected:
void purgeDirector();
2014-01-04 13:22:09 +08:00
bool _purgeDirectorInNextLoop; // this flag will be set to true in end()
2013-08-08 16:31:44 +08:00
void setNextScene();
void showStats();
void createStatsLabel();
void calculateMPF();
2013-12-05 17:19:01 +08:00
void getFPSImageData(unsigned char** datapointer, ssize_t* length);
2013-02-27 09:38:30 +08:00
/** calculates delta time since last time it was called */
void calculateDeltaTime();
//textureCache creation or release
void initTextureCache();
void destroyTextureCache();
/** Scheduler associated with this director
@since v2.0
*/
Scheduler *_scheduler;
/** ActionManager associated with this director
@since v2.0
*/
ActionManager *_actionManager;
/** EventDispatcher associated with this director
@since v3.0
*/
EventDispatcher* _eventDispatcher;
2013-12-21 16:56:28 +08:00
EventCustom *_eventProjectionChanged, *_eventAfterDraw, *_eventAfterVisit, *_eventAfterUpdate;
/* delta time since last tick to main loop */
float _deltaTime;
/* The EGLView, where everything is rendered */
EGLView *_openGLView;
//texture cache belongs to this director
TextureCache *_textureCache;
double _animationInterval;
double _oldAnimationInterval;
/* landscape mode ? */
bool _landscape;
bool _displayStats;
float _accumDt;
float _frameRate;
LabelAtlas *_FPSLabel;
LabelAtlas *_SPFLabel;
LabelAtlas *_drawsLabel;
/** Whether or not the Director is paused */
bool _paused;
2011-07-08 14:48:25 +08:00
/* How many frames were called since the director started */
unsigned int _totalFrames;
unsigned int _frames;
float _secondsPerFrame;
2013-11-27 10:35:12 +08:00
Frustum *_cullingFrustum;
2011-07-08 14:48:25 +08:00
/* The running scene */
Scene *_runningScene;
/* will be the next 'runningScene' in the next frame
nextScene is a weak reference. */
Scene *_nextScene;
/* If true, then "old" scene will receive the cleanup message */
bool _sendCleanupToScene;
/* scheduled scenes */
2013-12-07 10:48:02 +08:00
Vector<Scene*> _scenesStack;
/* last time the main loop was updated */
2013-07-19 13:42:45 +08:00
struct timeval *_lastUpdate;
/* whether or not the next delta time will be zero */
bool _nextDeltaTimeZero;
/* projection used */
Projection _projection;
/* window size in points */
Size _winSizeInPoints;
/* content scale factor */
float _contentScaleFactor;
/* store the fps string */
char *_FPS;
/* This object will be visited after the scene. Useful to hook a notification node */
Node *_notificationNode;
/* Projection protocol delegate */
DirectorDelegate *_projectionDelegate;
Renderer *_renderer;
2012-08-07 14:29:46 +08:00
// EGLViewProtocol will recreate stats labels to fit visible rect
friend class EGLViewProtocol;
};
2010-09-28 18:27:33 +08:00
/**
@brief DisplayLinkDirector is a Director that synchronizes timers with the refresh rate of the display.
Features and Limitations:
- Scheduled timers & drawing are synchronizes with the refresh rate of the display
- Only supports animation intervals of 1/60 1/30 & 1/15
@since v0.8.2
*/
class DisplayLinkDirector : public Director
{
public:
2013-08-08 16:31:44 +08:00
DisplayLinkDirector()
: _invalid(false)
{}
//
// Overrides
//
2013-08-08 16:31:44 +08:00
virtual void mainLoop() override;
virtual void setAnimationInterval(double value) override;
virtual void startAnimation() override;
virtual void stopAnimation() override;
protected:
bool _invalid;
};
2012-06-20 18:09:11 +08:00
// end of base_node group
/// @}
2012-04-18 18:43:45 +08:00
NS_CC_END
#endif // __CCDIRECTOR_H__