Using iSight
Wide
Using iSight
This document illustrates how to use iSight on your Mac using CocoaSequenceGrabber, an easy-to-use Cocoa framework for grabbing a photo from an iSight.
Preparation
- Install CocoaSequenceGrabber into /Library/Frameworks folder.
- Create a new Xcode project using Cocoa-Ruby Game Application template (avaialble at Downloads page).
- Add CocoaSequenceGrabber into "Frameworks -> Other Frameworks" group on the Xcode project.
- Add a new file (say Photogenic.rb) using "Ruby Game file" template.
Get CocoaSequenceGrabber ready to use from MiniKidsGames
First of all, you need to make CocoaSequenceGrabber class from ruby world. To do this, you can add a code as shown below at the beginning of your game file.
NSBundle.bundleWithPath('/Library/Frameworks/CocoaSequenceGrabber.framework').load
module OSX
ns_import :CSGCamera
ns_import :CSGImage
end
Write classes
Now you're ready to take a snapshot and save it onto Desktop. Here's the code for doing this. The following code works on the Game Framework. You can copy a snapshot to the Desktop by pressing space.
class PhotogenicModel < NSObject
def init()
# initialization
@size = NSSize.new(640, 480)
@buffer = NSImage.alloc.initWithSize(@size)
return self
end
def start()
@refreshed = false
@camera = CSGCamera.alloc.init
@camera.setDelegate(self)
@camera.startWithSize(@size);
@image = nil
@count = 0
end
def camera_didReceiveFrame(camera, frame)
@image = frame
@refreshed = true
end
def shoot()
@image.TIFFRepresentation.writeToFile_atomically("/Users/tat/Desktop/Shot#{@count}.tiff", true)
@count += 1
end
def stop()
@camera.stop if (@camera)
end
# Add some methods here
attr_reader :image, :refreshed, :size
end
class PhotogenicPlayingState < State
# define your timer IDs here
MYGAME_TIMER = 1000
def enter()
super()
@model.start()
# Define context dependent event handlers
# Each handlers are defined in a form of [EventType, Content, Proc object]
# See KeyEvent.rb, MouseEvent.rb and TimerEvent.rb for event specific info
# Other game's source code would also help you
contexts = [[TimerEvent::Fired, MYGAME_TIMER, proc {|id| tick() }],
[KeyEvent::NSKeyDown, KeyEvent::SPACE, proc {|event| @model.shoot(); true}]]
contexts.each {|context| EventContext.addContext(*context) }
@timer = TimerEvent.new(0.1, true, MYGAME_TIMER)
end
def tick()
# do something when timer event occurs
return true
end
def leave()
@model.stop()
super()
end
# add some event handlers or context handlers here
end
class PhotogenicView < View
def setModel(model)
@initialized = false
@point = NSPoint.new(0,0)
super(model)
end
#
# This method is called from MiniGamesView
#
def drawRect()
if (@initialized == false)
NSColor.blackColor.set
NSRectFill(@bounds)
@initialized = true
end
if (@model.image)
@point = Field.centerizedPoint(@model.image.size)
@model.image.compositeToPoint_operation(@point, NSCompositeXOR)
end
end
def getInvalidRects()
if (@model.refreshed)
return [NSRect.new(@point, @model.size)]
else
return super()
end
end
end
class Photogenic < Game
def initialize()
@bindings = [[PhotogenicPlayingState, PhotogenicModel, PhotogenicView]]
@initialState = PhotogenicPlayingState
end
def self.title() "Photogenic" end
def self.titleImage() nil end # return the name of an image here
def self.getPreferenceItems()
return [BoolPreference.new('Fullscreen', true),
BoolPreference.new('OpaqueWindow', false),
StringPreference.new('Font', 'Marker Felt'),
]
end
end
Downloads
Will be added soon







