2011-03-17 19:32:54 +00:00
|
|
|
#include <cogl/cogl.h>
|
|
|
|
#include <glib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2012-03-05 12:24:38 +00:00
|
|
|
typedef struct _Data
|
|
|
|
{
|
|
|
|
CoglContext *ctx;
|
|
|
|
CoglFramebuffer *fb;
|
|
|
|
CoglPrimitive *triangle;
|
|
|
|
CoglPipeline *pipeline;
|
2013-05-14 12:41:29 +00:00
|
|
|
|
|
|
|
unsigned int redraw_idle;
|
|
|
|
CoglBool is_dirty;
|
|
|
|
CoglBool draw_ready;
|
2012-03-05 12:24:38 +00:00
|
|
|
} Data;
|
|
|
|
|
2013-05-14 12:41:29 +00:00
|
|
|
static gboolean
|
2012-03-05 12:24:38 +00:00
|
|
|
paint_cb (void *user_data)
|
|
|
|
{
|
|
|
|
Data *data = user_data;
|
|
|
|
|
2013-05-14 12:41:29 +00:00
|
|
|
data->redraw_idle = 0;
|
|
|
|
data->is_dirty = FALSE;
|
|
|
|
data->draw_ready = FALSE;
|
|
|
|
|
2012-03-05 12:24:38 +00:00
|
|
|
cogl_framebuffer_clear4f (data->fb, COGL_BUFFER_BIT_COLOR, 0, 0, 0, 1);
|
2013-05-14 12:41:29 +00:00
|
|
|
cogl_framebuffer_draw_primitive (data->fb,
|
|
|
|
data->pipeline,
|
|
|
|
data->triangle);
|
2012-03-05 12:24:38 +00:00
|
|
|
cogl_onscreen_swap_buffers (COGL_ONSCREEN (data->fb));
|
|
|
|
|
2013-05-14 12:41:29 +00: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 12:24:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-01-29 16:08:36 +00:00
|
|
|
frame_event_cb (CoglOnscreen *onscreen,
|
|
|
|
CoglFrameEvent event,
|
|
|
|
CoglFrameInfo *info,
|
|
|
|
void *user_data)
|
2012-03-05 12:24:38 +00:00
|
|
|
{
|
2013-05-14 12:41:29 +00: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 12:24:38 +00:00
|
|
|
}
|
2011-07-15 11:31:21 +00:00
|
|
|
|
2011-03-17 19:32:54 +00:00
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
2012-03-05 12:24:38 +00:00
|
|
|
Data data;
|
2011-03-17 19:32:54 +00:00
|
|
|
CoglOnscreen *onscreen;
|
2012-08-31 18:28:27 +00:00
|
|
|
CoglError *error = NULL;
|
2011-03-17 19:32:54 +00:00
|
|
|
CoglVertexP2C4 triangle_vertices[] = {
|
2013-04-19 16:55:29 +00:00
|
|
|
{0, 0.7, 0xff, 0x00, 0x00, 0xff},
|
2011-03-17 19:32:54 +00:00
|
|
|
{-0.7, -0.7, 0x00, 0xff, 0x00, 0xff},
|
|
|
|
{0.7, -0.7, 0x00, 0x00, 0xff, 0xff}
|
|
|
|
};
|
2012-03-05 12:24:38 +00:00
|
|
|
GSource *cogl_source;
|
|
|
|
GMainLoop *loop;
|
2011-03-17 19:32:54 +00:00
|
|
|
|
2013-05-14 12:41:29 +00:00
|
|
|
data.redraw_idle = 0;
|
|
|
|
data.is_dirty = FALSE;
|
|
|
|
data.draw_ready = TRUE;
|
|
|
|
|
2012-03-05 12:24:38 +00:00
|
|
|
data.ctx = cogl_context_new (NULL, &error);
|
|
|
|
if (!data.ctx) {
|
2011-03-17 19:32:54 +00:00
|
|
|
fprintf (stderr, "Failed to create context: %s\n", error->message);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-03-05 12:24:38 +00:00
|
|
|
onscreen = cogl_onscreen_new (data.ctx, 640, 480);
|
2011-05-04 10:10:54 +00:00
|
|
|
cogl_onscreen_show (onscreen);
|
2012-03-05 12:24:38 +00:00
|
|
|
data.fb = COGL_FRAMEBUFFER (onscreen);
|
2011-03-17 19:32:54 +00:00
|
|
|
|
2012-06-20 15:22:36 +00:00
|
|
|
cogl_onscreen_set_resizable (onscreen, TRUE);
|
|
|
|
|
2012-03-05 12:24:38 +00: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-08 02:59:04 +00:00
|
|
|
|
2012-03-05 12:24:38 +00:00
|
|
|
cogl_source = cogl_glib_source_new (data.ctx, G_PRIORITY_DEFAULT);
|
2012-01-08 02:59:04 +00:00
|
|
|
|
2012-03-05 12:24:38 +00:00
|
|
|
g_source_attach (cogl_source, NULL);
|
2011-12-19 15:40:55 +00:00
|
|
|
|
2013-01-29 16:08:36 +00:00
|
|
|
cogl_onscreen_add_frame_callback (COGL_ONSCREEN (data.fb),
|
|
|
|
frame_event_cb,
|
|
|
|
&data,
|
|
|
|
NULL); /* destroy notify */
|
2013-05-14 12:41:29 +00:00
|
|
|
cogl_onscreen_add_dirty_callback (COGL_ONSCREEN (data.fb),
|
|
|
|
dirty_cb,
|
|
|
|
&data,
|
|
|
|
NULL); /* destroy notify */
|
2012-03-05 12:24:38 +00:00
|
|
|
|
|
|
|
loop = g_main_loop_new (NULL, TRUE);
|
|
|
|
g_main_loop_run (loop);
|
2011-03-17 19:32:54 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|