Skip to content

Instantly share code, notes, and snippets.

@MathewReiss
Created June 4, 2015 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MathewReiss/a0a014c55608efea423b to your computer and use it in GitHub Desktop.
Save MathewReiss/a0a014c55608efea423b to your computer and use it in GitHub Desktop.
Pokedex main.c File
#include <pebble.h>
#include "pokedex.h"
#define POKEDEX_DATA 0
#define DATA 0
int DEBUG = 0;
int pokedex[152];
uint8_t array[19];
int num_caught = 0;
char num_caught_string[8];
int recent_catch = 0;
Window *window;
TextLayer *text_layer;
Layer *fake_status_bar;
GBitmap *poke_image;
BitmapLayer *poke_image_layer;
GBitmap *top_bar_image;
BitmapLayer *top_bar_layer;
GBitmap *bottom_bar_image;
BitmapLayer *bottom_bar_layer;
GFont *custom_font;
int currentID;
int mode; // 1 type, 2 data
#define MODE_TYPE 1
#define MODE_DATA 2
char current_text[64];
char recent_catch_request_string[4];
void send_request(int key, char *request){
Tuplet request_tuplet = TupletCString(key, request);
DictionaryIterator *iter;
app_message_outbox_begin(&iter);
dict_write_tuplet(iter, &request_tuplet);
dict_write_end(iter);
app_message_outbox_send();
}
void inbox_received_handler(DictionaryIterator *iter, void *context){
//if(recent_catch != 0){
snprintf(recent_catch_request_string, 4, "%i", recent_catch);
send_request(DATA, recent_catch_request_string);
//}
}
void update_selection() {
gbitmap_destroy( poke_image );
if(DEBUG == 1) APP_LOG(APP_LOG_LEVEL_DEBUG, "update selection 1");
if (currentID < 1){ currentID = 1; }
if (currentID > NUM_POKEMON){ currentID = NUM_POKEMON; }
if(DEBUG == 1) APP_LOG(APP_LOG_LEVEL_DEBUG, "update selection 2");
if(pokedex[currentID] == 1){
if(DEBUG == 1) APP_LOG(APP_LOG_LEVEL_DEBUG, "update selection 3 - caught");
if (mode == MODE_TYPE){ // 1 type, 2 data
text_layer_set_text( text_layer, poke_names[currentID-1]);
}
else{
text_layer_set_text( text_layer, poke_info[currentID-1]);
}
poke_image = gbitmap_create_with_resource( poke_images[currentID-1] );
}
else{
if(DEBUG == 1) APP_LOG(APP_LOG_LEVEL_DEBUG, "update selection 3 - not caught");
snprintf(current_text, sizeof(current_text), " %03d \n\n", currentID);
strncat(current_text, "???", 4);
text_layer_set_text( text_layer, current_text);
poke_image = gbitmap_create_with_resource( RESOURCE_ID_UNKNOWN );
}
bitmap_layer_set_bitmap( poke_image_layer, poke_image);
if(DEBUG == 1) APP_LOG(APP_LOG_LEVEL_DEBUG, "update selection 4");
}
void draw_fake_status_bar(Layer *layer, GContext *ctx){
if(DEBUG == 1) APP_LOG(APP_LOG_LEVEL_DEBUG, "draw fake status bar 1");
#ifdef PBL_COLOR
graphics_context_set_fill_color(ctx, GColorRed);
#else
graphics_context_set_fill_color(ctx, GColorBlack);
#endif
if(DEBUG == 1) APP_LOG(APP_LOG_LEVEL_DEBUG, "draw fake status bar 2");
graphics_fill_rect(ctx, GRect(0,0,144,16), 0, GCornerNone);
if(DEBUG == 1) APP_LOG(APP_LOG_LEVEL_DEBUG, "draw fake status bar 3");
graphics_context_set_text_color(ctx, GColorWhite);
graphics_context_set_stroke_color(ctx, GColorWhite);
graphics_draw_text(ctx, "Pokedex", fonts_get_system_font(FONT_KEY_GOTHIC_14), GRect(0,-2,144,16), GTextOverflowModeFill, GTextAlignmentCenter, NULL);
graphics_draw_text(ctx, num_caught_string, fonts_get_system_font(FONT_KEY_GOTHIC_14), GRect(0,-2,144,16), GTextOverflowModeFill, GTextAlignmentRight, NULL);
if(DEBUG == 1) APP_LOG(APP_LOG_LEVEL_DEBUG, "draw fake status bar 4");
for(int i = 0; i < 144; i++){
if(i%2 == 0) graphics_draw_pixel(ctx, GPoint(i, 15));
}
if(DEBUG == 1) APP_LOG(APP_LOG_LEVEL_DEBUG, "draw fake status bar 5");
}
///////////////////////// INPUT /////////////////////
void up_click_handler(ClickRecognizerRef recognizer, void *context)
{
currentID = currentID -1;
update_selection();
}
void down_click_handler(ClickRecognizerRef recognizer, void *context)
{
currentID = currentID +1;
update_selection();
}
void select_click_handler(ClickRecognizerRef recognizer, void *context)
{
if (mode == MODE_TYPE)
mode = MODE_DATA;
else
mode = MODE_TYPE;
update_selection();
}
void long_select(ClickRecognizerRef recognizer, void *context){
send_request(DATA, "9000");
persist_delete(POKEDEX_DATA);
vibes_short_pulse();
}
void long_up(ClickRecognizerRef recognizer, void *context){
//currentID -= 25;
for(int i = currentID; i > 0; i--){
currentID--;
if(pokedex[currentID] == 1) break;
}
update_selection();
}
void long_down(ClickRecognizerRef recognizer, void *context){
//currentID += 25;
for(int i = currentID; i < 152; i++){
currentID++;
if(pokedex[currentID] == 1) break;
}
update_selection();
}
void click_config_provider(void *context) {
window_single_click_subscribe(BUTTON_ID_SELECT, select_click_handler);
window_single_click_subscribe(BUTTON_ID_UP, up_click_handler);
window_single_click_subscribe(BUTTON_ID_DOWN, down_click_handler);
window_long_click_subscribe(BUTTON_ID_UP, 300, long_up, NULL);
window_long_click_subscribe(BUTTON_ID_DOWN, 300, long_down, NULL);
}
///////////////////////// INIT /////////////////////
void handle_init(void) {
app_message_open(app_message_inbox_size_maximum(), app_message_outbox_size_maximum());
app_message_register_inbox_received(inbox_received_handler);
if(persist_exists(POKEDEX_DATA)){
persist_read_data(POKEDEX_DATA, array, 19);
for(int i = 0; i < 152; i++){
pokedex[i] = ( array[ i / 8 ] >> ( i % 8 ) ) & 1;
if(pokedex[i] == 1) num_caught++;
}
}
else{
for(int i = 0; i < 152; i++){
pokedex[i] = 0;
}
}
int launch = launch_get_args();
if(launch == 0) {
currentID = 1;
}
else{
recent_catch = launch;
if(launch < 1000){
if(pokedex[launch] == 0){
num_caught++;
pokedex[launch] = 1;
}
currentID = launch;
}
else{
currentID = launch-1000;
}
}
snprintf(num_caught_string, sizeof(num_caught_string), "%d/151 ", num_caught);
mode = MODE_TYPE; //Type Mode
window = window_create();
window_set_background_color( window, GColorWhite);
#ifdef PBL_PLATFORM_APLITE
window_set_fullscreen(window, true);
#endif
window_set_click_config_provider( window, click_config_provider );
/******* TEXT LAYER ********/
text_layer = text_layer_create( GRect(4, 44+16, 140 /* width */, 98 /* height */));
custom_font = fonts_load_custom_font(resource_get_handle(RESOURCE_ID_FONT_PKMN_14));
text_layer_set_font( text_layer, custom_font);
text_layer_set_text_color( text_layer, GColorBlack);
text_layer_set_overflow_mode( text_layer, GTextOverflowModeWordWrap);
text_layer_set_background_color( text_layer, GColorClear);
text_layer_set_text_alignment( text_layer, GTextAlignmentCenter);
layer_add_child( window_get_root_layer( window ), text_layer_get_layer( text_layer ));
/////// IMAGE ////////////
poke_image = gbitmap_create_with_resource( RESOURCE_ID_IMAGE_poke1);
poke_image_layer = bitmap_layer_create( GRect( 70, 10+16, 58, 58) );
bitmap_layer_set_alignment( poke_image_layer, GAlignCenter);
bitmap_layer_set_background_color( poke_image_layer, GColorClear);
bitmap_layer_set_compositing_mode( poke_image_layer, GCompOpSet);
bitmap_layer_set_bitmap( poke_image_layer, poke_image);
layer_add_child( window_get_root_layer( window ), bitmap_layer_get_layer( poke_image_layer ));
/////// UI TOP ////////////
top_bar_image = gbitmap_create_with_resource( RESOURCE_ID_UI_TOP );
top_bar_layer = bitmap_layer_create(GRect( 0, 16, 144, 11));
bitmap_layer_set_bitmap( top_bar_layer, top_bar_image);
layer_add_child( window_get_root_layer( window ), bitmap_layer_get_layer( top_bar_layer ));
/////// UI BOTTOM ////////////
bottom_bar_image = gbitmap_create_with_resource( RESOURCE_ID_UI_BOTTOM );
bottom_bar_layer = bitmap_layer_create( GRect( 0, 168-11, 144, 11) );
bitmap_layer_set_bitmap( bottom_bar_layer, bottom_bar_image);
layer_add_child( window_get_root_layer( window ), bitmap_layer_get_layer( bottom_bar_layer ));
fake_status_bar = layer_create(GRect(0,0,144,16));
layer_set_update_proc(fake_status_bar, draw_fake_status_bar);
layer_add_child( window_get_root_layer( window ), fake_status_bar );
update_selection();
window_stack_push( window, false );
for(int i = 0; i < 152; i++){
if(pokedex[i] == 1){
array[ i / 8 ] |= 1 << ( i % 8 );
}
else{
array [ i / 8] &= ~( 1 << ( i % 8 ) );
}
}
persist_write_data(POKEDEX_DATA, array, 19);
}
///////////////////////// DE INIT /////////////////////
void handle_deinit(void) {
app_message_deregister_callbacks();
bitmap_layer_destroy( poke_image_layer );
bitmap_layer_destroy( top_bar_layer );
bitmap_layer_destroy( bottom_bar_layer );
gbitmap_destroy( poke_image );
gbitmap_destroy( top_bar_image );
gbitmap_destroy( bottom_bar_image );
text_layer_destroy( text_layer );
layer_destroy( fake_status_bar );
window_destroy( window );
fonts_unload_custom_font( custom_font );
}
///////////////////////// MAIN /////////////////////
int main(void) {
handle_init();
app_event_loop();
handle_deinit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment