You are looking at historical revision 32900 of this page. It may differ significantly from its current revision.

sdl2-image

Introduction

The sdl2-image egg provides bindings to SDL_image version 2. SDL_image is a library for loading various image formats, compatible with Simple DirectMedia Layer version 2 (SDL2), a popular library used in games and other media-rich software.

The sdl2-image egg is designed to be compatible with the sdl2 egg, which provides bindings to SDL 2 itself.

Project / Source Code Repository
https://gitlab.com/chicken-sdl2/chicken-sdl2-image
Issue Tracker
https://gitlab.com/chicken-sdl2/chicken-sdl2-image/issues
Maintainer
John Croisant (john+chicken at croisant dot net)
License
BSD 2-Clause

Requirements

The sdl2-image egg requires the sdl2 egg, Simple DirectMedia Layer version 2.0.0 or higher, and SDL_image version 2.0 or higher. It will not work with older versions of SDL or SDL_image.

Installation

ATTENTION: The sdl2-image egg has not been released yet. For now, you must download it from its source code repository and follow the instructions in the README.

When installing the egg, you should set the SDL2_FLAGS environment variable to a string of compiler flags to be used when compiling the egg. If you have the sdl2-config helper program installed on your system, you can set appropriate flags and install the extension like so (notice these are back ticks, not quotes):

export SDL2_FLAGS=`sdl2-config --cflags --libs`
chicken-install sdl2-image

If you do not have the sdl2-config helper program installed on your computer, you may manually specify SDL-related compiler flags (notice these are double quotes, not back ticks):

export SDL2_FLAGS="-I/usr/local/include/SDL2 -L/usr/local/lib -lSDL2"
chicken-install sdl2-image

By default, the sdl2-image egg will be linked against SDL_image using the compiler flag -lSDL2_image. You can override this by setting the SDL2_IMAGE_FLAGS environment variable, if needed. You can also use that environment variable in case you have installed SDL_image in a different location than SDL.

The SDL2_FLAGS and SDL2_IMAGE_FLAGS environment variables are only needed during installation of the egg. They do not need to be set during normal use.

Usage and Examples

To avoid procedure name collisions, it is recommended that you import the sdl2-image module using a prefix such as "img:", like so:

(use (prefix sdl2-image img:))
(img:load "image.jpg")

The chicken-sdl2-examples repository contains complete example games and programs, some of which use the sdl2-image egg.

Version History

The sdl2-image egg has not yet been released. Coming soon!

API

Quick Start

(use (prefix sdl2 sdl2:)
     (prefix sdl2-image img:)
     (only lolevel
           object-evict object-release))

;; Load an image from a file.
(img:load "path/to/my-image.jpg") ;; or png, gif, etc.

;; Load an image from a blob containing image data.
;; The blob is evicted to static memory so that it will
;; not be moved in memory by the garbage collector.
(let* ((source (object-evict '#${...}))
       (rwops (sdl2:rw-from-blob source))
       (surf (img:load-rw rwops #t)))
  (object-release source)
  surf)

;; Load an image from a string containing image data.
;; Like above, the string is evicted.
(let* ((source (object-evict "..."))
       (rwops (sdl2:rw-from-string source))
       (surf (img:load-rw rwops #t)))
  (object-release source)
  surf)

Conventions

General

[procedure] (init! #!optional loaders) → list of symbols

See IMG_Init.

loaders defaults to '(jpg png tif). It must be a list of zero or more of those symbols, indicating which image loaders to initialize. (Other image formats are built into SDL_image and do not need to be initialized.)

Returns a list of symbols indicating all the image loaders that are now initialized, including any that were already initialized. You should check the return value to see whether all the image loaders you requested were actually initialized. If not, get-error from the sdl2 egg might return an error message explaining why the image loader could not be initialized. (This is a limitation of SDL_image.)

It is not usually necessary to call this procedure. Image loaders will automatically be initialized when needed. But, you may wish to call this procedure to check whether a loader is available, or to initialize the loaders ahead of time to avoid a small delay later.

[procedure] (quit!)

See IMG_Quit.

[procedure] (compiled-version) → list of fixnums
[procedure] (current-version) → list of fixnums

Returns a list of three nonnegative integers, indicating a version number of SDL_image. For example, the list (2 0 0) indicates SDL_image 2.0.0.

For example, the user may have compiled the sdl2-image egg with SDL_image 2.0.0, then later upgraded SDL_image to 2.1.0, but not yet recompiled the sdl2-image egg with the new version. In such a case, compiled-version would return (2 0 0), and current-version would return (2 1 0). But, features from the new version would not be available until the user recompiles the sdl2-image egg.

See SDL_IMAGE_VERSION and IMG_LinkedVersion.

Loading

[procedure] (load filepath) → sdl2:surface or #f
[procedure] (load* filepath) → sdl2:surface or #f

Attempts to load the image file at the given filepath (a string). The image may be any format supported by SDL_image. See IMG_Load.

Returns a sdl2:surface with the image contents, or #f if the image could not be loaded. Call get-error (from the sdl2 egg) to find out what the problem is.

[procedure] (load-rw rwops #!optional close?) → sdl2:surface or #f
[procedure] (load-rw* rwops #!optional close?) → sdl2:surface or #f

Attempts to load an image from an sdl2:rwops. This procedure allows you to load an image from a variety of sources, such as a blob, string, memory pointer, or file. See IMG_Load_RW.

The image may be any format supported by SDL_image. But, if you want to load a TGA image from an sdl2:rwops, you will probably need to use load-typed-rw instead of load-rw.

If close? is #t, the sdl2:rwops will be automatically closed after the image is loaded. See rw-close! in the sdl2 egg. If close? is #f (the default), the sdl2:rwops will not be closed.

Returns a sdl2:surface with the image contents, or #f if the image could not be loaded. Call get-error (from the sdl2 egg) to find out what the problem is.

[procedure] (load-typed-rw rwops close? type-hint) → sdl2:surface or #f
[procedure] (load-typed-rw* rwops close? type) → sdl2:surface or #f

See IMG_LoadTyped_RW.

Similar to load-rw, except you also give a hint to help SDL_image figure out what the image format. In practice, this is only necessary when loading TGA images. For all other image formats you can use load-rw instead.

type-hint must be one of the following strings (case is not important):

Returns a sdl2:surface containing the image, or #f if the image could not be loaded. Call get-error (from the sdl2 egg) to find out what the problem is.