Request regarding Color class

2 views
Skip to first unread message

Hima

unread,
Jan 1, 2009, 10:56:52 AM1/1/09
to Star Ruby (English)
Hello it's me again. Now that I've solved the last problem, I'm trying
to rewrite my puzzle game that I wrote with gosu to use StarRuby
instead :D

However, I've found one problem though. I wish we should change the
attribute of the color class. This would make playing text effect
become much easier. Like, a fade in text you can just write
@color.alpha = [@color.alpha + 1, 255].min instead of having to
create a new instance from Color class every update.

Is this already possible or is there a way to work around this? Thank
you in advance :)

Hajime Hoshi

unread,
Jan 1, 2009, 1:03:30 PM1/1/09
to starr...@googlegroups.com
Hi, Hima

A Happy New Year!

> Hello it's me again. Now that I've solved the last problem, I'm trying
> to rewrite my puzzle game that I wrote with gosu to use StarRuby
> instead :D

That's good!

> However, I've found one problem though. I wish we should change the
> attribute of the color class. This would make playing text effect
> become much easier. Like, a fade in text you can just write
> @color.alpha = [@color.alpha + 1, 255].min instead of having to
> create a new instance from Color class every update.
>
> Is this already possible or is there a way to work around this? Thank
> you in advance :)

Since a color object is immutable (not changeable), you can not do so.

Moreover, it's very difficult to reimplement Color objects to be changeable.
We have developed Star Ruby library on the basis that the color class is
immutable.
Whether a object is immutable or not is a tread-off between briefness
and performance.
Why we decided Color should be immutable is that we think we do not have to care
about allocation a new color object very much.

Do you have a performance probrem about new color object-allocation?

--
Hajime Hoshi <hajim...@gmail.com>

Hima

unread,
Jan 2, 2009, 1:35:07 AM1/2/09
to Star Ruby (English)
明けましておめでとうございます、星さん :D

Yeah the perfomance hit was pretty bad X( It lower my fps down to 50
- 52.

Is StarRuby suitable for a game with a resolution of 640 x 480? I also
find drawing sprite to be pretty slow, compare to Gosu.

Here is the source code. Somehow the number I drew through render_text
is painted over by the sprite even though I render the text after the
sprite @_@;

http://hima.gptouch.com/stuff/DotPlusSrc.rar

Thank you for your help!

Hajime Hoshi

unread,
Jan 2, 2009, 5:56:17 AM1/2/09
to starr...@googlegroups.com

As you suggest, Star Ruby's rendering performance is not good.
Since most part of a execution is rendering, I can often find some improvements
in drawing processes.

In your souce code:

* Texture#clear is faster than Texture#render_rect(0, 0, 640, 480,
Color.new(0, 0, 0, 255)).
(To be exact, Texture#clear is as same as
Texture#fill_rect(0, 0, 640, 480, Color.new(0,0,0,0)).)
For example, I found it in draw method of SceneKurickZum class.
* In 'draw' method of NumberBlock class, some render_text methods are used.
If you use a texture object as a buffer where the texts are rendered,
you need only one render_texture method with this buffer.
Above all, Texture#render_text method is one of the heaviest methods.
It is recommended to use a texture object as a buffer or a cache.

--
Hajime Hoshi <hajim...@gmail.com>

Hima

unread,
Jan 2, 2009, 12:07:54 PM1/2/09
to Star Ruby (English)
> * Texture#clear is faster than  Texture#render_rect(0, 0, 640, 480,
> Color.new(0, 0, 0, 255)).
>   (To be exact, Texture#clear is as same as
>     Texture#fill_rect(0, 0, 640, 480, Color.new(0,0,0,0)).)

I tried it, but then the problem of wrong color mixing occur :( You
should try it and see what happens.
Also, is render_rect and fill_rect different?

>   For example, I found it in draw method of SceneKurickZum class.
> * In 'draw' method of NumberBlock class, some render_text methods are used.
>   If you use a texture object as a buffer where the texts are rendered,
>   you need only one render_texture method with this buffer.
>   Above all, Texture#render_text method is one of the heaviest methods.
>   It is recommended to use a texture object as a buffer or a cache.
>
I don't quite understand what you mean. Could you please show me or
give me an example of how to do this? Thank you in advance :)

Daigo

unread,
Jan 2, 2009, 3:47:35 PM1/2/09
to Star Ruby (English)
hi Hima,

> I don't quite understand what you mean.

I think what he meant is that you need to pre-render the text if you
want to use the text for each frame.
Since render_text is a heavy method, it is more efficient to cache the
render result.

For example you can:
-------------------
def get_text_texture(text)
text = "Render Text"
font = Font.new("verdana")
size = Font.get_size(text)
tex = Texture.new(size[0], size[1])
tex.render_text(text, 0, 0)
return tex
end

# game main loop
Game.do |game|
# Get texture a text rendered on it first time.
unless @text_texture
@text_texture = get_text_texture("Render Text")
end
game.screen.clear
# render cached texture to render each frame
game.screen.render_texture(text_texture)
end
-------------------
I didn't debug the code, but I guess you can get the idea from this.
So, the idea is to cache the text render result, so that your computer
doesn't have to spend extra computational power to call render_text
method each frame. Since it is a little bit tedious, probably Mr.Hoshi
can think about adding a method like "get_text_texture" as a starruby
built-in method.
Reply all
Reply to author
Forward
0 new messages