I am having trouble drawing a basic white rectangle using the Cocos2dx library in the iOS environment. I have looked at a couple of other implementations for guidance.
http://discuss.cocos2d-x.org/t/draw-rectangle-with-drawrect-in-cocos2dx-not-working/14836/3
Cocos2d-x: How can I draw a resizing rectangle?
It basically looks like I need to use the class method of DrawNode::create() to make the node, then establish the vertices like in openGL, then draw it with the DrawNode->drawPolygon method and add the child node to the scene with the addChild method of the cocos2d::Layer subclass.
Here is my code.
bool JFScene::init()
{
if ( !Layer::init() )
{
return false;
}
auto visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
auto closeItem = MenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
CC_CALLBACK_1(JFScene::menuCloseCallback, this));
closeItem->setScale(2.0);
closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
origin.y + closeItem->getContentSize().height/2));
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 1);
auto CButton = Sprite::create("CButton.png");
CButton->setPosition(
Point(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y)
);
this->addChild(CButton, 1);
auto rectNode = DrawNode::create();
Vec2 vertices[4] = {
Vec2(-50, -50),
Vec2(50, -50),
Vec2(50, 50),
Vec2(-50, 50)
};
rectNode->drawPolygon(vertices, 4, Color4F::WHITE, 1, Color4F::WHITE);
this->addChild(rectNode);
return true;
}
The odd thing is that the CButton Node appears to render when I run it on my iPhone 5s as well as the close item that was included in the default file, but the white rectangle I am trying to draw does not render. Any ideas why?
You want to draw a rectangle... Look at this code snippet:
auto rectNode = DrawNode::create();
Vec2 rectangle[4];
rectangle[0] = Vec2(-50, -50);
rectangle[1] = Vec2(50, -50);
rectangle[2] = Vec2(50, 50);
rectangle[3] = Vec2(-50, 50);
Color4F white(1, 1, 1, 1);
rectNode->drawPolygon(rectangle, 4, white, 1, white);
this->addChild(rectNode);
I hope it works for you.
Reference: cocso2d-x forum
Besides, I recommend you take a look at this similar question if you didn't.
Related
I have trouble while combining Qt with vtkImplicitPlaneWidget2 then.
Without Qt, I could have the result shown like this.
Without Qt
But with Qt, I could not display the basic function of vtkImplicitPlaneWidget2, or in other words, it only shows the volume data, but the clipper of the plane could not be shown, just like the picture below.
With Qt
I try to find the result, and it seems that the callback function is never called in Qt. I think it is because that I don't connect the Qt event to the callback function, but I don't know how to deal with this.
And the following is the code.
void planeWidgetPara::showLabelPlane()
{
this->setupUi(this);
#pragma region image_process
vtkSmartPointer<vtkImageData> imageData =
vtkSmartPointer<vtkImageData>::New();
imageData->SetExtent(0, dims[0] / scaledown - 1,
0, dims[1] / scaledown - 1,
0, dims[2]);
imageData->SetSpacing(this->getSpacing());
imageData->AllocateScalars(VTK_UNSIGNED_CHAR, 3);
int scaledown = this->getScaledown();
for (int i = 0;i < dims[0] / scaledown;i++)
{
for (int j = 0;j < dims[1] / scaledown;j++)
{
for (int k = 0;k < dims[2];k++)
{
imageData->SetScalarComponentFromDouble(i, j, k, 0, getLabelScaledownColor(i, j, k, 0, scaledown));
imageData->SetScalarComponentFromDouble(i, j, k, 1, getLabelScaledownColor(i, j, k, 1, scaledown));
imageData->SetScalarComponentFromDouble(i, j, k, 2, getLabelScaledownColor(i, j, k, 2, scaledown));
}
}
}
vtkSmartPointer<vtkImageDataGeometryFilter> imageDataGeometryFilter =
vtkSmartPointer<vtkImageDataGeometryFilter>::New();
imageDataGeometryFilter->SetInputData(imageData);
imageDataGeometryFilter->Update();
// Setup a visualization pipeline
vtkSmartPointer<vtkPlane> plane =
vtkSmartPointer<vtkPlane>::New();
plane->SetOrigin(this->getRawOrigin());
vtkSmartPointer<vtkClipPolyData> clipper =
vtkSmartPointer<vtkClipPolyData>::New();
clipper->SetClipFunction(plane);
clipper->InsideOutOn();
clipper->SetInputConnection(imageDataGeometryFilter->GetOutputPort());
// Create a mapper and actor
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(clipper->GetOutputPort());
vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
vtkSmartPointer<vtkProperty> backFaces =
vtkSmartPointer<vtkProperty>::New();
backFaces->SetDiffuseColor(.8, .8, .4);
actor->SetBackfaceProperty(backFaces);
#pragma endregion
// A renderer and render window
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
this->qvtkWidget->GetRenderWindow();
renderWindow->AddRenderer(renderer);
renderer->AddActor(actor);
//// An interactor
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
this->qvtkWidget->GetRenderWindow()->GetInteractor();
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindow->Render();
// The callback will do the work
vtkSmartPointer<vtkIPWCallback> myCallback =
vtkSmartPointer<vtkIPWCallback>::New();
myCallback->Plane = plane;
myCallback->Actor = actor;
vtkSmartPointer<vtkImplicitPlaneRepresentation> rep =
vtkSmartPointer<vtkImplicitPlaneRepresentation>::New();
rep->SetPlaceFactor(1.25); // This must be set prior to placing the widget
rep->PlaceWidget(actor->GetBounds());
rep->SetNormal(plane->GetNormal());
vtkSmartPointer<vtkImplicitPlaneWidget2> planeWidget =
vtkSmartPointer<vtkImplicitPlaneWidget2>::New();
planeWidget->SetInteractor(renderWindowInteractor);
planeWidget->SetRepresentation(rep);
planeWidget->AddObserver(vtkCommand::InteractionEvent, myCallback);
// Render
renderWindowInteractor->Initialize();
renderWindow->Render();
planeWidget->On();
// Begin mouse interaction
//renderWindowInteractor->Start();
// VTK/Qt wedded
this->qvtkWidget->GetRenderWindow()->AddRenderer(renderer);
// Set up action signals and slots
connect(this->actionExit, SIGNAL(triggered()), this, SLOT(slotExit()));
}
I'm assinging a swipe event to a cocos2dx sprite but basically the problem is the event is assigned to the whole screen. I want it to be assigned only to the single sprite. Here is my code:
rect = Sprite::create();
rect->setTextureRect(Rect(0, 0, 180, 80));
// ekranın y ekseninde ortası | visibleSize.height / 2 + origin.y
rect->setPosition(Point(visibleSize.width / 2 + origin.x, visibleSize.height + 80));
auto grad = LayerGradient::create(Color4B(255, 255, 38, 255), Color4B(199, 173, 68, 255));
grad->changeHeight(rect->getContentSize().height);
grad->changeWidth(rect->getContentSize().width);
//grad->setPosition(Point(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y));
//grad->addChild(rect);
rect->addChild(grad);
this->addChild(rect);
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = CC_CALLBACK_2(GameScene::onTouchBegan, this);
listener->onTouchMoved = CC_CALLBACK_2(GameScene::onTouchMoved, this);
listener->onTouchEnded = CC_CALLBACK_2(GameScene::onTouchEnded, this);
listener->onTouchCancelled = CC_CALLBACK_2(GameScene::onTouchCancelled, this);
listener->setSwallowTouches(true);
this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, rect);
A touch event is usually triggered when the player touches the screen. The best way to implement a listener on a sprite would be by sub-classing it:
http://www.cocos2d-x.org/wiki/How_To_Subclass_Sprite_And_Add_Event_Listeners
You can later check the collision of the touch within the sprite listener event (on Touch Began) and then perform your function
I'm newbie in cocos2d-x and I need your help.
I need to make transparent a touched portion of the layer.
How to make a portion of the layer transparent? I had thought to use ССClippingNode, but I'm not find examples or docs.
I use C++. Thanks.
In TestCpp, project that was added to all cocos2d-x version, you can find examples of CCClipingNode.
If you want to hide part of CCNode(for example "layer") using CCClipingNode, you should add your layer to CCClipingNode.
This is the example that you can paste in the HelloWorld init:
bool HelloWorld::init()
{
if ( !CCLayer::init() )
{
return false;
}
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
addChild(CCLayerColor::create(ccc4(122, 144, 0, 255), visibleSize.width, visibleSize.height));
//this is the layer that we want to "cut"
CCLayer *layer = CCLayer::create();
CCSprite* pSprite = CCSprite::create("HelloWorld.png");
pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
layer->addChild(pSprite, 0);
//we need to create a ccnode, which will be a stencil for ccclipingnode, draw node is a good choice for that
CCDrawNode * stecil = CCDrawNode::create();
stecil->drawDot(ccp(visibleSize.width/2 + origin.x - 100, visibleSize.height/2 + origin.y), 30, ccc4f(0, 0, 0, 255));
stecil->drawSegment(ccp(0, 0), ccp(visibleSize.width, visibleSize.height), 20, ccc4f(0, 0, 0, 255));
//CCClipingNode show the intersection of stencil and theirs children
CCClippingNode *cliper = CCClippingNode::create(stecil);
//you want to hide intersection so we setInverted to true
cliper->setInverted(true);
cliper->addChild(layer);
addChild(cliper);
return true;
}
I tried to use Direct2D in our map application. But it is much more slower than GDI because D2D draw the whole screen.
When user scroll the window, GDI scroll the video buffer and ONLY draw a small portion of screen. That is why GDI is faster than D2D.
My questions are:
How D2D scroll screen? (Similar to ScrollWindow())
How I only draw a small portion of screen in D2D? (Similar to InvalidateRect() and GetClipBox())
I found a solution in
How to scroll window contents using Direct2D api?
Does anyone have a better solution?
My example is:
D2D1::Matrix3x2F matrix;
CPoint org(GetDeviceScrollPosition());
FLOAT scaleW=(FLOAT)m_totalLog.cx/m_totalDev.cx;
FLOAT scaleH=(FLOAT)m_totalLog.cy/m_totalDev.cy;
if(scaleW > scaleH) scaleW = scaleH;
matrix.SetProduct(D2D1::Matrix3x2F::Scale(scaleW, scaleH, D2D1::Point2F(0.0f, 0.0f)), D2D1::Matrix3x2F::Translation((FLOAT)-org.x, (FLOAT)-org.y));
//"Scale Device Screen" * "Scroll Device Screen"
pRenderTarget->SetTransform(matrix);
in CScrollView. But the performance is very poor.
I found one solution: Using "CDCRenderTarget". Then we can use the scroll/GetClipBox() functions in CScrollView.
void CMFCGdi_D2D2View::OnDraw(CDC* pDC)
{
CMFCGdi_D2D2Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc) return;
// TODO: add draw code for native data here
#define LINE_WIDTH 3
CRect rcInvalid;
pDC->GetClipBox(rcInvalid);
if(rcInvalid.left==0 && rcInvalid.top==0) pDC->FillSolidRect(rcInvalid, RGB(255,0,0));
else pDC->FillSolidRect(rcInvalid, RGB(0, 255, 0)); //visible redraw area
CRect rcLogical;
GetClientRect(rcLogical);
rcLogical.bottom+=GetScrollPosition().y;
rcLogical.right+=GetScrollPosition().x;
m_RenderTarget.BindDC(*pDC, rcLogical);
m_RenderTarget.BeginDraw();
m_RenderTarget.SetTransform(D2D1::Matrix3x2F::Identity());
m_RenderTarget.DrawEllipse(CD2DEllipse(CD2DPointF(150.0f, 150.0f), CD2DSizeF(100., 100.)), m_pBlackBrush, LINE_WIDTH);
m_RenderTarget.DrawLine(CD2DPointF(150.0f, 150.0f), CD2DPointF(150.0f + 100.0f * 0.15425f, 150.0f - 100.0f * 0.988f),
m_pBlackBrush, LINE_WIDTH);
m_RenderTarget.DrawLine(CD2DPointF(150.0f, 150.0f), CD2DPointF(150.0f + 100.0f * 0.525f, 150.0f + 100.0f * 0.8509f),
m_pBlackBrush, LINE_WIDTH);
m_RenderTarget.DrawLine(CD2DPointF(150.0f, 150.0f), CD2DPointF(150.0f - 100.0f * 0.988f, 150.0f - 100.0f * 0.15425f),
m_pBlackBrush, LINE_WIDTH);
HRESULT hr = m_RenderTarget.EndDraw();
if (SUCCEEDED(hr))
{
// Draw the pie chart with GDI.
CPen penBlack(PS_SOLID, LINE_WIDTH, RGB(255, 0, 0));
CPen* pOldPen = pDC->SelectObject(&penBlack);
pDC->Ellipse(300, 50, 500, 250);
POINT pntArray1[2];
pntArray1[0].x = 400;
pntArray1[0].y = 150;
pntArray1[1].x = static_cast<LONG>(400 + 100 * 0.15425);
pntArray1[1].y = static_cast<LONG>(150 - 100 * 0.9885);
POINT pntArray2[2];
pntArray2[0].x = 400;
pntArray2[0].y = 150;
pntArray2[1].x = static_cast<LONG>(400 + 100 * 0.525);
pntArray2[1].y = static_cast<LONG>(150 + 100 * 0.8509);
POINT pntArray3[2];
pntArray3[0].x = 400;
pntArray3[0].y = 150;
pntArray3[1].x = static_cast<LONG>(400 - 100 * 0.988);
pntArray3[1].y = static_cast<LONG>(150 - 100 * 0.15425);
pDC->Polyline(pntArray1, 2);
pDC->Polyline(pntArray2, 2);
pDC->Polyline(pntArray3, 2);
pDC->SelectObject(&pOldPen);
}
}
I'm developing game in iPhone in that pixel perfect collision will work only if one sprite appears on scene otherwise it wont work.can you please provide me some information?
I used this code for pixel perfect collision between animated sprites(spritesheet).
-(BOOL) isCollisionBetweenSpriteA:(CCSprite*)spr1 spriteB:(CCSprite*)spr2 pixelPerfect:(BOOL)pp
{
BOOL isCollision = NO;
CGRect intersection = CGRectIntersection([spr1 boundingBox], [spr2 boundingBox]);
// Look for simple bounding box collision
if (!CGRectIsEmpty(intersection))
{
// If we're not checking for pixel perfect collisions, return true
if (!pp) {return YES;}
CGPoint spr1OldPosition = spr1.position;
CGPoint spr2OldPosition = spr2.position;
spr1.position = CGPointMake(spr1.position.x - intersection.origin.x, spr1.position.y - intersection.origin.y);
spr2.position = CGPointMake(spr2.position.x - intersection.origin.x, spr2.position.y - intersection.origin.y);
intersection = CGRectIntersection([spr1 boundingBox], [spr2 boundingBox]);
// Assuming that the spritebatchnode of both sprites is the same, I just use one. If each sprite has a different sprite batch node as parent you should modify the code to get the spriteBatchNode for each sprite and visit them.
CCSpriteBatchNode* _sbnMain =(CCSpriteBatchNode*) spr1.parent;
//NOTE: We are assuming that the spritebatchnode is always at 0,0
// Get intersection info
unsigned int x = (intersection.origin.x)* CC_CONTENT_SCALE_FACTOR();
unsigned int y = (intersection.origin.y)* CC_CONTENT_SCALE_FACTOR();
unsigned int w = intersection.size.width* CC_CONTENT_SCALE_FACTOR();
unsigned int h = intersection.size.height* CC_CONTENT_SCALE_FACTOR();
unsigned int numPixels = w * h;// * CC_CONTENT_SCALE_FACTOR();
// create render texture and make it visible for testing purposes
int renderWidth = w+1;
int renderHeight = h+1;
if(renderWidth<32)
{
renderWidth =32;
}
if(renderHeight < 32)
{
renderHeight =32;
}
renderTexture = [[CCRenderTexture alloc] initWithWidth:renderWidth height:renderHeight pixelFormat:kCCTexture2DPixelFormat_RGBA8888];
//rt is always going to be at 0,0 - can't change it.
renderTexture.position = CGPointMake(0, 0);
[self addChild:renderTexture];
renderTexture.visible = NO;
//NSLog(#"\nintersection = (%u,%u,%u,%u), area = %u",x,y,w,h,numPixels);
// Draw into the RenderTexture
[renderTexture beginWithClear:0 g:0 b:0 a:0];
// Render both sprites: first one in RED and second one in GREEN
glColorMask(1, 0, 0, 1);
[_sbnMain visitSprite:spr1];
glColorMask(0, 1, 0, 1);
[_sbnMain visitSprite:spr2];
glColorMask(1, 1, 1, 1);
// Get color values of intersection area
ccColor4B *buffer = malloc( sizeof(ccColor4B) * numPixels );
glReadPixels(x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
[renderTexture end];
// Read buffer
unsigned int step = 1;
for(unsigned int i=0; i<numPixels; i+=step)
{
ccColor4B color = buffer[i];
if (color.r > 0 && color.g > 0)
{
isCollision = YES;
break;
}
}
// Free buffer memory
free(buffer);
spr1.position = spr1OldPosition;
spr2.position = spr2OldPosition;
[renderTexture release];
[self removeChild:renderTexture cleanup:YES];
} return isCollision;}