2012-09-18 22:21:08 +00:00
|
|
|
/* -*- mode: C; c-basic-offset: 2; indent-tabs-mode: nil; -*- */
|
|
|
|
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
#include <math.h>
|
2012-11-14 22:54:03 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2013-04-21 22:31:36 +00:00
|
|
|
#include "frame-stats.h"
|
2012-09-18 22:21:08 +00:00
|
|
|
|
2012-11-14 22:24:02 +00:00
|
|
|
#define RADIUS 64
|
|
|
|
#define DIAMETER (2*RADIUS)
|
2012-09-18 22:21:08 +00:00
|
|
|
#define WIDTH 600
|
|
|
|
#define HEIGHT 600
|
|
|
|
#define WINDOW_SIZE_JITTER 200
|
|
|
|
#define CYCLE_TIME 5.
|
|
|
|
|
|
|
|
static GtkWidget *window;
|
|
|
|
static int window_width = WIDTH, window_height = HEIGHT;
|
|
|
|
|
2012-12-19 17:03:16 +00:00
|
|
|
gint64 start_frame_time;
|
2012-09-18 22:21:08 +00:00
|
|
|
static double angle;
|
|
|
|
|
|
|
|
static double load_factor = 1.0;
|
|
|
|
static double cb_no_resize = FALSE;
|
|
|
|
|
|
|
|
static cairo_surface_t *source_surface;
|
|
|
|
|
|
|
|
static void
|
|
|
|
ensure_resources(cairo_surface_t *target)
|
|
|
|
{
|
|
|
|
cairo_t *cr;
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
if (source_surface != NULL)
|
|
|
|
return;
|
|
|
|
|
2012-11-14 22:24:02 +00:00
|
|
|
source_surface = cairo_surface_create_similar (target, CAIRO_CONTENT_COLOR_ALPHA,
|
|
|
|
16 * DIAMETER, 16 * DIAMETER);
|
2012-09-18 22:21:08 +00:00
|
|
|
cr = cairo_create(source_surface);
|
|
|
|
|
|
|
|
cairo_save(cr);
|
|
|
|
cairo_set_source_rgba(cr, 0, 0, 0, 0);
|
|
|
|
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
|
|
|
|
cairo_paint(cr);
|
|
|
|
cairo_restore(cr);
|
|
|
|
|
|
|
|
cairo_set_line_width(cr, 1.0);
|
|
|
|
|
|
|
|
for (j = 0; j < 16; j++)
|
|
|
|
for (i = 0; i < 16; i++)
|
|
|
|
{
|
|
|
|
cairo_set_source_rgba(cr,
|
|
|
|
((i * 41) % 16) / 15.,
|
|
|
|
((i * 31) % 16) / 15.,
|
|
|
|
((i * 23) % 16) / 15.,
|
|
|
|
0.25);
|
|
|
|
cairo_arc(cr,
|
2012-11-14 22:24:02 +00:00
|
|
|
i * DIAMETER + RADIUS, j * DIAMETER + RADIUS,
|
|
|
|
RADIUS - 0.5, 0, 2 * M_PI);
|
2012-09-18 22:21:08 +00:00
|
|
|
cairo_fill_preserve(cr);
|
|
|
|
cairo_set_source_rgba(cr,
|
|
|
|
((i * 41) % 16) / 15.,
|
|
|
|
((i * 31) % 16) / 15.,
|
|
|
|
((i * 23) % 16) / 15.,
|
|
|
|
1.0);
|
|
|
|
cairo_stroke(cr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-18 14:02:49 +00:00
|
|
|
static void
|
|
|
|
on_draw (GtkDrawingArea *da,
|
|
|
|
cairo_t *cr,
|
|
|
|
int width,
|
|
|
|
int height,
|
|
|
|
gpointer data)
|
2012-09-18 22:21:08 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
GRand *rand = g_rand_new_with_seed(0);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
ensure_resources (cairo_get_target (cr));
|
|
|
|
|
|
|
|
cairo_set_source_rgb(cr, 1, 1, 1);
|
|
|
|
cairo_paint(cr);
|
|
|
|
|
|
|
|
cairo_set_source_rgb(cr, 0, 0, 0);
|
|
|
|
cairo_set_line_width(cr, 1.0);
|
|
|
|
cairo_rectangle (cr, 0.5, 0.5, width - 1, height - 1);
|
|
|
|
cairo_stroke (cr);
|
|
|
|
|
|
|
|
for(i = 0; i < load_factor * 150; i++)
|
|
|
|
{
|
|
|
|
int source = g_rand_int_range(rand, 0, 255);
|
|
|
|
double phi = g_rand_double_range(rand, 0, 2 * M_PI) + angle;
|
2012-11-14 22:24:02 +00:00
|
|
|
double r = g_rand_double_range(rand, 0, width / 2 - RADIUS);
|
2012-09-18 22:21:08 +00:00
|
|
|
int x, y;
|
|
|
|
|
2012-11-14 22:24:02 +00:00
|
|
|
int source_x = (source % 16) * DIAMETER;
|
|
|
|
int source_y = (source / 16) * DIAMETER;
|
2012-09-18 22:21:08 +00:00
|
|
|
|
2012-11-14 22:24:02 +00:00
|
|
|
x = round(width / 2 + r * cos(phi) - RADIUS);
|
|
|
|
y = round(height / 2 - r * sin(phi) - RADIUS);
|
2012-09-18 22:21:08 +00:00
|
|
|
|
|
|
|
cairo_set_source_surface(cr, source_surface,
|
|
|
|
x - source_x, y - source_y);
|
2012-11-14 22:24:02 +00:00
|
|
|
cairo_rectangle(cr, x, y, DIAMETER, DIAMETER);
|
2012-09-18 22:21:08 +00:00
|
|
|
cairo_fill(cr);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_rand_free(rand);
|
|
|
|
}
|
|
|
|
|
2012-11-14 22:54:03 +00:00
|
|
|
static void
|
2012-12-19 17:03:16 +00:00
|
|
|
on_frame (double progress)
|
2012-11-14 22:54:03 +00:00
|
|
|
{
|
|
|
|
int jitter;
|
|
|
|
|
2012-09-18 22:21:08 +00:00
|
|
|
angle = 2 * M_PI * progress;
|
|
|
|
jitter = WINDOW_SIZE_JITTER * sin(angle);
|
|
|
|
|
|
|
|
if (!cb_no_resize)
|
|
|
|
{
|
|
|
|
window_width = WIDTH + jitter;
|
|
|
|
window_height = HEIGHT + jitter;
|
|
|
|
}
|
|
|
|
|
|
|
|
gtk_window_resize (GTK_WINDOW (window),
|
|
|
|
window_width, window_height);
|
|
|
|
|
|
|
|
gtk_widget_queue_draw (window);
|
|
|
|
}
|
|
|
|
|
2012-12-19 17:03:16 +00:00
|
|
|
static gboolean
|
|
|
|
tick_callback (GtkWidget *widget,
|
|
|
|
GdkFrameClock *frame_clock,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
gint64 frame_time = gdk_frame_clock_get_frame_time (frame_clock);
|
|
|
|
double scaled_time;
|
|
|
|
|
|
|
|
if (start_frame_time == 0)
|
|
|
|
start_frame_time = frame_time;
|
|
|
|
|
|
|
|
scaled_time = (frame_time - start_frame_time) / (CYCLE_TIME * 1000000);
|
|
|
|
on_frame (scaled_time - floor (scaled_time));
|
|
|
|
|
2013-02-14 21:24:27 +00:00
|
|
|
return G_SOURCE_CONTINUE;
|
2012-12-19 17:03:16 +00:00
|
|
|
}
|
|
|
|
|
2012-11-14 22:54:03 +00:00
|
|
|
static gboolean
|
2017-12-31 19:19:34 +00:00
|
|
|
on_map (GtkWidget *widget)
|
2012-11-14 22:54:03 +00:00
|
|
|
{
|
2012-12-19 17:03:16 +00:00
|
|
|
gtk_widget_add_tick_callback (window, tick_callback, NULL, NULL);
|
2012-11-14 22:54:03 +00:00
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2012-09-18 22:21:08 +00:00
|
|
|
static GOptionEntry options[] = {
|
|
|
|
{ "factor", 'f', 0, G_OPTION_ARG_DOUBLE, &load_factor, "Load factor", "FACTOR" },
|
|
|
|
{ "no-resize", 'n', 0, G_OPTION_ARG_NONE, &cb_no_resize, "No Resize", NULL },
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2020-02-10 03:24:47 +00:00
|
|
|
static void
|
|
|
|
quit_cb (GtkWidget *widget,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
gboolean *done = data;
|
|
|
|
|
|
|
|
*done = TRUE;
|
|
|
|
|
|
|
|
g_main_context_wakeup (NULL);
|
|
|
|
}
|
|
|
|
|
2012-09-18 22:21:08 +00:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
GError *error = NULL;
|
2016-11-18 14:02:49 +00:00
|
|
|
GtkWidget *da;
|
2016-09-04 19:13:41 +00:00
|
|
|
GdkDisplay *display;
|
|
|
|
GdkMonitor *monitor;
|
2012-09-18 22:21:08 +00:00
|
|
|
GdkRectangle monitor_bounds;
|
2020-02-10 03:24:47 +00:00
|
|
|
gboolean done = FALSE;
|
2012-09-18 22:21:08 +00:00
|
|
|
|
2013-04-21 22:31:36 +00:00
|
|
|
GOptionContext *context = g_option_context_new (NULL);
|
|
|
|
g_option_context_add_main_entries (context, options, NULL);
|
|
|
|
frame_stats_add_options (g_option_context_get_main_group (context));
|
|
|
|
|
|
|
|
if (!g_option_context_parse (context, &argc, &argv, &error))
|
2012-09-18 22:21:08 +00:00
|
|
|
{
|
2012-11-14 22:54:03 +00:00
|
|
|
g_printerr ("Option parsing failed: %s\n", error->message);
|
2012-09-18 22:21:08 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-12-28 13:53:22 +00:00
|
|
|
gtk_init ();
|
|
|
|
|
2013-04-21 22:31:36 +00:00
|
|
|
g_print ("# Load factor: %g\n",
|
2012-11-14 22:54:03 +00:00
|
|
|
load_factor);
|
2013-04-21 22:31:36 +00:00
|
|
|
g_print ("# Resizing?: %s\n",
|
2012-11-14 22:54:03 +00:00
|
|
|
cb_no_resize ? "no" : "yes");
|
|
|
|
|
2020-02-14 19:55:36 +00:00
|
|
|
window = gtk_window_new ();
|
2013-04-21 22:31:36 +00:00
|
|
|
frame_stats_ensure (GTK_WINDOW (window));
|
|
|
|
|
2016-11-18 14:02:49 +00:00
|
|
|
da = gtk_drawing_area_new ();
|
|
|
|
gtk_drawing_area_set_draw_func (GTK_DRAWING_AREA (da), on_draw, NULL, NULL);
|
|
|
|
gtk_container_add (GTK_CONTAINER (window), da);
|
|
|
|
|
2012-09-18 22:21:08 +00:00
|
|
|
g_signal_connect (window, "destroy",
|
2020-02-10 03:24:47 +00:00
|
|
|
G_CALLBACK (quit_cb), NULL);
|
2012-09-18 22:21:08 +00:00
|
|
|
|
2017-12-31 19:19:34 +00:00
|
|
|
g_signal_connect (window, "map",
|
|
|
|
G_CALLBACK (on_map), NULL);
|
2012-12-19 17:03:16 +00:00
|
|
|
on_frame (0.);
|
2012-09-18 22:21:08 +00:00
|
|
|
|
2016-09-04 19:13:41 +00:00
|
|
|
display = gtk_widget_get_display (window);
|
2020-01-30 20:01:24 +00:00
|
|
|
monitor = gdk_display_get_monitor (display, 0);
|
2016-09-04 19:13:41 +00:00
|
|
|
gdk_monitor_get_geometry (monitor, &monitor_bounds);
|
2012-09-18 22:21:08 +00:00
|
|
|
|
2017-01-19 09:02:04 +00:00
|
|
|
gtk_widget_show (window);
|
2012-09-18 22:21:08 +00:00
|
|
|
|
2020-02-10 03:24:47 +00:00
|
|
|
while (!done)
|
|
|
|
g_main_context_iteration (NULL, TRUE);
|
2012-09-18 22:21:08 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|