#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);
}
moving a image
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???
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 :(
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 :(
You're twice fliping the backbuffer to screen - that flip in DisplayImage isn't needed. Hope it helps.
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); }
ow thanks lol :P
I already reated you extremely helpfull some time ago so no better rating for you Gaiiden :P
I already reated you extremely helpfull some time ago so no better rating for you Gaiiden :P
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
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
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.
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.
SDL_FillRect
To turn the screen black do SDL_FillRect(my_screen, NULL, 0);
Do this before drawing your screen.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement