8000 Revamp the text drawing routine so it can draw backgrounds. · davidgiven/micropython@96c7fe2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 96c7fe2

Browse files
committed
Revamp the text drawing routine so it can draw backgrounds.
1 parent 578c4fc commit 96c7fe2

File tree

1 file changed

+50
-36
lines changed

1 file changed

+50
-36
lines changed

ports/tc32/tc32_screen.c

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
#include <stdlib.h>
21
#include "modtc32.h"
32
#include "port.h"
43
#include "py/binary.h"
54
#include "py/mphal.h"
65
#include "py/runtime.h"
6+
#include <stdlib.h>
77

88
#include "extmod/font_petme128_8x8.h"
99

@@ -699,58 +699,72 @@ static mp_obj_t screen_poly(size_t n_args, const mp_obj_t *args_in) {
699699
}
700700
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(screen_poly_obj, 5, 6, screen_poly);
701701

702-
static void drawchar(char c, int x, int y, int col, int rx, int ry, int dx,
703-
int dy) {
702+
static void drawchar(char c, int x, int y, int fg, int bg, int rx, int ry,
703+
int dx, int dy) {
704704
const uint8_t *cd = &font_petme128_8x8[(c - 32) * 8];
705705
// loop over char data
706706
for (int j = 0; j < 8; j++) {
707707
uint8_t column = *cd++;
708-
int k = 0;
709-
while (column) {
710-
if (column & 1) { // only draw if pixel set
708+
for (int k = 0; k < 8; k++) {
709+
int c = (column & 1) ? fg : bg;
710+
if (c != -1) {
711711
int xx1 = x + rx * j + dx * k;
712712
int yy1 = y + ry * j + dy * k;
713713
int xx2 = xx1 + rx + dx;
714714
int yy2 = yy1 + ry + dy;
715-
fill_rect(MIN(xx1, xx2), MIN(yy1, yy2), abs(rx + dx), abs(ry + dy),
716-
col);
715+
fill_rect(MIN(xx1, xx2), MIN(yy1, yy2), abs(rx + dx), abs(ry + dy), c);
717716
}
718-
k++;
719717
column >>= 1;
720718
}
721719
}
722720
}
723721

724-
static mp_obj_t screen_text(size_t n_args, const mp_obj_t *args_in) {
722+
static mp_obj_t screen_text(size_t n_args, const mp_obj_t *pos_args,
723+
mp_map_t *kw_args) {
725724
static const int8_t directions[4][2][2] = {{{1, 0}, {0, 1}},
726725
{{0, 1}, {-1, 0}},
727726
{{-1, 0}, {0, -1}},
728727
{{0, -1}, {1, 0}}};
729728

730-
const char *str = mp_obj_str_get_str(args_in[1]);
731-
mp_int_t x = mp_obj_get_int(args_in[2]);
732-
mp_int_t y = mp_obj_get_int(args_in[3]);
733-
mp_int_t col = 0xffff;
734-
mp_int_t rx, ry;
735-
mp_int_t dx, dy;
736-
mp_int_t dir = 0;
737-
if (n_args >= 5) {
738-
col = mp_obj_get_int(args_in[4]);
739-
}
740-
if (n_args >= 6) {
741-
dir = mp_obj_get_int(args_in[5]) & 3;
742-
}
743-
rx = directions[dir][0][0];
744-
ry = directions[dir][0][1];
745-
dx = directions[dir][1][0];
746-
dy = directions[dir][1][1];
747-
if (n_args >= 7) {
748-
int scale = mp_obj_get_int(args_in[6]);
749-
rx *= scale;
750-
ry *= scale;
751-
dx *= scale;
752-
dy *= scale;
753-
}
729+
enum {
730+
ARG_self,
731+
ARG_text,
732+
ARG_x,
733+
ARG_y,
734+
ARG_fg,
735+
ARG_bg,
736+
ARG_dir,
737+
ARG_xscale,
738+
ARG_yscale
739+
};
740+
741+
static const mp_arg_t allowed_args[] = {
742+
{MP_QSTR_self, MP_ARG_REQUIRED | MP_ARG_OBJ},
743+
{MP_QSTR_text, MP_ARG_REQUIRED | MP_ARG_OBJ},
744+
{MP_QSTR_x, MP_ARG_REQUIRED | MP_ARG_INT},
745+
{MP_QSTR_y, MP_ARG_REQUIRED | MP_ARG_INT},
746+
{MP_QSTR_fg, MP_ARG_INT, {.u_int = 0xffff}},
747+
{MP_QSTR_bg, MP_ARG_INT, {.u_int = -1}},
748+
{MP_QSTR_dir, MP_ARG_INT, {.u_int = 0}},
749+
{MP_QSTR_xscale, MP_ARG_INT, {.u_int = 1}},
750+
{MP_QSTR_yscale, MP_ARG_INT, {.u_int = 1}}};
751+
752+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
753+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args),
754+
allowed_args, args);
755+
756+
const char *str = mp_obj_str_get_str(args[ARG_text].u_obj);
757+
mp_int_t x = args[ARG_x].u_int;
758+
mp_int_t y = args[ARG_y].u_int;
759+
mp_int_t fg = args[ARG_fg].u_int;
760+
mp_int_t bg = args[ARG_bg].u_int;
761+
mp_int_t dir = args[ARG_dir].u_int;
762+
mp_int_t xscale = args[ARG_xscale].u_int;
763+
mp_int_t yscale = args[ARG_yscale].u_int;
764+
mp_int_t rx = directions[dir][0][0] * xscale;
765+
mp_int_t ry = directions[dir][0][1] * yscale;
766+
mp_int_t dx = directions[dir][1][0] * xscale;
767+
mp_int_t dy = directions[dir][1][1] * yscale;
754768

755769
// loop over chars
756770
for (; *str; ++str) {
@@ -759,13 +773,13 @@ static mp_obj_t screen_text(size_t n_args, const mp_obj_t *args_in) {
759773
if (chr < 32 || chr > 127) {
760774
chr = 127;
761775
}
762-
drawchar(chr, x, y, col, rx, ry, dx, dy);
776+
drawchar(chr, x, y, fg, bg, rx, ry, dx, dy);
763777
x += rx * 8;
764778
y += ry * 8;
765779
}
766780
return mp_const_none;
767781
}
768-
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(screen_text_obj, 4, 7, screen_text);
782+
static MP_DEFINE_CONST_FUN_OBJ_KW(screen_text_obj, 4, screen_text);
769783

770784
static const mp_rom_map_elem_t screen_locals_dict_table[] = {
771785
{MP_ROM_QSTR(MP_QSTR_readreg), MP_ROM_PTR(&screen_readreg_obj)},

0 commit comments

Comments
 (0)
0