1

Normally we either create a sprite from a sprite frame, a file, or a texture by doing something like:

Sprite* foo =  Sprite::create(filename);

How can one create a white square of specified dimensions without using DrawNode or passing a file to a sprite ?

I know this is possible, because I stumbled upon another post which described how to do it, but neglected to book mark it, and the post doesn't show up in search results..

Rahul Iyer
  • 19,924
  • 21
  • 96
  • 190

2 Answers2

4

Something like this:

auto dataLen = width * height * bitsPerPixel * sizeof(unsigned char);
auto data = static_cast<unsigned char*>(malloc(dataLen));
memset(data, 255, dataLen);
auto texture = new Texture2D();
texture->initWithData(data, dataLen, Texture2D::PixelFormat::RGBA8888, width, height, Size(width, height));
auto sprite = Sprite::createWithTexture(texture);
Makalele
  • 7,431
  • 5
  • 54
  • 81
1

You could also try to use a base64-encoded string to create the sprite, there are some online converters to output such format.

  • jajajaja my aim was to gen-gen the image without outside files or having to store any thing (even a small base64 code).... that way from a single pixel we can draw anything... and make bigger and bigger sprites.... great things from small beginnings! The accepted answer is the kind of thing I was looking for.... props for suggesting though! – Rahul Iyer Oct 13 '16 at 09:03