Trouble with simple moving

Hello, i have trouble with moveing an simple bmp surface:

Thats the code


#include <SDL2/SDL.h> 


#include <stdio.h>  
#include <stdlib.h> 

SDL_Surface* screen =NULL;

int main( int argc, char *argv[] )
{
  SDL_Event event; 
   
  // this starts the SDL2 IN C
  SDL_Init(SDL_INIT_EVERYTHING);
  
  SDL_Window *window = SDL_CreateWindow("move test", 0, 0, 1024, 768, SDL_WINDOW_SHOWN);
  
  SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);

//----------------

  
  
  SDL_Surface *ding = 
  SDL_LoadBMP("any_pic.bmp");
  
  
 SDL_Texture *texture_ding= 
 SDL_CreateTextureFromSurface(renderer, 
  ding);
  
 

//--------------

  int quit = 1;
  
  
  while(quit == 1)
 {
  SDL_WaitEvent(&event);
  
  while (SDL_PollEvent(&event))
 {
  switch (event.type)
  {
  case SDL_QUIT:
  quit = 0;
  break;	
  }
 }


 // draw sprite (pos.x, pos.y, image_size.x,..y)

 SDL_Rect d_pos= { 400, 385, 304,120};
  SDL_RenderCopy(renderer,texture_ding, NULL, &d_pos);



d_pos.x -= 100;  // here is the error


  
// ________

 SDL_RenderPresent(renderer);
 }
  
  
  SDL_DestroyRenderer(renderer);
  SDL_DestroyWindow(window);
  
 SDL_Quit();
 return 0;
}

All works but no moving whats wrong

You’re creating the destination rectangle and assigning its initial position every frame.

Create the rect and assign its initial position before the game loop.

Also, you don’t need to call SDL_WaitEvent() in this case. You want your game to keep running even if no events have occurred.

Lastly, moving downward 100 pixels every frame is going to have it exiting the visible area within a few frames.

2 Likes

Yep → the pollevent was it :grin:
I found it out 30mins atfer send this post

Thanks.