Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Mario Visic
    @mariovisic
    ah; :sweat_smile: you mentioned the issue about exact multiples above
    Sourav Goswami
    @Souravgoswami
    Hello! How's the development going? Are we going to see version 1.0 soon?
    Sourav Goswami
    @Souravgoswami
    Simple one, can be improved a lot!
    #!/usr/bin/env ruby
    # Frozen_String_Literal: true
    require 'ruby2d'
    
    W, H = 640, 480
    set width: W, height: H, fps_cap: 100
    
    pixel_size = 6
    y_offset = 200
    x_offset = 40
    
    a = <<~'EOF'
    .   . ...... .       .      ......    ......  .    . ......  .     . ...... ......  .     .   .
    .   . .      .       .      .    .    .     . .    . .     .  .   .       . .     . .   .   .   .
    ..... ....   .       .      .    .    ......  .    . ......    . .   ...... .     . .   .       .
    .   . .      .       .      .    .    .     . .    . .     .    .    .      .     .       .   .
    .   . ...... ......  ...... ......    .     . ...... ......     .    ...... ......  .       .
    EOF
    m, squares = a.lines.map(&:length).max, []
    
    a.each_line.with_index do |y, yi|
        y.each_char.with_index do |x, xi|
            squares << Square.new(size: pixel_size, x: rand(W), y: rand(H), color: [1 - xi.to_f / (m * 2), 1 - xi.to_f / m, xi.to_f / m, 1]) if x == ?.
        end
    end
    
    no_of_stars = 500.0
    stars = no_of_stars.to_i.times.map { Square.new(size: 1, x: rand(W), y: rand(H), color: [rand, rand, rand, 1]) }
    speeds = stars.map.with_index { |_, i| i / (no_of_stars / 2) }
    t = Time.now
    
    update do
        i = -1
        stars.each do |x|
            x.y -= speeds[i += 1]
            x.y = H if x.y < 0 - x.size
        end
    
        i = -1
        if a.lines.map.with_index { |y, yi|
            y.chars.map.with_index { |x, xi|
                if x == ?.
                    s = squares[i += 1]
                    s.x == x_offset + xi * pixel_size && s.y == y_offset + yi * pixel_size
                end
            }.compact
        }.flatten.all?(&:itself)
            tt = Time.now.-(t)
    
            if tt > 4
                squares.each { |x| x.x, x.y = rand(W), rand(H) }
            elsif tt > 3
                squares.each { |x| x.opacity = rand }
            elsif tt > 1
                b = rand
                squares.each { |x| x.opacity = b }
            end
        else
            i = -1
            t = Time.now
            a.each_line.with_index do |y, yi|
                y.each_char.with_index do |x, xi|
                    if x == ?.
                        s = squares[i += 1]
    
                        if s.x > x_offset + xi * pixel_size
                            s.x -= 1
                        elsif s.x < x_offset + xi * pixel_size
                            s.x += 1
                        end
    
                        if s.y > y_offset + yi * pixel_size
                            s.y -= 1
                        elsif s.y < y_offset + yi * pixel_size
                            s.y += 1
                        end
    
                        s.color = [1 - xi.to_f / (m * 2), 1 - xi.to_f / m, xi.to_f / m, 1]
                    end
                end
            end
        end
    end
    
    show
    image.png
    Sourav Goswami
    @Souravgoswami
    Here's another one!
    #!/usr/bin/ruby -w
    # Frozen_String_Literal: true
    require 'ruby2d'
    
    W, H = 640, 480
    set width: W, height: H, fps_cap: 120
    
    pixel_size = 6
    y_offset = 200
    x_offset = 40
    no_of_stars = 500
    
    a = <<~'EOF'
    .   . ...... .       .      ......    ......  .    . ......  .     . ...... ......  .     .   .
    .   . .      .       .      .    .    .     . .    . .     .  .   .       . .     . .   .   .   .
    ..... ....   .       .      .    .    ......  .    . ......    . .   ...... .     . .   .       .
    .   . .      .       .      .    .    .     . .    . .     .    .    .      .     .       .   .
    .   . ...... ......  ...... ......    .     . ...... ......     .    ...... ......  .       .
    EOF
    
    m, squares = a.lines.map(&:length).max, []
    
    Ruby2D::Square.attr_accessor :state
    
    a.each_line.with_index do |y, yi|
        y.each_char.with_index do |x, xi|
            squares << Square.new(size: pixel_size, x: rand(W), y: rand(H), color: [1 - xi.to_f / (m * 2), 1 - xi.to_f / m, xi.to_f / m, 1]) if x == ?.
        end
    end
    
    stars = no_of_stars.times.map { Square.new(size: 1, x: rand(W), y: rand(H), color: [rand, rand, rand, 1]) }
    speeds = stars.map.with_index { |_, i| i / (no_of_stars / 2.0) }
    
    t, flag = Time.now, false
    
    update do
        i = -1
        stars.each { |x| x.y = x.y > 0 - x.size ? x.y - speeds[i += 1] : H }
    
        i = -1
        if a.lines.map.with_index do |y, yi|
            y.chars.map.with_index { |x, xi| squares[i += 1].then { |s| s.x == x_offset + xi * pixel_size && s.y == y_offset + yi * pixel_size } if x == ?. }.compact
        end.flatten.all?(&:itself)
    
            tt = Time.now.-(t)
    
            if tt > 4
                squares.each { |x| x.x, x.y, x.opacity, x.state = x.x + 10, x.y + 10, 1, false }
            elsif tt > 3
                flag ||= true
                squares.each { |x| x.opacity = rand }
            elsif tt > 1
                r = rand
                squares.each { |x| x.opacity = r }
            end
        elsif !flag
            i = -1
            t = Time.now
    
            a.each_line.with_index do |y, yi|
                y.each_char.with_index do |x, xi|
                    if x == ?.
                        s = squares[i += 1]
    
                        if s.x > x_offset + xi * pixel_size
                            s.x -= 1
                        elsif s.x < x_offset + xi * pixel_size
                            s.x += 1
                        end
    
                        if s.y > y_offset + yi * pixel_size
                            s.y -= 1
                        elsif s.y < y_offset + yi * pixel_size
                            s.y += 1
                        end
    
                        s.color = [1 - xi.to_f / (m * 2), 1 - xi.to_f / m, xi.to_f / m, s.opacity]
                        s.opacity += 0.01 if s.opacity < 1
                    end
                end
            end
        else
            squares.each_with_index do |x, i|
                x.x, x.y, x.opacity = x.x + Math.tan(i) * Math.cos(i), x.y + Math.tan(i) * Math.sin(i), x.opacity - 0.005
    
                if x.opacity < 0
                    x.x = x.x.to_i.then { |z| (z < 0 || z > W) ? rand(W) : z }
                    x.y = x.y.to_i.then { |z| (z < 0 || z > H) ? rand(H) : z }
                    x.opacity = 0
                    x.state ||= true
                end
            end
    
            flag = false if squares.all?(&:state)
        end
    end
    
    Window.show
    Exa Stencil
    @exastencil
    I'm just checking, but Ruby2D doesn't support bitfonts, does it?
    Bitmap fonts, even. :D
    Because at low res TrueType fonts are garbo: https://www.dropbox.com/s/sot63ddr546yeee/lowrezfont.png?dl=0
    Sourav Goswami
    @Souravgoswami
    LOL I personally never had such issues with fonts... Which font are you using?
    Sourav Goswami
    @Souravgoswami

    Sorry I remember having issues with fonts XD

    Ruby2D was unable to render characters with acute, circumflex, tilde, umlaut, grave, etc. And for my language, Bengali, it won't show the characters. Same goes for emoji. Basically Ruby2D can only handle ASCII characters! On of my solution is to draw characters! :(

    a = <<~'EOF'
    .   . ...... .       .      ......    ......  .    . ......  .     . ...... ......  .     .   .
    .   . .      .       .      .    .    .     . .    . .     .  .   .       . .     . .   .   .   .
    ..... ....   .       .      .    .    ......  .    . ......    . .   ...... .     . .   .       .
    .   . .      .       .      .    .    .     . .    . .     .    .    .      .     .       .   .
    .   . ...... ......  ...... ......    .     . ...... ......     .    ...... ......  .       .
    EOF

    Just like the above example, but if you make pixel size to 1, you can draw pretty much anything you want...

    That's a slow and boring solution, to write stuff from scratch, and 1 px objects will use a lot of processing power, and we can't truely use multithread, and forks can't be used, so the solution is unknown so far for a lots of text and faster development time!
    Exa Stencil
    @exastencil
    That was Arial, font size 12pt. I'll see if I can implement my own fonts as a renderable if needed.
    Sourav Goswami
    @Souravgoswami
    Ah! Good luck!
    Sourav Goswami
    @Souravgoswami
    1 reply
    Exa Stencil
    @exastencil
    I managed to get bitmap fonts rendering by generating a spritesheet and associated metadata from BDF files. Then I added behavior for creating and managing the sprites inside my custom renderer. Not sure how performant that will be, but it works at least.
    Exa Stencil
    @exastencil
    image.png
    The colors randomize on the second to last one every 5 frames, and the bottom line reveals one character every 5 frames.
    create_entity :label, text: 'A bitmap font with color', x: 20, y: 200, color: 'lime', font: :terminus
        create_entity :label, text: 'A bitmap font with opacity', x: 20, y: 220, color: 'yellow', opacity: 0.6, font: :terminus
    
        whacky_text = create_entity(
          :label,
          text: "Let's  go  a  little  crazy!",
          x: 20, y: 250,
          color: 'random',
          font: :terminus
        )
    
        text_reveal = create_entity(
          :label,
          text: "I bet you can't wait to hear what I have to say...",
          x: 20, y: 270,
          color: 'orange',
          opacity: 0.0,
          font: :terminus
        )
    
        each_frame do
          next unless Window.frames % 5 == 0
    
          whacky_text.text_object.each do |sprite|
            sprite.color = 'random'
          end
    
          next if Window.frames > text_reveal.text.size * 5
    
          text_reveal.text_object[0..(Window.frames / 5)].each { |sprite| sprite.opacity = 1.0 }
        end
    Mario Visic
    @mariovisic
    neat, where are the create_entity and each_frame methods coming from, do you have your own ruby2d wrapper for convinence?
    Exa Stencil
    @exastencil
    Yeah, it's something I'm working on: https://github.com/exastencil/pixelometry
    Grzegorz Jakubiak
    @grzegorz-jakubiak
    Hey @blacktm! First of all I'd like to thank the creator for this awesome gem! I've got a question if there is any specific reason why there is no way to create multiple or rather separate windows? I mean there's always one window assigned to the class variable. I was trying to wrap window into Ractor and send things to update loop from other Ractor but it's impossible because of the class variable.
    Mario Visic
    @mariovisic
    Not @blacktm ... so I can't tell you why the window is a singleton. If you want to send data to the window via another process or thread you can use the queue and message constructs above
    would that help get around the fact that the window is a singleton @grzegorz-jakubiak ?
    Tom Black
    @blacktm
    Hi @grzegorz-jakubiak. Thanks! Truthfully, it was just easier dealing with one window than many. I kinda thought SDL (which is used internally) had multi-window issues, but maybe not. Could try to support it, would take some re-architecting probably.
    Grzegorz Jakubiak
    @grzegorz-jakubiak

    @mariovisic I basically used the same idea but even without a Thread.

    animating = nil
    
    window.update do
      if animating.nil?
        unless scene.animation_queue.empty?
          animating = scene.animation_queue.pop
          animating.call
        end
      elsif animating.finished == false
        animating.call
      else
        animating = nil
      end
    end

    Scene instance holds this queue. I reckon it could be possible to use a Ractor managed queue the same way.
    @blacktm I see I can try and help :)

    Victor Maslov
    @Nakilon
    you speak about game engines
    this was meant to be a basis for such engines: https://www.youtube.com/watch?v=CkMqUPJU4Bc
    but I was stuck because of ruby2d/ruby2d#182
    now idk when would I return to the project
    Mario Visic
    @mariovisic
    @Nakilon you should still be able to achieve what you want, though you do need to avoid altering ruby2d objects in a seperate thread, you can pass messages to the main thread so it can then alter the text content
    Victor Maslov
    @Nakilon
    image.png
    I wonder why markdown fails to format the link
    SADNB
    @SADNB
    error.JPG
    Hi everyone, I'm tryng to buid an Snake game in ruby with ruby2d. My program gets stucked when I try to show. When I run it in the bash an error appears related with SDL
    If someone could help me i would be really pleased
    Im using Bash of Ubuntu in Windows
    Victor Maslov
    @Nakilon
    @SADNB what about export XDG_RUNTIME_DIR=$(pwd) and then try again?
    Ruby2d can run on Windows without Linux
    SADNB
    @SADNB
    Now, the XDG Runtime errors have gone, but those that are in red have not dissapear
    Reinaldo Romero
    @Rei_Romero_twitter
    image.png
    Hi, I'm trying to run ruby2d but it just opens and closes a window very quickly
    SADNB
    @SADNB
    error.JPG
    Hi guys, I was trying to solve these errors but I haven't solved anything. Anyone knows how to solve it? Thanks
    Tom Black
    @blacktm
    Nice website @Souravgoswami!
    Mario Visic
    @mariovisic
    what instructions did you follow to install SDL @SADNB ?
    maybe try setting the display explicitly with export DISPLAY=:0
    or try the solution listed here:
    Gwitr
    @Gwitr
    If you're using WSL, then you'll need to install a Windows X server and add "DISPLAY=:0" before the command.