Sunday, December 4, 2022
HomeGame Developmentc++ - When calling SDL_CreateTextureFromSurface utilizing SDL2 it units all different textures...

c++ – When calling SDL_CreateTextureFromSurface utilizing SDL2 it units all different textures to be white containers


I’ve a sport loop which reveals 6 heroes, and 1 is assigned to the consumer; if the consumer clicks the left mouse button, I need to spawn a bullet.

The entire heroes present high-quality and render works when the sport loop begins, nonetheless, once I press the left mouse button, all the textures at the moment loaded flip into white squares.

whereas (isRunning())
{
    readInput();
    replace();
    render();

}

Within replace I name an entity replace methodology on Hero:

void Hero::replace()
{
    if (isPlayer)
    {
        transfer();

        if (input->getLeftMouse())
        {
            hearth();
        }
    }
}

Which in flip calls hearth() if left the mouse button I pressed.

void Hero::hearth()
{
    std::cout << "Firing bullet..." << std::endl;

    if (bullets.measurement() > 1)
    {
        return;
    }

    Bullet *bullet = new Bullet(messageBus, enter); // Consructor known as and texture loaded from right here
    bullet->setX(getX());
    bullet->setY(getY());
    bullet->setSrc(getX(), getY());
    bullet->setDst(input->getMouseX(), input->getMouseY());

    addChild(bullet);
}

The bullet constructor hundreds the feel

Bullet::Bullet(MessageBus *messageBus, Enter *enter) : Node2d(messageBus, enter)
{
    setW(2);
    setH(2);
    velocity = 10;
    loadTexture("../property/bullet.png");

    std::cout << "Bullet created." << std::endl;
}

loadTexture to load the feel utilizing that renderer, I’m utilizing a singleton to deal with the shop the renderer pointer as I did not need to need to move the renderer to each class constructor.

void Node2d::loadTexture(std::string path)
{
    RenderManager *renderManager = RenderManager::create();
    // Load PNG texture
    texture = renderManager->loadTexture(path);
}

SDL_Texture *RenderManager::loadTexture(std::string path)
{
    if (renderer == NULL)
    {
        std::cout << "RenderManager::loadTexture renderer is null" << std::endl;
        return NULL;
    }
    // The ultimate texture
    SDL_Texture *newTexture;

    // Load picture at specified path
    SDL_Surface *loadedSurface = IMG_Load(path.c_str());
    if (loadedSurface == NULL)
    {
        printf("Unable to load picture %s! SDL_image Error: %sn", path.c_str(), IMG_GetError());
    }
    else
    {
        // Create texture from floor pixels
        newTexture = SDL_CreateTextureFromSurface(renderer, loadedSurface);

        if (newTexture == NULL)
        {
            printf("Unable to create texture from %s! SDL Error: %sn", path.c_str(), SDL_GetError());
        }

        // Do away with previous loaded floor
        SDL_FreeSurface(loadedSurface);
    }

    return newTexture;
}

I’ve remoted the difficulty to SDL_CreateTextureFromSurface, if I name that operate and set newTexture = null, the difficulty nonetheless occurs, if I do not name SDL_CreateTextureFromSurface if the trail == “../property/bullet.png the heroes present.

if (path == "../property/bullet.png")
{
    SDL_CreateTextureFromSurface(renderer, loadedSurface);
    newTexture = NULL;
}
else
{
    newTexture = SDL_CreateTextureFromSurface(renderer, loadedSurface);
}

My render methodology calls each renderable courses render methodology which seems like:

void Node2d::render()
{
    if (texture == NULL)
    {
        std::cout << "Node2d texture is null" << std::endl;
    }
    // If I place loadTexture right here it additionally turns all textures white.
    RenderManager *renderManager = RenderManager::create();
    renderManager->renderTexture(texture, x, y, w, h);
}
void RenderManager::renderTexture(SDL_Texture *texture, int x, int y, int width, int top)
{
    if (renderer == NULL)
    {
        std::cout << "RenderManager::renderTexture renderer is null" << std::endl;
        return;
    }
    SDL_Rect dstrect = {x, y, width, top};
    SDL_RenderCopy(renderer, texture, NULL, &dstrect);
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments