Sign in to follow this  

SDL Problems with Blitting

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

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);
}

Share this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4816 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this