Skip to content

Instantly share code, notes, and snippets.

@PEZ
Created December 28, 2013 21:00
Show Gist options
  • Select an option

  • Save PEZ/8164161 to your computer and use it in GitHub Desktop.

Select an option

Save PEZ/8164161 to your computer and use it in GitHub Desktop.
Drawing polygons on Pebble SDK2 Beta 4 bites me. Running this on Helco's Pebble Simulator - https://github.com/Helco/PebbleLocalSim - draws a nice and tidy triangle (just using 100 points to do it). Running it on my pebble draws some crazy stuff. Moreover, setting NUM_POINTS to 400 causes my pebble to reset and sometimes even enter recovery mode.
#include <pebble.h>
static Window *window;
static Layer *poly_layer;
const int NUM_POINTS = 100;
// Draws a simple triangle, just using many points
static GPath *create_poly_path(int num_points) {
APP_LOG(APP_LOG_LEVEL_DEBUG, "Creating poly path with %d points.", num_points);
GPoint points[num_points];
points[0].x = 0;
points[0].y = 0;
for (int i = 0; i < num_points - 1; i++) {
points[i+1].x = -(num_points/2) + i;
points[i+1].y = 40;
}
GPathInfo path_info = {
num_points,
points
};
return gpath_create(&path_info);
}
static void poly_layer_update_callback(Layer *layer, GContext* ctx) {
GRect bounds = layer_get_bounds(layer);
GPoint center = grect_center_point(&bounds);
GPath *poly_path = create_poly_path(NUM_POINTS);
APP_LOG(APP_LOG_LEVEL_DEBUG, "Poly path created. Drawing...");
gpath_move_to(poly_path, center);
graphics_context_set_fill_color(ctx, GColorWhite);
gpath_draw_filled(ctx, poly_path);
APP_LOG(APP_LOG_LEVEL_DEBUG, "Poly path drawn.");
gpath_destroy(poly_path);
}
static void init(void) {
window = window_create();
window_stack_push(window, true);
Layer *window_layer = window_get_root_layer(window);
window_set_background_color(window, GColorBlack);
window_stack_push(window, true);
GRect bounds = layer_get_bounds(window_layer);
poly_layer = layer_create(bounds);
layer_set_update_proc(poly_layer, poly_layer_update_callback);
layer_add_child(window_layer, poly_layer);
APP_LOG(APP_LOG_LEVEL_DEBUG, "Marking poly layer dirty");
layer_mark_dirty(poly_layer);
}
static void deinit(void) {
layer_destroy(poly_layer);
window_destroy(window);
}
int main(void) {
init();
APP_LOG(APP_LOG_LEVEL_DEBUG, "Done initializing, pushed window: %p", window);
app_event_loop();
deinit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment