Troubles With Dynamic Memory

Sage
Posts: 1,066
Joined: 2004.07
Post: #1
So I decided to start learning pointers and dynamic memory management so I tried my hand at writing a little particle solver that would just let a bunch of circles fall from the top-center of the screen and, if they fell off any direction, they'd reset to their origin. For some reason, though, when I run the program, I don't even see any of the particles. Is it involving the dynamic memory or is another simple error to blame?

Here's the header:
Code:
/*
*  ParticleSolver.h
*
*  Copyright 2005 SimReality Games. All rights reserved.
*
*/

#ifndef __PARTICLESOLVER_H
#define __PARTICLESOLVER_H

#include <OpenGL/OpenGL.h>
#include <OpenGL/gl.h>
#include "Particle.h"

class ParticleSolver
{
public:
    ParticleSolver();
    ~ParticleSolver();
    ParticleSolver(int numParticles);
    
    void Resize(int numParticles);
    void UpdateParticles(float frameRate);
    void DrawParticles();
    
    Particle *particles;
    int numberOfParticles;
};

#endif

And the .cpp:
Code:
/*
*  ParticleSolver.cpp
*
*  Copyright 2005 SimReality Games. All rights reserved.
*
*/

#include "ParticleSolver.h"

extern int screenWidth, screenHeight;
extern GLint myList;

ParticleSolver::ParticleSolver()
{
    particles = new Particle();
    numberOfParticles = 0;
}

ParticleSolver::~ParticleSolver()
{
    delete[] particles;
}

ParticleSolver::ParticleSolver(int numParticles)
{
    particles = new Particle[numParticles];
    numberOfParticles = numParticles;
}

void ParticleSolver::Resize(int numParticles)
{  
    Particle *tempStorage = particles;
    particles = new Particle[numParticles];
    int workingSize = (numParticles < numberOfParticles) ? numParticles : numberOfParticles;
    for(int i = 0; i < workingSize; i++)
        particles[i].CopyData(tempStorage[i]);
    delete[] tempStorage;
}

void ParticleSolver::UpdateParticles(float frameRate)
{
    for(int i = 0; i < numberOfParticles; i++)
    {
        particles[i].position.x += particles[i].velocity.x;
        particles[i].position.y += particles[i].velocity.y;
        particles[i].position.z += particles[i].velocity.z;
        
        if(particles[i].position.y > screenHeight ||
           particles[i].position.x < 0 ||
           particles[i].position.x > screenWidth)
        {
            particles[i].position.y = 0;
            particles[i].position.x = screenWidth / 2;
        }
    }
}

void ParticleSolver::DrawParticles()
{
    for(int i = 0; i < numberOfParticles; i++)
    {
        glPushMatrix();
        {
            glScalef(10,10,10);
            glTranslatef(particles[i].position.x,
                         particles[i].position.y,
                         particles[i].position.z);
            glCallList(myList);
        }
        glPopMatrix();
    }
}
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #2
Ok. Thanks to akb825, I found out that I should have my glScale and glTranslate functions called in the opposite order (translate, then scale). Now if anyone reads this and has a similar problem, they can fix it. Smile
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Troubles with simple events in Pygame perks 4 3,134 Aug 27, 2007 10:23 PM
Last Post: Malarkey
  Interface Troubles DylanE 2 2,629 Nov 17, 2006 04:30 PM
Last Post: DylanE
  Do you actually use dynamic memory? Najdorf 38 16,553 Sep 7, 2005 10:06 PM
Last Post: akb825
  Carbon Apple quit Event troubles deekpyro 3 5,786 May 7, 2002 06:24 AM
Last Post: deekpyro