Hi, SDL query..

Hi, I'm new to c++ and have made it far enough to start building a game in SDL (beginning to anyway) I have read a lot of tutorials and can't figure this one out.

In the codes below, I can't move my sdl_surface(image) up, down, left, or right, without it getting cut off by an invisible 256,64 box. Help please? thanks.


#include <iostream>
#include <string>
#include "SDL.h"
#include <stdio.h>
#include <cstdlib>

int SCREEN_WIDTH = 640;

const int SCREEN_HEIGHT = 480;

const int SCREEN_BPP = 32;



using namespace std;


int main (int argc, char *argv[])
{

printf("Initializing SDL.\n");


// Initializing.... Video.. Audio //

if((SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)==-1)) {
printf("Could not initialize SDL: %s.\n", SDL_GetError());
exit(-1);
}

//The attributes of the screen const //


SDL_Surface *screen = NULL;
SDL_Surface *image = SDL_LoadBMP("title.bmp");

SDL_SetColorKey( image, SDL_SRCCOLORKEY, SDL_MapRGB(image->format, 255, 255, 255) );


SDL_Surface *background = SDL_LoadBMP("background.bmp");


screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE);


//Set the window caption //

SDL_WM_SetCaption( "Carnage 1.0", NULL );



// Part of the bitmap that you want to draw //

SDL_Rect source;
source.x = 0;
source.y = 0;
source.w = 256;
source.h = 64;

// Part of the screen we want to draw the sprite to
SDL_Rect destination;
destination.x = 100;
destination.y = 0;
destination.w = 256;
destination.h = 64;


SDL_BlitSurface(image, &source, screen, &destination);


SDL_Flip(image);

//Part of image to draw //

SDL_Rect source2;
source.x = NULL;
source.y = NULL;
source.w = 640;
source.h = 480;

// Part of the screen we want to draw the sprite to
SDL_Rect destination2;
destination.x = NULL;
destination.y = NULL;
destination.w = NULL;
destination.h = NULL;



SDL_UpdateRect(screen, 0, 0, image->w, image->h);



SDL_Delay(2000);



// Free allocated images //

SDL_FreeSurface (image);


printf("SDL initialized.\n");

printf("Quitting SDL.\n");

/* Shutdown all subsystems */

SDL_Quit ();

printf("Quiting......\n");

exit(0);

}
Last edited on
You're using SDL_Flip wrong. I don't know what you expect it to do there, but it isn't doing it.

SDL_Flip basically takes what you drew to the screen surface and makes it so it is actually displayed on screen. Since you're not [explicitly] doing any double buffering, it likely has the same effect as SDL_UpdateRect.

So basically, you flip the screen surface, not an offscreen surface. Flipping 'image' doesn't make any sense.

You probably want to do this:

1
2
3
4
5
6
7
SDL_BlitSurface(image, &source, screen, &destination);

SDL_Flip(screen);

// get rid of SDL_UpdateRect

SDL_FreeSurface(image);
Topic archived. No new replies allowed.