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
# 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)
    set_size(width, height)
    set_color_mode(color_mode) if color_mode
    @last_timestamp = Time.now.to_f
    if block_given?
        instance_eval(&block)
    end
end

Instance Attribute Details

#color_modeObject (readonly)

Returns the value of attribute color_mode.



62
63
64
# File 'lib/pixelflow_canvas.rb', line 62

def color_mode
  @color_mode
end

#heightObject (readonly)

Returns the value of attribute height.



62
63
64
# File 'lib/pixelflow_canvas.rb', line 62

def height
  @height
end

#widthObject (readonly)

Returns the value of attribute width.



62
63
64
# File 'lib/pixelflow_canvas.rb', line 62

def width
  @width
end

Class Method Details

.load_font(font) ⇒ Object

FONT RENDERING



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
# File 'lib/pixelflow_canvas.rb', line 712

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



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
# File 'lib/pixelflow_canvas.rb', line 542

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



395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/pixelflow_canvas.rb', line 395

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



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

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



460
461
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
493
494
495
496
497
498
499
# File 'lib/pixelflow_canvas.rb', line 460

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



370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/pixelflow_canvas.rb', line 370

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



590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
# File 'lib/pixelflow_canvas.rb', line 590

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



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/pixelflow_canvas.rb', line 341

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



743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
# File 'lib/pixelflow_canvas.rb', line 743

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



622
623
624
625
626
# File 'lib/pixelflow_canvas.rb', line 622

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



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

def ensure_max_fps(fps)
    fps1 = 1.0 / fps
    t = Time.now.to_f
    if @last_timestamp
        loop do
            sleep 0.01
            break if (Time.now.to_f - @last_timestamp) >= fps1
        end
    end
    @last_timestamp = Time.now.to_f
end

#fetch_events(&block) ⇒ Object



794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
# File 'lib/pixelflow_canvas.rb', line 794

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



570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
# File 'lib/pixelflow_canvas.rb', line 570

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



428
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
# File 'lib/pixelflow_canvas.rb', line 428

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



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
534
535
536
537
538
539
540
# File 'lib/pixelflow_canvas.rb', line 501

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



358
359
360
361
362
363
364
365
366
367
368
# File 'lib/pixelflow_canvas.rb', line 358

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



628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
# File 'lib/pixelflow_canvas.rb', line 628

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



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

def flip()
    if @draw_mode == :buffered
        @socket.write([7].pack('C'))
        @socket.write(@screen.pack('C*'))
        @socket.flush
    end
end

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



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
700
701
702
703
704
705
706
707
708
# File 'lib/pixelflow_canvas.rb', line 667

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



330
331
332
333
334
335
336
337
338
339
# File 'lib/pixelflow_canvas.rb', line 330

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



58
59
60
# File 'lib/pixelflow_canvas.rb', line 58

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

#move_to(x, y) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/pixelflow_canvas.rb', line 253

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
    buffer = [5].pack('C')
    buffer += [x].pack((@width <= 256) ? 'C' : 'n')
    buffer += [y].pack((@height <= 256) ? 'C' : 'n')
    @socket.write(buffer)
    @socket.flush
end

#recreate_screenObject



78
79
80
# File 'lib/pixelflow_canvas.rb', line 78

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

#remove_maskObject



204
205
206
# File 'lib/pixelflow_canvas.rb', line 204

def remove_mask()
    @mask = nil
end

#run(&block) ⇒ Object



64
65
66
# File 'lib/pixelflow_canvas.rb', line 64

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

#save_as_png(path) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/pixelflow_canvas.rb', line 149

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



95
96
97
98
99
100
101
102
# File 'lib/pixelflow_canvas.rb', line 95

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



208
209
210
211
212
213
214
215
# File 'lib/pixelflow_canvas.rb', line 208

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



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/pixelflow_canvas.rb', line 82

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



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

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



104
105
106
107
108
109
# File 'lib/pixelflow_canvas.rb', line 104

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



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

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



199
200
201
202
# File 'lib/pixelflow_canvas.rb', line 199

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

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



217
218
219
220
221
222
223
224
225
226
227
# File 'lib/pixelflow_canvas.rb', line 217

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



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/pixelflow_canvas.rb', line 266

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
    if @draw_mode == :direct
        if @color_mode == :rgb
            @socket.write([6, r, g, b].pack('CCCC'))
        else
            @socket.write([6, r].pack('CC'))
        end
        @socket.flush()
    end
    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



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/pixelflow_canvas.rb', line 229

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



68
69
70
71
72
73
74
75
76
# File 'lib/pixelflow_canvas.rb', line 68

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



766
767
768
769
770
771
772
773
774
775
776
# File 'lib/pixelflow_canvas.rb', line 766

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