|
| 1 | +#include "applications/poketch/backlight_toggle/graphics.h" |
| 2 | + |
| 3 | +#include <nitro.h> |
| 4 | + |
| 5 | +#include "generated/sdat.h" |
| 6 | + |
| 7 | +#include "applications/poketch/poketch_animation.h" |
| 8 | +#include "applications/poketch/poketch_graphics.h" |
| 9 | +#include "applications/poketch/poketch_system.h" |
| 10 | +#include "applications/poketch/poketch_task.h" |
| 11 | + |
| 12 | +#include "bg_window.h" |
| 13 | +#include "graphics.h" |
| 14 | +#include "heap.h" |
| 15 | +#include "sys_task_manager.h" |
| 16 | + |
| 17 | +static void SetupSprites(BacklightToggleGraphics *graphics, const BacklightData *backlight); |
| 18 | +static void UnloadSprites(BacklightToggleGraphics *graphics); |
| 19 | +static void EndTask(PoketchTaskManager *taskMan); |
| 20 | +static void Task_DrawBackground(SysTask *task, void *taskMan); |
| 21 | +static void Task_FreeBackground(SysTask *task, void *taskMan); |
| 22 | +static void Task_UpdateSwitch(SysTask *task, void *taskMan); |
| 23 | + |
| 24 | +BOOL PoketchBacklightToggleGraphics_New(BacklightToggleGraphics **dest, const BacklightData *backlight, BgConfig *bgConfig) |
| 25 | +{ |
| 26 | + BacklightToggleGraphics *graphics = Heap_Alloc(HEAP_ID_POKETCH_APP, sizeof(BacklightToggleGraphics)); |
| 27 | + |
| 28 | + if (graphics != NULL) { |
| 29 | + PoketchTask_InitActiveTaskList(graphics->activeTasks, BACKLIGHT_TOGGLE_TASK_SLOTS); |
| 30 | + graphics->backlight = backlight; |
| 31 | + graphics->bgConfig = PoketchGraphics_GetBgConfig(); |
| 32 | + graphics->animMan = PoketchGraphics_GetAnimationManager(); |
| 33 | + |
| 34 | + SetupSprites(graphics, backlight); |
| 35 | + *dest = graphics; |
| 36 | + |
| 37 | + return TRUE; |
| 38 | + } |
| 39 | + |
| 40 | + return FALSE; |
| 41 | +} |
| 42 | + |
| 43 | +void PoketchBacklightToggleGraphics_Free(BacklightToggleGraphics *graphics) |
| 44 | +{ |
| 45 | + if (graphics != NULL) { |
| 46 | + Heap_Free(graphics); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +static void SetupSprites(BacklightToggleGraphics *graphics, const BacklightData *backlight) |
| 51 | +{ |
| 52 | + static const PoketchAnimation_AnimationData animData = { |
| 53 | + .translation = { FX32_CONST(112), FX32_CONST(104) }, |
| 54 | + .animIdx = 0, |
| 55 | + .flip = NNS_G2D_RENDERERFLIP_NONE, |
| 56 | + .oamPriority = 2, |
| 57 | + .priority = 0, |
| 58 | + .hasAffineTransform = FALSE, |
| 59 | + }; |
| 60 | + |
| 61 | + Graphics_LoadObjectTiles(NARC_INDEX_GRAPHIC__POKETCH, 104, DS_SCREEN_SUB, 0, 0, TRUE, HEAP_ID_POKETCH_APP); |
| 62 | + PoketchAnimation_LoadSpriteFromNARC(&graphics->sprite, NARC_INDEX_GRAPHIC__POKETCH, 102, 103, HEAP_ID_POKETCH_APP); |
| 63 | + |
| 64 | + graphics->animation = PoketchAnimation_SetupNewAnimatedSprite(graphics->animMan, &animData, &graphics->sprite); |
| 65 | + |
| 66 | + if (!backlight->isOn) { |
| 67 | + PoketchAnimation_UpdateAnimationIdx(graphics->animation, 1); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +static void UnloadSprites(BacklightToggleGraphics *graphics) |
| 72 | +{ |
| 73 | + PoketchAnimation_RemoveAnimatedSprite(graphics->animMan, graphics->animation); |
| 74 | + PoketchAnimation_FreeSpriteData(&graphics->sprite); |
| 75 | +} |
| 76 | + |
| 77 | +static const PoketchTask sBacklightToggleGraphicsTasks[] = { |
| 78 | + { BACKLIGHT_TOGGLE_GRAPHICS_INIT, Task_DrawBackground, 0 }, |
| 79 | + { BACKLIGHT_TOGGLE_GRAPHICS_FREE, Task_FreeBackground, 0 }, |
| 80 | + { BACKLIGHT_TOGGLE_GRAPHICS_UPDATE_SWITCH, Task_UpdateSwitch, 0 }, |
| 81 | + { 0 } |
| 82 | +}; |
| 83 | + |
| 84 | +void PoketchBacklightToggleGraphics_StartTask(BacklightToggleGraphics *graphics, enum BacklightToggleGraphicsTask taskID) |
| 85 | +{ |
| 86 | + PoketchTask_Start(sBacklightToggleGraphicsTasks, taskID, graphics, graphics->backlight, graphics->activeTasks, 2, HEAP_ID_POKETCH_APP); |
| 87 | +} |
| 88 | + |
| 89 | +BOOL PoketchBacklighToggleGraphics_TaskIsNotActive(BacklightToggleGraphics *graphics, enum BacklightToggleGraphicsTask taskID) |
| 90 | +{ |
| 91 | + return PoketchTask_TaskIsNotActive(graphics->activeTasks, taskID); |
| 92 | +} |
| 93 | + |
| 94 | +BOOL PoketchBacklightToggle_NoActiveTasks(BacklightToggleGraphics *graphics) |
| 95 | +{ |
| 96 | + return PoketchTask_NoActiveTasks(graphics->activeTasks); |
| 97 | +} |
| 98 | + |
| 99 | +static void EndTask(PoketchTaskManager *taskMan) |
| 100 | +{ |
| 101 | + BacklightToggleGraphics *graphics = PoketchTask_GetTaskData(taskMan); |
| 102 | + |
| 103 | + PoketchTask_EndTask(graphics->activeTasks, taskMan); |
| 104 | +} |
| 105 | + |
| 106 | +static void Task_DrawBackground(SysTask *task, void *taskMan) |
| 107 | +{ |
| 108 | + static const BgTemplate bgTemplate = { |
| 109 | + .x = 0, |
| 110 | + .y = 0, |
| 111 | + .bufferSize = 0x800, |
| 112 | + .baseTile = 0, |
| 113 | + .screenSize = BG_SCREEN_SIZE_256x256, |
| 114 | + .colorMode = GX_BG_COLORMODE_16, |
| 115 | + .screenBase = GX_BG_SCRBASE_0x7000, |
| 116 | + .charBase = GX_BG_CHARBASE_0x00000, |
| 117 | + .bgExtPltt = GX_BG_EXTPLTT_01, |
| 118 | + .priority = 2, |
| 119 | + .areaOver = 0, |
| 120 | + .mosaic = FALSE, |
| 121 | + }; |
| 122 | + BacklightToggleGraphics *graphics = PoketchTask_GetTaskData(taskMan); |
| 123 | + |
| 124 | + Bg_InitFromTemplate(graphics->bgConfig, BG_LAYER_SUB_2, &bgTemplate, BG_TYPE_STATIC); |
| 125 | + Graphics_LoadTilesToBgLayer(NARC_INDEX_GRAPHIC__POKETCH, 101, graphics->bgConfig, BG_LAYER_SUB_2, 0, 0, TRUE, HEAP_ID_POKETCH_APP); |
| 126 | + Graphics_LoadTilemapToBgLayer(NARC_INDEX_GRAPHIC__POKETCH, 100, graphics->bgConfig, BG_LAYER_SUB_2, 0, 0, TRUE, HEAP_ID_POKETCH_APP); |
| 127 | + |
| 128 | + PoketchGraphics_LoadActivePalette(0, 0); |
| 129 | + Bg_CopyTilemapBufferToVRAM(graphics->bgConfig, BG_LAYER_SUB_2); |
| 130 | + |
| 131 | + GXSDispCnt dispCnt = GXS_GetDispCnt(); |
| 132 | + GXS_SetVisiblePlane(dispCnt.visiblePlane | GX_PLANEMASK_BG2); |
| 133 | + |
| 134 | + EndTask(taskMan); |
| 135 | +} |
| 136 | + |
| 137 | +static void Task_FreeBackground(SysTask *task, void *taskMan) |
| 138 | +{ |
| 139 | + BacklightToggleGraphics *graphics = PoketchTask_GetTaskData(taskMan); |
| 140 | + |
| 141 | + UnloadSprites(graphics); |
| 142 | + Bg_FreeTilemapBuffer(graphics->bgConfig, BG_LAYER_SUB_2); |
| 143 | + EndTask(taskMan); |
| 144 | +} |
| 145 | + |
| 146 | +static void Task_UpdateSwitch(SysTask *task, void *taskMan) |
| 147 | +{ |
| 148 | + BacklightToggleGraphics *graphics = PoketchTask_GetTaskData(taskMan); |
| 149 | + const BacklightData *backlight = PoketchTask_GetConstTaskData(taskMan); |
| 150 | + |
| 151 | + PoketchSystem_PlaySoundEffect(SEQ_SE_DP_POKETCH_003); |
| 152 | + |
| 153 | + if (backlight->isOn) { |
| 154 | + PoketchAnimation_UpdateAnimationIdx(graphics->animation, 0); |
| 155 | + PM_SetBackLight(PM_LCD_BOTTOM, PM_BACKLIGHT_ON); |
| 156 | + } else { |
| 157 | + PoketchAnimation_UpdateAnimationIdx(graphics->animation, 1); |
| 158 | + PM_SetBackLight(PM_LCD_BOTTOM, PM_BACKLIGHT_OFF); |
| 159 | + } |
| 160 | + |
| 161 | + EndTask(taskMan); |
| 162 | +} |
0 commit comments