File: clock.c

package info (click to toggle)
clutter-cairo 0.8.2-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,520 kB
  • ctags: 107
  • sloc: sh: 9,120; ansic: 883; makefile: 31
file content (118 lines) | stat: -rw-r--r-- 3,037 bytes parent folder | download
1
2
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
110
111
112
113
114
115
116
117
118
#include <stdlib.h>
#include <math.h>

#include <clutter/clutter.h>

#include "clutter-cairo.h"

void 
drawClock(cairo_t * cr)
{
  /* clock drawing code from http://www.cairographics.org/SDLCLock */

  /* store the current time */
  time_t rawtime;
  struct tm *timeinfo;
  double minutes, hours, seconds;

  time(&rawtime);
  
  /* In newer versions of Visual Studio localtime(..) is deprecated. */
  /* Use localtime_s instead. See MSDN. */
  timeinfo = localtime (&rawtime);

  /* compute the angles for the indicators of our clock */
  minutes = timeinfo->tm_min * G_PI / 30;
  hours = timeinfo->tm_hour * G_PI / 6;
  seconds = timeinfo->tm_sec * G_PI / 30;

  /* Clear our surface */
  cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
  cairo_paint(cr);

  cairo_set_operator (cr, CAIRO_OPERATOR_OVER);

  /* who doesn't want all those nice line settings :) */
  cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
  cairo_set_line_width(cr, 0.1);

  /* translate to the center of the rendering context and draw a black */
  /* clock outline */
  cairo_set_source_rgba(cr, 0, 0, 0, 1);
  cairo_translate(cr, 0.5, 0.5);
  cairo_arc(cr, 0, 0, 0.4, 0, G_PI * 2);
  cairo_stroke(cr);

  /* draw a white dot on the current second. */
  cairo_set_source_rgba(cr, 1, 1, 1, 0.6);
  cairo_arc(cr, sin(seconds) * 0.4, -cos(seconds) * 0.4, 0.05, 0,
	    G_PI * 2);
  cairo_fill(cr);

  /* draw the minutes indicator */
  cairo_set_source_rgba(cr, 0.2, 0.2, 1, 0.6);
  cairo_move_to(cr, 0, 0);
  cairo_line_to(cr, sin(minutes) * 0.4, -cos(minutes) * 0.4);
  cairo_stroke(cr);

  /* draw the hours indicator      */
  cairo_move_to(cr, 0, 0);
  cairo_line_to(cr, sin(hours) * 0.2, -cos(hours) * 0.2);
  cairo_stroke(cr);
}

gboolean 
tick (gpointer data)
{
  ClutterActor *actor = data;
  ClutterActor *stage;
  cairo_t *cr;

  stage = clutter_stage_get_default ();
  cr = clutter_cairo_create (CLUTTER_CAIRO (actor));

  cairo_scale (cr,
               clutter_actor_get_width (actor),
               clutter_actor_get_height (actor));
  
  drawClock (cr);
  cairo_destroy (cr);

  return TRUE;
}

int
main (int argc, char **argv)
{
  ClutterActor    *ctex, *stage;
  ClutterColor     stage_color = { 0x99, 0xcc, 0xff, 0xff };

  clutter_init (&argc, &argv);

  stage = clutter_stage_get_default ();

  g_signal_connect (stage, "button-press-event",
                    G_CALLBACK (clutter_main_quit),
                    NULL);

  clutter_stage_set_color (CLUTTER_STAGE (stage),
		           &stage_color);

  ctex = clutter_cairo_new (300, 300);
  clutter_actor_set_position (ctex,
                              (clutter_actor_get_width (stage) -
                               clutter_actor_get_width (ctex)) / 2,
                              (clutter_actor_get_height (stage) -
                               clutter_actor_get_height (ctex)) / 2);
  clutter_container_add (CLUTTER_CONTAINER (stage), ctex, NULL);

  tick (ctex);

  g_timeout_add_seconds (1, tick, ctex);

  clutter_actor_show (stage);

  clutter_main();

  return EXIT_SUCCESS;
}