Is this the most effect simple way to display a moving image? SDL2

Posted by user36324 on Game Development See other posts from Game Development or by user36324
Published on 2013-10-23T02:26:00Z Indexed on 2013/10/23 4:11 UTC
Read the original article Hit count: 229

Filed under:
|
|
|

I've looked around for tutorials on SDL2, but there isnt many so I am curious i was messing around and is this an effective way to move an image. One problem is that it drags along the image to where it moves.

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

int main(int argc, char* argv[])
{
    bool exit = false;
    SDL_Init(SDL_INIT_EVERYTHING);

    SDL_Window *win = SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN);

    SDL_Renderer *ren = SDL_CreateRenderer(win, -1,
    SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);

    SDL_Surface *png = IMG_Load("character.png");
    SDL_Rect src;
    src.x = 0;
    src.y = 0;
    src.w = 161;
    src.h = 159;
    SDL_Rect dest;
    dest.x = 50;
    dest.y = 50;
    dest.w = 161;
    dest.h = 159;

    SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, png);
    SDL_FreeSurface(png);


    while(exit==false){
        dest.x++;
        SDL_RenderClear(ren);
        SDL_RenderCopy(ren, tex, &src, &dest);
        SDL_RenderPresent(ren);
    }

    SDL_Delay(5000);

    SDL_DestroyTexture(tex);
    SDL_DestroyRenderer(ren);
    SDL_DestroyWindow(win);
    SDL_Quit();

}

© Game Development or respective owner

Related posts about c++

Related posts about sdl