what must to be an atribute?



hello

well, if I must to draw some points of different
colors on Gtk::DrawingArea derived, I really must to
have Gdk::Color and Gdk::GC atributes in my class,
instead a dynamic allocation of these ???
someone can tell me the best way (if exists) to do it?
see attatched files...

thanks

"A machine can do the work of fifty ordinary men, but no machine can do the work of one extraordinary man."

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
#ifndef GROWT_HAREA_H
#define GROWT_HAREA_H

#include "margolus.h"

#include <string>

#include <gtkmm/drawingarea.h>


class GrowthArea : public Gtk::DrawingArea
	{
		static const short MAXCELL = 180;

		protected:
		// overridden signal handler
			virtual void on_realize();
			virtual bool on_expose_event( GdkEventExpose *e );

			Glib::RefPtr<Gdk::GC> black_gc, white_gc;
			Gdk::Color black_color, white_color;

		private:
			short sites[MAXCELL+1][MAXCELL+1];
			short down_right;
			Margolus margolus;

		public:
			explicit GrowthArea( std::string init_rule );
			virtual ~GrowthArea();

			void source();
			void next_state();
			void make_build( std::string new_rule );
	};

#endif // GROWTH_AREA_H
#include "growth_area.h"

#include <cstdlib>
#include <vector>


GrowthArea::GrowthArea( std::string init_rule )
	: black_color( Gdk::Color( "black" ) ), white_color( Gdk::Color( "white" ) ),
		down_right(0), margolus( init_rule )
{
	// initializing randomization
	time_t t;	time(&t);	srandom(t);

	this->set_size_request( MAXCELL*2, MAXCELL*2 );

	// initializing all grid
	for( int w = 0; w <= MAXCELL; w++ )
		for( int i = 0; i <= MAXCELL; i++ )
			sites[w][i] = 0;
}



GrowthArea::~GrowthArea() { }
	


void GrowthArea::on_realize()
{
	// we need to call the base on_realize()
	Gtk::DrawingArea::on_realize();

	// a colormap to allocate resources
	Glib::RefPtr<Gdk::Colormap> curr_colormap = this->get_default_colormap();
	curr_colormap->alloc_color( this->black_color );
	curr_colormap->alloc_color( this->white_color );

	// getting window to create graphical contexts
	Glib::RefPtr<Gdk::Window> curr_win = this->get_window();

	// black gc
	this->black_gc = Gdk::GC::create( curr_win );
	this->black_gc->set_foreground( this->black_color );

	// white gc
	this->white_gc = Gdk::GC::create( curr_win );
	this->white_gc->set_foreground( this->white_color );
}
	



bool GrowthArea::on_expose_event( GdkEventExpose *e )
{
	// getting the current window to draw
	Glib::RefPtr<Gdk::Window> curr_win = this->get_window();

	// a black square
	std::vector<Gdk::Point> sqr;
	// storing black square's points 
	sqr.push_back( Gdk::Point( 0, 0 ) );
	sqr.push_back( Gdk::Point( 0, MAXCELL*2 ) );
	sqr.push_back( Gdk::Point( MAXCELL*2, MAXCELL*2 ) );
	sqr.push_back( Gdk::Point( MAXCELL*2, 0 ) );
	// drawing the black square
	curr_win->draw_polygon( black_gc, true, sqr );

	// drawing the points
	for( int y = 0; y <= MAXCELL; y++ )
		for( int x = 0; x <= MAXCELL; x++ )
			if( sites[y][x] )
				{
					curr_win->draw_point( white_gc, 2*x, 2*y );
					curr_win->draw_point( white_gc, 2*x, 2*y+1 );
					curr_win->draw_point( white_gc, 2*x+1, 2*y );
					curr_win->draw_point( white_gc, 2*x+1, 2*y+1 );
				}

	return true;	// everything is ok
}



void GrowthArea::source()
{
// sources for the middle north of drawing area
	for( int w = MAXCELL/2-3; w <= MAXCELL/2+2; w++ )
		sites[0][w] = rand()%10 == 1 ? 1 : 0;
}



void GrowthArea::next_state()
{
	short *neigh[2][2];

	for( int w = down_right; w < MAXCELL + down_right; w+=2 )
		for( int i = down_right; i < MAXCELL + down_right; i+=2 )
			{
				neigh[0][0] = &sites[w][i];
				neigh[0][1] = &sites[w][i+1];
				neigh[1][0] = &sites[w+1][i];
				neigh[1][1] = &sites[w+1][i+1];

				margolus.update_block( neigh );
			}

	this->source();

// updating flag if must down_right
	if( down_right ) down_right = 0;
	else down_right = 1;
}



void GrowthArea::make_build( std::string new_rule )
{
	// setting new_rule
	margolus.set_rule( new_rule );

	// reinitializing all grid
	for( int w = 0; w <= MAXCELL; w++ )
		for( int i = 0; i <= MAXCELL; i++ )
			sites[w][i] = 0;
}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]