diff --git a/config.json b/config.json index 812b493..04e46a1 100644 --- a/config.json +++ b/config.json @@ -36,11 +36,11 @@ { "json-save": true, "json-save-directory": "local/scans", - "barcode": "015700057605" + "barcode": "645175525233" }, "api": { - "user-agent": "Pudding making game for http://shampoo.ooo", + "user-agent": "Custom pudding creation game for http://shampoo.ooo", "nutronix-app-id": "ea0f2e7e", "nutronix-app-key": "39218dde526dd3349daa028deda518ae", "edamam-app-id": "c23b139f", diff --git a/src/Item.cpp b/src/Item.cpp index 850cea4..01c2734 100644 --- a/src/Item.cpp +++ b/src/Item.cpp @@ -38,6 +38,11 @@ const std::vector>& Item::get_image_textures() cons return image_textures; } +const std::shared_ptr& Item::get_active_image_texture() const +{ + return get_image_textures()[current_image_index]; +} + void Item::set_brand_name(const std::string& name) { set_text_property(name, brand_name, "brand name"); @@ -79,6 +84,11 @@ std::string Item::get_full_name() const return name; } +void Item::increment_image_index(int increment) +{ + current_image_index = sfw::mod(current_image_index + increment, static_cast(get_image_textures().size())); +} + Item::~Item() { SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "destroying item %p", this); diff --git a/src/Item.hpp b/src/Item.hpp index 79f3875..a3db4de 100644 --- a/src/Item.hpp +++ b/src/Item.hpp @@ -5,6 +5,7 @@ #include #include #include +#include class Item { @@ -12,12 +13,14 @@ class Item private: std::vector> image_textures; std::string brand_name = "", product_name = "", upc = ""; + int current_image_index = 0; void set_text_property(const std::string&, std::string&, const std::string&); static void destroy_texture(SDL_Texture*); public: void add_image_texture(SDL_Texture*); const std::vector>& get_image_textures() const; + const std::shared_ptr& get_active_image_texture() const; void set_brand_name(const std::string&); const std::string& get_brand_name() const; void set_product_name(const std::string&); @@ -25,6 +28,7 @@ public: void set_upc(const std::string&); const std::string& get_upc() const; std::string get_full_name() const; + void increment_image_index(int = 1); ~Item(); }; diff --git a/src/Pudding.cpp b/src/Pudding.cpp index 210e92e..ee98499 100644 --- a/src/Pudding.cpp +++ b/src/Pudding.cpp @@ -25,9 +25,30 @@ int main() Pudding::Pudding() { + get_delegate().subscribe(&Pudding::respond, this); load_sdl_context(); } +void Pudding::respond(SDL_Event& event) +{ + if (get_delegate().compare(event, "up")) + { + increment_item_index(); + } + else if (get_delegate().compare(event, "right")) + { + get_current_item().increment_image_index(); + } + else if (get_delegate().compare(event, "down")) + { + increment_item_index(-1); + } + else if (get_delegate().compare(event, "left")) + { + get_current_item().increment_image_index(-1); + } +} + void Pudding::load_sdl_context() { super::load_sdl_context(); @@ -252,6 +273,16 @@ SDL_Texture* Pudding::texture_from_image_url(const std::string& url) } } +void Pudding::increment_item_index(int increment) +{ + current_item_index = sfw::mod(current_item_index + increment, static_cast(items.size())); +} + +Item& Pudding::get_current_item() +{ + return items[current_item_index]; +} + void Pudding::update() { get_root()->configuration.load("config.json"); @@ -263,9 +294,6 @@ void Pudding::update() } if (items.size() > 0) { - if (items.front().get_image_textures().size() > 0) - { - SDL_RenderCopyF(get_renderer(), items.front().get_image_textures().back().get(), nullptr, nullptr); - } + SDL_RenderCopyF(get_renderer(), get_current_item().get_active_image_texture().get(), nullptr, nullptr); } } diff --git a/src/Pudding.hpp b/src/Pudding.hpp index 855a6f1..8c29cb3 100644 --- a/src/Pudding.hpp +++ b/src/Pudding.hpp @@ -32,9 +32,11 @@ struct Pudding : Game typedef Game super; std::string current_barcode; std::vector items; + int current_item_index; Pudding(); void load_sdl_context(); + void respond(SDL_Event&); void add_item(const std::string&); void incorporate_open_food_api(Item&); void incorporate_nutronix_api(Item&); @@ -44,6 +46,8 @@ struct Pudding : Game void curl_get_bytes(const std::string& url, std::vector&, const std::vector& = {}); static size_t curl_write_response(std::uint8_t*, size_t, size_t, std::vector*); SDL_Texture* texture_from_image_url(const std::string&); + void increment_item_index(int = 1); + Item& get_current_item(); void update(); std::string get_class_name() { return "Pudding"; }