SDL Problems with Blitting

Started by
2 comments, last by Gaiiden 19 years, 6 months ago
Hey guys, im making an SDL Pong clone atm. I just started as you see by the code but i already have a problem. I have paddle.bmp and bg.bmp that i load with the Draw() function. but if i load the paddle anywhere but 0,0 it either gets chopped off or doesnt appear. please help! code:

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <ctime>
#include <SDL/SDL.h>

#if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
#define RMASK 0xFF000000
#define BMASK 0x00FF0000
#define GMASK 0x0000FF00
#define AMASK 0x000000FF
#else
#define AMASK 0xFF000000
#define GMASK 0x00FF0000
#define BMASK 0x0000FF00
#define RMASK 0x000000FF
#endif

SDL_Surface *screen;

void Draw(int x, int y, SDL_Surface *source);

int main(int argc, char **argv)
{
	SDL_Surface *image;
	SDL_Surface *paddle;
	bool isRunning;

	SDL_Init(SDL_INIT_VIDEO);

	screen = SDL_SetVideoMode(800,600,0,SDL_HWSURFACE|SDL_DOUBLEBUF);
	image = SDL_LoadBMP("bg.bmp");
	paddle = SDL_LoadBMP("paddle.bmp");
	isRunning = true;

	while(isRunning)
	{
		SDL_Event event;
		SDL_PollEvent(&event);
		if(event.type == SDL_QUIT)
			isRunning = false;
		Draw(0,0,image);
		Draw(0,0,paddle);
		SDL_Flip(screen);
	}

	return 0;
}

void Draw(int x, int y, SDL_Surface *source)
{
	SDL_Rect dest;
	SDL_Rect src;

	src.x = x;
	src.y = y;
	src.h = source->h;
	src.w = source->w;

	dest.x = x;
	dest.y = y;
	dest.w = source->w;
	dest.h = source->h;

	SDL_BlitSurface(source, &src, screen, &dest);
}

//-----------------------------------------One short sleepe past, wee wake eternally, And Death shall be no more, Death thou shalt die.
Advertisement
its been a while since ive used SDL for drawing, but i think your screwing up in your Draw() function. you only want to specify an SDL_Rect for the source if you want to draw only a piece of this source. im guessing you want to draw the whole thing... also, the x/y/ that you draw at shouldnt be the same for the x/y of the source position, unless your drawing a background or something.. well, its hard to explain, but check out cone3d. it explains this stuff pretty decently. anyway, try switching your draw() function to this:

void Draw(int x, int y, SDL_Surface *source){	SDL_Rect dest;	dest.x = x;	dest.y = y;	SDL_BlitSurface(source,NULL, screen, &dest);}


note i send NULL where source was - this means draw the entire image. also, the w/h field of a destination rect is not valid or used. so no need to set it.

also, i notice your not taking input correctly. you need to check for and process all events in the que, but since your not looping, you will probably only catch one of the events, in fact, your not even checking if SDL_PollEvents() returns true (if there is an event). your main loop should look like this:

	while(isRunning)	{		SDL_Event event;		while(SDL_PollEvent(&event))		if(event.type == SDL_QUIT)			isRunning = false;		                Draw(0,0,image);		Draw(0,0,paddle);		SDL_Flip(screen);	}
FTA, my 2D futuristic action MMORPG
you were right thanks a lot man :-D im sure ill have many more questions soon :-)
//-----------------------------------------One short sleepe past, wee wake eternally, And Death shall be no more, Death thou shalt die.
If you have more SDL questions, you can ask em in the Alternative Libs forum. It could use the traffic and that's what it's there for anyways.

Drew Sikora
Executive Producer
GameDev.net

This topic is closed to new replies.

Advertisement