Skip to content

Commit a20490c

Browse files
committed
Add method VertexFormat() and variable fixed-point scaling in Vertex2f()
1 parent 09a9e36 commit a20490c

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

shared-bindings/_eve/__init__.c

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@
4545

4646
typedef struct _mp_obj__EVE_t {
4747
mp_obj_base_t base;
48-
mp_obj_t dest[3];
49-
size_t n;
50-
uint8_t buf[512];
48+
mp_obj_t dest[3]; // Own 'write' method, plus argument
49+
int vscale; // fixed-point scaling used for Vertex2f
50+
size_t n; // Current size of command buffer
51+
uint8_t buf[512]; // Command buffer
5152
} mp_obj__EVE_t;
5253

5354
STATIC const mp_obj_type_t _EVE_type;
@@ -59,7 +60,6 @@ STATIC void _write(mp_obj__EVE_t *_EVE, mp_obj_t b) {
5960

6061
STATIC mp_obj_t _register(mp_obj_t self, mp_obj_t o) {
6162
mp_obj__EVE_t *_EVE = mp_instance_cast_to_native_base(self, &_EVE_type);
62-
_EVE->n = 0;
6363
mp_load_method(o, MP_QSTR_write, _EVE->dest);
6464
return mp_const_none;
6565
}
@@ -115,14 +115,24 @@ STATIC mp_obj_t _cmd0(mp_obj_t self, mp_obj_t n) {
115115
}
116116
STATIC MP_DEFINE_CONST_FUN_OBJ_2(cmd0_obj, _cmd0);
117117

118-
STATIC mp_obj_t _vertex2f(mp_obj_t self , mp_obj_t a0, mp_obj_t a1) {
119-
int16_t x = (int16_t)(16 * mp_obj_get_float(a0));
120-
int16_t y = (int16_t)(16 * mp_obj_get_float(a1));
118+
STATIC mp_obj_t _vertex2f(mp_obj_t self, mp_obj_t a0, mp_obj_t a1) {
119+
mp_obj__EVE_t *_EVE = mp_instance_cast_to_native_base(self, &_EVE_type);
120+
int16_t x = (int16_t)(_EVE->vscale * mp_obj_get_float(a0));
121+
int16_t y = (int16_t)(_EVE->vscale * mp_obj_get_float(a1));
121122
C4(self, (0x40000000 | ((x & 32767) << 15) | (y & 32767)));
122123
return mp_const_none;
123124
}
124125
STATIC MP_DEFINE_CONST_FUN_OBJ_3(vertex2f_obj, _vertex2f);
125126

127+
STATIC mp_obj_t _vertexformat(mp_obj_t self, mp_obj_t a0) {
128+
mp_obj__EVE_t *_EVE = mp_instance_cast_to_native_base(self, &_EVE_type);
129+
uint32_t frac = mp_obj_get_int_truncated(a0);
130+
C4(self, ((0x27 << 24) | (frac & 3)));
131+
_EVE->vscale = 1 << frac;
132+
return mp_const_none;
133+
}
134+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(vertexformat_obj, _vertexformat);
135+
126136
STATIC mp_obj_t _cmd(size_t n_args, const mp_obj_t *args) {
127137
mp_obj_t self = args[0];
128138
mp_obj_t num = args[1];
@@ -180,6 +190,7 @@ STATIC const mp_rom_map_elem_t _EVE_locals_dict_table[] = {
180190
{ MP_ROM_QSTR(MP_QSTR_cc), MP_ROM_PTR(&cc_obj) },
181191
{ MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&flush_obj) },
182192
{ MP_ROM_QSTR(MP_QSTR_Vertex2f), MP_ROM_PTR(&vertex2f_obj) },
193+
{ MP_ROM_QSTR(MP_QSTR_VertexFormat), MP_ROM_PTR(&vertexformat_obj) },
183194
{ MP_ROM_QSTR(MP_QSTR_cmd), MP_ROM_PTR(&cmd_obj) },
184195
{ MP_ROM_QSTR(MP_QSTR_cmd0), MP_ROM_PTR(&cmd0_obj) },
185196
ROM_DECLS
@@ -197,6 +208,8 @@ STATIC mp_obj_t _EVE_make_new(const mp_obj_type_t *type, size_t n_args, const mp
197208
mp_obj__EVE_t *o = m_new_obj(mp_obj__EVE_t);
198209
mp_printf(&mp_plat_print, "_EVE %p make_new\n", o);
199210
o->base.type = &_EVE_type;
211+
o->n = 0;
212+
o->vscale = 16;
200213
return MP_OBJ_FROM_PTR(o);
201214
}
202215

0 commit comments

Comments
 (0)