Class: Pixelflow::Canvas

Inherits:
Object
  • Object
show all
Defined in:
lib/pixelflow_canvas.rb

Defined Under Namespace

Classes: Event, MaskGenerator

Constant Summary collapse

COLOR_MODES =
{
    :rgb => 0,
    :palette => 1,
}
ADVANCE_MODES =
{
    :right => 0,
    :down => 1
}
DRAW_MODES =
{
    :direct => 0,
    :buffered => 1
}
COMPOSE_MODES =
{
    :copy => 0,
    :add => 1,
    :subtract => 2,
    :multiply => 3,
}
INTERPOLATION_MODES =
{
    :nearest => 0,
    :bilinear => 1,
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height, color_mode = nil, &block) ⇒ Canvas

Returns a new instance of Canvas.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/pixelflow_canvas.rb', line 35

def initialize(width, height, color_mode = nil, &block)
    @width = 320
    @height = 180
    @x = 0
    @y = 0
    @r = color_mode == :palette ? 15 : 255
    @g = color_mode == :palette ? 0 : 255
    @b = color_mode == :palette ? 0 : 255
    @mask = nil
    @color_mode = :rgb
    @advance_mode = :right
    @draw_mode = :direct
    @compose_mode = :copy
    @palette = VGA_PALETTE.dup
    @socket = TCPSocket.new('127.0.0.1', 19223)


    # Fast transport: all drawing changes the local framebuffer.
    # Complete frames are sent instead of MOVE_TO/SET_PIXEL commands.
    @dirty = false
    @direct_frame_interval = 1.0 / 30.0
    @direct_pixel_counter = 0
    @last_present_timestamp =
        Process.clock_gettime(Process::CLOCK_MONOTONIC)
    set_size(width, height)
    set_color_mode(color_mode) if color_mode
    @last_timestamp = Process.clock_gettime(Process::CLOCK_MONOTONIC)
    at_exit do
        begin
            present if @dirty && @socket && !@socket.closed?
        rescue IOError, SystemCallError
            # VS Code may already have gone away during shutdown.
        end
    end

    if block_given?
        instance_eval(&block)
        present if @dirty
    end
end

Instance Attribute Details

#color_modeObject (readonly)

Returns the value of attribute color_mode.



80
81
82
# File 'lib/pixelflow_canvas.rb', line 80

def color_mode
  @color_mode
end

#heightObject (readonly)

Returns the value of attribute height.



80
81
82
# File 'lib/pixelflow_canvas.rb', line 80

def height
  @height
end

#widthObject (readonly)

Returns the value of attribute width.



80
81
82
# File 'lib/pixelflow_canvas.rb', line 80

def width
  @width
end

Class Method Details

.load_font(font) ⇒ Object

FONT RENDERING



746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
# File 'lib/pixelflow_canvas.rb', line 746

def self.load_font(font)
    @@cypher_fonts ||= {}
    return if @@cypher_fonts[font]
    # STDERR.puts "Loading font from #{path}..."
    font_data = {}
    File.open(File.join(File.dirname(__FILE__), 'pixelflow_canvas', 'fonts', "#{font}.bdf")) do |f|
        char = nil
        f.each_line do |line|
            if line[0, 9] == 'STARTCHAR'
                char = {}
            elsif line[0, 8] == 'ENCODING'
                char[:encoding] = line.sub('ENCODING ', '').strip.to_i
            elsif line[0, 7] == 'ENDCHAR'
                font_data[char[:encoding]] = char
                char = nil
            elsif line[0, 3] == 'BBX'
                parts = line.split(' ')
                char[:width] = parts[1].to_i
                char[:height] = parts[2].to_i
            elsif line[0, 6] == 'BITMAP'
                char[:bitmap] = []
            else
                if char && char[:bitmap]
                    char[:bitmap] << line.to_i(16)
                end
            end
        end
    end
    @@cypher_fonts[font] = font_data
end

Instance Method Details

#draw_arc(x, y, r, a0, a1, close_path = false) ⇒ Object



576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
# File 'lib/pixelflow_canvas.rb', line 576

def draw_arc(x, y, r, a0, a1, close_path = false)
    # x = x.to_i
    # y = y.to_i
    # r = r.to_i
    a0 = a0.to_f
    a1 = a1.to_f
    a0 = a0 * Math::PI / 180.0
    a1 = a1 * Math::PI / 180.0
    da = 1.0 / r
    a = a0
    while a < a1
        x0 = x + r * Math.cos(a)
        y0 = y + r * Math.sin(a)
        a += da
        x1 = x + r * Math.cos(a)
        y1 = y + r * Math.sin(a)
        draw_line(x0, y0, x1, y1)
    end
    if close_path
        x0 = x + r * Math.cos(a0)
        y0 = y + r * Math.sin(a0)
        draw_line(x, y, x0, y0)
        x0 = x + r * Math.cos(a1)
        y0 = y + r * Math.sin(a1)
        draw_line(x, y, x0, y0)
    end
end

#draw_circle(x, y, r) ⇒ Object



429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# File 'lib/pixelflow_canvas.rb', line 429

def draw_circle(x, y, r)
    x = x.to_i
    y = y.to_i
    r = r.to_i
    f = 1 - r
    ddF_x = 1
    ddF_y = -2 * r
    xx = 0
    yy = r
    set_pixel(x, y + r)
    set_pixel(x, y - r)
    set_pixel(x + r, y)
    set_pixel(x - r, y)
    while xx < yy
        if f >= 0
            yy -= 1
            ddF_y += 2
            f += ddF_y
        end
        xx += 1
        ddF_x += 2
        f += ddF_x
        set_pixel(x + xx, y + yy)
        set_pixel(x - xx, y + yy)
        set_pixel(x + xx, y - yy)
        set_pixel(x - xx, y - yy)
        set_pixel(x + yy, y + xx)
        set_pixel(x - yy, y + xx)
        set_pixel(x + yy, y - xx)
        set_pixel(x - yy, y - xx)
    end
end

#draw_cubic_bezier(x0, y0, x1, y1, x2, y2, x3, y3, steps = 100) ⇒ Object



640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
# File 'lib/pixelflow_canvas.rb', line 640

def draw_cubic_bezier(x0, y0, x1, y1, x2, y2, x3, y3, steps = 100)
    steps = steps.to_i
    xp = nil
    yp = nil
    (0..steps).each do |i|
        t = i.to_f / steps
        x = (1 - t) ** 3 * x0 + 3 * (1 - t) ** 2 * t * x1 + 3 * (1 - t) * t ** 2 * x2 + t ** 3 * x3
        y = (1 - t) ** 3 * y0 + 3 * (1 - t) ** 2 * t * y1 + 3 * (1 - t) * t ** 2 * y2 + t ** 3 * y3
        if xp && yp
            draw_line(xp.to_i, yp.to_i, x.to_i, y.to_i)
        end
        xp = x
        yp = y
    end
end

#draw_ellipse(x, y, ra, rb) ⇒ Object



494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
# File 'lib/pixelflow_canvas.rb', line 494

def draw_ellipse(x, y, ra, rb)
    x = x.to_i
    y = y.to_i
    ra = ra.to_i
    rb = rb.to_i
    a2 = ra * ra
    b2 = rb * rb
    fa2 = 4 * a2
    fb2 = 4 * b2
    x0 = 0
    y0 = rb
    sigma = 2 * b2 + a2 * (1 - 2 * rb)
    while b2 * x0 <= a2 * y0
        set_pixel(x + x0, y + y0)
        set_pixel(x - x0, y + y0)
        set_pixel(x + x0, y - y0)
        set_pixel(x - x0, y - y0)
        if sigma >= 0
            sigma += fa2 * (1 - y0)
            y0 -= 1
        end
        sigma += b2 * ((4 * x0) + 6)
        x0 += 1
    end
    x0 = ra
    y0 = 0
    sigma = 2 * a2 + b2 * (1 - 2 * ra)
    while a2 * y0 <= b2 * x0
        set_pixel(x + x0, y + y0)
        set_pixel(x - x0, y + y0)
        set_pixel(x + x0, y - y0)
        set_pixel(x - x0, y - y0)
        if sigma >= 0
            sigma += fb2 * (1 - x0)
            x0 -= 1
        end
        sigma += a2 * ((4 * y0) + 6)
        y0 += 1
    end
end

#draw_line(x0, y0, x1, y1) ⇒ Object



404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'lib/pixelflow_canvas.rb', line 404

def draw_line(x0, y0, x1, y1)
    x0 = x0.to_i
    y0 = y0.to_i
    x1 = x1.to_i
    y1 = y1.to_i
    dx = (x1 - x0).abs
    dy = (y1 - y0).abs
    sx = x0 < x1 ? 1 : -1
    sy = y0 < y1 ? 1 : -1
    err = dx - dy
    loop do
        set_pixel(x0, y0)
        break if x0 == x1 && y0 == y1
        e2 = 2 * err
        if e2 > -dy
            err -= dy
            x0 += sx
        end
        if e2 < dx
            err += dx
            y0 += sy
        end
    end
end

#draw_quadratic_bezier(x0, y0, x1, y1, x2, y2, steps = 100) ⇒ Object



624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
# File 'lib/pixelflow_canvas.rb', line 624

def draw_quadratic_bezier(x0, y0, x1, y1, x2, y2, steps = 100)
    steps = steps.to_i
    xp = nil
    yp = nil
    (0..steps).each do |i|
        t = i.to_f / steps
        x = (1 - t) ** 2 * x0 + 2 * (1 - t) * t * x1 + t ** 2 * x2
        y = (1 - t) ** 2 * y0 + 2 * (1 - t) * t * y1 + t ** 2 * y2
        if xp && yp
            draw_line(xp.to_i, yp.to_i, x.to_i, y.to_i)
        end
        xp = x
        yp = y
    end
end

#draw_rect(x0, y0, x1, y1) ⇒ Object



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/pixelflow_canvas.rb', line 375

def draw_rect(x0, y0, x1, y1)
    x0 = x0.to_i
    y0 = y0.to_i
    x1 = x1.to_i
    y1 = y1.to_i
    (x0..x1).each do |x|
        set_pixel(x, y0)
    end
    (x0..x1).each do |x|
        set_pixel(x, y1)
    end
    (y0+1..y1-1).each do |y|
        set_pixel(x0, y)
        set_pixel(x1, y)
    end
end

#draw_text(x, y, s, font, scale = 1) ⇒ Object



777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
# File 'lib/pixelflow_canvas.rb', line 777

def draw_text(x, y, s, font, scale = 1)
    Canvas.load_font(font)
    dx = 0
    s.each_char do |c|
        glyph = @@cypher_fonts[font][c.ord]
        if glyph
            w = ((((glyph[:width] - 1) >> 3) + 1) << 3) - 1
            (0...glyph[:height]).each do |iy|
                (0...glyph[:width]).each do |ix|
                    if (((glyph[:bitmap][iy] >> (w - ix)) & 1) == 1)
                        (0...scale).each do |oy|
                            (0...scale).each do |ox|
                                set_pixel(x + (ix + dx) * scale + ox, y + iy * scale + oy)
                            end
                        end
                    end
                end
            end
            dx += glyph[:width]
        end
    end
end

#draw_triangle(x0, y0, x1, y1, x2, y2) ⇒ Object



656
657
658
659
660
# File 'lib/pixelflow_canvas.rb', line 656

def draw_triangle(x0, y0, x1, y1, x2, y2)
    draw_line(x0, y0, x1, y1)
    draw_line(x1, y1, x2, y2)
    draw_line(x2, y2, x0, y0)
end

#ensure_max_fps(fps) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/pixelflow_canvas.rb', line 180

def ensure_max_fps(fps)
    # Existing examples call this once after drawing a frame.
    present if @draw_mode == :direct && @dirty

    frame_time = 1.0 / fps
    now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
    if @last_timestamp
        remaining = frame_time - (now - @last_timestamp)
        sleep remaining if remaining > 0
    end
    @last_timestamp = Process.clock_gettime(Process::CLOCK_MONOTONIC)
end

#fetch_events(&block) ⇒ Object



828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
# File 'lib/pixelflow_canvas.rb', line 828

def fetch_events(&block)
    @socket.write([9].pack('C'))
    @socket.flush
    json = ''
    loop do
        begin
            buffer = @socket.recv_nonblock(1024)
            json += buffer
        rescue IO::WaitReadable
            break
        end
    end
    return if json.empty?
    json.split("\r\n").each do |line|
        line.strip!
        next if line.empty?
        JSON.parse(line).each do |event|
            yield Event.new(event)
        end
    end
end

#fill_arc(x, y, r, a0, a1) ⇒ Object



604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
# File 'lib/pixelflow_canvas.rb', line 604

def fill_arc(x, y, r, a0, a1)
    # x = x.to_i
    # y = y.to_i
    # r = r.to_i
    a0 = a0.to_f
    a1 = a1.to_f
    a0 = a0 * Math::PI / 180.0
    a1 = a1 * Math::PI / 180.0
    da = 1.0 / r
    a = a0
    while a < a1
        x0 = x + r * Math.cos(a)
        y0 = y + r * Math.sin(a)
        a += da
        x1 = x + r * Math.cos(a)
        y1 = y + r * Math.sin(a)
        fill_triangle(x, y, x0, y0, x1, y1)
    end
end

#fill_circle(x, y, r) ⇒ Object



462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# File 'lib/pixelflow_canvas.rb', line 462

def fill_circle(x, y, r)
    x = x.to_i
    y = y.to_i
    r = r.to_i
    f = 1 - r
    ddF_x = 1
    ddF_y = -2 * r
    xx = 0
    yy = r
    (y - r..y + r).each do |i|
        set_pixel(x, i)
    end
    while xx < yy
        if f >= 0
            yy -= 1
            ddF_y += 2
            f += ddF_y
        end
        xx += 1
        ddF_x += 2
        f += ddF_x
        (y - yy..y + yy).each do |i|
            set_pixel(x + xx, i)
            set_pixel(x - xx, i)
        end
        (y - xx..y + xx).each do |i|
            set_pixel(x + yy, i)
            set_pixel(x - yy, i)
        end
    end
end

#fill_ellipse(x, y, ra, rb) ⇒ Object



535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
# File 'lib/pixelflow_canvas.rb', line 535

def fill_ellipse(x, y, ra, rb)
    x = x.to_i
    y = y.to_i
    ra = ra.to_i
    rb = rb.to_i
    a2 = ra * ra
    b2 = rb * rb
    fa2 = 4 * a2
    fb2 = 4 * b2
    x0 = 0
    y0 = rb
    sigma = 2 * b2 + a2 * (1 - 2 * rb)
    while b2 * x0 <= a2 * y0
        (x - x0..x + x0).each do |i|
            set_pixel(i, y + y0)
            set_pixel(i, y - y0)
        end
        if sigma >= 0
            sigma += fa2 * (1 - y0)
            y0 -= 1
        end
        sigma += b2 * ((4 * x0) + 6)
        x0 += 1
    end
    x0 = ra
    y0 = 0
    sigma = 2 * a2 + b2 * (1 - 2 * ra)
    while a2 * y0 <= b2 * x0
        (x - x0..x + x0).each do |i|
            set_pixel(i, y + y0)
            set_pixel(i, y - y0)
        end
        if sigma >= 0
            sigma += fb2 * (1 - x0)
            x0 -= 1
        end
        sigma += a2 * ((4 * y0) + 6)
        y0 += 1
    end
end

#fill_rect(x0, y0, x1, y1) ⇒ Object



392
393
394
395
396
397
398
399
400
401
402
# File 'lib/pixelflow_canvas.rb', line 392

def fill_rect(x0, y0, x1, y1)
    x0 = x0.to_i
    y0 = y0.to_i
    x1 = x1.to_i
    y1 = y1.to_i
    (y0..y1).each do |y|
        (x0..x1).each do |x|
            set_pixel(x, y)
        end
    end
end

#fill_triangle(x0, y0, x1, y1, x2, y2) ⇒ Object



662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
# File 'lib/pixelflow_canvas.rb', line 662

def fill_triangle(x0, y0, x1, y1, x2, y2)
    x0 = x0.to_i
    y0 = y0.to_i
    x1 = x1.to_i
    y1 = y1.to_i
    x2 = x2.to_i
    y2 = y2.to_i
    if y0 > y1
        x0, x1 = x1, x0
        y0, y1 = y1, y0
    end
    if y1 > y2
        x1, x2 = x2, x1
        y1, y2 = y2, y1
    end
    if y0 > y1
        x0, x1 = x1, x0
        y0, y1 = y1, y0
    end
    total_height = y2 - y0
    (0...total_height).each do |i|
        second_half = i > y1 - y0 || y1 == y0
        segment_height = second_half ? y2 - y1 : y1 - y0
        alpha = i.to_f / total_height
        beta = (i - (second_half ? y1 - y0 : 0)).to_f / segment_height
        ax = x0 + (x2 - x0) * alpha
        ay = y0 + (y2 - y0) * alpha
        bx = second_half ? x1 + (x2 - x1) * beta : x0 + (x1 - x0) * beta
        by = second_half ? y1 + (y2 - y1) * beta : y0 + (y1 - y0) * beta
        if ax > bx
            ax, bx = bx, ax
            ay, by = by, ay
        end
        (ax.to_i..bx.to_i).each do |j|
            set_pixel(j, y0 + i)
        end
    end
end

#flipObject



147
148
149
# File 'lib/pixelflow_canvas.rb', line 147

def flip()
    present
end

#flood_fill(x, y, r = nil, g = nil, b = nil) ⇒ Object



701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
# File 'lib/pixelflow_canvas.rb', line 701

def flood_fill(x, y, r = nil, g = nil, b = nil)
    x = x.to_i
    y = y.to_i
    r ||= @r
    g ||= @g
    b ||= @b
    return if x < 0 || x >= @width || y < 0 || y >= @height
    sr = 0
    sg = 0
    sb = 0
    if @color_mode == :rgb
        sr = @screen[(y * @width + x) * 3 + 0]
        sg = @screen[(y * @width + x) * 3 + 1]
        sb = @screen[(y * @width + x) * 3 + 2]
    else
        sr = @screen[y * @width + x]
    end
    stack = [[x, y]]
    while stack.any?
        x, y = stack.pop
        next if x < 0 || x >= @width || y < 0 || y >= @height
        if @color_mode == :rgb
            offset = (y * @width + x) * 3
            if @screen[offset + 0] == sr && @screen[offset + 1] == sg && @screen[offset + 2] == sb
                set_pixel(x, y)
                stack.push([x - 1, y])
                stack.push([x + 1, y])
                stack.push([x, y - 1])
                stack.push([x, y + 1])
            end
        else
            offset = y * @width + x
            if @screen[offset] == sr
                set_pixel(x, y)
                stack.push([x - 1, y])
                stack.push([x + 1, y])
                stack.push([x, y - 1])
                stack.push([x, y + 1])
            end
        end
    end
end

#get_pixel(x, y) ⇒ Object



364
365
366
367
368
369
370
371
372
373
# File 'lib/pixelflow_canvas.rb', line 364

def get_pixel(x, y)
    x = x.to_i
    y = y.to_i
    return 0 if x < 0 || x >= @width || y < 0 || y >= @height
    if @color_mode == :rgb
        return @screen[(y * @width + x) * 3, 3]
    else
        return @screen[y * @width + x]
    end
end

#inspectObject



76
77
78
# File 'lib/pixelflow_canvas.rb', line 76

def inspect
    "#<Pixelflow::Canvas @ #{@width}x#{@height}>"
end

#move_to(x, y) ⇒ Object



297
298
299
300
301
302
303
304
# File 'lib/pixelflow_canvas.rb', line 297

def move_to(x, y)
    x = x.to_i
    y = y.to_i
    return if x < 0 || x >= @width || y < 0 || y >= @height

    @x = x
    @y = y
end

#presentObject

Send one complete framebuffer as one SET_BUFFER packet.



152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/pixelflow_canvas.rb', line 152

def present
    return unless @dirty

    packet = "\x07".b
    packet << @screen.pack('C*')
    @socket.write(packet)
    @socket.flush

    @dirty = false
    @direct_pixel_counter = 0
    @last_present_timestamp =
        Process.clock_gettime(Process::CLOCK_MONOTONIC)
end

#present_if_dueObject

Direct mode remains progressive without checking the clock for every single pixel. We only test the 30 fps deadline every 1024 pixel writes. Existing animation code gets an exact frame boundary through ensure_max_fps below.



170
171
172
173
174
175
176
177
178
# File 'lib/pixelflow_canvas.rb', line 170

def present_if_due
    return unless @draw_mode == :direct && @dirty

    @direct_pixel_counter += 1
    return unless (@direct_pixel_counter & 1023).zero?

    now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
    present if now - @last_present_timestamp >= @direct_frame_interval
end

#recreate_screenObject



96
97
98
# File 'lib/pixelflow_canvas.rb', line 96

def recreate_screen()
    @screen = [0] * @width * @height * (@color_mode == :rgb ? 3 : 1)
end

#remove_maskObject



248
249
250
# File 'lib/pixelflow_canvas.rb', line 248

def remove_mask()
    @mask = nil
end

#run(&block) ⇒ Object



82
83
84
# File 'lib/pixelflow_canvas.rb', line 82

def run(&block)
    instance_eval(&block)
end

#save_as_png(path) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/pixelflow_canvas.rb', line 193

def save_as_png(path)
    if @color_mode == :rgb
        image = ChunkyPNG::Image.from_rgb_stream(@width, @height, @screen.pack('C*'))
        image.save(path)
    else
        buffer = [0] * @width * @height * 3
        offset = 0
        source = 0
        (0...@height).each do |y|
            (0...@width).each do |x|
                i = @screen[source]
                buffer[offset + 0] = @palette[i * 3+ 0]
                buffer[offset + 1] = @palette[i * 3 + 1]
                buffer[offset + 2] = @palette[i * 3 + 2]
                source += 1
                offset += 3
            end
        end
        image = ChunkyPNG::Image.from_rgb_stream(@width, @height, buffer.pack('C*'))
        image.save(path)
    end
end

#set_advance_mode(mode) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/pixelflow_canvas.rb', line 113

def set_advance_mode(mode)
    unless ADVANCE_MODES.keys.include?(mode)
        raise "Invalid advance mode: #{mode}"
    end
    @advance_mode = mode
    @socket.write([4, ADVANCE_MODES[@advance_mode]].pack('CC'))
    @socket.flush
end

#set_color(r, g = 0, b = 0) ⇒ Object



252
253
254
255
256
257
258
259
# File 'lib/pixelflow_canvas.rb', line 252

def set_color(r, g = 0, b = 0)
    r = r.to_i.clamp(0, 255)
    g = g.to_i.clamp(0, 255)
    b = b.to_i.clamp(0, 255)
    @r = r
    @g = g
    @b = b
end

#set_color_mode(mode) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/pixelflow_canvas.rb', line 100

def set_color_mode(mode)
    unless COLOR_MODES.keys.include?(mode)
        raise "Invalid color mode: #{mode}"
    end
    @color_mode = mode
    if mode == :palette
        @palette = VGA_PALETTE.dup
    end
    recreate_screen()
    @socket.write([2, COLOR_MODES[@color_mode]].pack('CC'))
    @socket.flush
end

#set_compose_mode(mode) ⇒ Object



129
130
131
132
133
134
135
136
137
# File 'lib/pixelflow_canvas.rb', line 129

def set_compose_mode(mode)
    unless COMPOSE_MODES.keys.include?(mode)
        raise "Invalid compose mode: #{mode}"
    end
    if mode != :copy && @color_mode != :rgb
        raise "Cannot switch compose mode to #{mode} in palette color mode!"
    end
    @compose_mode = mode
end

#set_draw_mode(mode) ⇒ Object



122
123
124
125
126
127
# File 'lib/pixelflow_canvas.rb', line 122

def set_draw_mode(mode)
    unless DRAW_MODES.keys.include?(mode)
        raise "Invalid draw mode: #{mode}"
    end
    @draw_mode = mode
end

#set_interpolation_mode(mode) ⇒ Object



139
140
141
142
143
144
145
# File 'lib/pixelflow_canvas.rb', line 139

def set_interpolation_mode(mode)
    unless INTERPOLATION_MODES.keys.include?(mode)
        raise "Invalid interpolation mode: #{mode}"
    end
    @socket.write([8, INTERPOLATION_MODES[mode]].pack('CC'))
    @socket.flush
end

#set_mask(&block) ⇒ Object



243
244
245
246
# File 'lib/pixelflow_canvas.rb', line 243

def set_mask(&block)
    @mask = MaskGenerator.new(self)
    @mask.instance_eval(&block)
end

#set_palette(i, r, g, b) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
# File 'lib/pixelflow_canvas.rb', line 261

def set_palette(i, r, g, b)
    i = i.to_i.clamp(0, 255)
    r = r.to_i.clamp(0, 255)
    g = g.to_i.clamp(0, 255)
    b = b.to_i.clamp(0, 255)
    @palette[i * 3 + 0] = r
    @palette[i * 3 + 1] = g
    @palette[i * 3 + 2] = b
    @socket.write([3, i, r, g, b].pack('CCCCC'))
    @socket.flush
end

#set_pixel(x, y, r = nil, g = nil, b = nil) ⇒ Object



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/pixelflow_canvas.rb', line 306

def set_pixel(x, y, r = nil, g = nil, b = nil)
    r ||= @r
    g ||= @g
    b ||= @b
    x = x.to_i
    y = y.to_i
    return if x < 0 || x >= @width || y < 0 || y >= @height
    unless x == @x && y == @y
        move_to(x, y)
    end
    if @mask
        return unless @mask.mask[y * @width + x]
    end
    if @color_mode == :rgb
        offset = (@y * @width + @x) * 3
        if @compose_mode == :add
            r += @screen[offset + 0]
            g += @screen[offset + 1]
            b += @screen[offset + 2]
            r = 255 if r > 255
            g = 255 if g > 255
            b = 255 if b > 255
        elsif @compose_mode == :subtract
            r = @screen[offset + 0] - r
            g = @screen[offset + 1] - g
            b = @screen[offset + 2] - b
            r = 0 if r < 0
            g = 0 if g < 0
            b = 0 if b < 0
        elsif @compose_mode == :multiply
            r = (@screen[offset + 0] * r) / 255
            g = (@screen[offset + 1] * g) / 255
            b = (@screen[offset + 2] * b) / 255
        end
        @screen[offset + 0] = r
        @screen[offset + 1] = g
        @screen[offset + 2] = b
    else
        offset = @y * @width + @x
        @screen[offset] = r
    end
    @dirty = true
    present_if_due
    if @advance_mode == :right
        @x += 1
        if @x >= @width
            @x = 0
            @y = (@y + 1) % @height
        end
    else
        @y += 1
        if @y >= @height
            @y = 0
            @x = (@x + 1) % @width
        end
    end
end

#set_predefined_palette(key) ⇒ Object



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/pixelflow_canvas.rb', line 273

def set_predefined_palette(key)
    if key == :vga
        @palette = VGA_PALETTE.dup
        (0..255).each do |i|
            @socket.write([3, i, @palette[i * 3 + 0], @palette[i * 3 + 1], @palette[i * 3 + 2]].pack('CCCCC'))
        end
        @socket.flush
    else
        @@palettes ||= YAML.load(File.read(File.join(File.dirname(__FILE__), 'pixelflow_canvas', 'palettes.yaml')))
        if @@palettes[key]
            @palette = [0] * 768
            @@palettes[key].each_with_index do |color, i|
                @palette[i * 3 + 0] = color[1, 2].to_i(16)
                @palette[i * 3 + 1] = color[3, 2].to_i(16)
                @palette[i * 3 + 2] = color[5, 2].to_i(16)
                @socket.write([3, i, @palette[i * 3 + 0], @palette[i * 3 + 1], @palette[i * 3 + 2]].pack('CCCCC'))
            end
            @socket.flush
        else
            raise "Invalid predefined palette: #{key}"
        end
    end
end

#set_size(width, height) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/pixelflow_canvas.rb', line 86

def set_size(width, height)
    @x = 0
    @y = 0
    @width = width
    @height = height
    recreate_screen()
    @socket.write([1, width, height].pack('Cnn'))
    @socket.flush
end

#text_width(s, font, scale = 1) ⇒ Object



800
801
802
803
804
805
806
807
808
809
810
# File 'lib/pixelflow_canvas.rb', line 800

def text_width(s, font, scale = 1)
    Canvas.load_font(font)
    width = 0
    s.each_char do |c|
        glyph = @@cypher_fonts[font][c.ord]
        if glyph
            width += glyph[:width] * scale
        end
    end
    width
end