1

I'm writing a program to read a file to get the size, mass, position etc about the sun and planets. I can get the planets drawn as circles in an Xwindow but I can't get the sleep function to work. I just want to make the planets show up for five seconds then disappear to show just black, but when I try to call sleep and run the program I never see the planets in the window. It just starts out as a black screen. Am I just using sleep wrong or is there something I need to add? Or am I missing the point entirely?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <plot.h>

struct body {
    char color[10];
    char size[4];
    char mass[10];
    char xpos[10];
    char ypos[10];
};

int main()
{    
    /* just drawing the first five for now */
    struct body sun;
    struct body mercury;
    struct body venus;
    struct body earth;
    struct body mars;

    /* not gonna show code for reading file and assigning values to the struct
     * for the space
    */

    plPlotter *plotter;
    plPlotterParams *plotterParams;

    plotterParams = pl_newplparams();
    pl_setplparam(plotterParams, "BITMAPSIZE", "750x750");
    pl_setplparam(plotterParams, "USE_DOUBLE_BUFFERING", "no");
    pl_setplparam(plotterParams, "BG_COLOR", "black");

    if ((plotter = pl_newpl_r("X", stdin, stdout, stderr, plotterParams)) == NULL) {
        fprint(stderr, "Couldn't create Plotter\n");
        exit(1);
    } else if(pl_openpl_r(plotter) < 0) {
         fprintf(stderr, "Couldn't open Xwindows Plotter\n");
         exit(1);
    }

    pl_fspace_r(plotter, -2500, -2500, 2500, 2500);

    pl_pentype_r(plotter, 1);
    pl_filltype_r(plotter, 1);

    pl_pencolorname_r(plotter, "black");

    /* draw the sun */
    pl_fillcolorname_r(plotter, sun.color);
    pl_fcircle_r(plotter, atof(sun.xpos), atof(sun.ypos), atof(sun.size));

    /* I do this same thing 4 more times but with 
     * mercury, venus, earth, and mars
    */

    sleep(5); /* try to wait 5 seconds then sleep */

    /* close and cleanup */
    if (pl_close_pl_r(plotter) < 0) {
        fprintf(stderr, "Couldnt delete plotter\n");
    }
    else if (pl_deletepl_r(plotter) < 0) {
        fprintf(stderr, "Couldnt delete plotter\n");
    }

    return 0; 
}
Josh
  • 41
  • 7
  • 1
    Well, first include a proper header file for `sleep()` and fix the other compiler warnings and errors. And for the next question: "gnuplot" != "GNU libplot" ;) – Christoph Mar 06 '14 at 23:10
  • @Christoph I don't have any compiler warnings. – Josh Mar 07 '14 at 00:50
  • Do you have any idea of how to make the circles show up then wait for some period of time then have the screen go black? – Josh Mar 07 '14 at 01:31
  • also what header do I need for sleep()? – Josh Mar 07 '14 at 01:33
  • 1
    I'm surprised you didn't have any compiler warnings before I added the opening `{` to your main function. See this question regarding the `sleep` function http://stackoverflow.com/q/14818084/2088135 – Tom Fenech Mar 07 '14 at 08:11
  • See `man 3 sleep`. Besides the missing `{` the function `pl_close_pl_r` isn't known (must be `pl_closepl_r`), you do not set a sun, which is reported as `libplot: substituting "black" for undefined fill color ""`, because `sun.color` is empty etc. – Christoph Mar 07 '14 at 08:27

0 Answers0