Araq / pixels

Toy support library for primitive graphics programming.
MIT License
12 stars 6 forks source link

nothing appears in window #2

Open ckp95 opened 2 years ago

ckp95 commented 2 years ago

I have the "Mastering Nim" book. Trying to run the first code example:

import pixels

# needed to install sdl2 and sdl2_tff

put_pixel(5, 9)
put_pixel(11, 18, Red)

(btw the book didn't say I needed the sdl2 and sdl2_tff libraries; kept getting compilation errors until I figured it out)

Anyway, I run this example and a window appears, but nothing is in it. It just shows the background pixels fixed in place:

https://user-images.githubusercontent.com/43073868/177007927-34349cce-9341-4960-b871-7c344335d6af.mp4

Is there anything else I need to install to make this work? Or some kind of config setting?

OS is Manjaro Linux KDE.

nixfreak commented 1 year ago
import pixels

  type
    x: int
    y: int

  type
    Direction = enum
      Horizontal
      Vertical

# Create a proc to draw a horizontal line
proc drawHorizontalLine(start: Point, length: Positive) =
  for delta in 0 .. length:
     putPixel(start.x + delta, start.y)

# Create a proc to draw a vertical line
proc drawVerticalLine(start: Point, length: Positive) =
  for delta in 0 .. length:
    putPixel(start.x, start.y + delta)

# Create a proc that will combine both drawHorizontal and drawVertical
proc drawLine(start: Point, length: Positive, direction: Direction) =
  case direction
  of Horizontal:
    drawHorizontalLine(start, length)
  of Vertical:
     drawVerticalLine(start, length)

# crate a point with coordinates x: 60, y: 40
let a = Point(x: 60, y: 40)

#  Call drawLine
drawLine(a, 50, Horizontal)
drawLine(a, 30, Vertical)
nixfreak commented 1 year ago

Archlinux x86-64 nim 1.6.8

I can't get the drawText to work at all , but putPixel does print lines for me in the canvas window.

I have sdl2 installed also.

nixfreak commented 1 year ago

I figured it out you need Mozilla's Fira-sans ttf fonts.

I have the "Mastering Nim" book. Trying to run the first code example:

import pixels

# needed to install sdl2 and sdl2_tff

put_pixel(5, 9)
put_pixel(11, 18, Red)

(btw the book didn't say I needed the sdl2 and sdl2_tff libraries; kept getting compilation errors until I figured it out)

Anyway, I run this example and a window appears, but nothing is in it. It just shows the background pixels fixed in place:

untitled.mp4 Is there anything else I need to install to make this work? Or some kind of config setting?

OS is Manjaro Linux KDE.

You need Mozilla Fira-sans ttf , do this.

sudo pacman -S ttf-fira-sans
JinnRoad commented 1 year ago

Solution on WSL2 Ubuntu: Change the font directory and font that pixels.nim uses, which can be found in ~/.nimble/pkgs2/pixels[...etc]. This is the correction that worked for me. Others will have to point pixels.nim to a font that is already at /usr/share/fonts/[TTF or truetype]/:

    elif defined(linux):
      const location = r"/usr/share/fonts/truetype/"
.
.
.
                        elif defined(linux): "dejavu/DejaVuSans"

Explanation:

JinnRoad commented 1 year ago

For the maintainer @Araq , it looks like on Linux, the default font absolute path can be found via the shell command^1:

fc-match --format=%{file}

This approach may be better than hard-coding fonts and their locations^2. I don't know enough Nim yet to change pixels myself. If the issue still exists when I'm more familiar, I'll give it a go.

Araq commented 1 year ago

But then how can I find the command to use in order to find the font locations? Maybe on Dünnpfiff-Linux fc-match does not exist?

JinnRoad commented 1 year ago

That's true; my Kali installation doesn't have fc-match. Maybe pixels could come with a font and just reference that, as I didn't find an obvious OS-independent solution.