Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gosu examples: available keys? #576

Closed
rubyFeedback opened this issue Feb 8, 2021 · 11 comments
Closed

Gosu examples: available keys? #576

rubyFeedback opened this issue Feb 8, 2021 · 11 comments

Comments

@rubyFeedback
Copy link

Hey guys,

I am currently SLOWLY (really slowly) ... learning Gosu.

I know a lot of ruby; also a lot of ruby-gtk.

I am also reading other people's old Gosu code. :D
(I work so much faster with working examples that I can tweak).

Anyway - I am currently studying and modifying an old tetris game
written in gosu from 2012 (almost 10 years old!).

There is a lot of code checks like:

if button_down?(Gosu::KbSpace)
if button_down?(Gosu::KbEscape)

I assume these are keys gosu registered. I am sure I can look
up somewhere too BUT! Being a very lazy person, I was
wondering whether the examples/ directory could simply
have a tiny widget that outputs the name of the keys when
pressed?

That way gosu-users can quickly find out on their own,
without even having to read online documentation. I am
sure I'll find out in a few hours from now on anyway (that
happened to me with ruby-gtk; I found solutions accidentally
heheh), but before I forget it, I wanted to suggest to
add such a small .rb file as an example.

Really simple!

Something that, in pseudo code, says:

"if user hits the escape-key, output the name of the constant on the commandline".

@rubyFeedback
Copy link
Author

Hmm found it already.

Via google though. :D

https://www.rubydoc.info/gems/minigl/2.0.4/MiniGL/KB

Perhaps I will contribute my own implementation here some time later;
right now I am fixing the old example code here:

https://www.gamedev.net/blogs/entry/2254256-tetris-in-ruby/

By the way, perhaps we could have a meta-gem for gosu-games
that is also available on rubygems.org where multiple folks
are authors. As it is, we tend to lose old code like this eventually. :(

People move on a lot, due to reallife and stuff. But for new generation
of ruby users it is VERY helpful to have code that still works!

@mblumtritt
Copy link

I use this

require 'gosu'

module Gosu
  module Button
    NAME_MAP =
      constants
        .each_with_object({}.compare_by_identity) do |name, ret|
          ret[Button.const_get(name)] = name
        end
        .freeze

    def self.name(button_id)
      NAME_MAP[button_id] || button_id.to_s
    end
  end
end

With this you can call Gosu::Button.name(button_id) :)

@jlnr
Copy link
Member

jlnr commented Feb 25, 2021

Iterating through constants is a good approach 👍
By the way, both of you are using "kind of deprecated" names for the buttons. Gosu::Button::KbSpace is now Gosu::KB_SPACE for brevity, and to match the more popular SCREAMING_SNAKE_CASE convention for constants. I don't think the old names will go away anytime soon, though. It's hard to deprecate constants in Ruby without potentially breaking code that uses include Gosu.

@mblumtritt
Copy link

Mmmm - I prefer to have such "magic constants" in a separate module - it works like a namespace and allows something like the code above.

Ruby Module#deprecate_constant is a good helper to pushing developers a little bit ;)

@jlnr
Copy link
Member

jlnr commented Feb 26, 2021

Ooohh, thanks for the pointer! Looks like it has been introduced in Ruby 2.4, which is safe to use in Gosu now.

@jlnr
Copy link
Member

jlnr commented Feb 26, 2021

By the way, you can work around the lack of a namespace in the global Gosu module by grepping for /KB_|MS_|GP_/, but I realize that it isn't quite as pretty.

@jlnr
Copy link
Member

jlnr commented Jan 2, 2022

@mblumtritt Resurrecting this issue because I just got a chance to play around with Module#deprecate_constant. Does it work for you? It has almost no documentation and doesn't emit anything in quick tests. Maybe I'm doing it wrong? (Haven't checked out the C source code yet.)

@cyberarm
Copy link
Collaborator

cyberarm commented Jan 2, 2022

module Gosu
  KB_SPACE = 0
  KbSpace = KB_SPACE
  
  deprecate_constant :KbSpace
end

puts Gosu::KbSpace, Gosu::KB_SPACE

I just ran this and also didn't get any warning. I added the -w flag, ruby -w deprecate_constant_test.rb, and it displayed.

PS C:\Users\cyber\Code> ruby -w .\deprecate_constant_test.rb
./deprecate_constant_test.rb:8: warning: constant Gosu::KbSpace is deprecated
0
0

@jlnr
Copy link
Member

jlnr commented Jan 2, 2022

Oooohh, I forgot about -w 🤦‍♂️ Well, it's almost useless then, but I'll start using it regardless. Our own const deprecation mechanism still has the issue that include Gosu breaks it.

Edit: Also, thanks!

@mblumtritt
Copy link

@jlnr
Copy link
Member

jlnr commented Jan 3, 2022

@mblumtritt Thanks, but unlike in a "normal" Ruby project I'm trying my best to avoid dependencies, mostly because I've been burned by all the sketchy deployment processes that are in use for Ruby/Gosu games.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants