• 12
• 9
• 9
• 13
• 10

# SDL Problems with Blitting

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

## Recommended Posts

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)
#else
#endif

SDL_Surface *screen;

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

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

SDL_Init(SDL_INIT_VIDEO);

screen = SDL_SetVideoMode(800,600,0,SDL_HWSURFACE|SDL_DOUBLEBUF);
isRunning = true;

while(isRunning)
{
SDL_Event event;
SDL_PollEvent(&event);
if(event.type == SDL_QUIT)
isRunning = false;
Draw(0,0,image);
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);
}



##### Share on other sites
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);	}

##### Share on other sites
you were right thanks a lot man :-D im sure ill have many more questions soon :-)