lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



LuaX SDL binding uses bitwise operators, it just makes so much more sense to have 'flag' kind of arguments resemble the original C SDL API.

This is done using operators such that:

Binary or:
	print ( SDL_INIT_VIDEO..SDL_INIT_AUDIO )    -- 48 :)
print ( type( SDL_INIT_VIDEO..SDL_INIT_AUDIO ) ) -- "enum [SDL_INIT]" (LuaX has overridden 'type')

	if SDL_Init( SDL_INIT_VIDEO..SDL_INIT_AUDIO ) ~= 0 then
	    error( "Unable to initialize SDL: "..SDL_GetError() )
	end

	-- Set transparent color to black (?)
	s:SetColorKey( SDL_SRCCOLORKEY..SDL_RLEACCEL, SDL_Color(0) )

I feel the .. operator match conceptually nicely with logical or, "adding" the two flags together.

Binary test (similar to and):

	if gamestate.screen.flags(SDL_HWSURFACE) then
		print "Screen is in video memory"
	else
		print "Screen is in system memory"
	end
	if gamestate.screen.flags(SDL_DOUBLEBUF) then
		print "Screen has double-buffering enabled"
	end

This is interesting. 'flags' is the bitfield, and its call operator has been set to do logical and, thus testing for whether a certain bit (s) are there. Finding this readable, too.


For others, I've used the call method, too:

  // bool= call( ud_enum a, ["test",] ud_enum b, ... )
  // ud_enum= call( ud_enum a, "or"|"and"|"xor", ud_enum b, ... )
  // ud_enum= call( ud_enum a, "xor", [ud_enum b, ...] )
// ud_enum= call( ud_enum a, "not" ) (same as 'xor' without arguments)
  // ud_enum= call( ud_enum a, "<<"|">>" [,int=1] )
  // num= call( ud_enum a, "number" )
  // str= call( ud_enum a, "string" [,base_uint=10 [,width_uint]] )


Also, [] is in use:

  // int= index( ud_enum a, ud_enum key )
  // int= index( ud_enum a, uint bit )    <-- experimental!
  // int= index( ud_enum a, "value" )
  //
// Index notation can be used for testing against a certain flag bit or a masked value
  // within the enum. I.e. "var[HAS_FULLSCREEN]"

var.value can be used for getting the numeric value of a bitfield.


  // Setting bits by the table notation
  //
  //      myflags[FULLSCREEN]= true
  //      myflags[BITFIELD]= 10
  //      myflags[BITFIELD]= PRESET_VALUE
  //      myflags[20]= 1

That's all!  :)

Currently, this code is within gluax.c, but could be extracted into a neat separate module. require "bitwise" or similar. There is one place, where redefinition of 'type()' is required (to keep bitfields of conceptually different types separate, and not reported as "userdata"). Otherwise, it's all built upon regular Lua methods. :) Lua bends, it does!

-asko

Attachment: shower_new.lua
Description: Binary data



Roberto Ierusalimschy kirjoitti 20.9.2006 kello 23.51:

Actually, I think he was just talking about needing to use 'and', 'or' and
'not' instead of '&&', '||', and '!' --- Lua *has* boolean operators.

But I think you were talking about logical operators, which stock Lua doesn't
have, [...]

Sure. Sorry about the confusion. (I should call them bitwise operators.)

-- Roberto