Difficulties in making the g2 library work in C code

0

I have this code in which the following errors appear in the terminal, and the goal is to be shown with the g2 library:

codigocanvas.c: In function ‘world_canvas_new’:

codigocanvas.c:22:33: error: expected identifier before ‘(’ token

     *device = g2_open_X11(wrld->(sw->xdim) * 10, wrld->(sw->ydim) * 1

                                 ^
codigocanvas.c:22:15: error: too few arguments to function ‘g2_open_X11’

     *device = g2_open_X11(wrld->(sw->xdim) * 10, wrld->(sw->ydim) * 1

               ^
In file included from codigocanvas.c:5:0:

/usr/include/g2_X11.h:28:5: note: declared here

 int g2_open_X11(int width, int height);

     ^
codigocanvas.c:16:36: warning: parameter ‘wrld’ set but not used [-Wunused-

but-set-parameter]

 CANVAS world_canvas_new(SHOWWORLD *wrld) {

                                    ^
codigocanvas.c: In function ‘world_canvas_update’:

codigocanvas.c:35:38: error: dereferencing pointer to incomplete type

‘SHOWWORLD {aka struct showworld}’

     for (unsigned int y = 0; y < wrld->ydim; ++y) {

                                      ^
codigocanvas.c:38:34: warning: implicit declaration of function ‘world_get’

 [-Wimplicit-function-declaration]

             AGENT *a = (AGENT *) world_get(wrld, x, y);

                                  ^
codigocanvas.c:38:24: warning: cast to pointer from integer of different size

 [-Wint-to-pointer-cast]

             AGENT *a = (AGENT *) world_get(wrld, x, y);

                        ^
<builtin>: recipe for target 'codigocanvas.o' failed

make: *** [codigocanvas.o] Error 1

    #include "codigocanvas.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <g2.h>
    #include <g2_X11.h> 
    #include "example.c"

    /* Cores que se vão utilizar */
    enum g2_colors {
        WHITE = 0,
        BLACK = 1,
        GREEN = 7,
        YELLOW = 25
    };

    CANVAS world_canvas_new(SHOWWORLD *wrld) {

        int *device;
        device = malloc(sizeof(int));

        /* Abre janela com g2 */
        *device = g2_open_X11(wrld->xdim * 10, wrld->ydim * 10);

        /* Especificar tamanho e forma dos agentes */
        g2_set_QP(*device, 25, QPrect);

        return (CANVAS) device;
    }
        /* Desenhar os zombies e os humanos  */
    void world_canvas_update(CANVAS cnvs, SHOWWORLD *wrld) {

        int device = *((int *) cnvs);
        g2_clear(device);

        for (unsigned int y = 0; y < wrld->ydim; ++y) {
            for (unsigned int x = 0; x < wrld->xdim; ++x) {

                AGENT *a = (AGENT *) world_get(wrld, x, y);

                if (a != NULL) {
                    switch (a->type) {
                        case Zombie:
                            g2_pen(device, GREEN);
                            g2_plot_QP(device, x, y);
                            break;
                        case Human:
                            g2_pen(device, YELLOW);
                            g2_plot_QP(device, x, y);
                            break;

                        default:
                            /* Se der erro esta mensagem aparece */
                            fprintf(stderr, "Agente desconhecido!");
                    }
                }
            }
        }

    }

    void world_canvas_destroy(CANVAS cnvs) {

        g2_close(*((int *) cnvs));
        free(cnvs);

    }
    
asked by anonymous 29.01.2018 / 17:20

1 answer

0

The wrld->(sw->xdim) syntax is invalid. The compiler is expecting a function name before the parentheses, because it thinks sw->xdim is the argument of a function call that comes after the arrow.

Switch by (*wrld).(sw->xdim) and at least this line will compile. You may have other errors after that, of course.

    
29.01.2018 / 17:55