diff --git a/.gitignore b/.gitignore index af63d0d..df790f9 100644 --- a/.gitignore +++ b/.gitignore @@ -16,4 +16,6 @@ _build .idea *.iml rebar3.crashdump -*~ \ No newline at end of file +*~ +.clj-kondo/ +.lsp/ \ No newline at end of file diff --git a/resources/pacman.png b/resources/pacman.png new file mode 100644 index 0000000..f695c8d Binary files /dev/null and b/resources/pacman.png differ diff --git a/src/doodler/core.clje b/src/doodler/core.clje index 444a5c5..bfbcc49 100644 --- a/src/doodler/core.clje +++ b/src/doodler/core.clje @@ -401,7 +401,7 @@ `width` and `height` parameters. In clj the `format` parameter defines how the pixels are stored. - See the PImage reference for more information. + See the wxImage reference for more information. Possible formats: `:rgb`, `:argb`, `:alpha` (grayscale alpha channel) Prefer using `create-image` over initialising new `wxImage` (or `Image`) @@ -409,6 +409,52 @@ [w h] (p/create-image *canvas* w h)) +(defn load-image + "Loads an image into a variable of type wxImage. Four types of + images ( .gif, .jpg, .tga, .png) images may be loaded. To load + correctly, images must be located in the data directory of the + current sketch. In most cases, load all images in setup to preload + them at the start of the program. Loading images inside draw will + reduce the speed of a program. + + The filename parameter can also be a URL to a file found online. + + If an image is not loaded successfully, the null value is returned + and an error message will be printed to the console. The error + message does not halt the program, however the null value may cause + a NullPointerException if your code does not check whether the value + returned from load-image is nil. + + Depending on the type of error, a wxImage object may still be + returned, but the width and height of the image will be set to + -1. This happens if bad image data is returned or cannot be decoded + properly. Sometimes this happens with image URLs that produce a 403 + error or that redirect to a password prompt, because load-image + will attempt to interpret the HTML as image data." + [filename] + (p/load-image *canvas* (str filename))) + + +(defn image + "Displays images to the screen. Processing currently works with GIF, + JPEG, and Targa images. The color of an image may be modified with + the tint function and if a GIF has transparency, it will maintain + its transparency. The img parameter specifies the image to display + and the x and y parameters define the location of the image from its + upper-left corner. The image is displayed at its original size + unless the width and height parameters specify a different size. The + image-mode fn changes the way the parameters work. A call to + (image-mode :corners) will change the width and height parameters to + define the x and y values of the opposite corner of the image. + + Starting with release 0124, when using the default (JAVA2D) + renderer, smooth will also improve image quality of resized + images." + ([img x y] + (p/image *canvas* img x y)) + ([img x y w h] + (p/image *canvas* img x y w h))) + ;; Stroke & fill (defn- save-current-stroke diff --git a/src/doodler/examples/image.clje b/src/doodler/examples/image.clje new file mode 100644 index 0000000..a019a1c --- /dev/null +++ b/src/doodler/examples/image.clje @@ -0,0 +1,21 @@ +;; Source: https://github.com/quil/quil/blob/master/README.md +(ns doodler.examples.image + (:require [doodler.core :as d])) + +(defn setup + [] + (d/frame-rate 60)) + +(defn draw [] + (d/background 200) + (d/text (str (d/current-frame-rate)) 0 10) + (let [img (d/load-image "resources/pacman.png")] + (d/image img 10 10 50 50))) + +(d/defsketch sketch + :title "Load image" + :size [323 200] + :setup setup + :draw draw + :features [:resizable :keep-on-top] + :bgcolor [200]) diff --git a/src/doodler/protocols.clje b/src/doodler/protocols.clje index 365e04b..2c7ce6c 100644 --- a/src/doodler/protocols.clje +++ b/src/doodler/protocols.clje @@ -6,7 +6,9 @@ (height [this])) (defprotocol IBitmap - (create-image [this w h])) + (create-image [this w h]) + (load-image [this filename]) + (image [this img x y] [this img x y w h])) (defprotocol IPrimitives (arc [this x y w h start end]) diff --git a/src/doodler/wx/panel.clje b/src/doodler/wx/panel.clje index 86704de..a6555d5 100644 --- a/src/doodler/wx/panel.clje +++ b/src/doodler/wx/panel.clje @@ -12,16 +12,28 @@ matrix current-shape] p/ICanvas - (canvas [this] panel) - (width [this] width) - (height [this] height) + (canvas [_this] panel) + (width [_this] width) + (height [_this] height) p/IBitmap - (create-image [this w h] + (create-image [_this w h] (wxBitmap/new w h)) + (load-image [_this filename] + (let [img (wxImage/new)] + (if (wxImage/loadFile img filename) + img + (throw (str "Could not load " filename))))) + (image [_this img x y] + (let [bitmap (wxBitmap/new img)] + (wxDC/drawBitmap bitmap-dc bitmap #erl[x y]))) + (image [_this img x y w h] + (let [img (wxImage/scale img w h) + bitmap (wxBitmap/new img)] + (wxDC/drawBitmap bitmap-dc bitmap #erl[x y]))) p/IPrimitives - (arc [this x y w h start end] + (arc [_this x y w h start end] (let [path (wxGraphicsContext/createPath @gc) r (/ (math/sqrt (+ (* w w) (* h h))) 2)] (wxGraphicsPath/addArc path @@ -144,7 +156,7 @@ (p/show-unsupported-warning! "print-camera")) (print-matrix [this] (let [m (wxGraphicsContext/getTransform @gc) - [a b c d tx ty](wxGraphicsMatrix/get m)] + [a b c d tx ty] (wxGraphicsMatrix/get m)] (println "Matrix:" :a a :b b :c c :d d :tx tx :ty ty))) (print-projection [this] (p/show-unsupported-warning! "print-projection"))