void initPlayer(player_s* p) { if(!p)return; initPhysicalPoint(&p->object, vect3Df(0,0,0), PLAYER_RADIUS); initCamera(&p->camera); md2InstanceInit(&p->gunInstance, &gunModel, &gunTextureOrange); md2InstanceInit(&p->ratmanInstance, &ratmanModel, &ratmanTexture); p->ratmanInstance.speed=0.1f; p->oldInPortal = p->inPortal = false; passthroughDvlb = DVLB_ParseFile((u32*)passthrough_vsh_shbin, passthrough_vsh_shbin_size); shaderProgramInit(&passthroughProgram); if(!passthroughDvlb)return; shaderProgramSetVsh(&passthroughProgram, &passthroughDvlb->DVLE[0]); rectangleVertexData = linearAlloc(sizeof(rectangleData)); memcpy(rectangleVertexData, rectangleData, sizeof(rectangleData)); crosshairVertexData = linearAlloc(sizeof(crosshairData)); memcpy(crosshairVertexData, crosshairData, sizeof(crosshairData)); p->flying = false; p->life = 80; p->walkCnt1 = 0; p->walkCnt2 = 0; }
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; }
void audio_init(int rate) { /* Clear sound context */ memset(&snd, 0, sizeof(t_snd)); /* Reset logging data */ snd.log = 0; snd.callback = NULL; /* Oops.. sound is disabled */ if(!rate) return; /* Calculate buffer size in samples */ snd.bufsize = 2048; /* Sound output */ #ifdef _3DS snd.buffer[0] = (signed short int *)linearAlloc(snd.bufsize * 2); snd.buffer[1] = (signed short int *)linearAlloc(snd.bufsize * 2); #else snd.buffer[0] = (signed short int *)malloc(snd.bufsize * 2); snd.buffer[1] = (signed short int *)malloc(snd.bufsize * 2); #endif if(!snd.buffer[0] || !snd.buffer[1]) return; memset(snd.buffer[0], 0, snd.bufsize * 2); memset(snd.buffer[1], 0, snd.bufsize * 2); /* YM2413 sound stream */ snd.fm_buffer = (signed short int *)malloc(snd.bufsize * 2); if(!snd.fm_buffer) return; memset(snd.fm_buffer, 0, snd.bufsize * 2); /* SN76489 sound stream */ snd.psg_buffer[0] = (signed short int *)malloc(snd.bufsize * 2); snd.psg_buffer[1] = (signed short int *)malloc(snd.bufsize * 2); if(!snd.psg_buffer[0] || !snd.psg_buffer[1]) return; memset(snd.psg_buffer[0], 0, snd.bufsize * 2); memset(snd.psg_buffer[1], 0, snd.bufsize * 2); /* Set up SN76489 emulation */ SN76496_init(0, MASTER_CLOCK, 255, rate); /* Set up YM2413 emulation */ OPLL_init(3579545, rate) ; opll = OPLL_new() ; OPLL_reset(opll) ; OPLL_reset_patch(opll,0) ; /* if use default voice data. */ /* Inform other functions that we can use sound */ snd.enabled = 1; }
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; }
void Audio_Init() { Result res; Audio_Type = 0; Audio_Buffer0 = (s16*)linearAlloc(MIXBUFSIZE*4*4); Audio_Buffer1 = &Audio_Buffer0[MIXBUFSIZE*4]; memset(Audio_Buffer0, 0, MIXBUFSIZE*4*4*sizeof(s16)); curbuffer = 0; Audio_Buffer = Audio_Buffer0; cursample = 0; curpos = 0; // try using CSND res = CSND_initialize(NULL); if (!res) { Audio_Type = 1; // TODO: figure out how to do panning, if it's possible at all? //CSND_playsound(8, 1, 1/*PCM16*/, 31994, (u32*)Audio_LeftBuffer, (u32*)Audio_LeftBuffer, MIXBUFSIZE*4, 2, 0); //CSND_playsound(9, 1, 1/*PCM16*/, 31994, (u32*)Audio_RightBuffer, (u32*)Audio_RightBuffer, MIXBUFSIZE*4, 2, 0); } // TODO: DSP black magic }
uint8_t* audio_load(uint8_t* buf, const char *audio) { FILE* file = fopen(audio, "rb"); off_t size = 0; if (file != NULL) { fseek(file, 0, SEEK_END); size = ftell(file); buf = linearAlloc(size); fseek(file, 0, SEEK_SET); fread(buf, 1, size, file); if (ferror(file)) { return NULL; } fclose(file); } uint8_t channel = 0x8; uint32_t sample_rate = 44100; uint32_t flags = SOUND_FORMAT_16BIT | SOUND_ONE_SHOT; //channel++; //channel%=8; GSPGPU_FlushDataCache(buf, size); csndPlaySound(channel, flags, sample_rate, 1.0, 0.0, buf, buf, size); return buf; }
void setClockrate(u8 setting) { int j, i; u32* patchArea = linearAlloc(0x00100000); if(setting != 0) setting = 3; // grab waitLoop stub GSPGPU_FlushDataCache(NULL, (u8*)patchArea, 0x100); gspwn(patchArea, (u32*)(MENU_LOADEDROP_BUFADR-0x100), 0x100); svcSleepThread(20*1000*1000); // patch it for(i=0; i<0x100/4; i++) { if(patchArea[i] == 0x67666E63) // "cnfg" { patchArea[i+1] = (patchArea[i+1] & ~0xFF) | setting; break; } } // copy it back GSPGPU_FlushDataCache(NULL, (u8*)patchArea, 0x100); gspwn((u32*)(MENU_LOADEDROP_BUFADR-0x100), patchArea, 0x100); svcSleepThread(20*1000*1000); // ghetto dcache invalidation // don't judge me for(j=0; j<4; j++) for(i=0; i<0x00100000/0x4; i+=0x4) patchArea[i+j]^=0xDEADBABE; linearFree(patchArea); }
void audio_load(const char *audio){ FILE *file = fopen(audio, "rb"); // seek to end of file fseek(file, 0, SEEK_END); // file pointer tells us the size off_t size = ftell(file); // seek back to start fseek(file, 0, SEEK_SET); //allocate a buffer buffer = linearAlloc(size); //read contents ! //close the file because we like being nice and tidy fclose(file); //memcpy(b, sfx_data, sfx_size); }
void load_texture(C3D_Tex *tex, const u8 *img, const u32 img_size) { unsigned int width, height; u8* image; lodepng_decode32(&image, &width, &height, img, img_size); u8 *gpusrc = (u8 *) linearAlloc(width * height * 4); // lodepng outputs big endian rgba so we need to convert convert_endianess(gpusrc, image, width * height); // ensure data is in physical ram GSPGPU_FlushDataCache(gpusrc, width * height * 4); // Load the texture and bind it to the first texture unit C3D_TexInit(tex, width, height, GPU_RGBA8); // Convert image to 3DS tiled texture format C3D_SyncDisplayTransfer((u32*)gpusrc, GX_BUFFER_DIM(width, height), (u32*) tex->data, GX_BUFFER_DIM(width, height), TEXTURE_TRANSFER_FLAGS); C3D_TexSetFilter(tex, GPU_LINEAR, GPU_NEAREST); free(image); linearFree(gpusrc); }
static int lua_stoprec_DSP(lua_State *L){ int argc = lua_gettop(L); #ifndef SKIP_ERROR_HANDLING if (argc != 0) return luaL_error(L, "wrong number of arguments"); #endif bool isSampling; MICU_IsSampling(&isSampling); u32 offset; if (isSampling){ offset = micGetLastSampleOffset(); MICU_StopSampling(); }else offset = micsize; u32 samplerate = sampleSize(srate); u32 mute_size = samplerate<<1; u8* bytebuf = (u8*)linearAlloc(offset); memcpy(bytebuf, (u8*)micbuf, offset); micExit(); free(micbuf); Music* songFile = (Music*)malloc(sizeof(Music)); songFile->audiobuf = bytebuf; songFile->audiobuf2 = NULL; songFile->big_endian = false; songFile->mem_size = 0; songFile->ch = 0xDEADBEEF; songFile->size = offset; songFile->samplerate = samplerate; songFile->encoding = CSND_ENCODING_PCM16; strcpy(songFile->author,""); strcpy(songFile->title,""); songFile->isPlaying = false; songFile->bytepersample = 2; songFile->magic = 0x4C534E44; lua_pushinteger(L,(u32)songFile); return 1; }
void Sprite::create(uint16 width, uint16 height, const Graphics::PixelFormat &f) { free(); actualWidth = width; actualHeight = height; format = f; w = MAX(nextHigher2(width), 64u); h = MAX(nextHigher2(height), 64u); pitch = w * format.bytesPerPixel; dirtyPixels = true; if (width && height) { pixels = linearAlloc(h * pitch); C3D_TexInit(&texture, w, h, GPU_RGBA8); C3D_TexSetFilter(&texture, GPU_LINEAR, GPU_LINEAR); assert(pixels && texture.data); clear(); } float x = 0.f, y = 0.f; float u = (float)width/w; float v = (float)height/h; vertex tmp[4] = { {{x, y, 0.5f}, {0, 0}}, {{x+width, y, 0.5f}, {u, 0}}, {{x, y+height, 0.5f}, {0, v}}, {{x+width, y+height, 0.5f}, {u, v}}, }; memcpy(vertices, tmp, sizeof(vertex) * 4); }
void AllocatorTest(void*args) { LinearAllocator linearAlloc(KILOBYTES(2), TaggedHeap::Instance().GetMemoryBlock("Gameplay")); //; TestStruct * te[20]; //= (TestShit*)linearAlloc.Allocate(sizeof(TestShit)); Job jArray[20]; for (int i = 0; i < 20; ++i) { te[i] = (TestStruct*)linearAlloc.Allocate(sizeof(TestStruct)); jArray[i].InitialiseJob(MoveTestShit, te[i], 0, "testing"); } JobCounter counter = JobScheduler::Instance().AddJob(20, jArray); JobScheduler::Instance().WaitForCounter(counter, 20); TestStruct finalResult = { 100, 100 }; for (int i = 0; i < 20; ++i) { ASSERT_EQ(finalResult, *te[i]); } linearAlloc.Free(); }
void initEmancipation(void) { int i; for(i=0;i<NUMEMANCIPATORS;i++) { emancipators[i].used=false; } for(i=0;i<NUMEMANCIPATIONGRIDS;i++) { emancipationGrids[i].used=false; } md2ReadModel(&gridModel, "grid.md2"); textureLoad(&gridTexture, "balllauncher.png", GPU_TEXTURE_MAG_FILTER(GPU_LINEAR)|GPU_TEXTURE_MIN_FILTER(GPU_LINEAR), 0); textureLoad(&gridSurfaceTexture, "grid.png", GPU_TEXTURE_MAG_FILTER(GPU_LINEAR)|GPU_TEXTURE_MIN_FILTER(GPU_LINEAR)|GPU_TEXTURE_WRAP_S(GPU_REPEAT)|GPU_TEXTURE_WRAP_T(GPU_REPEAT), 0); md2InstanceInit(&gridInstance, &gridModel, &gridTexture); emancipationRectangleVertexData = linearAlloc(sizeof(emancipationRectangleData)); memcpy(emancipationRectangleVertexData, emancipationRectangleData, sizeof(emancipationRectangleData)); emancipationDvlb = DVLB_ParseFile((u32*)emancipation_vsh_shbin, emancipation_vsh_shbin_size); if(!emancipationDvlb)return; shaderProgramInit(&emancipationProgram); shaderProgramSetVsh(&emancipationProgram, &emancipationDvlb->DVLE[0]); emancipationUniformTextureDimensions = shaderInstanceGetUniformLocation(emancipationProgram.vertexShader, "textureDimensions"); emancipationSFX=createSFX("emancipation.raw", SOUND_FORMAT_16BIT); }
void NES_LOADROMLIST() { Handle romHandle; FS_dirent dirStruct; FS_path dirPath = FS_makePath(PATH_CHAR, "/3DNES/ROMS"); // init SDMC archive sdmcArchive = (FS_archive){0x9, (FS_path){PATH_EMPTY, 1, (u8*)""}}; FSUSER_OpenArchive(NULL, &sdmcArchive); FSUSER_OpenDirectory(NULL, &romHandle, sdmcArchive, dirPath); // Get number of files in directory fileSystem.totalFiles = 0 while(1) { u32 dataRead = 0; FSDIR_Read(romHandle, &dataRead, 1, &dirStruct); if(dataRead == 0) break; fileSystem.totalFiles++; } fileSystem.fileList = linearAlloc(MAX_FILENAME_SIZE * fileSystem.totalFiles); FSUSER_OpenDirectory(NULL, &romHandle, sdmcArchive, dirPath); fileSystem.totalFiles = 0; while(1) { u32 dataRead = 0; FSDIR_Read(romHandle, &dataRead, 1, &dirStruct); if(dataRead == 0) break; unicodeToChar(&fileSystem.fileList[MAX_FILENAME_SIZE * fileSystem.totalFiles), dirStruct.name); fileSystem.totalFiles++; } FSDIR_Close(romHandle); }
void play_file_from_filename(const std::string name) { currently_playing = std::string(name); if (audiobuf) linearFree(audiobuf); if (name.rfind(".flac") != std::string::npos) { if (!FLAC_decoder) { FLAC_decoder = FLAC__stream_decoder_new(); FLAC__stream_decoder_set_md5_checking(FLAC_decoder, true); } audiobuf_index = 0; decode_mode = AUDIO_MODE_FLAC; FLAC__StreamDecoderInitStatus init_status = FLAC__stream_decoder_init_file(FLAC_decoder, name.c_str(), FLAC_write_callback, FLAC_metadata_callback, FLAC_error_callback, NULL); if(init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK) { printf("ERROR: initializing decoder: %s\n", FLAC__StreamDecoderInitStatusString[init_status]); } FLAC__stream_decoder_process_until_end_of_metadata(FLAC_decoder); } else { decode_mode = AUDIO_MODE_VORBIS; v = stb_vorbis_open_filename(name.c_str(), &error, NULL); info = stb_vorbis_get_info(v); Samples = info.sample_rate; audiobuf_size = Samples * sizeof(s16) * 2; audiobuf = (s16*)linearAlloc(audiobuf_size); } paused = false; }
void TextEngine::Initialize(gfxScreen_t scn) { //The two screens are different sizes if(scn == GFX_TOP) { target = C3D_RenderTargetCreate(240, 400, GPU_RB_RGBA8, GPU_RB_DEPTH24_STENCIL8); } else { target = C3D_RenderTargetCreate(240, 320, GPU_RB_RGBA8, GPU_RB_DEPTH24_STENCIL8); } C3D_RenderTargetSetClear(target, C3D_CLEAR_ALL, CLEAR_COLOR, 0); C3D_RenderTargetSetOutput(target, scn, GFX_LEFT, DISPLAY_TRANSFER_FLAGS); Result res = fontEnsureMapped(); if (R_FAILED(res)) printf("fontEnsureMapped: %08lX\n", res); vshader_dvlb = DVLB_ParseFile((u32*)vshader_v_shbin, vshader_v_shbin_size); shaderProgramInit(&program); shaderProgramSetVsh(&program, &vshader_dvlb->DVLE[0]); C3D_BindProgram(&program); // Get the location of the uniforms uLoc_projection = shaderInstanceGetUniformLocation(program.vertexShader, "projection"); // Configure attributes for use with the vertex shader C3D_AttrInfo* attrInfo = C3D_GetAttrInfo(); AttrInfo_Init(attrInfo); AttrInfo_AddLoader(attrInfo, 0, GPU_FLOAT, 3); // v0=position AttrInfo_AddLoader(attrInfo, 1, GPU_FLOAT, 2); // v1=texcoord if(scn == GFX_TOP) { // Compute the projection matrix Mtx_OrthoTilt(&projection, 0.0, 400.0, 240.0, 0.0, 0.0, 1.0); } else { Mtx_OrthoTilt(&projection, 0.0, 320.0, 240.0, 0.0, 0.0, 1.0); } // Configure depth test to overwrite pixels with the same depth (needed to draw overlapping glyphs) C3D_DepthTest(true, GPU_GEQUAL, GPU_WRITE_ALL); // Load the glyph texture sheets int i; TGLP_s* glyphInfo = fontGetGlyphInfo(); glyphSheets = malloc(sizeof(C3D_Tex)*glyphInfo->nSheets); for (i = 0; i < glyphInfo->nSheets; i ++) { C3D_Tex* tex = &glyphSheets[i]; tex->data = fontGetGlyphSheetTex(i); tex->fmt = glyphInfo->sheetFmt; tex->size = glyphInfo->sheetSize; tex->width = glyphInfo->sheetWidth; tex->height = glyphInfo->sheetHeight; tex->param = GPU_TEXTURE_MAG_FILTER(GPU_LINEAR) | GPU_TEXTURE_MIN_FILTER(GPU_LINEAR) | GPU_TEXTURE_WRAP_S(GPU_CLAMP_TO_EDGE) | GPU_TEXTURE_WRAP_T(GPU_CLAMP_TO_EDGE); } // Create the text vertex array textVtxArray = (textVertex_s*)linearAlloc(sizeof(textVertex_s)*TEXT_VTX_ARRAY_COUNT); }
Result DownloadFile_Internal(const char *url, void *out, bool bProgress, void (*write)(void* out, unsigned char* buffer, u32 readSize)) { httpcContext context; u32 fileSize = 0; u32 procSize = 0; Result ret = 0; Result dlret = HTTPC_RESULTCODE_DOWNLOADPENDING; u32 status; u32 bufSize = 0x100000; u32 readSize = 0; httpcOpenContext(&context, HTTPC_METHOD_GET, (char*)url, 1); ret = httpcBeginRequest(&context); if (ret != 0) goto _out; ret = httpcGetResponseStatusCode(&context, &status, 0); if (ret != 0) goto _out; if (status != 200) { ret = status; goto _out; } ret = httpcGetDownloadSizeState(&context, NULL, &fileSize); if (ret != 0) goto _out; { unsigned char *buffer = (unsigned char *)linearAlloc(bufSize); if (buffer == NULL) { printf("Error allocating download buffer\n"); ret = -1; goto _out; } while (dlret == (s32)HTTPC_RESULTCODE_DOWNLOADPENDING) { memset(buffer, 0, bufSize); dlret = httpcDownloadData(&context, buffer, bufSize, &readSize); write(out, buffer, readSize); procSize += readSize; if (bProgress) { PrintProgress(fileSize, procSize); } } linearFree(buffer); } _out: httpcCloseContext(&context); return ret; }
void gpuInit(void) { colorBuf = vramAlloc(400*240*4); depthBuf = vramAlloc(400*240*4); cmdBuf = linearAlloc(0x40000*4); GPU_Init(NULL); GPU_Reset(NULL, cmdBuf, 0x40000); }
void* linearCalloc(size_t num, size_t size) { char* res = (char*)linearAlloc(size*num); size_t i; for(i=0;i<size*num;i++){ res[i] = 0; } return (void*)res; }
void ctr_rend_buffer_init() { ctr_state.buffer = ctr_buffer = linearAlloc(CTR_REND_BUFFER_MAX * 2); if (ctr_state.buffer == 0) { printf("-------------\n\nctr_rend_buffer_copy_stride Out of Memory\n\n-------------\n"); while (1); } ctr_state.buffer_len = CTR_REND_BUFFER_MAX; ctr_state.buffer_pos = 0; ctr_buffer_frame = 0; }
static int lua_alloc2(lua_State *L){ int argc = lua_gettop(L); #ifndef SKIP_ERROR_HANDLING if(argc != 1) return luaL_error(L, "wrong number of arguments."); #endif u32 size = (u32)luaL_checkinteger(L, 1); u8* memblock = (u8*)linearAlloc(size); lua_pushinteger(L, (u32)memblock); return 1; }
void FLAC_metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data) { /* print some stats */ // if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) { /* save for later */ // total_samples = metadata->data.stream_info.total_samples; Samples = metadata->data.stream_info.sample_rate; audiobuf_size = Samples * sizeof(s16) * 2; audiobuf = (s16*)linearAlloc(audiobuf_size); // } }
void audio_load(const char *audio) { FILE *file = fopen(audio, "rb"); fseek(file, 0, SEEK_END); off_t size = ftell(file); fseek(file, 0, SEEK_SET); buffer = linearAlloc(size); off_t bytesRead = fread(buffer, 1, size, file); fclose(file); csndPlaySound(8, SOUND_FORMAT_16BIT | SOUND_REPEAT, 48000, 1, 0, buffer, buffer, size); linearFree(buffer); }
void generatePortalGeometry(portalVertex_s** v1, int* n1, portalVertex_s** v2, int* n2) { if(!v1 || !v2 || !n1 || !n2)return; *v1 = linearAlloc(sizeof(portalVertex_s)*(PORTAL_DETAIL+1)*2); *v2 = linearAlloc(sizeof(portalVertex_s)*(PORTAL_DETAIL+1)*2); if(!*v1 || !*v2)return; *n1 = 0; *n2 = 0; int i; for(i=0; i<PORTAL_DETAIL+1; i++) { (*v1)[(*n1)++] = (portalVertex_s){(vect3Df_s){0.0f, 0.0f, 0.0f}}; (*v1)[(*n1)++] = (portalVertex_s){(vect3Df_s){cos((i*2*M_PI)/PORTAL_DETAIL)*PORTAL_WIDTH, sin((i*2*M_PI)/PORTAL_DETAIL)*PORTAL_HEIGHT, 0.0f}}; (*v2)[(*n2)++] = (portalVertex_s){(vect3Df_s){cos((i*2*M_PI)/PORTAL_DETAIL)*PORTAL_WIDTH, sin((i*2*M_PI)/PORTAL_DETAIL)*PORTAL_HEIGHT, 0.0f}}; (*v2)[(*n2)++] = (portalVertex_s){(vect3Df_s){cos((i*2*M_PI)/PORTAL_DETAIL)*(PORTAL_WIDTH+PORTAL_OUTLINE), sin((i*2*M_PI)/PORTAL_DETAIL)*(PORTAL_HEIGHT+PORTAL_OUTLINE), 0.0f}}; } }
int pm_3ds_sound_init(void) { if(csndInit()) return 0; stream = (u8*)linearAlloc(SOUND_BUFFER_SIZE); if (!stream) { printf("ERROR : Couldn't malloc stream\n"); return 0; } bufferpos=0; return 1; }
void sf2d_fill_texture_from_RGBA8(sf2d_texture *dst, const void *rgba8, int source_w, int source_h) { // TODO: add support for non-RGBA8 textures u8 *tmp = linearAlloc(dst->pow2_w * dst->pow2_h * 4); int i, j; for (i = 0; i < source_h; i++) { for (j = 0; j < source_w; j++) { ((u32 *)tmp)[i*dst->pow2_w + j] = ((u32 *)rgba8)[i*source_w + j]; } } memcpy(dst->data, tmp, dst->pow2_w*dst->pow2_h*4); linearFree(tmp); }
Sprite::Sprite() : dirtyPixels(true) , dirtyMatrix(true) , actualWidth(0) , actualHeight(0) , posX(0) , posY(0) , scaleX(1.f) , scaleY(1.f) { Mtx_Identity(&modelview); vertices = (vertex *)linearAlloc(sizeof(vertex) * 4); }
void gfxLoadBorder(u8* imgData, int imgWidth, int imgHeight) { if(imgData == NULL || (borderInit && (borderWidth != imgWidth || borderHeight != imgHeight))) { if(borderInit) { C3D_TexDelete(&borderTexture); borderInit = false; } borderWidth = 0; borderHeight = 0; gpuBorderWidth = 0; gpuBorderHeight = 0; if(imgData == NULL) { return; } } // Adjust the texture to power-of-two dimensions. borderWidth = imgWidth; borderHeight = imgHeight; gpuBorderWidth = (int) pow(2, ceil(log(borderWidth) / log(2))); gpuBorderHeight = (int) pow(2, ceil(log(borderHeight) / log(2))); // Create the texture. if(!borderInit && !C3D_TexInit(&borderTexture, gpuBorderWidth, gpuBorderHeight, GPU_RGBA8)) { return; } C3D_TexSetFilter(&borderTexture, GPU_LINEAR, GPU_LINEAR); // Copy the texture to a power-of-two sized buffer. u32* imgBuffer = (u32*) imgData; u32* temp = (u32*) linearAlloc(gpuBorderWidth * gpuBorderHeight * sizeof(u32)); for(int x = 0; x < borderWidth; x++) { for(int y = 0; y < borderHeight; y++) { temp[y * gpuBorderWidth + x] = imgBuffer[y * borderWidth + x]; } } GSPGPU_FlushDataCache(temp, gpuBorderWidth * gpuBorderHeight * sizeof(u32)); if(R_SUCCEEDED(GX_DisplayTransfer(temp, (u32) GX_BUFFER_DIM(gpuBorderWidth, gpuBorderHeight), (u32*) borderTexture.data, (u32) GX_BUFFER_DIM(gpuBorderWidth, gpuBorderHeight), GX_TRANSFER_FLIP_VERT(1) | GX_TRANSFER_OUT_TILED(1) | GX_TRANSFER_RAW_COPY(0) | GX_TRANSFER_IN_FORMAT(GX_TRANSFER_FMT_RGBA8) | GX_TRANSFER_OUT_FORMAT(GX_TRANSFER_FMT_RGBA8) | GX_TRANSFER_SCALING(GX_TRANSFER_SCALE_NO)))) { gspWaitForPPF(); } linearFree(temp); GSPGPU_InvalidateDataCache(borderTexture.data, borderTexture.size); borderInit = true; }
static void audioThreadFunc(void *arg) { Audio::MixerImpl *mixer = (Audio::MixerImpl *)arg; OSystem_3DS *osys = (OSystem_3DS *)g_system; int i; const int channel = 0; int bufferIndex = 0; const int bufferCount = 3; const int bufferSize = 80000; // Can't be too small, based on delayMillis duration const int sampleRate = mixer->getOutputRate(); int sampleLen = 0; uint32 lastTime = osys->getMillis(true); uint32 time = lastTime; ndspWaveBuf buffers[bufferCount]; for (i = 0; i < bufferCount; ++i) { memset(&buffers[i], 0, sizeof(ndspWaveBuf)); buffers[i].data_vaddr = linearAlloc(bufferSize); buffers[i].looping = false; buffers[i].status = NDSP_WBUF_FREE; } ndspChnReset(channel); ndspChnSetInterp(channel, NDSP_INTERP_LINEAR); ndspChnSetRate(channel, sampleRate); ndspChnSetFormat(channel, NDSP_FORMAT_STEREO_PCM16); while (!osys->exiting) { osys->delayMillis(100); // Note: Increasing the delay requires a bigger buffer time = osys->getMillis(true); sampleLen = (time - lastTime) * 22 * 4; // sampleRate / 1000 * channelCount * sizeof(int16); lastTime = time; if (!osys->sleeping && sampleLen > 0) { bufferIndex++; bufferIndex %= bufferCount; ndspWaveBuf *buf = &buffers[bufferIndex]; buf->nsamples = mixer->mixCallback(buf->data_adpcm, sampleLen); if (buf->nsamples > 0) { DSP_FlushDataCache(buf->data_vaddr, bufferSize); ndspChnWaveBufAdd(channel, buf); } } } for (i = 0; i < bufferCount; ++i) linearFree(buffers[i].data_pcm8); }
int main() { gfxInitDefault(); //gfxSet3D(true); // uncomment if using stereoscopic 3D memset(logstring, 0, 256); inaddr = linearAlloc(0x100000); outaddr = linearAlloc(0x100000); if(inaddr && outaddr) { memset(inaddr, 0, 0x100000); memset(outaddr, 0, 0x100000); draw_startup(); } // Main loop while (aptMainLoop()) { gspWaitForVBlank(); hidScanInput(); // Your code goes here u32 kDown = hidKeysDown(); if (kDown & KEY_START) break; // break in order to return to hbmenu } if(inaddr)linearFree(inaddr); if(outaddr)linearFree(outaddr); gfxExit(); return 0; }