hello guys, I am learing SDl 2.0 now. I have made a window,renderer, and some textures where I have put my srpite sheet. animation is working fine but I wan to add another texture(background which will be) behind the sprite animation. have added the texture and also SDL_RenderCopy() it. but it's not showing. please help stuck with it.. below is my code.
#include<iostream>
#include<SDL.h>
#include<SDL_image.h>
#include<string>
using namespace std;
float frameTime=0;
int prevTime=0;
int currentTime=0;
float deltaTime=0;
float moveSpeed=300.0f;
SDL_Texture *loadTexture(string filepath,SDL_Renderer *renderTargetOp){
SDL_Texture *texture=NULL;
SDL_Surface *surface=IMG_Load(filepath.c_str());
if(surface==NULL)
cout<<"Error"<<endl;
else{
texture=SDL_CreateTextureFromSurface(renderTargetOp,surface);
}
SDL_FreeSurface(surface);
return texture;
}
int main(int argc, char* argv[]){
SDL_Window *window=NULL;
SDL_Renderer *renderTarget=NULL;
SDL_Surface *bgSurface=IMG_Load("grass.png");
SDL_Texture *bgTexture=SDL_CreateTextureFromSurface(renderTarget,bgSurface);
SDL_Rect bgRect;
bgRect={0,0,640,480};
SDL_FreeSurface(bgSurface);
bgSurface=NULL;
SDL_Texture *currentImage=NULL;
SDL_Rect playerRect;
SDL_Rect playerPos;
playerPos.x=300;
playerPos.y=200;
playerPos.w=30;
playerPos.h=30;
int frameWidth,frameHeight;
int textureWidth, textureHeight;
int imgFlags=IMG_INIT_PNG | IMG_INIT_JPG;
if(!(IMG_Init(imgFlags) &imgFlags))
cout<<"Error "<<IMG_GetError<<endl;
if(SDL_Init(SDL_INIT_EVERYTHING)<0){
cout<<"Int error: "<<SDL_GetError()<<endl;
} else{
window= SDL_CreateWindow("Bullshit Crap",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,640,480,SDL_WINDOW_SHOWN);
if (window==NULL){
cout<<"window creation error: "<<SDL_GetError()<<endl;
}
renderTarget=SDL_CreateRenderer(window,-1,SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
currentImage=loadTexture("image.png",renderTarget);
SDL_QueryTexture(currentImage,NULL,NULL,&textureWidth,&textureHeight);
frameWidth=textureWidth/3;
frameHeight=textureHeight/4;
playerRect.x=playerRect.y=0;
playerRect.w=frameWidth;
playerRect.h=frameHeight;
bool running = true;
SDL_Event ev;
while(running){
prevTime=currentTime;
currentTime=SDL_GetTicks();
deltaTime=(currentTime-prevTime)/1000.0f;
while(SDL_PollEvent(&ev) !=0){
if(ev.type==SDL_QUIT)
running=false;
else if(ev.type==SDL_KEYDOWN)
switch(ev.key.keysym.sym){
case SDLK_RIGHT:
playerPos.x +=deltaTime*moveSpeed;
break;
case SDLK_LEFT:
playerPos.x -=deltaTime*moveSpeed;
break;
case SDLK_UP:
playerPos.y -=deltaTime*moveSpeed;
break;
case SDLK_DOWN:
playerPos.y +=deltaTime*moveSpeed;
break;
}
}
frameTime+=deltaTime;
if(frameTime>=0.10f){
frameTime=0;
playerRect.x += frameWidth;
if(playerRect.x >=textureWidth)
playerRect.x=0;
}
SDL_RenderClear(renderTarget);
SDL_RenderCopy(renderTarget,bgTexture,NULL,&bgRect);
SDL_RenderCopy(renderTarget,currentImage,&playerRect,&playerPos);
SDL_RenderPresent(renderTarget);
}
}
SDL_DestroyWindow(window);
window=NULL;
SDL_DestroyTexture(currentImage);
SDL_DestroyTexture(bgTexture);
bgTexture=NULL;
SDL_DestroyRenderer(renderTarget);
currentImage=NULL;
renderTarget=NULL;
SDL_Quit();
return 0;
}