GFXprim
2D bitmap graphics library with emphasis on speed and correctness
Loading...
Searching...
No Matches
gp_sdl_pixmap.h
1// SPDX-License-Identifier: LGPL-2.1-or-later
2/*
3 * Copyright (C) 2009-2021 Cyril Hrubis <metan@ucw.cz>
4 */
5
6#ifndef BACKENDS_GP_SDL_PIXMAP_H
7#define BACKENDS_GP_SDL_PIXMAP_H
8
9#include <core/gp_pixel.h>
10#include <core/gp_pixmap.h>
11
12/*
13 * This function lets you use GFXprim together with SDL. All you need to do
14 * is to initialize pixmap from surface. The usage is as follows:
15 *
16 * ...
17 *
18 * gp_pixmap c;
19 *
20 * if (gp_pixmap_from_sdl_surface(&c, surface)) {
21 * error("Failed to match PIXEL_TYPE for given surface");
22 * exit(1);
23 * }
24 *
25 * ...
26 *
27 * Now you have initialized pixmap that shares the pixel buffer with
28 * the SDL surface.
29 */
30static inline int gp_pixmap_from_sdl_surface(gp_pixmap *pixmap,
31 const SDL_Surface *surf)
32{
33 /* sanity checks on the SDL surface */
34 if (surf->format->BytesPerPixel == 0) {
35 GP_WARN("Surface->BytesPerPixel == 0");
36 return 1;
37 }
38
39 if (surf->format->BytesPerPixel > 4) {
40 GP_WARN("Surface->BytesPerPixel > 4");
41 return 1;
42 }
43
44 enum gp_pixel_type pixeltype = gp_pixel_rgb_match(surf->format->Rmask,
45 surf->format->Gmask,
46 surf->format->Bmask,
47 surf->format->Ashift,
48 surf->format->BitsPerPixel);
49
50 if (pixeltype == GP_PIXEL_UNKNOWN)
51 return 1;
52
53 /* basic structure and size */
54 pixmap->pixels = surf->pixels;
55 pixmap->pixel_type = pixeltype;
56 pixmap->bytes_per_row = surf->pitch;
57 pixmap->w = surf->w;
58 pixmap->h = surf->h;
59
60 return 0;
61}
62
63#endif /* BACKENDS_GP_SDL_PIXMAP_H */
#define GP_WARN(...)
A debug WARN printf-like macro.
Definition gp_debug.h:104
gp_pixel_type
List of all pixel types.
A pixel description.
gp_pixel_type gp_pixel_rgb_match(gp_pixel rmask, gp_pixel gmask, gp_pixel bmask, gp_pixel amask, uint8_t bits_per_pixel)
Matches a RGB pixel type againts known pixel types.
A pixel buffer.
A pixmap buffer.
Definition gp_pixmap.h:33
uint32_t h
Pixmap height in pixels.
Definition gp_pixmap.h:46
uint8_t * pixels
A pointer to image pixels.
Definition gp_pixmap.h:35
enum gp_pixel_type pixel_type
A pixel format.
Definition gp_pixmap.h:63
uint32_t bytes_per_row
Number of bytes per row.
Definition gp_pixmap.h:42
uint32_t w
Pixmap width in pixels.
Definition gp_pixmap.h:44