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 Mon 22 Dec 2014, 18:19
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 1 of 5 [63 Posts]   Goto page: 1, 2, 3, 4, 5 Next
Author Message
technosaurus


Joined: 18 May 2008
Posts: 4427

PostPosted: Wed 15 Aug 2012, 03:16    Post subject:  simple game framework for scripting languages  

You can get free game resources (sprites, tilesets, sounds, backgrounds...) here:
http://funplosion.com/free-assets.html
http://stackoverflow.com/questions/143050/

Here is what I have so far:
Code:
#include <sys/inotify.h>
#include <gtk/gtk.h>
#define IMAGE "svgame.svg"
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*/

int main(int argc, char *argv[]){
   int watch, fd;   GtkWidget *window, *image;
gtk_init (&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
fd = inotify_init();
image = gtk_image_new_from_file(IMAGE);
gtk_container_add(GTK_CONTAINER(window), image);
watch = inotify_add_watch( fd, IMAGE, IN_MODIFY ); /* more events? */
gdk_input_add( fd, GDK_INPUT_READ, refresh, image ); /* refresh on inotify */
g_signal_connect((gpointer) window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(window);
gtk_main ();}
Yes, it is intentionally sparse by design, just the minimum to allow a scripting language to do the rest.
TODO add a callback to printf key-presses and mouse-clicks (until then, control is decoupled)

EDIT: see my next post for the mouse/key actions

Just to give you an idea, you can do something like this:
Code:

<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>

and use your favorite scripting language to move your "sprite" (buddy) around on the background just by changing the x and y, for instance for a move right, the new code is:
Code:

<svg width="640" height="480">
<image x="1" y="1" width="640" height="480"
     xlink:href="/usr/share/backgrounds/default.jpg" />
<image x="65" y="49" width="64" height="48"
     xlink:href="/usr/share/pixmaps/buddy.png" />
</svg>


types of games: matching, block pushing, maze, strategy,... probably a lot more, but then you also need to figure out the game logic (sold separately)

(oops I accidently created a webcam viewer - use ffmpeg to generate images of the same name defined by IMAGE)
(oops2 ... can do a slideshow also)
(oops3 ... and presentations via svg)
(oops4 ... someone on IRC also suggested using svg to build a nicer widget/toolkit)

Eventually this could be shifted to using libtinysvg and cairo-xcb?? for a multithread capable 2d toolkit.

P.S. If Thunor is reading this, it would be nice to have a autorefresh="true" flag on images in gtkdialog to do the same thing I am doing here. I could help come up with a patch, but this was just a quick proof of concept, no frills adaptation of my simple icon tray.
svgame.tar.gz
Description  Here is a precompiled version.that takes an input filename as 1st arg.
usage:
svgame /path/to/image.svg | your_controls
gz

 Download 
Filename  svgame.tar.gz 
Filesize  2.19 KB 
Downloaded  329 Time(s) 

_________________
Web Programming - Pet Packaging 100 & 101

Last edited by technosaurus on Thu 16 Aug 2012, 19:24; edited 3 times in total
Back to top
View user's profile Send private message 
vovchik


Joined: 23 Oct 2006
Posts: 1287
Location: Ukraine

PostPosted: Wed 15 Aug 2012, 13:50    Post subject:  

Dear technosaurus,

Looks very clever and useful. Thanks....

With kind regards,
vovchik
Back to top
View user's profile Send private message 
seaside

Joined: 11 Apr 2007
Posts: 888

PostPosted: Wed 15 Aug 2012, 17:01    Post subject:  

technosaurus,

Another great seed for expansion. They just keep coming.

Regards and thanks,
s
(Now if I could only think of some unusual game - perhaps one where you can cheat, but it doesn't appear that way Very Happy )
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4427

PostPosted: Wed 15 Aug 2012, 21:30    Post subject:  

Added key and mouse events - now you can pipe the output to a while read LINE; do case *)... stlye event handler
Code:
#include <sys/inotify.h>
#include <gtk/gtk.h>
#define IMAGE "svgame.svg"

void key_press_event(GtkWidget *widget, GdkEventKey *event){
   g_print("KEY=%s\n",gdk_keyval_name(event->keyval));}
void button_press_event(GtkWidget *widget, GdkEventButton *event){
   g_print("BUTTON=%f,%f\n",event->x ,event->y);}
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*/

int main(int argc, char *argv[]){
   int watch, fd;   GtkWidget *window, *image, *eventbox;
gtk_init (&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
eventbox = gtk_event_box_new ();
gtk_container_add(GTK_CONTAINER(window), eventbox);
fd = inotify_init();
image = gtk_image_new_from_file(IMAGE);
gtk_container_add(GTK_CONTAINER(eventbox), image);
watch = inotify_add_watch( fd, IMAGE, IN_MODIFY ); /* more events? */
gdk_input_add( fd, GDK_INPUT_READ, refresh, image ); /* refresh on inotify */
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 ();}
I am leaving the original code in case someone wants to keep it decoupled and use a terminal window for controls or something.

Anyone remember Adventures of Lolo? (a block pushing game like that may work well) ... assuming you are using bash or some other language with arrays you could build a grid of obstacles and use stock images in each grid (scenery) using a 2D array (strategy games would be similar, except you may need various extra dimensions )

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


Joined: 23 Oct 2006
Posts: 1287
Location: Ukraine

PostPosted: Thu 16 Aug 2012, 07:10    Post subject:  

Dear technosaurus,

Hats off! It is very good and interesting. I'll do a Bacon port, too. Simple and versatile. With move instructions in the svg, placement is easy. Thanks.

With kind regards,
vovchik

PS. Lately I have been using lib_rsvg so that I can keep the svg not on disk but in the script and use gdk_pixbuf to create the pixmap on the fly. I will experiment.
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4427

PostPosted: Thu 16 Aug 2012, 16:10    Post subject:  

I just made a patch for gtkdialog to autorefresh icons when the image file modified. So now you can use it instead if you would like more features. http://murga-linux.com/puppy/viewtopic.php?p=647004#647004
_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
seaside

Joined: 11 Apr 2007
Posts: 888

PostPosted: Fri 17 Aug 2012, 15:48    Post subject:  

technosaurus,

Started to play around with this and noticed that the background does not fill the window using the example.

Compiled in racy and I have two white bands as below.

Any ideas on how to fix?

Regards,
s
game.png
 Description   Window background
 Filesize   159.19 KB
 Viewed   908 Time(s)

game.png

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


Joined: 18 May 2008
Posts: 4427

PostPosted: Fri 17 Aug 2012, 17:21    Post subject:  

In your background image use 100% for the width and height, but set the size of the svg image to fixed integers.(i am using 640x480 as the default size)
_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
seaside

Joined: 11 Apr 2007
Posts: 888

PostPosted: Fri 17 Aug 2012, 17:49    Post subject:  

technosaurus wrote:
In your background image use 100% for the width and height, but set the size of the svg image to fixed integers.(i am using 640x480 as the default size)


technosaurus,

Thanks. I didn't realize my "/usr/share/backgrounds/default.jpg" was 1024 X 576 Very Happy

All now works well..
regards,
s
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4427

PostPosted: Sat 18 Aug 2012, 01:08    Post subject:  

here is some stuff to do some stuff ... though only about as good as this description

Code:
#!/bin/ash
#use a 13x10 grid to draw a scene - very hacky
draw_svg(){
i=0
SVG='<svg width="640" height="480">
<image x="1" y="1" width="640" height="480" xlink:href="'$BACKGROUND'" />
'
while ([ $i -lt ${#1} ]) do
#replace with eval echo \${GRID:$i:1}
[ "${1:$i:1}" != " " ] && SVG=$SVG'   <image x="'$(($i % 13 * 48))'" y="'$(($i / 13 * 48))'" width="48" height="48" xlink:href="'`eval echo \$"${1:$i:1}"`'" />
'
i=$(($i+1))
done
echo "$SVG</svg>" >svgame.svg
}


BASEDIR="/usr/share/pixmaps"
BACKGROUND="/usr/share/backgrounds/default.jpg"

#faking a 12x9 2d array for bourne shells using a string and mod/div ops.
#each letter represents a 48x48 pixel square on a 640x480 grid
GRID=""
GRID="${GRID}             "
GRID="${GRID}             "
GRID="${GRID}             "
GRID="${GRID}             "
GRID="${GRID}      p      "
GRID="${GRID}          q  "
GRID="${GRID}             "
GRID="${GRID}             "
GRID="${GRID}             "
GRID="${GRID}             "
#Sprites and Tilesets
p="$BASEDIR/ayttm.png"
q="$BASEDIR/buddy.png"
draw_svg "${GRID}"

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

Joined: 11 Apr 2007
Posts: 888

PostPosted: Sat 18 Aug 2012, 16:29    Post subject:  

technosaurus wrote:
here is some stuff to do some stuff ...


technosaurus,

Sounds like a perfect description to me, and might even be a good candidate forum category itself Very Happy

Thanks for this handy tool.

Regards,
s
(This is especially useful for me because I keep forgetting that x,y coordinates for drawing start in the upper left corner Confused )
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4427

PostPosted: Mon 20 Aug 2012, 21:36    Post subject:  

Looks like we can even use this for creating tray applets using the swallow tag.
_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
seaside

Joined: 11 Apr 2007
Posts: 888

PostPosted: Wed 22 Aug 2012, 15:54    Post subject:  

technosaurus wrote:
Looks like we can even use this for creating tray applets using the swallow tag.


technosaurus,

Just wondering what you had in mind? Would this be instead of or in addition to SIT.

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


Joined: 18 May 2008
Posts: 4427

PostPosted: Wed 22 Aug 2012, 18:02    Post subject:  

seaside wrote:
technosaurus wrote:
Looks like we can even use this for creating tray applets using the swallow tag.


technosaurus,

Just wondering what you had in mind? Would this be instead of or in addition to SIT.

Regards
s
either, but also for goingnuts' gtkdialog1 to have "attached" controls (or use my patch for gtkdialog4)... Swallow can have non-square dimensions though
_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
seaside

Joined: 11 Apr 2007
Posts: 888

PostPosted: Thu 23 Aug 2012, 11:15    Post subject:  

technosaurus,

Thanks for the explanation for SIT and svgame.

I tried this-
Code:
svgame  /root/svgame.svg  2>/dev/null  | \
while read LINE; do
case "$LINE" in
KEY=F3)  geany ;;
*) exit
;;
esac
done


The geany command did not run until after the image window was closed when detecting key F3. Is there a way to execute commands and keep the image window open?

Regards,
s
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 5 [63 Posts]   Goto page: 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.0970s ][ Queries: 13 (0.0056s) ][ GZIP on ]