pixelflow_canvas 0.4.3 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PixelflowCanvas
4
- VERSION = "0.4.3"
4
+ VERSION = "0.6.0"
5
5
  end
@@ -3,6 +3,7 @@
3
3
  require_relative "pixelflow_canvas/version"
4
4
  require_relative "pixelflow_canvas/vga_palette.rb"
5
5
  require 'chunky_png'
6
+ require 'json'
6
7
  require 'socket'
7
8
  require 'time'
8
9
 
@@ -484,6 +485,112 @@ module Pixelflow
484
485
  draw_line(x1, y1, x2, y2)
485
486
  draw_line(x2, y2, x0, y0)
486
487
  end
488
+
489
+ # FONT RENDERING
490
+
491
+ def self.load_font(font)
492
+ @@cypher_fonts ||= {}
493
+ return if @@cypher_fonts[font]
494
+ # STDERR.puts "Loading font from #{path}..."
495
+ font_data = {}
496
+ File.open(File.join(File.dirname(__FILE__), 'pixelflow_canvas', 'fonts', "#{font}.bdf")) do |f|
497
+ char = nil
498
+ f.each_line do |line|
499
+ if line[0, 9] == 'STARTCHAR'
500
+ char = {}
501
+ elsif line[0, 8] == 'ENCODING'
502
+ char[:encoding] = line.sub('ENCODING ', '').strip.to_i
503
+ elsif line[0, 7] == 'ENDCHAR'
504
+ font_data[char[:encoding]] = char
505
+ char = nil
506
+ elsif line[0, 3] == 'BBX'
507
+ parts = line.split(' ')
508
+ char[:width] = parts[1].to_i
509
+ char[:height] = parts[2].to_i
510
+ elsif line[0, 6] == 'BITMAP'
511
+ char[:bitmap] = []
512
+ else
513
+ if char && char[:bitmap]
514
+ char[:bitmap] << line.to_i(16)
515
+ end
516
+ end
517
+ end
518
+ end
519
+ @@cypher_fonts[font] = font_data
520
+ end
521
+
522
+ def draw_text(x, y, s, font, scale = 1)
523
+ Canvas.load_font(font)
524
+ dx = 0
525
+ s.each_char do |c|
526
+ glyph = @@cypher_fonts[font][c.ord]
527
+ if glyph
528
+ w = ((((glyph[:width] - 1) >> 3) + 1) << 3) - 1
529
+ (0...glyph[:height]).each do |iy|
530
+ (0...glyph[:width]).each do |ix|
531
+ if (((glyph[:bitmap][iy] >> (w - ix)) & 1) == 1)
532
+ (0...scale).each do |oy|
533
+ (0...scale).each do |ox|
534
+ set_pixel(x + (ix + dx) * scale + ox, y + iy * scale + oy)
535
+ end
536
+ end
537
+ end
538
+ end
539
+ end
540
+ dx += glyph[:width]
541
+ end
542
+ end
543
+ end
544
+
545
+ def self.text_width(s, font, options = {})
546
+ Canvas.load_font(font)
547
+ width = 0
548
+ s.each_char do |c|
549
+ glyph = @@cypher_fonts[font][c.ord]
550
+ if glyph
551
+ width += glyph[:width]
552
+ end
553
+ end
554
+ width
555
+ end
556
+
557
+ # EVENT POLLING
558
+
559
+ class Event
560
+ def initialize(data)
561
+ @data = data
562
+ end
563
+
564
+ attr_reader :data
565
+
566
+ def on(type, &block)
567
+ if type == :mouse_move && @data[0] == 3
568
+ yield(@data[1], @data[2])
569
+ end
570
+ end
571
+ end
572
+
573
+ def fetch_events(&block)
574
+ @socket.write([9].pack('C'))
575
+ @socket.flush
576
+ json = ''
577
+ loop do
578
+ begin
579
+ buffer = @socket.recv_nonblock(1024)
580
+ json += buffer
581
+ rescue IO::WaitReadable
582
+ break
583
+ end
584
+ end
585
+ return if json.empty?
586
+ json.split("\r\n").each do |line|
587
+ line.strip!
588
+ next if line.empty?
589
+ JSON.parse(line).each do |event|
590
+ yield Event.new(event)
591
+ end
592
+ end
593
+ end
487
594
  end
488
595
 
489
596
  class Turtle
data/test-sirds.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require './lib/pixelflow_canvas.rb'
2
+ require 'json'
2
3
 
3
4
  def depth(x, y)
4
5
  r = (x - 128) ** 2 + (y - 128) ** 2
@@ -34,4 +35,12 @@ end
34
35
  # end
35
36
  # end
36
37
 
37
- canvas.save_as_png('sirds.png')
38
+ i = 0
39
+ loop do
40
+ canvas.fetch_events do |event|
41
+ STDERR.puts event.to_json
42
+ end
43
+ canvas.ensure_max_fps(30)
44
+ i += 1
45
+ # STDERR.puts "\r #{i}"
46
+ end
@@ -4,7 +4,6 @@ width = 256
4
4
  height = 256
5
5
 
6
6
  canvas = Pixelflow::Canvas.new(width, height, :palette)
7
- canvas.set_interpolation_mode(:bilinear)
8
7
  turtle = Pixelflow::Turtle.new(canvas)
9
8
  turtle.set_color(13)
10
9
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pixelflow_canvas
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Specht
@@ -36,12 +36,20 @@ files:
36
36
  - README.md
37
37
  - Rakefile
38
38
  - lib/pixelflow_canvas.rb
39
+ - lib/pixelflow_canvas/fonts/4x6.bdf
40
+ - lib/pixelflow_canvas/fonts/6x10.bdf
41
+ - lib/pixelflow_canvas/fonts/7x13.bdf
42
+ - lib/pixelflow_canvas/fonts/7x13B.bdf
43
+ - lib/pixelflow_canvas/fonts/8x13.bdf
44
+ - lib/pixelflow_canvas/fonts/8x13B.bdf
45
+ - lib/pixelflow_canvas/fonts/9x15.bdf
46
+ - lib/pixelflow_canvas/fonts/9x15B.bdf
39
47
  - lib/pixelflow_canvas/version.rb
40
48
  - lib/pixelflow_canvas/vga_palette.rb
41
49
  - sig/pixelflow_canvas.rbs
42
50
  - test-anaglyph.rb
43
- - test-draw.rb
44
51
  - test-sirds.rb
52
+ - test-triangle.rb
45
53
  - test-turtle.rb
46
54
  - test.rb
47
55
  homepage: https://github.com/specht/pixelflow_canvas_ruby