How I can blit multi textures in the same time

I’am from Poland and my english isn’t good but I think you will understand my problem and you will help me. Can somebody tell me how I can blit two diffrent surface in the same time? I’am writting platformer game and I don’t know how i can move my hero and in the same time independly move some enemy. Until i had only my hero without enemies when i want jump i blit hero on the following coordinates but i had to blit background everytime when i change coordinates because without blit background my hero was leaving streak and now when i want add enemy I don’t know how I can blit “flying rocket from right to left” and in the same time jump because now when hero is jumping i can’t do nothing else.

Can you show us your source code? It will make it easier for us to understand you, and for you to understand the solution.

But I have a feeling that your problem is coming from not knowing that your screen shouldn’t be updated more than once in the same loop iteration.------------------------
EM3 Nathaniel Fries, U.S. Navy

http://natefries.net/

All my code

Code:
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_thread.h>
#include

const int SCREEN_WIDTH = 774;
const int SCREEN_HEIGHT = 375;
const int SCREEN_BPP = 32;
const int PLAYER_WIDTH = 70;
const int PLAYER_HIGHT = 75;
const int FRAMES_PER_SECOND = 15;
SDL_Rect position;
SDL_Surface *player = NULL;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *screen2 = NULL;
SDL_Event event;
SDL_Rect offset;
SDL_Rect offset2;
int bgX = 0, bgY = 0;
SDL_Thread *thread = NULL;

class Player
{
private:

public:
SDL_Rect player1[4];
SDL_Surface *loadImage;
int x,y;
int xVel;
bool run;
int frame;
int temp;
float xV,yV;
int t;
static const int g=-2;
static const int Vx=2;

Player()
{
x=SCREEN_WIDTH/2;
y=SCREEN_HEIGHT-80;
xVel=0;
frame=0;
temp=0;
run=false;
loadImage=IMG_Load( “sheet5.png” );
yV=0;
}

void Set_Player()
{
player1[0].x=0;
player1[0].y=0;
player1[0].h=PLAYER_HIGHT;
player1[0].w=PLAYER_WIDTH;

player1[1].x=PLAYER_WIDTH;
player1[1].y=0;
player1[1].h=PLAYER_HIGHT;
player1[1].w=PLAYER_WIDTH;

player1[2].x=PLAYER_WIDTH*2;
player1[2].y=0;
player1[2].h=PLAYER_HIGHT;
player1[2].w=PLAYER_WIDTH;

player1[3].x=(PLAYER_WIDTH*3)+5;
player1[3].y=0;
player1[3].h=PLAYER_HIGHT;
player1[3].w=PLAYER_WIDTH;

}

void Gravity(int xbg, int width)
{
SDL_Rect back;
SDL_Rect back2;
back.x=-xbg;
back.y=0;
back2.x=xbg+width;
back2.y=0;
if(position.y<(SCREEN_HEIGHT-80))
{
do
{
position.y+=5;
position.x+=2;
SDL_BlitSurface(screen,&back,background,NULL);
SDL_BlitSurface(screen,NULL,background,&back2);
SDL_BlitSurface(player,&player1[frame],background,&position);
SDL_Flip(background);
}while(position.y!=(SCREEN_HEIGHT-80));
}

}
void Skok(int xbg, int width)
{
SDL_Rect back;
SDL_Rect back2;
back.x=-xbg;
back.y=0;
back2.x=xbg+width;
back2.y=0;
for(int i=80;i<200;i+=5)
{
position.x+=2;
if(position.x + 100 > SCREEN_WIDTH)
{
position.x-=20;
position.y=SCREEN_HEIGHT-80;
SDL_BlitSurface(screen,&back,background,NULL);
SDL_BlitSurface(screen2,NULL,background,&back2);
SDL_BlitSurface(player,&player1[frame],background,&position);
SDL_Flip(background);
}
position.y=SCREEN_HEIGHT-i;
SDL_BlitSurface(screen,&back,background,NULL);
SDL_BlitSurface(screen2,NULL,background,&back2);
SDL_BlitSurface(player,&player1[frame],background,&position);
SDL_Flip(background);
}

}
void Draw()
{
position.x=this->x;
position.y=this->y;
player = SDL_DisplayFormatAlpha(loadImage);
SDL_BlitSurface(player,player1,background,&position);
//SDL_Flip(background);
}

void handle_input()
{
if( event.type == SDL_KEYDOWN )
{

 switch( event.key.keysym.sym )
 {
 case SDLK_LEFT:
	 {
		 xVel -= PLAYER_WIDTH/4;
		 temp=1;
		 break;
	 }
 case SDLK_RIGHT: 
	 {
		 xVel += PLAYER_WIDTH/4; 
		temp=1;
		 break; 
	 }
 }

}else if( event.type == SDL_KEYUP )
{
switch( event.key.keysym.sym )
{
case SDLK_LEFT: xVel += PLAYER_WIDTH/4; temp=0; break;
case SDLK_RIGHT: xVel -= PLAYER_WIDTH/4; temp=0; break; }
}
}

void Move()
{

position.x+=xVel;
if( ( x < 0 ) || ( position.x + 70 > SCREEN_WIDTH ) ) 
{ 
	position.x -= xVel; 
}

}

void Update ()
{
if(temp==1)
{
if(frame==0 )
{
frame++;

	}else if( frame == 1){
		frame++;	
	}else if(frame == 2) {
		frame++;
		
	}else {
		frame=0;
		}
}
//SDL_BlitSurface(screen,NULL,background,NULL);
SDL_BlitSurface(player,&player1[frame],background,&position);

}
};

class Timer
{
private:
//czas zegara kiedy startujemy
int startTicks;

//ticks kiedy zatrzymano timer
int pausedTicks;

//status timera
bool paused;
bool started;

public:

Timer()
{
	
	startTicks = 0;
	pausedTicks = 0;
	paused = false;
	started = false;
}


void start()
{
	
	started = true;
	paused = false;
	//obecny czas
	startTicks = SDL_GetTicks();
}
void stop()
{
	started = false;
	//unpause timera
	paused = false;
}
void pause()

{
	
	if( ( started == true ) && ( paused == false ) )
	{
		paused = true;
		//obliczanie paused ticks
		pausedTicks = SDL_GetTicks() - startTicks;
	}
}

void unpause()

{
	if( paused == true )
	{
		paused = false;
		//restart ticks
		startTicks = SDL_GetTicks() - pausedTicks;
		pausedTicks = 0;
	}
}


//pobranie czasu timera
int get_ticks()

{
	if( started == true )
	{
		if( paused == true )
		{
			return pausedTicks;
		}
		else
		{
			return SDL_GetTicks() - startTicks;
		}
	}
	return 0;
}

bool is_started()		
{
	return started;
}
bool is_paused()
{
	return paused;
}

};

class Rocket
{
public:
int xR,yR;
SDL_Surface *rocket;
SDL_Rect rock;

Rocket()
{
	xR=SCREEN_WIDTH;
	yR=SCREEN_HEIGHT-40;
	rocket = IMG_Load("rocket.png");
}

void Draw()
{
	xR=SCREEN_WIDTH;
	yR=SCREEN_HEIGHT-40;
	do{
		rock.x=xR;
		rock.y=yR;
		SDL_BlitSurface(rocket,NULL,background,&rock);
		SDL_Flip(background);
		xR--;
		}while(xR>=0);
}

};

int main( int argc, char* args[] )
{
Player norio;
Timer fps;
Rocket ofe;
bool quit = false;
SDL_Init(SDL_INIT_EVERYTHING);
background=SDL_SetVideoMode(774,375,32,SDL_HWSURFACE | SDL_DOUBLEBUF);
screen=IMG_Load(“bg2.png”);
screen2=IMG_Load(“bg2.png”);
norio.Set_Player();
norio.Draw();

while (quit == false)
{
	fps.start();
	while(SDL_PollEvent(&event))
	{
		norio.handle_input();
		

		if(event.type == SDL_QUIT)
		{
			quit=true;
		}
		if(event.type == SDL_KEYDOWN)
		{
			if(event.key.keysym.sym == SDLK_s)
			{
				norio.Skok(bgX,screen->w);
				norio.Gravity(bgX,screen->w);
			}
			if(event.key.keysym.sym == SDLK_d)
			{
				ofe.Draw();
			}
			
		}
		
		
		
	}
		bgX-=2;
		if( bgX <= -screen->w )
		{
			bgX = 0; 
		}
		offset.x=bgX;
		offset.y=bgY;
		SDL_BlitSurface(screen,NULL,background,&offset);
		offset2.x=bgX+screen->w;
		offset2.y=0;
		SDL_BlitSurface(screen,NULL,background,&offset2);
		
			
			
	norio.Move();
	norio.Update();
	SDL_Flip(background);
	if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
    {
        SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
    }	
}


SDL_Quit();


return 0;

}

and i don’t know how i can ofe.Draw(); and (norio.Skok(bgX,screen->w); norio.Gravity(bgX,screen->w):wink: in the same time

Yep, that’s your problem. You shouldn’t be drawing the player (or any enemy) more than once per loop.

Your source code isn’t terribly structured, either. I would recommend you think about what you want to implement it before you start implementing things, that way you can make a decent structure. But I assume you’re still learning, so it’s fine for now. Good structure will save you hours of development and debugging time later on, though.------------------------
EM3 Nathaniel Fries, U.S. Navy

http://natefries.net/

You need two things to do this right: An object describing a mobile character,
and a list/vector/array of such objects.

The object needs to contain information about the character’s position and its
movement state. Each frame, do the following (pseudocode):

procedure Draw:
draw background
for each object in list:
calculate new position from old position and movement state
blit sprite at correct position
end for
SDL_Flip/SDL_RenderPresent (depending on which version you’re using)
end procedure

That’s the basic drawing idea. As your skills and your game evolve, that’ll
probably become more sophisticated, but that’s the general idea. Draw
everything in one big loop and then send it to the screen.________________________________
From: blandzi@tlen.pl (norbi123)
To: sdl at lists.libsdl.org
Sent: Sat, April 16, 2011 10:36:42 AM
Subject: [SDL] How I can blit multi textures in the same time

I’am from Poland and my english isn’t good but I think you will understand my
problem and you will help me. Can somebody tell me how I can blit two diffrent
surface in the same time? I’am writting platformer game and I don’t know how i
can move my hero and in the same time independly move some enemy. Until i had
only my hero without enemies when i want jump i blit hero on the following
coordinates but i had to blit background everytime when i change coordinates
because without blit background my hero was leaving streak and now when i want
add enemy I don’t know how I can blit “flying rocket from right to left” and in
the same time jump because now when hero is jumping i can’t do nothing else.

how are you making the character jump that you cannot do anything while its
jumping? I’m suspecting some form of for loop which runs until the target is
met, which which probably isn’t ideal.

One simple way to fix this is to give your character some “states” like
jumping_up, jumping_down, etc and update the position based on those during your
main loop. Without knowing your programming language of choice, its hard to give
better detail, but for example give your enemy class an “Update” method, which
will update position, while keeping your main game loop from getting cluttered
with the calculations. So then Mason’s pseudo code becomes something like:

procedure Update:
calculate new position from old position and movement state
anything other variables related to the character that must be changed when
animating
other stuff, perhaps collision detection
end procedure

procedure Draw:
draw background
for each object in list:
character.Update()
blit sprite at correct position
end for
SDL_Flip/SDL_RenderPresent (depending on which version you’re using)
end procedure________________________________
From: masonwheeler@yahoo.com (Mason Wheeler)
To: sdl at lists.libsdl.org
Sent: Tue, April 19, 2011 12:35:24 AM
Subject: Re: [SDL] How I can blit multi textures in the same time

You need two things to do this right: An object describing a mobile character,
and a list/vector/array of such objects.

The object needs to contain information about the character’s position and its
movement state. Each frame, do the following (pseudocode):

procedure Draw:
draw background
for each object in list:
calculate new position from old position and movement state
blit sprite at correct position
end for
SDL_Flip/SDL_RenderPresent (depending on which version you’re using)
end procedure

That’s the basic drawing idea. As your skills and your game evolve, that’ll
probably become more sophisticated, but that’s the general idea. Draw
everything in one big loop and then send it to the screen.


From: blandzi@tlen.pl (norbi123)
To: sdl at lists.libsdl.org
Sent: Sat, April 16, 2011 10:36:42 AM
Subject: [SDL] How I can blit multi textures in the same time

I’am from Poland and my english isn’t good but I think you will understand my
problem and you will help me. Can somebody tell me how I can blit two diffrent
surface in the same time? I’am writting platformer game and I don’t know how i
can move my hero and in the same time independly move some enemy. Until i had
only my hero without enemies when i want jump i blit hero on the following
coordinates but i had to blit background everytime when i change coordinates
because without blit background my hero was leaving streak and now when i want
add enemy I don’t know how I can blit “flying rocket from right to left” and in
the same time jump because now when hero is jumping i can’t do nothing else.

Oops, for some reason I couldn’t find the OP’s post with source. That and a few
other posts from this thread.Now I see it.

I stand by what I said. :slight_smile:

That’s C++ right? Eww, get those globals out of there :slight_smile: Seriously, going
straight to the class I had to search for where some of those variables came
from.

To give you a better idea of what I meant then:

class Player
{
enum PlayerStates
{
RUNNING,
JUMP_UP,
JUMP_DOWN,
CROUTCH,
etc…
} player_state;

static int const MAX_JUMP_HEIGHT = 20;    // Just for demonstration purposes

};

void
Player::Update()
{

if (RUNNING = player_state)
    ...
if (JUMPING_UP == player_state)
{
    position.y += jump_diff;
    if (MAX_JUMP_HEIGHT <= position.y)
    {
        player_state = JUMP_DOWN;
    }
}
else if(JUMPING_DOWN == player_state)
{
    position.y -= jump_diff;
    if (GROUND_LEVEL >= position.y)
    {
        player_state = IDLE;
    }
}

etc etc etc

SDL_BlitSurface(player,&player1[frame],background,&position);

}

You could also inherit Rocket from Player, it may make things easier for you in
the future.________________________________
From: @Anthony_T (Anthony T.)
To: SDL Development List
Sent: Tue, April 19, 2011 11:05:39 AM
Subject: Re: [SDL] How I can blit multi textures in the same time

how are you making the character jump that you cannot do anything while its
jumping? I’m suspecting some form of for loop which runs until the target is
met, which which probably isn’t ideal.

One simple way to fix this is to give your character some “states” like
jumping_up, jumping_down, etc and update the position based on those during your
main loop. Without knowing your programming language of choice, its hard to give
better detail, but for example give your enemy class an “Update” method, which
will update position, while keeping your main game loop from getting cluttered
with the calculations. So then Mason’s pseudo code becomes something like:

procedure Update:
calculate new position from old position and movement state
anything other variables related to the character that must be changed when
animating
other stuff, perhaps collision detection
end procedure

procedure Draw:
draw background
for each object in list:
character.Update()
blit sprite at correct position
end for
SDL_Flip/SDL_RenderPresent (depending on which version you’re using)
end procedure


From: masonwheeler@yahoo.com (Mason Wheeler)
To: sdl at lists.libsdl.org
Sent: Tue, April 19, 2011 12:35:24 AM
Subject: Re: [SDL] How I can blit multi textures in the same time

You need two things to do this right: An object describing a mobile character,
and a list/vector/array of such objects.

The object needs to contain information about the character’s position and its
movement state. Each frame, do the following (pseudocode):

procedure Draw:
draw background
for each object in list:
calculate new position from old position and movement state
blit sprite at correct position
end for
SDL_Flip/SDL_RenderPresent (depending on which version you’re using)
end procedure

That’s the basic drawing idea. As your skills and your game evolve, that’ll
probably become more sophisticated, but that’s the general idea. Draw
everything in one big loop and then send it to the screen.


From: blandzi@tlen.pl (norbi123)
To: sdl at lists.libsdl.org
Sent: Sat, April 16, 2011 10:36:42 AM
Subject: [SDL] How I can blit multi textures in the same time

I’am from Poland and my english isn’t good but I think you will understand my
problem and you will help me. Can somebody tell me how I can blit two diffrent
surface in the same time? I’am writting platformer game and I don’t know how i
can move my hero and in the same time independly move some enemy. Until i had
only my hero without enemies when i want jump i blit hero on the following
coordinates but i had to blit background everytime when i change coordinates
because without blit background my hero was leaving streak and now when i want
add enemy I don’t know how I can blit “flying rocket from right to left” and in
the same time jump because now when hero is jumping i can’t do nothing else.