SDL rendering texture to another texture

I am trying to convert a graphical user interface that I developed using SDL1.2 to SDL2.0. My GUI uses a bunch of png images loaded from file which are rotated, re-sized and then blitted to various sub-surfaces. These sub-surfaces are then blitted to the main background screen before doing a final SDL_Flip().

For my SDL2.0 conversion I have tried to move away from Surfaces and use Textures wherever possible. Since my GUI is built up in layers, I would like to be able to render one texture onto another before rendering a final set of built up textures to the window. I am having trouble doing this. My approach has been to create a background texture with SDL_CreateTexture setting its access to SDL_TEXTUREACCESS_TARGET. I load a bunch of images as surfaces and convert them to textures using SDL_CreateTextureFromSurface, these are the textures I want to use to build up sub-texture layers. I set the background texture to be the rendering target using SDL_SetRenderTarget, then render my other textures onto it. After all that I set the render target back to be the window and render the background texture (which should now have all the other textures rendered on to it) to the window. And it doesn’t work…

I just get a blank scree. I am aware that the textures created using CreateTextureFromSurface have access SDL_TEXTUREACCESS_STATIC, but I am not using these as the target. Can anyone help?

Thanks

Here's the code...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//Example trying to render texture to another texture
#include <string>
#include <iostream>
#include <iomanip>
#include <windows.h>
#include <time.h>
#include <sstream>
#include <fstream>
#include <cmath>
#include <stdio.h>

#include "SDL.h"
#include "SDL_image.h"

using namespace std;

//Screen attributes
const int SCREEN_WIDTH = 1250;
const int SCREEN_HEIGHT = 700;

//*****************************************************************************
//------------------------------- START OF MAIN -------------------------------
//*****************************************************************************

int main( int argc, char* args[] )
{
    
	//SDL2.0 Initialisation
	SDL_Init( SDL_INIT_VIDEO ); //Initialize SDL
	IMG_Init(IMG_INIT_PNG | IMG_INIT_JPG | IMG_INIT_TIF); //Initialise SDL_Image to load .png, .jpeg and .tiff files, must go after window has been created
	
	//Declare window and renderer
	SDL_Window* pWindow = NULL;
	SDL_Renderer* pRenderer = NULL;
	pWindow = SDL_CreateWindow( "TDPB HMI", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
	pRenderer = SDL_CreateRenderer(pWindow, -1, SDL_RENDERER_TARGETTEXTURE);
	SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");  // make the scaled rendering look smoother.
	SDL_RenderSetLogicalSize(pRenderer, SCREEN_WIDTH, SCREEN_HEIGHT);

    //Other SDL parameters
    bool quit = false;
    SDL_Event event;

	//Load surfaces
	SDL_Surface *pScreen = SDL_GetWindowSurface(pWindow);
	SDL_Surface *pTripod = IMG_Load("images/tripod.png");
	SDL_Surface *pConsoleBackground = IMG_Load("images/consoleBackground1.png");
	
	//Create textures
	SDL_Texture *pTripodTexture = SDL_CreateTextureFromSurface(pRenderer, pTripod);
	SDL_Texture *pConsoleBackgroundTexture = SDL_CreateTextureFromSurface(pRenderer, pConsoleBackground);
	SDL_Texture *pBackgroundTexture = SDL_CreateTexture(pRenderer, SDL_PIXELFORMAT_RGBX8888, SDL_TEXTUREACCESS_TARGET, 1250, 700);

	//Get the size of pVesselTexture
	SDL_Rect l_sourceRect;
	l_sourceRect.x = 0;
	l_sourceRect.y = 0;
	SDL_QueryTexture(pTripodTexture, NULL, NULL, &l_sourceRect.w, &l_sourceRect.h);

	//Configure Renderer
	SDL_SetRenderDrawColor(pRenderer, 0, 0, 0, 255);
	SDL_RenderClear(pRenderer);

	//Render pConsoleBackgroundTexture then pTripodTexture to pBackgroundTexture, then renders pBackgroundTexture to renderer
	SDL_SetRenderTarget(pRenderer, pBackgroundTexture);
	SDL_RenderCopy(pRenderer, pConsoleBackgroundTexture, NULL, NULL );
	SDL_RenderCopy(pRenderer, pTripodTexture, &l_sourceRect, &l_sourceRect );
	SDL_SetRenderTarget(pRenderer, NULL);

	//Render present
	SDL_RenderPresent(pRenderer);
	SDL_Delay(2000);

    //Free surfaces
    SDL_FreeSurface(pScreen);
	SDL_FreeSurface(pTripod);
	SDL_FreeSurface(pConsoleBackground);
	pScreen = NULL;
	pTripod = NULL;
	pConsoleBackground = NULL;
    
	//Destroy textures
	SDL_DestroyTexture(pTripodTexture);
	SDL_DestroyTexture(pConsoleBackgroundTexture);
	SDL_DestroyTexture(pBackgroundTexture);
	pTripodTexture = NULL;
	pConsoleBackgroundTexture = NULL;
	pBackgroundTexture = NULL;

	//Destroy Renderer and Window
	SDL_DestroyRenderer(pRenderer);
	SDL_DestroyWindow(pWindow);
	pRenderer = NULL;
	pWindow = NULL;
	
	//Quit SDL
    IMG_Quit();
	SDL_Quit();
    
    return 0;
} //END MAIN 
Topic archived. No new replies allowed.