Sign in to follow this  
MeritGamer

Initialializer Lists

Recommended Posts

I have no idea how they work. Amung other errors in this code, how do I use initializer lists in this case? ---------------------------------------------------------------------------- .cpp Bullet::Bullet() { //Initialize the velocity bXVel = 4; bYVel = 4; Bullet : (/*shooting*/) } void Bullet::handle_input() { if( event.type == SDL_KEYDOWN ) { switch( event.key.keysym.sym ) { case SDLK_SPACE: Bullet_Pointer->draw; Shooting = true; break; } if (Shooting == true) { Bullet_Pointer->update; } } } void Bullet::update() { bX += bXVel; bY += bYVel; } void Bullet::draw(SDL_Surface *bulletImage, SDL_Surface *screen) { SDL_Rect destination = {bX,bY}; SDL_BlitSurface(Bullet_Pointer,0,screen, &destination); } bool Bullet::dead() const { SDL_Surface *screen = SDL_GetVideoSurface(); if(bX > screen->w || bX < 0) { return false; } else if(bY > screen->h || bY < 0) { return false; } return true } }; ---------------------------------------------------------------------------- .h class Bullet { private: //The x and y offsets of the bullet int bX, bY; //The velocity of the bullet int bXVel, bYVel; bool Bbullet; public: Bullet : { void update(); void draw(); bool dead() const; void handle_input(); } };

Share this post


Link to post
Share on other sites
Your class definition has a few syntax errors. I assume you want to declare a constructor:

class Bullet
{
private:
//The x and y offsets of the bullet
int bX, bY;

//The velocity of the bullet
int bXVel, bYVel;

bool Bbullet;

public:
Bullet : // problem: not legal syntax

{ // problem: this shouldn't be here

void update();
void draw();
bool dead() const;
void handle_input();

} // problem: this shouldn't be here either
};


It should be:

class Bullet
{
private:
//The x and y offsets of the bullet
int bX, bY;

//The velocity of the bullet
int bXVel, bYVel;

bool Bbullet;

public:
Bullet();

void update();
void draw();
bool dead() const;
void handle_input();
};



Also note that you don't set the value of the members bX, bY and Bbullet in your constructor. You should.

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
Your class definition has a few syntax errors. I assume you want to declare a constructor:
*** Source Snippet Removed ***
It should be:
*** Source Snippet Removed ***

Also note that you don't set the value of the members bX, bY and Bbullet in your constructor. You should.


Thanks, now that I look back, I don't know what I was thinking, that is quite basic.

However I do have another issue. Im getting an error on:
Bullet_Pointer->draw;
Bullet_Pointer->update;

94 C:\Users\Family\Documents\FinalProject\Include\Headers\classes.cpp 'struct SDL_Surface' has no member named 'draw'

Same for update.

I know you had mentioned this earlier, but changing the pointer name to something more descriptive did not chnage anyhting.

Share this post


Link to post
Share on other sites
Quote:
Original post by MeritGamer
Im getting an error on:
Bullet_Pointer->draw;
Bullet_Pointer->update;

94 C:\Users\Family\Documents\FinalProject\Include\Headers\classes.cpp 'struct SDL_Surface' has no member named 'draw'


What is the type of Bullet_Pointer? It looks like it's a pointer to an SDL_Surface, and you probably intended for it to be a pointer to your Bullet class.

Share this post


Link to post
Share on other sites
Quote:
Original post by Gage64
Quote:
Original post by MeritGamer
Im getting an error on:
Bullet_Pointer->draw;
Bullet_Pointer->update;

94 C:\Users\Family\Documents\FinalProject\Include\Headers\classes.cpp 'struct SDL_Surface' has no member named 'draw'


What is the type of Bullet_Pointer? It looks like it's a pointer to an SDL_Surface, and you probably intended for it to be a pointer to your Bullet class.


Yes, Bullet_Pointer is pointing to an image. I diddnt know you can point to class. How would it look and work?

Share this post


Link to post
Share on other sites
I don't understand why a Bullet needs to react to input in the first place. In my mind, some other object (e.g. a Player object) might react to input to create a bullet.

E.g:

class Bullet
{
// bullet stuff
};

class Player
{
public:
// player stuff

Bullet shoot();
};

int main()
{
Player player;

std::vector<Bullet> bullets;

while(running)
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
if( event.type == SDL_KEYDOWN )
{
switch( event.key.keysym.sym )
{
case SDLK_SPACE:

bullets.push_back(player.shoot());
break;

// other keys

default:
break;
}
}
}

// update all objects

// remove dead objects from the simulation

// draw objects
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
I don't understand why a Bullet needs to react to input in the first place. In my mind, some other object (e.g. a Player object) might react to input to create a bullet.

E.g:
*** Source Snippet Removed ***


Well I am still not familier with vectors or the push.back function.

You can call Bullet(); into the "player" class? I never knew, I have a similar class relating to player, but instead it's just "Plane".

Share this post


Link to post
Share on other sites
Quote:

Well I am still not familier with vectors or the push.back function.

A vector is a dynamic object container. That means that std::vector<Bullet> can hold many bullets. A vector is modelled on an array, so you can use array like semantics when dealing with one. Because the vector can contain any number of instances, you can ask the vector for its size to determine how many times to loop:

std::vector<Bullet> bullets;

// add some bullets to the vector

for(int i = 0 ; i < bullets.size() ; ++i)
{
bullets[i].update();
}



This is very simple usage.

The push_back function simply adds an element to the end of the vector.

std::vector<int> integers;

// integers is empty, so its size == 0.

integers.push_back(1);

// integers is {1}, its size is now 1 and integers[0] == 1.

integers.push_back(42);

// integers is {1, 42}, its size == 2.
// integers[0] is still 1.
// integers[1] is 42.

integers.push_back(13);

// integers is {1, 42, 13}, its size == 3
// integers[0] is still 1.
// integers[1] is 42.
// integers[2] is 13.

// This next bit prints
//
// integers are: 1 42 13
//
std::cout << "integers are:";

for(int i = 0 ; i < integers.size() ; ++i)
{
std::cout << ' ' << integers[i];
}

std::cout << '\n';




Quote:

You can call Bullet(); into the "player" class?

Well, once you have defined the Bullet class it is like any other type. You can treat bullet like you would int or std::string. So you can create a function in the Plane class that returns Bullet instances.

Share this post


Link to post
Share on other sites
Quote:
Original post by MeritGamer
Quote:
Original post by Gage64
Quote:
Original post by MeritGamer
Im getting an error on:
Bullet_Pointer->draw;
Bullet_Pointer->update;

94 C:\Users\Family\Documents\FinalProject\Include\Headers\classes.cpp 'struct SDL_Surface' has no member named 'draw'


What is the type of Bullet_Pointer? It looks like it's a pointer to an SDL_Surface, and you probably intended for it to be a pointer to your Bullet class.


Yes, Bullet_Pointer is pointing to an image. I diddnt know you can point to class. How would it look and work?


First of all, when you call a function, you need to include the () on the end, as in Bullet_Pointer->draw();

Second, you can only call a member function of a class on a pointer to that class. Therefore you can only call your draw() on a pointer that points to a bullet object.

You would create a class pointer like this...

Bullet* bulletPointer = new Bullet();

Now, you can call bulletPointer->draw();

As I look at your code again, it appears you are trying to call a member function from another member function. In that case you would just call it as:
draw();
no pointer or -> needed.

I must ask, if you didn't even know you could create a pointer to a class, how are you making a game?

Share this post


Link to post
Share on other sites
Quote:
Original post by Chadwell
Quote:
Original post by MeritGamer
Quote:
Original post by Gage64
Quote:
Original post by MeritGamer
Im getting an error on:
Bullet_Pointer->draw;
Bullet_Pointer->update;

94 C:\Users\Family\Documents\FinalProject\Include\Headers\classes.cpp 'struct SDL_Surface' has no member named 'draw'


What is the type of Bullet_Pointer? It looks like it's a pointer to an SDL_Surface, and you probably intended for it to be a pointer to your Bullet class.


Yes, Bullet_Pointer is pointing to an image. I diddnt know you can point to class. How would it look and work?


First of all, when you call a function, you need to include the () on the end, as in Bullet_Pointer->draw();

Second, you can only call a member function of a class on a pointer to that class. Therefore you can only call your draw() on a pointer that points to a bullet object.

You would create a class pointer like this...

Bullet* bulletPointer = new Bullet();

Now, you can call bulletPointer->draw();

As I look at your code again, it appears you are trying to call a member function from another member function. In that case you would just call it as:
draw();
no pointer or -> needed.

I must ask, if you didn't even know you could create a pointer to a class, how are you making a game?


Rediculous amounts of research. I have general understanding of how most of my code works. But I havn't taught myself the mechanics of every function. I just havnt had time since this is an assignment that is due soon. I plan to study more when I have more time.

Share this post


Link to post
Share on other sites
This is an update of my code and also including my main.

Main
===============================================================================

// Headers
#include "SDL\SDL.h"
#include "SDL\SDL_image.h"
#include "SDL\SDL_mixer.h"
#include "SDL\SDL_ttf.h"
#include "Include\Headers\classes.h"
#include "Include\Headers\globals.h"
#include "Include\Headers\imagef.h"
#include "Include\Headers\misc.h"
#include <string>

int main( int argc, char* args[] )
{
bool quit = false;
bool game = false;

//Handle classes
Plane myPlane;
Bullet myBullet;
Level myLevel;
Title myTitle;
Timer myTimer;

//The frame rate regulator
Timer fps;

//Initialize
if( init() == false )
{
return 1;
}

//Load the files
if( load_files() == false )
{
return 1;
}

//While the user hasn't quit
while( quit == false )
{
//Start the frame timer
fps.start();

//While there's events to handle
while( SDL_PollEvent( &event ) )
{
//Handle events for the plane
myPlane.handle_input();
//If the user has Xed out the window
if( event.type == SDL_QUIT )
{
//Quit the program
quit = true;
}
}

if (title == true)
{
//Apply surfaces & handle input
myTitle.show();
myTitle.handle_input();
myTimer.update(fps);
}

if (title == false)
{
//Apply surfaces & handle input
myLevel.show();
myPlane.show();
myTimer.update(fps);

//Movement
myPlane.move();
myBullet.handle_input();
myLevel.move();
myTimer.update(fps);
}
}
//Free the surface and quit SDL
clean_up();

return 0;
}

Classes.cpp
==============================================================================
Note* Plane structure is going ot be remodeld when I understand more about how my bullet is going to work.

#include "SDL\SDL.h"
#include "SDL\SDL_mixer.h"
#include "SDL\SDL_ttf.h"
#include "globals.h"
#include "classes.h"
#include "imagef.h"
#include <sstream>
#include <cmath>

Plane::Plane()
{
//Initialize the offsets
x = 0;
y = 0;

//Initialize the velocity
xVel = 0;
yVel = 0;

}

void Plane::handle_input()
{
//If a key was pressed
if( event.type == SDL_KEYDOWN )
{
//Adjust the velocity
switch( event.key.keysym.sym )
{
case SDLK_UP: yVel -= PLANE_HEIGHT / 4; break;
case SDLK_DOWN: yVel += PLANE_HEIGHT / 4; break;
case SDLK_LEFT: xVel -= PLANE_WIDTH / 3; break;
case SDLK_RIGHT: xVel += PLANE_WIDTH / 4; break;
}
}
//If a key was released
else if( event.type == SDL_KEYUP )
{
//Adjust the velocity
switch( event.key.keysym.sym )
{
case SDLK_UP: yVel += PLANE_HEIGHT / 4; break;
case SDLK_DOWN: yVel -= PLANE_HEIGHT / 4; break;
case SDLK_LEFT: xVel += PLANE_WIDTH / 3; break;
case SDLK_RIGHT: xVel -= PLANE_WIDTH / 4; break;
}
}
}

void Plane::move()
{
//Move the plane left or right
x += xVel;
//Move the plane up or down
y += yVel;

//If the plane went too far to the left or right or has collided with the wall
if( ( x < 0 ) || ( x + PLANE_WIDTH > SCREEN_WIDTH ) )
{
//Move back
x -= xVel;
}

//If the plane went too far up or down
if( ( y < 0 ) || ( y + PLANE_HEIGHT > SCREEN_HEIGHT ) )
{
//move back
y -= yVel;
}
}

void Plane::show()
{
//Show the plane
apply_surface( x, y, plane, screen );
}

Bullet::Bullet(int x, int y) : bXVel(4), bYVel(4), Shooting(false), bX(x), bY(y)
{
}

void Bullet::handle_input()
{
if( event.type == SDL_KEYDOWN )
{
switch( event.key.keysym.sym )
{
case SDLK_SPACE:
draw();
Shooting = true;
break;
}
if (Shooting == true)
{
update();
}
}
}

void Bullet::update()
{
bX += bXVel;
bY += bYVel;
}

void Bullet::draw()
{
SDL_Rect destination = {bX,bY};
SDL_BlitSurface(Bullet_Surface,0,screen, &destination);
}

bool Bullet::dead() const
{
if(bX > SCREEN_WIDTH || bX < 0)
{
return false;
}
else if(bY > SCREEN_HEIGHT || bY < 0)
{
return false;
}
return true;
};

Classes.h
===============================================================================
class Plane
{
private:
//The X and Y offsets of the plane
int x, y;

//The velocity of the plane
int xVel, yVel;

public:
//Initializes the variables
Plane();

int getx(){return x;};
int gety(){return y;};


//Takes key presses and adjusts the plane's velocity
void handle_input ();

//Moves the plane
void move();

//Shows the plane on the screen
void show();

};



class Bullet
{
private:
//The x and y offsets of the bullet
int bX, bY;

//The velocity of the bullet
int bXVel, bYVel;

bool Shooting;

public:
Bullet(int x, int y);

void update();
void draw();
bool dead() const;
void handle_input();

};

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