1 #include <gmodule.h>
2 #include <clutter/clutter.h>
3 #include <stdlib.h>
4 
5 #include "tests/clutter-test-utils.h"
6 
7 #define MAX_TEXT_LEN  10
8 #define MIN_FONT_SIZE 10
9 #define MAX_FONT_SIZE 30
10 
11 static const char * const font_names[] =
12   {
13     "Sans", "Sans Italic", "Serif", "Serif Bold", "Times", "Monospace"
14   };
15 #define FONT_NAME_COUNT 6
16 
17 static gboolean
on_idle(gpointer data)18 on_idle (gpointer data)
19 {
20   ClutterActor *stage = CLUTTER_ACTOR (data);
21   int line_height = 0, xpos = 0, ypos = 0;
22   int stage_width = clutter_actor_get_width (stage);
23   int stage_height = clutter_actor_get_height (stage);
24   char text[MAX_TEXT_LEN + 1];
25   char font_name[64];
26   int i;
27   GList *children, *node;
28   static GTimer *timer = NULL;
29   static int frame_count = 0;
30 
31   /* Remove all of the children of the stage */
32   children = clutter_container_get_children (CLUTTER_CONTAINER (stage));
33   for (node = children; node; node = node->next)
34     clutter_container_remove_actor (CLUTTER_CONTAINER (stage),
35                                     CLUTTER_ACTOR (node->data));
36   g_list_free (children);
37 
38   /* Fill the stage with new random labels */
39   while (ypos < stage_height)
40     {
41       int text_len = rand () % MAX_TEXT_LEN + 1;
42       ClutterActor *label;
43 
44       for (i = 0; i < text_len; i++)
45         text[i] = rand () % (128 - 32) + 32;
46       text[text_len] = '\0';
47 
48       sprintf (font_name, "%s %i",
49                font_names[rand () % FONT_NAME_COUNT],
50                rand () % (MAX_FONT_SIZE - MIN_FONT_SIZE) + MIN_FONT_SIZE);
51 
52       label = clutter_text_new_with_text (font_name, text);
53 
54       if (clutter_actor_get_height (label) > line_height)
55         line_height = clutter_actor_get_height (label);
56 
57       if (xpos + clutter_actor_get_width (label) > stage_width)
58         {
59           xpos = 0;
60           ypos += line_height;
61           line_height = 0;
62         }
63 
64       clutter_actor_set_position (label, xpos, ypos);
65 
66       clutter_container_add (CLUTTER_CONTAINER (stage), label, NULL);
67 
68       xpos += clutter_actor_get_width (label);
69     }
70 
71   if (timer == NULL)
72     timer = g_timer_new ();
73   else
74     {
75       if (++frame_count >= 10)
76         {
77           printf ("10 frames in %f seconds\n",
78                   g_timer_elapsed (timer, NULL));
79           g_timer_start (timer);
80           frame_count = 0;
81         }
82     }
83 
84   return TRUE;
85 }
86 
87 int
main(int argc,char * argv[])88 main (int argc, char *argv[])
89 {
90   ClutterActor *stage;
91 
92   clutter_test_init (&argc, &argv);
93 
94   stage = clutter_test_get_stage ();
95   clutter_stage_set_title (CLUTTER_STAGE (stage), "Random Text");
96 
97   clutter_actor_show (stage);
98 
99   clutter_threads_add_idle (on_idle, stage);
100 
101   clutter_test_main ();
102 
103   clutter_actor_destroy (stage);
104 
105   return 0;
106 }
107