Skip to content

Commit 347fbb6

Browse files
authored
Merge pull request #2030 from pewpew-game/stage-scale
Add support for scaling to _stage
2 parents 64bf0f8 + c1e5247 commit 347fbb6

File tree

3 files changed

+31
-20
lines changed

3 files changed

+31
-20
lines changed

shared-bindings/_stage/__init__.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
//| Layer
5151
//| Text
5252
//|
53-
//| .. function:: render(x0, y0, x1, y1, layers, buffer, display)
53+
//| .. function:: render(x0, y0, x1, y1, layers, buffer, display[, scale])
5454
//|
5555
//| Render and send to the display a fragment of the screen.
5656
//|
@@ -61,6 +61,7 @@
6161
//| :param list layers: A list of the :py:class:`~_stage.Layer` objects.
6262
//| :param bytearray buffer: A buffer to use for rendering.
6363
//| :param ~displayio.Display display: The display to use.
64+
//| :param int scale: How many times should the image be scaled up.
6465
//|
6566
//| There are also no sanity checks, outside of the basic overflow
6667
//| checking. The caller is responsible for making the passed parameters
@@ -89,6 +90,10 @@ STATIC mp_obj_t stage_render(size_t n_args, const mp_obj_t *args) {
8990
mp_raise_TypeError(translate("argument num/types mismatch"));
9091
}
9192
displayio_display_obj_t *display = MP_OBJ_TO_PTR(native_display);
93+
uint8_t scale = 1;
94+
if (n_args >= 8) {
95+
scale = mp_obj_get_int(args[7]);
96+
}
9297

9398
while (!displayio_display_begin_transaction(display)) {
9499
#ifdef MICROPY_VM_HOOK_LOOP
@@ -103,12 +108,13 @@ STATIC mp_obj_t stage_render(size_t n_args, const mp_obj_t *args) {
103108
displayio_display_set_region_to_update(display, &area);
104109

105110
display->send(display->bus, true, &display->write_ram_command, 1);
106-
render_stage(x0, y0, x1, y1, layers, layers_size, buffer, buffer_size, display);
111+
render_stage(x0, y0, x1, y1, layers, layers_size, buffer, buffer_size,
112+
display, scale);
107113
displayio_display_end_transaction(display);
108114

109115
return mp_const_none;
110116
}
111-
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(stage_render_obj, 7, 7, stage_render);
117+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(stage_render_obj, 7, 8, stage_render);
112118

113119

114120
STATIC const mp_rom_map_elem_t stage_module_globals_table[] = {

shared-module/_stage/__init__.c

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,35 @@
3434
void render_stage(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1,
3535
mp_obj_t *layers, size_t layers_size,
3636
uint16_t *buffer, size_t buffer_size,
37-
displayio_display_obj_t *display) {
37+
displayio_display_obj_t *display, uint8_t scale) {
3838

3939
size_t index = 0;
4040
for (uint16_t y = y0; y < y1; ++y) {
41-
for (uint16_t x = x0; x < x1; ++x) {
42-
for (size_t layer = 0; layer < layers_size; ++layer) {
41+
for (uint8_t yscale = 0; yscale < scale; ++yscale) {
42+
for (uint16_t x = x0; x < x1; ++x) {
4343
uint16_t c = TRANSPARENT;
44-
layer_obj_t *obj = MP_OBJ_TO_PTR(layers[layer]);
45-
if (obj->base.type == &mp_type_layer) {
46-
c = get_layer_pixel(obj, x, y);
47-
} else if (obj->base.type == &mp_type_text) {
48-
c = get_text_pixel((text_obj_t *)obj, x, y);
44+
for (size_t layer = 0; layer < layers_size; ++layer) {
45+
layer_obj_t *obj = MP_OBJ_TO_PTR(layers[layer]);
46+
if (obj->base.type == &mp_type_layer) {
47+
c = get_layer_pixel(obj, x, y);
48+
} else if (obj->base.type == &mp_type_text) {
49+
c = get_text_pixel((text_obj_t *)obj, x, y);
50+
}
51+
if (c != TRANSPARENT) {
52+
break;
53+
}
4954
}
50-
if (c != TRANSPARENT) {
55+
for (uint8_t xscale = 0; xscale < scale; ++xscale) {
5156
buffer[index] = c;
52-
break;
57+
index += 1;
58+
// The buffer is full, send it.
59+
if (index >= buffer_size) {
60+
display->send(display->bus, false, ((uint8_t*)buffer),
61+
buffer_size * 2);
62+
index = 0;
63+
}
5364
}
5465
}
55-
index += 1;
56-
// The buffer is full, send it.
57-
if (index >= buffer_size) {
58-
display->send(display->bus, false, ((uint8_t*)buffer), buffer_size * 2);
59-
index = 0;
60-
}
6166
}
6267
}
6368
// Send the remaining data.

shared-module/_stage/__init__.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@
3737
void render_stage(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1,
3838
mp_obj_t *layers, size_t layers_size,
3939
uint16_t *buffer, size_t buffer_size,
40-
displayio_display_obj_t *display);
40+
displayio_display_obj_t *display, uint8_t scale);
4141

4242
#endif // MICROPY_INCLUDED_SHARED_MODULE__STAGE

0 commit comments

Comments
 (0)