[6d5e378] | 1 | /*
|
---|
| 2 | * Copyright (c) 2012 Petr Koupy
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup draw
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <assert.h>
|
---|
| 37 |
|
---|
| 38 | #include "source.h"
|
---|
| 39 |
|
---|
| 40 | void source_init(source_t *source)
|
---|
| 41 | {
|
---|
| 42 | transform_identity(&source->transform);
|
---|
| 43 | source->filter = filter_nearest;
|
---|
| 44 |
|
---|
| 45 | source->color = PIXEL(0, 0, 0, 0);
|
---|
| 46 | source->texture = NULL;
|
---|
| 47 | source->texture_tile = false;
|
---|
| 48 |
|
---|
| 49 | source->alpha = PIXEL(255, 0, 0, 0);
|
---|
| 50 | source->mask = NULL;
|
---|
| 51 | source->mask_tile = false;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | void source_set_transform(source_t *source, transform_t transform)
|
---|
| 55 | {
|
---|
| 56 | source->transform = transform;
|
---|
| 57 | transform_invert(&source->transform);
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | void source_reset_transform(source_t *source)
|
---|
| 61 | {
|
---|
| 62 | transform_identity(&source->transform);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | void source_set_filter(source_t *source, filter_t filter)
|
---|
| 66 | {
|
---|
| 67 | source->filter = filter;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | void source_set_color(source_t *source, pixel_t color)
|
---|
| 71 | {
|
---|
| 72 | source->color = color;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | void source_set_texture(source_t *source, surface_t *texture, bool tile)
|
---|
| 76 | {
|
---|
| 77 | source->texture = texture;
|
---|
| 78 | source->texture_tile = tile;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | void source_set_alpha(source_t *source, pixel_t alpha)
|
---|
| 82 | {
|
---|
| 83 | source->alpha = alpha;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | void source_set_mask(source_t *source, surface_t *mask, bool tile)
|
---|
| 87 | {
|
---|
| 88 | source->mask = mask;
|
---|
| 89 | source->mask_tile = tile;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[7dba813] | 92 | bool source_is_fast(source_t *source)
|
---|
| 93 | {
|
---|
| 94 | return (source->mask == NULL)
|
---|
| 95 | && (source->alpha == (pixel_t) PIXEL(255, 0, 0, 0))
|
---|
| 96 | && (source->texture != NULL)
|
---|
| 97 | && (source->texture_tile == false)
|
---|
| 98 | && transform_is_fast(&source->transform);
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | pixel_t *source_direct_access(source_t *source, double x, double y)
|
---|
| 102 | {
|
---|
| 103 | assert(source_is_fast(source));
|
---|
| 104 |
|
---|
| 105 | long _x = (long) (x + source->transform.m[0][2]);
|
---|
| 106 | long _y = (long) (y + source->transform.m[1][2]);
|
---|
| 107 |
|
---|
| 108 | pixelmap_t *pixmap = surface_pixmap_access(source->texture);
|
---|
| 109 | if (_x < 0 || _x >= (long) pixmap->width || _y < 0 || _y >= (long) pixmap->height) {
|
---|
| 110 | return NULL;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | return pixelmap_pixel_at(pixmap, (sysarg_t) _x, (sysarg_t) _y);
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[6d5e378] | 116 | pixel_t source_determine_pixel(source_t *source, double x, double y)
|
---|
| 117 | {
|
---|
| 118 | if (source->mask || source->texture) {
|
---|
| 119 | transform_apply_affine(&source->transform, &x, &y);
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | pixel_t mask_pix;
|
---|
| 123 | if (source->mask) {
|
---|
| 124 | mask_pix = source->filter(
|
---|
| 125 | surface_pixmap_access(source->mask),
|
---|
| 126 | x, y, source->mask_tile);
|
---|
| 127 | } else {
|
---|
| 128 | mask_pix = source->alpha;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | if (!ALPHA(mask_pix)) {
|
---|
| 132 | return 0;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | pixel_t texture_pix;
|
---|
| 136 | if (source->texture) {
|
---|
| 137 | texture_pix = source->filter(
|
---|
| 138 | surface_pixmap_access(source->texture),
|
---|
| 139 | x, y, source->texture_tile);
|
---|
| 140 | } else {
|
---|
| 141 | texture_pix = source->color;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | if (ALPHA(mask_pix) < 255) {
|
---|
| 145 | double ratio = ((double) ALPHA(mask_pix)) / 255.0;
|
---|
| 146 | double res_a = ratio * ((double) ALPHA(texture_pix));
|
---|
| 147 | return PIXEL((unsigned) res_a,
|
---|
| 148 | RED(texture_pix), GREEN(texture_pix), BLUE(texture_pix));
|
---|
| 149 | } else {
|
---|
| 150 | return texture_pix;
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | /** @}
|
---|
| 155 | */
|
---|