/* * input_test.c - testa o estado de entrada do core (tb_input) e os blocos * de Sensores da tinyblocks-input: mouse em coordenadas do palco, teclas * por nome, toques, sensores e cronometro. */ #include #include #include #include #include #include #include #include #include static int fails = 0; #define CHECK(c, m) do { if (c) printf("ok %s\n", m); else { printf("FAIL %s\n", m); fails++; } } while (0) static TinyBlocks* rt; static TinyBlocks_Block* last; static TinyBlocks_Value* run(const char* type, const char* arg) { if (last) { tb_block_destroy(last, rt); last = NULL; } TinyBlocks_TypeBlock* t = tb_rt_find_type(rt, type); if (!t) { printf(" (tipo %s nao existe)\n", type); return NULL; } last = tb_block_create(rt, t); if (arg) { TinyBlocks_Value v = t->input_descs[0].expected_kind == TB_VALUE_INTEGER ? tb_value_integer(atoll(arg)) : tb_value_text(arg); tb_block_set_default(last, 0, v); tb_value_free(&v); } tb_block_eval(last, rt); return last->output_count ? &last->outputs[0] : NULL; } static long long num(TinyBlocks_Value* v) { if (!v) return -99999; if (v->kind == TB_VALUE_INTEGER) return v->as.integer; if (v->kind == TB_VALUE_REAL) return llround(v->as.real); return -99999; } static double real(TinyBlocks_Value* v) { if (!v) return -99999; return v->kind == TB_VALUE_REAL ? v->as.real : v->kind == TB_VALUE_INTEGER ? (double)v->as.integer : -99999; } int main(void) { tinyblocks_init(&rt, NULL); TinyBlocks_LibraryBlock* lib = tb_library_register(rt, tb_input_get_library()); CHECK(lib && !strcmp(tb_library_category(lib)->name, "Sensores"), "categoria 'Sensores'"); /* vista 480x360 = palco 1:1; (240,180) da vista = centro do palco */ tb_stage_set_view(480, 360); tb_input_reset(); tb_input_begin_frame(); tb_input_set_mouse(240, 180, false, false); CHECK(num(run("Input.MouseX", NULL)) == 0 && num(run("Input.MouseY", NULL)) == 0, "centro da vista = (0, 0) do palco"); tb_input_set_mouse(480, 0, true, false); CHECK(num(run("Input.MouseX", NULL)) == 240 && num(run("Input.MouseY", NULL)) == 180, "canto superior direito = (240, 180), y para cima"); CHECK(num(run("Input.MouseDown", NULL)) == 1, "mouse pressionado?"); tb_input_set_mouse(900, 500, false, false); CHECK(num(run("Input.MouseX", NULL)) == 240 && num(run("Input.MouseOver", NULL)) == 0, "fora do palco: posição presa na borda e 'sobre o palco?' falso"); /* vista maior com faixas (letterbox): 960x540 -> escala 1.5, faixas laterais */ tb_stage_set_view(960, 540); tb_input_set_mouse(480, 270, false, false); CHECK(num(run("Input.MouseX", NULL)) == 0 && num(run("Input.MouseY", NULL)) == 0, "letterbox: centro continua (0, 0)"); tb_input_set_mouse(120 + 720, 270, false, false); CHECK(num(run("Input.MouseX", NULL)) == 240, "letterbox: borda direita do palco = 240"); tb_stage_set_view(480, 360); /* teclado */ CHECK(tb_input_key_from_name("espaço") == TB_KEY_SPACE && tb_input_key_from_name("Space") == TB_KEY_SPACE && tb_input_key_from_name("seta para cima") == TB_KEY_UP && tb_input_key_from_name("a") == TB_KEY_A && tb_input_key_from_name("7") == TB_KEY_0 + 7 && tb_input_key_from_name("qualquer") == -1 && tb_input_key_from_name("f5") == TB_KEY_F5, "nomes de tecla (PT/EN, letras, números, F)"); tb_input_clear_keys(); tb_input_set_key(TB_KEY_SPACE, true); CHECK(num(run("Input.KeyPressed", "espaço")) == 1, "tecla espaço pressionada?"); CHECK(num(run("Input.KeyPressed", "a")) == 0, "tecla a não pressionada"); CHECK(num(run("Input.KeyPressed", "qualquer")) == 1, "qualquer tecla"); tb_input_clear_keys(); CHECK(num(run("Input.KeyPressed", "qualquer")) == 0, "soltou todas"); tb_input_key_pressed(TB_KEY_UP); TinyBlocks_Value* lk = run("Input.LastKey", NULL); CHECK(lk && lk->kind == TB_VALUE_TEXT && !strcmp(lk->as.text, "seta para cima"), "última tecla em português"); /* toque */ float tx[2] = {240, 0}, ty[2] = {180, 0}; tb_input_set_touches(2, tx, ty); CHECK(num(run("Input.TouchCount", NULL)) == 2 && num(run("Input.Touching", NULL)) == 1, "dois dedos na tela"); CHECK(num(run("Input.TouchX", "2")) == -240 && num(run("Input.TouchY", "2")) == 180, "toque 2 no canto (-240, 180)"); tb_input_set_touches(0, NULL, NULL); CHECK(num(run("Input.Touching", NULL)) == 0, "sem toque"); /* sensores */ CHECK(num(run("Input.HasAccel", NULL)) == 0 && num(run("Input.HasGyro", NULL)) == 0, "sem sensores por padrão"); tb_input_set_accel(true, 0.5f, -9.81f, 0.f); tb_input_set_gyro(true, 0.f, 0.f, 3.14159265f); CHECK(num(run("Input.HasAccel", NULL)) == 1 && fabs(real(run("Input.Accel", "y")) + 9.81) < 1e-6, "acelerômetro y = -9.81"); CHECK(fabs(real(run("Input.Gyro", "z")) - 180.0) < 0.01, "giroscópio em graus/s (pi rad/s = 180)"); /* cronômetro */ run("Input.ResetTimer", NULL); double t = real(run("Input.Timer", NULL)); CHECK(t >= 0 && t < 0.5, "zere o cronômetro"); /* booleanos declarados com '?' */ TinyBlocks_TypeBlock* tk = tb_rt_find_type(rt, "Input.KeyPressed"); CHECK(tk && tk->output_descs[0].name[strlen(tk->output_descs[0].name) - 1] == '?', "tecla pressionada? é booleano ('?')"); if (last) tb_block_destroy(last, rt); tinyblocks_deinit(&rt); if (fails) printf("\n%d FALHA(S)\n", fails); else printf("\nTUDO OK\n"); return fails != 0; }