Module: AAlib

Includes:
ArgumentChecks
Defined in:
lib/aalib.rb

Overview

This module provides initialization methods and other miscellaneous methods.

Defined Under Namespace

Modules: ArgumentChecks, Attr, AttrMask, Dither, Foreign, Key, Mouse Classes: CPtr, Context, Driver, Error, Font, HardwareParams, RenderParams, SaveData, SaveFormat

Constant Summary collapse

SENDRELEASE =
1
KBDALLMASK =
1

Class Method Summary collapse

Methods included from ArgumentChecks

included

Class Method Details

.array_from_null_terminated_c(ptr, target_class) ⇒ Object

Converts a NULL terminated C pointer array into a Ruby array of objects made by talling target_class.new() on each DL::PtrData in the C array.



362
363
364
365
366
367
368
369
370
371
372
# File 'lib/aalib.rb', line 362

def array_from_null_terminated_c(ptr, target_class)  #:nodoc:
  targets = Array.new
  loop do
    ptr.struct!('P', :target)
    target = ptr[:target]
    break unless target
    targets << target_class.new(target)
    ptr += DL.sizeof('P')
  end
  targets
end

.autoinit(hardware_params = HardwareParams::DEFAULT) ⇒ Object

Initializes AA-lib. Attempts to find an available output driver supporting the optionally specified hardware_params. First attempts to initialize the recommended drivers and then in order drivers available in the AAlib.drivers array.

Returns an AAlib::Context on success or nil on failure.



293
294
295
296
297
298
# File 'lib/aalib.rb', line 293

def autoinit(hardware_params=HardwareParams::DEFAULT)
  check_hardware_params(hardware_params)

  ptr, garbage = Foreign.autoinit(hardware_params)
  Context.new(ptr)
end

.driversObject

Returns array of supported display drivers. See AAlib::Driver.



328
329
330
# File 'lib/aalib.rb', line 328

def drivers
  array_from_null_terminated_c(Foreign.drivers, Driver)
end

.formatsObject

Returns array of supported save formats. See AAlib::SaveFormat.



334
335
336
# File 'lib/aalib.rb', line 334

def formats
  array_from_null_terminated_c(Foreign.formats, SaveFormat)
end

.helpObject

Returns the AAlib command line options help text. Note that the text is formatted to 74 columns.



245
246
247
248
249
# File 'lib/aalib.rb', line 245

def help
  help = Foreign.help
  help.struct!('S', :text)
  help[:text].to_s
end

.init(driver, hardware_params = HardwareParams::DEFAULT, driver_opts = nil) ⇒ Object

Initializes AA-lib using the specified driver from AAlib.drivers and optionally specified hardware_params. Use driver_opts to pass extra options to a hardware driver. For example, pass an instance of AAlib::SaveData when using AAlib.save_driver. Note that driver_opts is a required argument when using the save driver.

Returns an AAlib::Context on success or nil on failure.



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/aalib.rb', line 308

def init(driver, hardware_params=HardwareParams::DEFAULT, driver_opts=nil)
  check_type(driver, "driver", AAlib::Driver)

  if driver == AAlib.save_driver
    if driver_opts == nil
      msg = "AAlib::SaveData required as third argument when using save driver"
      raise ArgumentError.new(msg)
    else
      check_type(driver_opts, "driver_opts", AAlib::SaveData)
    end
  end

  check_hardware_params(hardware_params)

  ptr, garbage = Foreign.init(driver, hardware_params, driver_opts)
  Context.new(ptr)
end

.memory_driverObject

Returns an AAlib::Driver for an in-memory context for custom ascii-art output. should be passed to AAlib.init.



341
342
343
# File 'lib/aalib.rb', line 341

def memory_driver
  AAlib::Driver.new(Foreign.mem_driver)
end

.parseoptions(hardware_params, render_params, argv = ARGV) ⇒ Object

Parse commandline options from the given Array of Strings, argv, parsing AAlib options and removing them from the array. Fills in hardware_params and render_params with appropriate values based on the commandline arguments. It is expected that these parameters be used to initialize AAlib and render graphics.

Note that this function replaces the strings in the given argv with new, duplicate strings.



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/aalib.rb', line 260

def parseoptions(hardware_params, render_params, argv=ARGV)
  check_hardware_params(hardware_params)
  check_render_params(render_params)

  cargv = [$0, argv].flatten  # C argv includes $0

  cargc = DL.malloc(DL.sizeof('I'))
  cargc.struct!('I', :num)
  cargc[:num] = cargv.size

  r,rs = Foreign.parseoptions(hardware_params, render_params,
                              cargc, cargv)

  if r == 1  # success
    rs[2].struct!('I', :num)
    len = rs[2][:num]

    newargv = rs[3].to_a('S', len)
    newargv.shift  # remove $0 that we added previously
    argv.replace newargv
    true
  else
    false
  end
end

.save_driverObject

Returns an AAlib::Driver for saving a AAlib::Context to disk in various formats; should be passed to AAlib.init.



348
349
350
# File 'lib/aalib.rb', line 348

def save_driver
  AAlib::Driver.new(Foreign.save_driver)
end