5. timer
5. timer
#include <stdio.h>
#include <string.h>
#include <gtk/gtk.h>
/* Determines if to continue the timer or not */
static gboolean continue_timer = FALSE;
/* Determines if the timer has started */
static gboolean start_timer = FALSE;
/* Display seconds expired */
static int sec_expired = 0;
static gboolean
_label_update(gpointer data)
{
GtkLabel *label = (GtkLabel*)data;
char buf[256];
memset(&buf, 0x0, 256);
snprintf(buf, 255, "Time elapsed: %d secs", ++sec_expired);
gtk_label_set_label(label, buf);
return continue_timer;
}
int main(int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *frame;
GtkWidget *quit_button;
GtkWidget *label;
gtk_init(NULL, NULL);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "The first Game");
gtk_window_set_default_size(GTK_WINDOW(window), 500, 500);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
frame = gtk_fixed_new();
gtk_container_add(GTK_CONTAINER(window), frame);
label = gtk_label_new("Time elapsed: 0 secs");
gtk_fixed_put(GTK_FIXED(frame), label, 10, 10);
gtk_widget_show_all(window);
g_signal_connect (G_OBJECT (window), "destroy",
G_CALLBACK (gtk_main_quit),
NULL);
g_timeout_add_seconds(1, _label_update, label);
continue_timer = TRUE;
start_timer = TRUE;
gtk_main();
return 0;
}
#include <stdio.h>
#include <string.h>
#include <gtk/gtk.h>
/* Determines if to continue the timer or not */
static gboolean continue_timer = FALSE;
/* Determines if the timer has started */
static gboolean start_timer = FALSE;
/* Display seconds expired */
static int sec_expired = 0;
static gboolean
_label_update(gpointer data)
{
GtkLabel *label = (GtkLabel*)data;
char buf[256];
memset(&buf, 0x0, 256);
snprintf(buf, 255, "Time elapsed: %d secs", ++sec_expired);
gtk_label_set_label(label, buf);
return continue_timer;
}
int main(int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *frame;
GtkWidget *quit_button;
GtkWidget *label;
gtk_init(NULL, NULL);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "The first Game");
gtk_window_set_default_size(GTK_WINDOW(window), 500, 500);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
frame = gtk_fixed_new();
gtk_container_add(GTK_CONTAINER(window), frame);
label = gtk_label_new("Time elapsed: 0 secs");
gtk_fixed_put(GTK_FIXED(frame), label, 10, 10);
gtk_widget_show_all(window);
g_signal_connect (G_OBJECT (window), "destroy",
G_CALLBACK (gtk_main_quit),
NULL);
g_timeout_add_seconds(1, _label_update, label);
continue_timer = TRUE;
start_timer = TRUE;
gtk_main();
return 0;
}
ความคิดเห็น
แสดงความคิดเห็น