0

I am trying to learn SDL and I have an application where I want to draw to a textureout of the paint cycle of the application and then I only have to copy the texture to the screen during the refresh cycle. I have two methods to my class that do this:

Tester::CreateTexture(SDL_Renderer* pRenderer, XPoint<int>* pResolution)
{
    mResolution.x = pResolution->x;
    mResolution.y = pResolution->y;
    std::cout << "Renderer: " << pRenderer << std::endl;
    SDL_ClearError();
    mpTexture = SDL_CreateTexture(pRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, mResolution.x, mResolution.y);
    std::cout << "BeamformImage: " << mpTexture << " : " << mResolution.x << ", " << mResolution.y << std::endl;

    int foo = SDL_SetRenderTarget(pRenderer, mpTexture);
    std::cout << "Set Renderer: Cn: " << foo << " : " << SDL_GetError() << std::endl;

    int bar = SDL_SetRenderTarget(pRenderer, NULL);
    std::cout << "Set Renderer: Tn: " << bar << " : " << SDL_GetError() << std::endl;
}

This method creates the texture and saves it as an instance variable. I added code to set the target of the renderer to see if it works and there is no error and both calls return 0 as expected.

Then I have a method that wants to write to the texture:

void Tester::WriteTexture(SDL_Renderer* pRenderer)
{
    SDL_ClearError();
    std::cout << "Renderer: " << pRenderer << " : " << mpTexture << std::endl;
    int foo = SDL_SetRenderTarget(pRenderer, mpTexture);
    std::cout << "Render Target: " << foo << " : " << SDL_GetError() << std::endl;

    // Code to write to the renderer
}

The renderer is always the same in both methods and the texture is the same.

When Write Texture is called foo is -1 and the error is Invalid texture.

Is there a way to find out what the issue with the texture is? Is the problem that I have somehow trashed the renderer between the two calls?

A added this at the bottom of each of the methods to see if somehow the renderer changed and wouldn't allow rendering to textures

SDL_RendererInfo info;

SDL_GetRendererInfo(pRenderer, &info);

if (info.flags && SDL_RENDERER_TARGETTEXTURE > 0)
    std::cout << "*Can Target Texture: " << std::endl;
else
    std::cout << "*Can't Target Texture: " << std::endl;

In both places rendering to textures seems to be allowed.

Nefarious
  • 458
  • 6
  • 21
  • What is the type of mpTexture here? – Serkan Pekçetin Oct 10 '17 at 20:23
  • 1
    SDL_Texture* mpTexture; – Nefarious Oct 10 '17 at 20:31
  • Possible duplicate of [C++ SDL2 Error when trying to render SDL\_Texture: Invalid texture](https://stackoverflow.com/questions/25738096/c-sdl2-error-when-trying-to-render-sdl-texture-invalid-texture) – Serkan Pekçetin Oct 10 '17 at 20:31
  • The question posted as a possible solution to this question is seemingly different because that question uses an instance variable not as a pointer and the assignment operator is a copy into the instance variable. This question uses a pointer and a pointer to the created texture is used. No copy of the texture is required. I'm not saying what I am doing is correct, but it is different. – Nefarious Oct 10 '17 at 20:56
  • @Nefarious I don't think your problem is in the presented code. Can you still query texture parameters (or inspect it in debugger if you have SDL debugging symbols)? Is trere any SDL error before you clear last error? Can you reproduce it in minimal complete example? – keltar Oct 11 '17 at 04:20
  • Unfortunately I haven't figured out how to debug this, which may mean my life with this project is over. I develop on an x86 machine running Linux in Eclipse and use the Remote system explorer to deploy the solution to a Jetson TK1. I am looking into the texture parameters now. I am looking for some api documentation, SDL seems to be low on the documentation scale. – Nefarious Oct 11 '17 at 04:38

0 Answers0