/*** Return the color of a pixel. @function :getPixel @tparam number x X position of the pixel @tparam number y Y position of the pixel @treturn number color of the pixel. */ static int texture_getPixel(lua_State *L) { texture_userdata *texture = luaL_checkudata(L, 1, "LTexture"); int x = luaL_checkinteger(L, 2); int y = luaL_checkinteger(L, 3); lua_pushinteger(L, sf2d_get_pixel(texture->texture, x, y)); return 1; }
int main() { cfguInit(); CFGU_GetSystemModel(&MODEL_3DS); FILE * file; shouldRenderDebug = true; if ((file = fopen("settings.bin", "r"))) { fread(&shouldRenderDebug,sizeof(bool),1,file); fread(&shouldSpeedup,sizeof(bool),1,file); osSetSpeedupEnable(shouldSpeedup); fclose(file); } sf2d_init(); csndInit(); noItem = newItem(ITEM_NULL, 0); currentMenu = MENU_TITLE; currentSelection = 0; quitGame = false; icons = sfil_load_PNG_buffer(icons2_png, SF2D_PLACE_RAM); font = sfil_load_PNG_buffer(Font_png, SF2D_PLACE_RAM); bottombg = sfil_load_PNG_buffer(bottombg_png, SF2D_PLACE_RAM); dirtColor[0] = SWAP_UINT32(sf2d_get_pixel(icons, 16, 0)); dirtColor[1] = SWAP_UINT32(sf2d_get_pixel(icons, 16, 1)); dirtColor[2] = SWAP_UINT32(sf2d_get_pixel(icons, 16, 2)); dirtColor[3] = SWAP_UINT32(sf2d_get_pixel(icons, 16, 3)); dirtColor[4] = SWAP_UINT32(sf2d_get_pixel(icons, 16, 4)); loadSound(&snd_playerHurt, "resources/playerhurt.raw"); loadSound(&snd_playerDeath, "resources/playerdeath.raw"); loadSound(&snd_monsterHurt, "resources/monsterhurt.raw"); loadSound(&snd_test, "resources/test.raw"); loadSound(&snd_pickup, "resources/pickup.raw"); loadSound(&snd_bossdeath, "resources/bossdeath.raw"); loadSound(&snd_craft, "resources/craft.raw"); bakeLights(); int i; for (i = 0; i < 5; ++i) { minimap[i] = sf2d_create_texture(128, 128, TEXFMT_RGBA8, SF2D_PLACE_RAM); sf2d_texture_tile32(minimap[i]); } sf2d_set_vblank_wait(true); sf2d_set_clear_color(0xFF000000); k_up.input = KEY_DUP | KEY_CPAD_UP | KEY_CSTICK_UP; k_down.input = KEY_DDOWN | KEY_CPAD_DOWN | KEY_CSTICK_DOWN; k_left.input = KEY_DLEFT | KEY_CPAD_LEFT | KEY_CSTICK_LEFT; k_right.input = KEY_DRIGHT | KEY_CPAD_RIGHT | KEY_CSTICK_RIGHT; k_attack.input = KEY_A | KEY_B | KEY_L | KEY_ZR; k_menu.input = KEY_X | KEY_Y | KEY_R | KEY_ZL; k_pause.input = KEY_START; k_accept.input = KEY_A; k_decline.input = KEY_B; k_delete.input = KEY_X; k_menuNext.input = KEY_R; k_menuPrev.input = KEY_L; if ((file = fopen("btnSave.bin", "rb"))) { fread(&k_up.input, sizeof(int), 1, file); fread(&k_down.input, sizeof(int), 1, file); fread(&k_left.input, sizeof(int), 1, file); fread(&k_right.input, sizeof(int), 1, file); fread(&k_attack.input, sizeof(int), 1, file); fread(&k_menu.input, sizeof(int), 1, file); fread(&k_pause.input, sizeof(int), 1, file); fread(&k_accept.input, sizeof(int), 1, file); fread(&k_decline.input, sizeof(int), 1, file); fread(&k_delete.input, sizeof(int), 1, file); fread(&k_menuNext.input, sizeof(int), 1, file); fread(&k_menuPrev.input, sizeof(int), 1, file); fclose(file); } if ((file = fopen("lastTP.bin", "r"))) { char fnbuf[256]; fgets(fnbuf, 256, file); // get directory to texturepack loadTexturePack(fnbuf); fclose(file); } tickCount = 0; initRecipes(); defineTables(); while (aptMainLoop()) { ++tickCount; hidScanInput(); tickKeys(hidKeysHeld(), hidKeysDown()); if (quitGame) break; if (initGame > 0) setupGame(initGame == 1 ? true : false); if (currentMenu == 0) { tick(); sf2d_start_frame(GFX_TOP, GFX_LEFT); offsetX = xscr; offsetY = yscr; sf2d_draw_rectangle(0, 0, 400, 240, 0xFF0C0C0C); //RGBA8(12, 12, 12, 255)); //You might think "real" black would be better, but it actually looks better that way renderLightsToStencil(); renderBackground(xscr, yscr); renderEntities(player.x, player.y, &eManager); renderPlayer(); resetStencilStuff(); offsetX = 0; offsetY = 0; if(shouldRenderDebug){ sprintf(fpsstr, " FPS: %.0f, X:%d, Y:%d, E:%d", sf2d_get_fps(), player.x, player.y, eManager.lastSlot[currentLevel]); drawText(fpsstr, 2, 225); } sf2d_end_frame(); sf2d_start_frame(GFX_BOTTOM, GFX_LEFT); if(!shouldRenderMap){ sf2d_draw_texture(bottombg, 0, 0); renderGui(); } else { renderZoomedMap(); } sf2d_end_frame(); } else { tickMenu(currentMenu); renderMenu(currentMenu, xscr, yscr); } sf2d_swapbuffers(); } freeRecipes(); freeLightBakes(); sf2d_free_texture(icons); sf2d_free_texture(minimap[0]); sf2d_free_texture(minimap[1]); sf2d_free_texture(minimap[2]); sf2d_free_texture(minimap[3]); sf2d_free_texture(minimap[4]); freeSounds(); csndExit(); cfguExit(); sf2d_fini(); return 0; }
/*** Save a texture to a file. @function :save @tparam string filename path to the file to save the texture to @tparam[opt=TYPE_PNG] number type type of the image to save. Can be TYPE_PNG or TYPE_BMP @treturn[1] boolean true on success @treturn[2] boolean `false` in case of error @treturn[2] string error message */ static int texture_save(lua_State *L) { texture_userdata *texture = luaL_checkudata(L, 1, "LTexture"); const char* path = luaL_checkstring(L, 2); u8 type = luaL_optinteger(L, 3, 0); int result = 0; if (type == 0) { // PNG FILE* file = fopen(path, "wb"); if (file == NULL) { lua_pushboolean(L, false); lua_pushstring(L, "Can open file"); return 2; } png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); png_infop infos = png_create_info_struct(png); setjmp(png_jmpbuf(png)); png_init_io(png, file); png_set_IHDR(png, infos, texture->texture->width, texture->texture->height, 8, PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); png_write_info(png, infos); png_bytep row = malloc(4 * texture->texture->width * sizeof(png_byte)); for(int y=0;y<texture->texture->height;y++) { for (int x=0;x<texture->texture->width;x++) { ((u32*)row)[x] = __builtin_bswap32(sf2d_get_pixel(texture->texture, x, y)); } png_write_row(png, row); } png_write_end(png, NULL); fclose(file); png_free_data(png, infos, PNG_FREE_ALL, -1); png_destroy_write_struct(&png, &infos); free(row); result = 1; } else if (type == 2) { // BMP u32* buff = malloc(texture->texture->width * texture->texture->height * 4); if (buff == NULL) { lua_pushboolean(L, false); lua_pushstring(L, "Failed to allocate buffer"); return 2; } for (int y=0;y<texture->texture->height;y++) { for (int x=0;x<texture->texture->width;x++) { buff[x+(y*texture->texture->width)] = __builtin_bswap32(sf2d_get_pixel(texture->texture, x, y)); } } result = stbi_write_bmp(path, texture->texture->width, texture->texture->height, 4, buff); free(buff); } else { lua_pushboolean(L, false); lua_pushstring(L, "Not a valid type"); return 2; } if (result == 0) { lua_pushboolean(L, false); lua_pushstring(L, "Failed to save the texture"); return 2; } lua_pushboolean(L, true); return 1; }