(PB) Loading Assets, the simple way.

Posted by on January 23, 2012

Intro

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:

Prototype.i LOAD_DIRECTORY_CALLBACK( Map Assets.i(), directory.s, extension.s, name.s, userDefined.i = #Null )
 
Procedure.i LoadDirectory( Map assets.i(), directory.s, extension.s, *callback.LOAD_DIRECTORY_CALLBACK, userDefined.i = #Null )
 
	Define.i dir = ExamineDirectory( #PB_Any, directory, "*" + extension )
	If IsDirectory( dir )
 
		Define.i count = 0
		Define.s name = ""
 
		While NextDirectoryEntry( dir )
			If DirectoryEntryType( dir ) = #PB_DirectoryEntry_File
				name = DirectoryEntryName( dir );
				assets( ReplaceString( name, extension, "" ) ) = *callback( assets(), directory, extension, name, userDefined );LoadSound( #PB_Any, directory + "\" + name )
				count + 1
			EndIf
		Wend
 
		FinishDirectory( dir )
	EndIf
	ProcedureReturn count
 
EndProcedure

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:

Global NewMap sprite.i()
Procedure.i callback_loadsprites( Map Assets.i(), directory.s, extension.s, name.s, userDefined.i = #Null )
	ProcedureReturn LoadSprite( #PB_Any, directory + "\" + name, userDefined )
EndProcedure
 
LoadDirectory( sprite(), "sprites", ".png", @callback_loadsprites() )

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!

Well, This sucks.

Posted by on December 21, 2011

Technically homeless, in a hotel waiting to meet with the realtors in regard with the property I was going to acquire…

They keep BSing me about this and that with stupid constraints and whatnot, so I really need to get some lawyers down there first.

What also sucks is that they have to make sure the owner is available for the meeting otherwise it’ll be a futile exercise.

I’m trying to get this over with ASAP but it seems to be out of reach.

The biggest issues right now are having my pets living in a friggin’ vet store, which by the way they won’t be able to keep ‘em for longer than a few days, borderline Saturday. In fact, it took half a day of calls and walking half the damn town to find a place that would keep them in such a short notice (met with a clerk at night, they were closed but the guy opened anyway and told me to come back at the morning.

I had to get to downtown to seal the deal at 11AM, woke up at 7 or 8AM if I recall after going to sleep at 3-4AM — slept on the floor, wasn’t comfy but hey it helps with the back pain at least! — and I still had things to do at “home” before I left for good, however the pain from moving everything the day before was just too much to bare so I had to leave a few untied knots here and there; which I’ll have to deal with today or tomorrow.

Right now I’m using the shitty hotel’s WIFI, which is also shitty — obviously. The password is bosh780 by the way.

What really, really bothers me aside from all this crap is the price, they charge premium for an “apart hotel” room that I would never ever take if it wasn’t for the fact that every other place was fully booked.

I mean seriously, there are no facilities at all, the breakfast was a-ok but if I had to choose, I’d rather sleep in a car; which by the way is at the shop right now so yeah…

Anyway, f*** real estate agencies, in the arse with a machinegun.

See ya.

Off for a while…

Posted by on December 13, 2011

Well, it’s no secret to some — I’m moving out — And this means I’ll be away for a while. I’ll try to get online as soon as possible (not that any of you care!) and start posting some of my drafts right away; I have quite some interesting notes to share. However I’m not sure about the Internet providers and the quality of service at the new place, I may even have to resort to “stealing” WIFI while I figure out the ISPs.

It’s worth noting that right now the site has a low priority as well, I need to figure out how to get the massive amount of stuff I own moved from point A to point B without getting stuff damaged or stolen in the process. I also need to deal with a lot of paperwork and this requires a lot of time and dedication, so that’s where I’ll be focusing for the moment.

Hopefully I’ll be able to start with some nice weekly video entries as well once I’m half settled down.

There are lots of things that need to be done in the new house and I would like to share the process with everyone. Not everything is going to be electronics related, but I reckon some of it will be :)

Either way, have a good one!

Gus

PS: Shit this whole thing is stressful. But if it were easy it wouldn’t be worth it.

Mains safety: How not to pop the breakers.

Posted by on December 8, 2011

So you’ve been troubleshooting some device or fooling around with a mains circuit and pop, goes the breaker. Well OK, let’s discuss a few ways of dealing with this situation.

Here are some common methods:

  1. Fuses
  2. Ballasts
  3. Secondary, lower rated breaker
  4. Dedicated line

 

The methods explained:

Fuses

The first and probably simplest method is to use fuses, however it soon becomes apparent that it’s also an expensive idea. Fuses should be, in my opinion, one of the last defences.

Another problem comes from an improperly wired plug (or non polarized as is the case in some countries) then you could have an open fuse by the netural side, but your device could still be at live potential! — This could be lethal.

So even if you’ve got an open fuse, don’t assume it’s safe to get your paws in there just yet, unplug it first. Likewise with power switches, if they aren’t bipolar then don’t assume, be smart.

Ballasts

As simple to implement as a fuse, but it won’t cut-out if you short the line, it will just provide whatever current the ballast lets through.

A ballast can be inductive or resistive, typically you’d want to use a resistive ballast, examples are room heaters, incandescent light bulbs, etc.

The down-side here is that if you were to use a ballast big or small enough, you can actually cause more damage with it than without it.

Explanation:

A “big” ballast (low value, allows for a lot of current) will most likely damage your device in case of a dead short, because it will continue to use your device as the current path, whereas a breaker would most likely trip open very quickly. Now if your device failed short, something already went wrong, but if you keep powering this device, chain reactions can occur and more damage can be caused. Because of this, sizing the ballasts properly is very important.

A “small” ballast (high value, allows for very little current) will display a noticeable resistive divider behavior between itself and the load, this means that the load (device) will see a lower voltage and this can cause trouble in certain cases, for example poorly (or cheaply) designed switch mode power supplies can have it’s switching / rectifier sections fail due to over-current (lower voltage forces the controller to use a higher duty cycle to compensate for this, thus longer “ON” times are seen). This can actually lead to yet another chain-reaction event.

Remember, as the smps starts up it needs to charge the input filter capacitors and this is a relatively heavy load, at this stage the voltage on the power supply may be way below it’s minimum specified rating thanks to your ballast.

Secondary breaker

You can wire up a box with an inlet and an outlet with a small breaker in between, typically breakers have a higher trip current than rated to allow for high inrush currents as seen on inductive loads such as motors, etc. So definetly do take this into consideration.

One issue however is that lower rated breakers can either be harder to find or more expensive, so keep that in mind as well.

Dedicated line

Having a dedicated line just for testing devices is ideal, if you add a secondary breaker and a properly sized ballast where applicable, you’ll never have to worry about blacking out or damaging something ever again.

You can add your own ground fault (switchable in and out) as well as residual current devices, etc. I would even suggest implementing phase indicators like you’d find on most properly installed residential panels.

 
 

It should be clear that a combination of all methods can only be ideal if it suits your needs, so there is no definitive answer here. Investigate, experiment and figure out what’s best for you.

Cheers.

 
 

Safety 101: A word on mains and safety. P1

Posted by on December 4, 2011

It is said that you can disrupt the normal heart rhythm by passing no more than 10mA across the heart, this should be enough to cause fibrillation in most cases. What this means is that eventually you’ll die of cardiac arrest if you don’t get defibrilated in time (assuming of course you are in a shockable rhythm or can be brought to one to begin with). Heart failure is just one of the many risks involved.

Because of this you’d want to take precautions when working with mains at home, whether it’s 110VAC or 220VAC it doesn’t matter; both can be lethal if the right (or wrong) conditions are met.

A big part of safety comes from your behavior. As a general rule of thumb never grab things with both hands, specially if they have a metallic chasis and they happen to be energized — This is why most people will tell you to place your left hand behind your back or in your pocket, the reason for this is just so you don’t create that return path through your ticker with your hand by touching something that happens to be on the neutral side of the circuit while on the other hand you may be mistakenly touching the live side and vice versa.

Now, given the right conditions you could be grounded and thus your feet, or legs (point of contact) would become the return path, if you are touching live with your left hand chances are you may not make it.

So you can’t assume any safety measures are in place or functional, for instance the earth / ground wire or an RCD may be damaged or non existent. Even if they are tested and functional you still have to conduct proper behavior to avoid accidents.

Remember: Behavior. Don’t assume, don’t fool around — Be smart or be with Darwin.

We’ll discuss how to safely work on mains related projects at home on the next entry.