IRIX Network Forums
Why the errors in SGI example graphics animation code? - Printable Version

+- IRIX Network Forums (//forums.irixnet.org)
+-- Forum: SGI/MIPS (//forums.irixnet.org/forum-3.html)
+--- Forum: Development/Porting (//forums.irixnet.org/forum-9.html)
+--- Thread: Why the errors in SGI example graphics animation code? (/thread-2084.html)



Why the errors in SGI example graphics animation code? - KayBee - 04-27-2020

Hi Again Devs,

Learning graphics programming on my O2 has been a tough start. So I was excited to find some examples in this SGI document:

Graphics Library Programming Guide Volume I 
Document Number 007-1210-060

In it I found an animation example and typed it in:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <gl/gl.h>
#include <gl/device.h>
#define RGB_BLACK 0x000000
#define RGB_WHITE 0xffffff
#define X        0
#define Y        1
#define XY        2
#define SIZE    0.05
#define BOUNDS    (1.0 + SIZE)
#define EDGE    (1.1 * BOUNDS)

float boundary[4][XY] = {
    {-BOUNDS, -BOUNDS},
    {-BOUNDS, BOUNDS},
    { BOUNDS, BOUNDS},
    { BOUNDS, -BOUNDS}
};

struct ball_s {
    float pos[XY];
    float delta[XY];
    unsigned long col;
};

void main(int argc, char *argv[])
{
    int i, j;
    int nballs;
    struct ball_s *balls;
    short val;
    if (getgdesc(GD_BITS_NORM_DBL_RED) == 0) {
        fprintf(stderr,"Double buffered RGB not available on this machine\n");
        return 1;
    }
    if(argc!=2){
        fprintf(stderr, "Usage: bounce <ball count>");
        return 1;
    }
    nballs = atoi(argv[1]);
    if (!(balls = (struct ball_s *)malloc(nballs * sizeof(struct ball_s)))) {
    fprintf(stderr, "bounce: malloc failed\n");
    return 1;
    }
    for(i=0;i<nballs;i++){
        for(j=0;j<XY;j++){
            balls[i].pos[j] = 2.0 * (drand48() - 0.5);
            balls[i].delta[j] = 2.0 * (drand48() - 0.5) / 50.0;
        }
        balls[i].col = drand48() * 0xffffff;
    }
    prefsize(400, 400);
    winopen("bounce");
    doublebuffer();
    RGBmode();
    gconfig();
    shademodel(FLAT);
    qdevice(ESCKEY);
    ortho2(-EDGE, EDGE, -EDGE, EDGE);
    while (!(qtest() && qread(&val) == ESCKEY && val == 0)) {
        for(i=0;i<nballs;i++){
            for(j=0;j<XY;j++){
                balls[i].pos[j] += balls[i].delta[j];
                if ((balls[i].pos[j] >= 1.0) || (balls[i].pos[j] <= -1.0))
                    balls[i].delta[j] = -balls[i].delta[j];
                }
            }
            cpack(RGB_BLACK);
            clear();
            cpack(RGB_WHITE);
            bgnclosedline();
            for(i=0;i<4;i++)
                v2f(boundary[i]);
            endclosedline();
            for(i=0;i<nballs;i++){
                cpack(balls[i].col);
                sboxf(balls[i].pos[X]-SIZE, balls[i].pos[Y]-SIZE,
                        balls[i].pos[X]+SIZE, balls[i].pos[Y]+SIZE);
            }
            swapbuffers();
        }
        gexit();
        return 0;
    }


Compiling this code via: cc -n32 animation.c -lgl
yields 4 errors, each identifying one of the returns and the accompanying number and stating for example:
      return 1;
An expression appears after a "return" in a "void" function

Could you knowledgable folks give it a go and let me know what is going on?

Thanks as always.

KB


RE: Why the errors in SGI example graphics animation code? - nintendoeats - 04-27-2020

The main() function should return an int. When main() returns, the program ends. If it returns the value 0, that means the program executed successfully. Any other value indicates that it exited with an error.

void main() would indicate that the function returns nothing, which is not correct. Change it to int main() and you should be good.


RE: Why the errors in SGI example graphics animation code? - KayBee - 04-29-2020

As usual, N.E. you were right. Thank you for taking the time. Oddly, it compiles without error, and when I execute it, nada. It does nothing. The GL book is old (1992) and SGI transitioned to OpenGL, but testing with simple examples of GL, I was able draw boxes etc, so I thought this would work also. I will have to do some digging.

Cheers,

KB


RE: Why the errors in SGI example graphics animation code? - jpstewart - 04-30-2020

(04-29-2020, 02:26 AM)KayBee Wrote:  Oddly, it compiles without error, and when I execute it, nada. It does nothing.

How did you execute it?  From the command line it needs exactly one argument (the number of balls to animate) and will tell you so if you don't provide it.  But if you try to run it by clicking it's icon in the file manager GUI (for example) there's no way to provide the necessary argument and no way to see the error message.

If you're running it from the command line and it is still doing nothing, we'll have to look into the code a little more deeply....


RE: Why the errors in SGI example graphics animation code? - KayBee - 05-01-2020

Nice JP, that was it. Thank you for pointing that out, I missed the argument. It was so great to see the movement. On my R5K O2, I set it to 10, 100, then 1000. I'm very happy that it works because I am pulling it apart to learn from it.

Excellent, thanks again JP and N.E.

KB