first pass at 12 hour clock mode

This commit is contained in:
Shaun Inman 2023-01-14 15:29:02 -05:00
parent 1c2d50d730
commit 8531236d62
5 changed files with 150 additions and 60 deletions

View file

@ -13,9 +13,6 @@
int main(int argc , char* argv[]) {
SDL_Surface* screen = GFX_init();
// SDL_Surface* confirm = IMG_Load(RES_PATH "confirm.png");
// SDL_Surface* digits = IMG_Load(RES_PATH "digits.png"); // 20x32
// TODO: make use of SCALE1()
SDL_Surface* digits = SDL_CreateRGBSurface(SDL_SWSURFACE, 240,32, 16, 0,0,0,0);
SDL_FillRect(digits, NULL, RGB_BLACK);
@ -38,16 +35,17 @@ int main(int argc , char* argv[]) {
int quit = 0;
int save_changes = 0;
int select_cursor = 0;
int show_24hour = 0; // TODO: exists(USERDATA_PATH "/show_24hour");
time_t t = time(NULL);
struct tm tm = *localtime(&t);
uint32_t day_selected = tm.tm_mday;
uint32_t month_selected = tm.tm_mon + 1;
int32_t day_selected = tm.tm_mday;
int32_t month_selected = tm.tm_mon + 1;
uint32_t year_selected = tm.tm_year + 1900;
uint32_t hour_selected = tm.tm_hour;
uint32_t minute_selected = tm.tm_min;
uint32_t seconds_selected = tm.tm_sec;
int32_t hour_selected = tm.tm_hour;
int32_t minute_selected = tm.tm_min;
int32_t seconds_selected = tm.tm_sec;
#define kSlash 10
#define kColon 11
@ -57,11 +55,7 @@ int main(int argc , char* argv[]) {
return x + 20;
}
void blitBar(int x, int y, int len) {
// TODO: create an ASSET_UNDERLINE that is 2.5 (x2 = 5) for
GFX_blitPill(ASSET_DOT, screen, &(SDL_Rect){ x,y,len * 20});
// GFX_blitAsset(ASSET_BAR, NULL, screen, &(SDL_Rect){ x + (len * 20 - SCALE1(4))/2, y });
GFX_blitPill(ASSET_UNDERLINE, screen, &(SDL_Rect){ x,y,len * 20});
}
int blitNumber(int num, int x, int y) {
int n;
@ -89,9 +83,9 @@ int main(int argc , char* argv[]) {
if ( ((year_selected % 4 == 0) && (year_selected % 100 != 0)) || (year_selected % 400 == 0)) february_days = 29;
// day
if (day_selected < 1) day_selected = 1;
if (month_selected > 12) month_selected = 12;
else if (month_selected < 1) month_selected = 1;
// if (day_selected < 1) day_selected = 1;
if (month_selected > 12) month_selected -= 12;
else if (month_selected < 1) month_selected += 12;
if (year_selected > 2100) year_selected = 2100;
else if (year_selected < 1970) year_selected = 1970;
@ -99,31 +93,40 @@ int main(int argc , char* argv[]) {
switch(month_selected)
{
case 2:
if (day_selected > february_days) day_selected = february_days;
if (day_selected > february_days) day_selected -= february_days;
else if (day_selected<1) day_selected += february_days;
break;
case 4:
case 6:
case 9:
case 11:
if (day_selected > 30) day_selected = 30;
if (day_selected > 30) day_selected -= 30;
else if (day_selected < 1) day_selected += 30;
break;
default:
if (day_selected > 31) day_selected = 31;
if (day_selected > 31) day_selected -= 31;
else if (day_selected < 1) day_selected += 31;
break;
}
// time
if (hour_selected > 23) hour_selected = 0;
if (minute_selected > 59) minute_selected = 0;
if (seconds_selected > 59) seconds_selected = 0;
if (hour_selected > 23) hour_selected -= 24;
else if (hour_selected < 0) hour_selected += 24;
if (minute_selected > 59) minute_selected -= 60;
else if (minute_selected < 0) minute_selected += 60;
if (seconds_selected > 59) seconds_selected -= 60;
else if (seconds_selected < 0) seconds_selected += 60;
}
int dirty = 1;
while(!quit) {
unsigned long frame_start = SDL_GetTicks();
PAD_poll();
if (PAD_justRepeated(BTN_UP)) {
dirty = 1;
switch(select_cursor) {
case 0:
year_selected++;
@ -146,6 +149,7 @@ int main(int argc , char* argv[]) {
}
}
else if (PAD_justRepeated(BTN_DOWN)) {
dirty = 1;
switch(select_cursor) {
case 0:
year_selected--;
@ -168,10 +172,12 @@ int main(int argc , char* argv[]) {
}
}
else if (PAD_justRepeated(BTN_LEFT)) {
dirty = 1;
select_cursor--;
if (select_cursor < 0) select_cursor += 6;
}
else if (PAD_justRepeated(BTN_RIGHT)) {
dirty = 1;
select_cursor++;
if (select_cursor > 5) select_cursor -= 6;
}
@ -182,44 +188,72 @@ int main(int argc , char* argv[]) {
else if (PAD_justPressed(BTN_B)) {
quit = 1;
}
validate();
// render
GFX_clear(screen);
GFX_blitABButtons("OKAY", "CANCEL", screen);
// datetime
int x = 130;
int y = 185;
x = blitNumber(year_selected, x,y);
x = blit(kSlash, x,y);
x = blitNumber(month_selected, x,y);
x = blit(kSlash, x,y);
x = blitNumber(day_selected, x,y);
x += 20; // space
x = blitNumber(hour_selected, x,y);
x = blit(kColon, x,y);
x = blitNumber(minute_selected, x,y);
x = blit(kColon, x,y);
x = blitNumber(seconds_selected, x,y);
// cursor
x = 130;
y = 222;
if (select_cursor>0) {
x += 100; // YYYY/
x += (select_cursor - 1) * 60;
else if (PAD_justPressed(BTN_SELECT)) {
dirty = 1;
show_24hour = !show_24hour;
}
blitBar(x,y, (select_cursor>0 ? 2 : 4));
GFX_flip(screen);
// // slow down to 60fps
// unsigned long frame_duration = SDL_GetTicks() - frame_start;
// #define kTargetFrameDuration 17
// if (frame_duration<kTargetFrameDuration) SDL_Delay(kTargetFrameDuration-frame_duration);
if (dirty) {
dirty = 0;
validate();
// render
GFX_clear(screen);
GFX_blitButtonGroup((char*[]){ "SELECT",show_24hour?"12 HOUR":"24 HOUR", NULL }, screen, 0);
GFX_blitButtonGroup((char*[]){ "B","CANCEL", "A","SET", NULL }, screen, 1);
// datetime
int x = show_24hour ? 130 : 90;
int y = 185;
x = blitNumber(year_selected, x,y);
x = blit(kSlash, x,y);
x = blitNumber(month_selected, x,y);
x = blit(kSlash, x,y);
x = blitNumber(day_selected, x,y);
x += 20; // space
int am = hour_selected < 12;
if (show_24hour) {
x = blitNumber(hour_selected, x,y);
}
else {
// 12 hour
int hour = hour_selected;
if (hour==0) hour = 12;
else if (hour>12) hour -= 12;
x = blitNumber(hour, x,y);
}
x = blit(kColon, x,y);
x = blitNumber(minute_selected, x,y);
x = blit(kColon, x,y);
x = blitNumber(seconds_selected, x,y);
if (!show_24hour) {
x += 20; // space
SDL_Surface* text = TTF_RenderUTF8_Blended(font.large, am ? "AM" : "PM", COLOR_WHITE);
SDL_BlitSurface(text, NULL, screen, &(SDL_Rect){x,y-6});
SDL_FreeSurface(text);
}
// cursor
x = show_24hour ? 130 : 90;
y = 222;
if (select_cursor>0) {
x += 100; // YYYY/
x += (select_cursor - 1) * 60;
}
blitBar(x,y, (select_cursor>0 ? 2 : 4));
GFX_flip(screen);
}
else {
// slow down to 60fps
unsigned long frame_duration = SDL_GetTicks() - frame_start;
#define kTargetFrameDuration 17
if (frame_duration<kTargetFrameDuration) SDL_Delay(kTargetFrameDuration-frame_duration);
}
}
SDL_FreeSurface(digits);

View file

@ -136,6 +136,7 @@ static SDL_Rect asset_rects[] = {
[ASSET_PAGE] = (SDL_Rect){SCALE4(39,54, 6, 6)},
[ASSET_BAR] = (SDL_Rect){SCALE4(33,58, 4, 4)},
[ASSET_BAR_BG] = (SDL_Rect){SCALE4(15,55, 4, 4)},
[ASSET_UNDERLINE] = (SDL_Rect){SCALE4(85,51, 3, 3)},
[ASSET_DOT] = (SDL_Rect){SCALE4(33,54, 2, 2)},
[ASSET_BRIGHTNESS] = (SDL_Rect){SCALE4(23,33,19,19)},
@ -212,6 +213,7 @@ SDL_Surface* GFX_init(void) {
RGB_WHITE = SDL_MapRGB(gfx.screen->format, TRIAD_WHITE);
RGB_BLACK = SDL_MapRGB(gfx.screen->format, TRIAD_BLACK);
RGB_LIGHT_GRAY = SDL_MapRGB(gfx.screen->format, TRIAD_LIGHT_GRAY);
RGB_GRAY = SDL_MapRGB(gfx.screen->format, TRIAD_GRAY);
RGB_DARK_GRAY = SDL_MapRGB(gfx.screen->format, TRIAD_DARK_GRAY);
asset_rgbs[ASSET_WHITE_PILL] = RGB_WHITE;
@ -223,6 +225,7 @@ SDL_Surface* GFX_init(void) {
asset_rgbs[ASSET_PAGE] = RGB_BLACK;
asset_rgbs[ASSET_BAR] = RGB_WHITE;
asset_rgbs[ASSET_BAR_BG] = RGB_BLACK;
asset_rgbs[ASSET_UNDERLINE] = RGB_GRAY;
asset_rgbs[ASSET_DOT] = RGB_LIGHT_GRAY;
char asset_path[MAX_PATH];
@ -450,6 +453,55 @@ void GFX_blitABButtons(char* a, char* b, SDL_Surface* dst) {
}
}
int GFX_blitButtonGroup(char** pairs, SDL_Surface* dst, int align_right) {
int ox;
int oy;
int ow;
char* hint;
char* button;
struct Hint {
char* hint;
char* button;
int ow;
} hints[2];
int w = 0; // individual button dimension
int h = 0; // hints index
ow = 0; // full pill width
ox = align_right ? SCREEN_WIDTH - SCALE1(PADDING) : SCALE1(PADDING);
oy = SCREEN_HEIGHT - SCALE1(PADDING + PILL_SIZE);
for (int i=0; i<2; i++) {
if (!pairs[i*2]) break;
button = pairs[i * 2];
hint = pairs[i * 2 + 1];
w = GFX_getButtonWidth(hint, button);
hints[h].hint = hint;
hints[h].button = button;
hints[h].ow = w;
h += 1;
ow += SCALE1(BUTTON_MARGIN) + w;
}
ow += SCALE1(BUTTON_MARGIN);
if (align_right) ox -= ow;
GFX_blitPill(ASSET_DARK_GRAY_PILL, dst, &(SDL_Rect){
ox,
oy,
ow,
SCALE1(PILL_SIZE)
});
ox += SCALE1(BUTTON_MARGIN);
oy += SCALE1(BUTTON_MARGIN);
for (int i=0; i<h; i++) {
GFX_blitButton(hints[i].hint, hints[i].button, dst, &(SDL_Rect){ox,oy});
ox += hints[i].ow + SCALE1(BUTTON_MARGIN);
}
return ow;
}
///////////////////////////////
// based on picoarch's audio

View file

@ -28,6 +28,7 @@ void LOG_note(int level, const char* fmt, ...);
uint32_t RGB_WHITE;
uint32_t RGB_BLACK;
uint32_t RGB_LIGHT_GRAY;
uint32_t RGB_GRAY;
uint32_t RGB_DARK_GRAY;
enum {
@ -40,6 +41,7 @@ enum {
ASSET_PAGE,
ASSET_BAR,
ASSET_BAR_BG,
ASSET_UNDERLINE,
ASSET_DOT,
ASSET_COLORS,
@ -76,6 +78,7 @@ void GFX_blitBattery(SDL_Surface* dst, SDL_Rect* dst_rect);
int GFX_getButtonWidth(char* hint, char* button);
void GFX_blitButton(char* hint, char*button, SDL_Surface* dst, SDL_Rect* dst_rect);
void GFX_blitABButtons(char* a, char* b, SDL_Surface* dst);
int GFX_blitButtonGroup(char** hints, SDL_Surface* dst, int align_right);
///////////////////////////////

View file

@ -47,17 +47,18 @@
#define TRIAD_WHITE 0xff,0xff,0xff
#define TRIAD_BLACK 0x00,0x00,0x00
#define TRIAD_LIGHT_GRAY 0x7f,0x7f,0x7f
#define TRIAD_GRAY 0x99,0x99,0x99
#define TRIAD_DARK_GRAY 0x26,0x26,0x26
#define TRIAD_LIGHT_TEXT 0xcc,0xcc,0xcc
#define TRIAD_DARK_TEXT 0x66,0x66,0x66
#define TRIAD_BUTTON_TEXT 0x99,0x99,0x99
#define COLOR_WHITE (SDL_Color){TRIAD_WHITE}
#define COLOR_GRAY (SDL_Color){TRIAD_GRAY}
#define COLOR_BLACK (SDL_Color){TRIAD_BLACK}
#define COLOR_LIGHT_TEXT (SDL_Color){TRIAD_LIGHT_TEXT}
#define COLOR_DARK_TEXT (SDL_Color){TRIAD_DARK_TEXT}
#define COLOR_BUTTON_TEXT (SDL_Color){TRIAD_BUTTON_TEXT}
#define COLOR_BUTTON_TEXT (SDL_Color){TRIAD_GRAY}
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480