Cocos2d-x Where will be the best place to cache sprites? - cocos2d-iphone

I want to cache some sprite frames while the user sees a loading page.
When should I implement the following three things?
display loading screen
cache sprites
hide loading screen
The choices are:
FirstScene : init
FirstScene : onEnter
FirstScene : onEnterTransitionDidFinish
... Move to the second scene ...
SecondScene : init
FirstScene : onExitTransitionStart
FirstScene : onExit
FirstScene : dealloc
SecondScene : onEnter
SecondScene : onEnterTransitionDidFinish
... Exit second scene ...
SecondScene : onExitTransitionDidStart
SecondScene : onExit
SecondScene : dealloc

Related

Using QWebEngineView seems to disrupt QPropertyAnimation smoothness, even if QWebEngineView has just been destroyed

I have a problem with QPropertyAnimation when QWebEngineView is used in the same QMainWindow.
If I create 15 QPropertyAnimation (animate QGraphicsRectItem),
and in the same time I use a QWebEngineView, then animations become very slow.
I have created a demo (to compile yourself), with minimum usefull source code : https://media.nperf.com/files/misc/src_sandbox_web_animation.zip
Tests made with one QMainWindow :
After creating QWebEngineView, if I kill it, the problem continues.
If I use QTimer loop instead of QPropertyAnimation, the problem is the same.
Tests made with two QMainWindow :
If I embed the QWebEngineView in an external window (new QMainWindow), then animations become smooth again.
This new QMainWindow is not the same QMainWindow where QPropertyAnimations are running.
However, this is not an acceptable solution for me, but this shows that there is a resource that is shared between QWebEngineView and QPropertyAnimations,
if and only if the QWebEngineView was created in the same QMainWindow.
So I wonder :
Did I kill the QWebEngineView correctly ?
Is there a way to seperate QWebEngineView threads from QMainWindow ?
Or any other solution...
Environment context :
This problem exist with two environments, win10/Qt5.15.2 and Mac/Qt5.15.0.
But does not exist with ubuntu20/Qt5.12 and ubuntu18/Qt5.9.2.
Reproductible code example (Code description of demo shared):
The QPropertyAnimation change size of a QGraphicsRectItem :
class AnimationBarItem : public QObject, public QGraphicsRectItem
{
Q_OBJECT
Q_PROPERTY(QRectF rect READ rect WRITE setRect)
...
}
class AnimationBar : public QGraphicsView
{
Q_OBJECT
...
AnimationBarItem *bar = nullptr;
}
void AnimationBar::setPourcent(qreal pourc){
QPropertyAnimation *animation = new QPropertyAnimation(bar, "rect");
animation->setDuration(200);
animation->setStartValue(QRect(0, 0, 0, mH));
animation->setEndValue(QRect(0, 0, ww,mH));
animation->start();
}
The QWebEngineView is created as follow :
mWebEngineView = new QWebEngineView();//can't link to this, for compatibility with my project
mWebEnginePage = new QWebEnginePage(mWebEngineView);
mWebEnginePage->setView(mWebEngineView);
mWebEngineView->setUrl(QUrl("http://www.google.com"));
then add in QMainWindow with method :
mLayoutForBrowse=new QVBoxLayout(ui->ZONE_webview);
mLayoutForBrowse->addWidget(mWebEngineView);
Or, in a new QMainWindow :
And not in the same QMainWindow like previous point, with :
this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
killing webview :
void MainWindow::killWebView(bool is){
if (mWebEngineView!=nullptr) mWebEngineView->stop();
mWebEngineView->deleteLater();
mLayoutForBrowse->deleteLater();
mWebEnginePage->deleteLater();
mWebEngineView=nullptr;
mLayoutForBrowse=nullptr;
mWebEnginePage=nullptr;
}

Constructor not drawing borders. (my mini game)

I'm trying to make a mini game in c++ and i have encountered a problem. I am currently trying to make a board for my game and i've made a function to draw the borders but i don't want to call it, i want it to be called by the constructor.
Board.h :
class Board
{
public:
Board(Graphics& out_gfx);
private:
void DrawBoardBorder();
Graphics& in_gfx;
Color borderColor = Colors::MakeRGB(94,35,113);
};
Board.cpp :
Board::Board(Graphics & out_gfx)
:
in_gfx(out_gfx)
{
DrawBoardBorder();
}
The borders are drawn correctly if the function is moved to public and called with the help of the object, but the borders are not drawn if i let the constructor do the job. Why?

Overlay multiple widgets

I have one QWidget which contains a multiple sliders. All sliders resized to main QWidget size. As result all sliders share same draw rectangle. For sliders I overload paintEvent method, so it draw only required stuff. Here is an example code:
class MySlider : public QSlider
{
void paintEvent(QPaintEvent *event) {
...
}
}
class MyWidget : public QWidget
{
MyWidget() : QWidget() {
slider1 = new MySlider(this);
slider2 = new MySlider(this);
slider1->resize(rect().width(), rect().height());
slider2->resize(rect().width(), rect().height());
}
MySlider * slider1;
MySlider * slider2;
}
adsf
Groove is not seen with this solution (because we don't call QSlider::paintEvent), but it still exist. For this widget it is possible to use only the last created slider (slider2). The rest are visible, but they are not available.
Is it possible to overlay multiple widgets on each other and still be able to access all of them with mouse event?
Overlapping widgets isn't a good idea, expect only one is visible at the same time. What is the purpose for that overlapping?
You can set QWidget::setAttribute(Qt::WA_TransparentForMouseEvents) to not generate any mouse events for that particular widget so that only one slider will get that events. Then you are able to redirect that messages to your other sliders.

What is the difference between `Subclass of CCScene` and `Subclass of CCNode` in cocos2d-iphone?

What is the difference between Subclass of CCScene and Subclass of CCNode when adding new cocos2d CCNode class? I used CCScene when I was following cocos2d-tutorials, but in the cocos2d + SpriteBuilder project default MainScene class is subclass of node, so can somebody explain it to me?
Very little. Open CCScene's header file and you see it subclasses CCNode. If you open CCScene's implementation file you'll essentially see this (assuming we are talking about cocos2d v3):
#implementation CCScene
// -----------------------------------------------------------------
// Private method used by the CCNode.scene property.
-(BOOL)isScene {return YES;}
-(id)init
{
if((self = [ super init ]))
{
CGSize s = [CCDirector sharedDirector].designSize;
_anchorPoint = ccp(0.0f, 0.0f);
[self setContentSize:s];
self.colorRGBA = [CCColor blackColor];
}
return( self );
}
// -----------------------------------------------------------------
- (void)onEnter
{
[super onEnter];
// mark starting scene as dirty, to make sure responder manager is updated
[[[CCDirector sharedDirector] responderManager] markAsDirty];
}
// -----------------------------------------------------------------
- (void)onEnterTransitionDidFinish
{
[super onEnterTransitionDidFinish];
// mark starting scene as dirty, to make sure responder manager is updated
[[[CCDirector sharedDirector] responderManager] markAsDirty];
}
// -----------------------------------------------------------------
#end
What you see above is the scene's implementation. It is essentially a node with the content size set to the design size (view size if it is not set), an anchor point of (0,0), a flag that marks it as a scene (which is privately used by CCNode), and a black color for its RGBA. A CCNode does not assume this stuff (for example content size of a node is 0,0 whereas the scene has a a content size of the view/design).
Nodes are the base class for many cocos2d classes. A scene, as you see, is just a node with a meaningful name, a set content size the size of the view (unless you specify a design size), and a black color. On the other hand a sprite is also a node but with other properties related to sprites.
I haven't bothered to use Sprite Builder, but I'd assume by what you've wrote that everything is added to a CCNode, which is ultimately added to a CCScene. Since there is no CCLayer, I'd assume it is simply just using a CCNode to act as the default layer. Someone else can confirm since I've never had a reason to use Sprite Builder but that sounds like it would be the case.

QGraphicsView::itemAt() method always return zero

I have some custom QGraphicsItems in a QGraphicsView of a QGraphicsScene.
I wish to know if there is an Item in the view at given (x,y) coordinates.
To test purpose I use the following class as QGraphicsScene:
class CustomScene : public QGraphicsScene
{
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if (QGraphicsItem *item = itemAt(event->scenePos())) {
qDebug() << "You clicked on item" << item;
}
else {
qDebug() << "You didn't click on an item.";
}
}
};
In my application I have:
a class "Screen::Screen(QWidget *parent): QWidget(parent){...}" with inside an instance of my class "CustomScene : public QGraphicsScene {...}" described above and an instance of class QGraphicsView.
some instances of my class "Rect : public QGraphicsRectItem {...}", added to the QGraphicsView,s that draw some rectangles after some calculations.
When I launch the application and click on the drawn rectangles, I always have "You didn't click on an item." message.
Searching here in previous posts or searching on google I didn't find the reason why my code doesn't work. What am I doing wrong?
Edit 1: boundingRect() method returns correctly. I tried to add some QGraphicsRectItem, itemAt() method returns their information correctly.
The problem was that I didn't override the QPainterPath QGraphicsItem::shape () const [virtual] method.
Once done, itemAt() method started to works as expected.