Thread: SDL movement

  1. #1
    Registered User zergdeath1's Avatar
    Join Date
    Sep 2003
    Posts
    39

    SDL movement

    Now I am trying to get the image to just move off center a little bit by using differant x and y values. But when I run this program the image move off screen in increments. Ironically this is the effect I wanted but, I didn't think I was ready so i was just trying to move image a little bit off center not have it keep moving. So here is my code the problem is that the images keeps moving rather than just blitting onto the screen a bit off center. Not any where in the code do I modify xpos or ypos, that I can tell, so I Dont see why when ever I redraw the screen the image moves.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <iostream>
    #include "SDL.h"
    using namespace std;
    
    SDL_Surface *scrn1, *back;
    int misc;
    int xpos=2;
    int ypos=1;
    
    
    int InitSDL();
    int Setscreenvidmode();
    int Blitscreen(SDL_Surface *scrn, SDL_Surface *scrnn);
    int SwitchLoadImg(SDL_Surface *scrn,int imgnum);
    
    
    
    int main(int argc, char*argv[]){
        
        int imgnum=1,done=0;
        
        InitSDL();
        atexit(SDL_Quit);
        Setscreenvidmode();
        SwitchLoadImg(scrn1,imgnum);
    while(done == 0)
    {
        Blitscreen(scrn1,back);
        
        if (imgnum>4){imgnum=1;}
        
        SDL_Event event;
    
        while ( SDL_PollEvent(&event) )
        {
          if ( event.type == SDL_QUIT )  {  done = 1;  }
    
          if ( event.type == SDL_KEYDOWN )
          {
            if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
            if ( event.key.keysym.sym == SDLK_n) {SwitchLoadImg(scrn1,++imgnum);}
          }
        }
        
      
    
    
    }}//////////////////////////////////////////END OF MAIN//////////////////////
    
    int InitSDL() {
        if ((SDL_Init(SDL_INIT_VIDEO||SDL_INIT_AUDIO))<0){
            cout<<SDL_GetError();
        }
    };
    
    int Setscreenvidmode(){
        scrn1 = SDL_SetVideoMode(500,380,32,SDL_SWSURFACE||SDL_DOUBLEBUF);
        back = SDL_SetVideoMode(500,380,32,SDL_SWSURFACE||SDL_DOUBLEBUF);    
    };
    
    int Blitscreen(SDL_Surface *scrnn, SDL_Surface *scrn){
        SDL_Rect trect;
        trect.x=xpos;
        trect.y=ypos;
        SDL_BlitSurface(scrnn,NULL,scrn,&trect);
        SDL_Flip(scrn);
    };    
        
    int SwitchLoadImg(SDL_Surface *scrn,int imgnum){
        SDL_Surface *temp;
        switch (imgnum){
            case 1:
                temp = SDL_LoadBMP("Pic1.bmp");
                break;
            case 2:
                temp = SDL_LoadBMP("Pic2.bmp");
                break;
            case 3:
                temp = SDL_LoadBMP("Pic3.bmp");
                break;
            case 4:
                temp = SDL_LoadBMP("Pic4.bmp");
                break;
            default:
                temp = SDL_LoadBMP("Pic1.bmp");
                break;
        }
        SDL_BlitSurface(temp,NULL,scrn,NULL);
    };
    Stolen Quote: Buttered Toast always lands butter side down and cats always land on their feet, what happens when you strap buttered Toast to the back of a cat?
    My Quote: Practice Makes Perfect Nobodys Perfect Why Practice?

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Im confused what exactally are you trying to do if I am reading your code correctly your just swapping different images at the same point.
    Woop?

  3. #3
    Registered User zergdeath1's Avatar
    Join Date
    Sep 2003
    Posts
    39
    I am trying to get the. The image I have to be placed a little off center when the program starts instead it is continually moving to the upper left. Leave whatever color trail it is behind it. The original code I had just displayed and image then when u hit the n key it switched to the next image. Then I decided to play around with the SDL_Rect structures and this started to happen. I am pretty sure the problem is around here:

    Code:
    int Blitscreen(SDL_Surface *scrnn, SDL_Surface *scrn){
        SDL_Rect trect;
        trect.x=xpos;
        trect.y=ypos;
        SDL_BlitSurface(scrnn,NULL,scrn,&trect);
        SDL_Flip(scrn);
    };
    The thing is that, that piece of code should just disp the image of center as long as the xpos and ypos do not change, right? So somewhere else they must be changing inadvertantly.
    Stolen Quote: Buttered Toast always lands butter side down and cats always land on their feet, what happens when you strap buttered Toast to the back of a cat?
    My Quote: Practice Makes Perfect Nobodys Perfect Why Practice?

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    I get a seg fault when I try to run your code im not exactly sure why you have two screen modes with the back and the scrn1 but you dont need to setting 2 different video modes is not a good idea. I re-wrote your code to do what you wanted.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <iostream>
    #include "SDL/SDL.h"
    using namespace std;
    
    SDL_Surface *scrn1, *back;
    int misc;
    int xpos=2;
    int ypos=1;
    
    
    int InitSDL();
    int Setscreenvidmode();
    int Blitscreen(SDL_Surface *scrn, SDL_Surface *scrnn);
    int SwitchLoadImg(SDL_Surface *scrn,int imgnum);
    
    
    
    int main(int argc, char*argv[]){
        
        int imgnum=1,done=0;
        
        InitSDL();
        atexit(SDL_Quit);
        Setscreenvidmode();
        SwitchLoadImg(scrn1,imgnum);
    while(done == 0)
    {
        //BAD idea you were blitting two different video modes to one another
        //Blitscreen(scrn1,back);
        
        
        f (imgnum>4){imgnum=1;}
        SDL_Event event;
    
        while ( SDL_PollEvent(&event) )
        {
          i
          if ( event.type == SDL_QUIT )  {  done = 1;  }
    
          if ( event.type == SDL_KEYDOWN )
          {
            if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
            if ( event.key.keysym.sym == SDLK_n) {SwitchLoadImg(scrn1,++imgnum);}
          }
          
        }
        
      
    
    
    }
    }//////////////////////////////////////////END OF MAIN//////////////////////
    
    int InitSDL() {
        if ((SDL_Init(SDL_INIT_VIDEO||SDL_INIT_AUDIO))<0){
            cout<<SDL_GetError();
        }
    };
    
    int Setscreenvidmode(){
        scrn1 = SDL_SetVideoMode(500,380,32,SDL_SWSURFACE||SDL_DOUBLEBUF);
        //you didn't need this you were setting 2 different video modes
        //back = SDL_SetVideoMode(500,380,32,SDL_SWSURFACE||SDL_DOUBLEBUF);    
    };
    
    int Blitscreen(SDL_Surface *scrnn, SDL_Surface *scrn){
        SDL_Rect trect;
        trect.x=xpos;
        trect.y=ypos;
        SDL_BlitSurface(scrnn,NULL,scrn,&trect);
        SDL_Flip(scrn);
    };    
        
    int SwitchLoadImg(SDL_Surface *scrn,int imgnum){
        SDL_Surface *temp;
        switch (imgnum){
            case 1:
                temp = SDL_LoadBMP("Pic1.bmp");
                break;
            case 2:
                temp = SDL_LoadBMP("Pic2.bmp");
                break;
            case 3:
                temp = SDL_LoadBMP("Pic3.bmp");
                break;
            case 4:
                temp = SDL_LoadBMP("Pic4.bmp");
                break;
            default:
                temp = SDL_LoadBMP("Pic1.bmp");
                break;
        }
        Blitscreen(temp,scrn);//changed this so that your blitting your temp to the screen
    };
    Last edited by prog-bman; 11-20-2004 at 04:51 PM.
    Woop?

  5. #5
    Registered User zergdeath1's Avatar
    Join Date
    Sep 2003
    Posts
    39
    Wow thanks that fixxed it. Int the other one the image was moving approx 1 or 2 pixels at a time toward the upper left. Do You know why that fixed it?

    I want to make a pong game (but dont think I am ready yet), so could someone explain why I dont want 2 SetVideoMode's? I was under the impression that that was what sets the width and heigth of each screen. So lets say I have a 5 by 5 pixel ball bmp. How would I set a screen just around that ball so I can disp it im the main game/paddle are?
    Stolen Quote: Buttered Toast always lands butter side down and cats always land on their feet, what happens when you strap buttered Toast to the back of a cat?
    My Quote: Practice Makes Perfect Nobodys Perfect Why Practice?

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    The way you want to do thing is just blit the bitmap to the screen at its specific location. so you just create an x,y coord I personally like to do this with a struct if iI don't need member functions.
    Code:
    struct ball
    {
      int x;
      int y;
    };
    Then you just create an instance of the ball lets say pongBall.
    Code:
    int main()
    {
      SDL_Surface *screen= SDL_SetVideoMode(500,380,32,SDL_SWSURFACE||SDL_DOUBLEBUF)
      SDl_Surface *ball = SDL_LoadBitmap("ball.bmp");
      ball pongBall;
      pongBall.x = 200;
      pongBall.y = 200;
      SDL_Rect trect;
      trect.x = pongBall.x;
      trext.y = pongBall.y;
      SDL_BlitSurface(screen,NULL,ball,&trect);
      SDL_Flip(screen);
      return 0;
    }
    Woop?

  7. #7
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    As for the two screen modes well Im not absoluty sure if its bad in the sense that it is going to crash etc. I just know you really shouldn't need two different screen modes for a pong game. What was bad with your code is that you were blitting a screen mode to a screen mode so Im pretty sure thats bad your program just opened and closed on my comp and the was SDL deploy parachute in stderr.txt.
    Woop?

  8. #8
    Registered User zergdeath1's Avatar
    Join Date
    Sep 2003
    Posts
    39
    Wait in that last bit of Pong Code aren't you blitting, screen onto the ball. Dont you want to be blitting ball onto screen?
    Stolen Quote: Buttered Toast always lands butter side down and cats always land on their feet, what happens when you strap buttered Toast to the back of a cat?
    My Quote: Practice Makes Perfect Nobodys Perfect Why Practice?

  9. #9
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Ya im sorry i was typing all the code in the quick reply
    Woop?

  10. #10
    Registered User zergdeath1's Avatar
    Join Date
    Sep 2003
    Posts
    39
    In dont care, I just wanted to make sure I understood what you said. Thx for ur help by the way.
    Stolen Quote: Buttered Toast always lands butter side down and cats always land on their feet, what happens when you strap buttered Toast to the back of a cat?
    My Quote: Practice Makes Perfect Nobodys Perfect Why Practice?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SDL + Newbie
    By Livijn in forum Game Programming
    Replies: 7
    Last Post: 04-30-2007, 11:20 PM
  2. Problems compiling this SDL app
    By Rider in forum C++ Programming
    Replies: 3
    Last Post: 03-27-2007, 12:22 PM
  3. SDL and MinGW Studio
    By Vicious in forum Tech Board
    Replies: 0
    Last Post: 07-30-2004, 09:59 PM
  4. SDL and Windows
    By nickname_changed in forum Windows Programming
    Replies: 14
    Last Post: 10-24-2003, 12:19 AM
  5. sdl in c++
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-07-2002, 07:46 AM