[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;
|
---|
[a35b458] | 44 |
|
---|
[6d5e378] | 45 | source->color = PIXEL(0, 0, 0, 0);
|
---|
| 46 | source->texture = NULL;
|
---|
[00ddb40] | 47 | source->texture_extend = PIXELMAP_EXTEND_TRANSPARENT_BLACK;
|
---|
[6d5e378] | 48 |
|
---|
| 49 | source->alpha = PIXEL(255, 0, 0, 0);
|
---|
| 50 | source->mask = NULL;
|
---|
[00ddb40] | 51 | source->mask_extend = PIXELMAP_EXTEND_TRANSPARENT_BLACK;
|
---|
[6d5e378] | 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 |
|
---|
[00ddb40] | 75 | void source_set_texture(source_t *source, surface_t *texture,
|
---|
| 76 | pixelmap_extend_t extend)
|
---|
[6d5e378] | 77 | {
|
---|
| 78 | source->texture = texture;
|
---|
[00ddb40] | 79 | source->texture_extend = extend;
|
---|
[6d5e378] | 80 | }
|
---|
| 81 |
|
---|
| 82 | void source_set_alpha(source_t *source, pixel_t alpha)
|
---|
| 83 | {
|
---|
| 84 | source->alpha = alpha;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[00ddb40] | 87 | void source_set_mask(source_t *source, surface_t *mask,
|
---|
| 88 | pixelmap_extend_t extend)
|
---|
[6d5e378] | 89 | {
|
---|
| 90 | source->mask = mask;
|
---|
[00ddb40] | 91 | source->mask_extend = extend;
|
---|
[6d5e378] | 92 | }
|
---|
| 93 |
|
---|
[7dba813] | 94 | bool source_is_fast(source_t *source)
|
---|
| 95 | {
|
---|
[75baf6e] | 96 | return ((source->mask == NULL) &&
|
---|
| 97 | (source->alpha == (pixel_t) PIXEL(255, 0, 0, 0)) &&
|
---|
| 98 | (source->texture != NULL) &&
|
---|
| 99 | (transform_is_fast(&source->transform)));
|
---|
[7dba813] | 100 | }
|
---|
| 101 |
|
---|
| 102 | pixel_t *source_direct_access(source_t *source, double x, double y)
|
---|
| 103 | {
|
---|
| 104 | assert(source_is_fast(source));
|
---|
| 105 |
|
---|
[071fefec] | 106 | long _x = (long) (x + source->transform.matrix[0][2]);
|
---|
| 107 | long _y = (long) (y + source->transform.matrix[1][2]);
|
---|
[7dba813] | 108 |
|
---|
[f698054] | 109 | return pixelmap_pixel_at(
|
---|
| 110 | surface_pixmap_access(source->texture), (sysarg_t) _x, (sysarg_t) _y);
|
---|
[7dba813] | 111 | }
|
---|
| 112 |
|
---|
[6d5e378] | 113 | pixel_t source_determine_pixel(source_t *source, double x, double y)
|
---|
| 114 | {
|
---|
| 115 | if (source->mask || source->texture) {
|
---|
| 116 | transform_apply_affine(&source->transform, &x, &y);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | pixel_t mask_pix;
|
---|
| 120 | if (source->mask) {
|
---|
| 121 | mask_pix = source->filter(
|
---|
| 122 | surface_pixmap_access(source->mask),
|
---|
[00ddb40] | 123 | x, y, source->mask_extend);
|
---|
[6d5e378] | 124 | } else {
|
---|
| 125 | mask_pix = source->alpha;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | if (!ALPHA(mask_pix)) {
|
---|
| 129 | return 0;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | pixel_t texture_pix;
|
---|
| 133 | if (source->texture) {
|
---|
| 134 | texture_pix = source->filter(
|
---|
| 135 | surface_pixmap_access(source->texture),
|
---|
[00ddb40] | 136 | x, y, source->texture_extend);
|
---|
[6d5e378] | 137 | } else {
|
---|
| 138 | texture_pix = source->color;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | if (ALPHA(mask_pix) < 255) {
|
---|
| 142 | double ratio = ((double) ALPHA(mask_pix)) / 255.0;
|
---|
| 143 | double res_a = ratio * ((double) ALPHA(texture_pix));
|
---|
| 144 | return PIXEL((unsigned) res_a,
|
---|
| 145 | RED(texture_pix), GREEN(texture_pix), BLUE(texture_pix));
|
---|
| 146 | } else {
|
---|
| 147 | return texture_pix;
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | /** @}
|
---|
| 152 | */
|
---|