Tag: purebasic

Embedding Python in PureBasic – Part 1

Posted by on December 24, 2012

The following is a bare-bones example of how to run a statement in python, from within PureBasic and then retrieve the result from Python. As said, the example has been stripped down for simplicity sake until you can get familiar with the Python API.

Requirements: python3.3, obtain it here.
The python33.lib file resides within Python33/python33.lib (assuming your install directory is Python33, of course)
On Windows, also, the python33.dll file resides in your system32 directory (at least for XP)

The example above may look over-complicated, in fact we could get rid of most checks since we know there has to be a main and a dictionary as we already defined a variable… But, you have to write safe code and this implies always checking your pointers/handles.

At this point we haven’t defined any structures pertinent to Python nor have we set any variables within PB so that we may read them from Python itself, but that’s something we’ll discuss later.

Right now this code could serve you to execute simple scripts for your games or applications. For instance on an RPG game you could calculate a melee attack using a few fixed rules and then obtain the result on PB.

Have fun,

(PB) Timeout Library

Posted by on December 12, 2012


This simple “timeout” library allows you to easily perform timed operations in your games and applications. Thanks to the callbacks you can also perform arbitrary functions within each update cycle.

The library is very simple to use, you must define a maximum timeout period and an update rate when you create an instance of TIMEOUT.

The “maximum” is in milliseconds and corresponds to the timeout period, for example 1000 would be 1 second timeout.

The “update_rate” sets how many updates there will be within 0 and the “maximum” in this case 1000), if we were to use 10 updates then we would experience the callback to be called every 100ms.

Of course both the callback and the user variables are optional, but they’re a handy feature.

Simple use case:

One of the many uses I have for this library includes waiting for a server to respond, sending timed events to clients, etc. I even use it to send notices to every client on the server before the server actually shuts down! (game server)
You can actually select whether the library should perform an actual sleep / delay operation to save cycles ( for example in cases where you have a waiting loop it’s ideal to sleep for a given period of time instead of wasting those cycles ) or you can have it compare old vs new time instead of performing a real delay. On timeout_update() the second parameter defines this behavior.

The library:


That’s all, enjoy!

If you have any comments, feel free to leave them here!
Any additions to the code will be welcomed.



(PB) Loading Assets, the simple way.

Posted by on January 23, 2012


Assets are an essential part of most games, how you manage them determines whether you spend more time working on them than dealing with them.

For small to medium games, loading an entire directory and having it referenced to a Map is the ideal solution. It’s both flexible and simple.

The following code allows you to do just this:

To use this code you must define a procedure of your own, this procedure is going to be called on each file to be loaded; you’ll have to load and process the file in this function.

Example use:

The example will attempt to scan through the directory “sprites”, it will execute your callback on each png file it finds inside the aforementioned directory, furthermore it’ll reference the filename (without extension) to the sprite() Map, so when you need a handle for the sprite called “fire.png” you’d just use sprite(“fire”) to obtain it.

Since the files are referenced to a Map by their actual name, you could easily implement a scripting system or any other dynamic management solution for your assets without much hassle.

An interesting part of the code is the return value, it’s actually the number of assets found (not the ones loaded, since your callback could choose not to load a certain file, for instance).

There are several limitations to this simple implementation, which is why I mentioned “small games” – There is no directory recursion, no advanced filtering options, no way to parallel the process on a separate thread and there’s no error handling at the moment.

However, it’s still very useful and I highly recommend you try it out, while it’s not a novel aproach, it’s always been the method I used to load my assets and it just works.

Have fun!

(PB) String Between Characters

Posted by on April 26, 2011

This is a small purebasic function used to retrieve a string between two known characters, it also allows you to provide a starting position.

Example use:

If you’re working on a parser or similar project this will sure come in handy.

PB Parallel Port Library

Posted by on January 12, 2011

What is it:

This is a minimalist  library based on the inpout32 DLL

Basically I wrapped the original library and added a whole bunch of useful constants to easily access the Control and Status registers while keeping things clean and simple.

This library is currently being used on a side project I’ve been working on with a friend, since he doesn’t have any μController experience we decided to use LPT for the time being.

The code:

And the constants include:

The hardware:

You don’t need any special hardware other than a Centronics or similar cable and a PC with a Parallel port (I’m sure some of you keep older PCs around for a good reason!) — Aside from this if you’re planning on running the examples you might want to get some LEDs and Switches.

Something to download:

The entire sources and support files including the examples can be found Here. Remember to exit the demo programs with the ESC key so the program gets a chance to reset the output bits.

Useful companion:

During development you may want to keep an eye on the status of each pin, however not everyone has a breakout board for this particular interface so you may want to use a software version of this concept instead, one of my favourite ones is LPT.exe — It works fairly well and there are several ports to different languages in case you’re interested in modifying it to suit your own needs.

Closing up:

That’s all for now, hopefully I’ll be able to finish the project and post some of the code here. Hint: it involves controlling unipolar steppers, computer vision and webcams!

Can you guess what it is?