Adding/Spawning an Object

I've been trying to figure this out for days: how would I go about spawning/adding an object in c++. To clarify, I have a pong game I made in C++ with GLUT, and I want to press a certain key to add another Ball.
Thanks in Advance.
Last edited on
Use a container to hold the objects http://www.cplusplus.com/reference/stl/
By instance
1
2
3
vector<Ball> balls;
//...
balls.push_back( Ball(/**/) );
You would have to store your balls in a dynamic array or STL container, such as a vector: http://cplusplus.com/reference/stl/vector/

Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Ball
{
    int x, y;

    public:
    Ball(int xx, int yy) : x(xx), y(yy)
    {
        //ctor
    }
}; //this class is obviously lacking...


vector<Ball> balls(1, Ball(320,240));


//in main loop
if (magicKeyIsPressed())
balls.push_back(Ball(X,Y));
It overwrites the last object though(when I press space it creates 1 object and when I press it again it deletes that object and makes a new one)? How do I fix this.
Are you accessing every object in the vector?

1
2
3
4
5
for (unsigned i = 0; i<Balls.size(); i++)
{
    Balls[i]move();
    Balls[i].draw();
}
I didnt even use a vector... I dont know how to... the format you posted seemes to be incorrect in Visual Studio C++ or something.
You need to include <vector> and it is in the std namespace. If you're not using a vector then what are you using?
Forget about the game. Learn how to properly use C++ first.
its not about the game its about how to do this in general...
and it seem vector is not a template
Last edited on
its not about the game its about how to do this in general...

Exactly. Learning C++ properly will teach you that.

and it seem vector is not a template

Yes, it is. Learning C++ properly will teach you that too.
As far as I am concerend I am learning C++...
Test Program

Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// insert cube.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <vector>
#include <set>
#include <xtree>

double delta = 0.001;
double s_x;
double s_y;
 


 typedef std::deque<ball> balls;
    balls balls_;
void spawner(){
	//glPushMatrix();
	glColor3f(0,1,0);
	glTranslatef(s_x,s_y,0);
	
	glBegin(GL_QUADS);
		glVertex3f(-.1,.1,0);
		glVertex3f(-.1,-.1,0);
		glVertex3f(.1,-.1,0);
		glVertex3f(.1,.1,0);
	glEnd();
	//glFlush();
	//glPopMatrix();
	
}

// vector<ball>;//, Ball(320,240));
void keyboard(){
	ball b;
	//new ball;
	if(GetAsyncKeyState(VK_SPACE)){

        b.x = s_x;
       balls_.push_back(b);
	   //ball::add(s_x,s_y);
	};
	
	if(GetAsyncKeyState(VK_UP)){
		s_y = delta;
	};
	if(GetAsyncKeyState(VK_DOWN)){
		s_y = -delta;
	};
	if(GetAsyncKeyState(VK_LEFT)){
		s_x = -delta;
	};
	if(GetAsyncKeyState(VK_RIGHT)){
		s_x = delta;
	};
	if(!GetAsyncKeyState(VK_UP)&&!GetAsyncKeyState(VK_DOWN)){
		s_y = 0;
	}
	if(!GetAsyncKeyState(VK_LEFT)&&!GetAsyncKeyState(VK_RIGHT)){
		s_x = 0;
	}
	//s_x = 0;
	//s_y = 0;
	
}
void draw(){
	glClear(GL_COLOR_BUFFER_BIT);
	glClearColor(0,0,0,0);
	keyboard();
	spawner();
	ball::mainloop();
	//glEnd();
	glFlush();
	glutPostRedisplay();
}

int main(int iArgc, char* cppArgv[])
{
	glutInit(&iArgc,cppArgv);
	//glutInitWindowSize(1000,600);
	ball::ball();
	glutCreateWindow("HELLO");
	glutDisplayFunc(draw);
		glutMainLoop();
	return 0;
}


stdafx.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>



// TODO: reference additional headers your program requires here#include "ball.cpp"
//#include "ball.h"

#include <GL/glut.h>
#include <deque>

#include <Windows.h>
#include <iostream>
#include "ball.h" 


ball.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#pragma once
class ball
{

	//static double ix,iy;
public:	
	static double x,y;
	ball(void);
	static void mainloop();
	static void add(double x,double y);
	~ball(void);

};


ball.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "StdAfx.h"
#include "ball.h"

extern double delta;
extern double s_x,s_y;
	double ball::x = .1; //= s_x;
	double ball::y = 0; //= s_y;
ball::ball(void)
{
	add(x,0);
}

void ball::mainloop(){
	x+=delta;
	
}
void ball::add(double x,double y)
{
		glPushMatrix();
	glColor3f(0,0,1);
	glTranslatef(delta,0,0);
	glBegin(GL_QUADS);
		glVertex3f(-.1+x,.1+y,0);
		glVertex3f(-.1+x,-.1+y,0);
		glVertex3f(.1+x,-.1+y,0);
		glVertex3f(.1+x,.1+y,0);
		glEnd();
	glPopMatrix();	
}

ball::~ball(void)
{
}
Last edited on
I still cant get it to spawn more than one though Can you just explain it to me in further detail, please?
Last edited on
1
2
3
4
5
6
7
8
9
class ball
{
public:	
	static double x,y; // <---
	ball(void);
	static void mainloop();
	static void add(double x,double y); // <---
	~ball(void);
};
If a member is static it means that all instances of that object share that variable (it's the same)
If a method is static it means that you don't need an object to call it (it is practically a function
Thank you, I never would have known that because without the static I used to get an error.
Topic archived. No new replies allowed.