Debian Codegraph @

tests/micro-bench/test-random-text.c

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