ruboids (on mac os) - singleton instance() returns nil?

  • Thread starter Michal 'hramrach' Suchanek
  • Start date
M

Michal 'hramrach' Suchanek

Hello

I wanted some OpenGL demo for ruby and the first thing I found was
ruboids.
I had some problems compiling qtruby because it uses libtool. But now I
can run some qtruby examples so it should work.

But when I tried running ruboids I found some things are named
differently in my setup (see patch). I disabled those for which there
was no apparent equivalent to see if I get at least some window as a
start. For testing purposes I also symlinked Canvas.rb to MyCanvas.rb

The thing that fais is that there is no canvas= method in singleton
instance of World although it has attr_accessor for :canvas.

Also I can get a World instance by new() but I get nil from instance()

DISPLAY=:0 ruby ruboids.rb ../boids.properties
±./MyCanvas.rb:27: warning: don't put space before argument parentheses
QLayout: Adding MyCanvas/unnamed (top-level widget) to layout for
WorldWindow/unnamed
QLayout "unnamed" added to WorldWindow "unnamed", which already has a
layout
QLayout "unnamed" added to WorldWindow "unnamed", which already has a
layout
ruboids.rb:29: undefined method `canvas=' for nil:NilClass
(NoMethodError)

I get the expected behavior if I test with a simple class in irb but I
get non-singleton if I subclass Qt::Object.

suchanek:~ hramrach$ irb
irb(main):001:0> require "qt"
requi=> true
irb(main):002:0> require "singleton"
=> true
irb(main):003:0> class Single < Qt::Object
irb(main):004:1> include Singleton
irb(main):005:1> def initialize
irb(main):006:2> STDERR.puts inspect
irb(main):007:2> end
irb(main):008:1> end
=> nil
irb(main):009:0> Single.new
=> #<Qt::Object:0x7b685c>
irb(main):010:0> Single.instance
#<Single:0x7b4fd4>
=> nil
irb(main):011:0> Single.new
=> #<Qt::Object:0x7b3878>
irb(main):012:0>

Is it impossble to base singletons on QObject?

Thanks

Michal Suchanek

Just for reference:
http://www.io.com/~jimm/downloads/ruboids/


diff -ur ruboids-0.0.1.org/ruboids/BoidView.rb ruboids-0.0.1/ruboids/BoidView.rb
--- ruboids-0.0.1.org/ruboids/BoidView.rb Wed May 30 17:39:43 2001
+++ ruboids-0.0.1/ruboids/BoidView.rb Tue Sep 28 03:33:09 2004
@@ -11,7 +11,7 @@

BODY_COLOR = [0, 0, 0]
BEAK_COLOR = [0.75, 0.5, 0.0]
- SHADOW_COLOR = [.25, .55, .25]
+ SHADOW_COLOR = [0.25, 0.55, 0.25]

HALF_WING_BASE = 3
HALF_LENGTH = 5
diff -ur ruboids-0.0.1.org/ruboids/CameraDialog.rb ruboids-0.0.1/ruboids/CameraDialog.rb
--- ruboids-0.0.1.org/ruboids/CameraDialog.rb Wed May 30 17:39:43 2001
+++ ruboids-0.0.1/ruboids/CameraDialog.rb Tue Sep 28 16:46:27 2004
@@ -28,7 +28,7 @@
end
end

-class CameraDialog < QDialog
+class CameraDialog < Qt::Dialog

def initialize(parent)
@locAdjustors = []
diff -ur ruboids-0.0.1.org/ruboids/Canvas.rb ruboids-0.0.1/ruboids/Canvas.rb
--- ruboids-0.0.1.org/ruboids/Canvas.rb Wed May 30 17:39:43 2001
+++ ruboids-0.0.1/ruboids/Canvas.rb Tue Sep 28 17:34:00 2004
@@ -7,7 +7,7 @@

require 'qt'
require 'opengl'
-require 'qgl'
+#require 'qgl'
require 'World'
require 'Cloud'
require 'Flock'
@@ -16,16 +16,17 @@

include GL

-class Canvas < QRGLWidget
+class MyCanvas < Qt::GLWidget

- GRASS_COLOR = [0, .75, 0]
+ GRASS_COLOR = [0, 0.75, 0]
MDA_ROTATE = :MDA_ROTATE
MDA_ZOOM = :MDA_ZOOM
MDA_CHANGE_FOCUS = :MDA_CHANGE_FOCUS

def initialize(parent = nil, name = '')
+ super ()
@grassObject = nil
- catchEvent
+ #catchEvent
end

def update
diff -ur ruboids-0.0.1.org/ruboids/CloudView.rb ruboids-0.0.1/ruboids/CloudView.rb
--- ruboids-0.0.1.org/ruboids/CloudView.rb Wed May 30 17:39:43 2001
+++ ruboids-0.0.1/ruboids/CloudView.rb Tue Sep 28 03:31:10 2004
@@ -5,7 +5,7 @@
# http://www.ruby-lang.org/en/LICENSE.txt.
#

-require 'qgl'
+require 'qt'
require 'View'
require 'Cloud'
require 'Params'
Only in ruboids-0.0.1/ruboids: MyCanvas.rb
diff -ur ruboids-0.0.1.org/ruboids/View.rb ruboids-0.0.1/ruboids/View.rb
--- ruboids-0.0.1.org/ruboids/View.rb Wed May 30 17:39:43 2001
+++ ruboids-0.0.1/ruboids/View.rb Tue Sep 28 17:28:44 2004
@@ -8,7 +8,7 @@
# A lightweight view
class View

- SHADOW_COLOR = [ .25, .25, .25 ]
+ SHADOW_COLOR = [ 0.25, 0.25, 0.25 ]

attr_accessor :model, :color, :eek:bject, :shadow

@@ -74,7 +74,7 @@
shadowColor = []
SHADOW_COLOR.each_with_index { | c0, i |
min = c0
- max = Canvas::GRASS_COLOR
+ max = MyCanvas::GRASS_COLOR
if min > max
tmp = min
min = max
diff -ur ruboids-0.0.1.org/ruboids/World.rb ruboids-0.0.1/ruboids/World.rb
--- ruboids-0.0.1.org/ruboids/World.rb Wed May 30 17:39:43 2001
+++ ruboids-0.0.1/ruboids/World.rb Tue Sep 28 17:29:32 2004
@@ -12,9 +12,9 @@
require 'Flock'
require 'Boid'
require 'Camera'
-require 'Canvas'
+require 'MyCanvas'

-class World < QObject
+class World < Qt::Object

include Singleton

@@ -48,8 +48,8 @@
@flock.add(b) # flock will delete boid
}

- @clock = QTimer.new()
- connect(@clock, QSIGNAL('timeout()'), self, 'slotMove')
+ @clock = Qt::Timer.new()
+ # connect(@clock, Qt::SIGNAL('timeout()'), self, 'slotMove')

@camera = Camera.new # Reads values from params
setupTranslation()
diff -ur ruboids-0.0.1.org/ruboids/WorldWindow.rb ruboids-0.0.1/ruboids/WorldWindow.rb
--- ruboids-0.0.1.org/ruboids/WorldWindow.rb Wed May 30 17:39:43 2001
+++ ruboids-0.0.1/ruboids/WorldWindow.rb Tue Sep 28 17:32:54 2004
@@ -6,10 +6,10 @@
#

require 'qt'
-require 'Canvas'
+require 'MyCanvas'
require 'CameraDialog'

-class WorldWindow < QMainWindow
+class WorldWindow < Qt::MainWindow

MENU_CAMERA_DIALOG = 1

@@ -18,11 +18,11 @@
def initialize
super
setCaption("Boids")
- vLayout = QVBoxLayout.new(self, 0, 0, "vLayout")
+ vLayout = Qt::VBoxLayout.new(self, 0, 0, "vLayout")
mbar = setupMenubar()
vLayout.setMenuBar(mbar)

- @canvas = Canvas.new(self)
+ @canvas = MyCanvas.new(self,"TheDamnCanvas")
vLayout.addWidget(@canvas)

vLayout.activate()
@@ -33,23 +33,23 @@

def setupMenubar
# Create menu bar
- mbar = QMenuBar.new(self)
+ mbar = Qt::MenuBar.new(self)

# Create and populate file menu
- menu = QPopupMenu.new()
- menu.insertItemQObj("Exit", QApplication::qApp, QSLOT("quit()"),
- CTRL+Key_Q)
+ menu = Qt::popupMenu.new()
+ # menu.insertItemQObj("Exit", Qt::Application::qApp, Qt::SLOT("quit()"),
+ #CTRL+Key_Q)

# Add file menu to menu bar
mbar.insertItem("&File", menu)

# Create and populate options menu
- menu = QPopupMenu.new()
+ menu = Qt::popupMenu.new()
menu.insertItem("&Camera...", MENU_CAMERA_DIALOG, -1)

# Add options menu to menu bar and link it to method below
mbar.insertItem("&Options", menu)
- Qt::connect(menu, QSIGNAL("activated(int)"), self, 'slotMenuActivated')
+ # connect(menu, Qt::SIGNAL("activated(int)"), self, 'slotMenuActivated')

return mbar
end
Only in ruboids-0.0.1/ruboids: qtruby.bundle
Only in ruboids-0.0.1/ruboids: qui.bundle
diff -ur ruboids-0.0.1.org/ruboids/ruboids.rb ruboids-0.0.1/ruboids/ruboids.rb
--- ruboids-0.0.1.org/ruboids/ruboids.rb Wed May 30 17:39:43 2001
+++ ruboids-0.0.1/ruboids/ruboids.rb Tue Sep 28 17:42:44 2004
@@ -7,20 +7,21 @@
#

require 'qt'
-require 'qgl'
+#require 'qgl'
require 'World'
require 'WorldWindow'
-require 'Canvas'
+require 'MyCanvas'
require 'Params'

-app = QApplication.new([$0] + ARGV)
-if (!QGLFormat::hasOpenGL())
+app = Qt::Application.new([$0] + ARGV)
+if (!Qt::GLFormat::hasOpenGL())
warning("This system has no OpenGL support. Exiting.")
exit -1
end

Params.readParamsFromFile(ARGV[0] || 'boids.properties')
-world = World.instance() # Force creation
+#world = World.instance() # Force creation
+world = World.instance

win = WorldWindow.new()
app.setMainWidget(win)
 
R

Richard Dale

Michal said:
I get the expected behavior if I test with a simple class in irb but I
get non-singleton if I subclass Qt::Object.
....


Is it impossble to base singletons on QObject?
Yes, it looks as though that doesn't work. I changed it to a global variable
$world instead:

Params.readParamsFromFile(ARGV[0] || 'boids.properties')
#world = World.instance
$world = World.new
win = WorldWindow.new()
app.setMainWidget(win)

$world.canvas = win.canvas
win.show()
$world.start()
app.exec()

But I had to make quite a few changes as well because the game is written in
Ruby/Qt as opposed to QtRuby. So now I've got the camera dialog working,
and the main window has a 'Quit' and a 'Camera' option. It doesn't seem to
have any way to start the game though. I've emailed the current version to
you.

-- Richard
 
R

Richard Dale

Richard said:
QtRuby uses method_missing to trap method calls and redirect them to the
Smoke library. So I wonder if Singleton interferes with method_missing in
some way.

I've just restored the Singleton stuff, and it works fine. So I think the
Singleton error was actually a problem with the code only being half ported
from Ruby/Qt to QtRuby.
 
M

Michal 'hramrach' Suchanek

I've just restored the Singleton stuff, and it works fine. So I think the
Singleton error was actually a problem with the code only being half ported
from Ruby/Qt to QtRuby.
They said qtruby on the ruboids website unless I misread it. But the
specification of requirements was very vague.

I must have done something wrong when compiling the extension.
I first compiled everything with autoconf and it did not work because I
was missing -lstdc++, so I added that.
And the result was the broken singleton.

Reading about the compilation once again I decided to try the extconf.rb
stuff but now Qt.rb gets installed as Qt/Qt so I have to modify the
included examples to work (I tried hello only).
But now the singleton in ruboids works. I get the proper error message I
do not have GLX (which I currently do not) and nothing else.

I am sorry I caused so much trouble by miscompiling the extension.
But it would be much clearer what to do if the stuff expanded into two
directories, one handled by automake and the other by extconf.

I will look at the other stuff you sent me tomorrow, it is quite late
here already.

Thanks for your help

Michal Suchanek
 
R

Richard Dale

Michal said:
They said qtruby on the ruboids website unless I misread it. But the
specification of requirements was very vague.

I must have done something wrong when compiling the extension.
I first compiled everything with autoconf and it did not work because I
was missing -lstdc++, so I added that.
And the result was the broken singleton.

Reading about the compilation once again I decided to try the extconf.rb
stuff but now Qt.rb gets installed as Qt/Qt so I have to modify the
included examples to work (I tried hello only).
I think that is another problem with the Mac OS X build - Qt.rb should be
installed at the top level along with qtruby.bundle, and qtruby.rb
installed in Qt/qtruby.rb.
But now the singleton in ruboids works. I get the proper error message I
do not have GLX (which I currently do not) and nothing else.

I am sorry I caused so much trouble by miscompiling the extension.
But it would be much clearer what to do if the stuff expanded into two
directories, one handled by automake and the other by extconf.

I will look at the other stuff you sent me tomorrow, it is quite late
here already.
No problem. I've added it to the QtRuby code examples now in
qtruby/rubylib/examples/ruboids. It's a really good OpenGL/Qt::GL* widgets
example, and there wasn't one before. I've also added you exconf.rb change
and corrected the README.build file about Mac OS X.
 
M

Michal 'hramrach' Suchanek

I think that is another problem with the Mac OS X build - Qt.rb should be
installed at the top level along with qtruby.bundle, and qtruby.rb
installed in Qt/qtruby.rb.
hehe, I tried running the ruboids I have on the correct display and the
singleton is still broken. So there is not much difference wrt the build
system used to compile the extension.
No problem. I've added it to the QtRuby code examples now in
qtruby/rubylib/examples/ruboids. It's a really good OpenGL/Qt::GL* widgets
example, and there wasn't one before. I've also added you exconf.rb change
and corrected the README.build file about Mac OS X.
--- /Users/hramrach/qtruby-cvs/configure.in.in Mon Sep 27 16:45:43 2004
+++ qtruby-cvs/configure.in.in Sat Jun 19 03:47:45 2004
@@ -28,4 +28,5 @@
AC_PATH_QT()
AC_SUBST(all_includes)
AC_SUBST(all_libraries)
+AC_SUBST(CONF_FILES)

Thanks

Michal Suchanek
 
R

Richard Dale

Michal said:
hehe, I tried running the ruboids I have on the correct display and the
singleton is still broken. So there is not much difference wrt the build
system used to compile the extension.
irb(main):001:0> require 'Qt'
=> true
irb(main):002:0> require 'singleton'
=> true
irb(main):003:0> class Single < Qt::Object
irb(main):004:1> include Singleton
irb(main):005:1> def initialize
irb(main):006:2> super
irb(main):007:2> puts "in initialize"
irb(main):008:2> end
irb(main):009:1> end
=> nil
irb(main):010:0> Single.instance
in initialize
=> #<Single:0x3031d188>
irb(main):011:0> Single.instance
=> #<Single:0x3031d188>
irb(main):012:0>

You weren't calling super in your original irb log. It looks like you didn't
need to do that in Ruby/Qt, but you do in QtRuby.

-- Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top