• Advertisement
Sign in to follow this  

[SOLVED :) ] [SDL] I'm doing something wrong (image move)

This topic is 3826 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

Well, I'm trying to make an image move until it gets to the end of the window. The problem is, it moves only once, and stops there.. What am I doing wrong?
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>
#include <ctime>

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

SDL_Surface* background = NULL;
SDL_Surface* message = NULL;
SDL_Surface* screen = NULL;

SDL_Event event;

SDL_Surface* loadImage (std::string filename)
{
    //temporary storage for the loaded image
    SDL_Surface* loadedImage = NULL;
    
    //the optimized image that will be used
    SDL_Surface* optimizedImage = NULL;
    
    //load the image
    loadedImage = SDL_LoadBMP (filename.c_str() );
    
    optimizedImage = SDL_DisplayFormat (loadedImage);
    SDL_FreeSurface (loadedImage);
    
    return optimizedImage;
}

//aply the surface on the screen
void applySurface (int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
     SDL_Rect offset;
     offset.x = x;
     offset.y = y;
     
     //apply it
     SDL_BlitSurface (source, NULL, destination, &offset);
}

int main (int argc, char* args[] )
{
    if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
    {
        return 0;
    }
    
    screen = SDL_SetVideoMode (SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
    
    if (screen == NULL)
    {
        return 0;
    }
    
    SDL_WM_SetCaption ("Hello World", NULL);
    
    message = loadImage ("message.bmp");
    background = loadImage ("background.bmp");
    
    applySurface (0, 0, background, screen);
    
    int messageX = 0;
    applySurface (messageX, 0, message, screen);
    SDL_Flip (screen);
    
    int tick = 0;
    int lastTick =0;
    bool quit = false;
    
    while (tick < 1000 && messageX < SCREEN_WIDTH - 200 && quit == false)
    {
        if (clock() - lastTick > 1000)
		{
           tick ++;
	       lastTick = clock();
        
           messageX ++;
           applySurface (0, 0, background, screen);
           applySurface (messageX, 0, message, screen);       
           SDL_Flip (screen);
    
           while (quit == false)
           {
              while (SDL_PollEvent (&event) )
              {
                   if (event.type == SDL_QUIT)
                   quit = true;
              }
           }
        }
    }   
    
    SDL_FreeSurface (background);
    SDL_FreeSurface (message);

    return 0;
}


Thanks in advance. [Edited by - sheep19 on November 4, 2007 1:47:01 PM]

Share this post


Link to post
Share on other sites
Advertisement
What's with that inner "while (quit==false)" loop?
That's going to repeatedly loop until the user quits, and it's never going to come out to do the movement/blitting code again.


Cheers,
--Brian

Share this post


Link to post
Share on other sites
Quote:
Original post by Nairb
What's with that inner "while (quit==false)" loop?
That's going to repeatedly loop until the user quits, and it's never going to come out to do the movement/blitting code again.


Cheers,
--Brian


You are right, thank you. I have solved it, here it is:

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>
#include <ctime>

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

SDL_Surface* background = NULL;
SDL_Surface* message = NULL;
SDL_Surface* screen = NULL;

SDL_Event event;

SDL_Surface* loadImage (std::string filename)
{
//temporary storage for the loaded image
SDL_Surface* loadedImage = NULL;

//the optimized image that will be used
SDL_Surface* optimizedImage = NULL;

//load the image
loadedImage = SDL_LoadBMP (filename.c_str() );

optimizedImage = SDL_DisplayFormat (loadedImage);
SDL_FreeSurface (loadedImage);

return optimizedImage;
}

//aply the surface on the screen
void applySurface (int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
SDL_Rect offset;
offset.x = x;
offset.y = y;

//apply it
SDL_BlitSurface (source, NULL, destination, &offset);
}

int main (int argc, char* args[] )
{
if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
return 0;
}

screen = SDL_SetVideoMode (SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);

if (screen == NULL)
{
return 0;
}

SDL_WM_SetCaption ("Hello World", NULL);

message = loadImage ("message.bmp");
background = loadImage ("background.bmp");

int messageX = 0;
const int messageY = 0;

applySurface (0, 0, background, screen);
applySurface (messageX, messageY, message, screen);

SDL_Flip (screen);

int tick = 0;
int lastTick =0;
bool quit = false;

while (quit == false)
{
if (messageX >= SCREEN_WIDTH)
{
messageX = - 280;
// I don't need to write anything else, because messageX will be increased by one
// and it will be placed at messageX + 1 (which is - )
}

if (clock() - lastTick > 10)
{
tick ++;
lastTick = clock();

messageX ++;
applySurface (0, 0, background, screen);
applySurface (messageX, messageY, message, screen);

SDL_Flip (screen);
}

while (SDL_PollEvent (&event) )
{
if (event.type == SDL_QUIT)
quit = true;
}
}

SDL_FreeSurface (background);
SDL_FreeSurface (message);

return 0;
}

Share this post


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

  • Advertisement