~darkxst/ubuntu/raring/cogl/lp1163025

« back to all changes in this revision

Viewing changes to examples/cogl-hello.c

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2012-03-13 19:11:11 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20120313191111-3hgk529qkh9m6uk2
Tags: 1.9.8-0ubuntu1
* New upstream release (LP: #941617)
* Updated symbols & library name for soname update
* debian/control.in: Bump minimum glib to 2.28
* debian/patches/02_disable_armv5t_specific_optimization.patch: Disabled

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <cogl/cogl.h>
 
2
#include <glib.h>
 
3
#include <stdio.h>
 
4
 
 
5
typedef struct _Data
 
6
{
 
7
    CoglContext *ctx;
 
8
    CoglFramebuffer *fb;
 
9
    CoglPrimitive *triangle;
 
10
    CoglPipeline *pipeline;
 
11
} Data;
 
12
 
 
13
static gboolean
 
14
paint_cb (void *user_data)
 
15
{
 
16
    Data *data = user_data;
 
17
 
 
18
    cogl_framebuffer_clear4f (data->fb, COGL_BUFFER_BIT_COLOR, 0, 0, 0, 1);
 
19
    cogl_framebuffer_draw_primitive (data->fb, data->pipeline, data->triangle);
 
20
    cogl_onscreen_swap_buffers (COGL_ONSCREEN (data->fb));
 
21
 
 
22
    /* If the driver can deliver swap complete events then we can remove
 
23
     * the idle paint callback until we next get a swap complete event
 
24
     * otherwise we keep the idle paint callback installed and simply
 
25
     * paint as fast as the driver will allow... */
 
26
    if (cogl_has_feature (data->ctx, COGL_FEATURE_ID_SWAP_BUFFERS_EVENT))
 
27
      return FALSE; /* remove the callback */
 
28
    else
 
29
      return TRUE;
 
30
}
 
31
 
 
32
static void
 
33
swap_complete_cb (CoglFramebuffer *framebuffer, void *user_data)
 
34
{
 
35
    g_idle_add (paint_cb, user_data);
 
36
}
 
37
 
 
38
int
 
39
main (int argc, char **argv)
 
40
{
 
41
    Data data;
 
42
    CoglOnscreen *onscreen;
 
43
    GError *error = NULL;
 
44
    CoglVertexP2C4 triangle_vertices[] = {
 
45
        {0, 0.7, 0xff, 0x00, 0x00, 0x80},
 
46
        {-0.7, -0.7, 0x00, 0xff, 0x00, 0xff},
 
47
        {0.7, -0.7, 0x00, 0x00, 0xff, 0xff}
 
48
    };
 
49
    GSource *cogl_source;
 
50
    GMainLoop *loop;
 
51
 
 
52
    data.ctx = cogl_context_new (NULL, &error);
 
53
    if (!data.ctx) {
 
54
        fprintf (stderr, "Failed to create context: %s\n", error->message);
 
55
        return 1;
 
56
    }
 
57
 
 
58
    onscreen = cogl_onscreen_new (data.ctx, 640, 480);
 
59
    cogl_onscreen_show (onscreen);
 
60
    data.fb = COGL_FRAMEBUFFER (onscreen);
 
61
 
 
62
    data.triangle = cogl_primitive_new_p2c4 (data.ctx,
 
63
                                             COGL_VERTICES_MODE_TRIANGLES,
 
64
                                             3, triangle_vertices);
 
65
    data.pipeline = cogl_pipeline_new (data.ctx);
 
66
 
 
67
    cogl_source = cogl_glib_source_new (data.ctx, G_PRIORITY_DEFAULT);
 
68
 
 
69
    g_source_attach (cogl_source, NULL);
 
70
 
 
71
    if (cogl_has_feature (data.ctx, COGL_FEATURE_ID_SWAP_BUFFERS_EVENT))
 
72
      cogl_onscreen_add_swap_buffers_callback (COGL_ONSCREEN (data.fb),
 
73
                                               swap_complete_cb, &data);
 
74
 
 
75
    g_idle_add (paint_cb, &data);
 
76
 
 
77
    loop = g_main_loop_new (NULL, TRUE);
 
78
    g_main_loop_run (loop);
 
79
 
 
80
    return 0;
 
81
}