#include <pebble.h>
static Window *s_main_window;
static TextLayer *s_time_layer;
static BitmapLayer *s_background_layer;
static GBitmap *s_background_bitmap;
static Layer *s_canvas1_layer;
static Layer *s_canvas2_layer;
static Layer *s_canvas3_layer;
static Layer *s_canvas4_layer;
static GBitmap *s_image1_bitmap;
static GBitmap *s_image2_bitmap;
static GBitmap *s_image3_bitmap;
static GBitmap *s_image4_bitmap;
static void layer_update_proc(Layer *layer, GContext *ctx) {
// Draw the image with the correct compositing mode
graphics_context_set_compositing_mode(ctx, GCompOpSet);
graphics_draw_bitmap_in_rect(ctx, s_image1_bitmap, gbitmap_get_bounds(s_image1_bitmap));
}
static GFont s_time_font;
static void update_time() {
// Get a tm structure
time_t temp = time(NULL);
struct tm *tick_time = localtime(&temp);
// Write the current hours and minutes into a buffer
static char s_buffer[8];
strftime(s_buffer, sizeof(s_buffer), "%I:%M", tick_time);
// Display this time on the TextLayer
text_layer_set_text(s_time_layer, s_buffer);
}
static void tick_handler(struct tm *tick_time, TimeUnits units_changed) {
update_time();
}
static void main_window_load(Window *window) {
// Get information about the Window
Layer *window_layer = window_get_root_layer(window);
GRect bounds = layer_get_bounds(window_layer);
// Create the TextLayer with specific bounds
s_time_layer = text_layer_create(
GRect(0, PBL_IF_ROUND_ELSE(58, 115), bounds.size.w, 50));
// Improve the layout to be more like a watchface
text_layer_set_background_color(s_time_layer, GColorClear);
text_layer_set_text_color(s_time_layer, GColorBlack);
text_layer_set_text(s_time_layer, "00:00");
text_layer_set_text_alignment(s_time_layer, GTextAlignmentCenter);
// Create GFont
s_time_font = fonts_load_custom_font(resource_get_handle(RESOURCE_ID_FONT_PERFECT_DOS_48));
// Apply to TextLayer
text_layer_set_font(s_time_layer, s_time_font);
// Add it as a child layer to the Window's root layer
layer_add_child(window_layer, text_layer_get_layer(s_time_layer));
if(MINUTE_UNIT>= 0 && MINUTE_UNIT <13){
s_image1_bitmap = gbitmap_create_with_resource(RESOURCE_ID_IMAGE_RICE_EMOJI);
s_canvas1_layer = layer_create(GRect(8, 5, 148, 148));
layer_set_update_proc(s_canvas1_layer, layer_update_proc);
layer_add_child(window_layer, s_canvas1_layer);
}
if(MINUTE_UNIT >= 13 && MINUTE_UNIT <30){
//gbitmap_destroy(s_image1_bitmap);
s_image2_bitmap = gbitmap_create_with_resource(RESOURCE_ID_IMAGE_ATOM);
s_canvas2_layer = layer_create(GRect(8, 5, 148, 148));
layer_set_update_proc(s_canvas2_layer, layer_update_proc);
layer_add_child(window_layer, s_canvas2_layer);
}
if(MINUTE_UNIT >= 30 && MINUTE_UNIT <45){
s_image3_bitmap = gbitmap_create_with_resource(RESOURCE_ID_IMAGE_BOOK);
s_canvas3_layer = layer_create(GRect(8, 5, 148, 148));
layer_set_update_proc(s_canvas3_layer, layer_update_proc);
layer_add_child(window_layer, s_canvas3_layer);
}
if(MINUTE_UNIT >= 45 && MINUTE_UNIT <60){
s_image4_bitmap = gbitmap_create_with_resource(RESOURCE_ID_IMAGE_POPCORN);
s_canvas4_layer = layer_create(GRect(8, 5, 148, 148));
layer_set_update_proc(s_canvas4_layer, layer_update_proc);
layer_add_child(window_layer, s_canvas4_layer);
}
}
static void main_window_unload(Window *window) {
// Destroy TextLayer
text_layer_destroy(s_time_layer);
// Unload GFont
fonts_unload_custom_font(s_time_font);
// Destroy GBitmap
gbitmap_destroy(s_background_bitmap);
//destroy rice
gbitmap_destroy(s_image1_bitmap);
layer_destroy(s_canvas1_layer);
layer_destroy(s_canvas2_layer);
layer_destroy(s_canvas3_layer);
layer_destroy(s_canvas4_layer);
gbitmap_destroy(s_image2_bitmap);
gbitmap_destroy(s_image3_bitmap);
gbitmap_destroy(s_image4_bitmap);
// Destroy BitmapLayer
bitmap_layer_destroy(s_background_layer);
}
static void init() {
// Create main Window element and assign to pointer
s_main_window = window_create();
// Set the background color
window_set_background_color(s_main_window, GColorRed);
// Set handlers to manage the elements inside the Window
window_set_window_handlers(s_main_window, (WindowHandlers) {
.load = main_window_load,
.unload = main_window_unload
});
// Show the Window on the watch, with animated=true
window_stack_push(s_main_window, true);
// Make sure the time is displayed from the start
update_time();
// Register with TickTimerService
tick_timer_service_subscribe(MINUTE_UNIT, tick_handler);
}
static void deinit() {
// Destroy Window
window_destroy(s_main_window);
}
int main(void) {
init();
app_event_loop();
deinit();
}