GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Dec 20, 2014 4:32 pm

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: GTK Timeout Is Driving Me Crazy!!!!
PostPosted: Mon Nov 26, 2007 8:35 pm 
Offline
Familiar Face

Joined: Sat Nov 24, 2007 10:59 pm
Posts: 5
Hello, I am a desperate programmer, I need a simple, just a simple example on using GTK timeout add and gtk timeout remove.

I am trying to simulate the shot of a tank with GTK and I have these elements

1. The x and y coordinates.
2. GTK Fixed
3. And the image of the shot

gint shoot (gint x, gint y)
{
y = y - 5;
if (y <= 0)
//RETURN FALSE???
gtk_fixed_move (fixed, image_shot, x, y);
//RETURN TRUE???
}

How can I add this function to a GTK timeout?? How can I remove it?


I know this is a noob question, but please help me

Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 26, 2007 9:00 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
The short answer is that you can't. The function has to be in the form of GSourceFunc, which returns a gboolean value and receives the gpointer data. What exactly is this shot doing? If you are creating an animation, how about try this.

Create a structure that holds the start x/y points and end x/y points. It could look like this (syntax on all of this code has not been tested!):

Code:
struct Points
{
  GtkWidget *fixed, *image_shot;
  gint initX, initY;
  gint endX, endY;
  gint animations;
  gint count;
};


Then, you will want to initialize these values and setup the timeout. Note that the next timeout does not start to count the 100 milliseconds until the previous ends. This will also add pointers to your fixed and image_shot widgets.

Code:
Points *pts = (Points*) g_malloc (sizeof (Points));
pts->fixed = fixed;
pts->image_shot = image_shot;
pts->initX = 5;
pts->initY = 10;
pts->endX = 15;
pts->endY = 25;
pts->animations = 7;
pts->count = 0;

// This timeout will run about every 100 milliseconds
g_timeout_add (100, (GSourceFunc) shoot, (gpointer) pts);


Lastly, you can create the timeout function. This will move the image from (initX,initY) to (endX,endY) in 'animations' moves. (Like I said, this will require cleaning because I didn't test any of this.

Code:
gboolean shoot (Points *pts)
{
  gint x, y;
  gdouble scale;

  // The scale tells us where to place the image
  pts->count++;
  scale = (gdouble) pts->count / (gdouble) pts->animations;

  // The new position adds the progress to the initial
  x = (scale * (endX - initX)) + initX;
  y = (scale * (endY - initY)) + initY;
  gtk_fixed_move (GTK_FIXED (pts->fixed), pts->image_shot, x, y);

  // If this has run 'animations' times, remove
  // the timeout by returning FALSE
  if (pts->count >= pts->animations)
    return FALSE;

  // Otherwise, continue running the timeout
  return TRUE;
}


If you don't understand any part of this code, let me know. I think this is what you are trying to do, but if it is not, I am sure that it atleast shows you how to use timeouts.

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 26, 2007 9:26 pm 
Offline
Familiar Face

Joined: Sat Nov 24, 2007 10:59 pm
Posts: 5
Thanks man, you are officialy my savior


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 26, 2007 9:37 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
Glad I could be of help. If you're interested, please check out my book, Foundations of GTK+ Development.

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group