|
| 1 | +/* |
| 2 | + * This example code reports power status (plugged in, battery level, etc). |
| 3 | + * |
| 4 | + * This code is public domain. Feel free to use it for any purpose! |
| 5 | + */ |
| 6 | + |
| 7 | +#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */ |
| 8 | +#include <SDL3/SDL.h> |
| 9 | +#include <SDL3/SDL_main.h> |
| 10 | + |
| 11 | +#include "../../save-rendering-to-bitmaps.h" |
| 12 | + |
| 13 | +/* We will use this renderer to draw into this window every frame. */ |
| 14 | +static SDL_Window *window = NULL; |
| 15 | +static SDL_Renderer *renderer = NULL; |
| 16 | + |
| 17 | +/* This function runs once at startup. */ |
| 18 | +SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) |
| 19 | +{ |
| 20 | + SDL_SetAppMetadata("Example Misc Power", "1.0", "com.example.misc-power"); |
| 21 | + |
| 22 | + if (!SDL_Init(SDL_INIT_VIDEO)) { |
| 23 | + SDL_Log("Couldn't initialize SDL: %s", SDL_GetError()); |
| 24 | + return SDL_APP_FAILURE; |
| 25 | + } |
| 26 | + |
| 27 | + if (!SDL_CreateWindowAndRenderer("examples/misc/power", 640, 480, 0, &window, &renderer)) { |
| 28 | + SDL_Log("Couldn't create window/renderer: %s", SDL_GetError()); |
| 29 | + return SDL_APP_FAILURE; |
| 30 | + } |
| 31 | + |
| 32 | + return SDL_APP_CONTINUE; /* carry on with the program! */ |
| 33 | +} |
| 34 | + |
| 35 | +/* This function runs when a new event (mouse input, keypresses, etc) occurs. */ |
| 36 | +SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) |
| 37 | +{ |
| 38 | + if (event->type == SDL_EVENT_QUIT) { |
| 39 | + return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */ |
| 40 | + } |
| 41 | + return SDL_APP_CONTINUE; /* carry on with the program! */ |
| 42 | +} |
| 43 | + |
| 44 | +/* This function runs once per frame, and is the heart of the program. */ |
| 45 | +SDL_AppResult SDL_AppIterate(void *appstate) |
| 46 | +{ |
| 47 | + const SDL_FRect frame = { 100, 200, 440, 80 }; /* the percentage bar dimensions. */ |
| 48 | + |
| 49 | +static Uint64 start = 0; |
| 50 | +Uint64 now = SDL_GetTicks(); |
| 51 | +if (!start) { |
| 52 | + start = now; |
| 53 | +} |
| 54 | +now -= start; |
| 55 | + |
| 56 | + /* Query for battery info */ |
| 57 | + int seconds = 0, percent = 0; |
| 58 | + //const SDL_PowerState state = SDL_GetPowerInfo(&seconds, &percent); |
| 59 | + |
| 60 | +SDL_PowerState state; |
| 61 | +if (now < 500) { |
| 62 | + state = SDL_POWERSTATE_ON_BATTERY; |
| 63 | + seconds = 8088 + ((500 / 1000) * 300); |
| 64 | + percent = ((int) ((500 / 1500.0f) * 100.0f)); |
| 65 | +} else { |
| 66 | + state = (now < 1500) ? SDL_POWERSTATE_CHARGING : SDL_POWERSTATE_CHARGED; |
| 67 | + seconds = 8088 + ((int) ((SDL_min(now, 1500) / 1500.0) * 10000)); |
| 68 | + percent = (now < 1500) ? ((int) ((now / 1500.0f) * 100.0f)) : 100; |
| 69 | +} |
| 70 | + |
| 71 | + /* We set up different drawing details for each power state, then |
| 72 | + run it all through the same drawing code. */ |
| 73 | + int clearr = 0, clearg = 0, clearb = 0; /* clear window to this color. */ |
| 74 | + int textr = 255, textg = 255, textb = 255; /* draw messages in this color. */ |
| 75 | + int framer = 255, frameg = 255, frameb = 255; /* draw a percentage bar frame in this color. */ |
| 76 | + int barr = 0, barg = 0, barb = 0; /* draw a percentage bar in this color. */ |
| 77 | + const char *msg = NULL; |
| 78 | + const char *msg2 = NULL; |
| 79 | + |
| 80 | + switch (state) { |
| 81 | + case SDL_POWERSTATE_ERROR: |
| 82 | + msg2 = "ERROR GETTING POWER STATE"; |
| 83 | + msg = SDL_GetError(); |
| 84 | + clearr = 255; /* red background */ |
| 85 | + break; |
| 86 | + |
| 87 | + default: /* in case this does something unexpected later, treat it as unknown. */ |
| 88 | + case SDL_POWERSTATE_UNKNOWN: |
| 89 | + msg = "Power state is unknown."; |
| 90 | + clearr = clearb = clearg = 50; /* grey background */ |
| 91 | + break; |
| 92 | + |
| 93 | + case SDL_POWERSTATE_ON_BATTERY: |
| 94 | + msg = "Running on battery."; |
| 95 | + barr = 255; /* draw in red */ |
| 96 | + break; |
| 97 | + |
| 98 | + case SDL_POWERSTATE_NO_BATTERY: |
| 99 | + msg = "Plugged in, no battery available."; |
| 100 | + clearg = 50; /* green background */ |
| 101 | + break; |
| 102 | + |
| 103 | + case SDL_POWERSTATE_CHARGING: |
| 104 | + msg = "Charging."; |
| 105 | + barb = barg = 255; /* draw in cyan */ |
| 106 | + break; |
| 107 | + |
| 108 | + case SDL_POWERSTATE_CHARGED: |
| 109 | + msg = "Charged."; |
| 110 | + barg = 255; /* draw in green */ |
| 111 | + break; |
| 112 | + } |
| 113 | + |
| 114 | + SDL_SetRenderDrawColor(renderer, clearr, clearg, clearb, 255); |
| 115 | + SDL_RenderClear(renderer); |
| 116 | + |
| 117 | + if (percent >= 0) { |
| 118 | + float x, y; |
| 119 | + SDL_FRect pctrect; |
| 120 | + char remainstr[64]; |
| 121 | + char msgbuf[128]; |
| 122 | + |
| 123 | + SDL_copyp(&pctrect, &frame); |
| 124 | + pctrect.w *= percent / 100.0f; |
| 125 | + |
| 126 | + if (seconds < 0) { |
| 127 | + SDL_strlcpy(remainstr, "unknown time", sizeof (remainstr)); |
| 128 | + } else { |
| 129 | + int hours, minutes; |
| 130 | + hours = seconds / (60 * 60); |
| 131 | + seconds -= hours * (60 * 60); |
| 132 | + minutes = seconds / 60; |
| 133 | + seconds -= minutes * 60; |
| 134 | + SDL_snprintf(remainstr, sizeof (remainstr), "%02d:%02d:%02d", hours, minutes, seconds); |
| 135 | + } |
| 136 | + |
| 137 | + SDL_snprintf(msgbuf, sizeof (msgbuf), "Battery: %3d percent, %s remaining", percent, remainstr); |
| 138 | + x = frame.x + ((frame.w - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(msgbuf))) / 2.0f); |
| 139 | + y = frame.y + frame.h + SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE; |
| 140 | + |
| 141 | + SDL_SetRenderDrawColor(renderer, barr, barg, barb, 255); /* draw percent bar. */ |
| 142 | + SDL_RenderFillRect(renderer, &pctrect); |
| 143 | + SDL_SetRenderDrawColor(renderer, framer, frameg, frameb, 255); /* draw frame on top of bar. */ |
| 144 | + SDL_RenderRect(renderer, &frame); |
| 145 | + SDL_SetRenderDrawColor(renderer, textr, textg, textb, 255); |
| 146 | + SDL_RenderDebugText(renderer, x, y, msgbuf); /* draw text about battery level */ |
| 147 | + } |
| 148 | + |
| 149 | + if (msg) { |
| 150 | + const float x = frame.x + ((frame.w - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(msg))) / 2.0f); |
| 151 | + const float y = frame.y - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * 2); |
| 152 | + SDL_SetRenderDrawColor(renderer, textr, textg, textb, 255); |
| 153 | + SDL_RenderDebugText(renderer, x, y, msg); |
| 154 | + } |
| 155 | + |
| 156 | + if (msg2) { |
| 157 | + const float x = frame.x + ((frame.w - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(msg2))) / 2.0f); |
| 158 | + const float y = frame.y - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * 4); |
| 159 | + SDL_SetRenderDrawColor(renderer, textr, textg, textb, 255); |
| 160 | + SDL_RenderDebugText(renderer, x, y, msg2); |
| 161 | + } |
| 162 | + |
| 163 | + /* put the new rendering on the screen. */ |
| 164 | + SDL_RenderPresent(renderer); |
| 165 | + |
| 166 | + return SDL_APP_CONTINUE; /* carry on with the program! */ |
| 167 | +} |
| 168 | + |
| 169 | +/* This function runs once at shutdown. */ |
| 170 | +void SDL_AppQuit(void *appstate, SDL_AppResult result) |
| 171 | +{ |
| 172 | + /* SDL will clean up the window/renderer for us. */ |
| 173 | +} |
| 174 | + |
0 commit comments