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