2011-03-17 15:32:54 -04:00
|
|
|
#include <cogl/cogl.h>
|
|
|
|
#include <glib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2012-03-05 07:24:38 -05:00
|
|
|
typedef struct _Data
|
|
|
|
{
|
|
|
|
CoglContext *ctx;
|
|
|
|
CoglFramebuffer *fb;
|
|
|
|
CoglPrimitive *triangle;
|
|
|
|
CoglPipeline *pipeline;
|
2013-05-14 08:41:29 -04:00
|
|
|
|
|
|
|
unsigned int redraw_idle;
|
|
|
|
CoglBool is_dirty;
|
|
|
|
CoglBool draw_ready;
|
2012-03-05 07:24:38 -05:00
|
|
|
} Data;
|
|
|
|
|
2013-05-14 08:41:29 -04:00
|
|
|
static gboolean
|
2012-03-05 07:24:38 -05:00
|
|
|
paint_cb (void *user_data)
|
|
|
|
{
|
|
|
|
Data *data = user_data;
|
|
|
|
|
2013-05-14 08:41:29 -04:00
|
|
|
data->redraw_idle = 0;
|
|
|
|
data->is_dirty = FALSE;
|
|
|
|
data->draw_ready = FALSE;
|
|
|
|
|
2012-03-05 07:24:38 -05:00
|
|
|
cogl_framebuffer_clear4f (data->fb, COGL_BUFFER_BIT_COLOR, 0, 0, 0, 1);
|
2013-07-09 18:47:29 -04:00
|
|
|
cogl_primitive_draw (data->triangle,
|
|
|
|
data->fb,
|
|
|
|
data->pipeline);
|
2013-10-09 08:31:12 -04:00
|
|
|
cogl_onscreen_swap_buffers (data->fb);
|
2012-03-05 07:24:38 -05:00
|
|
|
|
2013-05-14 08:41:29 -04:00
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
maybe_redraw (Data *data)
|
|
|
|
{
|
|
|
|
if (data->is_dirty && data->draw_ready && data->redraw_idle == 0) {
|
|
|
|
/* We'll draw on idle instead of drawing immediately so that
|
|
|
|
* if Cogl reports multiple dirty rectangles we won't
|
|
|
|
* redundantly draw multiple frames */
|
|
|
|
data->redraw_idle = g_idle_add (paint_cb, data);
|
|
|
|
}
|
2012-03-05 07:24:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-01-29 11:08:36 -05:00
|
|
|
frame_event_cb (CoglOnscreen *onscreen,
|
|
|
|
CoglFrameEvent event,
|
|
|
|
CoglFrameInfo *info,
|
|
|
|
void *user_data)
|
2012-03-05 07:24:38 -05:00
|
|
|
{
|
2013-05-14 08:41:29 -04:00
|
|
|
Data *data = user_data;
|
|
|
|
|
|
|
|
if (event == COGL_FRAME_EVENT_SYNC) {
|
|
|
|
data->draw_ready = TRUE;
|
|
|
|
maybe_redraw (data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dirty_cb (CoglOnscreen *onscreen,
|
|
|
|
const CoglOnscreenDirtyInfo *info,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
Data *data = user_data;
|
|
|
|
|
|
|
|
data->is_dirty = TRUE;
|
|
|
|
maybe_redraw (data);
|
2012-03-05 07:24:38 -05:00
|
|
|
}
|
2011-07-15 07:31:21 -04:00
|
|
|
|
2011-03-17 15:32:54 -04:00
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
2012-03-05 07:24:38 -05:00
|
|
|
Data data;
|
2011-03-17 15:32:54 -04:00
|
|
|
CoglOnscreen *onscreen;
|
2012-08-31 14:28:27 -04:00
|
|
|
CoglError *error = NULL;
|
2011-03-17 15:32:54 -04:00
|
|
|
CoglVertexP2C4 triangle_vertices[] = {
|
2013-04-19 12:55:29 -04:00
|
|
|
{0, 0.7, 0xff, 0x00, 0x00, 0xff},
|
2011-03-17 15:32:54 -04:00
|
|
|
{-0.7, -0.7, 0x00, 0xff, 0x00, 0xff},
|
|
|
|
{0.7, -0.7, 0x00, 0x00, 0xff, 0xff}
|
|
|
|
};
|
2012-03-05 07:24:38 -05:00
|
|
|
GSource *cogl_source;
|
|
|
|
GMainLoop *loop;
|
2011-03-17 15:32:54 -04:00
|
|
|
|
2013-05-14 08:41:29 -04:00
|
|
|
data.redraw_idle = 0;
|
|
|
|
data.is_dirty = FALSE;
|
|
|
|
data.draw_ready = TRUE;
|
|
|
|
|
2012-03-05 07:24:38 -05:00
|
|
|
data.ctx = cogl_context_new (NULL, &error);
|
|
|
|
if (!data.ctx) {
|
2011-03-17 15:32:54 -04:00
|
|
|
fprintf (stderr, "Failed to create context: %s\n", error->message);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-03-05 07:24:38 -05:00
|
|
|
onscreen = cogl_onscreen_new (data.ctx, 640, 480);
|
2011-05-04 06:10:54 -04:00
|
|
|
cogl_onscreen_show (onscreen);
|
2013-10-09 08:31:12 -04:00
|
|
|
data.fb = onscreen;
|
2011-03-17 15:32:54 -04:00
|
|
|
|
2012-06-20 11:22:36 -04:00
|
|
|
cogl_onscreen_set_resizable (onscreen, TRUE);
|
|
|
|
|
2012-03-05 07:24:38 -05:00
|
|
|
data.triangle = cogl_primitive_new_p2c4 (data.ctx,
|
|
|
|
COGL_VERTICES_MODE_TRIANGLES,
|
|
|
|
3, triangle_vertices);
|
|
|
|
data.pipeline = cogl_pipeline_new (data.ctx);
|
2012-01-07 21:59:04 -05:00
|
|
|
|
2012-03-05 07:24:38 -05:00
|
|
|
cogl_source = cogl_glib_source_new (data.ctx, G_PRIORITY_DEFAULT);
|
2012-01-07 21:59:04 -05:00
|
|
|
|
2012-03-05 07:24:38 -05:00
|
|
|
g_source_attach (cogl_source, NULL);
|
2011-12-19 10:40:55 -05:00
|
|
|
|
2013-10-09 08:31:12 -04:00
|
|
|
cogl_onscreen_add_frame_callback (data.fb,
|
2013-01-29 11:08:36 -05:00
|
|
|
frame_event_cb,
|
|
|
|
&data,
|
|
|
|
NULL); /* destroy notify */
|
2013-10-09 08:31:12 -04:00
|
|
|
cogl_onscreen_add_dirty_callback (data.fb,
|
2013-05-14 08:41:29 -04:00
|
|
|
dirty_cb,
|
|
|
|
&data,
|
|
|
|
NULL); /* destroy notify */
|
2012-03-05 07:24:38 -05:00
|
|
|
|
|
|
|
loop = g_main_loop_new (NULL, TRUE);
|
|
|
|
g_main_loop_run (loop);
|
2011-03-17 15:32:54 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|