Advertisement
Guest User

Untitled

a guest
Jul 18th, 2015
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Vala 2.44 KB | None | 0 0
  1. /* cogl-hello by CYB3R */
  2.  
  3. using Cogl;
  4.  
  5. class Hello {
  6.     public static Context ctx;
  7.     public static Framebuffer fb;
  8.     public static Primitive triangle;
  9.     public static Pipeline pipeline;
  10.  
  11.     public static uint redraw_idle;
  12.     public static bool is_dirty;
  13.     public static bool draw_ready;
  14.  
  15.     public static const VertexP2C4 triangle_vertices[] = {
  16.         { 0, 0.7f, 0xff, 0x00, 0x00, 0xff },
  17.         { -0.7f, -0.7f, 0x00, 0xff, 0x00, 0xff },
  18.         { 0.7f, -0.7f, 0x00, 0x00, 0xff, 0xff }
  19.     };
  20.  
  21.     public static void maybe_redraw(){
  22.         if (is_dirty && draw_ready && redraw_idle == 0) {
  23.             /* We'll draw on idle instead of drawing immediately so that
  24.              * if Cogl reports multiple dirty rectangles we won't
  25.              * redundantly draw multiple frames */
  26.             redraw_idle = Idle.add(() => {
  27.                     redraw_idle = 0;
  28.                     is_dirty = false;
  29.                     draw_ready = false;
  30.  
  31.                     fb.clear4f(BufferBit.COLOR, 0, 0, 0, 1);
  32.                     triangle.draw(fb, pipeline);
  33.                     ((Onscreen)fb).swap_buffers();
  34.  
  35.                     return false;
  36.                 });
  37.         }
  38.     }
  39.  
  40.     public static int main(string[] args) {
  41.         Onscreen onscreen;
  42.  
  43.         Source cogl_source;
  44.         MainLoop loop;
  45.  
  46.         redraw_idle = 0;
  47.         is_dirty = false;
  48.         draw_ready = true;
  49.  
  50.         try {
  51.             ctx = new Context(null);
  52.         } catch (Error e) {
  53.             stderr.printf("Failed to create context: %s\n", e.message);
  54.             return 1;
  55.         }
  56.  
  57.         onscreen = new Onscreen(ctx, 640, 480);
  58.         onscreen.show();
  59.         fb = onscreen;
  60.  
  61.         onscreen.set_resizable((Bool)true);
  62.  
  63.         triangle = new Primitive.p2c4(ctx, VerticesMode.TRIANGLES, triangle_vertices);
  64.         pipeline = new Pipeline(ctx);
  65.  
  66.         cogl_source = glib_source_new(ctx, Priority.DEFAULT);
  67.         cogl_source.attach(null);
  68.  
  69.         ((Onscreen)fb).add_frame_callback((onscreen, event, info) => {
  70.                 if (event == FrameEvent.SYNC) {
  71.                     draw_ready = true;
  72.                     maybe_redraw();
  73.                 }
  74.             }, null);
  75.         ((Onscreen)fb).add_dirty_callback((onscreen, info) => {
  76.                 is_dirty = true;
  77.                 maybe_redraw();
  78.             }, null);
  79.  
  80.         loop = new MainLoop(null, true);
  81.         loop.run();
  82.  
  83.         return 0;
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement