Using Box2D in Cocos2d-x - cocos2d-x

I am trying to add a box2d on my current project in cocos2dx, I am using an ubuntu for my os and clion for my ide. everything was working but I can't use box2d
Box2D/Box2D.h No such file or directory
I tried to set this
option(USE_CHIPMUNK "Use chipmunk for physics library" OFF)
option(USE_BOX2D "Use box2d for physics library" ON)
and got this error
chipmunk.h: No such file or directory
Tried also to disable PHYSICS on cocos2d/cocos/base/ccConfig.h
#ifndef CC_USE_PHYSICS
#define CC_USE_PHYSICS 0
#endif
And I can't run the project with this error only
Error running command, return code: 2.
using cocos2d-x v3 from github
FULL SOURCE CODE JUST TO TEST IF IT WORKS
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
#include "Box2D/Box2D.h"
class HelloWorld : public cocos2d::Layer
{
public:
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// a selector callback
void menuCloseCallback(cocos2d::Ref* pSender);
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
};
#endif // __HELLOWORLD_SCENE_H__

Related

'Gdiplus::Graphics' : no appropriate default constructor available

Not sure what I am doing wrong.. I have a very muddy idea of how Constructors should be formatted or structured, so any insights would help!
Renderer.h
#pragma once
#include <afxwin.h>
#include <winapifamily.h>
#include <wtypes.h>
#include <gdiplus.h>
class Renderer
{
public:
Renderer();
~Renderer();
void Clear(Gdiplus::Color clearColor);
virtual void Free() = 0;
virtual void LoadFace(int index, char* path) = 0;
void InitFromHDC(HDC dc);
void Shutdown();
// Drawing surface
Gdiplus::Graphics _graphics;
protected:
private:
bool _gdiplusActive;
};
Renderer.cpp
Renderer::Renderer()
: _gdiplusActive(false)
{ // <-error here
}
Renderer::~Renderer() {}
...
I tried many variation of adding variables... but honestly, the error may be obvious who understands what a default constructor is. I dunno.
A default constructor is a construtor that takes no parameters.
You can't create a Gdiplus::Graphics out of thin air, you need to give it something to draw on: a bitmap, a window or a device context.
Here is the list of constructors available:
https://learn.microsoft.com/en-us/windows/win32/api/gdiplusgraphics/nf-gdiplusgraphics-graphics-graphics(constgraphics_)

cocos2d-x-3.0 draw vs onDraw

I'm using cocos2d-x v3.0 and in some test project I'm doing some custom drawing by overriding Node's draw method, but in the DrawPrimitives example provided they do something like this:
void DrawPrimitivesTest::draw()
{
_customCommand.init(_globalZOrder);
_customCommand.func = CC_CALLBACK_0(DrawPrimitivesTest::onDraw, this);
Director::getInstance()->getRenderer()->addCommand(&_customCommand);
}
void DrawPrimitivesTest::onDraw()
{
// drawing code here, why?
}
From reading the header and source files it seems like this may be some way of sending render commands straight to the renderer, is that correct?
Should I be using this method to do custom drawing? What's the difference between draw an onDraw?
EDIT:
As #Pedro Soares mentioned, since Cocos2D-X 3.0 you can't override draw() anymore. you have to use draw(Renderer *renderer, const kmMat4 &transform, bool transformUpdated) instead.
There is sample on cocos2d-x RC0 package that shows how to use the DrawPrimitives on top of other layers.
On your Layer .h add the following:
private:
void onDrawPrimitives(const kmMat4 &transform, bool transformUpdated);
CustomCommand _customCommand;
Now in the cpp of the Layer, override the layer draw method and include the onDrawPrimitives method:
void MyLayer::onDrawPrimitives(const kmMat4 &transform, bool transformUpdated)
{
kmGLPushMatrix();
kmGLLoadMatrix(&transform);
//add your primitive drawing code here
DrawPrimitives::drawLine(ccp(0,0), ccp(100, 100));
}
void MyLayer::draw(Renderer *renderer, const kmMat4& transform, bool transformUpdated)
{
_customCommand.init(_globalZOrder);
_customCommand.func = CC_CALLBACK_0(MyLayer::onDrawPrimitives, this, transform, transformUpdated);
renderer->addCommand(&_customCommand);
}
In future, cocos2d-x 3.x renderer will be multithreaded with command pool.
draw method called by visit method, to create new command. When command is performed by command pool, onDraw is called. At this moment, commands are performed in single thread, but in overloaded onDraw method you should assume, that it will be called in another thread to simplify future migration.
I use draw method for debugDraw Like this It may be helpful
void HelloWorld::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
{
Layer::draw(renderer, transform, flags);
Director* director = Director::getInstance();
GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POSITION );
director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
world->DrawDebugData();
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
}
The draw() expression should be the same as the base class function.
The draw method of Node for cocos 3.3rc is:
virtual void draw(Renderer *renderer, const Mat4& transform, uint32_t flags);

Cocos2d-x : stop Previous action

how to stop the previous action in Cococs2d-x
my sprite have continous animation in init method
and recently i launched move action
i want to stop that move action only
that repeat animation still to be there only.
For action we can set Tag using setTag method. When you don't want that action remove that using CCNode method "removeActionByTag"
Hi bit late to the party.
Another solution is to call stopAction(currentAction) before calling runAction(newAction). This enables you to stop currentActionSave before you start newAction. You will need to store the currentAction somehow, probably a member var, so you can later call stopAction(currentAction).
Heres a code example:
Dog.h
#include "cocos2d.h"
class Dog : public cocos2d::Sprite
{
public:
....
private:
....
bool OnTouchEnded(cocos2d::Touch* touch, cocos2d::Event* /*event*/)
cocos2d::Action* m_currentAction = nullptr;
};
Dog.cpp
#include "Dog.h"
....
bool Dog::OnTouchEnded(cocos2d::Touch* touch, cocos2d::Event* /*event*/)
{
using namespace cocos2d;
....
MoveTo* moveTo = MoveTo::create(time, pos);
if (m_currentAction)
{
stopAction(m_currentAction);
}
m_currentAction = runAction(moveTo);
return true;
}
Hope this helps someone.

function new or public static main

I am new to Haxe and probably this is a pretty basic question, which I can't really find an answer.
I see three ways to call main class:
1) use main()
//Entry point
public static main():void{
//do something...
}
2) use constructor new()
//Constructor
public function new(){
// init
}
3) use both main() and new()
static function main()
{
Lib.window.onload = function(e) new Main();
}
public function new()
{
//init
}
Is there a guideline or best practice to which one to use?
Thanks
Addressing all 3:
static function main() {} is the correct entry point. No matter which target, Haxe always begins execution at main().
The new() constructor isn't called automatically, if you want your main app to execute as an object rather than from a static function, you have to explicitly create the object: static function main() { new Main(); }
Some people prefer to keep their code in an object, rather than in static functions, which is where your 3rd example comes from. I usually do, and that's what you show in your 3rd example.
A few extra points:
The steps things are executed is explained here, basic summary:
All types/classes are registered
Boot.__init() runs platform specific initializations
Classes which have static function __init__() initialization methods are initialized
Static variables are intiailzed
Your main() function is executed
If you compile a class without a -main Main argument, but with the class path/name as an argument (eg haxe my.pack.MyClass) and with no public static function main(), the class is compiled, but it is not run automatically. This can be useful if you are creating a library or something similar - your class is there, but it needs to be called explicitly by some other Javascript etc.
In Javascript, the Haxe code starts running as soon as it is loaded, possibly before the DOM is ready. That is why it is a good idea to do:
static function main() {
js.Lib.window.onload = function(e) { runMyApp(); }
}
As you did in your example, this is a good idea if you want your code to run after the DOM is ready. Whether on load you call another static function, or instantiate a new MyApp() and run your app from there, that is up to you.

ReadingChanged never called for Accelerometer on WP8

I'm trying to use the C++ Accelerometer interface in the Windows::Devices::Sensors namespace on the Windows Phone 8. The code is very similar to a C# project I have that works, but I can't get the C++ event to fire like I can with my C# code.
My C++ code is a C# project with a C++ component, the C++ component just opens up the Accelerometer device for reading, and then tries to setup an event to fire whenever data is ready:
AccelerometerWrapper::AccelerometerWrapper() {
Accelerometer^ acc = Accelerometer::GetDefault();
accReading = acc->ReadingChanged::add( ref new TypedEventHandler<Accelerometer^, AccelerometerReadingChangedEventArgs^>(this, &AccelerometerWrapper::ReadingChanged));
}
void AccelerometerWrapper::ReadingChanged(Accelerometer^ sender, AccelerometerReadingChangedEventArgs^ e) {
...
}
Unfortunately, my ReadingChanged() function is never being called. I've looked around for a Start() method or somesuch but I can't find anything. I'm basing most of my knowledge off of the AccelerometerCPP example, but I can't actually test that as it is a generic WinRT (e.g. Windows 8, not Windows Phone 8) example, and my computer does not have an accelerometer. Everything compiles and runs, the event is just never triggered.
EDIT: I have successfully run a test to verify that I can manually call acc->GetCurrentReading(), so the accelerometer is working, it just seems to be the event that is not getting triggered.
Thank you in advance!
I'm not a C++ expert, but the new Accelerometer sensor works on my machine using C#.
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
var accelerometer = Accelerometer.GetDefault();
if (accelerometer != null)
{
accelerometer.ReadingChanged += accelerometer_ReadingChanged;
}
}
void accelerometer_ReadingChanged(Accelerometer sender, AccelerometerReadingChangedEventArgs args)
{
Debug.WriteLine(args.Reading.AccelerationX + ", " + args.Reading.AccelerationY + "," + args.Reading.AccelerationZ);
}
When we run that code snippet we can see the following expected output:
Just a guess for C++, do you need to cache the Accelerometer instance somewhere instead of letting it go out of scope?