Why cocos2d-iphone v2,v3 x-v2 CCLabelBMFont align is not center? - cocos2d-iphone

I update cocos2d-iphone for required x64 ios.
But my game's CCLabelBMFont all go align error. How can I fix it ?
I set
txt.anchorPoint = ccp(.25, .5);
It worked. But it not the real solution way.
here the font resource: http://pan.baidu.com/s/1sjkDT7n
I write the debug code:
CGPoint basePot= ccp(0, 500);
CGPoint offsetPot= ccp(0, 40);
{
basePot = ccpSub(basePot, offsetPot);
CCLabelBMFont *txt = [CCLabelBMFont labelWithString:#"12345654321" fntFile:#"font_finish_score.fnt"];
txt.position=basePot;
txt.anchorPoint = ccp(0, .5);
[self addChild:txt];
}
{
basePot = ccpSub(basePot, offsetPot);
CCLabelBMFont *txt = [CCLabelBMFont labelWithString:#"12345654321" fntFile:#"font_finish_score.fnt"];
txt.position=basePot;
txt.anchorPoint = ccp(.5, .5);
[self addChild:txt];
}
{
basePot = ccpSub(basePot, offsetPot);
CCLabelBMFont *txt = [CCLabelBMFont labelWithString:#"12345654321" fntFile:#"font_finish_score.fnt"];
txt.position=basePot;
txt.anchorPoint = ccp(1, .5);
[self addChild:txt];
}
{
basePot = ccpSub(basePot, offsetPot);
CCLabelBMFont *txt = [CCLabelBMFont labelWithString:#"12345654321" fntFile:#"font_finish_score.fnt"];
txt.position=basePot;
txt.alignment = kCCTextAlignmentLeft;
[self addChild:txt];
}
{
basePot = ccpSub(basePot, offsetPot);
CCLabelBMFont *txt = [CCLabelBMFont labelWithString:#"12345654321" fntFile:#"font_finish_score.fnt"];
txt.position=basePot;
txt.alignment = kCCTextAlignmentCenter;
[self addChild:txt];
}
{
basePot = ccpSub(basePot, offsetPot);
CCLabelBMFont *txt = [CCLabelBMFont labelWithString:#"12345654321" fntFile:#"font_finish_score.fnt"];
txt.position=basePot;
txt.alignment = kCCTextAlignmentRight;
[self addChild:txt];
}
alignment don't work , and anthorPoint (.5,.5) not the center. Bothering...
I fount that the alignment align isn't intelligence. test_font.fnt by "Arial" work well. But my score font "HYZhongYuanJ" don't align well.

Related

How to draw a selected part of one surface above another image surface?

I want to draw the area of one surface above another surface, but nothing appears on the screen. I want to create area selection mechanics for making a screenshot. That means that I should clear the entire surface before next mouse movement in order not to place one selection above another.
void OcctGtkViewer::buttonclick(int *x, int *y, int *width, int *height)
{
bool isPressed = false;
int init_x, init_y, fin_x, fin_y;
int prevInitX, prevInitY, prevFinX, prevFinY;
Display *xdisplay = XOpenDisplay(0);
XEvent xevent;
::Window xroot = gdk_x11_window_get_xid(gtk_widget_get_window((GtkWidget *)myGLArea->gobj()));
int scr = DefaultScreen(xdisplay);
cairo_surface_t *surface = cairo_xlib_surface_create(xdisplay, xroot, DefaultVisual(xdisplay, scr), DisplayWidth(xdisplay, scr), DisplayHeight(xdisplay, scr));
cairo_t *cr = cairo_create(surface);
const char *backgroundImagePath = "/tmp/test.png";
const char *backgroundImagePath2 = "/tmp/test2.png";
cairo_surface_write_to_png(
surface,
backgroundImagePath);
cairo_surface_t *surfaceClearImage = cairo_image_surface_create_from_png(backgroundImagePath);
cairo_set_source_rgba(cr, 0, 0, 0, 0.2);
cairo_rectangle(cr, 0, 0, DisplayWidth(xdisplay, scr), DisplayHeight(xdisplay, scr));
cairo_fill(cr);
cairo_surface_write_to_png(
surface,
backgroundImagePath2);
cairo_surface_t *surfaceOverlay = cairo_image_surface_create_from_png(backgroundImagePath2);
cr = cairo_create(surfaceOverlay);
XGrabPointer(xdisplay, xroot, 1, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
while (1)
{
XNextEvent(xdisplay, &xevent);
if (xevent.type == MotionNotify && isPressed == true)
{
int tmp_x = xevent.xmotion.x;
int tmp_y = xevent.xmotion.y;
cairo_save(cr);
cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
cairo_paint(cr);
cairo_restore(cr);
cairo_set_source_surface(cr, surfaceClearImage, 0, 0);
cairo_rectangle(cr, init_x, init_y, tmp_x - init_x, tmp_y - init_y); // set rectangle
cairo_fill(cr);
XSync(xdisplay, true);
}
}
}
The result I want to achieve:
If I delete these lines of code: `
cairo_surface_t *surfaceOverlay = cairo_image_surface_create_from_png(backgroundImagePath2);
cr = cairo_create(surfaceOverlay);`
I get this result:

How to cut a part of a shape with the help of another painted shape above?

On the screen below I have the image with the painted translucent rectangle and with the painted opaque rectangle. My purpose is to cut the area of the opaque rectangle - delete pixels in the translucent rectangle in order to see the initial image.
cairo_surface_t *surface = cairo_xlib_surface_create(xdisplay, xroot, DefaultVisual(xdisplay, scr), DisplayWidth(xdisplay, scr), DisplayHeight(xdisplay, scr));
cairo_t *cr = cairo_create(surface);
cairo_surface_write_to_png(
surface,
"test.png");
cairo_surface_t *surfaceTmp = cairo_image_surface_create_from_png("./test.png");
if (xevent.type == MotionNotify && isPressed == true)
{
int tmp_x = xevent.xmotion.x_root;
int tmp_y = xevent.xmotion.y_root;
cairo_save(cr);
cairo_set_source_surface(cr, surfaceTmp, 1, 1);
cairo_paint(cr);
cairo_set_source_rgba(cr, 0, 0, 0, 0.2);
cairo_rectangle(cr, 0, 0, DisplayWidth(xdisplay, scr), DisplayHeight(xdisplay, scr));
cairo_fill(cr);
cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
cairo_rectangle(cr, init_x, init_y, tmp_x - init_x, tmp_y - init_y); // set rectangle
cairo_fill(cr);
cairo_restore(cr);
}
Why do I have this black rectangle? I thought that CAIRO_OPERATOR_CLEAR should delete the shape part beneath.
The desired outcome:
Maybe, since cairo's drawings change pixels directly (=not buffered), once you draw something, there remain no underlying original pixels that can be recovered afterwards. If you'd like to hole the rectangle, try the fill rule: CAIRO_FILL_RULE_EVEN_ODD.
cairo_set_fill_rule(cr, CAIRO_FILL_RULE_EVEN_ODD); // The default is CAIRO_FILL_RULE_WINDING.
cairo_set_source_rgba(cr, 0, 0, 0, 0.2);
cairo_rectangle(cr, 0, 0, DisplayWidth(xdisplay, scr), DisplayHeight(xdisplay, scr));
//cairo_fill(cr);
//cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
cairo_rectangle(cr, init_x, init_y, tmp_x - init_x, tmp_y - init_y); // set rectangle
cairo_fill(cr);

How to erace (delete) the filled cairo shape?

I want to paint a rectangle to select the area according to mouse coordinates. So, the rectangle should be created and deleted every frame (the if statement below executes every frame). I was trying to call these functions, but nothing happened - shapes were painting one above another.
cairo_surface_destroy(surface);
cairo_destroy(cr);
if (xevent.type == MotionNotify && isPressed == true)
{
int tmp_x = xevent.xmotion.x_root;
int tmp_y = xevent.xmotion.y_root;
int scr = DefaultScreen(xdisplay);
cairo_surface_t *surface = cairo_xlib_surface_create(xdisplay, xroot, DefaultVisual(xdisplay, scr), DisplayWidth(xdisplay, scr), DisplayHeight(xdisplay, scr));
cairo_t *cr = cairo_create(surface);
cairo_rectangle(cr, init_x, init_y, tmp_x - init_x, tmp_y - init_y); /* set rectangle */
cairo_set_source_rgba(cr, 0.3, 0.4, 0.6, 0.1); /* set fill color */
cairo_fill(cr);
cairo_destroy(cr);
}

How to fix random trimming of text in DirectWrite?

I'm displaying the fps of my application. When I change the size or position of the text, it sometimes isn't rendered properly and the layout rect is big enough.
Anyone know how to fix this?
Example code, only snippets:
Variables:
IDWriteTextFormat* textFormat;
WCHAR* text = L"Example0101";
int textSize = (int)wcslen(text);
D2D1_RECT_F rect = {};
ID2D1SolidColorBrush* brush;
Constructor:
Game::Game(ID2D1HwndRenderTarget* D2DRenderTarget) {
this->D2DRenderTarget = D2DRenderTarget;
CoInitialize(NULL);
CoCreateInstance(
CLSID_WICImagingFactory,
NULL,
CLSCTX_INPROC_SERVER,
IID_IWICImagingFactory,
(LPVOID*)&WICImagingFactory);
DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), (IUnknown**)&writeFactory);
writeFactory->CreateTextFormat(
L"Arial",
NULL,
DWRITE_FONT_WEIGHT_REGULAR,
DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL,
16.0f,
L"en-us",
&textFormat
);
rect.left = 480.0f;
rect.top = 275.0f;
rect.right = 1000.0f;
rect.bottom = 1000.0f;
D2DRenderTarget->CreateSolidColorBrush(D2D1::ColorF(1.0f, 1.0f, 1.0f, 1.0f), &brush);
}
Drawing:
void Game::draw() {
D2DRenderTarget->Clear(D2D1::ColorF(0.5f, 0.5f, 0.9f, 1.0f));
D2DRenderTarget->DrawTextW(text, textSize, textFormat, rect, brush, D2D1_DRAW_TEXT_OPTIONS_NO_SNAP, DWRITE_MEASURING_MODE_GDI_CLASSIC);
}
RenderTarget:
D2DFactory->CreateHwndRenderTarget(
D2D1::RenderTargetProperties(),
D2D1::HwndRenderTargetProperties(
window,
D2D1::SizeU(960, 540),
D2D1_PRESENT_OPTIONS_IMMEDIATELY),
&D2DRenderTarget
);
Everything is very normal. I guess this is solved with some kind of smoothing or maybe I just forgot something.

Qt C++ Set Set Vtk Axis Scake

I have the following code where i plot points in 3d space:
vtkSmartPointer<vtkPoints> points =
vtkSmartPointer<vtkPoints>::New();
for(int in = 0; in<x.size(); in++)
{
points->InsertNextPoint (x[in], y[in], ry[in]);
}
vtkSmartPointer<vtkPolyData> pointsPolydata =
vtkSmartPointer<vtkPolyData>::New();
pointsPolydata->SetPoints(points);
vtkSmartPointer<vtkVertexGlyphFilter> vertexFilter =
vtkSmartPointer<vtkVertexGlyphFilter>::New();
#if VTK_MAJOR_VERSION <= 5
vertexFilter->SetInputConnection(pointsPolydata->GetProducerPort());
#else
vertexFilter->SetInputData(pointsPolydata);
#endif
vertexFilter->Update();
vtkSmartPointer<vtkPolyData> polydata =
vtkSmartPointer<vtkPolyData>::New();
polydata->ShallowCopy(vertexFilter->GetOutput());
// Setup colors
unsigned char red[3] = {255, 0, 0};
unsigned char green[3] = {0, 255, 0};
unsigned char blue[3] = {0, 0, 255};
vtkSmartPointer<vtkUnsignedCharArray> colors =
vtkSmartPointer<vtkUnsignedCharArray>::New();
colors->SetNumberOfComponents(3);
colors->SetName ("Colors");
colors->InsertNextTupleValue(red);
colors->InsertNextTupleValue(green);
colors->InsertNextTupleValue(blue);
polydata->GetPointData()->SetScalars(colors);
// Visualization
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
#if VTK_MAJOR_VERSION <= 5
mapper->SetInputConnection(polydata->GetProducerPort());
#else
mapper->SetInputData(polydata);
#endif
vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
actor->GetProperty()->SetPointSize(5);
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
renderer->AddActor(actor);
this->ui->qvtkWidget->GetRenderWindow()->AddRenderer(renderer);
renderWindowInteractor->Start();
When I get the resulting plot. All the points are plotted, but is hard to see anything due to the default scale. What I'm not sure is how to zoom in the view on the z axis. Any help would be appreciated.