Last active
August 29, 2015 14:01
-
-
Save C-D-Lewis/17eb11ab355950595ca2 to your computer and use it in GitHub Desktop.
SDK Tutorial 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <pebble.h> | |
Window* g_window; | |
TextLayer *g_text_layer; | |
GBitmap *g_frame_bitmap; | |
BitmapLayer *g_frame_layer; | |
void window_load(Window *window) | |
{ | |
//Create and add the image | |
g_frame_bitmap = gbitmap_create_with_resource(RESOURCE_ID_FRAME); | |
g_frame_layer = bitmap_layer_create(GRect(7, 56, 129, 60)); | |
bitmap_layer_set_bitmap(g_frame_layer, g_frame_bitmap); | |
layer_add_child(window_get_root_layer(window), bitmap_layer_get_layer(g_frame_layer)); | |
//Create the TextLayer | |
g_text_layer = text_layer_create(GRect(0, 59, 144, 50)); | |
text_layer_set_background_color(g_text_layer, GColorClear); | |
text_layer_set_text_color(g_text_layer, GColorBlack); | |
//Improve the layout to be more like a watchface | |
text_layer_set_font(g_text_layer, fonts_get_system_font(FONT_KEY_BITHAM_42_BOLD)); | |
text_layer_set_text_alignment(g_text_layer, GTextAlignmentCenter); | |
layer_add_child(window_get_root_layer(window), text_layer_get_layer(g_text_layer)); | |
} | |
void window_unload(Window *window) | |
{ | |
//We will safely destroy the Window's elements here! | |
text_layer_destroy(g_text_layer); | |
gbitmap_destroy(g_frame_bitmap); | |
bitmap_layer_destroy(g_frame_layer); | |
} | |
void tick_handler(struct tm *tick_time, TimeUnits units_changed) | |
{ | |
//Allocate long-lived storage (required by TextLayer) | |
static char buffer[] = "00:00"; | |
//Write the time to the buffer in a safe manner | |
strftime(buffer, sizeof("00:00"), "%H:%M", tick_time); | |
//Set the TextLayer to display the buffer | |
text_layer_set_text(g_text_layer, buffer); | |
} | |
void init() | |
{ | |
//Create the app elements here! | |
g_window = window_create(); | |
window_set_window_handlers(g_window, (WindowHandlers) { | |
.load = window_load, | |
.unload = window_unload, | |
}); | |
tick_timer_service_subscribe(MINUTE_UNIT, (TickHandler)tick_handler); | |
window_stack_push(g_window, true); | |
} | |
void deinit() | |
{ | |
//Destroy elements here to save memory! | |
window_destroy(g_window); | |
} | |
int main(void) | |
{ | |
init(); | |
app_event_loop(); | |
deinit(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment