I’m a new person to C++, but i have learned a little Java and I/m working with SDL for the first time at the same time but my problem is I’m trying to create an object (paddle) p1 and p2.
this
#ifndef PONG_H_
#define PONG_H_
#undef main
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_ttf.h>
class paddle{
private:
int y;
int yv;
int x;
public:
paddle(){
y = 0;
yv = 0;
x = 0;
}
paddle(int ex, int why, int vel){
y = why;
yv = vel;
x = ex;
}
void setY(int ycoord) {y = ycoord;}
void setV(int velocity){ yv = velocity;}
void setX(int xcoord) {x = xcoord;}
int getX() {return x;}
int getY() {return y;}
int update(){y += yv; return y;}
};
class Pong { //pong class where everythin gets its name and a purpose.
private:
SDL_Surface * Paddle;
SDL_Surface * Ball; //SDL_Surface pointer for Ball
SDL_Surface * screen; //SDL_Surface pointer to backbuffer
public:
int Running;
Pong(); //eh..
int OnExecute(); //another function returning a value.
int w; //width of screen
int h; //height of screen
bool OnInit();
void OnEvent(SDL_Event* Event);
void OnLoop();
void OnRender();
void OnCleanup();
void redraw( int x, int y, SDL_Surface* source, SDL_Surface* destination ) { //Make a temporary rectangle to hold the offsets
SDL_Rect offset; //Give the offsets to the rectangle
offset.x = x; offset.y = y;
SDL_BlitSurface( source, NULL, destination, &offset );//Blit the surface
offset.x =0; offset.y=0; //resets the offsets sicne they apaprently dont reset per use.
};
};
#endif /* PONG_H_ */
is my header file, it contains the main functions of pong class also but I omitted those to make things less convoluted. Let me know if those are needed for a good answer.
and i declare the objects here…
#include <iostream>
#include <SDL/SDL.h>
#include "Pong.h"
paddle p1;
paddle p2;
Pong::Pong(){
h = 768;
w = 1024;
screen = NULL;
Paddle = NULL;
Ball = NULL;
Running = true;
atexit(SDL_Quit);
}
int Pong::OnExecute() {
if(OnInit() == false)
return-1;
SDL_Event Event;
while(Running) {
while(SDL_PollEvent(&Event)) {
OnEvent(&Event);
}
OnLoop();
OnRender();
}
OnCleanup();
return 0;
}
int main( int argc, char* args[]) {
Pong theApp;
return theApp.OnExecute();
}
but what sort of makes it difficult is i use it in…here:
#include "Pong.h"
void Pong::OnEvent(SDL_Event* Event) {
if(Event->type == SDL_QUIT)
Running = false;
switch(Event->type)
{
case SDL_KEYDOWN: //look for key holds
switch(Event->key.keysym.sym) //check key values and changes coords respectiely
{
default:
break;
case SDLK_UP:
p2.setV(-2);
break;
case SDLK_DOWN:
p2.setV(2);
break;
case SDLK_w:
p1.setV(-2);
break;
case SDLK_s:
p1.setV(2);
break;
case SDLK_ESCAPE:
Running = false;
break;
}
break;
case SDL_KEYUP:
switch(Event->key.keysym.sym)
{
default:
break;
case SDLK_UP:
p2.setV(0);
break;
case SDLK_DOWN:
p2.setV(0);
break;
case SDLK_w:
p1.setV(0);
break;
case SDLK_s:
p1.setV(0);
break;
}
break;
break;//break of Key holding event check
}
}
Its hard for me to ask the question because I don’t know where to begin. I tried many different weird things and I’m just lost now. If you need any more info I’ll be happy to provide.
void Pong::OnLoop(){
if(p1.getY()<=0){
p1.setV(0);
p1.setY(0);
}
if(p2.getY()<=0){
p2.setV(0);
p2.setY(0);
}
if(p1.getY()>=h - Paddle->h){
p1.setV(0);
p1.setY(h - Paddle->h);
}
if(p2.getY()>=h - Paddle->h){
p2.setV(0);
p2.setY(h - Paddle->h);
}
}
..\src\Pong_OnLoop.cpp:11:5: error: ‘p1’ was not declared in this scope
..\src\Pong_OnLoop.cpp:15:5: error: ‘p2’ was not declared in this scope
..\src\Pong_OnLoop.cpp:19:5: error: ‘p1’ was not declared in this scope
..\src\Pong_OnLoop.cpp:23:5: error: ‘p2’ was not declared in this scope
Since you haven’t provided a clear question, it’s difficult to provide the right answer, but my guess is that you can’t access p1 and p2 in the OnEvent-/OnLoop-Method.
Is it absolutely necessary to let the two paddle-objects live in the global namespace? Because in this scenario I think it would be best to have 2 paddle-members as part of the Pong class, also I would use initialization lists like so:
You might not even need all initializers depending on the optimization settings and the default memory initialization but that’s not the point. This way you should have access to p1 and p2 in all of Pongs own methods. Bo Perssons answer may work as well, but I think this is a more object- and thus C++-oriented approach.
On a side note: You said that the header file contains some more methods – seeing that your constructor is implemented in the cpp-file and the redraw-Method in the header-file you might want to learn a bit more about inlining. It won’t be much of an issue in this example but it’s usually best practice to move the more complex methods to the translation units (cpp-files), especially when creating libraries.