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
|