I cannot get my program to make more than one object.

I want to create a sort of game where you build ships and then fight with them. I am ignoring the ship construction part for now to get the physics/graphics rendering down.
I am using OpenGL with GLUT in codeblocks with the GNU GCC C++ compiler.
I created a program which draws and moves a ship with turning and speed and rotating the ship properly based on its travel direction.
Now I want it to be able to shoot projectiles on a flat path. I will deal with real ballistics later with gravity and rising and falling and so forth.
The problem is that I can only create one instance of my projectile class. When I was using PhysX it could create an infinite number of objects of the same class. But I am having trouble replicating this functionality.
Here is my code:
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#include <windows.h>
#include <iostream>
#include <math.h>
#include <cstdlib>
#include <GL/glut.h>
#include <vector>
using namespace std;
int WINDOW_WIDTH = 640, WINDOW_HEIGHT = 480;
void SetOrthoForFont(){
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0, WINDOW_WIDTH, 0, WINDOW_HEIGHT);
glScalef(1, -1, 1);
glTranslatef(0, -WINDOW_HEIGHT, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();}
void ResetPerspectiveProjection()
{	glMatrixMode(GL_PROJECTION);
	glPopMatrix();
	glMatrixMode(GL_MODELVIEW);}
void RenderSpacedBitmapString(
int x,
int y,
int spacing,
void *font,
char *string)
{
char *c;
int x1=x;
for (c=string; *c != '\0'; c++) {
glRasterPos2i(x1,y);
glutBitmapCharacter(font, *c);
x1 = x1 + glutBitmapWidth(font,*c) + spacing;
}
}

void DrawAxes()
{
	glPushMatrix();
		glColor3f(0,0,1);
		glPushMatrix();
			glTranslatef(0,0, 0.8f);
			glutSolidCone(0.0325,0.2, 4,1);
			
			glTranslatef(0,0.0625,0.225f);
			RenderSpacedBitmapString(0,0,0,GLUT_BITMAP_HELVETICA_10, "Z");
		glPopMatrix();
		glutSolidCone(0.0225,1, 4,1);

		glColor3f(1,0,0);
		glRotatef(90,0,1,0);
		glPushMatrix();
			glTranslatef(0,0,0.8f);
			glutSolidCone(0.0325,0.2, 4,1);
			
			glTranslatef(0,0.0625,0.225f);
			RenderSpacedBitmapString(0,0,0,GLUT_BITMAP_HELVETICA_10, "X");
		glPopMatrix();
		glutSolidCone(0.0225,1, 4,1);

		glColor3f(0,1,0);
		glRotatef(90,-1,0,0);
		glPushMatrix();
			glTranslatef(0,0, 0.8f);
			glutSolidCone(0.0325,0.2, 4,1);
			
			glTranslatef(0,0.0625,0.225f);
			RenderSpacedBitmapString(0,0,0,GLUT_BITMAP_HELVETICA_10, "Y");
		glPopMatrix();
		glutSolidCone(0.0225,1, 4,1);
	glPopMatrix();
}
void DrawGrid(int GRID_SIZE)
{
	glBegin(GL_LINES);
	glColor3f(0.75f, 0.75f, 0.75f);
	for(int i=-GRID_SIZE;i<=GRID_SIZE;i++)
	{
		glVertex3f((float)i,0,(float)-GRID_SIZE);
		glVertex3f((float)i,0,(float)GRID_SIZE);

		glVertex3f((float)-GRID_SIZE,0,(float)i);
		glVertex3f((float)GRID_SIZE,0,(float)i);
	}
	glEnd();
}

void OnReshape(int nw, int nh) {
	glViewport(0,0,nw, nh);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(60, (GLfloat)nw / (GLfloat)nh, 0.1f, 100.0f);
	glMatrixMode(GL_MODELVIEW);
}

int oldX = 0, oldY = 0, rX = 15, rY = 0, state = 0, dist = -10;

float mx = 0;
float my = 0;
float mz = 0;
float bx = 0;
float bz = 0;
float direction = 0;
float speed = -0.1;
float bmx = 0;
float bmz = 0;

static void mover(void)
{
float radians = direction * 3.141592653 / 180;
float xPos = sin(radians) * speed;
float zPos = cos(radians) * speed;
float bxa = 0.1;
mx = mx+xPos;
mz = mz+zPos;
bx = bx+bxa;
bz = mz;
bmx = bx;
bmz = bz;
}

class ballistabolt {
public:
float bbx, bby, bbz, mom, momx, momy, momz;
void bolt_inputs(int,int,int,int,int,int);
int dbbx() {return (bbx);}
int dbby() {return (bby+1);}
int dbbz() {return (bbz);}
};

void ballistabolt::bolt_inputs (int mx,int my,int mz,int speed,int xPos,int zPos) {
bbx = mx;
bby = my;
bbz = mz;
mom = speed;
momx = xPos;
momz = zPos;
}

static void creballi(int vcreballi)
{
vector<ballistabolt> bolt;
ballistabolt ballbolt;
for (vcreballi; vcreballi>0; vcreballi--)
{
ballistabolt* newballistabolt=new ballistabolt();
bolt.push_back(*newballistabolt);
glPushMatrix();
    glTranslated(ballbolt.dbbx()+bmx,ballbolt.dbby(),ballbolt.dbbz()+bmz+ballbolt.bbx);
    glScaled(0.25,0.25,0.25);
    glutSolidSphere(1,16,16);
glPopMatrix();
}
}

int vcreballi = 0;

const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
const double ztm = t*+0.1;

static void display(void)
{
    float radians = direction * 3.141592653 / 180;
    float xPos = sin(radians) * (speed);
    float zPos = cos(radians) * (speed);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	glTranslatef(-mx,-12+mz/4,dist-24-mz);
	glRotatef(rX,1,0,0);
	glRotatef(rY,0,1,0);

	DrawAxes();
	DrawGrid(1000);

	glEnable(GL_LIGHTING);

    glColor3f(1,0,0);

    #shiptestobject.h(just the push and pop to draw the ship, its 5 squares.

    int markerz = -3, markerorigin = 0;
    for (markerz;markerz<=3;markerz++) {
    glPushMatrix();
        glScaled(1.0,20.0,1.0);
        glTranslated(5,0,markerorigin+markerz*3);
        glRotated(ztm*90,0,1,0);
        glutSolidCube(1);
    glPopMatrix();

    glPushMatrix();
        glScaled(1.0,20.0,1.0);
        glTranslated(-5,0,markerorigin+markerz*3);
        glRotated(ztm*90,0,1,0);
        glutSolidCube(1);
    glPopMatrix();
    }

    mover();
    if (vcreballi>0) {
    creballi(vcreballi);
    }

    glDisable(GL_LIGHTING);

    SetOrthoForFont();
	ResetPerspectiveProjection();
    glutSwapBuffers();
}

static void key(unsigned char key, int x, int y)
{
    float radians = direction * 3.141592653 / 180;
    float xPos = sin(radians) * speed;
    float zPos = cos(radians) * speed;
    if (key=='m') {
    direction=direction+.2;
    mx=mx+xPos;
    mz=mz+zPos;
    } else if(key=='n') {
    direction=direction-.2;
    mx=mx+xPos;
    mz=mz+zPos;
    } else if(key=='b') {
    mx=mx+xPos;
    mz=mz+zPos;
    } else if(key=='c') {
    direction=direction+.2;
    } else if(key=='x') {
    direction=direction-.2;
    } else if(key=='q') {
    vcreballi++;
    } else {
    }

    glutPostRedisplay();
}

void Mouse(int button, int s, int x, int y)
{
	if (s == GLUT_DOWN)
	{
		oldX = x;
		oldY = y;
	}

	if(button == GLUT_MIDDLE_BUTTON)
		state = 0;
	else
		state = 1;
}

void Motion(int x, int y)
{
	if (state == 0)
		dist *= (1 + (y - oldY)/60.0f);
	else
	{
		rY += (x - oldX)/5.0f;
		rX += (y - oldY)/5.0f;
	}
	oldX = x;
	oldY = y;

	glutPostRedisplay();
}

static void idle(void)
{
    glutPostRedisplay();
}

const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };

const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };

/* Program entry point */

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitWindowSize(WINDOW_WIDTH,WINDOW_HEIGHT);
    glutInitWindowPosition(10,10);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("GLUT Shapes");

    glutDisplayFunc(display);
    glutIdleFunc(idle);
    glutReshapeFunc(OnReshape);
    glutMouseFunc(Mouse);
	glutMotionFunc(Motion);
	glutKeyboardFunc(key);

    glClearColor(0,0,0,1);
    glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);

    glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);

    glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);

    glutMainLoop();

    return EXIT_SUCCESS;
}


Does anyone know why I can't create more than one object of class ballistabolt?
It looks like you are creating many ballistabolt objects, however, in your function...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
static void creballi(int vcreballi)
{
vector<ballistabolt> bolt;
ballistabolt ballbolt; //you create one of these and it only exists in this function..
for (vcreballi; vcreballi>0; vcreballi--)
{
ballistabolt* newballistabolt=new ballistabolt();
bolt.push_back(*newballistabolt);
glPushMatrix();
    glTranslated(ballbolt.dbbx()+bmx,ballbolt.dbby(),ballbolt.dbbz()+bmz+ballbolt.bbx); //You are using that object over and over, does the fact that you are not using one of your containers make a difference (I don't know what glTranslated does to with the object)
    glScaled(0.25,0.25,0.25);
    glutSolidSphere(1,16,16);
glPopMatrix();
}
}
Please indent your code.
¿What the hell I'm supposed to look at?

1
2
ballistabolt* newballistabolt=new ballistabolt();
bolt.push_back(*newballistabolt);
memory leak
@ne555,
according to me the memory leak in the program should onry hamper the run time
as usual i will take your code and show you your mistake
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
class ballistabolt {
public:
float bbx, bby, bbz, mom, momx, momy, momz;
void bolt_inputs(int,int,int,int,int,int);
int dbbx() {return (bbx);}
int dbby() {return (bby+1);}
int dbbz() {return (bbz);}
};

void ballistabolt::bolt_inputs (int mx,int my,int mz,int speed,int xPos,int zPos) {
bbx = mx;
bby = my;
bbz = mz;
mom = speed;
momx = xPos;
momz = zPos;
}

static void creballi(int vcreballi)
{
vector<ballistabolt> bolt;
ballistabolt ballbolt;//choose one function in this class
for (vcreballi; vcreballi>0; vcreballi--)// define vcreballi so as to call it into the class
{
ballistabolt* newballistabolt=new ballistabolt();
bolt.push_back(*newballistabolt);
glPushMatrix();
    glTranslated(ballbolt.dbbx()+bmx,ballbolt.dbby(),ballbolt.dbbz()+bmz+ballbolt.bbx);
    glScaled(0.25,0.25,0.25);
    glutSolidSphere(1,16,16);
glPopMatrix();
}
}

apart from that the class is structured well and you can create another
Topic archived. No new replies allowed.