Ruby Gosu渲染技术的优化

Ruby Gosu渲染技术的优化,ruby,game-development,libgosu,Ruby,Game Development,Libgosu,我第一次用ruby gem作为图形库编写了一个游戏。我想我大体上理解更新游戏状态的逻辑和更新/渲染的逻辑应该分开,并且我已经尽了最大努力从渲染循环中移除尽可能多的工作负载 我的游戏由32x32像素的瓷砖组成,屏幕宽度为1920x1080,这意味着我一次在屏幕上渲染~1980个瓷砖(有时更多,取决于特定瓷砖上是否有多个精灵) 我的问题是,尽管我觉得我已经从程序的draw方法中剥离了几乎所有的逻辑,但我似乎仍然平均每秒22帧。如果有人能看一下下面的代码片段,并提出性能缓慢的可能原因/优化,那将非常有

我第一次用ruby gem作为图形库编写了一个游戏。我想我大体上理解更新游戏状态的逻辑和更新/渲染的逻辑应该分开,并且我已经尽了最大努力从渲染循环中移除尽可能多的工作负载

我的游戏由32x32像素的瓷砖组成,屏幕宽度为1920x1080,这意味着我一次在屏幕上渲染~1980个瓷砖(有时更多,取决于特定瓷砖上是否有多个精灵)

我的问题是,尽管我觉得我已经从程序的
draw
方法中剥离了几乎所有的逻辑,但我似乎仍然平均每秒22帧。如果有人能看一下下面的代码片段,并提出性能缓慢的可能原因/优化,那将非常有价值艾德

# Main file (seven_deadly_sins.rb)
require_relative 'initializer'

class SevenDeadlySins < Gosu::Window
  WIDTH, HEIGHT = 1920, 1080

  def initialize
    super WIDTH, HEIGHT
    self.caption = "Seven Deadly Sins"

    @player = Player.new(0,0,10,40,40)
    @game_tiles = Gosu::Image.load_tiles('./assets/tiles/map_tiles.png', 16, 16, {retro: true}) # Retro means no weird border around smaller tiles
    @level_mapper = LevelMapper.new(self, 'test.json', @game_tiles)
  end

  def update
    @player.update
    @level_mapper.update
  end

  def draw
    @player.draw
    @level_mapper.draw
  end
end

SevenDeadlySins.new.show

我最终弄明白了这一点,我想我应该在这里发布我的答案,以防其他人遇到同样的问题

Gosu有一个名为
record
的函数,它可以保存您的绘图操作,并将其作为可绘图图像返回给您(请参阅)。这对于使用tilemaps非常有用(这正是我在本文中所做的).我最终在我的
初始化器
方法中预先记录了绘制操作,然后绘制了记录,如下所示:

class LevelMapper

  def initializer
    ...
    # Pre-record map so that we can speed up rendering.
    create_static_recording
  end

  def create_static_recording
    @map_rec = @window.record(@window.width, @window.height) do |x, y|
      # Replace the following lines with whatever your draw function would do
      @tiles_within_viewport.each do |tiles|
        tiles.each do |tile|
          Gosu.draw_rect(tile['x'], tile['y'], TILE_SIZE, TILE_SIZE, 0xff292634, 1)
          if tile['sprite_index']
            @sprites[tile['sprite_index']].draw(tile['x'], tile['y'], tile['z'], TILE_SIZE / 16, TILE_SIZE / 16)
          end
        end
      end
    end
  end

  def draw
    @map_rec.draw(0, 0, 0)
  end

end
# player.rb
class Player < Humanoid
  def initialize(*opts)
    super(*opts)
  end
end
# humanoid.rb
class Humanoid
  attr_reader :bounding_box

  def initialize(x, y, z, w, h, move_speed = 5)
    @bounding_box = BoundingBox.new(x, y, z, w, h)
    @move_speed = move_speed
  end

  def draw
    if (needs_render?)
      Gosu.draw_rect(bounding_box.x, bounding_box.y, bounding_box.w, bounding_box.h, Gosu::Color::RED, bounding_box.z)
    end
  end

  def update
    if Gosu.button_down? Gosu::KB_LEFT or Gosu::button_down? Gosu::GP_LEFT
      move :left
    end
    if Gosu.button_down? Gosu::KB_RIGHT or Gosu::button_down? Gosu::GP_RIGHT
      move :right
    end
    if Gosu.button_down? Gosu::KB_UP or Gosu::button_down? Gosu::GP_BUTTON_0
      move :up
    end
    if Gosu.button_down? Gosu::KB_DOWN or Gosu::button_down? Gosu::GP_BUTTON_1
      move :down
    end
  end

  def needs_render?
    true
  end

  def move(direction)
    if direction == :left
      @bounding_box.x -= @move_speed
    elsif direction == :right
      @bounding_box.x += @move_speed
    elsif direction == :up
      @bounding_box.y -= @move_speed
    else
      @bounding_box.y += @move_speed
    end
  end
end
class LevelMapper

  def initializer
    ...
    # Pre-record map so that we can speed up rendering.
    create_static_recording
  end

  def create_static_recording
    @map_rec = @window.record(@window.width, @window.height) do |x, y|
      # Replace the following lines with whatever your draw function would do
      @tiles_within_viewport.each do |tiles|
        tiles.each do |tile|
          Gosu.draw_rect(tile['x'], tile['y'], TILE_SIZE, TILE_SIZE, 0xff292634, 1)
          if tile['sprite_index']
            @sprites[tile['sprite_index']].draw(tile['x'], tile['y'], tile['z'], TILE_SIZE / 16, TILE_SIZE / 16)
          end
        end
      end
    end
  end

  def draw
    @map_rec.draw(0, 0, 0)
  end

end