diff options
author | Denis Dzyubenko <shadone@gmail.com> | 2014-01-13 20:03:42 +0400 |
---|---|---|
committer | Denis Dzyubenko <shadone@gmail.com> | 2014-01-13 20:04:10 +0400 |
commit | 087c318e6a827afceb0eeed843501d17a048eb56 (patch) | |
tree | 3a3c1fd0df6f4369a36afbee20c8bdd474de520d /src | |
parent | 25cb2081b636bf63fb531504dc1b1ec8df4c2f1e (diff) |
Ported the source code to SDK 2.0
Diffstat (limited to 'src')
-rw-r--r-- | src/pebble-mario.c | 317 |
1 files changed, 162 insertions, 155 deletions
diff --git a/src/pebble-mario.c b/src/pebble-mario.c index 1fd0867..e17bc59 100644 --- a/src/pebble-mario.c +++ b/src/pebble-mario.c @@ -17,27 +17,19 @@ // You may contact the author at denis@ddenis.info // -#include "pebble_os.h" -#include "pebble_app.h" -#include "pebble_fonts.h" +#include <pebble.h> +#include <time.h> // #define INVERSED_COLORS -#define MY_UUID { 0xB6, 0xA0, 0xAB, 0x5F, 0x92, 0xB7, 0x4C, 0x2B, 0xBC, 0x0F, 0x34, 0x6B, 0x99, 0xAE, 0x30, 0xA0 } -PBL_APP_INFO(MY_UUID, - "Mario", "Denis Dzyubenko", - 1, 2, /* App version */ - DEFAULT_MENU_ICON, - APP_INFO_WATCH_FACE); +Window *window; -Window window; - -Layer blocks_layer; -Layer mario_layer; -TextLayer text_hour_layer; -TextLayer text_minute_layer; -Layer ground_layer; -TextLayer date_layer; +Layer *blocks_layer; +Layer *mario_layer; +TextLayer *text_hour_layer; +TextLayer *text_minute_layer; +Layer *ground_layer; +TextLayer *date_layer; static char hour_text[] = "00"; static char minute_text[] = "00"; @@ -58,20 +50,21 @@ GRect minute_down_rect; static int mario_is_down = 1; -BmpContainer mario_normal_bmp; -BmpContainer mario_jump_bmp; -BmpContainer ground_bmp; +// TODO: I can really make use of BitmapLayer here +GBitmap *mario_normal_bmp; +GBitmap *mario_jump_bmp; +GBitmap *ground_bmp; -PropertyAnimation mario_animation_beg; -PropertyAnimation mario_animation_end; +PropertyAnimation *mario_animation_beg; +PropertyAnimation *mario_animation_end; -PropertyAnimation block_animation_beg; -PropertyAnimation block_animation_end; +PropertyAnimation *block_animation_beg; +PropertyAnimation *block_animation_end; -PropertyAnimation hour_animation_slide_away; -PropertyAnimation hour_animation_slide_in; -PropertyAnimation minute_animation_slide_away; -PropertyAnimation minute_animation_slide_in; +PropertyAnimation *hour_animation_slide_away; +PropertyAnimation *hour_animation_slide_in; +PropertyAnimation *minute_animation_slide_away; +PropertyAnimation *minute_animation_slide_in; #define BLOCK_SIZE 50 #define BLOCK_LAYER_EXTRA 3 @@ -89,6 +82,8 @@ PropertyAnimation minute_animation_slide_in; # define BackgroundColor GColorBlack #endif +void handle_tick(struct tm *tick_time, TimeUnits units_changed); + void draw_block(GContext *ctx, GRect rect, uint8_t width) { static const uint8_t radius = 1; @@ -140,14 +135,17 @@ void blocks_update_callback(Layer *layer, GContext *ctx) GRect block_rect[2]; - block_rect[0] = GRect(layer->bounds.origin.x, - layer->bounds.origin.y + BLOCK_LAYER_EXTRA, + GRect layer_bounds = layer_get_bounds(layer); + GRect layer_frame = layer_get_frame(layer); + + block_rect[0] = GRect(layer_bounds.origin.x, + layer_bounds.origin.y + BLOCK_LAYER_EXTRA, BLOCK_SIZE, - layer->frame.size.h - BLOCK_LAYER_EXTRA); - block_rect[1] = GRect(layer->bounds.origin.x + BLOCK_SIZE + BLOCK_SPACING, - layer->bounds.origin.y + BLOCK_LAYER_EXTRA, + layer_frame.size.h - BLOCK_LAYER_EXTRA); + block_rect[1] = GRect(layer_bounds.origin.x + BLOCK_SIZE + BLOCK_SPACING, + layer_bounds.origin.y + BLOCK_LAYER_EXTRA, BLOCK_SIZE, - layer->frame.size.h - BLOCK_LAYER_EXTRA); + layer_frame.size.h - BLOCK_LAYER_EXTRA); for (uint8_t i = 0; i < 2; ++i) { GRect *rect = block_rect + i; @@ -161,14 +159,13 @@ void mario_update_callback(Layer *layer, GContext *ctx) (void)layer; (void)ctx; - BmpContainer *bmp = mario_is_down ? &mario_normal_bmp : &mario_jump_bmp; - - GRect destination = layer_get_frame(&bmp->layer.layer); + GRect destination; + GBitmap *bmp; - destination.origin.x = 0; - destination.origin.y = 0; + bmp = mario_is_down ? mario_normal_bmp : mario_jump_bmp; + destination = GRect(0, 0, bmp->bounds.size.w, bmp->bounds.size.h); - graphics_draw_bitmap_in_rect(ctx, &bmp->bmp, destination); + graphics_draw_bitmap_in_rect(ctx, bmp, destination); } void ground_update_callback(Layer *layer, GContext *ctx) @@ -176,30 +173,26 @@ void ground_update_callback(Layer *layer, GContext *ctx) (void)layer; (void)ctx; - GRect image_rect = layer_get_frame(&ground_bmp.layer.layer); + GRect image_rect = ground_bmp->bounds; GRect rect = layer_get_frame(layer); int16_t image_width = image_rect.size.w; image_rect.origin.x = -10; image_rect.origin.y = 0; for (int i = 0; i < rect.size.w / image_rect.size.w + 1; ++i) { - graphics_draw_bitmap_in_rect(ctx, &ground_bmp.bmp, image_rect); + graphics_draw_bitmap_in_rect(ctx, ground_bmp, image_rect); image_rect.origin.x += image_width; } - text_layer_set_text(&date_layer, date_text); + text_layer_set_text(date_layer, date_text); } -void handle_init(AppContextRef ctx) +void handle_init() { - (void)ctx; - - window_init(&window, "Window Name"); - window_stack_push(&window, true /* Animated */); + window = window_create(); + window_stack_push(window, true /* Animated */); - window_set_background_color(&window, BackgroundColor); - - resource_init_current_app(&APP_RESOURCES); + window_set_background_color(window, BackgroundColor); blocks_down_rect = GRect(22, 7, BLOCK_SIZE*2, BLOCK_SIZE + BLOCK_LAYER_EXTRA); blocks_up_rect = GRect(22, 0, BLOCK_SIZE*2, BLOCK_SIZE + BLOCK_LAYER_EXTRA - BLOCK_SQUEEZE); @@ -214,58 +207,66 @@ void handle_init(AppContextRef ctx) minute_normal_rect = GRect(5+BLOCK_SIZE+BLOCK_SPACING, 5 + BLOCK_LAYER_EXTRA, 40, 40); minute_down_rect = GRect(5+BLOCK_SIZE+BLOCK_SPACING, BLOCK_SIZE + BLOCK_LAYER_EXTRA, 40, 40); - layer_init(&blocks_layer, blocks_down_rect); - layer_init(&mario_layer, mario_down_rect); - layer_init(&ground_layer, ground_rect); + blocks_layer = layer_create(blocks_down_rect); + mario_layer = layer_create(mario_down_rect); + ground_layer = layer_create(ground_rect); + + layer_set_update_proc(blocks_layer, &blocks_update_callback); + layer_set_update_proc(mario_layer, &mario_update_callback); + layer_set_update_proc(ground_layer, &ground_update_callback); - blocks_layer.update_proc = &blocks_update_callback; - mario_layer.update_proc = &mario_update_callback; - ground_layer.update_proc = &ground_update_callback; + Layer *window_layer = window_get_root_layer(window); - layer_add_child(&window.layer, &blocks_layer); - layer_add_child(&window.layer, &mario_layer); - layer_add_child(&window.layer, &ground_layer); + layer_add_child(window_layer, blocks_layer); + layer_add_child(window_layer, mario_layer); + layer_add_child(window_layer, ground_layer); - text_layer_init(&text_hour_layer, hour_normal_rect); - text_layer_set_text_color(&text_hour_layer, MainColor); - text_layer_set_background_color(&text_hour_layer, GColorClear); - text_layer_set_text_alignment(&text_hour_layer, GTextAlignmentCenter); - text_layer_set_font(&text_hour_layer, fonts_get_system_font(FONT_KEY_GOTHIC_28_BOLD)); - layer_add_child(&blocks_layer, &text_hour_layer.layer); + text_hour_layer = text_layer_create(hour_normal_rect); + text_layer_set_text_color(text_hour_layer, MainColor); + text_layer_set_background_color(text_hour_layer, GColorClear); + text_layer_set_text_alignment(text_hour_layer, GTextAlignmentCenter); + text_layer_set_font(text_hour_layer, fonts_get_system_font(FONT_KEY_GOTHIC_28_BOLD)); + layer_add_child(blocks_layer, (Layer *)text_hour_layer); - text_layer_init(&text_minute_layer, GRect(55, 5, 40, 40)); - text_layer_set_text_color(&text_minute_layer, MainColor); - text_layer_set_background_color(&text_minute_layer, GColorClear); - text_layer_set_text_alignment(&text_minute_layer, GTextAlignmentCenter); - text_layer_set_font(&text_minute_layer, fonts_get_system_font(FONT_KEY_GOTHIC_28_BOLD)); - layer_add_child(&blocks_layer, &text_minute_layer.layer); + text_minute_layer = text_layer_create(GRect(55, 5, 40, 40)); + text_layer_set_text_color(text_minute_layer, MainColor); + text_layer_set_background_color(text_minute_layer, GColorClear); + text_layer_set_text_alignment(text_minute_layer, GTextAlignmentCenter); + text_layer_set_font(text_minute_layer, fonts_get_system_font(FONT_KEY_GOTHIC_28_BOLD)); + layer_add_child(blocks_layer, (Layer *)text_minute_layer); GRect date_rect = GRect(30, 6, 144-30*2, ground_rect.size.h-6*2); - text_layer_init(&date_layer, date_rect); - text_layer_set_text_color(&date_layer, BackgroundColor); - text_layer_set_background_color(&date_layer, MainColor); - text_layer_set_text_alignment(&date_layer, GTextAlignmentCenter); - text_layer_set_font(&date_layer, fonts_get_system_font(FONT_KEY_GOTHIC_18)); - layer_add_child(&ground_layer, &date_layer.layer); + date_layer = text_layer_create(date_rect); + text_layer_set_text_color(date_layer, BackgroundColor); + text_layer_set_background_color(date_layer, MainColor); + text_layer_set_text_alignment(date_layer, GTextAlignmentCenter); + text_layer_set_font(date_layer, fonts_get_system_font(FONT_KEY_GOTHIC_18)); + layer_add_child(ground_layer, (Layer *)date_layer); #ifndef INVERSED_COLORS - bmp_init_container(RESOURCE_ID_IMAGE_MARIO_NORMAL, &mario_normal_bmp); - bmp_init_container(RESOURCE_ID_IMAGE_MARIO_JUMP, &mario_jump_bmp); - bmp_init_container(RESOURCE_ID_IMAGE_GROUND, &ground_bmp); + mario_normal_bmp = gbitmap_create_with_resource(RESOURCE_ID_IMAGE_MARIO_NORMAL); + mario_jump_bmp = gbitmap_create_with_resource(RESOURCE_ID_IMAGE_MARIO_JUMP); + ground_bmp = gbitmap_create_with_resource(RESOURCE_ID_IMAGE_GROUND); #else - bmp_init_container(RESOURCE_ID_IMAGE_MARIO_NORMAL_INVERSED, &mario_normal_bmp); - bmp_init_container(RESOURCE_ID_IMAGE_MARIO_JUMP_INVERSED, &mario_jump_bmp); - bmp_init_container(RESOURCE_ID_IMAGE_GROUND_INVERSED, &ground_bmp); + mario_normal_bmp = gbitmap_create_with_resource(RESOURCE_ID_IMAGE_MARIO_NORMAL_INVERSED); + mario_jump_bmp = gbitmap_create_with_resource(RESOURCE_ID_IMAGE_MARIO_JUMP_INVERSED); + ground_bmp = gbitmap_create_with_resource(RESOURCE_ID_IMAGE_GROUND_INVERSED); #endif + + tick_timer_service_subscribe(MINUTE_UNIT, handle_tick); } -void handle_deinit(AppContextRef ctx) +void handle_deinit() { - (void)ctx; + tick_timer_service_unsubscribe(); + + gbitmap_destroy(mario_normal_bmp); + gbitmap_destroy(mario_jump_bmp); + gbitmap_destroy(ground_bmp); + + // TODO: destroy all the resources! - bmp_deinit_container(&mario_normal_bmp); - bmp_deinit_container(&mario_jump_bmp); - bmp_deinit_container(&ground_bmp); + window_destroy(window); } void mario_down_animation_started(Animation *animation, void *data) @@ -279,7 +280,7 @@ void mario_down_animation_stopped(Animation *animation, void *data) (void)animation; (void)data; mario_is_down = 1; - layer_mark_dirty(&mario_layer); + layer_mark_dirty(mario_layer); } void mario_jump_animation_started(Animation *animation, void *data) @@ -287,7 +288,7 @@ void mario_jump_animation_started(Animation *animation, void *data) (void)animation; (void)data; mario_is_down = 0; - layer_mark_dirty(&mario_layer); + layer_mark_dirty(mario_layer); } void mario_jump_animation_stopped(Animation *animation, void *data) @@ -295,19 +296,20 @@ void mario_jump_animation_stopped(Animation *animation, void *data) (void)animation; (void)data; - text_layer_set_text(&text_hour_layer, hour_text); - text_layer_set_text(&text_minute_layer, minute_text); + text_layer_set_text(text_hour_layer, hour_text); + text_layer_set_text(text_minute_layer, minute_text); - property_animation_init_layer_frame(&mario_animation_end, &mario_layer, - &mario_up_rect, &mario_down_rect); - animation_set_duration(&mario_animation_end.animation, MARIO_JUMP_DURATION); - animation_set_curve(&mario_animation_end.animation, AnimationCurveEaseIn); - animation_set_handlers(&mario_animation_end.animation, (AnimationHandlers){ + mario_animation_end = property_animation_create_layer_frame(mario_layer, + &mario_up_rect, + &mario_down_rect); + animation_set_duration((Animation *)mario_animation_end, MARIO_JUMP_DURATION); + animation_set_curve((Animation *)mario_animation_end, AnimationCurveEaseIn); + animation_set_handlers((Animation *)mario_animation_end, (AnimationHandlers){ .started = (AnimationStartedHandler)mario_down_animation_started, .stopped = (AnimationStoppedHandler)mario_down_animation_stopped }, 0); - animation_schedule(&mario_animation_end.animation); + animation_schedule((Animation *)mario_animation_end); } void block_up_animation_started(Animation *animation, void *data) @@ -321,11 +323,12 @@ void block_up_animation_stopped(Animation *animation, void *data) (void)animation; (void)data; - property_animation_init_layer_frame(&block_animation_end, &blocks_layer, - &blocks_up_rect, &blocks_down_rect); - animation_set_duration(&block_animation_end.animation, MARIO_JUMP_DURATION); - animation_set_curve(&block_animation_end.animation, AnimationCurveEaseIn); - animation_schedule(&block_animation_end.animation); + block_animation_end = property_animation_create_layer_frame(blocks_layer, + &blocks_up_rect, + &blocks_down_rect); + animation_set_duration((Animation *)block_animation_end, MARIO_JUMP_DURATION); + animation_set_curve((Animation *)block_animation_end, AnimationCurveEaseIn); + animation_schedule((Animation *)block_animation_end); } void clock_animation_slide_away_started(Animation *animation, void *data) @@ -339,55 +342,61 @@ void clock_animation_slide_away_stopped(Animation *animation, void *data) (void)animation; (void)data; - layer_set_frame(&text_hour_layer.layer, hour_down_rect); - layer_set_frame(&text_minute_layer.layer, minute_down_rect); - - property_animation_init_layer_frame(&hour_animation_slide_in, &text_hour_layer.layer, - &hour_down_rect, &hour_normal_rect); - animation_set_duration(&hour_animation_slide_in.animation, CLOCK_ANIMATION_DURATION); - animation_set_curve(&hour_animation_slide_in.animation, AnimationCurveLinear); - animation_schedule(&hour_animation_slide_in.animation); - - property_animation_init_layer_frame(&minute_animation_slide_in, &text_minute_layer.layer, - &minute_down_rect, &minute_normal_rect); - animation_set_duration(&minute_animation_slide_in.animation, CLOCK_ANIMATION_DURATION); - animation_set_curve(&minute_animation_slide_in.animation, AnimationCurveLinear); - animation_schedule(&minute_animation_slide_in.animation); + layer_set_frame((Layer *)text_hour_layer, hour_down_rect); + layer_set_frame((Layer *)text_minute_layer, minute_down_rect); + + hour_animation_slide_in = property_animation_create_layer_frame((Layer *)text_hour_layer, + &hour_down_rect, + &hour_normal_rect); + animation_set_duration((Animation *)hour_animation_slide_in, CLOCK_ANIMATION_DURATION); + animation_set_curve((Animation *)hour_animation_slide_in, AnimationCurveLinear); + animation_schedule((Animation *)hour_animation_slide_in); + + minute_animation_slide_in = property_animation_create_layer_frame((Layer *)text_minute_layer, + &minute_down_rect, + &minute_normal_rect); + animation_set_duration((Animation *)minute_animation_slide_in, CLOCK_ANIMATION_DURATION); + animation_set_curve((Animation *)minute_animation_slide_in, AnimationCurveLinear); + animation_schedule((Animation *)minute_animation_slide_in); } -void handle_tick(AppContextRef app_ctx, PebbleTickEvent *event) +void handle_tick(struct tm *tick_time, TimeUnits units_changed) { - property_animation_init_layer_frame(&mario_animation_beg, &mario_layer, - &mario_down_rect, &mario_up_rect); - animation_set_duration(&mario_animation_beg.animation, MARIO_JUMP_DURATION); - animation_set_curve(&mario_animation_beg.animation, AnimationCurveEaseOut); - animation_set_handlers(&mario_animation_beg.animation, (AnimationHandlers){ + mario_animation_beg = property_animation_create_layer_frame(mario_layer, + &mario_down_rect, + &mario_up_rect); + animation_set_duration((Animation *)mario_animation_beg, MARIO_JUMP_DURATION); + animation_set_curve((Animation *)mario_animation_beg, AnimationCurveEaseOut); + animation_set_handlers((Animation *)mario_animation_beg, (AnimationHandlers){ .started = (AnimationStartedHandler)mario_jump_animation_started, .stopped = (AnimationStoppedHandler)mario_jump_animation_stopped }, 0); - property_animation_init_layer_frame(&block_animation_beg, &blocks_layer, - &blocks_down_rect, &blocks_up_rect); - animation_set_duration(&block_animation_beg.animation, MARIO_JUMP_DURATION); - animation_set_curve(&block_animation_beg.animation, AnimationCurveEaseOut); - animation_set_handlers(&block_animation_beg.animation, (AnimationHandlers){ + block_animation_beg = property_animation_create_layer_frame(blocks_layer, + &blocks_down_rect, + &blocks_up_rect); + animation_set_duration((Animation *)block_animation_beg, MARIO_JUMP_DURATION); + animation_set_curve((Animation *)block_animation_beg, AnimationCurveEaseOut); + animation_set_handlers((Animation *)block_animation_beg, (AnimationHandlers){ .started = (AnimationStartedHandler)block_up_animation_started, .stopped = (AnimationStoppedHandler)block_up_animation_stopped }, 0); - property_animation_init_layer_frame(&hour_animation_slide_away, &text_hour_layer.layer, - &hour_normal_rect, &hour_up_rect); - animation_set_duration(&hour_animation_slide_away.animation, CLOCK_ANIMATION_DURATION); - animation_set_curve(&hour_animation_slide_away.animation, AnimationCurveLinear); - animation_set_handlers(&hour_animation_slide_away.animation, (AnimationHandlers){ + hour_animation_slide_away = property_animation_create_layer_frame((Layer *)text_hour_layer, + &hour_normal_rect, + &hour_up_rect); + animation_set_duration((Animation *)hour_animation_slide_away, CLOCK_ANIMATION_DURATION); + animation_set_curve((Animation *)hour_animation_slide_away, AnimationCurveLinear); + animation_set_handlers((Animation *)hour_animation_slide_away, (AnimationHandlers){ .started = (AnimationStartedHandler)clock_animation_slide_away_started, .stopped = (AnimationStoppedHandler)clock_animation_slide_away_stopped }, 0); - property_animation_init_layer_frame(&minute_animation_slide_away, &text_minute_layer.layer, - &minute_normal_rect, &minute_up_rect); - animation_set_duration(&minute_animation_slide_away.animation, CLOCK_ANIMATION_DURATION); - animation_set_curve(&minute_animation_slide_away.animation, AnimationCurveLinear); + minute_animation_slide_away = property_animation_create_layer_frame((Layer *)text_minute_layer, + &minute_normal_rect, + &minute_up_rect); + animation_set_duration((Animation *)minute_animation_slide_away, CLOCK_ANIMATION_DURATION); + animation_set_curve((Animation *)minute_animation_slide_away, AnimationCurveLinear); char *hour_format; if (clock_is_24h_style()) { @@ -395,31 +404,29 @@ void handle_tick(AppContextRef app_ctx, PebbleTickEvent *event) } else { hour_format = "%I"; } - string_format_time(hour_text, sizeof(hour_text), hour_format, event->tick_time); + + time_t current_time = time(NULL); + struct tm *tm = localtime(¤t_time); + + strftime(hour_text, sizeof(hour_text), hour_format, tm); if (!clock_is_24h_style() && (hour_text[0] == '0')) { memmove(hour_text, &hour_text[1], sizeof(hour_text) - 1); } char *minute_format = "%M"; - string_format_time(minute_text, sizeof(minute_text), minute_format, event->tick_time); + strftime(minute_text, sizeof(minute_text), minute_format, tm); - string_format_time(date_text, sizeof(date_text), "%a, %b %d", event->tick_time); + strftime(date_text, sizeof(date_text), "%a, %b %d", tm); - animation_schedule(&mario_animation_beg.animation); - animation_schedule(&block_animation_beg.animation); - animation_schedule(&hour_animation_slide_away.animation); - animation_schedule(&minute_animation_slide_away.animation); + animation_schedule((Animation *)mario_animation_beg); + animation_schedule((Animation *)block_animation_beg); + animation_schedule((Animation *)hour_animation_slide_away); + animation_schedule((Animation *)minute_animation_slide_away); } -void pbl_main(void *params) +int main(void) { - PebbleAppHandlers handlers = { - .init_handler = &handle_init, - .deinit_handler = &handle_deinit, - .tick_info = { - .tick_handler = &handle_tick, - .tick_units = MINUTE_UNIT - } - }; - app_event_loop(params, &handlers); + handle_init(); + app_event_loop(); + handle_deinit(); } |