Currently I am essentially recreating the original Mario for an assignment. I haave it setup so that the game uses a ScreenManager and currently by default loads Level1 when run. Level1 then loads up the background and the character, both using a Texture2D class I made earlier. If one of these is loaded and the other is commented out then they render to the screen fine and look normal, however if I render both of them to the screen it begins to flicker continuously.

Level1 Render() Function:

void GameScreenLevel1::Render()
{
    mBackgroundTexture->Render(Vector2D(), SDL_FLIP_NONE);
    myCharacter->Render();
}

Texture2D Class:

#include <iostream>
#include <SDL_image.h>
#include "Texture2D.h"
using namespace::std;

Texture2D::Texture2D(SDL_Renderer* renderer)
{
    mRenderer = renderer;
    SDL_Texture* mTexture = NULL;
    SDL_Texture* vTexture = NULL;

    int             mWidth = 0;
    int             mHeight = 0;
}

Texture2D::~Texture2D()
{
    Free();
    mRenderer = NULL;
}

bool Texture2D::LoadFromFile(string path)
{
    Free();

    //Loadtheimage
    SDL_Surface* pSurface = IMG_Load(path.c_str());
    if (pSurface != NULL)
    {
        SDL_SetColorKey(pSurface, SDL_TRUE, SDL_MapRGB(pSurface->format, 0, 0xFF, 0xFF));
        mTexture = SDL_CreateTextureFromSurface(mRenderer, pSurface);

        if (mTexture == NULL)
        {
            cout << "Unable to create texture from surface. Error:" << SDL_GetError() << endl;
        }

        else
        {
            mWidth = pSurface->w;
            mHeight = pSurface->h;
        }
    }

    else
    {
        cout << "Unable to create texture from surface. Error: " << IMG_GetError() << endl;
    }

    //vTexture = mTexture;
    return mTexture != NULL;
}

void Texture2D::Render(Vector2D newPosition, SDL_RendererFlip flip, double angle)
{
    //clear the screen
    SDL_SetRenderDrawColor(mRenderer, 0x00, 0x00, 0x00, 0x00);
    SDL_RenderClear(mRenderer);

    //set where to render the texture
    SDL_Rect renderLocation = {newPosition.x, newPosition.y, mWidth, mHeight};

    //render to screen
    SDL_RenderCopyEx(mRenderer, mTexture, NULL, &renderLocation, 0, NULL, flip);
    SDL_RenderPresent(mRenderer);
}

void Texture2D::Free()
{
    if (mTexture != NULL)
    {
        SDL_DestroyTexture(mTexture);
        mTexture = NULL;
        SDL_DestroyTexture(vTexture);
        vTexture = NULL;
        mHeight = 0;
        mWidth = 0;
    }
}

It seems to me the code is getting caught up clearing the screen and rendering the textures over and over each frame causing the flickering and because of having multiple textures on screen it slows the process to the point its visible but im unsure how to correct the issue.

1 Answers

1
Person On

It seems that in your render function you are clearing the screen for every texture you’re loading, so the screen is cleared, then the first texture is loaded and rendered, but before the second texture is also rendered, the screen is cleared again, and instead of the second texture being overlayed on top of the first, it would flicker between the two textures