//Initializes the player struct to memory. A player is represented by a circle. Player player_init(char *name, int x, int y, float radius, int lives, int damage, int fire_rate) { Player self = malloc(sizeof *self); if (self == NULL) return NULL; /* Initialize variables */ self->name = name; //Centre Location self->x = x; self->y = y; self->radius = radius; self->lives = lives; self->damage = damage; self->fire_rate = fire_rate; //Variables with no parameters self->player_texture = NULL; self->x_velocity = 30; self->y_velocity = 30; self->player_direction = EAST; //Flags self->flag_focus = false; //Frames self->frame_last_fired = 0; self->frame_invincibility = 0; //Set timers self->timer_last_vertical_move = osGetTime(); self->timer_last_horizontal_move = osGetTime(); return self; }
static int lua_startBMPV(lua_State *L){ int argc = lua_gettop(L); if ((argc != 3) && (argc != 4)) return luaL_error(L, "wrong number of arguments"); BMPV* src = (BMPV*)luaL_checkint(L, 1); int loop = luaL_checkint(L, 2); int ch1 = luaL_checkint(L, 3); if (argc == 4){ int ch2 = luaL_checkint(L, 4); src->ch2 = ch2; } src->loop = loop; src->isPlaying = true; src->ch1 = ch1; src->currentFrame = 0; u32 bytesRead; if (src->samplerate != 0 && src->audio_size != 0 && !GW_MODE){ while(src->mem_size > MAX_RAM_ALLOCATION){ src->mem_size = src->mem_size / 2; } if (src->audiotype == 1){ FSFILE_Read(src->sourceFile, &bytesRead, 28, src->audiobuf, src->mem_size); GSPGPU_FlushDataCache(NULL, src->audiobuf, src->audio_size); My_CSND_playsound(ch1, CSND_LOOP_ENABLE, CSND_ENCODING_PCM16, src->samplerate, (u32*)src->audiobuf, (u32*)src->audiobuf, src->mem_size, 0xFFFF, 0xFFFF); }else{ u8* audiobuf = (u8*)linearAlloc(src->mem_size); FSFILE_Read(src->sourceFile, &bytesRead, 28, audiobuf, src->mem_size); src->audiobuf = (u8*)linearAlloc(src->mem_size/2); src->audiobuf2 = (u8*)linearAlloc(src->mem_size/2); u32 off=0; u32 i=0; u16 z; while (i < (src->mem_size)){ z=0; while (z < (src->bytepersample/2)){ src->audiobuf[off+z] = audiobuf[i+z]; src->audiobuf2[off+z] = audiobuf[i+z+(src->bytepersample/2)]; z++; } i=i+src->bytepersample; off=off+(src->bytepersample/2); } linearFree(audiobuf); GSPGPU_FlushDataCache(NULL, src->audiobuf, src->mem_size/2); GSPGPU_FlushDataCache(NULL, src->audiobuf2, src->mem_size/2); My_CSND_playsound(src->ch1, CSND_LOOP_ENABLE, CSND_ENCODING_PCM16, src->samplerate, (u32*)src->audiobuf, (u32*)src->audiobuf, src->mem_size/2, 0xFFFF, 0); My_CSND_playsound(src->ch2, CSND_LOOP_ENABLE, CSND_ENCODING_PCM16, src->samplerate, (u32*)src->audiobuf2, (u32*)src->audiobuf2, src->mem_size/2, 0, 0xFFFF); } src->tick = osGetTime(); CSND_setchannel_playbackstate(ch1, 1); if (src->audiotype == 2){ CSND_setchannel_playbackstate(src->ch2, 1); } CSND_sharedmemtype0_cmdupdatestate(0); src->moltiplier = 1; }else{ src->tick = osGetTime(); } return 0; }
//Checks if a player can legally move on the axis. This is limited by the MOVE_LIMIT rate. bool player_can_move(Player self, Direction direction) { if (direction == NORTH || direction == SOUTH) { //Vertical int time_since_last_vertical_move = osGetTime() - self->timer_last_vertical_move; if (time_since_last_vertical_move < MOVE_LIMIT) return false; else return true; } else { // Horizontal int time_since_last_horizontal_move = osGetTime() - self->timer_last_horizontal_move; if (time_since_last_horizontal_move < MOVE_LIMIT) return false; else return true; } }
void sf2d_swapbuffers() { gfxSwapBuffersGpu(); if (vblank_wait) { gspWaitForEvent(GSPGPU_EVENT_VBlank0, false); } //Calculate FPS frames++; u64 delta_time = osGetTime() - last_time; if (delta_time >= 1000) { current_fps = frames/(delta_time/1000.0f); frames = 0; last_time = osGetTime(); } }
static int lua_newT(lua_State *L) { int argc = lua_gettop(L); #ifndef SKIP_ERROR_HANDLING if (argc != 0) return luaL_error(L, "wrong number of arguments."); #endif Timer* new_timer = (Timer*)malloc(sizeof(Timer)); // Calculating current frame new_timer->tick = osGetTime(); new_timer->magic = 0x4C544D52; new_timer->isPlaying = true; drawCommand("Timer.new: ","Timer created at offset 0x%lX.\n",(u32)new_timer); allocatedDatas = allocatedDatas + sizeof(Timer); size_t percent = (allocatedDatas * 100) / maxDatas; drawDebug("Debug: ","Allocated %lu bytes (Mem. usage: %u%%).\n",sizeof(Timer), percent); if (percent > 80) drawWarning("Warning: ", "High memory usage!"); if (percent > 100){ drawError("FATAL ERROR: ", "Out of memory!"); return luaL_error(L, "internal error."); } lua_pushinteger(L,(u32)new_timer); return 1; }
static int lua_time(lua_State *L) { int argc = lua_gettop(L); if (argc != 1) return luaL_error(L, "wrong number of arguments"); u64 timer = luaL_checknumber(L,1); lua_pushnumber(L,(osGetTime()-timer)); return 1; }
void drawStatusBar(bool wifiStatus, bool charging, int batteryLevel) { u64 timeInSeconds = osGetTime() / 1000; u64 dayTime = timeInSeconds % SECONDS_IN_DAY; u8 hour = dayTime / SECONDS_IN_HOUR; u8 min = (dayTime % SECONDS_IN_HOUR) / SECONDS_IN_MINUTE; u8 seconds = dayTime % SECONDS_IN_MINUTE; char timeString[9]; sprintf(timeString, "%02d:%02d:%02d", hour, min, seconds); gfxDrawText(GFX_TOP, GFX_LEFT, NULL, timeString, 240 - 18, 400 / 2 - 16); if(wifiStatus) { gfxDrawSpriteAlphaBlend(GFX_TOP, GFX_LEFT, (u8*)wifi_full_bin, 18, 20, 240 - 18, 0); } else { gfxDrawSpriteAlphaBlend(GFX_TOP, GFX_LEFT, (u8*)wifi_none_bin, 18, 20, 240 - 18, 0); } if(charging) { gfxDrawSpriteAlphaBlend(GFX_TOP, GFX_LEFT, (u8*)battery_charging_bin, 18, 27, 240 - 18, 400 - 27); } else { gfxDrawSpriteAlphaBlend(GFX_TOP, GFX_LEFT, batteryLevels[batteryLevel], 18, 27, 240 - 18, 400 - 27); } }
bool Handler::handleMessageUnsafe() { auto message = peek(); bool handled = false; if (message) { //printf("Message %p (time %llu current %llu next %p)\n", *message, message->when, osGetTime(), *message->next); } else { //printf("No message\n"); } if (message && message->when <= osGetTime()) { if (message->callback) { message->callback(); } if (callback) { callback(message); } handled = true; pop(); } return handled; }
void GUI_Clock() { u64 timeInSeconds = osGetTime() / 1000; u64 dayTime = timeInSeconds % SECONDS_IN_DAY; sprintf(buffer, "%02llu:%02llu", dayTime / SECONDS_IN_HOUR, (dayTime % SECONDS_IN_HOUR) / SECONDS_IN_MINUTE); gfxDrawText(GFX_TOP, GFX_LEFT, &fontBlack, buffer, 365, 238 - fontBlack.height * 1); }
static int lua_newT(lua_State *L) { int argc = lua_gettop(L); if (argc != 0) return luaL_error(L, "wrong number of arguments"); Timer* new_timer = (Timer*)malloc(sizeof(Timer)); new_timer->tick = osGetTime(); new_timer->magic = 0x4C544D52; new_timer->isPlaying = true; lua_pushinteger(L,(uint32_t)new_timer); return 1; }
void guiClock() { //CLOCK drawFillRect(276, 196, 399, 219, 255, 0, 0, screenTopLeft); u64 timeInSeconds = osGetTime() / 1000; u64 dayTime = timeInSeconds % SECONDS_IN_DAY; sprintf(buffer, "%.2llu:%.2llu:%.2llu", dayTime / SECONDS_IN_HOUR, (dayTime % SECONDS_IN_HOUR) / SECONDS_IN_MINUTE, dayTime % SECONDS_IN_MINUTE); gfxDrawText(GFX_TOP, GFX_LEFT, NULL, buffer, 300, 248 - fontDefault.height * 14); }
static int lua_newT(lua_State *L) { int argc = lua_gettop(L); #ifndef SKIP_ERROR_HANDLING if (argc != 0) return luaL_error(L, "wrong number of arguments."); #endif Timer* new_timer = (Timer*)malloc(sizeof(Timer)); new_timer->tick = osGetTime(); new_timer->magic = 0x4C544D52; new_timer->isPlaying = true; lua_pushinteger(L,(u32)new_timer); return 1; }
static int lua_reset(lua_State *L) { int argc = lua_gettop(L); if (argc != 1) return luaL_error(L, "wrong number of arguments"); Timer* src = (Timer*)luaL_checkinteger(L, 1); #ifndef SKIP_ERROR_HANDLING if (src->magic != 0x4C544D52) return luaL_error(L, "attempt to access wrong memory block type"); #endif if (src->isPlaying) src->tick = osGetTime(); else src->tick = 0; return 0; }
int sf2d_init_advanced(int gpucmd_size, int temppool_size) { if (sf2d_initialized) return 0; gpu_fb_addr = vramMemAlign(400*240*8, 0x100); gpu_depth_fb_addr = vramMemAlign(400*240*8, 0x100); gpu_cmd = linearAlloc(gpucmd_size * 4); pool_addr = linearAlloc(temppool_size); pool_size = temppool_size; gpu_cmd_size = gpucmd_size; //gfxInitDefault(); GPU_Init(NULL); //gfxSet3D(false); GPU_Reset(NULL, gpu_cmd, gpucmd_size); //Setup the shader dvlb = DVLB_ParseFile((u32 *)shader_vsh_shbin, shader_vsh_shbin_size); shaderProgramInit(&shader); shaderProgramSetVsh(&shader, &dvlb->DVLE[0]); //Get shader uniform descriptors projection_desc = shaderInstanceGetUniformLocation(shader.vertexShader, "projection"); shaderProgramUse(&shader); matrix_init_orthographic(ortho_matrix_top, 0.0f, 400.0f, 0.0f, 240.0f, 0.0f, 1.0f); matrix_init_orthographic(ortho_matrix_bot, 0.0f, 320.0f, 0.0f, 240.0f, 0.0f, 1.0f); matrix_gpu_set_uniform(ortho_matrix_top, projection_desc); //Register the apt callback hook //aptHook(&apt_hook_cookie, apt_hook_func, NULL); vblank_wait = 1; current_fps = 0.0f; frames = 0; last_time = osGetTime(); cur_screen = GFX_TOP; cur_side = GFX_LEFT; GPUCMD_Finalize(); GPUCMD_FlushAndRun(); gspWaitForP3D(); sf2d_pool_reset(); sf2d_initialized = 1; return 1; }
// Timer for the player's speed void timerStep(void) { int currTime = osGetTime(); // Set and calculate the timer dt = currTime - prevTime; dt *= 0.15; // TODO: Why 0.15? // We don't want to dt to be negative. // TODO: Can this ever actually happen? if (dt < 0) dt = 0; // Set previous time to the current time prevTime = currTime; }
static int lua_time(lua_State *L) { int argc = lua_gettop(L); if (argc != 1) return luaL_error(L, "wrong number of arguments"); Timer* src = (Timer*)luaL_checkinteger(L,1); #ifndef SKIP_ERROR_HANDLING if (src->magic != 0x4C544D52) return luaL_error(L, "attempt to access wrong memory block type"); #endif if (src->isPlaying){ lua_pushinteger(L, (osGetTime() - src->tick)); }else{ lua_pushinteger(L, src->tick); } return 1; }
static int lua_resumeBMPV(lua_State *L){ int argc = lua_gettop(L); if (argc != 1) return luaL_error(L, "wrong number of arguments"); BMPV* src = (BMPV*)luaL_checkint(L, 1); src->isPlaying = true; src->tick = (osGetTime() - src->tick); if (src->samplerate != 0 && src->audio_size != 0 && !GW_MODE){ CSND_setchannel_playbackstate(src->ch1, 1); if (src->audiotype == 2){ CSND_setchannel_playbackstate(src->ch2, 1); } CSND_sharedmemtype0_cmdupdatestate(0); } return 0; }
void setTheme(char * themeName) { audio_stop(); //Save the selected theme in main config setConfigString("currentTheme", themeName, configTypeMain); //Reload theme config loadConfigWithType(configTypeTheme); //Reload theme variables for menu loadThemeConfig(); loadSplashImages(); if (themeImageExists(themeImageSplashTop)) { drawThemeImage(themeImageSplashTop, GFX_TOP, 0, 0); } if (themeImageExists(themeImageSplashBottom)) { drawThemeImage(themeImageSplashBottom, GFX_BOTTOM, 0, 0); } gfxFlip(); int startMs = osGetTime(); playBootSound(); //Reload theme images initThemeImages(); //Load BGM initThemeSounds(); //Re-initialise colours initColours(); //Force reload of GUI elements statusbarNeedsUpdate = true; toolbarNeedsUpdate = true; alphaImagesDrawn = false; waitForDurationOfSound(&themeSoundBoot, startMs); startBGM(); panelsDrawn = false; pageControlPanelsDrawn = false; }
//--------------------------------------------------------------------------- int CClock::onTimer() { char c[20]; u64 t; int h,m,s; t = osGetTime() / 1000; s = t % 86400; h = s / 3600; s -= h * 3600; m = s / 60; s -= m * 60; sprintf(c,"%02d:%02d.%02d",h,m,s); set_Text(c); top->Invalidate(); return 0; }
void resetGame() { //Init all vars u64 timeInSeconds = osGetTime() / 1000; srand (timeInSeconds); cur_field=0; cur_row=0; win=0; int x; for (x=0; x < 4; x++) { local_field[x]=0; ans[x]=(rand() % col_amount)+1; } for (x=0; x < 28; x++) { full_field[x]=0; full_judgement[x]=0; } }
// startMusic: Plays a song with network streaming feature void startMusic(Socket* sock, Music* src) { closeStream = false; src->streamLoop = false; // TODO: Add looping feature songPointer = 0; netSize = 0; u32 ch = 0x08; u32 ch2 = 0x09; bool non_native_encode = false; ThreadFunc streamFunction = streamWAV; u8 tmp_encode; /*if (src->encoding == CSND_ENCODING_VORBIS){ streamFunction = streamOGG; tmp_encode = src->encoding; src->encoding = CSND_ENCODING_PCM16; non_native_encode = true; }*/ int raw_format; if (src->audiotype == 1) raw_format = NDSP_FORMAT_MONO_PCM16; else raw_format = NDSP_FORMAT_STEREO_PCM16; ndspChnReset(ch); ndspChnWaveBufClear(ch); ndspChnSetInterp(ch, NDSP_INTERP_LINEAR); ndspChnSetRate(ch, float(src->samplerate)); ndspChnSetFormat(ch, raw_format); ndspWaveBuf* waveBuf = (ndspWaveBuf*)calloc(1, sizeof(ndspWaveBuf)); createDspBlock(waveBuf, src->bytepersample, src->mem_size, 0, (u32*)src->audiobuf); src->blocks = NULL; populatePurgeTable(src, waveBuf); ndspChnWaveBufAdd(ch, waveBuf); src->tick = osGetTime(); src->wavebuf = waveBuf; src->ch = ch; src->isPlaying = true; src->lastCheck = ndspChnGetSamplePos(ch); src->streamLoop = false; svcCreateEvent(&updateStream,0); cachePackage* pkg = (cachePackage*)malloc(sizeof(cachePackage)); pkg->client = sock; pkg->song = src; svcSignalEvent(updateStream); threadCreate(streamFunction, pkg, 8192, 0x18, 0, true); src->isPlaying = true; }
void Handler::run() { lock(); running = true; while (running) { // TODO: volatile bool for running state if (!handleMessageUnsafe()) { // If there is a message on the queue, find the reason why it didn't execute auto message = peek(); u64 time = osGetTime(); if (message && message->when > time) { u64 when = message->when; unlock(); svcWaitSynchronization(eventHandle, (when-time)*1000000LL); } else { unlock(); svcWaitSynchronization(eventHandle, UINT64_MAX); } lock(); } svcClearEvent(eventHandle); } unlock(); }
int main() { // Initialize services srvInit(); aptInit(); hidInit(NULL); gfxInit(); CSND_initialize(NULL); srand(osGetTime()); //gfxSet3D(true); // uncomment if using stereoscopic 3D u8 *audiobuf = linearAlloc(sadloop_bin_size); int i; for (i=0;i<sadloop_bin_size;i++) { memcpy(&audiobuf[i], &sadloop_bin[i], 1); } bool flip = false; u64 nextflip = osGetTime()+500; u8 *crow = linearAlloc(crow1_bin_size); for (i=0;i<crow1_bin_size;i++) { memcpy(&crow[i], &crow1_bin[i], 1); } CSND_setchannel_playbackstate(0x8, 0); CSND_sharedmemtype0_cmdupdatestate(0); u64 nextsong = osGetTime()+68000; CSND_playsound(0x8, CSND_LOOP_ENABLE, CSND_ENCODING_PCM16, 22050, (u32*)audiobuf, NULL, sadloop_bin_size, 2, 0); char* fact; u64 nextfact = osGetTime()+newFact(&fact); // Main loop while (aptMainLoop()) { gspWaitForVBlank(); hidScanInput(); u64 now = osGetTime(); if (now >= nextsong) { nextsong = now+68000; CSND_playsound(0x8, CSND_LOOP_ENABLE, CSND_ENCODING_PCM16, 22050, (u32*)audiobuf, NULL, sadloop_bin_size, 2, 0); } if (now >= nextflip) { nextflip = now+500; if (flip) { for (i=0;i<crow1_bin_size;i++) { memcpy(&crow[i], &crow1_bin[i], 1); } flip = false; } else { for (i=0;i<crow1_bin_size;i++) { memcpy(&crow[i], &crow2_bin[i], 1); } flip = true; } } if (now >= nextfact) { nextfact = now+newFact(&fact); } u32 kDown = hidKeysDown(); if (kDown & KEY_START) break; // break in order to return to hbmenu // Please note that the 3DS screens are sideways (thus 240x400 and 240x320) u8* fb = gfxGetFramebuffer(GFX_TOP, GFX_LEFT, NULL, NULL); memset(fb, 0, 240*400*3); tsDrawWord(GFX_TOP, GFX_LEFT, "Fact Crow says:", 30, 180, 255, 255, 255); drawFact(fact); gfxDrawSprite(GFX_TOP, GFX_LEFT, crow, 37, 37, 138, 30); // Flush and swap framebuffers gfxFlushBuffers(); gfxSwapBuffers(); } CSND_shutdown(); linearFree(audiobuf); // Exit services gfxExit(); hidExit(); aptExit(); srvExit(); return 0; }
static Result task_data_op_copy(data_op_data* data, u32 index) { data->currProcessed = 0; data->currTotal = 0; data->bytesPerSecond = 0; data->estimatedRemainingSeconds = 0; Result res = 0; bool isDir = false; if(R_SUCCEEDED(res = data->isSrcDirectory(data->data, index, &isDir)) && isDir) { res = data->makeDstDirectory(data->data, index); } else { u32 srcHandle = 0; if(R_SUCCEEDED(res = data->openSrc(data->data, index, &srcHandle))) { if(R_SUCCEEDED(res = data->getSrcSize(data->data, srcHandle, &data->currTotal))) { if(data->currTotal == 0) { if(data->copyEmpty) { u32 dstHandle = 0; if(R_SUCCEEDED(res = data->openDst(data->data, index, NULL, data->currTotal, &dstHandle))) { res = data->closeDst(data->data, index, true, dstHandle); } } else { res = R_APP_BAD_DATA; } } else { u8* buffer = (u8*) calloc(1, data->bufferSize); if(buffer != NULL) { u32 dstHandle = 0; u64 ioStartTime = 0; u64 lastBytesPerSecondUpdate = osGetTime(); u32 bytesSinceUpdate = 0; bool firstRun = true; while(data->currProcessed < data->currTotal) { if(R_FAILED(res = task_data_op_check_running(data, data->processed, &srcHandle, &dstHandle))) { break; } u32 bytesRead = 0; if(R_FAILED(res = data->readSrc(data->data, srcHandle, &bytesRead, buffer, data->currProcessed, data->bufferSize))) { break; } if(firstRun) { firstRun = false; if(R_FAILED(res = data->openDst(data->data, index, buffer, data->currTotal, &dstHandle))) { break; } } u32 bytesWritten = 0; if(R_FAILED(res = data->writeDst(data->data, dstHandle, &bytesWritten, buffer, data->currProcessed, bytesRead))) { break; } data->currProcessed += bytesWritten; bytesSinceUpdate += bytesWritten; u64 time = osGetTime(); u64 elapsed = time - lastBytesPerSecondUpdate; if(elapsed >= 1000) { data->bytesPerSecond = (u32) (bytesSinceUpdate / (elapsed / 1000.0f)); if(ioStartTime != 0) { data->estimatedRemainingSeconds = (u32) ((data->currTotal - data->currProcessed) / (data->currProcessed / ((time - ioStartTime) / 1000.0f))); } else { data->estimatedRemainingSeconds = 0; } if(ioStartTime == 0 && data->currProcessed > 0) { ioStartTime = time; } bytesSinceUpdate = 0; lastBytesPerSecondUpdate = time; } } if(dstHandle != 0) { Result closeDstRes = data->closeDst(data->data, index, res == 0, dstHandle); if(R_SUCCEEDED(res)) { res = closeDstRes; } } free(buffer); } else { res = R_APP_OUT_OF_MEMORY; } } } Result closeSrcRes = data->closeSrc(data->data, index, res == 0, srcHandle); if(R_SUCCEEDED(res)) { res = closeSrcRes; } } } return res; }
static int lua_drawBMPV(lua_State *L){ int argc = lua_gettop(L); if ((argc != 4) && (argc != 5)) return luaL_error(L, "wrong number of arguments"); int x = luaL_checkint(L, 1); int y = luaL_checkint(L, 2); BMPV* src = (BMPV*)luaL_checkint(L, 3); u32 bytesRead; int screen = luaL_checkint(L, 4); int side = 0; if (argc == 5){ side = luaL_checkint(L,5); } if (src->isPlaying){ if (src->currentFrame >= src->tot_frame){ if (src->loop == 1){ src->currentFrame = 0; if (!GW_MODE){ src->moltiplier = 1; } src->tick = osGetTime(); }else{ src->isPlaying = false; src->moltiplier = 1; CSND_setchannel_playbackstate(src->ch1, 0); if (src->audiobuf2 != NULL) CSND_setchannel_playbackstate(src->ch2, 0); CSND_sharedmemtype0_cmdupdatestate(0); } if (src->audiobuf2 == NULL){ FSFILE_Read(src->sourceFile, &bytesRead, 28, src->audiobuf, src->mem_size); GSPGPU_FlushDataCache(NULL, src->audiobuf, src->mem_size); }else{ u8* tmp_buffer = (u8*)linearAlloc(src->mem_size); FSFILE_Read(src->sourceFile, &bytesRead, 28, tmp_buffer, src->mem_size); u32 size_tbp = src->mem_size; u32 off=0; u32 i=0; u16 z; while (i < size_tbp){ z=0; while (z < (src->bytepersample/2)){ src->audiobuf[off+z] = tmp_buffer[i+z]; src->audiobuf2[off+z] = tmp_buffer[i+z+(src->bytepersample/2)]; z++; } z=0; i=i+src->bytepersample; off=off+(src->bytepersample/2); } linearFree(tmp_buffer); GSPGPU_FlushDataCache(NULL, src->audiobuf, (src->mem_size)/2); GSPGPU_FlushDataCache(NULL, src->audiobuf2, (src->mem_size)/2); } }else{ if (((src->samplerate * src->bytepersample * ((osGetTime() - src->tick) / 1000)) > ((src->mem_size / 2) * src->moltiplier)) && (src->isPlaying)){ if ((src->moltiplier % 2) == 1){ //Update and flush first half-buffer if (src->audiobuf2 == NULL){ FSFILE_Read(src->sourceFile, &bytesRead, 28+(((src->mem_size)/2)*(src->moltiplier + 1)), src->audiobuf, (src->mem_size)/2); if (bytesRead != ((src->mem_size)/2)){ FSFILE_Read(src->sourceFile, &bytesRead, 28, src->audiobuf, (src->mem_size)/2); src->moltiplier = src->moltiplier + 1; } src->moltiplier = src->moltiplier + 1; GSPGPU_FlushDataCache(NULL, src->audiobuf, src->mem_size); }else{ u8* tmp_buffer = (u8*)linearAlloc((src->mem_size)/2); FSFILE_Read(src->sourceFile, &bytesRead, 28+(src->mem_size/2)*(src->moltiplier + 1), tmp_buffer, (src->mem_size)/2); if (bytesRead != ((src->mem_size)/2)){ FSFILE_Read(src->sourceFile, &bytesRead, 28, tmp_buffer, (src->mem_size)/2); src->moltiplier = src->moltiplier + 1; } src->moltiplier = src->moltiplier + 1; u32 size_tbp = (src->mem_size)/2; u32 off=0; u32 i=0; u16 z; while (i < size_tbp){ z=0; while (z < (src->bytepersample/2)){ src->audiobuf[off+z] = tmp_buffer[i+z]; src->audiobuf2[off+z] = tmp_buffer[i+z+(src->bytepersample/2)]; z++; } i=i+src->bytepersample; off=off+(src->bytepersample/2); } linearFree(tmp_buffer); GSPGPU_FlushDataCache(NULL, src->audiobuf, (src->mem_size)/2); GSPGPU_FlushDataCache(NULL, src->audiobuf2, (src->mem_size)/2); } }else{ u32 bytesRead; //Update and flush second half-buffer if (src->audiobuf2 == NULL){ FSFILE_Read(src->sourceFile, &bytesRead, 28+(((src->mem_size)/2)*(src->moltiplier + 1)), src->audiobuf+((src->mem_size)/2), (src->mem_size)/2); src->moltiplier = src->moltiplier + 1; GSPGPU_FlushDataCache(NULL, src->audiobuf, src->mem_size); }else{ u8* tmp_buffer = (u8*)linearAlloc((src->mem_size)/2); FSFILE_Read(src->sourceFile, &bytesRead, 28+(src->mem_size/2)*(src->moltiplier + 1), tmp_buffer, (src->mem_size)/2); src->moltiplier = src->moltiplier + 1; u32 size_tbp = (src->mem_size)/2; u32 off=0; u32 i=0; u16 z; while (i < size_tbp){ z=0; while (z < (src->bytepersample/2)){ src->audiobuf[(src->mem_size)/4+off+z] = tmp_buffer[i+z]; src->audiobuf2[(src->mem_size)/4+off+z] = tmp_buffer[i+z+(src->bytepersample/2)]; z++; } i=i+src->bytepersample; off=off+(src->bytepersample/2); } linearFree(tmp_buffer); GSPGPU_FlushDataCache(NULL, src->audiobuf, (src->mem_size)/2); GSPGPU_FlushDataCache(NULL, src->audiobuf2, (src->mem_size)/2); } } } src->currentFrame =((osGetTime() - src->tick) * src->framerate / 1000); Bitmap bitmap; bitmap.width = src->width; bitmap.height = src->height; bitmap.bitperpixel = 24; u32 frame_size = src->width * src->height * 3; u32 bytesRead; FSFILE_Read(src->sourceFile, &bytesRead, 28+src->audio_size+(src->currentFrame*frame_size), src->framebuf, frame_size); bitmap.pixels = src->framebuf; if (screen > 1) PrintImageBitmap(x,y,&bitmap,screen); else PrintScreenBitmap(x,y,&bitmap,screen,side); } }else{ if (src->tick != 0){ Bitmap bitmap; bitmap.width = src->width; bitmap.height = src->height; bitmap.bitperpixel = 24; u32 frame_size = src->width * src->height * 3; u32 bytesRead; FSFILE_Read(src->sourceFile, &bytesRead, 28+src->audio_size+(src->currentFrame*frame_size), src->framebuf, frame_size); bitmap.pixels = src->framebuf; if (screen > 1) PrintImageBitmap(x,y,&bitmap,screen); else PrintScreenBitmap(x,y,&bitmap,screen,side); } } return 0; }
int main() { sf2d_init(); sf2d_set_vblank_wait(0); // this will depend on the level uint8_t mapWidth = 4; uint8_t mapHeight = 3; uint16_t mapDim = mapWidth*mapHeight; uint8_t map_u[mapDim]; uint16_t mapLength = generateLevel(map_u, mapWidth, mapHeight, 12); uint8_t map[mapDim]; memcpy(map, map_u, mapDim); // get some drawing parameters uint16_t areaWidth = TWIDTH*2/3; uint16_t areaHeight = THEIGHT*2/3; uint16_t recWidth = areaWidth/mapWidth; uint16_t recHeight = areaHeight/mapHeight; uint16_t areaTop = (THEIGHT-mapHeight*recHeight)/2; uint16_t areaLeft = (TWIDTH-mapWidth*recWidth)/2; uint8_t curX = 0; uint8_t curY = 0; uint16_t playerLength = 0; float colorInterpolation = 0; u64 oldTime = 0; u64 keyTime = 0; while (aptMainLoop()) { // manage timer according to time passed since last frame u64 newTime = osGetTime(); keyTime += newTime-oldTime; oldTime = newTime; hidScanInput(); if (hidKeysDown() & KEY_START) break; // move cursor according to input uint16_t oldCurXY = curY*mapWidth+curX; curX += mapWidth; curY += mapHeight; if (hidKeysDown() & KEY_LEFT) curX--; if (hidKeysDown() & KEY_RIGHT) curX++; if (hidKeysDown() & KEY_UP) curY--; if (hidKeysDown() & KEY_DOWN) curY++; curX %= mapWidth; curY %= mapHeight; uint16_t newCurXY = curY*mapWidth+curX; if (newCurXY != oldCurXY) { map[newCurXY]--; playerLength++; keyTime=0; // force cursor display now } if (map[newCurXY] == 255) { // reset level curX = 0; curY = 0; playerLength = 0; memcpy(map, map_u, mapDim); } if (playerLength == mapLength) break; // TODO should be "you won" u32 targetColor = RGBA8(0x00,0xaa,0xaa,255); float targetInterpolation = (float)playerLength/(float)mapLength; if (newTime%256==0) colorInterpolation = (colorInterpolation*3+targetInterpolation*1)/4; u32 darkColor = interpolate(targetColor, greyed(targetColor), colorInterpolation); u32 liteColor = interpolate(darkColor, 0xffffffff, 1.0f/3.0f); // white is too clear u32 bgColor = interpolate(darkColor, 0, 0.5f); sf2d_set_clear_color(bgColor); sf2d_start_frame(GFX_TOP, GFX_LEFT); { // draw tiles for (uint8_t x=0; x<mapWidth; x++) for (uint8_t y=0; y<mapHeight; y++) { sf2d_draw_rectangle(x*recWidth+areaLeft, y*recHeight+areaTop, recWidth, recHeight, interpolate(darkColor, liteColor, ((float)map[y*mapWidth+x])/3)); } // draw cursor float t = (float)(keyTime%2048)/2048.0f; uint8_t op = pow(fabs(t-0.5f)*2,4)*255; sf2d_draw_rectangle(curX*recWidth+areaLeft, curY*recHeight+areaTop, recWidth, recHeight, RGBA8(0,0,0,op)); } sf2d_end_frame(); sf2d_swapbuffers(); } sf2d_fini(); return 0; }
int main() { gfxInitDefault(); gfxSet3D(true); // uncomment if using stereoscopic 3D gfxFlushBuffers(); transparent = BLACK; gfxSetScreenFormat(GFX_TOP, GSP_BGR8_OES); gfxSetScreenFormat(GFX_BOTTOM, GSP_BGR8_OES); InitParx(BLACK); /* ParxLeft = gfxGetFramebuffer(GFX_TOP, GFX_LEFT, NULL, NULL); ParxRight = gfxGetFramebuffer(GFX_TOP, GFX_RIGHT, NULL, NULL); ParxBot = gfxGetFramebuffer(GFX_BOTTOM, GFX_LEFT, NULL, NULL); ClrParx(ParxLeft, BLACK); ClrParx(ParxRight, BLACK); ClrParx(ParxBot, BLACK); */ char* str[256]; int l, k, j, i=20, posx = 100, posy = 100; TBGR rgbsam; TBGR rgb; u64 time; // Main loop while (aptMainLoop()) { // gspWaitForVBlank(); // ParxLeft = gfxGetFramebuffer(GFX_TOP, GFX_LEFT, NULL, NULL); // ParxRight = gfxGetFramebuffer(GFX_TOP, GFX_RIGHT, NULL, NULL); // ParxBot = gfxGetFramebuffer(GFX_BOTTOM, GFX_LEFT, NULL, NULL); hidScanInput(); u32 kDown = hidKeysHeld(); if (kDown & KEY_START) break; // break in order to return to hbmenu if (kDown & KEY_A) { CanvasString(ParxBot, CopyRight(), 10,10, LIGHT_GREEN); } if (kDown & KEY_B) { time= osGetTime(); rgb.r= 0xCC; rgb.g= 0x33; rgb.b= 0xCC; for (k=0;k<400;k++) for (l=0;l<240;l++) { SetPixL(k,l,rgb); //TopLCD SetPixR(k,l,rgb); if (k<320) SetPixB(k,l,rgb); //BotLCD } time = osGetTime() - time; sprintf(str, "%i:ms ParxPro SetPix L/R/B,kdl", time); CanvasString(ParxBot, str, 10,10, GREEN); } if (kDown & KEY_X) { TestPattern(); } if (kDown & KEY_Y) { InitParx(BLACK); // PasBotfill(ParxBot); rgb.r= 0xFF; rgb.g= 0x00; rgb.b= 0x8F; PasClrSrc(ParxBot, rgb); CanvasString(ParxBot, "InitParx", 10,10, GREEN); } if(kDown & KEY_CPAD_DOWN) { rgb.r= 0x00; rgb.g= 0x00; rgb.b= 0XFF; PasTopfill(ParxLeft, rgb); PasTopfill(ParxRight, rgb); } if(kDown & KEY_CPAD_UP) { rgb.r= 0xFF; rgb.g= 0x00; rgb.b= 0x00; PasTopfill(ParxLeft, rgb); PasTopfill(ParxRight, rgb); } if(kDown & KEY_CPAD_RIGHT) { rgb.r= 0x00; rgb.g= 0xFF; rgb.b= 0x00; time= osGetTime(); PasTopfill(ParxLeft, rgb); PasTopfill(ParxRight, rgb); time = osGetTime() - time; sprintf(str, "%i:ms L&R BGRTop,kdl", time); CanvasString(ParxBot, str, 10,10, GREEN); } if(kDown & KEY_CPAD_LEFT) { rgb.r= 0x00; rgb.g= 0x11; rgb.b= 0x00; time= osGetTime(); HexTopfill(ParxLeft); HexTopfill(ParxRight); time = osGetTime() - time; sprintf(str, "%i:ms L&R TopMapLED,kdl", time); CanvasString(ParxBot, str, 10,10, GREEN); } if(kDown & KEY_R) { InitBufSingle(BLACK); Topfill2; // ClrParx(ParxBot, BLACK); // sprintf(str, "Dergibal Rad:%i X:%i Y:%i", i, posx, posy); // CanvasString(ParxBot, str, 0, 0, RED); } if(kDown & KEY_L) { InitBufDub(BLACK); Topfill1; // ClrParx(ParxBot, BLACK); // sprintf(str, "Dergibal Rad:%i X:%i Y:%i", i, posx, posy); // CanvasString(ParxBot, str, 0, 0, RED); } if(kDown & KEY_DUP) { for (k=0;k<400;k++) for (l=0;l<240;l++) { if (k<320) PSetPixT(ParxRight,k,l, GetPixB(k,l)); } } if(kDown & KEY_DDOWN) { time= osGetTime(); rgb.r= 0xCC; rgb.g= 0x11; rgb.b= 0xCC; for (k=0;k<400;k++) for (l=0;l<240;l++) { PSetPixT(ParxRight,k,l, rgb); //TopLCD PSetPixT(ParxLeft,k,l, rgb); if (k<320) PSetPixB(ParxBot,k,l, rgb); //BotLCD } time = osGetTime() - time; sprintf(str, "%i:ms ParxPro,kdl", time); CanvasString(ParxBot, str, 10,10, GREEN); } if(kDown & KEY_DRIGHT) { ClrParx(ParxBot, BLACK); rgb.r= 0xEE; rgb.g= 0x00; rgb.b= 0xCC; time= osGetTime(); for (k=0;k<400;k++) for (l=0;l<240;l++) SetPixL(k,l,rgb); //TopLCD time = osGetTime() - time; sprintf(str, "Left %i:ms ParxPro,kdl", time); CanvasString(ParxBot, str, 10,10, LIGHT_GREEN); time= osGetTime(); for (k=0;k<400;k++) for (l=0;l<240;l++) SetPixR(k,l,rgb); time = osGetTime() - time; sprintf(str, "Right %i:ms ParxPro,kdl", time); CanvasString(ParxBot, str, 10,20, LIGHT_GREEN); } if(kDown & KEY_DLEFT) { // SetTopFramebuffers(0); time= osGetTime(); for (k=0;k<400;k++) for (l=0;l<240;l++) { SetPix(ParxRight,k,l,BLACK); SetPix(ParxLeft,k,l,BLACK); if (k<320) SetPix(ParxBot,k,l,BLACK); } time = osGetTime() - time; sprintf(str, "%i:ms Parx-GDI,kdl", time); CanvasString(ParxBot, str, 10,10, GREEN); } //gfxString(ParxRight, str, 30,30, 3, rgb); //gfxString(ParxLeft, str, 30,30, 3, rgb); //for (l=1;l<16;l++) print3d(rgb,10*l,10*l,l-1,3,str); //render rainbow // renderEffect(); //copy buffer to lower screen (don't have to do it every frame) // memcpy(gfxGetFramebuffer(GFX_BOTTOM, GFX_BOTTOM, NULL, NULL), buffer, size); //wait & swap // gfxSwapBuffersGpu(); // gspWaitForEvent(GSPGPU_EVENT_VBlank0, false); // Flush and swap framebuffers // gfxFlushBuffers(); // gfxSwapBuffers(); RefreshBuffer(); //Wait for VBlank gspWaitForVBlank(); } gfxExit(); return 0; }
static int lua_newT(lua_State *L) { int argc = lua_gettop(L); if (argc != 0) return luaL_error(L, "wrong number of arguments"); lua_pushnumber(L,osGetTime()); return 1; }
int main(void) { Result ret = 0, error = 0; sf2d_init(); sftd_init(); romfsInit(); srand(osGetTime()); // consoleInit(GFX_TOP, NULL); // TODO: Comment it! // consoleInit(GFX_BOTTOM, NULL); // TODO: Comment it! printf("> Loading texture manager\n"); PHBanku::texture = new TextureManager(); ret = PHBanku::texture->load(); if (R_FAILED(ret)) { // Graphics error |= ERR_GRAPHICS; } printf("> Loading font manager\n"); PHBanku::font = new FontManager(); ret = PHBanku::font->load(); if (R_FAILED(ret)) { // Font error |= ERR_FONT; } printf("> Loading data manager\n"); PHBanku::data = new DataManager(); ret = PHBanku::data->load(); if (R_FAILED(ret)) { // Data error |= ERR_DATA; } #ifdef __cia printf("> Starting title selector\n"); while (!error && TS_Loop()) { // Draw the static loading screen again because of ts.h PHBanku::texture->drawStaticLoadingScreen(); printf("> Loading filesystem services\n"); ret = FSCIA_Init(titleEntry.titleid, titleEntry.mediatype); if (R_FAILED(ret)) { // Filesystem error |= ERR_FILESYSTEM; } #else // __3dsx printf("> Loading filesystem services\n"); ret = FS_Init(); if (R_FAILED(ret)) { // Filesystem error |= ERR_FILESYSTEM; } #endif printf("> Loading save manager\n"); PHBanku::save = new SaveManager(); ret = PHBanku::save->load(); if (R_FAILED(ret)) { // Save error |= ERR_SAVE; } if (!error) { printf("> Starting box viewer...\n"); Viewer* viewer = new BoxViewer(); ViewState state = Viewer::startMainLoop(viewer); if (state == ViewState::Saving) { // TODO Remove when better save display! consoleInit(GFX_TOP, NULL); printf("Saving...\n"); // ^ PHBanku::save->save(); } else { // TODO Remove when better exit display! consoleInit(GFX_TOP, NULL); printf("Exiting...\n"); // ^ } delete viewer; } delete PHBanku::save; #ifdef __cia FSCIA_Exit(); consoleExit(GFX_TOP, NULL); break; // TODO Remove! The app crash itself after the 2nd ts, unknown cause. } // while (TS_LOOP()) if (!error) { // TODO Remove when better exit display! consoleInit(GFX_BOTTOM, NULL); // ^ printf("\nThe app execution ended!\n"); printf("Thanks for being awesome!\n"); } #else FS_Exit(); #endif if (error) { // TODO Remove when better error display! consoleInit(GFX_TOP, NULL); // ^ printf("\nProblem happened: 0x%lx\n", error); if (error & ERR_SAVE) printf(" \a Save\n"); if (error & ERR_DATA) printf(" \a Data\n"); if (error & ERR_FONT) printf(" \a Font\n"); if (error & ERR_GRAPHICS) printf(" \a Graphics\n"); if (error & ERR_FILESYSTEM) printf(" \a Filesystem\n"); printf("PHBank version: %08x\n", VERSION); printf("Can't start the viewer.\n"); printf("Press any key to exit\n"); waitKey(KEY_ANY); } delete PHBanku::data; delete PHBanku::font; delete PHBanku::texture; romfsExit(); sftd_fini(); sf2d_fini(); return 0; }
u64 ctr::core::time() { return osGetTime(); }