Variables in a struct not updating

This is a discussion on Variables in a struct not updating within the C++ Programming forums, part of the General Programming Boards category; This problem is driving me crazy - I've just spent the past hour trying to figure out a problem that ...

  1. #1
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546

    Variables in a struct not updating

    This problem is driving me crazy - I've just spent the past hour trying to figure out a problem that shouldn't be happening. I've narrowed it down to the z variable in the struct not being updated by the Update() function. I do know the function is being called because I put a printf command in there and it produced results. Any help would be great.

    Edit: Tried having the compiler check for all warnings. Fixed a few things it reported but the problem remains.

    Code:
    /*
     * Starfield screensaver
     */
    #include <stdio.h>
    #include <time.h>
    #include <vector>
    using namespace std;
    
    #include <GL/gl.h>
    #include <GL/glu.h>
    #include "SDL.h"
    
    #define STARS_COUNT 1000
    
    // Star
    struct Star {
      float x;
      float y;
      float z;
      float brightness;
      
      Star() { Randomize(); };
      
      void Randomize() {
        x = (rand() % 10000 - 5000) / 1000.0;
        y = (rand() % 10000 - 5000) / 1000.0;
        z = (rand() % 100) / -100.0;
        brightness = (rand() % 10) / 7.5;
      };
      
      void Render() {
        glColor3f(brightness, brightness, brightness);
        glVertex3f(x, y, z);
      };
      
      void Update() {
        z += 0.1;
      };
    };
    
    // Initialises SDL and OpenGL
    int InitVideo() {
      // SDL
      if (SDL_Init(SDL_INIT_VIDEO) < 0) { return 1; }
      
      SDL_WM_SetCaption("Starfield Screensaver", "Starfield Screensaver");
      
      SDL_GL_SetAttribute(SDL_GL_RED_SIZE,     5);
      SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,   5);
      SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,    5);
      SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
      
      const SDL_VideoInfo *videoinfo = SDL_GetVideoInfo();
      if (!videoinfo) { return 1; }
      
      if (SDL_SetVideoMode(0, 0, 0, SDL_OPENGL | SDL_FULLSCREEN) == NULL) { return 1; }
      
      // OpenGL
      glViewport(0, 0, videoinfo->current_w, videoinfo->current_h);
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      
      gluPerspective(60.0, (GLfloat)videoinfo->current_w / (GLfloat)videoinfo->current_h, 1.0, 10.0);
      
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
      
      return 0;
    }
    
    // Handles the events from the keyboard and mouse
    int ProcessEvents() {
      static    Uint32 movement;
      SDL_Event        event;
      
      while (SDL_PollEvent(&event)) {
        switch (event.type) {
          case SDL_QUIT:
          case SDL_KEYUP:
          case SDL_KEYDOWN:
          case SDL_MOUSEBUTTONUP:
          case SDL_MOUSEBUTTONDOWN:
            return 1;
          
          case SDL_MOUSEMOTION:
            if (movement > 5) { return 1; } else { movement++; }
            break;
          
          default:
            break;
        }
      }
      
      return 0;
    }
    
    // Renders the stars
    void Render(vector <Star> stars) {
      glClear(GL_COLOR_BUFFER_BIT);
      glLoadIdentity();
      
      // Camera
      gluLookAt(0.0, 0.0, 5.0,
                0.0, 0.0,-5.0,
                0.0, 1.0, 0.0);
      
      // Render the stars
      glBegin(GL_POINTS);
        for (unsigned int index = 0; index < stars.size(); index++) {
          stars[index].Render();
        }
      glEnd();
      
      SDL_GL_SwapBuffers();
    }
    
    // Updates the stars
    void Update(vector <Star> stars) {
      for (unsigned int index = 0; index < stars.size(); index++) {
        stars[index].Update();
      }
    }
    
    int main(int argc, char *argv[]) {
      if (InitVideo()) { return 1; }
      
      vector <Star> stars;
      for (int index = 0; index < STARS_COUNT; index++) { Star tempstar; stars.push_back(tempstar); }
      
      Uint8 running = true;
      while (running) {
        Render(stars);
        Update(stars);
        if (ProcessEvents() == 1) { running = false; }
        
        SDL_Delay(10);
      }
      
      SDL_Quit();
      return 0;
    }
    Last edited by Frobozz; 09-28-2006 at 01:21 AM.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Quote Originally Posted by Frobozz
    I've just spent the past hour trying to figure out a problem that shouldn't be happening
    ... is it your inability to be less vague? Could you describe the problem? Can you not move along the Z-axis or do you get a funny picture when you do? What?
    Sent from my iPadŽ

  3. #3
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    Shameless bump with a slight bit of info. I added printf statements once again only this time to see what happens with the data in the update routine and a modification of the update function to only update one star. It appears the star is resetting itself back to an old number after the function ends which makes me think the copy in the vector isn't being replaced with the new one.

    This to one star:

    Code:
    void Update() {
      printf("%f\n", z);
      z += 0.1;
      printf("%f\n", z);
    };
    Produces:

    Code:
    -0.34
    -0.24
    -0.34
    -0.24
    -0.34
    etc
    Last edited by Frobozz; 09-28-2006 at 10:52 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,439
    makes me think the copy in the vector isn't being replaced with the new one.
    You are passing the vector by value instead of by reference. Could that be the problem?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    Hehe. That's embarrasing. Works now. Thanks a lot.

    Here's the working code in case anyone is interested:

    Code:
    /*
     * Starfield screensaver
     */
    #include <stdio.h>
    #include <time.h>
    #include <vector>
    using namespace std;
    
    #include <GL/gl.h>
    #include <GL/glu.h>
    #include "SDL.h"
    
    #define STARS_COUNT 1000
    
    // Star
    struct Star {
      float x;
      float y;
      float z;
      float brightness;
      
      Star() { Randomize(); };
      
      void Randomize() {
        x = (rand() % 10000 - 5000) / 1000.0;
        y = (rand() % 10000 - 5000) / 1000.0;
        z = (rand() % 100) / -10.0;
        brightness = (rand() % 10) / 7.5;
      };
      
      void Render() {
        glColor3f(brightness, brightness, brightness);
        glVertex3f(x, y, z);
      };
      
      void Update() {
        z += 0.1;
        if (z >= 5.0) { Randomize(); }
      };
    };
    
    // Initialises SDL and OpenGL
    int InitVideo() {
      // SDL
      if (SDL_Init(SDL_INIT_VIDEO) < 0) { return 1; }
      
      SDL_WM_SetCaption("Starfield Screensaver", "Starfield Screensaver");
      
      SDL_GL_SetAttribute(SDL_GL_RED_SIZE,     5);
      SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,   5);
      SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,    5);
      SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
      
      const SDL_VideoInfo *videoinfo = SDL_GetVideoInfo();
      if (!videoinfo) { return 1; }
      
      if (SDL_SetVideoMode(0, 0, 0, SDL_OPENGL | SDL_FULLSCREEN) == NULL) { return 1; }
      
      // OpenGL
      glViewport(0, 0, videoinfo->current_w, videoinfo->current_h);
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      
      gluPerspective(60.0, (GLfloat)videoinfo->current_w / (GLfloat)videoinfo->current_h, 1.0, 10.0);
      
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
      
      return 0;
    }
    
    // Handles the events from the keyboard and mouse
    int ProcessEvents() {
      static    Uint32 movement;
      SDL_Event        event;
      
      while (SDL_PollEvent(&event)) {
        switch (event.type) {
          case SDL_QUIT:
          case SDL_KEYUP:
          case SDL_KEYDOWN:
          case SDL_MOUSEBUTTONUP:
          case SDL_MOUSEBUTTONDOWN:
            return 1;
          
          case SDL_MOUSEMOTION:
            if (movement > 5) { return 1; } else { movement++; }
            break;
          
          default:
            break;
        }
      }
      
      return 0;
    }
    
    // Renders the stars
    void Render(vector <Star> *stars) {
      glClear(GL_COLOR_BUFFER_BIT);
      glLoadIdentity();
      
      // Camera
      gluLookAt(0.0, 0.0, 5.0,
                0.0, 0.0,-5.0,
                0.0, 1.0, 0.0);
      
      // Render the stars
      glBegin(GL_POINTS);
        for (unsigned int index = 0; index < stars->size(); index++) {
          stars->at(index).Render();
        }
      glEnd();
      
      SDL_GL_SwapBuffers();
    }
    
    // Updates the stars
    void Update(vector <Star> *stars) {
      for (unsigned int index = 0; index < stars->size(); index++) {
        stars->at(index).Update();
      }
    }
    
    int main(int argc, char *argv[]) {
      if (InitVideo()) { return 1; }
      
      vector <Star> *stars = new vector <Star>;
      for (int index = 0; index < STARS_COUNT; index++) { Star tempstar; stars->push_back(tempstar); }
      
      Uint8 running = true;
      while (running) {
        Render(stars);
        Update(stars);
        if (ProcessEvents() == 1) { running = false; }
        
        SDL_Delay(10);
      }
      
      SDL_Quit();
      return 0;
    }
    Once again, thanks for the help!

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,439
    hmm... why pass by pointer?

    Code:
    // Renders the stars
    void Render(vector <Star>& stars) {
      glClear(GL_COLOR_BUFFER_BIT);
      glLoadIdentity();
      
      // Camera
      gluLookAt(0.0, 0.0, 5.0,
                0.0, 0.0,-5.0,
                0.0, 1.0, 0.0);
      
      // Render the stars
      glBegin(GL_POINTS);
        for (unsigned int index = 0; index < stars.size(); index++) {
          stars[index].Render();
        }
      glEnd();
      
      SDL_GL_SwapBuffers();
    }
    
    // Updates the stars
    void Update(vector <Star>& stars) {
      for (unsigned int index = 0; index < stars.size(); index++) {
        stars[index].Update();
      }
    Last edited by laserlight; 09-28-2006 at 11:11 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    163
    Quote Originally Posted by Frobozz
    Here's the working code in case anyone is interested:
    did you write/download SDL.h yourself? Because all of the SDL parts give compile errors for me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Updating in a sequential file?
    By Ronnyv1 in forum C Programming
    Replies: 1
    Last Post: 03-24-2009, 04:41 PM
  2. linked list question
    By brb9412 in forum C Programming
    Replies: 16
    Last Post: 01-04-2009, 03:05 PM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. Help please im stuck
    By ItsMeHere in forum C Programming
    Replies: 7
    Last Post: 06-15-2006, 04:07 AM
  5. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21