From 9f6f83e2961a3edc608b989b31a9823c5070c235 Mon Sep 17 00:00:00 2001 From: synzr Date: Wed, 15 Oct 2025 21:19:43 +0500 Subject: [PATCH] feat(services): resources service --- src/c/character_layer.c | 20 ++++------------- src/c/clock_layer.c | 32 +++++++++++----------------- src/c/main.c | 3 +++ src/c/resources_service.c | 45 +++++++++++++++++++++++++++++++++++++++ src/c/resources_service.h | 17 +++++++++++++++ 5 files changed, 81 insertions(+), 36 deletions(-) create mode 100644 src/c/resources_service.c create mode 100644 src/c/resources_service.h diff --git a/src/c/character_layer.c b/src/c/character_layer.c index 501ef6b..818b5c4 100644 --- a/src/c/character_layer.c +++ b/src/c/character_layer.c @@ -1,26 +1,19 @@ #include "character_layer.h" +#include "resources_service.h" #define CHARACTER_H 80 #define CHARACTER_STEP 4 typedef struct CharacterLayerData { - int ticks; - GBitmap *even; - GBitmap *odd; + int8_t ticks; } CharacterLayerData; static Layer *s_character_layer; static void character_layer_update_proc(Layer *layer, GContext *ctx) { - CharacterLayerData *layer_data = layer_get_data(s_character_layer); - // Get the character bitmap. - GBitmap *bitmap; - if (layer_data->ticks % 2 == 0) { - bitmap = layer_data->even; - } else { - bitmap = layer_data->odd; - } + CharacterLayerData *layer_data = layer_get_data(s_character_layer); + GBitmap *bitmap = resources_service_get_character(layer_data->ticks); // Get the position of character bitmap and align it to bottom. GRect frame = gbitmap_get_bounds(bitmap); @@ -50,8 +43,6 @@ void character_layer_init(Window *window) { layer_create_with_data(GRect(0, window_bounds.size.h - CHARACTER_H, window_bounds.size.w, CHARACTER_H), sizeof(CharacterLayerData)); CharacterLayerData *layer_data = layer_get_data(s_character_layer); - layer_data->even = gbitmap_create_with_resource(RESOURCE_ID_CHARACTER_EVEN); - layer_data->odd = gbitmap_create_with_resource(RESOURCE_ID_CHARACTER_ODD); layer_data->ticks = 0; layer_set_update_proc(s_character_layer, character_layer_update_proc); @@ -67,8 +58,5 @@ void character_layer_tick(void) { } void character_layer_deinit(void) { - CharacterLayerData *layer_data = layer_get_data(s_character_layer); - gbitmap_destroy(layer_data->even); - gbitmap_destroy(layer_data->odd); layer_destroy(s_character_layer); } diff --git a/src/c/clock_layer.c b/src/c/clock_layer.c index 804f73e..84dc703 100644 --- a/src/c/clock_layer.c +++ b/src/c/clock_layer.c @@ -1,19 +1,14 @@ #include "clock_layer.h" +#include "resources_service.h" + #define CLOCK_H 40 #define CLOCK_SPACE 2 #define CLOCK_OFFSET 4 -typedef struct ClockLayerData { - GFont font_big; - GFont font_small; -} ClockLayerData; - static Layer *s_clock_layer; static void clock_layer_update_proc(Layer *layer, GContext *ctx) { - ClockLayerData *layer_data = layer_get_data(s_clock_layer); - // Fill a whole layer with the black color. GRect layer_bounds = layer_get_unobstructed_bounds(layer); layer_bounds.origin = GPoint(0, 0); @@ -26,15 +21,19 @@ static void clock_layer_update_proc(Layer *layer, GContext *ctx) { // Drawing the time and seconds. static char time_text[6], seconds_text[3] = "00\0"; + + GFont font_small = resources_service_get_custom_font(CustomFontKonekoToro); + GFont font_big = resources_service_get_custom_font(CustomFontToro); + graphics_context_set_text_color(ctx, GColorWhite); strftime(time_text, sizeof(time_text), clock_is_24h_style() ? "%H:%M" : "%I:%M", time); GSize time_text_size = graphics_text_layout_get_content_size( - time_text, layer_data->font_big, layer_bounds, GTextOverflowModeFill, GTextAlignmentLeft); + time_text, font_big, layer_bounds, GTextOverflowModeFill, GTextAlignmentLeft); seconds_text[0] = seconds_text[1] = '0'; GSize seconds_text_size = graphics_text_layout_get_content_size( - seconds_text, layer_data->font_small, layer_bounds, GTextOverflowModeFill, GTextAlignmentLeft); + seconds_text, font_small, layer_bounds, GTextOverflowModeFill, GTextAlignmentLeft); strftime(seconds_text, sizeof(seconds_text), "%S", time); GRect timestamp_rect = @@ -44,12 +43,12 @@ static void clock_layer_update_proc(Layer *layer, GContext *ctx) { GRect time_rect = GRect(timestamp_rect.origin.x, timestamp_rect.origin.y, time_text_size.w, time_text_size.h); - graphics_draw_text(ctx, time_text, layer_data->font_big, time_rect, GTextOverflowModeFill, - GTextAlignmentLeft, NULL); + graphics_draw_text(ctx, time_text, font_big, time_rect, GTextOverflowModeFill, GTextAlignmentLeft, + NULL); GRect seconds_rect = GRect(0, 0, seconds_text_size.w, seconds_text_size.h); grect_align(&seconds_rect, ×tamp_rect, GAlignBottomRight, true); - graphics_draw_text(ctx, seconds_text, layer_data->font_small, seconds_rect, GTextOverflowModeFill, + graphics_draw_text(ctx, seconds_text, font_small, seconds_rect, GTextOverflowModeFill, GTextAlignmentLeft, NULL); } @@ -58,11 +57,7 @@ void clock_layer_init(Window *window, GPoint position) { GRect window_bounds = layer_get_unobstructed_bounds(window_root_layer); s_clock_layer = - layer_create_with_data(GRect(position.x, position.y, window_bounds.size.w - position.x, CLOCK_H), sizeof(ClockLayerData)); - - ClockLayerData *layer_data = layer_get_data(s_clock_layer); - layer_data->font_big = fonts_load_custom_font(resource_get_handle(RESOURCE_ID_FONT_TORO_38)); - layer_data->font_small = fonts_load_custom_font(resource_get_handle(RESOURCE_ID_FONT_KONEKO_TORO_28)); + layer_create(GRect(position.x, position.y, window_bounds.size.w - position.x, CLOCK_H)); layer_set_update_proc(s_clock_layer, clock_layer_update_proc); layer_add_child(window_root_layer, s_clock_layer); @@ -73,8 +68,5 @@ void clock_layer_tick(void) { } void clock_layer_deinit(void) { - ClockLayerData *layer_data = layer_get_data(s_clock_layer); - fonts_unload_custom_font(layer_data->font_big); - fonts_unload_custom_font(layer_data->font_small); layer_destroy(s_clock_layer); } diff --git a/src/c/main.c b/src/c/main.c index 7b7d319..734fa89 100644 --- a/src/c/main.c +++ b/src/c/main.c @@ -1,12 +1,15 @@ #include #include "main_window.h" +#include "resources_service.h" inline void init(void) { + resources_service_init(); main_window_init(); } inline void deinit(void) { main_window_deinit(); + resources_service_deinit(); } int main(void) { diff --git a/src/c/resources_service.c b/src/c/resources_service.c new file mode 100644 index 0000000..d0c13b8 --- /dev/null +++ b/src/c/resources_service.c @@ -0,0 +1,45 @@ +#include "resources_service.h" + +typedef struct ResourcesService { + GFont font_koneko_toro; + GFont font_toro; + GBitmap *character_even; + GBitmap *character_odd; +} ResourcesService; + +static ResourcesService *s_resources_service; + +void resources_service_init(void) { + s_resources_service = malloc(sizeof(ResourcesService)); + s_resources_service->font_koneko_toro = + fonts_load_custom_font(resource_get_handle(RESOURCE_ID_FONT_KONEKO_TORO_28)); + s_resources_service->font_toro = + fonts_load_custom_font(resource_get_handle(RESOURCE_ID_FONT_TORO_38)); + s_resources_service->character_even = gbitmap_create_with_resource(RESOURCE_ID_CHARACTER_EVEN); + s_resources_service->character_odd = gbitmap_create_with_resource(RESOURCE_ID_CHARACTER_ODD); +} + +void resources_service_deinit(void) { + fonts_unload_custom_font(s_resources_service->font_koneko_toro); + fonts_unload_custom_font(s_resources_service->font_toro); + gbitmap_destroy(s_resources_service->character_even); + gbitmap_destroy(s_resources_service->character_odd); + free(s_resources_service); +} + +GBitmap *resources_service_get_character(int ticks) { + if (ticks % 2 == 0) { + return s_resources_service->character_even; + } + return s_resources_service->character_odd; +} + +GFont resources_service_get_custom_font(CustomFont font) { + switch (font) { + case CustomFontKonekoToro: + return s_resources_service->font_koneko_toro; + case CustomFontToro: + return s_resources_service->font_toro; + } + return NULL; +} diff --git a/src/c/resources_service.h b/src/c/resources_service.h new file mode 100644 index 0000000..b5217e9 --- /dev/null +++ b/src/c/resources_service.h @@ -0,0 +1,17 @@ +#ifndef RESOURCES_SERVICE_H_ +#define RESOURCES_SERVICE_H_ + +#include + +typedef enum CustomFont { + CustomFontKonekoToro, + CustomFontToro +} CustomFont; + +void resources_service_init(void); +void resources_service_deinit(void); + +GBitmap *resources_service_get_character(int ticks); +GFont resources_service_get_custom_font(CustomFont font); + +#endif