|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2017 Radomir Dopieralski |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include <py/runtime.h> |
| 28 | + |
| 29 | +#include "__init__.h" |
| 30 | +#include "Layer.h" |
| 31 | + |
| 32 | +//| .. currentmodule:: _stage |
| 33 | +//| |
| 34 | +//| :class:`Layer` -- Keep information about a single layer of graphics |
| 35 | +//| =================================================================== |
| 36 | +//| |
| 37 | +//| .. class:: Layer(width, height, graphic, palette, [grid]) |
| 38 | +//| |
| 39 | +//| Keep internal information about a layer of graphics (either a |
| 40 | +//| ``Grid`` or a ``Sprite``) in a format suitable for fast rendering |
| 41 | +//| with the ``render()`` function. |
| 42 | +//| |
| 43 | +//| :param int width: The width of the grid in tiles, or 1 for sprites. |
| 44 | +//| :param int height: The height of the grid in tiles, or 1 for sprites. |
| 45 | +//| :param bytearray graphic: The graphic data of the tiles. |
| 46 | +//| :param bytearray palette: The color palette to be used. |
| 47 | +//| :param bytearray grid: The contents of the grid map. |
| 48 | +//| |
| 49 | +//| This class is intended for internal use in the ``stage`` library and |
| 50 | +//| it shouldn't be used on its own. |
| 51 | +//| |
| 52 | +STATIC mp_obj_t layer_make_new(const mp_obj_type_t *type, size_t n_args, |
| 53 | + size_t n_kw, const mp_obj_t *args) { |
| 54 | + mp_arg_check_num(n_args, n_kw, 4, 5, false); |
| 55 | + |
| 56 | + layer_obj_t *self = m_new_obj(layer_obj_t); |
| 57 | + self->base.type = type; |
| 58 | + |
| 59 | + self->width = mp_obj_get_int(args[0]); |
| 60 | + self->height = mp_obj_get_int(args[1]); |
| 61 | + self->x = 0; |
| 62 | + self->y = 0; |
| 63 | + self->frame = 0; |
| 64 | + self->rotation = false; |
| 65 | + |
| 66 | + mp_buffer_info_t bufinfo; |
| 67 | + mp_get_buffer_raise(args[2], &bufinfo, MP_BUFFER_READ); |
| 68 | + self->graphic = bufinfo.buf; |
| 69 | + if (bufinfo.len != 2048) { |
| 70 | + mp_raise_ValueError("graphic must be 2048 bytes long"); |
| 71 | + } |
| 72 | + |
| 73 | + mp_get_buffer_raise(args[3], &bufinfo, MP_BUFFER_READ); |
| 74 | + self->palette = bufinfo.buf; |
| 75 | + if (bufinfo.len != 32) { |
| 76 | + mp_raise_ValueError("palette must be 32 bytes long"); |
| 77 | + } |
| 78 | + |
| 79 | + if (n_args > 4) { |
| 80 | + mp_get_buffer_raise(args[4], &bufinfo, MP_BUFFER_READ); |
| 81 | + self->map = bufinfo.buf; |
| 82 | + if (bufinfo.len < (self->width * self->height) / 2) { |
| 83 | + mp_raise_ValueError("map buffer too small"); |
| 84 | + } |
| 85 | + } else { |
| 86 | + self-> map = NULL; |
| 87 | + } |
| 88 | + |
| 89 | + return MP_OBJ_FROM_PTR(self); |
| 90 | +} |
| 91 | + |
| 92 | +//| .. method:: move(x, y) |
| 93 | +//| |
| 94 | +//| Set the offset of the layer to the specified values. |
| 95 | +//| |
| 96 | +STATIC mp_obj_t layer_move(mp_obj_t self_in, mp_obj_t x_in, mp_obj_t y_in) { |
| 97 | + layer_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 98 | + self->x = mp_obj_get_int(x_in); |
| 99 | + self->y = mp_obj_get_int(y_in); |
| 100 | + return mp_const_none; |
| 101 | +} |
| 102 | +STATIC MP_DEFINE_CONST_FUN_OBJ_3(layer_move_obj, layer_move); |
| 103 | + |
| 104 | +//| .. method:: frame(frame, rotation) |
| 105 | +//| |
| 106 | +//| Set the animation frame of the sprite, and optionally rotation its |
| 107 | +//| graphic. |
| 108 | +//| |
| 109 | +STATIC mp_obj_t layer_frame(mp_obj_t self_in, mp_obj_t frame_in, |
| 110 | + mp_obj_t rotation_in) { |
| 111 | + layer_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 112 | + self->frame = mp_obj_get_int(frame_in); |
| 113 | + self->rotation = mp_obj_get_int(rotation_in); |
| 114 | + return mp_const_none; |
| 115 | +} |
| 116 | +STATIC MP_DEFINE_CONST_FUN_OBJ_3(layer_frame_obj, layer_frame); |
| 117 | + |
| 118 | + |
| 119 | +STATIC const mp_rom_map_elem_t layer_locals_dict_table[] = { |
| 120 | + { MP_ROM_QSTR(MP_QSTR_move), MP_ROM_PTR(&layer_move_obj) }, |
| 121 | + { MP_ROM_QSTR(MP_QSTR_frame), MP_ROM_PTR(&layer_frame_obj) }, |
| 122 | +}; |
| 123 | +STATIC MP_DEFINE_CONST_DICT(layer_locals_dict, layer_locals_dict_table); |
| 124 | + |
| 125 | +const mp_obj_type_t mp_type_layer = { |
| 126 | + { &mp_type_type }, |
| 127 | + .name = MP_QSTR_Layer, |
| 128 | + .make_new = layer_make_new, |
| 129 | + .locals_dict = (mp_obj_dict_t*)&layer_locals_dict, |
| 130 | +}; |
0 commit comments