cocos2d-x 3 add a Sprite into a Layout - c++

When I do this it works:
Layout* layout = Layout::create();
layout->setLayoutType(Layout::Type::HORIZONTAL);
layout->setContentSize(Size(280, 150));
layout->setPosition(Vec2(visibleOrigin.x + 100, visibleOrigin.y + visibleSize.height - 100));
addChild(layout);
auto jacket = Button::create("jacket.png", "jacket.png", "jacket.png", Widget::TextureResType::PLIST);
layout->addChild(jacket);
But when I do this (add a Sprite instead of the button added in above code):
Layout* layout = Layout::create();
layout->setLayoutType(Layout::Type::HORIZONTAL);
layout->setContentSize(Size(280, 150));
layout->setPosition(Vec2(visibleOrigin.x + 100, visibleOrigin.y + visibleSize.height - 100));
addChild(layout);
auto jacket = Sprite::createWithSpriteFrameName("jacket.png");
layout->addChild(jacket);
Then I get assertion fail on line layout->addChild(jacket); with the message Expression: vector subscript out of range. I suppose sprites are not supported in Layouts? Then what is the right way to add and image into a layout? Should I use ImageView as below?
auto jacket = ImageView::create("jacket.png",TextureResType::PLIST);
layout->addChild(jacket);
If yes then why? and what is the difference of Sprite and Image?

if i understand correctly, the button will try to load the individual file named "jacket.png" whereas the createWithSpriteFrameName initializer will try to get the jacket.png frame from a previously loaded texture atlas
The problem could be as simple as the jacket image not being in a texture atlas or the atlas (frames) not being loaded before running this code

Related

Qt QPainter in millimetres instead of inches

I have a QPrinter that prints A4 either directly to a physical printer or a PDF. Now I'd like to use QPainter to draw in millimetres, but the current coordinate system seems to be the width and height of an A4 in inches times the resolution of the printer.
8.26 inch x 1200 res = 9912
11.69 inch x 1200 res = 14028
I have tried the following but text just ended up huge.
auto page = printer.pageRect(QPrinter::Unit::Millimeter);
painter.setWindow(QRect(0, 0, page.width(), page.height()));
How do I change this so my QPainter can draw to 210 x 297 mm instead of the above system?
This is on Windows 10 and with Qt 5.10.
I tested this method on X11 (ubuntu linux) PDF print, using ScreenResolution printer mode:
painter.begin(printer);
int log_w = 210;
int log_h = 297;
painter.setWindow(0, 0, log_w, log_h);
int phys_w = printer->width();
int phys_h = printer->height();
painter.setViewport(0, 0, phys_w, phys_h);
Basically, set your logical size in mm using the painter window, and give the painter's viewport the printer's physical size.
This line should print a rectangle around the page with a border of 10 mm:
painter.drawRect(10, 10, log_w - 20, log_h -20);
Text should work accordingly. This code should print the word Ok at the top left corner of the rectangle:
QFont font = painter.font();
font.setPointSize(10); //1 cm height
painter.setFont(font);
painter.drawText(10, 20, "Ok");
painter.end();
Using HighResolution printer mode, font size must be set using
font.setPixelSize(10); //1 cm height
and a QPen must be set to the painter:
QPen pen(Qt::black);
pen.setWidthF(0.2);
painter.setPen(pen);
painter.drawRect(10, 10, log_w - 20, log_h - 20);
About loss of device dependency using setPixelSize, I'm aware that here is stated:
It is possible to set the height of characters shown on the screen to
a specified number of pixels with setPixelSize(); however using
setPointSize() has a similar effect and provides device independence.
but I think it refers to screen only, given that here is stated:
When rendering text on a QPrinter device, it is important to realize
that the size of text, when specified in points, is independent of the
resolution specified for the device itself. Therefore, it may be
useful to specify the font size in pixels when combining text with
graphics to ensure that their relative sizes are what you expect.
I think that you are looking for the QTransform class, according to the official doc:
The QTransform class specifies 2D transformations of a coordinate
system. A transformation specifies how to translate, scale, shear,
rotate or project the coordinate system, and is typically used when
rendering graphics.
You can initialise your custom transform class:
QTransform transform = QTransform::fromScale(painter.device()->physicalDpiX() / scale, painter.device()->physicalDpiY() / scale);
A think that this could be helpfull, the number of dots per militmeter:
const int dot_per_millimeter = qRound(qApp->primaryScreen()->physicalDotsPerInch() / 25.40);
Customise then your scale & apply it using a QPainter:
QPainter painter(parent);
painter.setWorldTransform(transform, false);
Your approach is correct. Here is an example of how to set up a printer/painter pair. I don't fiddle around with the transformation matrix since it's sufficient to specify a window/viewport pair. I don't even specify the viewport explicitly, since it is automatically set to the metrics of the paint device (in this case the QPrinter object).
#include <QPrinter>
#include <QPainter>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QPrinter printer(QPrinter::PrinterResolution);
printer.setOrientation(QPrinter::Portrait);
printer.setPageSize(QPageSize(QPageSize::A4));
printer.setResolution(300 /*dpi*/);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName("ellipse.pdf");
QPainter painter(&printer);
auto page = printer.pageRect(QPrinter::Unit::Millimeter);
painter.setWindow(page.toRect());
// Draw a 5mm thick ellipse across the whole page.
painter.setPen(QPen(Qt::black, 5.0));
painter.drawEllipse(0, 0, 210, 297);
return 0;
}
It is hard to tell what goes wrong in your case without seeing the rest of the code

How can i have an image on a scene 10 times in a row without having to create a sprite for it everytime?

I'm trying to develop a game in cocos2d-x and want one image on the scene to be repeated 10 times in one row. Can a do it in a function and call it again using setPosition wherever I want that image?
Suppose the function name is point() where I have defeinition:
auto sprite = Sprite::create("point.png");
and function returns the sprite.
So can I create a sprite in it using that function and call it like
point()->setPosition(40, 40);
Sprite* GenerateScene::point()
{
auto sprite6 = Sprite::create("point.png");
sprite6->setAnchorPoint(Vec2(0.0, 0.0));
return sprite6;
}
bool GenerateScene::init()
{
auto sprite = Sprite::create("bkgnd.png");
sprite->setAnchorPoint(Vec2(0.0, 0.0));
sprite->addChild(point());
sprite->setPosition(0, 0);
point()->setPosition(120, 480);
}
This piece of code here only generates an "point.png" at (0, 0) location of the background image i.e. bottom left corner. It is not considering
point()->setPosition(120, 480);
statement.
Thanks
The purpose of a Sprite is to repeat an image on the screen.
If you looked at the Sprite::create source you would see that creating 10 sprites with the same image does not create and load 10 images. Instead there is a SpriteFrameCache that caches the actual image. Each of the 10 Sprites would be just a lighweight reference to an instance of that one image in the Scene.

How to create polygons to display running number in cocos2dx

I'm trying to create a node that is simply a rectangle with a number in it. And this is how I'm doing it now:
int size = 100, fontSize = 64;
auto node = DrawNode::create();
Vec2 vertices[] =
{
Vec2(0,size),
Vec2(size,size),
Vec2(size,0),
Vec2(0,0)
};
node->drawPolygon(vertices, 4, Color4F(1.0f,0.3f,0.3f,1), 0, Color4F(1.0f,1.0f,1.0f,1));
auto texture = new Texture2D();
int numberToDisplay = 2000;
std::string s = std::to_string(numberToDisplay);
texture -> initWithString(s.c_str(), "fonts/Marker Felt.ttf", fontSize, Size(size, size), TextHAlignment::CENTER, TextVAlignment::CENTER);
auto textSprite = Sprite::createWithTexture(texture);
node -> addChild(textSprite);
textSprite -> setPosition(size/2, size/2);
Every time I want to change the number I have to re-create a textureSprite, remove the current child and add the new one. Is there a better way to do it?
i wonder whether you want some special features, so why not use LayerColor and labelTTF?
LayerColor* node = LayerColor::create(Color4B(255, 85, 85, 255), 100, 100);
LabelTTF* label = LabelTTF::create(s, "fonts/Marker Felt.ttf", fontSize);
node->addChild(label);
just change content of labelttf,no need to create sprite
You could use two different techniques for achieve this, to me both of them are good
1:- Use texture cache to cache texture and change image texture at run time(good if u know how many exact textures are there and texture has same Size). in your .h file define no of textures like:-
Texture2D *startTexture, *endTexture, *midTexture;
in you .cpp file do it like:-
startTexture = Director::getInstance()->getTextureCache()->addImage(
"start.png");
endTexture = Director::getInstance()->getTextureCache()->addImage(
"end.png");
middleTexture = Director::getInstance()->getTextureCache()->addImage(
"middle.png");
after that when you want to change texture of any Sprite, simply do it like:-
textSprite->setTexture(startTexture);
for this to work with you, declare "textSprite" in your .h file aswell for quick access.
Pit-fall:- changing texture doesn't change sprite initial bounding box, if initial sprite texture was 32*32 and changed texture was 50*50, then extra texture of 20*20 will be cropped automatically starting from origin point, which might look bad. to over come this you need to change rect also using
textSprite->setTextureRect(
Rect(0, 0, startTexture->getContentSize().width,
startTexture->getContentSize().height));
2:- Using Sprite Frame Cache, put all your texture in a spriteframe, load it into memory like :-
SpriteFrameCache *spriteCache = SpriteFrameCache::getInstance();
spriteCache->addSpriteFramesWithFile("test.plist", "test.png");
now when ever you want to change you texture do it like this
testSprite->setSpriteFrame(
(SpriteFrameCache::getInstance())->getSpriteFrameByName(
"newImage.png"));
this will first check sprite cache for a image named "newImage.png", if it found it in memory then it will return that texture or else it will return nullptr.

Scaling items and rendering

I am making a small game in C++11 with Qt. However, I am having some issues with scaling.
The background of my map is an image. Each pixel of that image represents a tile, on which a protagonist can walk and enemies/healthpacks can be.
To set the size of a tile, I calculat the maximum amount like so (where imageRows & imageCols is amount of pixels on x- and y-axis of the background image):
QRect rec = QApplication::desktop()->screenGeometry();
int maxRows = rec.height() / imageRows;
int maxCols = rec.width() / imageCols;
if(maxRows < maxCols){
pixSize = maxRows;
} else{
pixSize = maxCols;
}
Now that I have the size of a tile, I add the background-image to the scene (in GameScene ctor, extends from QGraphicsScene):
auto background = new QGraphicsPixmapItem();
background->setPixmap(QPixmap(":/images/map.png").scaledToWidth(imageCols * pixSize));
this->addItem(background);
Then for adding enemies (they extend from a QGraphicsPixMapItem):
Enemy *enemy = new Enemy();
enemy->setPixmap(QPixmap(":/images/enemy.png").scaledToWidth(pixSize));
scene->addItem(enemy);
This all works fine, except that on large maps images get scaled once (to a height of lets say 2 pixels), and when zooming in on that item it does not get more clear, but stays a big pixel. Here is an example: the left one is on a small map where pixSize is pretty big, the second one has a pixSize of pretty small.
So how should I solve this? In general having a pixSize based on the screen resolution is not really useful, since the QGrapicsScene is resized to fit the QGraphicsView it is in, so in the end the view still determines how big the pixels show on the screen.
MyGraphicsView w;
w.setScene(gameScene);
w.fitInView(gameScene->sceneRect(), Qt::KeepAspectRatio);
I think you might want to look at the chip example from Qt (link to Qt5 but also works for Qt4).
The thing that might help you is in the chip.cpp file:
in the paint method:
const qreal lod = option->levelOfDetailFromTransform(painter->worldTransform());
where painter is simply a QPainter and option is of type QStyleOptionGraphicsItem. This quantity gives you back a measure of the current zoom level of your QGraphicsView and thus as in the example you can adjust what is being drawn at which level, e.g.
if (lod < 0.2) {
if (lod < 0.125) {
painter->fillRect(QRectF(0, 0, 110, 70), fillColor);
return;
}
QBrush b = painter->brush();
painter->setBrush(fillColor);
painter->drawRect(13, 13, 97, 57);
painter->setBrush(b);
return;
}
[...]
if (lod >= 2) {
QFont font("Times", 10);
font.setStyleStrategy(QFont::ForceOutline);
painter->setFont(font);
painter->save();
painter->scale(0.1, 0.1);
painter->drawText(170, 180, QString("Model: VSC-2000 (Very Small Chip) at %1x%2").arg(x).arg(y));
painter->drawText(170, 200, QString("Serial number: DLWR-WEER-123L-ZZ33-SDSJ"));
painter->drawText(170, 220, QString("Manufacturer: Chip Manufacturer"));
painter->restore();
}
Does this help?

How to change an images size in c++, SDL

How can I change the images size in the code below:
const int XHome = 10, YHome = 10;
const int WHome = 50, HHome = 50;
.
.
.
SDL_Surface* Image = SDL_LoadBMP(Address);
SDL_Rect destRect;
destRect.x = WHome * x;
destRect.y = HHome * y;
destRect.w = WHome;
destRect.h = HHome;
SDL_BlitSurface(Image, NULL, mainScreen, &destRect);
SDL_FreeSurface(Image);
When I put Image in mainScreen which is another SDL_Surface, It's bigger than 50*50. Is it possible to resize Image? Thank you.
this is what happens when I set the WHome and HHome, 50*50.
Since I have only 5 reputation, I can't post images. To see the image please click here.
But when I set them like the original images size, this is what I see:
here
According to the SDL_BlitSurface documentation:
Only the position is used in the dstrect (the width and height are ignored).
I highly recommend switching to SDL 2 for many reasons (hardware acceleration being a big one); this task would also become trivial with a texture and SDL_RenderCopy. If you're somehow stuck using SDL 1, you can either look into scaling surfaces manually, or use a library like SDL_gfx, which has custom blit functions.