float get_key_analog_state(const int key_code) override { LP3_ASSERT(key_code >= 0); LP3_ASSERT(key_code < get_key_count()); if (key_code < this->button_count) { return 1 == SDL_JoystickGetButton(joystick, key_code) ? 1.0f : 0.0f; } else if (key_code < this->button_count + (this->hat_count * hat_sides)) { const int subtracted_kc = key_code - this->button_count; const int position_check = subtracted_kc % 4; const int hat_code = subtracted_kc / 4; Uint8 r = SDL_JoystickGetHat(joystick, hat_code); switch(position_check) { case 0: if (SDL_HAT_UP == r) { return 1.0f; } else if (SDL_HAT_LEFTUP == r || SDL_HAT_RIGHTUP == r) { return diag_slop; } return 0.0f; case 1: if (SDL_HAT_DOWN == r) { return 1.0f; } else if (SDL_HAT_LEFTDOWN == r || SDL_HAT_RIGHTDOWN == r) { return diag_slop; } return 0.0f; case 2: if (SDL_HAT_LEFT == r) { return 1.0f; } else if (SDL_HAT_LEFTUP == r || SDL_HAT_LEFTDOWN == r) { return diag_slop; } return 0.0f; case 3: if (SDL_HAT_RIGHT == r) { return 1.0f; } else if (SDL_HAT_RIGHTUP == r || SDL_HAT_RIGHTDOWN == r) { return diag_slop; } return 0.0f; default: LP3_THROW2(lp3::core::Exception, "Bad case!"); } } else { const int subtracted_kc = key_code - (button_count + (hat_sides * hat_count)); const int axis_code = subtracted_kc / axis_sides; const Sint16 r = SDL_JoystickGetAxis(joystick, axis_code); //LP3_LOG_DEBUG("MARIO, %i is %i", axis_code, r); const int position_check = subtracted_kc % axis_sides; if (0 == position_check) { return lp3::narrow<float>(n_dz_adjust(r)) / -32768.0f; } else { return lp3::narrow<float>(dz_adjust(r)) / 32767.0f; } } }
/*Test4 tests inserting a large number of keys, chekcing key_count, deleting a few, rechecking the key_count, then clearing the entire table. We will then reinsert some keys.*/ static int test4(){ int x = 1, y =2, z = 3, w =4, k = 5, a =6, b =7 , c =8, d = 9, e = 10, f = 11, g = 12; Table *t; /*Creates table and adds 13 keys to it.*/ create_table(&t, z, NULL); put(t, "Krabs", &a); put(t, "Larry", &b); put(t, "Herminator", &c); put(t, "Brian", &d); put(t, "Steve", &e); put(t, "Nelson", &f); put(t, "Corwin", &g); put(t, "Spongebob", &x); put(t, "Patrick", &y); put(t, "Squidward", &z); put(t, "Sandy", &w); put(t, "Art thou feeling it now,", NULL); put(t, "Mr. Krabs?", &k); printf("%d\n", get_key_count(t)); display_table(t); /*Removes two keys from the table, then rechekcs key_count and displays the state of the table.*/ remove_entry(t,"Mr. Krabs?"); remove_entry(t,"Art thou feeling it now,"); printf("%d\n", get_key_count(t)); display_table(t); /*Clears the table, then rechecks the key_count.*/ clear_table(t); printf("%d\n", get_key_count(t)); /*Reinserts 7 keys, then checks the state of the table again.*/ put(t, "A1", &a); put(t, "A2", &b); put(t, "A3", &c); put(t, "A4", &d); put(t, "A5", &e); put(t, "A6", &f); put(t, "A7", &g); display_table(t); destroy_table(t); return SUCCESS; }
const char * get_key_name(const int key_code) override { LP3_ASSERT(key_code >= 0); LP3_ASSERT(key_code < get_key_count()); return key_names[key_code].c_str(); }
int find_pressed_key_code() override { for (int i = 0; i < get_key_count(); ++ i) { if (get_key_state(i)) { return i; } } return -1; }
/*Tests creating a table, placing key/value pairs in it, getting values from the table, clearing the entire table, then reinserting some key/value pairs and getting their values. Also tests get_table_size */ static int test2() { int table_size = 4, x = 1, y = 3, z = 4, w = 5, a = 10, b = 2, c = 7, d = 8, e = 9, f = 12, g = 13; Table *t; void *value1, *value2, *value3, *value4, *value5, *value6, *value7, *value8, *value9, *value10, *value11; create_table(&t, table_size, NULL); /*Insert 4 keys into the table.*/ put(t, "Spongebob", &x); put(t, "Patrick", &y); put(t, "Squidward", &z); put(t, "Sandy", &w); /*Check that each key has the correct value, and that get_value is operating correctly.*/ get_value(t, "Spongebob", &value1); get_value(t, "Patrick", &value2); get_value(t, "Squidward", &value3); get_value(t, "Sandy", &value4); printf("Value for Spongebob: %d\n", *(int *) value1); printf("Value for Patrick: %d\n", *(int *) value2); printf("Value for Squidward: %d\n", *(int *) value3); printf("Value for Sandy: %d\n", *(int *) value4); printf("\n"); /*Check the State of the entire table.*/ display_table(t); /*Clear the entire table.*/ clear_table(t); /*Check that get_table_size and get_key_count are workign correctly.*/ if(get_table_size(t) != 4 || get_key_count(t) != 0){ return FAILURE; } /*Make sure the table is empty.*/ display_table(t); /*Reinsert 7 keys.*/ put(t, "Krabs", &a); put(t, "Larry", &b); put(t, "Herminator", &c); put(t, "Brian", &d); put(t, "Steve", &e); put(t, "Nelson", &f); put(t, "Corwin", &g); /*Check that these keys have the correct values associated witht hem.*/ get_value(t, "Krabs", &value5); get_value(t, "Larry", &value6); get_value(t, "Herminator", &value7); get_value(t, "Brian", &value8); get_value(t, "Steve", &value9); get_value(t, "Nelson", &value10); get_value(t, "Corwin", &value11); printf("Value for Krabs: %d\n", *(int *) value5); printf("Value for Larry: %d\n", *(int *) value6); printf("Value for Herminator: %d\n", *(int *) value7); printf("Value for Brian: %d\n", *(int *) value8); printf("Value for Steve: %d\n", *(int *) value9); printf("Value for Nelson: %d\n", *(int *) value10); printf("Value for Corwin: %d\n", *(int *) value11); /*Check the state of the entire table, especially to see if any keys from before clear_table was called are left over. */ display_table(t); printf("\n\n"); destroy_table(t); return SUCCESS; }
/*Tests null and failure cases for create_table, destroy_table, put, get_value, get_key_count*/ static int test3(){ int table_size =5, x =5; Table *t, *p; void *value1, *value2, *value3 = NULL; /*Tests NULL cases for create_table.*/ if(create_table(NULL, table_size, NULL ) != -1){ return FAILURE; }else if(create_table(&t, 0, NULL) != -1){ return FAILURE; }/*Tests NULL case for destroy_table.*/ if (destroy_table(NULL) != -1){ return FAILURE; }/*Tests NULL cases for put.*/ if (put(NULL, "Krabs", &x) != -1){ return FAILURE; }else if (put(t, NULL, &x) != -1){ return FAILURE; } /*Creates one empty table, and one table with two keys.*/ create_table(&t, table_size, NULL); create_table(&p, table_size, NULL); put(t, "Art thou feeling it now,", NULL); put(t, "Mr. Krabs?", &x); /*Tests NULL and FAILURE cases for get_value.*/ if(get_value(NULL, "Mr. Krabs?", &value1) != -1){ return FAILURE; } else if(get_value(t, NULL, &value1) != -1){ return FAILURE; } else if(get_value(t, "Spongebob", &value1) != -1){ return FAILURE; } /*Tests get_value for cases where the value stored is NULL and where the input parameter value is NULL. */ get_value(t, "Mr. Krabs?", &value1); get_value(t, "Art thou feeling it now,", &value2); get_value(t, "Mr. Krabs?", &value3); if(value2 != NULL){ return FAILURE; } printf("How much is Mr. Krabs feeling it: %d/10\n", *(int *) value1); printf("Key Count: %d\n", get_key_count(t)); /*Tests NULL case for get_key_count.*/ if(get_key_count(NULL) != -1){ return FAILURE; } /*Tests NULL and FAILURE cases for remove_entry.*/ if(remove_entry(NULL, "Mr. Krabs?") != -1){ return FAILURE; } else if (remove_entry(t, NULL) != -1){ return FAILURE; } else if (remove_entry(t, "Brian") != -1){ return FAILURE; } /*Tests NULL case for clear_table. */ if (clear_table(NULL) != -1){ return FAILURE; } /*Tests is_empty with NULL, empty, and non-empty table.*/ if (is_empty(t) != 0 || is_empty(NULL) != 1 || is_empty(p) != 1){ return FAILURE; } /*Tests NULL case of get_table_size.*/ if(get_table_size(NULL) != -1){ return FAILURE; } destroy_table(t); destroy_table(p); return SUCCESS; }