# Setting a renderer's target

**URL:** <https://discourse.libsdl.org/t/setting-a-renderers-target/20122>\
**Category:** SDL Development\
**Created:** [November 5, 2013, 1:57am UTC](https://discourse.libsdl.org/t/setting-a-renderers-target/20122 "2013-11-05T01:57:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zalerinian](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/zalerinian/32/2535_2.png) [@Zalerinian](https://discourse.libsdl.org/u/Zalerinian)\
**Post date:** [November 5, 2013, 1:57am UTC](https://discourse.libsdl.org/t/setting-a-renderers-target/20122/1 "2013-11-05T01:57:07Z")

</div>

I’ve been trying to change the rendering target of my renderer to a texture so that I can draw on it separately from the screen, but all my attempts to calling SDL\_SetRenderTarget have failed.

The first issue I was having is that if I call SDL\_GetError() before I even try to set the target, it says ‘Invalid Texture’. I’m not sure where this happens, or why it’s there because the texture should exist (I used cout and it displayed a memory address, I admit this is probably terrible checking, but I’m not sure what else to do).  
Calling SDL\_SetRenderTarger returns -1, and does not change the error message returned by SDL\_GetError (further reasoning to me believing the texture does exist.

Is there something I’m supposed to do before calling SDL\_SetRenderTarget?

Code:  
void Bitmap::draw\_rect(int x, int y, int w, int h, int r, int g, int b, int a){  
SDL\_SetRenderDrawColor(ren, r, g, b, a);  
cout \<\< SDL\_SetRenderTarget(ren, bitmap) \<\< endl;  
SDL\_Rect rect = {x, y, w, h};  
SDL\_RenderFillRect(ren, &rect);  
}

‘bitmap’ is defined as ‘SDL\_Texture \*bitmap’, and ‘ren’ is my renderer.  
Am I missing something I’m supposed to be doing? Is anyone else having issues with this? Is it me, or is it broken itself?

Thank you to anyone that can try to help solve this.

---

<div class="post-metadata">

**Author:** ![Mitch\_Crane](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/m/a5b964/32.png) [@Mitch\_Crane](https://discourse.libsdl.org/u/Mitch_Crane)\
**Post date:** [November 5, 2013, 3:10am UTC](https://discourse.libsdl.org/t/setting-a-renderers-target/20122/2 "2013-11-05T03:10:55Z")

</div>

Did you call SDL\_CreateTexture() to create the texture?

---

<div class="post-metadata">

**Author:** ![Zalerinian](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/zalerinian/32/2535_2.png) [@Zalerinian](https://discourse.libsdl.org/u/Zalerinian)\
**Post date:** [November 5, 2013, 12:29pm UTC](https://discourse.libsdl.org/t/setting-a-renderers-target/20122/3 "2013-11-05T12:29:41Z")

</div>

Yes, I have.

Code:  
int Bitmap::Create(int w, int h, int access = SDL\_TEXTUREACCESS\_STREAMING | SDL\_TEXTUREACCESS\_TARGET){  
SDL\_RendererInfo \*info = new SDL\_RendererInfo;  
SDL\_GetRendererInfo(ren, info);  
Uint32 format = 0;  
if (info-\>num\_texture\_formats \> 0){  
format = info-\>texture\_formats[0];  
}  
bitmap = SDL\_CreateTexture(ren, format, access, w, h);  
Bitmap::w = w; // These are just properties of the texture that I’m storing for refernce later.  
Bitmap::h = h;  
Bitmap::access = access;  
return 0;  
}

It does return a memory address for the texture, so I believe it works fine.

---

<div class="post-metadata">

**Author:** ![Mitch\_Crane](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/m/a5b964/32.png) [@Mitch\_Crane](https://discourse.libsdl.org/u/Mitch_Crane)\
**Post date:** [November 5, 2013, 1:41pm UTC](https://discourse.libsdl.org/t/setting-a-renderers-target/20122/4 "2013-11-05T13:41:29Z")

</div>

I don’t see the problem. I have a texture which I compost a few layers onto  
before drawing it in the window and I don’t do anything other than create  
the renderer, create the texture with the proper access, and set the  
target. I pass an immediate value for the format to SDL\_CreateTexture()  
(e.g. e.g. SDL\_CreateTexture(ren, SDL\_PIXELFORMAT\_RGBA8888, access, w, h)),  
but other than that I’m doing pretty much what you’re doing. Something  
going wrong upstream though if you’re getting the ‘Invalid Texture’ error,  
so I can only suggest you find out exactly when that happens.On Tue, Nov 5, 2013 at 7:29 AM, Zalerinian wrote:

> Yes, I have.
> 
> Code:
> 
> int Bitmap::Create(int w, int h, int access =  
> SDL\_TEXTUREACCESS\_STREAMING | SDL\_TEXTUREACCESS\_TARGET){  
> SDL\_RendererInfo \*info = new SDL\_RendererInfo;  
> SDL\_GetRendererInfo(ren, info);  
> Uint32 format = 0;  
> if (info-\>num\_texture\_formats \> 0){  
> format = info-\>texture\_formats[0];  
> }  
> bitmap = SDL\_CreateTexture(ren, format, access, w, h);  
> Bitmap::w = w; // These are just properties of the texture that I’m  
> storing for refernce later.  
> Bitmap::h = h;  
> Bitmap::access = access;  
> return 0;  
> }
> 
> It does return a memory address for the texture, so I believe it works  
> fine.
> 
> * * *
> 
> SDL mailing list  
> SDL at [lists.libsdl.org](http://lists.libsdl.org)  
> [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)

---

<div class="post-metadata">

**Author:** ![Mason\_Wheeler](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/m/b9bd4f/32.png) [@Mason\_Wheeler](https://discourse.libsdl.org/u/Mason_Wheeler)\
**Post date:** [November 5, 2013, 6:53pm UTC](https://discourse.libsdl.org/t/setting-a-renderers-target/20122/5 "2013-11-05T18:53:05Z")

</div>

A quick glance at the implementation code reveals the problem.? The  
"access" parameter is _not_ a bitmask.? You only want to pass  
SDL\_TEXTUREACCESS\_TARGET here.

MasonOn Tuesday, November 5, 2013 4:29 AM, Zalerinian wrote:

Yes, I have.

Code:

int Bitmap::Create(int w, int h, int access = SDL\_TEXTUREACCESS\_STREAMING | SDL\_TEXTUREACCESS\_TARGET){  
? SDL\_RendererInfo \*info = new SDL\_RendererInfo;  
? SDL\_GetRendererInfo(ren, info);  
? Uint32 format = 0;  
? if (info-\>num\_texture\_formats \> 0){  
? ? format = info-\>texture\_formats[0];  
? }  
? bitmap = SDL\_CreateTexture(ren, format, access, w, h);  
? Bitmap::w = w; // These are just properties of the texture that I’m storing for refernce later.  
? Bitmap::h = h;  
? Bitmap::access = access;  
? return 0;  
}

It does return a memory address for the texture, so I believe it works fine.

* * *

SDL mailing list  
SDL at [lists.libsdl.org](http://lists.libsdl.org)  
[http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)

---

<div class="post-metadata">

**Author:** ![Zalerinian](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/zalerinian/32/2535_2.png) [@Zalerinian](https://discourse.libsdl.org/u/Zalerinian)\
**Post date:** [November 5, 2013, 9:44pm UTC](https://discourse.libsdl.org/t/setting-a-renderers-target/20122/6 "2013-11-05T21:44:20Z")

</div>

@Mason I tried your suggestion, but it still has the same error.

@opcow: I’ve found where the problem occurs, but I’m not exactly sure why it does.

Window.cpp:

Code:  
int Window::CreateWindow(int x, int y, int w, int h){  
cout \<\< "Pre Sprite: " \<\< SDL\_GetError() \<\< endl;  
window = Sprite(x, y, w, h, 2);  
cout \<\< "Post Sprite: " \<\< SDL\_GetError() \<\< endl;  
return 0;  
}

Sprite.cpp:

Code:  
Sprite::Sprite(int x, int y, int w, int h, int z){  
cout \<\< "Pre Bitmap: " \<\< SDL\_GetError() \<\< endl;  
bitmap = Bitmap(w, h);  
rect = {x, y, w, h};  
Sprite::z = z;  
cout \<\< "Pre push: " \<\< SDL\_GetError() \<\< endl;  
sprites.push\_back(\*this);  
cout \<\< "Post Push: " \<\< SDL\_GetError() \<\< endl;  
}

‘rect’ is an SDL\_Rect, and ‘bitmap’ is a Bitmap object of my own (see the ‘create’ method (called by the constructor to actually make the class) above).

Console output:

Code:  
Pre Sprite:  
Pre Bitmap:  
Pre push:  
Post Push:  
Post Sprite: Invalid texture  
Post Window: Invalid texture  
-1 // The return value of the SDL\_SetRenderTarget call

Why would it magically get an error between pushing the sprite to a list (for updating later), and then exiting the constructor. Could it be an issue with the window = Sprite() line?

I’m going to try messing around a bit with pointers to see if anything gets fixed.

---

<div class="post-metadata">

**Author:** ![Gabriele\_Greco](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/g/8edcca/32.png) [@Gabriele\_Greco](https://discourse.libsdl.org/u/Gabriele_Greco)\
**Post date:** [November 6, 2013, 2:45pm UTC](https://discourse.libsdl.org/t/setting-a-renderers-target/20122/7 "2013-11-06T14:45:57Z")

</div>

> ```
> sprites.push_back(*this);
> 
> ```

You are copying complex objects and creating temporary Sprite/Bitmap  
objects as intermediate step doing something like this.

I suggest you to use vector of pointers or smart pointers or (harder) to  
build custom copy operators and a default constructor, otherwise it’s  
almost sure the default copy operation of Sprite and Bitmap will break  
somehow the objects contained inside the class (probably SDL\_Texture \*).–  
Bye,  
Gabry

---

<div class="post-metadata">

**Author:** ![Zalerinian](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/zalerinian/32/2535_2.png) [@Zalerinian](https://discourse.libsdl.org/u/Zalerinian)\
**Post date:** [November 6, 2013, 9:05pm UTC](https://discourse.libsdl.org/t/setting-a-renderers-target/20122/8 "2013-11-06T21:05:45Z")

</div>

I had forgotten to mention that ‘sprites’ is a list, not an array. Would it still be better to use a vector for holding objects of the Sprite class (I think it also may be better to store them as pointers, rather than copying the entire memory space, for increased performance), or is a list fine for this?
