Need to get the x and y of an SDL Surface which happens to be the comp bat

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

Recommended Posts

Hi I Need to get the x and y of an SDL Surface which happens to be the comp bat for a pong game .....the way Ive done it I have class Layer class Bat: Layer //class bat deals with both the cpu and player bat drawing .......it draws them as sdl surfaces class Ball: Layer //Responsible for drawing the ball and at present puts the ball in middle of //screen between the 2 bats.......the ball object is also drawns on as an sdl_surface Now everythings been working fine but I need the x and y of the cpubat at the time when the ball object gets created ......this is so I can initially put the ball stuck to the cpu bat on the right side of the screen
                                                  ___ cpu bat at R Side
ball------------->0|
|
|___


I have done some research on this namely to do with SDL_Rect r; SDL_GetClipRect; but I dont know how it applys to my code heres the classes where im trying to get the ball stuck to the right side bat for initialisation of the game only Sorry about the following three classes of code but its needed so that if any soulution is possible its going to fit the way Ive done things
class Layer {

public:
#define PLAYER_RACKET 1
#define CPU_RACKET 2
int x;
int y;
int xvel;
int yvel;
int offset;
int theLayerType;
SDL_Rect rP;
SDL_Rect rC;
//these are like prototypes if the actual func immplementation is outwith the class

Layer();
void setBatRects(void);
void setTypeCoords(int,int,int,int);
void show_the_layer();
void handle_events(int);

};

Layer::Layer(){
x = 0;
y = 0;
xvel = 0;
yvel = 0;
theLayerType = 0;
offset = 0;
}

void Layer::setBatRects(void)
{
SDL_GetClipRect(bat_player , &rP );
SDL_GetClipRect(bat_cpu , &rC );

}

void Layer::handle_events(int aType) {
cout<<"hi";
theLayerType = aType;
if(theLayerType == PLAYER_RACKET)
{
//handle player racket layer events
if(event.type == SDL_KEYDOWN) //a key is pressed
{
//set the velocity
switch(event.key.keysym.sym)
{
case SDLK_UP: yvel = yvel - 1; break;
case SDLK_DOWN: yvel = yvel + 1; break;

}

} else if(event.type == SDL_KEYUP) {

switch(event.key.keysym.sym)
{
case SDLK_UP: yvel = yvel + 1; break;
case SDLK_DOWN: yvel = yvel - 1; break;
}
}
}

if(theLayerType == CPU_RACKET)
{

}
}

//-----------------|EClass Layer|-------------------------------

//-----------------| Class Bat  |-------------------------------
class Bat: public Layer {

public:

int ply_x,ply_y,ply_xvel,ply_yvel;
int cpu_x,cpu_y,cpu_xvel,cpu_yvel;
bool notinited;

Bat();
//void setTypeCoords(int,int,int,int);
void setTypeLayer(int);
void handle_events(int);
void show_the_layer(void);
void show_move(void);
int get_player_type(void);
int get_cpu_type(void);

};

Bat::Bat(){
//set the bats stuff
//player
ply_x = 0;
ply_y = SCREEN_HEIGHT /2 - bat_player->h / 2;
ply_xvel = 0;
ply_yvel = 0;

//cpu need to setup still
cpu_x = SCREEN_WIDTH - bat_cpu->w;
cpu_y = SCREEN_HEIGHT / 2 - bat_cpu->h / 2;
cpu_xvel = 0;
cpu_yvel = 0;

offset = 0;

}

void Bat::setTypeLayer(int aTYPE){

theLayerType = aTYPE;
/*	if(aTYPE == PLAYER_RACKET)
{
theLayerType = PLAYER_RACKET;
}

if(aTYPE ==  CPU_RACKET)
{
theLayerType = CPU_RACKET;
} */
}

int Bat::get_player_type(void) {
return PLAYER_RACKET;
}

int Bat::get_cpu_type(void) {
return CPU_RACKET;
}

void Bat::handle_events(int)
{
Layer::handle_events(theLayerType);
}

void Bat::show_the_layer(){
if(theLayerType == PLAYER_RACKET){
if(notinited) {
x = ply_x;
y = ply_y;
offset = y;
notinited = false;
} else {

offset = yvel;
x = ply_x;
y = yvel+y;
}

apply_surface(x,y,bat_player,screen);

}

if(theLayerType == CPU_RACKET){
x = cpu_x;
y = cpu_y;
xvel = cpu_xvel;
yvel = cpu_yvel;
apply_surface(x,y,bat_cpu,screen);
}

//	SDL_GetClipRect(bat_player , &rP );
SDL_GetClipRect(bat_cpu , &rC ); //I have tried alsorts here but Im thinking this
//is a bad place for this as i only want the ball to be stuck to the bat the first time around
//e.g at the ball object creation .....I tried that also and could not get it to work ...I also figured that getClipRect only
//works after the surface has been blitted so this presents a problem

}

//-----------------| EClass Bat  |-------------------------------

//-----------------| Class Ball  |-------------------------------

class Ball: public Layer {
public:
bool is_inited;
Ball();
void ball_show();

};

Ball::Ball() {

x = SCREEN_WIDTH  / 2 - ball->w / 2;
y = SCREEN_HEIGHT / 2 - ball->h / 2;

xvel = 0;
yvel = 0;
}

//Ball::handle_events(void) {}

void Ball::ball_show() {
if(!is_inited)
{
apply_surface(x,y,ball,screen);

} else {
is_inited = true;

apply_surface(x,y,ball,screen);
x = rC.x;
y = rC.y;
}

}

//-----------------| EClass Ball  |-------------------------------


Thanks David

Share on other sites

I don't think I get your question though. You're drawing a "bat" for pong, at a specific X/Y, but you have no clue where its at?!

Share on other sites
NO .....nothing like that , as stated when the shapes get drawn to the board they are done like this

B                         BALL                BA                                             AT                                             TP                                             C                                              P                                              Uideally though what I want is thisB                                         BALLBA                                             AT                                             TP                                             C                                              P                                              U

e.g the ball touching the LEFT side of the CPU BAT
I have a problem with this as I cannot get the x and y values of an SDL_Surface
Because of this I did some reading and ended up trying to use SDL_Rect r;
and SDL_GetClipRect .......as stated above ^^^
but this is causing other complications
^^^^^also stated this initially

I have a problem because to get the ball object(surface) to stick to the cpubat object(surface) I need the cpu bat x and y and I cant get those from a surface type

Sorry for repeating but I just want to be absolutely clear this time.

Im needing some info on SDL_GetClipRect relevant to the way Ive coded my program that will get me the cpubats x and y.

Quote:
 I don't think I get your question though. You're drawing a "bat" for pong, at a specific X/Y, but you have no clue where its at?!

This is innacurate ^^ yeah i could just hard code in numbers but that is crap programming.....I as much as possible want to get my numbers OO style by using the machines own methods to get coord values , for instance intially I put the ball in the middle of the screen in an easy place until i figured out all my program was working like so
Ball:Ball {x = SCREEN_WIDTH  / 2 - ball->w / 2; //its not perfect but the point is//if I ever change things I dont want to have to be spending hours changing //lots of different numbers throughout the program .....I should just //be able to alter parts all at the same place in the classes and such}

Also sorry about the source tags I am so used of using code tags

Share on other sites
The real Issue here is that you are using parts of your graphics objects to represent the game physics.

Really, you should represent the paddle positions and sizes Separatly from the graphics, thus already have that data available; then the graphics stuff is drawn by querying the physics states.
You should not be trying to pull physics info (paddle location, size) from the graphics surface objects...

Yes, separate graphics and physics code/objects is a good idea.

Share on other sites
Yep Ap thinking back to my OO classes and stuff Im thinking you have just hilighted a very good point ........maybe I should have a constant in ball class called ball_start_position? do you think that is an acceptable way?

Share on other sites
Quote:
 Original post by redwingdwYep Ap thinking back to my OO classes and stuff Im thinking you have just hilighted a very good point ........maybe I should have a constant in ball class called ball_start_position? do you think that is an acceptable way?

Rather than that, you should have a ball position. When you create the ball it's constructor will set it's initial position (based on the location of the paddle if you like). All of this could be done in the console, it's simply retrieving and setting values. You then draw your graphics in the appropriate places based on those values.

So, if you want your ball to start off right next to the AI paddle:
- Find the position of the bat. This will probably be initialised so the bat is near the centre of the play area (vertically).
- Subtract the appropriate value from this so that the ball won't be inside the paddle. If you store coordinates based on the left on the graphic the appropriate value would be the width of the ball. If you store coordinates as the centre of the object it would be half the width of the ball.
- Initialise your ball object using this value. This calculation could either be inside the constructor of the ball, or could be done by some other object which would then pass the value to the ball's constructor as an argument.

Does that help at all?

Share on other sites
Hiya Ive been away for few days, but thanks for the reply and yes it does help :-) ......I have indeed started initialising init variables in the objects constructor and it has made things a bit better .......This sites forum has been priceless with the advice I have had .....I have got an intermediate knowledge of programming but I haver managed to step it up a gear by getting pointed in the right direction from people like yourself.

In my program I have managed to get the bats correctly colliding off the top & bottom of the y axis .....I am know going to try and a speed variable and setup varius speeds on the 2 bats and ball. I am also trying to figure out trajectorys , I was thinking of storing the points in some sort of array .....a person on the forum said I only need to store the start and end trajectory points but Im unsure of the formula that would let the computer work out the intermediate trajectory between point begin and point end

e.g

0              1     2      3                                    array[stores pbegin and pend only]1                          --pointend  2                    --3             --4pointbegin--

pointbegin = x0,y4;
pointend = x3,y1;

whats a formula to work out in between points based on start and end points

Thanks
redwing

• 10
• 17
• 9
• 14
• 41