 |
| View previous topic :: View next topic |
| Author |
Message |
rajli
Joined: 04 May 2007 Posts: 2 Location: 127.0.0.1
|
Posted: Fri May 04, 2007 6:51 pm Post subject: events_peending |
|
|
Hi im quite new to php-gtk2 and have a lite problem when updating the interface from within a callback function. I use:
| Code: |
while(Gtk::events_pending()) {Gtk::main_iteration();}
|
But it takes about 0,1 second to run that row adn i need it to be done faster.
In my last application it work fine and there arent many differnses between them.
This is my work. There might be loads of other bugs but ill will spend my time on them when i fix the problem with events_pedning loop.
| Code: |
<?php
$db_conn = mysql_connect("localhost","***","***");
mysql_select_db("labyrint");
require_once('Spelet.class.phpw');
$spelomgang = new Spelet(); // Startar en ny omgång av spelet.
$spelomgang->connect_simple('destroy', array('Gtk','main_quit')); // Ser till att programmet avslutas korrekt.
Gtk::main();
?>
|
| Code: |
<?php
class Spelet extends GtkWindow {
// INSTANSVARIABLER
private $x;
private $y;
private $guy;
private $badguy;
private $badX;
private $badY;
private $speed;
private $grid;
private $dir;
private $badguyDir;
private $go;
private $click;
// KONSTRUKTOR
public function __construct() {
parent::__construct(); // Anropar konstruktorn för GtkWindow
$this->set_title('Labyrint');
$this->set_size_request(550,550);
$this->set_resizable(false);
$this->set_position(Gtk::WIN_POS_CENTER_ALWAYS);
$this->set_decorated(false);
$this->guy = GtkImage::new_from_file('bilder/guy.gif');
$this->badguy = GtkImage::new_from_file('bilder/bad.gif');
$this->x = 406;
$this->y = 15;
$this->badX = 89;
$this->badY = 53;
$this->badguyDir = rand(0,4);
$this->speed = 7;
$this->click = 0;
$this->grid=array(); //$grid['x']['y'] = 0 eller 1
for($i = 0;$i<550;$i++){//y-cords
for($j = 0;$j<550;$j++){//x-cords
$this->grid[$j][$i] = "e";
}
}
$this->grapichs();
$this->show_all();
}
// INSTANSMETODER
function grapichs(){
$vbox = new GtkVBox();
$this->eventBox = new GtkEventBox(); // En event box kan lyssna på actions, t ex tangentclickningar
$vbox->add($this->eventBox);
$FixedMain = new GtkFixed(); // I en fixed kan man positionera saker med koordinater (pixlar).
$this->fixedMain = $FixedMain;
$this->eventBox->add($FixedMain);
$FixedMain->put(GtkImage::new_from_file('bilder/bg.gif'),10,25);
$this->fixedMain->put($this->guy,$this->x,$this->y);
$this->fixedMain->put($this->badguy,$this->badX,$this->badY);
$this->add($vbox);
/**************************************
*Ladda in Cordinater för ytre vägarna*
**************************************/
for($i = 25; $i < 528;$i++){
$this->grid['10'][$i] = "n";
$this->grid['510'][$i] = "n";
}
for($i = 10; $i < 510;$i++){
if($i<400||$i>420)
{
$this->grid[$i]['25'] = "n";
$this->grid[$i]['26'] = "n";
$this->grid[$i]['27'] = "n";
$this->grid[$i]['525'] = "r";
$this->grid[$i]['526'] = "r";
$this->grid[$i]['527'] = "r";
}
}
/****************************************
*Ladda in Cordinater för hinder/vägarna*
****************************************/
$strQuery = mysql_query("SELECT * FROM tbl_obstacles");
while($r = mysql_fetch_array($strQuery)){
$x = $r['x'];
$y = $r['y'];
for($i = 0; $i<$r['length'];$i++){
$this->grid[$x][$y] = $r['dir'];
if($r['dir'] == "n"){
$y++;
}
if($r['dir'] == "r"){
$this->grid[$x][$y+1] = $r['dir'];
$this->grid[$x][$y+2] = $r['dir'];
$x++;
}
}
}
/*****************************
*Printar ut vägar och hinder*
*****************************/
for($i = 0;$i<550;$i++){//y-cords
for($j = 0;$j<550;$j++){//x-cords
if($this->grid[$j][$i] != "e"){
$FixedMain->put(GtkImage::new_from_file('bilder/'.$this->grid[$j][$i].'.gif'),$j,$i);
}
}
}
$this->connect("key-press-event",array($this,"key_event")); // När tangent clicks ned körs metoden tangent_nedclickt
}
function checkxy(){
if($this->dir == 1){//Flyttas åt vänster
for($i = $this->y;$i<$this->y+10;$i++){
if($this->grid[$this->x-2][$i] != "e"){
$this->x++;
}
}
}
if($this->dir == 2){//Upp
for($i = $this->x-2;$i<$this->x+11;$i++){
if($this->grid[$i][$this->y] != "e"){
$this->y++;
}
}
}
if($this->dir == 3){//Höger
for($i = $this->y;$i<$this->y+10;$i++){
if($this->grid[$this->x+10][$i] != "e"){
$this->x--;
}
}
}
if($this->dir == 4){//Ner
for($i = $this->x-2;$i<$this->x+11;$i++){
if($this->grid[$i][$this->y+9] != "e"){
$this->y--;
}
}
}
}
function badguyCheckxy(){
if($this->dir == 1){//Flyttas åt vänster
for($i = $this->badY;$i<$this->badY+10;$i++){
if($this->grid[$this->badX-2][$i] != "e"){
$this->badX++;
$this->badguyDir = rand(0,4);
}
}
}
if($this->dir == 2){//Upp
for($i = $this->badX-2;$i<$this->badX+11;$i++){
if($this->grid[$i][$this->badY] != "e"){
$this->badY++;
$this->badguyDir = rand(0,4);
}
}
}
if($this->dir == 3){//Höger
for($i = $this->badY;$i<$this->badY+10;$i++){
if($this->grid[$this->badX+10][$i] != "e"){
$this->badX--;
$this->badguyDir = rand(0,4);
}
}
}
if($this->dir == 4){//Ner
for($i = $this->badX-2;$i<$this->badX+11;$i++){
if($this->grid[$i][$this->badY+9] != "e"){
$this->badY--;
$this->badguyDir = rand(0,4);
}
}
}
}
function key_event($widget,$event){
$this->go = 1;
if($event->keyval == 113) { // Q - avsluta (113)
$this->destroy();
}
if($event->keyval == 65293){ //Enter
echo $this->x . " - " . $this->y ." = " . $this->grid[$this->x][$this->y] ."\n";
}
if($event->keyval == 65361){ // Vänster
$this->dir = 1;
}
if($event->keyval == 65362){ // Upp
$this->dir = 2;
}
if($event->keyval == 65363){ // Höger
$this->dir = 3;
}
if($event->keyval == 65364){ // Ner
$this->dir = 4;
}
while($this->go == 1){
if($this->dir == 1){
for($i = 0; $i < $this->speed; $i++){
$this->x--;
$this->checkxy();
}
}
if($this->dir == 2){
for($i = 0; $i < $this->speed; $i++){
$this->y--;
$this->checkxy();
}
}
if($this->dir == 3){
for($i = 0; $i < $this->speed; $i++){
$this->x++;
$this->checkxy();
}
}
if($this->dir == 4){
for($i = 0; $i < $this->speed; $i++){
$this->y++;
$this->checkxy();
}
}
if($this->badguyDir == 1){ // Vänster
for($i = 0; $i < $this->speed; $i++){
$this->badX--;
$this->badguyCheckxy();
}
}
if($this->badguyDir == 2){ // Upp
for($i = 0; $i < $this->speed; $i++){
$this->badY--;
$this->badguyCheckxy();
}
}
if($this->badguyDir == 3){ // Höger
for($i = 0; $i < $this->speed; $i++){
$this->badX++;
$this->badguyCheckxy();
}
}
if($this->badguyDir == 4){ // Ner
for($i = 0; $i < $this->speed; $i++){
$this->badY++;
$this->badguyCheckxy();
}
}
$this->fixedMain->move($this->guy,$this->x,$this->y);
$this->fixedMain->move($this->badguy,$this->badX,$this->badY);
while(Gtk::events_pending()) {Gtk::main_iteration();}
}
}
}
?>
|
|
|
| Back to top |
|
 |
cweiske
Joined: 08 Dec 2005 Posts: 454 Location: Leipzig/Germany
|
Posted: Sat May 12, 2007 3:31 pm Post subject: |
|
|
| You could call this only once in 10 events if you want, but I doubt there is a faster way. |
|
| Back to top |
|
 |
rajli
Joined: 04 May 2007 Posts: 2 Location: 127.0.0.1
|
Posted: Mon May 14, 2007 10:04 pm Post subject: |
|
|
| The problem is gone for me now. Changed Most of the things in the code. Put the biggest problem was that i used the put($img,x,y) to many times and that slow events_pending() |
|
| Back to top |
|
 |
|
|
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 vote in polls in this forum
|
|
 |