vydd / sketch

A Common Lisp framework for the creation of electronic art, visual design, game prototyping, game making, computer graphics, exploration of human-computer interaction, and more.
MIT License
1.4k stars 67 forks source link

Added support for drawing partial images #35

Closed compmstr closed 4 years ago

compmstr commented 4 years ago

This will allow users to use things like sprite sheets and tile maps without needing a single image per frame/tile.

Added helper function to generate pixel-based uv rects with pixel-uv-rect

Example code: The two sprites being accessed are at (0, 0 -> 128, 64) and (0, 64 -> 128, 64).

(defsketch image-test
    ((title "Image test")
     (width 400)
     (height 400)
     (img (load-resource "sprite-sheet.png")))
  (background (rgb 1 1 0.5))
  (image-portion img
                 '(50 150 128 64)
                 '(0 0 128 64))
  (with-uv-rect (pixel-uv-rect img '(0 64 128 64))
    (with-pen (make-pen :fill img)
      (rect 50 50 128 64))))
vydd commented 4 years ago

Hey @compmstr ,

This is a really cool feature, and I definitely want to add it, thanks! Before merging this though, I would like to explore an idea with you. How about we make the uv rect a resource? A crop. That way, there would be no need for IMAGE-PORTION, and crops, since they are resources could easily be reused. You could load all characters from a sprite sheet into resource variables and then use them as needed. Please take a look at the following example:

(defsketch image-test
    ((title "Image test")
     (width 400)
     (height 400)
     (img (load-resource "test.png")))
  (background (rgb 1 1 0.5))
  (image (crop img 0 0 128 64) 50 150)
  ;; but also
  (let ((hero (crop img 0 0 128 64)))
    (image hero 50 150)))

I'd really like to hear your thoughts about this.

Thanks again, Danilo

compmstr commented 4 years ago

That seems like a much easier interface. I'll try setting up an image subclass with the extra field, then I can just return the extra value from shader-color-texture-values, and do with-uv-rect when needed in draw-shape. Does that sound like it would work?

vydd commented 4 years ago

Yup, looks like a clean solution to me!

vydd commented 4 years ago

@compmstr Awesome, thanks! https://www.dropbox.com/s/nsgodg0mostn844/Screen%20Recording%202020-04-13%20at%2000.09.28.mov?dl=0

Merging!

compmstr commented 4 years ago

@vydd :+1: Thanks for the feedback!