2021-09-19 21:16:59 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Tyler Perkins
|
|
|
|
// 19-9-21
|
|
|
|
// weather panel
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "weather.hpp"
|
2021-09-19 22:23:02 +00:00
|
|
|
#include "weather_config.hpp"
|
2021-09-19 21:16:59 +00:00
|
|
|
|
|
|
|
using namespace dashboard::panel;
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Constructors ///////////////////////////////////////////////////////////////
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
weather::weather(){
|
2021-10-02 18:14:58 +00:00
|
|
|
std::cerr << "WEATHER CONSTRUCTOR\n";
|
2021-09-19 21:16:59 +00:00
|
|
|
_time_on_screen = WEATHER_DEFAULT_ON_SCREEN_TIME;
|
2021-12-20 20:13:12 +00:00
|
|
|
_update_interval = std::chrono::milliseconds{WEATHER_UPDATE_INTERVAL};
|
2021-12-20 00:12:01 +00:00
|
|
|
//let set to default, will make it so it updates the texture ASAP
|
|
|
|
//_last_update;
|
2021-09-19 22:23:02 +00:00
|
|
|
_rss = rss_utils::rss(WEATHER_URL_SOURCE);
|
2021-09-19 21:16:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
weather::~weather(){
|
2021-10-02 18:14:58 +00:00
|
|
|
std::cerr << "WEATHER DECONSTRUCTOR\n";
|
2021-12-20 00:12:01 +00:00
|
|
|
if(_texture != nullptr){
|
|
|
|
SDL_DestroyTexture(_texture);
|
|
|
|
}
|
2021-09-19 21:16:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Draw function //////////////////////////////////////////////////////////////
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void weather::draw(){
|
2021-12-20 00:12:01 +00:00
|
|
|
//create the texture if this is the first time running draw
|
2021-12-20 00:59:53 +00:00
|
|
|
if(_texture == nullptr){
|
2021-12-20 00:12:01 +00:00
|
|
|
initTexture();
|
2021-12-20 00:59:53 +00:00
|
|
|
update();
|
|
|
|
update_texture();
|
|
|
|
}
|
2021-12-20 00:12:01 +00:00
|
|
|
|
2021-09-19 21:16:59 +00:00
|
|
|
//check if its time to update
|
2021-09-19 22:23:02 +00:00
|
|
|
if((std::chrono::high_resolution_clock::now() - _last_update)
|
|
|
|
> _update_interval){
|
2021-09-19 21:16:59 +00:00
|
|
|
update();
|
2021-09-19 22:23:02 +00:00
|
|
|
|
2021-10-02 18:14:58 +00:00
|
|
|
update_texture();
|
|
|
|
}
|
2021-09-19 22:23:02 +00:00
|
|
|
|
2021-12-20 00:12:01 +00:00
|
|
|
SDL_RenderCopy(board::getRenderer(), _texture, NULL, NULL);
|
2021-09-19 21:16:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Helper functions ///////////////////////////////////////////////////////////
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2021-10-02 18:14:58 +00:00
|
|
|
///////////////////////////////////////
|
|
|
|
// Update the information of the obj
|
|
|
|
// This DOES NOT update the display
|
2021-09-19 21:16:59 +00:00
|
|
|
void weather::update() {
|
|
|
|
std::cerr << "WEATHER::UPDATE\n";
|
|
|
|
_last_update = std::chrono::high_resolution_clock::now();
|
|
|
|
|
|
|
|
//fetch updates
|
2021-09-19 22:23:02 +00:00
|
|
|
_rss.update();
|
2021-12-20 00:59:53 +00:00
|
|
|
|
|
|
|
//update internal state
|
|
|
|
|
|
|
|
current_desc = _rss.getItem(0).getDescription();
|
|
|
|
current_desc = current_desc.substr(0,current_desc.find('<'));
|
|
|
|
std::cerr << "Current Description : (\" " << current_desc << "\")\n";
|
|
|
|
|
|
|
|
tommorow_desc = _rss.getItem(1).getDescription();
|
|
|
|
tommorow_desc = tommorow_desc.substr(0,tommorow_desc.find('<'));
|
|
|
|
std::cerr << "Tommorow Description : (\" " << tommorow_desc << "\")\n";
|
2021-09-19 21:16:59 +00:00
|
|
|
}
|
2021-10-02 18:14:58 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////
|
|
|
|
// Update the texture that is being
|
|
|
|
// displayed, based on data in
|
|
|
|
// _rss
|
|
|
|
void weather::update_texture(){
|
2021-12-20 00:12:01 +00:00
|
|
|
std::cerr << "WEATHER::UPDATE_TEXTURE\n";
|
2021-12-20 00:31:10 +00:00
|
|
|
SDL_Rect tgt;
|
2021-12-20 00:12:01 +00:00
|
|
|
|
2021-12-20 00:20:03 +00:00
|
|
|
SDL_SetRenderTarget(board::getRenderer(), _texture);
|
2021-12-20 20:13:12 +00:00
|
|
|
SDL_RenderClear(board::getRenderer());
|
2021-12-20 00:12:01 +00:00
|
|
|
|
2021-12-21 21:28:57 +00:00
|
|
|
//background image
|
|
|
|
SDL_RenderCopy(board::getRenderer(),
|
|
|
|
board::getImage("sky.png"), NULL, NULL);
|
|
|
|
|
2021-12-20 00:12:01 +00:00
|
|
|
//title
|
2021-10-02 18:14:58 +00:00
|
|
|
tgt.x = 50;
|
|
|
|
tgt.y = 50;
|
2021-12-20 01:29:41 +00:00
|
|
|
TTF_SizeText(board::getFont({ "Roboto_Mono/RobotoMono-Medium.ttf", 50 }),
|
2021-12-20 01:05:50 +00:00
|
|
|
"Today's Weather",
|
2021-10-02 18:14:58 +00:00
|
|
|
&tgt.w, &tgt.h);
|
2021-12-20 00:20:03 +00:00
|
|
|
SDL_RenderCopy(board::getRenderer(),
|
2021-12-20 01:05:50 +00:00
|
|
|
board::getString("Today's Weather",
|
2021-12-20 01:29:41 +00:00
|
|
|
{ "Roboto_Mono/RobotoMono-Medium.ttf", 50 }), NULL, &tgt);
|
2021-12-20 00:12:01 +00:00
|
|
|
|
2021-12-20 00:31:10 +00:00
|
|
|
//current weather
|
2021-12-20 01:29:41 +00:00
|
|
|
TTF_SizeText(board::getFont({ "Roboto_Mono/RobotoMono-Medium.ttf", 50 }),
|
2021-12-20 00:59:53 +00:00
|
|
|
current_desc.c_str(),
|
2021-12-20 00:31:10 +00:00
|
|
|
&tgt.w, &tgt.h);
|
|
|
|
tgt.x = SCREEN_WIDTH / 2 - (tgt.w / 2);
|
|
|
|
tgt.y = SCREEN_HEIGHT / 2 - (tgt.h / 2);
|
|
|
|
SDL_RenderCopy(board::getRenderer(),
|
2021-12-20 00:59:53 +00:00
|
|
|
board::getString(current_desc.c_str(),
|
2021-12-20 01:29:41 +00:00
|
|
|
{ "Roboto_Mono/RobotoMono-Medium.ttf", 50 }), NULL, &tgt);
|
2021-12-20 00:31:10 +00:00
|
|
|
|
2021-12-20 00:59:53 +00:00
|
|
|
//tommorow's weather
|
2021-12-20 01:29:41 +00:00
|
|
|
TTF_SizeText(board::getFont({ "Roboto_Mono/RobotoMono-Medium.ttf", 50 }),
|
2021-12-20 00:59:53 +00:00
|
|
|
tommorow_desc.c_str(),
|
|
|
|
&tgt.w, &tgt.h);
|
|
|
|
tgt.x = SCREEN_WIDTH / 2 - (tgt.w / 2);
|
2021-12-20 01:29:41 +00:00
|
|
|
tgt.y = SCREEN_HEIGHT / 2 - (tgt.h / 2) + 50;
|
2021-12-20 00:59:53 +00:00
|
|
|
SDL_RenderCopy(board::getRenderer(),
|
|
|
|
board::getString(tommorow_desc.c_str(),
|
2021-12-20 01:29:41 +00:00
|
|
|
{ "Roboto_Mono/RobotoMono-Medium.ttf", 50 }), NULL, &tgt);
|
2021-12-20 00:31:10 +00:00
|
|
|
|
2021-12-20 00:12:01 +00:00
|
|
|
SDL_SetRenderTarget(board::getRenderer(), NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////
|
|
|
|
// Lazy load the texture object
|
|
|
|
// This is to make sure that all of SDL
|
|
|
|
// is init before we attempt to draw anything
|
|
|
|
void weather::initTexture(){
|
|
|
|
if(_texture == nullptr){
|
|
|
|
_texture = SDL_CreateTexture(board::getRenderer(),
|
|
|
|
SDL_PIXELFORMAT_RGBA8888,
|
|
|
|
SDL_TEXTUREACCESS_TARGET,
|
|
|
|
SCREEN_WIDTH, SCREEN_HEIGHT);
|
|
|
|
|
2021-12-20 20:56:03 +00:00
|
|
|
SDL_SetTextureBlendMode(_texture, SDL_BLENDMODE_BLEND);
|
2021-12-20 00:12:01 +00:00
|
|
|
}
|
2021-10-02 18:14:58 +00:00
|
|
|
}
|