Need Help with optimizing code for first time PhP-GTK program
| From: | Mark_G | Date: | Fri, 27 Apr 2007 04:41:12 +0000 |
| Subject: | Need Help with optimizing code for first time PhP-GTK program | ||
| Groups: | php.gtk.general | ||
| Request: | Send a blank email to php-gtk-general+get-7798@lists.php.net to get a copy of this message | ||
<?PHP
/*
example PHP-GTK2 program by Ralf Schwoebel (puzzler@tradebit.com)
to demonstrate how to start writing GUI applications with PHP, GTK2
and Gnope... www.gnope.org
- destroy function of the main window ...
- if you click the X, the window gets closed and the program exits the
main loop = Gtk::main()
*/
// with PHP 5.x the gtk classes should be loaded in the php.ini file!
if( !class_exists('gtk')) {
die('Please load the php-gtk2 module in your php.ini' . "\r\n");
}
require_once("PEAR/Config.php");
define("ROWMAX", 93);
define("COLMAX", 119);
define("SPEED", 1);
define("DEBUG",0);
define("DEBUGGUI",0);
// create MAIN window
class Landscape extends GtkWindow {
public $break_loop;
public $run;
public $arnum;
public $landscape =
array(7,7,8,8,8,9,9,9,9,9,9,9,9,8,8,8,8,9,9,9,9,9,10,10,10,10,10,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11,11,11,12,12,12,11,11,11,12,12,12,12,12,12,12,13,13,13,13,14,14,14,15,15,15,15,15,15,15,15,15,16,16,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,16,15,15,16,16,17,17,17,17,17,17,17,17,17,17,17,17,18,18,18,18,18,18,18,18,18,18,18,18,18,18,19,19,19,20,20,20,21,21,21,21,21,21,21,21,21,20,20,20,20,21,21,21,22,22,21,21,21,21,21,21,20,20,20,20,20,20,20,20,20,20,20,19,19,19,19,19,19,19,19,20,20,20,21,21,21,21,21,21,21,20,20,20,20,20,20,20,20,21,21,21,21,21,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,21,21,21,21,21,21,20,20,20,20,20,21,21,21,20,20,20,20,20,20,20,20,20,21,22,23,23,24,24,24,24,24,24,24,24,24,23,23,23,22,22,22,22,23,23,23,24,25,25,25,24,24,24,23,23,23,23,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,25,25,25,25,25,25,25,25,25,25,25,25,26,26,26,26,26,26,26,26,26,27,27,27,27,27,27,27,27,27,27,27,27,27,27,28,28,29,30,30,30,31,32,32,33,34,34,35,35,35,36,37,37,38,38,38,39,39,40,41,41,42,43,44,44,45,45,45,44,44,44,45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,44,44,44,44,44,44,44,44,43,43,43,43,43,44,44,44,44,44,45,46,46,46,46,46,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,46,46,47,47,47,47,47,47,47,47,47,47,47,47,47,48,48,48,49,49,49,49,49,49,48,48,48,49,49,50,50,50,50,50,50,50,50,50,50,51,51,51,51,51,51,51,51,51,52,52,52,53,53,54,55,55,55,56,56,56,56,57,57,57,57,57,57,57,58,58,58,58,58,58,58,59,59,59,59,60,60,60,60,60,60,60,60,60,61,61,62,62,62,62,62,62,63,63,64,64,64,65,65,65,66,66,66,66,66,66,66,66,66,66,67,68,68,68,68,68,69,69,70,70,70,70,70,71,71,71,71,71,71,71,71,71,71,71,71,71,71,71,72,72,73,73,73,73,73,73,73,73,73,73,73,73,73,73,73,74,74,74,74,74,74,74,74,74,74,74,74,74,74,74,74,74,74,74,74,74,74,74,74,74,74,74,74,74,74,74,74,74,74,74,73,73,73,73,73,73,72,72,72,71,70,70,70,70,70,70,70,70,69,69,69,69,69,68,67,67,67,67,67,67,67,67,67,67,67,67,68,68,68,68,68,68,68,68,68,68,68,69,69,69,69,69,70,70);
public $landmade= array();
public function __construct() {
parent::__construct();
$this->buildGui();
$this->break_loop = false;
//$this->createtable();
$this->kill_loop = false;
$this->arnum = 0;
$this->show_all();
if(DEBUGGUI){
require_once 'Dev/GuiInspector.php';
$gi = new Dev_GuiInspector($this);
}
if(DEBUG){
//require_once 'Gtk2/VarDump.php';
//Gtk2_VarDump::display($this->landmade, 'variable title');
}
}
function move(){
//0-86 bottom row
//87-173 1 row up
$children = $this->table->get_children();
for($i=$this->arnum;$i<count($children);$i=COLMAX+$i){
$rownum = floor($i/COLMAX);
$colnum = $i-(COLMAX*$rownum);
if($this->landscape[$colnum]>=$rownum){
$children[$i]->set_text(chr(rand(65,90)));
} else {
$children[$i]->set_text(" ");
}
}
$this->arnum++;
if ($this->arnum>COLMAX) {
array_shift($this->landscape);
$this->arnum = 0;
}
}
function start_loop($do_break=0){
while(!$this->kill_loop){
while(gtk::events_pending()){
gtk::main_iteration_do(0);
}
$this->move();
usleep(SPEED);
if($do_break) break;
}
}
public function stop_loop(){
$do_break=1;
$this->kill_loop = true;
}
public function buildGUI() {
$this->set_title('Matrix Landscape');
$this->set_resizable(false);
$this->connect_simple('destroy', array($this,'close'));
$this->set_position(Gtk::WIN_POS_CENTER_ALWAYS);
// create an eventbox that allows you set background color
$this->eventbox = new GtkEventBox();
$this->eventbox->modify_bg(Gtk::STATE_NORMAL, GdkColor::parse('#ffffff'));
//$this->table = new GtkTable(ROWMAX,COLMAX+1,TRUE);
$this->table = new GtkFixed();
$this->eventbox->add($this->table);
for($i=ROWMAX;$i>0;$i--) {
for($j=COLMAX;$j>0;$j--){
$text1 = new GtkLabel(chr(rand(65,90)));
$text1->modify_font(new PangoFontDescription('Arial Bold 7'));
$text1->set_width_chars(1);
//$this->table->attach($text1,($j-1),$j,$i-1,$i);
$this->table->put($text1,($j*8),($i*8));
}
}
//$this->table->set_row_spacings(0);
//$this->table->set_col_spacings(0);
//$this->eventbox->add($this->table);
$bStart = new GtkButton("_Start");
$bStart->connect_simple('clicked',array($this,"start_loop"));
$bEnd = new GtkButton("_End");
$bEnd->connect_simple('clicked',array($this,'stop_loop'));
$bbox = new GtkHButtonBox();
$bbox->add($bStart);
$bbox->add($bEnd);
$vbox = new GtkVBox();
$vbox->pack_start($this->eventbox);
$vbox->pack_start($bbox);
if(DEBUG){
$this->dbox = new GtkLabel("DEBUGGING");
$vbox->pack_start($this->dbox);
}
$this->add($vbox);
}
function close() {
$this->kill_loop=true;
gtk::main_quit();
}
}
?>
The program works exactly how I want but just wondering if someone knew of
how to speed it up possibly (The refresh rate)? Also, it has a long load
time due to the size of the array it is loading. Anyone that can offer up
their time and advice would be well appreciated.
Thanks,
Mark
--
View this message in context: http://www.nabble.com/Need-Help-with-optimizing-code-for-first-time-PhP-GTK-program-tf3655701.html#a10213082
Sent from the Php - GTK - General mailing list archive at Nabble.com.