[Gtk-sharp-list] The pixbuf-demo in c#

Philip Van Hoof spamfrommailing@freax.org
28 Apr 2003 23:03:15 +0200


--=-zVu5rM3inUfuSqujP3NU
Content-Type: text/plain
Content-Transfer-Encoding: 7bit


Hi there,

I figured.. if I am going to contribute to this great "mono" project,
then I should learn myself working with it and should start searching in
it's sources for answers.

The best way to force myself to do this is , IMHO, to create a "sample"
or a "demo"-application :-)

I am a hobby-GNOME developer so I picked the Pixbuf-demo ([gnome
cvsroot]/gtk+/demos/pixbuf-demo.c)

I am still having one problem .. so if you have some time please take a
look at it, maybe even run it.. and if somebody can help me with
whatever I am doing wrong. I think it is a problem with a byte-pointer
assignment in a unsafe {}-block used for ...

window.DrawRgbImageDithalign (...)

in 

void DrawingArea_Expose(object obj, ExposeEventArgs args) { ... }


ps. Yes, you may add this demo to the samples directory of the gtk-sharp
cvs module if you want to do that.



-- 
Philip Van Hoof a.k.a. freax
me at freax dot org
http://www.freax.be -- http://www.freax.eu.org -- http://www.freax.org



--=-zVu5rM3inUfuSqujP3NU
Content-Disposition: attachment; filename=PixbufExample.cs
Content-Type: text/plain; name=PixbufExample.cs; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

// PixbufExample.cs - port of standard GNOME pixbuf demo 
//
// Author: Philip Van Hoof <me@freax.org>
//
// (c) 2003 Philip Van Hoof

namespace GtkSamples {

	using Gnome;
	using Gtk;
	using Gdk;
	using GtkSharp;
	using System;
	using System.IO;

	public class PixbufExample {
		private int back_width, back_height;
		private Gdk.Pixbuf frame;
		private Gdk.Pixbuf background;
		private Gdk.Pixbuf[] images = new Gdk.Pixbuf[8];

		private uint timeout_id;
		private int frame_num;
		private int n_images=8;
		private Gtk.DrawingArea da;

		public PixbufExample () {
			Gtk.Window win = new Gtk.Window ("Pixbufs");
			win.DeleteEvent += new DeleteEventHandler (Window_Delete);

			this.background = new Gdk.Pixbuf ("pixmaps"+ Path.DirectorySeparatorChar + "background.jpg");
			this.back_width = background.Width;
			this.back_height = background.Height;
  
			images[0] = new Gdk.Pixbuf ("pixmaps"+ Path.DirectorySeparatorChar + "apple-red.png");
			images[1] = new Gdk.Pixbuf ("pixmaps"+ Path.DirectorySeparatorChar + "gnome-ccdialog.png");
			images[2] = new Gdk.Pixbuf ("pixmaps"+ Path.DirectorySeparatorChar + "gnome-gmenu.png");
			images[3] = new Gdk.Pixbuf ("pixmaps"+ Path.DirectorySeparatorChar + "gtk-sharp-logo.png");
			images[4] = new Gdk.Pixbuf ("pixmaps"+ Path.DirectorySeparatorChar + "gnome-calendar.png");
			images[5] = new Gdk.Pixbuf ("pixmaps"+ Path.DirectorySeparatorChar + "gnome-applets.png");
			images[6] = new Gdk.Pixbuf ("pixmaps"+ Path.DirectorySeparatorChar + "gnome-color-browser.png");
			images[7] = new Gdk.Pixbuf ("pixmaps"+ Path.DirectorySeparatorChar + "gnome-mdi.png");

			this.frame = new Gdk.Pixbuf (Gdk.Colorspace.Rgb, false, 8, this.back_width, this.back_height);
			da = new Gtk.DrawingArea();
			win.Add(da);
			da.ExposeEvent += new ExposeEventHandler(DrawingArea_Expose);
			
			this.timeout_id = GLib.Timeout.Add(50, new GLib.TimeoutHandler(timeout));
			
			win.ShowAll ();
		}

		bool timeout ()
		{
			double f=0;
			int i=0;
			double xmid=0, ymid=0;
			double radius=0;

			this.background.CopyArea (0, 0, this.back_width, this.back_height, this.frame, 0, 0);
			f = (double) (this.frame_num % 60) / 60;
			xmid = this.back_width / 2.0;
			ymid = this.back_height / 2.0;
			
			radius = System.Math.Min (xmid, ymid) / 2.0;
			for (i = 0; i < this.n_images; i++)
			{
				int a=0;
				double ang=0;
				int xpos=0, ypos=0;
				int iw=0, ih=0;
				double r=0;
				Gdk.Rectangle r1, r2, dest=new Gdk.Rectangle();
				double k=0;

				ang = 2.0 * System.Math.PI * (double) i / this.n_images - f * 2.0 * System.Math.PI;

				iw = images[i].Width;
				ih = images[i].Height;
				r = radius + (radius / 3.0) * System.Math.Sin (f * 2.0 * System.Math.PI);
				
				xpos = (int) System.Math.Floor (xmid + r * System.Math.Cos (ang) - iw / 2.0 + 0.5);
				ypos = (int) System.Math.Floor (ymid + r * System.Math.Sin (ang) - ih / 2.0 + 0.5);

				if (i % 2 == 0){
					k = System.Math.Sin (f * 2.0 * System.Math.PI);
					a = (int) System.Math.Max (127, System.Math.Abs (255 * System.Math.Sin (f * 2.0 * System.Math.PI)));
				} else {
					k = System.Math.Cos (f * 2.0 * System.Math.PI);
					a = (int) System.Math.Max (127, System.Math.Abs (255 * System.Math.Cos (f * 2.0 * System.Math.PI)));
				}

				k = 2.0 * k * k;
				k = System.Math.Max (k, 0.25);

				r1.x = xpos;
				r1.y = ypos;
				r1.width = (int) (iw * k);
				r1.height = (int) (ih * k);

				r2.x = 0;
				r2.y = 0;
				r2.width = back_width;
				r2.height = back_height;

				if (r1.Intersect(r2, dest)) {


					images[i].Composite(
						frame,
						dest.x, dest.y,
						dest.width, dest.height,
						xpos, ypos,
						k, k,
			 			Gdk.InterpType.Nearest,
						a);
				}
			}

			this.da.QueueDraw();

			frame_num++;
			return true;
		}


		void DrawingArea_Expose(object obj, ExposeEventArgs args)
		{
			unsafe {
				int rowstride = frame.Rowstride;
				Gdk.EventExpose ev = args.Event;
				Gdk.Rectangle area = ev.area;
				// I think this one is still wrong
				byte pixels = *(frame.Pixels + rowstride * area.y + area.x *3);
				Gdk.Window window = ev.window;
	
				window.DrawRgbImageDithalign (
					new Gdk.GC(window),
					area.x, area.y,
					area.width, area.height,
					RgbDither.Normal,
					// So .. idd, this one
					pixels, rowstride,
					area.x, area.y);
			}
		}

		void Quit (object obj, EventArgs args)
		{
			Application.Quit ();
		}

		void Window_Delete (object obj, DeleteEventArgs args)
		{
			SignalArgs sa = (SignalArgs) args;
			Application.Quit ();
			sa.RetVal = true;
		}

		public static int Main (string[] args)
		{
			Application.Init ();
			PixbufExample example = new PixbufExample ();
			Application.Run ();
			return 0;
		}
	}
}

--=-zVu5rM3inUfuSqujP3NU--