Trammell Hudson avatar Trammell Hudson committed 29fb3fa

Converted some watches to use pebble_th.h and cleaned up font handling

Comments (0)

Files changed (7)

+DIRS += lib
 DIRS += hoelaat
 DIRS += auth
 DIRS += mario
 #include <pebble_os.h>
 #include <pebble_app.h>
 #include <pebble_fonts.h>
+#include <pebble_th.h>
 
 PBL_APP_INFO(
 	"CST Style",
 
 static Window window;
 
+static GFont font_big;
+static GFont font_small;
+
 static char time_text[8];
 static char date_text[16];
 
 
 
 static void
-text_layer(
-	TextLayer * layer,
-	GRect frame,
-	int font
-)
-{
-	text_layer_init(layer, frame);
-	text_layer_set_text(layer, "");
-	text_layer_set_text_color(layer, GColorWhite);
-	text_layer_set_background_color(layer, GColorClear);
-	text_layer_set_font(layer, fonts_load_custom_font(resource_get_handle(font)));
-
-	layer_add_child(&window.layer, &layer->layer);
-}
-
-
-static void
 handle_init(
 	AppContextRef ctx
 )
 	int y = 15;
 	int h = 30;
 
+	font_big  = fonts_load_custom_font(resource_get_handle(RESOURCE_ID_FONT_CST_BLACK_32));
+	font_small  = fonts_load_custom_font(resource_get_handle(RESOURCE_ID_FONT_CST_LIGHT_15));
+
 	// Stack top to bottom.  Note that the hour can take up
 	// two rows at midnight.
-	text_layer(
+	text_layer_setup(
+		&window,
 		&time_layer,
 		GRect(0, 80, 144, 168),
-		RESOURCE_ID_FONT_CST_BLACK_32
+		font_big
 	);
 
-	text_layer(
+	text_layer_setup(
+		&window,
 		&date_layer,
 		GRect(0, 120, 144, 40),
-		RESOURCE_ID_FONT_CST_LIGHT_15
+		font_small
 	);
 
-	text_layer(
+	text_layer_setup(
+		&window,
 		&day_layer,
 		GRect(0, 140, 144, 40),
-		RESOURCE_ID_FONT_CST_LIGHT_15
+		font_small
 	);
 }
 
 
+static void
+handle_deinit(
+	AppContextRef ctx
+)
+{
+	(void) ctx;
+
+	fonts_unload_custom_font(font_big);
+	fonts_unload_custom_font(font_small);
+}
+
+
 void
 pbl_main(
 	void * const params
 {
 	PebbleAppHandlers handlers = {
 		.init_handler	= &handle_init,
+		.deinit_handler	= &handle_deinit,
 		.tick_info	= {
 			.tick_handler = &handle_tick,
 			.tick_units = MINUTE_UNIT,

hoelaat/src/nederlands.c

 #include "pebble_os.h"
 #include "pebble_app.h"
 #include "pebble_fonts.h"
+#include "pebble_th.h"
 
 static Window window;
 static GFont font_thin;
 	GFont font
 )
 {
-	text_layer_init(&word->layer, frame);
-	text_layer_set_text(&word->layer, "");
-	text_layer_set_text_color(&word->layer, GColorWhite);
-	text_layer_set_background_color(&word->layer, GColorClear);
-	text_layer_set_font(&word->layer, font);
-
-	layer_add_child(&window.layer, &word->layer.layer);
+	text_layer_setup(&window, &word->layer, frame, font);
 
 	GRect frame_right = frame;
 	frame_right.origin.x = 150;
 }
 
 
+void
+monospace_text(
+	GContext * ctx,
+	const char * buf,
+	GFont font,
+	GPoint xy,
+	int x_size,
+	int y_size
+)
+{
+	while (1)
+	{
+		const char c = *buf++;
+		char str[2] = { c, '\0' };
 
+		if (c == '\0')
+			break;
+
+		graphics_text_draw(
+			ctx,
+			str,
+			font,
+			GRect(xy.x, xy.y, x_size, y_size),
+			GTextOverflowModeTrailingEllipsis,
+			GTextAlignmentRight,
+			NULL
+		);
+
+		xy.x += x_size;
+	}
+}

lib/src/pebble_th.h

 	GFont font
 );
 
+
+void
+monospace_text(
+	GContext * ctx,
+	const char * buf,
+	GFont font,
+	GPoint xy,
+	int x_size,
+	int y_size
+);
+
 #endif

stopwatch/src/stopwatch.c

 static int miliseconds;
 
 
-static void
-monospace_text(
-	GContext * ctx,
-	const char * buf,
-	GFont font,
-	GPoint xy,
-	int x_size,
-	int y_size
-)
-{
-	while (1)
-	{
-		const char c = *buf++;
-		char str[2] = { c, '\0' };
-
-		if (c == '\0')
-			break;
-
-		graphics_text_draw(
-			ctx,
-			str,
-			font,
-			GRect(xy.x, xy.y, x_size, y_size),
-			GTextOverflowModeTrailingEllipsis,
-			GTextAlignmentRight,
-			NULL
-		);
-
-		xy.x += x_size;
-	}
-}
-
-
 /**
  */
 static void

words/src/words.c

 #include <pebble_os.h>
 #include <pebble_app.h>
 #include <pebble_fonts.h>
+#include "pebble_th.h"
 
 PBL_APP_INFO(
 	"Wordsquare",
 	TextLayer * const layer = &layers[which];
 	const word_t * const w = &words[which];
 
-	font_on = fonts_load_custom_font(resource_get_handle(FONT_ON));
-	font_off = fonts_load_custom_font(resource_get_handle(FONT_OFF));
-
 	GRect frame = GRect(
 		w->col*FONT_W,
 		w->row*FONT_H - 2,
 		strlen(w->text_on)*(FONT_W+4),
 		FONT_H+8
 	);
-	text_layer_init(layer, frame);
-	text_layer_set_text_color(layer, GColorWhite);
-	text_layer_set_background_color(layer, GColorClear);
+
+	text_layer_setup(&window, layer, frame, font_off);
 	word_mark(which, 0); // all are "off" initially
-
-	layer_add_child(&window.layer, &layer->layer);
 }
 
 
 
 	resource_init_current_app(&RESOURCES);
 
+	font_on = fonts_load_custom_font(resource_get_handle(FONT_ON));
+	font_off = fonts_load_custom_font(resource_get_handle(FONT_OFF));
+
 	for (unsigned i = 0 ; i < WORD_COUNT ; i++)
 		word_layer_init(i);
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.