Sign in to follow this  
DominicHughes

Problem with animation after right key press in SDL?

Recommended Posts

DominicHughes    143
Hello I'm trying to fix this graphical problem but I don't know how to code the fix and I don't even know If I'm correct on my solution but I will go ahead and tell you the problem and some ideas to why I think its happening but moving onto the problem.



[u]The Problem[/u]
When I press the right arrow key and blit surfaces to the screen it doesn't do it one at a time but instead all blit surfaces inside the if right key statement go on the screen at once


[u]Why is it happening?[/u]
It's because of the main while loop I think.



But I was wondering how could I set the bliting so that it blits images one at a time so that it won't mess up the application or cause flickering or any other graphical glitch.

Here's my code

[CODE]
/////////////////////////////
//Main Libary
/////////////////////////////
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_mixer.h>
/////////////////////////////
//Other Files
#include "CollisionFunction.h"
/////////////////////////////
#define SCREEN_HEIGHT 640
#define SCREEN_WIDTH 480
/////////////////////////////

SDL_Rect OrangeGhostKnightRect;
//Main Rectangles
SDL_Rect LevelOneRect;
SDL_Rect GroundOneRect;
SDL_Rect GroundTwoRect;
SDL_Rect GroundThreeRect;
SDL_Rect GroundFourRect;
//Mario Rect
SDL_Rect MarioRect;
//Water Rects
SDL_Rect WaterRect;
SDL_Rect WaterRect2;
SDL_Rect WaterRect3;
SDL_Rect WaterGroundRect;

//Storage Rectangles
SDL_Rect StorageRect[12];

//BoundingBox
SDL_Rect BoundingBox[2];

//Timer Rect
SDL_Rect TimerTextRect;
//Weather Rect
SDL_Rect BubbleGumCloudRect;
//////////////////////////////////////////////////
//Orange Ghost Knight Logic
/////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////

int LevelOneFunction(void *unused)
{
Uint8 *keystate = SDL_GetKeyState(NULL);
////////////////////////////////////////////////////////////////////////////////
//Color Selections
Uint32 RedColor =SDL_MapRGB(Screen->format,255,0,0);
Uint32 OrangeColor =SDL_MapRGB(Screen->format,255,100,0);
Uint32 BlackColor =SDL_MapRGB(Screen->format,0,0,0);
Uint32 GreyColor = SDL_MapRGB(Screen->format,192,192,192);
Uint32 MintGreenColor = SDL_MapRGB(Screen->format,39,216,176);
Uint32 LightOliveGreenColor = SDL_MapRGB(Screen->format,135,179,77);
//SDL COLOR Colors
SDL_Color WhiteColor = {255,255,255};
////////////////////////////////////////////////////////////////////////////////
////////////////////////
//Storage Rect Positions
///////////////////////
StorageRect[1].x = 0;
StorageRect[1].y = 0;
StorageRect[2].x = 0;
StorageRect[2].y = 380;
StorageRect[3].x = 0;
StorageRect[3].y = 310;
StorageRect[4].x = 0;
StorageRect[4].y = 360;
StorageRect[5].x = 0;
StorageRect[5].y = 400;
StorageRect[6].x = 320;
StorageRect[6].y = 400;
StorageRect[7].x = 580;
StorageRect[7].y = 400;
StorageRect[10].x = 400;
StorageRect[10].y = 320;
StorageRect[11].y = -200;
StorageRect[12].x = 5;
///////////////////////////////
//BoundingBox 1 Values
BoundingBox[1].x =160;
BoundingBox[1].y =360;
BoundingBox[1].w = 60;
BoundingBox[1].h = 108;
//BoundingBox 2 Values
BoundingBox[2].x =300;
BoundingBox[2].y =300;
BoundingBox[2].w = 60;
BoundingBox[2].h = 120;

//Mario Rect Values
MarioRect.x = 0;
MarioRect.y = 0;
//LevelOneBG Values
LevelOneRect.x = 0;
LevelOneRect.y = 0;
//PurpleAmal Rock 1 Values
GroundOneRect.x = 0;
GroundOneRect.y = 0;
////////////////////////////

//Bool Variable
bool done = false;
//Event Creation
SDL_Event event;
while(!done)
{ /////////////////////////////////////////
//Logic
////////////////////////////////////////
StorageRect[11].x += 1;
StorageRect[10].x += 1;
//Movements of BoundingBoxes

if(StorageRect[3].x > SCREEN_WIDTH)
{
StorageRect[3].x -=20;
StorageRect[2].x -=3;
StorageRect[6].x -=3;
StorageRect[1].x -=10;
}

////////////////////////////
//Collision Boxes
////////////////////////////
if(Check_Collision(MarioRect,GroundOneRect))
{
StorageRect[3].y -= 5;
}

if(Check_Collision(MarioRect,BoundingBox[1]))
{
StorageRect[3].y += 6;
}

if(StorageRect[10].x == 400)
{
StorageRect[10].x += 40;

}

if(StorageRect[10].x == 480)
{
StorageRect[10].x -=40;
}

//////////////////////////////////////
//Drawing
/////////////////////////////////////


LevelOneRect = StorageRect[1];
SDL_BlitSurface(LevelOneBG,NULL,Screen,&LevelOneRect);

WaterGroundRect = StorageRect[5];
SDL_BlitSurface(WaterGroundSurf,NULL,Screen,&WaterGroundRect);

GroundOneRect = StorageRect[2];
SDL_BlitSurface(GroundOneBG,NULL,Screen,&GroundOneRect);

GroundTwoRect = StorageRect[6];
SDL_BlitSurface(GroundTwoBG,NULL,Screen,&GroundTwoRect);

GroundThreeRect = StorageRect[7];
SDL_BlitSurface(GroundThreeBG,NULL,Screen,&GroundThreeRect);

//If user doesn't press right key on keyboard
if(keystate[SDLK_RIGHT])
{
MarioRect = StorageRect[3];
SDL_BlitSurface(SuperMarioWalkSurf[1],NULL,Screen,&MarioRect);

MarioRect = StorageRect[3];
SDL_BlitSurface(SuperMarioWalkSurf[2],NULL,Screen,&MarioRect);

MarioRect = StorageRect[3];
SDL_BlitSurface(SuperMarioWalkSurf[3],NULL,Screen,&MarioRect);


//Movements of actual visual objects
StorageRect[3].x +=5;
StorageRect[3].y += 5;
StorageRect[1].x -=6;
StorageRect[2].x -=2;
StorageRect[4].x -=1;
StorageRect[5].x -= 4;
StorageRect[6].x -=2;
StorageRect[7].x -=2;
BoundingBox[2].x -=2;
}

//If user doesn't press right key on keyboard
else if(!keystate[SDLK_RIGHT])
{
MarioRect = StorageRect[3];
SDL_BlitSurface(SuperMarioWalkSurf[1],NULL,Screen,&MarioRect);
}

OrangeGhostKnightRect = StorageRect[10];
SDL_BlitSurface(OrangeGhostKnightSurf,NULL,Screen,&OrangeGhostKnightRect);
BubbleGumCloudRect = StorageRect[11];
SDL_BlitSurface(BubbleGumCloudSurf,NULL,Screen,&BubbleGumCloudRect);
SDL_BlitSurface(Text,NULL,Screen,&TimerTextRect);

////////////////////////////////////////////////////
//While continous events are running in event
////////////////////////////////////////////////////
while(SDL_PollEvent(&event))
{
//allow to be able to switch events
switch(event.type)
{
//if user presses on close
case SDL_QUIT:
//Close Application
return 0;

//break case
break;
//SWITCH EVENT TYPE
}
/////////
//POLL
/////////
}

//////////////////////////////////////
//END OF THE MAIN WHILE LOOP
/////////////////////////////////////
//Update Screen

SDL_Flip(Screen);

}

/////////////////////////////////////

//DONT KNOW
//COULD BE THE RIGHT POINT

}
////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////
[/CODE]


If anyone has any answers of any kind and or helpful advice please do say

Share this post


Link to post
Share on other sites

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