where to find code blocks Sfml image resources - c++

I'm using code blocks with sfml and besides it looking ugly, I'm not sure where to store images. I have to select the exact path by putting it in the project folder in the file system. How do i add images?

Say that your project was in
C:\Users\Me\Documents\MyProj
Then then putting the images in this folder would make them visible to SFML/codeblocks when using only the image name.
If you had an image called image.png then the full path to the image would be
C:\Users\Me\Documents\MyProj\image.png
To get the image into your program you must include
#include <SFML/Graphics.hpp>
And create a texture
sf::Texture Texture1;
And then you can assign this image like
Texture1.loadFromFile("C:\Users\Me\Documents\MyProj\image.png"); //Full path
Texture1.loadFromFile("image.png"); //Relative path
Next you create a sprite
sf::Sprite MySprite
And assign the texture to it
MySprite.setTexture(Texture1);
Finally to draw in the window use
window.draw(MySprite);

Related

Load a bmp file into a TSpeedButton

Using Embarcadero C++Builder, does anyone know how to manually load a .bmp file into a TSpeedButton using the Glyph property, setting a path to the image, not with the Object Inspector?
The Glyph property is a TBitmap, so you can use the TBitmap::LoadFromFile() method to load a new glyph from a file:
speedbutton->Glyph->LoadFromFile("filename.bmp");
Note: "Glyph can provide up to four images within a single bitmap. All images must be the same size and next to each other in a horizontal row."

Save sprite content as .png file with cocos2d-x

I need to create an empty sprite as a container and add there in different positions different sprites created by using different images. At the end I need to save the result which in added as child into the container sprite as an image - .png for example.
How I can do that?
You can use RenderTexture and call visit method of Sprite. Here is a sample code:
RenderTexture* renderTexture = RenderTexture::create(width, height, Texture2D::PixelFormat::RGBA8888);
renderTexture->begin();
sprite->visit();
renderTexture->end();
renderTexture->saveToFile("snapshot.png", Image::Format::PNG);

Can we make a cctexture from a sprite which dont have data in resource folder in cocos2dx c++ in ios

I made a sprite
CCSprite *sprite=CCSprite::create("1.png");
1.png and mask_circle.png is in my resource folder
CCSprite* mask = CCSprite::create("mask_circle.png");
mask->setPosition(ccp(winSize.width*.20, winSize.height*.80));
sprite->setPosition(ccp(mask->getContentSize().width/2,mask->getContentSize().height/2));
Now I masked this image by using CCMask.CCmask inherits CCSprite
CCMask* masked = CCMask::create(mask , sprite);
masked->setPosition(ccp(winSize.width*.20, winSize.height*.80));
this->addChild(masked,2);
Now this masked pointer of class CCMask is created using the 2 sprites which have 1.png and mask_circle.png in resources folder
Can we create a texture of masked object or not?It doesn't have any resource.It is created by code using other 2 sprites.
I want to create texture of this masked object.
If not possible is there a way to save this masked object sprite to my resource folder by some name like myimage.png?
Any help will be appreciated.
Thanks
I'm not sure if you can get a texture this way directly, but a way around it is to use RenderTexture. Draw the sprites by calling visit on them in a begin/end block and then use the appropriate method for saving the result to a file. After that you can load the image as a texture.

Loading a texture in SFML fails with no message as to why

I have been following the tutorials on SFML and tried to load a texture using the following code
sf::Texture texture;
if (!texture.loadFromFile("image.png"))
{
return sf::Texture();
}
This fails to load the texture, the sprite is white which is not the colour of my sprite..
As taken directly from the graphics-sprite tutorial on the SFML website
"The loadFromFile function sometimes fails with no obvious reason. First, check the error message printed by SFML in the
standard output (check the console). If the message is unable to open file, make sure that the working directory (which is the
directory any file path will be interpreted relatively to) is what you think it is: when you run the application from the explorer, the
working directory is the executable folder, but when you launch your program from your IDE (Visual Studio, Code::Blocks, ...) the
working directory is sometimes set to the project directory instead. This can generally be changed easily in the project settings."
Therefore ensure your image is firstly named correctly and secondendly make sure it is in the correct folder i.e. in your working directory.
Also if the texture fails to load instead of returning an empty sprite you could report an error to the console and then throw an exception.
This way you will be told the sprite does not load correctly and the program will have to handle the exception otherwise it will be terminated. This way no sprite in the game should
have a white texture unless intentional
Something like this:
sf::Texture texture;
if (!texture.loadFromFile("image.png"))
{
throw std::runtime_error("Could not load image.png");
}
Loading a PNG ? Make it 8bit. Other png formats can be loaded but are always displayed as white squares.

How to use texture generated by TexturePacker in CocosBuilder?

I know it's possible to create texture by Smart Sprite feature in CocosBuilder 3.0. But it's very unstable now. So I decided to use TexturePacker to generate the texture. My .plist and .png files are generated into different folders for different resolution inside my CocosBuilder project folder like this:
The problem is only the .png files shown in CocosBuilder (I can't find the .plist file):
Of course if I move the .png and .plist file into Resources folder. Both are will be shown inside CocosBuilder. But I have no way to select correct image for different resolution any more. I don't know what wrong with it. Any idea?
Thank you so much.
I sovled the problem. Actually it's because the folder structure. The CocosBuilder doesn't recognize resources-iphone folder. So I rearranged the folder structure like this. Then everything is fine.