class Worldgen::HeightMap

A square heightmap

Attributes

size[R]

Public Class Methods

new(size) click to toggle source

Create a new square heightmap. Arguments:

  • size - the width/height of the map

# File lib/worldgen/heightmap.rb, line 13
def initialize size
  @size = size
  initialize_native(size)
end

Public Instance Methods

[]=(p1, p2) click to toggle source

Get the value of our heightmap at a specified point.

VALUE get_at(VALUE self, VALUE vx, VALUE vy) {
  int x = FIX2INT(vx);
  int y = FIX2INT(vy);
  heightmap map = get_heights(self);
  int size = map.size;

  return ARR(map.heights, x, y);
}
each_height() click to toggle source

Iterate over all the points in the heightmap. Example: “` heightmap.each_height do |x, y, height|

puts "Height at (#{x}, #{y}) is #{height}"

end

VALUE each_height(VALUE self) {
  heightmap map = get_heights(self);
  heightmap_points ptr = map.heights;
  VALUE args = rb_ary_new2(3);
  int size = get_size(self);
  int x, y;

  for (x = 0; x < size; x++) {
    rb_ary_store(args, 0, INT2FIX(x));
    for (y = 0; y < size; y++) {
      rb_ary_store(args, 1, INT2FIX(y));
      rb_ary_store(args, 2, DBL2NUM(*ptr++));
      rb_yield(args);
    }
  }
  return self;
}
num_points() click to toggle source

Get the number of points within the heightmap. Right now this is a very simple calculation of size * size.

VALUE num_points_wrapped(VALUE self) {
  return INT2FIX(num_points(FIX2INT(rb_iv_get(self, "@size"))));
}

Private Instance Methods

initialize_native(p1) click to toggle source

Initialize the C side of things for a heightmap

VALUE initialize_native(VALUE self, VALUE vsize) {
  int size = FIX2INT(vsize);
  int memsize = num_points(size) * sizeof(double);

  heightmap_points map = (heightmap_points)malloc(memsize);
  memset(map, 0, memsize);
  set_heights(self, map);

  return Qnil;
}