Jump to content
  • Advertisement
Sign in to follow this  
Toadhead

moving a image

This topic is 4944 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

when I compile this I'll see an image (image.bmp in the same dir as the program), on 0*0. Why doesn't the image move when I press on the arrow keys???
#include <iostream> //header file
#include <cstdio> // header file
#include <cstdlib> //header file
#include "SDL.h" // custom header file
using namespace std; // using the standard namespace

void DisplayImage(SDL_Surface *image, SDL_Surface* screen, int xpos, int ypos);




int main(int argc, char** argv) { // start execution here
    
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) // initialize SDL VIDEO, and check if it is initialized
{
    cout << "Error: " << SDL_GetError();
    return 1;
}
atexit(SDL_Quit);  // frees up everything when program closes  
    

    SDL_Surface* screen; // The screen
    screen = SDL_SetVideoMode(800,600,16,SDL_HWSURFACE|SDL_DOUBLEBUF); //Setting screen mode
    
    if ( screen == NULL ) // check to see if the screen has been set
    {
        cout << "Failed to set the screen resolution to 800*600";
        return 1;
    }    
    
SDL_Surface* image;
image = SDL_LoadBMP("image.bmp");

int xpos = 0;
int ypos = 0;

   
    
    
    
    
    
    
    int gameloop = 0; //declarates the gameloop 
    SDL_Event event; //declarates the event
    
    while (gameloop == 0) //gameloop
    { 
        
        
        
        while ( SDL_PollEvent(&event) ) //Polling for events
        {
            if (event.type == SDL_QUIT) {gameloop = 1;}
            if (event.type == SDL_KEYDOWN)
            {
                if (event.key.keysym.sym == SDLK_q) {gameloop = 1;}
                if (event.key.keysym.sym == SDLK_LEFT) {xpos -= 1; }
                if (event.key.keysym.sym == SDLK_RIGHT) {xpos += 1; }
                if (event.key.keysym.sym == SDLK_DOWN) {ypos -= 1; }
                if (event.key.keysym.sym == SDLK_UP) {ypos += 1; }
            }
            

               
        }
        
    
        DisplayImage(image, screen,0,0);
            SDL_Flip(screen);
        

    }    
    
    
    
    
    
    
    

    return 0;
}




void DisplayImage(SDL_Surface* image, SDL_Surface* screen, int xpos, int ypos)
{
    SDL_Rect destination;
    destination.x = xpos ;
    destination.y = xpos ;
    SDL_BlitSurface(image,NULL,screen,&destination);
    SDL_Flip(screen);
   
} 

Share this post


Link to post
Share on other sites
Advertisement
DisplayImage(image, screen,0,0);

should be

DisplayImage(image, screen,xpos,ypos);




but now I cant use left and right, only up and down that will move the screen to up/left and down/right :(

Share this post


Link to post
Share on other sites
you have a whoopsie [smile]

void DisplayImage(SDL_Surface* image, SDL_Surface* screen, int xpos, int ypos)
{
SDL_Rect destination;
destination.x = xpos ;
destination.y = xpos ; <--- DOH!! :)
SDL_BlitSurface(image,NULL,screen,&destination);
SDL_Flip(screen);
}

Share this post


Link to post
Share on other sites
Damn, my picture moves, but after it is moved the pixels won't turn black :(
They will keep the same color as the picture, just try compiling and running the program and you'll see.

Can anyone tell me why the pixels dont become black again??>

Thanks,
Rob

Share this post


Link to post
Share on other sites
Don't know what FillRect is..
I'll learn it in the future when my tutorials reach this point.

But I was just wondering if I did something wrong, I tought that the pixels would turn black automaticly after the image was moved, but I have just drawed the image on another part of the screen and my older image wasn't removed.

Share this post


Link to post
Share on other sites
SDL_FillRect
Quote:

int SDL_FillRect(SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color);
This function performs a fast fill of the given rectangle with color. If dstrect is NULL, the whole surface will be filled with color.


To turn the screen black do SDL_FillRect(my_screen, NULL, 0);
Do this before drawing your screen.

Share this post


Link to post
Share on other sites
Ah..
Thanks, thats realy usefull :P


But...
The last color, what number is what color??

And when the 2e argument is NULL the whole screen will be filled, but what can I pass other than NULL and what will happen than?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!