Advertisement

SDL 2.0 Texture Rendering Help

Started by December 29, 2014 12:55 PM
3 comments, last by nobodynews 10 years ago

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 sad.png 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;
}

Well for starters you do this in main before you initialize SDL:


  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;

And then you never load the bgTexture texture again so it never points to a valid texture for SDL to use. How is SDL to know that you want bgTexture to contain the background texture when you immediately free'd it after loading it? You should add this line:


bgTexture=loadTexture(igrass.png",renderTarget);

Next to the similar line for currentImage so both textures are loaded in memory.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

Advertisement

Well for starters you do this in main before you initialize SDL:


  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;

And then you never load the bgTexture texture again so it never points to a valid texture for SDL to use. How is SDL to know that you want bgTexture to contain the background texture when you immediately free'd it after loading it? You should add this line:


bgTexture=loadTexture(igrass.png",renderTarget);

Next to the similar line for currentImage so both textures are loaded in memory.


sorry brother. I did not get what you said :( can you be more specific?? :( I am new in SDL so don't know the real mechanism till now

I think I got what you are trying to say unsure.png but what if I don't want to load the image with the function rather I want it to load manually? then? isn't my way of loading it manually correct? correct me if I am wrong mellow.png

What you had was correct in that you were calling the right functions in the right (relative) order. It was incorrect because to use SDL_CreateTextureFromSurface you need a valid renderer. To have a valid renderer you need to call SDL_CreateRenderer. Before you can call SDL_CreateRenderer you need to call SDL_CreateWindow. And before you call any SDL function you need to call SDL_Init. You were trying to create the background texture before you did any of that.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

This topic is closed to new replies.

Advertisement