void create() { Texture::ptr texture = Texture::ptr(new Texture(1024,1024, Pixmap::Format::RGBA8888, Pixmap::Gdx2d)); Pixmap::ptr pixmap = Pixmap::newFromRect(840, 480, Pixmap::Format::RGBA8888, Pixmap::Gdx2d); texture->setFilter(Texture::TextureFilter::Nearest, Texture::TextureFilter::Linear); texture->setWrap(Texture::TextureWrap::ClampToEdge, Texture::TextureWrap::ClampToEdge); pixmap->setColor(1.0f, 0.0f, 0.0f, 1.0f); // Red pixmap->drawLine(0, 0, 100, 100); pixmap->setColor(0.0f, 0.0f, 1.0f, 1.0f); // Blue pixmap->drawLine(100, 100, 200, 0); pixmap->setColor(0.0f, 1.0f, 0.0f, 1.0f); // Green pixmap->drawLine(100, 0, 100, 100); pixmap->setColor(1.0f, 1.0f, 1.0f, 1.0f); // White pixmap->drawCircle(400, 300, 100); texture->draw(*pixmap, 0, 0); region = new TextureRegion(texture, 0, 0, 800, 480); batch = new SpriteBatch(); Pixmap::ptr px = Pixmap::newFromRect(512, 1024, Pixmap::Format::RGBA8888, Pixmap::Gdx2d); for (int y = 0; y < pixmap->getHeight(); y++) { // 1024 for (int x = 0; x < pixmap->getWidth(); x++) { // 512 pixmap->getPixel(x, y); } } px->dispose(); }
void TextureManager::registerTexture(Texture::ptr& texture) { if (this->registeredTextures.find(texture->getName()) == this->registeredTextures.end()) { this->registeredTextures[texture->getName()] = texture; } else { LogError("aready has key: {}", texture->getName()); } }
void SpriteCache::add (Texture::ptr texture, float x, float y, int srcX, int srcY, int srcWidth, int srcHeight) { float invTexWidth = 1.0f / texture->getWidth(); float invTexHeight = 1.0f / texture->getHeight(); float u = srcX * invTexWidth; float v = (srcY + srcHeight) * invTexHeight; float u2 = (srcX + srcWidth) * invTexWidth; float v2 = srcY * invTexHeight; float fx2 = x + srcWidth; float fy2 = y + srcHeight; tempVertices[0] = x; tempVertices[1] = y; tempVertices[2] = color; tempVertices[3] = u; tempVertices[4] = v; tempVertices[5] = x; tempVertices[6] = fy2; tempVertices[7] = color; tempVertices[8] = u; tempVertices[9] = v2; tempVertices[10] = fx2; tempVertices[11] = fy2; tempVertices[12] = color; tempVertices[13] = u2; tempVertices[14] = v2; if (mesh->getNumIndices() > 0) { tempVertices[15] = fx2; tempVertices[16] = y; tempVertices[17] = color; tempVertices[18] = u2; tempVertices[19] = v; add(texture, tempVertices, 30, 0, 20); } else { tempVertices[15] = fx2; tempVertices[16] = fy2; tempVertices[17] = color; tempVertices[18] = u2; tempVertices[19] = v2; tempVertices[20] = fx2; tempVertices[21] = y; tempVertices[22] = color; tempVertices[23] = u2; tempVertices[24] = v; tempVertices[25] = x; tempVertices[26] = y; tempVertices[27] = color; tempVertices[28] = u; tempVertices[29] = v; add(texture, tempVertices, 30, 0, 30); } }
void SpriteCache::add ( Texture::ptr texture,float x,float y ) { float fx2 = x + texture->getWidth(); float fy2 = y + texture->getHeight(); tempVertices[0] = x; tempVertices[1] = y; tempVertices[2] = color; tempVertices[3] = 0; tempVertices[4] = 1; tempVertices[5] = x; tempVertices[6] = fy2; tempVertices[7] = color; tempVertices[8] = 0; tempVertices[9] = 0; tempVertices[10] = fx2; tempVertices[11] = fy2; tempVertices[12] = color; tempVertices[13] = 1; tempVertices[14] = 0; if ( mesh->getNumIndices() > 0 ) { tempVertices[15] = fx2; tempVertices[16] = y; tempVertices[17] = color; tempVertices[18] = 1; tempVertices[19] = 1; add ( texture, tempVertices, 30, 0, 20 ); } else { tempVertices[15] = fx2; tempVertices[16] = fy2; tempVertices[17] = color; tempVertices[18] = 1; tempVertices[19] = 0; tempVertices[20] = fx2; tempVertices[21] = y; tempVertices[22] = color; tempVertices[23] = 1; tempVertices[24] = 1; tempVertices[25] = x; tempVertices[26] = y; tempVertices[27] = color; tempVertices[28] = 0; tempVertices[29] = 1; add ( texture, tempVertices, 30, 0, 30 ); } }
void SpriteBatch::draw (const TextureRegion& region,float x,float y,float width,float height) { if (!drawing) throw std::runtime_error("SpriteBatch.begin must be called before draw."); Texture::ptr texture = region.getTexture(); if (texture.get() != lastTexture) { renderMesh(); lastTexture = texture.get(); invTexWidth = 1.0f / texture->getWidth(); invTexHeight = 1.0f / texture->getHeight(); } else if (idx == verticesSize) // renderMesh(); float fx2 = x + width; float fy2 = y + height; float u = region.u; float v = region.v2; float u2 = region.u2; float v2 = region.v; utils::float_buffer& buffer = mesh->getVerticesBuffer(); buffer.limit(buffer.position() + 20); vertices[idx++] = x; vertices[idx++] = y; vertices[idx++] = color; vertices[idx++] = u; vertices[idx++] = v; vertices[idx++] = x; vertices[idx++] = fy2; vertices[idx++] = color; vertices[idx++] = u; vertices[idx++] = v2; vertices[idx++] = fx2; vertices[idx++] = fy2; vertices[idx++] = color; vertices[idx++] = u2; vertices[idx++] = v2; vertices[idx++] = fx2; vertices[idx++] = y; vertices[idx++] = color; vertices[idx++] = u2; vertices[idx++] = v; }
void SpriteBatch::draw (const TextureRegion& region,float x,float y,float originX,float originY,float width,float height,float scaleX,float scaleY,float rotation,bool clockwise) { if (!drawing) throw std::runtime_error("SpriteBatch.begin must be called before draw."); Texture::ptr texture = region.getTexture(); if (texture.get() != lastTexture) { renderMesh(); lastTexture = texture.get(); invTexWidth = 1.0f / texture->getWidth(); invTexHeight = 1.0f / texture->getHeight(); } else if (idx == verticesSize) { renderMesh(); } // bottom left and top right corner points relative to origin float worldOriginX = x + originX; float worldOriginY = y + originY; float fx = -originX; float fy = -originY; float fx2 = width - originX; float fy2 = height - originY; // scale if (scaleX != 1 || scaleY != 1) { fx *= scaleX; fy *= scaleY; fx2 *= scaleX; fy2 *= scaleY; } // construct corner points, start from top left and go counter clockwise float p1x = fx; float p1y = fy; float p2x = fx; float p2y = fy2; float p3x = fx2; float p3y = fy2; float p4x = fx2; float p4y = fy; float x1; float y1; float x2; float y2; float x3; float y3; float x4; float y4; // rotate if (rotation != 0) { float cos = math::utils::cosDeg(rotation); float sin = math::utils::sinDeg(rotation); x1 = cos * p1x - sin * p1y; y1 = sin * p1x + cos * p1y; x2 = cos * p2x - sin * p2y; y2 = sin * p2x + cos * p2y; x3 = cos * p3x - sin * p3y; y3 = sin * p3x + cos * p3y; x4 = x1 + (x3 - x2); y4 = y3 - (y2 - y1); } else { x1 = p1x; y1 = p1y; x2 = p2x; y2 = p2y; x3 = p3x; y3 = p3y; x4 = p4x; y4 = p4y; } x1 += worldOriginX; y1 += worldOriginY; x2 += worldOriginX; y2 += worldOriginY; x3 += worldOriginX; y3 += worldOriginY; x4 += worldOriginX; y4 += worldOriginY; float u1, v1, u2, v2, u3, v3, u4, v4; if (clockwise) { u1 = region.u2; v1 = region.v2; u2 = region.u; v2 = region.v2; u3 = region.u; v3 = region.v; u4 = region.u2; v4 = region.v; } else { u1 = region.u; v1 = region.v; u2 = region.u2; v2 = region.v; u3 = region.u2; v3 = region.v2; u4 = region.u; v4 = region.v2; } vertices[idx++] = x1; vertices[idx++] = y1; vertices[idx++] = color; vertices[idx++] = u1; vertices[idx++] = v1; vertices[idx++] = x2; vertices[idx++] = y2; vertices[idx++] = color; vertices[idx++] = u2; vertices[idx++] = v2; vertices[idx++] = x3; vertices[idx++] = y3; vertices[idx++] = color; vertices[idx++] = u3; vertices[idx++] = v3; vertices[idx++] = x4; vertices[idx++] = y4; vertices[idx++] = color; vertices[idx++] = u4; vertices[idx++] = v4; }
void SpriteCache::add ( Texture::ptr texture,float x,float y,float originX,float originY,float width,float height,float scaleX,float scaleY,float rotation,int srcX,int srcY,int srcWidth,int srcHeight,bool flipX,bool flipY ) { // bottom left and top right corner points relative to origin float worldOriginX = x + originX; float worldOriginY = y + originY; float fx = -originX; float fy = -originY; float fx2 = width - originX; float fy2 = height - originY; // scale if ( scaleX != 1 || scaleY != 1 ) { fx *= scaleX; fy *= scaleY; fx2 *= scaleX; fy2 *= scaleY; } // construct corner points, start from top left and go counter clockwise float p1x = fx; float p1y = fy; float p2x = fx; float p2y = fy2; float p3x = fx2; float p3y = fy2; float p4x = fx2; float p4y = fy; float x1; float y1; float x2; float y2; float x3; float y3; float x4; float y4; // rotate if ( rotation != 0 ) { float cos = cosDeg ( rotation ); float sin = sinDeg ( rotation ); x1 = cos * p1x - sin * p1y; y1 = sin * p1x + cos * p1y; x2 = cos * p2x - sin * p2y; y2 = sin * p2x + cos * p2y; x3 = cos * p3x - sin * p3y; y3 = sin * p3x + cos * p3y; x4 = x1 + ( x3 - x2 ); y4 = y3 - ( y2 - y1 ); } else { x1 = p1x; y1 = p1y; x2 = p2x; y2 = p2y; x3 = p3x; y3 = p3y; x4 = p4x; y4 = p4y; } x1 += worldOriginX; y1 += worldOriginY; x2 += worldOriginX; y2 += worldOriginY; x3 += worldOriginX; y3 += worldOriginY; x4 += worldOriginX; y4 += worldOriginY; float invTexWidth = 1.0f / texture->getWidth(); float invTexHeight = 1.0f / texture->getHeight(); float u = srcX * invTexWidth; float v = ( srcY + srcHeight ) * invTexHeight; float u2 = ( srcX + srcWidth ) * invTexWidth; float v2 = srcY * invTexHeight; if ( flipX ) { float tmp = u; u = u2; u2 = tmp; } if ( flipY ) { float tmp = v; v = v2; v2 = tmp; } tempVertices[0] = x1; tempVertices[1] = y1; tempVertices[2] = color; tempVertices[3] = u; tempVertices[4] = v; tempVertices[5] = x2; tempVertices[6] = y2; tempVertices[7] = color; tempVertices[8] = u; tempVertices[9] = v2; tempVertices[10] = x3; tempVertices[11] = y3; tempVertices[12] = color; tempVertices[13] = u2; tempVertices[14] = v2; if ( mesh->getNumIndices() > 0 ) { tempVertices[15] = x4; tempVertices[16] = y4; tempVertices[17] = color; tempVertices[18] = u2; tempVertices[19] = v; add ( texture, tempVertices, 30, 0, 20 ); } else { tempVertices[15] = x3; tempVertices[16] = y3; tempVertices[17] = color; tempVertices[18] = u2; tempVertices[19] = v2; tempVertices[20] = x4; tempVertices[21] = y4; tempVertices[22] = color; tempVertices[23] = u2; tempVertices[24] = v; tempVertices[25] = x1; tempVertices[26] = y1; tempVertices[27] = color; tempVertices[28] = u; tempVertices[29] = v; add ( texture, tempVertices, 30, 0, 30 ); } }
Sprite::Sprite(const Texture::ptr& texture) : color(1,1,1,1) { initialize(texture, 0, 0, texture->getWidth(), texture->getHeight()); }