Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Wed 01 Oct 2014, 06:28
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
simple game framework for scripting languages
Post new topic   Reply to topic View previous topic :: View next topic
Page 2 of 5 [63 Posts]   Goto page: Previous 1, 2, 3, 4, 5 Next
Author Message
seaside

Joined: 11 Apr 2007
Posts: 886

PostPosted: Mon 27 Aug 2012, 15:02    Post subject: svg-placer  

Here's a GUI tool for technosaurus' svgame and 13x10 grid where you can try out images and text on each square. Just click in the upper left corner of any square where you want an image or text to start and then close the window. A dialog follows asking if you want an image, press enter and a fileselect dialog for the directory "/usr/share/pixmaps" opens. Press cancel and a text entry box opens. The program then rerenders the new svg. Repeat until done (sounds like a recipe Very Happy )

Kind of a kludge because commands don't run inside the case statements until the window is closed. Best run in a terminal, since the program calls itself, you have to kill it to stop.

Cheers,
s
Code:
 #!/bin/sh
# requires technosaurus' svgame
# svg-placer -click on location & close window
# run in terminal to kill

 if ! [ -f /tmp/svgame.svg ]; then
echo '
<svg  width="640" height="480">
  <defs>
    <pattern id="grid" width="48" height="48" patternUnits="userSpaceOnUse">
      <rect fill="white" stroke="black" x="0" y="0" width="48" height="48"/>
    </pattern>
  </defs>
  <rect fill="url(#grid)" stroke="black" x="0" y="0" width="640" height="480"/>
</svg>'  >/tmp/svgame.svg

fi

svgame  /tmp/svgame.svg  2>/dev/null  | \
while read LINE; do
case "$LINE" in
BUTTON*)
LINE="${LINE//BUTTON=/}"
 x="${LINE%,*}"
 y="${LINE##*,}" ;;
 *) exit
;;
esac

Xdialog --title "Image or Text dialog" --stdout  --yesno "Is this an image file?" 0 0

case $? in
  0)
    FILE="`Xdialog --backtitle "Please choose Image." --title "Image selector" --stdout --no-buttons --fselect  /usr/share/pixmaps 0 0`"
   sed -i 's|</svg>||' /tmp/svgame.svg
   
   echo '
   <image x="'$x'" y="'$y'" width="48" height="48"
        xlink:href="'$FILE'" />
   </svg>' >>/tmp/svgame.svg ;;

  1)
    TEXT="`Xdialog --backtitle "Please type text below" --title "Text Select" --stdout --no-wrap --inputbox "short text no wrap" 0 0`"
    sed -i 's|</svg>||' /tmp/svgame.svg
   
     echo '
    <text x="'$x'" y="'$y'"
        style="font-size:20" fill="black"
   >'"$TEXT"'</text>
   </svg>' >>/tmp/svgame.svg ;;
   
  255)
       exit ;;
esac
done
$0
svg-placer.png
 Description   
 Filesize   17.9 KB
 Viewed   621 Time(s)

svg-placer.png

Back to top
View user's profile Send private message 
seaside

Joined: 11 Apr 2007
Posts: 886

PostPosted: Tue 28 Aug 2012, 19:25    Post subject: Case statements  

technosaurus,

I just noticed that everything runs fine for the case statements in the 2nd version of the source code that included key and mouse events (but not /path/to/image) when I compiled it under Racy.

The other posting of svgame.tar.gz which was a compiled version, allowing a file to be passed on the command line, is the one that Racy is not handling the same way.

Would you be kind enough to post the source code for that compiled version
Quote:
Description Here is a precompiled version.that takes an input filename as 1st arg.
usage:
svgame /path/to/image.svg | your_controls


I'd like to compile that in Racy and see if that cures the "case statements not being run problem"

Thanks,
s
(Can this be a gtk version level problem?)
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4351

PostPosted: Tue 28 Aug 2012, 21:18    Post subject:  

formatting is scrunched, but here it is:
Code:
#include <sys/inotify.h>
#include <gtk/gtk.h>
#include <string.h>
typedef struct _variable { char Name[ 512 ]; GtkWidget *Widget; } variable;
void key_press_event(GtkWidget *widget, GdkEventKey *event){printf("KEY=%s\n",gdk_keyval_name(event->keyval));}
void button_press_event(GtkWidget *widget, GdkEventButton *event){printf("BUTTON=%f,%f\n",event->x ,event->y);}
void refresh(gpointer data, gint fd, GdkInputCondition c){
char buffer[sizeof (struct inotify_event)];   variable *var; var = (variable *)data;
read( fd, buffer, sizeof(buffer) ); gtk_image_set_from_file(GTK_IMAGE(var->Widget),var->Name);}
int main(int argc, char **argv){ int watch, fd;   GtkWidget *window, *image, *eventbox; variable *var; var = g_malloc(sizeof(variable));
gtk_init (&argc, &argv); fd = inotify_init();
window = gtk_window_new(GTK_WINDOW_TOPLEVEL); eventbox = gtk_event_box_new (); image = gtk_image_new_from_file(argv[1]);
gtk_container_add(GTK_CONTAINER(window), eventbox); gtk_container_add(GTK_CONTAINER(eventbox), image);
watch = inotify_add_watch( fd, argv[1], IN_MODIFY ); var->Widget=image; strncpy(var->Name, argv[1], 512);
gdk_input_add( fd, GDK_INPUT_READ, refresh, (gpointer) var );
g_signal_connect((gpointer)window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect((gpointer) window, "button_press_event", G_CALLBACK(button_press_event), NULL);
g_signal_connect ((gpointer) window, "key_press_event", G_CALLBACK (key_press_event), NULL);
gtk_widget_show_all(window); gtk_main ();}

Note: need to replace printf with g_print and remove string.h

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
seaside

Joined: 11 Apr 2007
Posts: 886

PostPosted: Wed 29 Aug 2012, 16:45    Post subject: Racy5.3  

technosaurus,

Thank you very much for posting the source code. I compiled it in Racy5.3 and it works.

In case anyone else wants the Racy5.3 compiled version, I've attached it below.

Regards,
s
svgame-r.tar.gz
Description  Compiled in Racy-5.3 and named svgame-r
gz

 Download 
Filename  svgame-r.tar.gz 
Filesize  2.21 KB 
Downloaded  239 Time(s) 
Back to top
View user's profile Send private message 
seaside

Joined: 11 Apr 2007
Posts: 886

PostPosted: Thu 30 Aug 2012, 10:36    Post subject: svg-placer
Subject description: non-recursive
 

Here's a handier version of svg-placer that doesn't call itself, with the added ability to change text font size and color. Also, F3 brings up Inkscape.

Code:
 #!/bin/sh
# requires technosaurus' svgame
# svg-placer -click on location
# choose image or text
#F3 brings up inkscape

 if ! [ -f /tmp/svgame.svg ]; then
echo '
<svg  width="640" height="480">
  <defs>
    <pattern id="grid" width="48" height="48" patternUnits="userSpaceOnUse">
      <rect fill="white" stroke="black" x="0" y="0" width="48" height="48"/>
    </pattern>
  </defs>
  <rect fill="url(#grid)" stroke="black" x="0" y="0" width="640" height="480"/>
</svg>'  >/tmp/svgame.svg

fi

svgame  /tmp/svgame.svg  2>/dev/null  | \
while read LINE; do
case "$LINE" in
BUTTON*)
   LINE="${LINE//BUTTON=/}"
    x="${LINE%,*}"
    y="${LINE##*,}"
   
   Xdialog --title "Image or Text dialog" --stdout  --yesno "Is this an image file?" 0 0
   
   case $? in
     0)
       FILE="`Xdialog --backtitle "Please choose Image." --title "Image selector" --stdout --no-buttons --fselect  /usr/share/pixmaps 0 0`"
      cp /tmp/svgame.svg /tmp/svgameNEW # need to copy else works once only
      
      sed -i 's|</svg>||' /tmp/svgameNEW
   
      echo '
      <image x="'$x'" y="'$y'" width="48" height="48"
           xlink:href="'$FILE'" />
      </svg>' >>/tmp/svgameNEW
      
      cp /tmp/svgameNEW /tmp/svgame.svg ;;
   
   
     1)
      SEL=`Xdialog \
              --title "Text Select" \
               --stdout \
              --3inputsbox  "Text Selector" 0 0 \
                  "Text to place" "" \
                  "Font Size" "20" \
                  "Color Name" "black"`
   
      cp /tmp/svgame.svg /tmp/svgameNEW # need to copy else works once only
      
      sed -i 's|</svg>||' /tmp/svgameNEW
      
      TEXT="${SEL%%/*}"
      n="${SEL#*/}"
      FONT="${n%/*}"
      COLOR="'"${SEL##*/}"'"
      
        echo '
       <text x="'$x'" y="'$y'"
           style="font-size:'"$FONT"'"  fill='"$COLOR"'
      >'"$TEXT"'</text>
      </svg>' >>/tmp/svgameNEW
      
      cp /tmp/svgameNEW /tmp/svgame.svg ;;
      
     255)
          exit ;;
   esac ;;
   
 KEY=F3) inkscapelite /tmp/svgame.svg ;;

*)  ;;

esac
done


Cheers,
s
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4351

PostPosted: Thu 30 Aug 2012, 23:41    Post subject:  

Here is some portable collision detection code:
Code:
#!/bin/ash
ALPHAS=" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
#alphas can represent tiles, sprites etc...
WIDTH=3
HEIGHT=3
GRID="   bbb   "
#so the grid looks like:
#   #
#bbb#
#   #
no_collision(){
   #x=$1 y=$2
   pos=${GRID:$(($1+$2*$HEIGHT)):1}
   retval=${ALPHAS%${pos}*}
   return ${#retval}
}
no_collision 1 1 || echo ${ALPHAS:$?:1}

basically collision detection would tell you if you are trying to place something where something else already exists... moving a character, placing a game piece etc...

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
jamesbond

Joined: 26 Feb 2007
Posts: 2186
Location: The Blue Marble

PostPosted: Wed 05 Dec 2012, 23:28    Post subject:  

The code on the first post doesn't work for me (and thus by extension all the other newer revisions because the part that doesn't work is the same for all revisions).

This part:
Code:
void refresh(gpointer si, gint fd, GdkInputCondition c){
   char buffer[sizeof (struct inotify_event)];
   read( fd, buffer, sizeof(buffer) ); /* just clearing, don't care the type */
   gtk_image_set_from_file(si,IMAGE);} /* force redraw of image*/

only works once because if the file is deleted and re-created, the watch is lost after the first trigger.

The fix that works for me:
Code:
void refresh(gpointer si, gint fd, GdkInputCondition c) {
   char buffer[sizeof (struct inotify_event)];
   gtk_image_set_from_file(si,IMAGE);  /* force redraw of image*/
      
   read( fd, buffer, sizeof(buffer) ); /* just clearing, don't care the type */
   inotify_rm_watch(fd, watch);
   watch = inotify_add_watch( fd, IMAGE, IN_MODIFY ); /* re-trigger watch in case file is deleted / recreated */
}

_________________
Fatdog64, Slacko and Puppeee user. Puppy user since 2.13.
Contributed Fatdog64 packages thread
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4351

PostPosted: Thu 06 Dec 2012, 01:32    Post subject:  

That's why none of my examples deleted the file, only modified them by ln/cp/mv -f or echo > my.svg

I had tried something similar, but gtk would sometimes hiccup on remove/replace (depending on timing) ... there are events for delete (and create in watched directory) too. Watching the directory may be a better idea anyhow - would allow you to redraw the svg if one of the include images changes (for instance a character uses a weapon while standing still)

I may try testing it with the file watch mechanism that I started using in sit (basically so it works on win32 and mac as well)

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
jamesbond

Joined: 26 Feb 2007
Posts: 2186
Location: The Blue Marble

PostPosted: Thu 06 Dec 2012, 06:56    Post subject:  

technosaurus wrote:
That's why none of my examples deleted the file, only modified them by ln/cp/mv -f or echo > my.svg

I wasn't trying to do anything fancy. I was doing a rox-clock http://murga-linux.com/puppy/viewtopic.php?t=82734 and it dawned upon me that svgame would be an excellent platform for it. I used sed -i to update the svg, and it only worked the first time. Only upon strace-ing sed -i, I realised that it worked by creating a new file, and moving that file over the old one ... As it happens geany also works this way.

Quote:
I had tried something similar, but gtk would sometimes hiccup on remove/replace (depending on timing) ... there are events for delete (and create in watched directory) too. Watching the directory may be a better idea anyhow - would allow you to redraw the svg if one of the include images changes (for instance a character uses a weapon while standing still)
Yes, but the problem with watching a directory is that you'll get notified for anything that happens in the directory. If you only need to watch a specific file then you will have to filter these events.

Quote:
I may try testing it with the file watch mechanism that I started using in sit (basically so it works on win32 and mac as well)
I thought you're using the same mechanism there?
_________________
Fatdog64, Slacko and Puppeee user. Puppy user since 2.13.
Contributed Fatdog64 packages thread
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4351

PostPosted: Thu 06 Dec 2012, 15:59    Post subject:  

lets say your svg file is mygame/mygame.svg and you are watching the directory mygame. if a file is modified or created in that directory you simply redraw mygame.svg (not caring what file it is - it could be an included sub-image)

for sit I made a win32 version, meaning I couldn't use the linux specific inotify, so I used a glib/gio wrapper pseudo-equivalent

rather than using sed, you could probably just store the strings in variables and echo > them for redraws (it will be faster anyways)

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
seaside

Joined: 11 Apr 2007
Posts: 886

PostPosted: Thu 06 Dec 2012, 20:44    Post subject:  

technosaurus wrote:


rather than using sed, you could probably just store the strings in variables and echo > them for redraws (it will be faster anyways)


jamesbond,

Not only works with svgame but on your very nicely done rox-clock as well -
Code:
j#!/bin/sh
# Copyright (C) jamesbond 2012
# License: GNU GPL Version 3 or later
#
# called by AppRun
#exec 2> /tmp/log-clock
APPDIR=${0%/*}

update_clock() {
   # date
   sed -i -e "s/ary>.*</ary>$(date +"%A %e %B %Y")</" \
${APPDIR}/AppInfo.xml
   
   # time
   time=$(date +%I-%M)
   hour=${time%-*}
   minute=${time#*-}
   hour=${hour#0}   
   minute=${minute#0}

   HROTATE=$(( $hour*30 + $minute/2 ))
   MROTATE=$(( $minute*6 ))
   #echo $hour_rot $min_rot
   
   echo '<?xml version="1.0" encoding="utf-8"?>
<svg xmlns="http://www.w3.org/2000/svg"
     xmlns:xlink="http://www.w3.org/1999/xlink"
     viewBox="-1024 -1024 2048 2048" width="600" height="600">
  <title>Swiss Railway Clock</title>
  <style type="text/css">
    .bg {stroke: none; fill: white;}
    .fc {stroke: none; fill: black;}
    .h1 {stroke: none; fill: black;}
    .h2 {stroke: none; fill: #aa0000;}
  </style>
  <defs>
    <path id="mark1" d="M -20,-1000 l 40,0 0,100 -40,0 z" />
    <path id="mark2" d="M -40,-1000 l 80,0 0,240 -80,0 z" />
    <path id="mark3" d="M -40,-1000 l 80,0 0,300 -80,0 z" />
    <path id="handh" d="M -50,-600  l 50,-50 50,50 0,800  -100,0 z" />
    <path id="handm" d="M -40,-900  l 40,-40 40,40 0,1180 -80,0  z" />
    <g    id="hands">
      <path d="M -10,-910 l  10,-10 10,10 2,300 -24,0 z
               M -13,-390 l  26,0         7,690 -40,0 z" />
      <path d="M   0,-620 a 120,120 0 0 1 0,240
                          a 120,120 0 0 1 0,-240 z
               M   0,-560 a  60,60  0 0 0 0,120
                          a  60,60  0 0 0 0,-120 z" />
    </g>
    <g id="face1">
      <use xlink:href="#mark1" transform="rotate(06)" />
      <use xlink:href="#mark1" transform="rotate(12)" />
      <use xlink:href="#mark1" transform="rotate(18)" />
      <use xlink:href="#mark1" transform="rotate(24)" />
    </g>
    <g id="face2">
      <use xlink:href="#face1" />
      <use xlink:href="#face1" transform="rotate(30)" />
      <use xlink:href="#face1" transform="rotate(60)" />
      <use xlink:href="#mark3" />
      <use xlink:href="#mark2" transform="rotate(30)" />
      <use xlink:href="#mark2" transform="rotate(60)" />
    </g>
    <g id="face">
      <use xlink:href="#face2" />
      <use xlink:href="#face2" transform="rotate(90)"  />
      <use xlink:href="#face2" transform="rotate(180)" />
      <use xlink:href="#face2" transform="rotate(270)" />
    </g>
  </defs>
  <circle class="bg" r="1024" />
  <use xlink:href="#face"  class="fc" />
  <use xlink:href="#handh" class="h1" transform="rotate('"$HROTATE"')" />
  <use xlink:href="#handm" class="h1" transform="rotate('"$MROTATE"')" />
  <!-- <use xlink:href="#hands" class="h2" transform="rotate(168)" /> -->
</svg> ' > ${APPDIR}/clock.svg   
   
   #sed -i -e "
#/\#handh/ s/rotate(.*)/rotate($hour_rot)/
#/\#handm/ s/rotate(.*)/rotate($min_rot)/
#" ${APPDIR}/clock.svg
   rox -x ${APPDIR}   
}

# 1. initial update
update_clock

# 2. sync to the start of the minute
while [ $(date +"%S") -ne 0 ]; do sleep 1; done

# 3. run the clock proper
while true; do
   update_clock
   sleep 60
done


Cheers,
s
Back to top
View user's profile Send private message 
jamesbond

Joined: 26 Feb 2007
Posts: 2186
Location: The Blue Marble

PostPosted: Fri 07 Dec 2012, 19:25    Post subject:  

technosaurus, seaside,

Thanks for the info Smile

cheers!

_________________
Fatdog64, Slacko and Puppeee user. Puppy user since 2.13.
Contributed Fatdog64 packages thread
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4351

PostPosted: Sun 23 Dec 2012, 17:36    Post subject:  

Todo: example of output mouse position on click

Maybe a dialog button.

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
linuph


Joined: 03 Jun 2012
Posts: 128
Location: Philippines

PostPosted: Sun 23 Dec 2012, 23:37    Post subject:  

I call this script 'control':
Quote:
<svg width="640" height="480">
<image x="1" y="1" width="640" height="480"
xlink:href="/usr/share/backgrounds/default.jpg" />
<image x="1" y="1" width="64" height="48"
xlink:href="/usr/share/pixmaps/buddy.png" />
</svg>


With 'svgame | control' I get an error:
./control: line 1: syntax error near unexpected token 'newline'
./control: line 1: '<svg width="300" height="300">'

The web is full with questions about this but I can't find a solution. Has it to to with line endings? Geany shows 'LF'. What should this script be? Just a script, or .xml or with #!/bin/sh in the top line.

A lot to learn here...
Back to top
View user's profile Send private message 
linuph


Joined: 03 Jun 2012
Posts: 128
Location: Philippines

PostPosted: Mon 24 Dec 2012, 00:04    Post subject:  

Double post
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 2 of 5 [63 Posts]   Goto page: Previous 1, 2, 3, 4, 5 Next
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

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
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1478s ][ Queries: 13 (0.0229s) ][ GZIP on ]