void preload_images() { double start_time = platform_get_time(); double dt; // slightly hardcoded unsigned int start = AssetFile::get_offset(0, AssetFile::IMAGE_DATA); unsigned int size = AssetFile::get_size(AssetFile::IMAGE_DATA); AssetFile read_file; read_file.open(); read_file.seek(start); startup_data = new unsigned char[size]; startup_size = size; read_file.read(startup_data, size); read_file.close(); dt = platform_get_time() - start_time; std::cout << "Image read took " << dt << std::endl; for (int i = 0; i < IMAGE_COUNT; ++i) { Image * image = get_internal_image(i); image->upload_texture(); image->set_static(); } delete[] startup_data; startup_data = NULL; dt = platform_get_time() - start_time; std::cout << "Image preload took " << dt << std::endl; }
void preload_images() { double start_time = platform_get_time(); #if !defined(CHOWDREN_IS_DESKTOP) && !defined(CHOWDREN_IS_ANDROID) #if defined(CHOWDREN_PRELOAD_IMAGES) || defined(CHOWDREN_PRELOAD_ALL) AssetFile fp; fp.open(); FileStream stream(fp); #ifdef CHOWDREN_IS_3DS Render::set_storage(true); #endif for (int i = 0; i < IMAGE_COUNT; i++) { unsigned short handle = stream.read_uint16(); Image * image = get_internal_image(handle); image->upload_texture(); #if !defined(CHOWDREN_PRELOAD_ALL) && defined(CHOWDREN_IS_3DS) if (Render::is_vram_full()) break; #endif image->set_static(); } #ifdef CHOWDREN_IS_3DS Render::set_storage(false); #endif #endif #endif double dt = platform_get_time() - start_time; std::cout << "Image preload took " << dt << std::endl; }
void Media::init() { ChowdrenAudio::open_audio(); double start_time = platform_get_time(); AssetFile fp; fp.open(); #ifdef CHOWDREN_IS_WIIU // experimental code to load faster unsigned int start = AssetFile::get_offset(0, AssetFile::SOUND_DATA); unsigned int size = AssetFile::get_size(AssetFile::SOUND_DATA); startup_data = new unsigned char[size]; startup_size = size; fp.seek(start); fp.read(startup_data, size); for (int i = 0; i < SOUND_COUNT; i++) { add_cache(i); } delete[] startup_data; #else for (int i = 0; i < SOUND_COUNT; i++) { fp.set_item(i, AssetFile::SOUND_DATA); add_cache(i, fp); } #endif std::cout << "Sound bank took " << (platform_get_time() - start_time) << std::endl; }
bool GameManager::update() { #ifdef CHOWDREN_USE_DYNAMIC_NUMBER static int save_time = 0; save_time--; if (save_time <= 0) { save_alterable_debug(); save_time += 60; } #endif #ifdef SHOW_STATS bool show_stats = false; static int measure_time = 0; measure_time -= 1; if (measure_time <= 0) { measure_time = 200; show_stats = true; } #endif #ifdef CHOWDREN_USER_PROFILER static int frame = 0; frame++; std::stringstream ss; ss << "Frame " << frame << ": " << fps_limit.dt << " "; #endif // update input keyboard.update(); mouse.update(); platform_poll_events(); #ifdef CHOWDREN_USE_GWEN gwen.update(); #endif // player controls int new_control = get_player_control_flags(1); player_press_flags = new_control & ~(player_flags); player_flags = new_control; // joystick controls new_control = get_joystick_control_flags(1); joystick_press_flags = new_control & ~(joystick_flags); joystick_release_flags = joystick_flags & ~(new_control); joystick_flags = new_control; #ifdef CHOWDREN_USE_JOYTOKEY for (int i = 0; i < simulate_count; i++) { if (simulate_keys[i].down) { simulate_keys[i].down = false; continue; } keyboard.remove(simulate_keys[i].key); simulate_keys[i] = simulate_keys[simulate_count-1]; i--; simulate_count--; } for (int i = 0; i < CHOWDREN_BUTTON_MAX-1; i++) { int key = key_mappings[i]; if (key == -1) continue; if (is_joystick_pressed_once(1, i+1)) keyboard.add(key); else if (is_joystick_released_once(1, i+1)) keyboard.remove(key); } axis_moved = false; for (int i = 0; i < CHOWDREN_AXIS_MAX-1; i++) { float value = get_joystick_axis(1, i+1); int pos = axis_pos_mappings[i]; int neg = axis_neg_mappings[i]; int axis_value = 0; if (value > deadzone) { last_axis = i; if (pos != -1 && axis_values[i] != 1) keyboard.add(pos); axis_value = 1; } else { if (pos != -1 && axis_values[i] == 1) keyboard.remove(pos); } if (value < -deadzone) { last_axis = i; if (neg != -1 && axis_values[i] != -1) keyboard.add(neg); axis_value = -1; } else { if (neg != -1 && axis_values[i] == -1) keyboard.remove(neg); } axis_values[i] = axis_value; static bool last_move = false; bool new_move = axis_value != 0; if (new_move && new_move != last_move) axis_moved = true; last_move = new_move; } static bool last_connected = false; bool connected = is_joystick_attached(1); pad_selected = connected && last_connected != connected; pad_disconnected = !connected && last_connected != connected; last_connected = connected; #endif // update mouse position platform_get_mouse_pos(&mouse_x, &mouse_y); #ifdef SHOW_STATS if (show_stats) std::cout << "Framerate: " << fps_limit.current_framerate << std::endl; #endif if (platform_has_error()) { if (platform_display_closed()) return false; } else { double event_update_time = platform_get_time(); int ret = update_frame(); #ifdef CHOWDREN_USER_PROFILER ss << (platform_get_time() - event_update_time) << " "; #endif #ifdef SHOW_STATS if (show_stats) std::cout << "Event update took " << platform_get_time() - event_update_time << std::endl; #endif if (ret == 0) return false; else if (ret == 2) return true; if (platform_display_closed()) return false; } double draw_time = platform_get_time(); draw(); #ifdef CHOWDREN_USER_PROFILER ss << (platform_get_time() - draw_time) << " "; #endif #ifdef SHOW_STATS if (show_stats) { std::cout << "Draw took " << platform_get_time() - draw_time << std::endl; #ifndef NDEBUG print_instance_stats(); #endif platform_print_stats(); } #endif fps_limit.finish(); #ifdef CHOWDREN_USER_PROFILER ss << "\n"; std::string logline = ss.str(); user_log.write(&logline[0], logline.size()); #endif #ifdef CHOWDREN_USE_PROFILER static int profile_time = 0; profile_time -= 1; if (profile_time <= 0) { profile_time += 500; PROFILE_UPDATE(); PROFILE_OUTPUT("data:/profile.txt"); } #endif return true; }
static void *novacom_usb_rx_thread(void *arg) { novacom_usb_handle_t *handle = (novacom_usb_handle_t *)arg; transport_recovery_token_t *rec_token = NULL; ///< recovery token int rc; int packet_type; char *buf; int sniff = 1; buf = platform_calloc(MAX_MTU); platform_assert(buf != NULL); LTRACEF("start, handle %p\n", handle); handle->rx_timeout = novacom_usbll_get_timeout(handle->usbll_handle); while (!novacom_shutdown && !handle->shutdown) { platform_time_t st; int time_used; // read a block from the pmux rc = novacom_usb_read(handle, buf, novacom_usbll_get_mtu(handle->usbll_handle)); platform_get_time(&st); time_used = 0; if (rc <= 0) { platform_time_t et; unsigned int count = 0; TRACEL(LOG_ALWAYS, "%s:%d -- usbll(%08x) error: reading packet, result(%d), errno %d\n", __FUNCTION__, __LINE__, novacom_usbll_getuid(handle->usbll_handle), rc, errno); while (rc <= 0 && !handle->shutdown) { //shutdown asap platform_get_time(&et); if (platform_delta_time_msecs(&st, &et) >= g_usbio_retry_timeout) { handle->shutdown = true; break; } if (g_usbio_retry_delay > 0) { if ((g_usbio_retry_timeout-time_used) >= g_usbio_retry_delay) { usleep(g_usbio_retry_delay * 1000); time_used += g_usbio_retry_delay; } else { usleep((g_usbio_retry_timeout - time_used) * 1000); time_used = g_usbio_retry_timeout; } } rc = novacom_usb_read(handle, buf, novacom_usbll_get_mtu(handle->usbll_handle)); count++; } TRACEL(LOG_ALWAYS, "%s:%d -- usbll(%08x) reading packet, reads(%ld), duration(%dms), result(%d), last_errno %ld\n", __FUNCTION__, __LINE__, novacom_usbll_getuid(handle->usbll_handle), count, platform_delta_time_msecs(&st, &et), rc, errno); count = 0; } /* sniff */ if(sniff) { uint32_t uid = ((handle->busnum & 0x0FFFF) << 16) | (handle->devnum & 0x0FFFF); transport_recovery_token_t sniff_token; int ret; /* generate token from packet */ ret = novacom_usbll_generate_recovery_token(buf, rc, &sniff_token); if(ret == -1) { TRACEL(LOG_ERROR, "%s:%d -- Used out system resouce, exit now !!!\n", __FUNCTION__, __LINE__); abort(); } /* check queue for saved connections */ ret = usbrecords_find(&sniff_token); /* free interface recovery token */ platform_free(sniff_token.token); /* check result: create new handle, or recover */ if(ret) { LTRACEF("Unable to recover(%d)\n", ret); handle->usbll_handle = novacom_usbll_create(handle->devtype, MAX_MTU, 0, USBDEVFS_IOCTL_TIMEOUT); } else { TRACEL(LOG_ERROR, "Recovered record...\n"); handle->usbll_handle = sniff_token.user_data; } /* update uid */ novacom_usbll_setuid(handle->usbll_handle, uid); handle->rx_timeout = novacom_usbll_get_timeout(handle->usbll_handle); handle->tx_timeout = novacom_usbll_get_timeout(handle->usbll_handle); sniff = 0; } /* process */ packet_type = PACKET_TYPE_NULL; if (rc > 0) { // process it packet_type = novacom_usbll_process_packet(handle->usbll_handle, buf, rc); if (packet_type == PACKET_TYPE_BADPACKET) { platform_time_t et; TRACEF("received bad packet\n"); platform_get_time(&et); if (platform_delta_time_msecs(&st, &et) >= g_usbio_retry_timeout) { handle->shutdown = true; break; } if (g_usbio_retry_delay > 0) { if ((g_usbio_retry_timeout-time_used) >= g_usbio_retry_delay) { usleep(g_usbio_retry_delay * 1000); time_used += g_usbio_retry_delay; } else { usleep((g_usbio_retry_timeout - time_used) * 1000); time_used = g_usbio_retry_timeout; } } ///handle->shutdown = true; ///break; } else if(handle->tx_startup_wait) { platform_event_signal(&handle->tx_startup_event); } } else { #if TRACE_ZERO_LEN_PACKETS log_printf(LOG_TRACE, "RX zero len\n"); #endif } } LTRACEF("shutting down handle %p\n", handle); /* wake up tx thread (if still waits for startup) */ if(handle->tx_startup_wait) { LTRACEF("wake up tx thread\n"); platform_event_signal(&handle->tx_startup_event); } /* wait for the tx thread to exit */ LTRACEF("waiting on tx thread\n"); platform_event_wait(&handle->tx_shutdown_event); /* RX thread is responsible for cleaning up */ LTRACEF("cleaning up handle %p\n", handle); /* grab recovery token if available */ if(handle->usbll_handle) { rc = -1; rec_token = platform_calloc(sizeof(transport_recovery_token_t)); if(rec_token) { snprintf(rec_token->nduid, sizeof(rec_token->nduid), "%s", novacom_usbll_get_nduid(handle->usbll_handle)); rc = novacom_usbll_get_recovery_token(handle->usbll_handle, rec_token); if(rc != -1) { rc = usbrecords_add(rec_token); } else { LTRACEF("unable to recovery token!!!\n"); } } /* error: free memory, destroy device */ if(rc == -1) { //we should never go here. novacom_usbll_destroy(handle->usbll_handle); platform_free(rec_token); } } novacom_usb_close(handle); platform_event_destroy(&handle->tx_startup_event); platform_event_destroy(&handle->tx_shutdown_event); platform_free(handle); platform_free(buf); return NULL; }
static void *novacom_usb_tx_thread(void *arg) { novacom_usb_handle_t *handle = (novacom_usb_handle_t *)arg; int rc; struct novacom_tx_packet packet; char *buf; buf = platform_calloc(MAX_MTU); platform_assert(buf != NULL); LTRACEF("start::wait for startup event: %p\n", handle); platform_event_wait(&handle->tx_startup_event); //why waiting rx for starting ??? handle->tx_startup_wait = 0; //change status to started LTRACEF("start::startup event received, continue: %p\n", handle); handle->tx_timeout = novacom_usbll_get_timeout(handle->usbll_handle); while (!novacom_shutdown && !handle->shutdown) { // see if we have something to send packet.len = novacom_usbll_get_mtu(handle->usbll_handle); packet.buf = buf; if (novacom_usbll_prepare_tx_packet(handle->usbll_handle, &packet, 100) != TX_NO_PACKET) { // write a block back #if FAULTY_TX if (rand() < (RAND_MAX / 10)) { TRACEF("dropped tx packet\n"); } else { #endif rc = novacom_usb_write(handle, packet.buf, packet.len); if (rc < 0) { platform_time_t st; platform_time_t et; int time_used = 0; unsigned int count = 0; TRACEL(LOG_ALWAYS, "usbll(%08x) error writing packet, result(%d), errno %d\n", novacom_usbll_getuid(handle->usbll_handle), rc, errno); platform_get_time(&st); while (rc < 0 && !handle->shutdown) { //shutdown asap platform_get_time(&et); if (platform_delta_time_msecs(&st, &et) >= g_usbio_retry_timeout) { handle->shutdown = true; break; } if (g_usbio_retry_delay > 0) { if ((g_usbio_retry_timeout-time_used) >= g_usbio_retry_delay) { usleep(g_usbio_retry_delay * 1000); time_used += g_usbio_retry_delay; } else { usleep((g_usbio_retry_timeout - time_used) * 1000); time_used = g_usbio_retry_timeout; } } rc = novacom_usb_write(handle, packet.buf, packet.len); count++; } TRACEL(LOG_ALWAYS, "usbll(%08x) writing packet, writes(%ld), duration(%dms), result(%d), last_errno %ld\n", novacom_usbll_getuid(handle->usbll_handle), count, platform_delta_time_msecs(&st, &et), rc, errno); count = 0; } if (rc >=0) { TRACEF/*LOG_PRINTF*/("usbll(%08x) wrote tx packet len=%d\n", novacom_usbll_getuid(handle->usbll_handle), rc); } #if FAULTY_TX } #endif } } LTRACEF("shutting down handle %p\n", handle); platform_event_signal(&handle->tx_shutdown_event); platform_free(buf); return NULL; }
static void *tx_thread_entry(void *arg) { device_pthread_setaffinity(); int rc; int state; struct novacom_tx_packet packet; const struct aiocb * list[1]; list[0] = &txaio; LOG_PRINTF("entry\n"); while (usb_online) { // see if we have something to send packet.len = novacom_usbll_get_mtu(usbll_handle); packet.buf = tx_buffer; state = novacom_usbll_get_state(usbll_handle); if (novacom_usbll_prepare_tx_packet(usbll_handle, &packet, 100) != TX_NO_PACKET) { #if LOCAL_TRACE_RW // write a block back LTRACEF("going to write packet\n"); #endif #if 0 rc = write(ep1in_fd, packet.buf, packet.len); LTRACEF("rc %d\n", rc); if (rc < 0) { TRACEF("error writing packet\n"); break; } #else rc = queue_tx(ep1in_fd, packet.buf, packet.len); if (rc < 0) { LOG_PRINTF("USB aio_write error, ret=%d, errno=%d\n", rc, errno); usleep(1000*GADGETFS_IO_RETRY_DELAY); novacom_usbll_changeback_state(usbll_handle, state); continue; } struct timespec timeout; rc = suspend(list, 1, &timeout); while (rc < 0 && errno == EAGAIN) { LOG_PRINTF("USB aio_suspend (for write) error, ret=%d, errno=%d\n", rc, errno); rc = suspend(list, 1, &timeout); if (rc >= 0) { LOG_PRINTF("USB aio_suspend (for write) ret=%d, errno=%d\n", rc, errno); } }; #if 0 //do we need it ??? if (rc < 0) { LTRACEF("timeout on tx\n"); rc = aio_suspend(list, 1, NULL); } #endif if (aio_error(&txaio) != EINPROGRESS) { rc = aio_return(&txaio); if (rc < 0) { /* online->offline transition */ LOG_PRINTF("USB aio_return (for write) error, ret=%d, \n", rc); if( (usb_online != gadgetfs_online) && (true == usb_online) ) { int ret = platform_event_wait_timeout(&usb_online_event, TRANSPORT_RECOVERY_TIMEOUT * 1000*2); if (!ret) { LOG_PRINTF("platform_event_wait_timeout for usb_online_event, ret=%d\n", ret); } else { LOG_PRINTF("platform_event_wait_timeout for usb_online_event, ret=%d, ignored\n", ret); } } } } else { LOG_PRINTF("we should never enter here (EINPROGRESS=%d), USB aio write seems to have problem!\n", EINPROGRESS); } if (rc < 0) { novacom_usbll_changeback_state(usbll_handle, state); } else { static platform_time_t prior; static int init = 0; static unsigned int tx_bytes = 0, tx_packets = 0; platform_time_t curr; tx_bytes += packet.len; tx_packets++; if (!init) { platform_get_time(&prior); init = 1; } platform_get_time(&curr); if (platform_delta_time_msecs(&prior, &curr) >= 2800) { //logging for every 3sec platform_get_time(&prior); LOG_PRINTF("wrote %u bytes, %u packets\n",tx_bytes, tx_packets); } } #endif } } LOG_PRINTF("shutting down\n"); platform_event_signal(&tx_shutdown_event); return NULL; }