| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 
 | #include <stdio.h>
#include <cairo.h>
#include <gtk/gtk.h>
#include <glib.h>
 
gint pos_x=500,pos_y=500;
GtkWidget *window;
GtkWidget *drawarea; /* drawing area */
GRand *tirage; /* a random generator */
 
gint grid[1001][1001]; /* content of the grid  */
 
int main(int argc, char *argv[])
{
    gboolean on_expose_event(GtkWidget *widget, GdkEvent *event, gpointer userdata);
    gboolean update(gpointer);
    void OnDestroy(GtkWidget *pWidget, gpointer pData); /* function call back destroy */
    void initialise(int);
 
    gtk_init(&argc, &argv);
 
    /* main window creation */
    window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
 
    /* random generator creation */
    tirage=g_rand_new();
 
    /* drawing points in the grid */
    initialise(200);
 
    /* drawing area creation */
    drawarea=gtk_drawing_area_new();
    gtk_widget_set_size_request(drawarea, (gint)1200, (gint)1200);
 
    /* adding the drawarea to the window */
    gtk_container_add (GTK_CONTAINER(window), drawarea);
 
    g_signal_connect(G_OBJECT(drawarea), "expose_event", G_CALLBACK(on_expose_event), NULL);
    g_signal_connect(window, "destroy", G_CALLBACK(OnDestroy), NULL);
    g_timeout_add((guint)10, update, NULL); /* called function at regular intervals */
    gtk_widget_show_all(window);
    gtk_main();
    return 0;
}
 
void OnDestroy(GtkWidget *pWidget, gpointer pData)
{
    g_rand_free(tirage);
    gtk_main_quit();
}
 
void initialise(int food)
{
    int i,j,flag,tmp_x,tmp_y;
    for (i=1;i<=1000;i++)
    {
        for (j=1;j<=1000;j++)
        {
            grid[i][j]=0;
        }
    }
 
    /* drawing randomly locations in the grid */
    for (i=1;i<=food;i++)
    {
        flag=1;
        while (flag)
        {
             /* drawing an empty cell in the grid */
            tmp_x=(int)(g_rand_double(tirage)*1000.)+1;
            tmp_y=(int)(g_rand_double(tirage)*1000.)+1;
            if (grid[tmp_x][tmp_y]==0)
                flag=0;
        }
        grid[tmp_x][tmp_y]=1;
    }
}
gboolean on_expose_event(GtkWidget *widget, GdkEvent *event, gpointer userdata)
{
    int i, j;
    cairo_t *cr=NULL;
    cr=gdk_cairo_create(drawarea->window);
    cairo_set_source_rgb(cr, .0, .0, .0);
    cairo_rectangle(cr, 10.,10.,1000.,1000.);
 
    /* drawing  items in the grid  */
    for (i=1;i<=1000;i++)
    {
        for (j=1;j<=1000;j++)
        {
            if (grid[i][j]==1)
            {
                cairo_move_to(cr,(double)i+5.+10.,(double)j+10.);
                cairo_arc(cr,(double)i+10.,(double)j+10., 5., 0.,6.28);
            }
        }
    }
    cairo_stroke(cr);
    cairo_destroy(cr);
    return FALSE;
}
 
gboolean update(gpointer pData)
{
    /* invalidating a zone around pos_x, pos-y to force redrawing by the callback function link to expose-event */
    gtk_widget_queue_draw_area(drawarea, pos_x-40,pos_y-40,80,80);
    return TRUE;
} | 
Partager