Add texture lazy loading to fix texture copying, update weather to use new lazy loading

This commit is contained in:
Tyler Perkins 2021-12-19 19:12:01 -05:00
parent f1dcda2316
commit 0758fbdbee
6 changed files with 61 additions and 22 deletions

View File

@ -159,7 +159,8 @@ bool SDL_Texture_Wrapper::load(){
} }
tmpSurface = TTF_RenderText_Solid(fs_font, _text.c_str(), tmpSurface = TTF_RenderText_Solid(fs_font, _text.c_str(),
{ BOARD_RED, BOARD_GREEN, BOARD_BLUE }); //{ BOARD_RED, BOARD_GREEN, BOARD_BLUE });
{ 0xCC, 0xCC, 0xCC });
if(tmpSurface == NULL){ if(tmpSurface == NULL){
SDL_Log("SDL_Texture_Wrapper: Failed to create surface from string (%s): %s\n", SDL_Log("SDL_Texture_Wrapper: Failed to create surface from string (%s): %s\n",
@ -358,6 +359,7 @@ void board::start(){
SDL_RenderClear(_renderer); SDL_RenderClear(_renderer);
//PLACEHOLDER, cycle color //PLACEHOLDER, cycle color
/*
{ {
static uint8_t red = 0; static uint8_t red = 0;
static bool up = true; static bool up = true;
@ -377,11 +379,12 @@ void board::start(){
red --; red --;
} }
} }
*/
//END PLACEHOLDER //END PLACEHOLDER
//call draw on the current panel //call draw on the current panel
//PANELS[i]->draw(); PANELS[i]->draw();
if(fcount % 10 == 0) if(fcount % 10 == 0)
std::cerr << "Frame : " << fcount << "\n"; std::cerr << "Frame : " << fcount << "\n";

View File

@ -173,7 +173,7 @@ SDL_Window* dashboard::board::getWindow(){
SDL_Renderer* dashboard::board::getRenderer(){ SDL_Renderer* dashboard::board::getRenderer(){
if(_renderer == nullptr){ if(_renderer == nullptr){
_renderer = SDL_CreateRenderer(board::getWindow(), -1, _renderer = SDL_CreateRenderer(board::getWindow(), -1,
SDL_RENDERER_ACCELERATED); SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE);
if(_renderer == NULL) if(_renderer == NULL)
SDL_Log("Renderer could not be created, %s\n", SDL_GetError()); SDL_Log("Renderer could not be created, %s\n", SDL_GetError());

View File

@ -29,5 +29,6 @@ namespace dashboard::panel {
protected: protected:
SDL_Texture* _texture; SDL_Texture* _texture;
virtual void initTexture() = 0;
}; };
} }

View File

@ -15,21 +15,19 @@ using namespace dashboard::panel;
weather::weather(){ weather::weather(){
std::cerr << "WEATHER CONSTRUCTOR\n"; std::cerr << "WEATHER CONSTRUCTOR\n";
//std::cerr << "Current Renderer : " << board::getRenderer() << "\n";
_time_on_screen = WEATHER_DEFAULT_ON_SCREEN_TIME; _time_on_screen = WEATHER_DEFAULT_ON_SCREEN_TIME;
_update_interval = std::chrono::milliseconds{UPDATE_INTERVAL}; _update_interval = std::chrono::milliseconds{UPDATE_INTERVAL};
_last_update = std::chrono::high_resolution_clock::now(); //let set to default, will make it so it updates the texture ASAP
//_last_update;
_rss = rss_utils::rss(WEATHER_URL_SOURCE); _rss = rss_utils::rss(WEATHER_URL_SOURCE);
_texture = SDL_CreateTexture(board::getRenderer(),
SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_TARGET,
SCREEN_WIDTH, SCREEN_HEIGHT);
update();
} }
weather::~weather(){ weather::~weather(){
std::cerr << "WEATHER DECONSTRUCTOR\n"; std::cerr << "WEATHER DECONSTRUCTOR\n";
if(_texture != nullptr){
SDL_DestroyTexture(_texture);
}
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
@ -37,19 +35,24 @@ weather::~weather(){
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
void weather::draw(){ void weather::draw(){
std::cerr << "WEATHER::DRAW\n";
//create the texture if this is the first time running draw
if(_texture == nullptr)
initTexture();
//check if its time to update //check if its time to update
if((std::chrono::high_resolution_clock::now() - _last_update) if((std::chrono::high_resolution_clock::now() - _last_update)
> _update_interval){ > _update_interval){
std::cerr << "UPDATING WEATHER IF STATEMENT\n";
//TODO multithread this //TODO multithread this
update(); update();
update_texture(); update_texture();
} }
static SDL_Rect tgt = { 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT }; SDL_RenderCopy(board::getRenderer(), _texture, NULL, NULL);
//SDL_RenderCopy(board::getRenderer(), _texture, NULL,
//&tgt);
//TODO add this all to one texture //TODO add this all to one texture
@ -68,7 +71,6 @@ void weather::update() {
//fetch updates //fetch updates
_rss.update(); _rss.update();
} }
/////////////////////////////////////// ///////////////////////////////////////
@ -76,18 +78,49 @@ void weather::update() {
// displayed, based on data in // displayed, based on data in
// _rss // _rss
void weather::update_texture(){ void weather::update_texture(){
//SDL_SetRenderTarget(board::getRenderer(), _texture); std::cerr << "WEATHER::UPDATE_TEXTURE\n";
int ret = SDL_SetRenderTarget(board::getRenderer(), _texture);
std::cerr << "ret : " << ret << "\n";
std::cerr << "Renderer : " << board::getRenderer() << "\n";
if(ret != 0)
SDL_Log("ERROR : %s\n", SDL_GetError());
SDL_Rect tgt; SDL_Rect tgt;
//title
tgt.x = 50; tgt.x = 50;
tgt.y = 50; tgt.y = 50;
TTF_SizeText(board::getFont({ "Roboto_Mono/RobotoMono-Medium.ttf", 24 }), TTF_SizeText(board::getFont({ "Roboto_Mono/RobotoMono-Medium.ttf", 24 }),
_rss.getTitle().c_str(), _rss.getTitle().c_str(),
&tgt.w, &tgt.h); &tgt.w, &tgt.h);
SDL_RenderCopy(board::getRenderer(), std::cerr << "tgt.w : " << tgt.w << "\n";
std::cerr << "tgt.h : " << tgt.h << "\n";
std::cerr << "board::getString : " << board::getString(_rss.getTitle(),{ "Roboto_Mono/RobotoMono-Medium.ttf", 24 }) << "\n";
ret = SDL_RenderCopy(board::getRenderer(),
//board::getString(_rss.getTitle(),
board::getString(_rss.getTitle(), board::getString(_rss.getTitle(),
{ "Roboto_Mono/RobotoMono-Medium.ttf", 24 }), NULL, &tgt); { "Roboto_Mono/RobotoMono-Medium.ttf", 24 }), NULL, &tgt);
//SDL_SetRenderTarget(board::getRenderer(), NULL); std::cerr << "ret : " << ret << "\n";
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);
SDL_SetTextureBlendMode(_texture, SDL_BLENDMODE_NONE);
}
} }

View File

@ -28,6 +28,7 @@ namespace dashboard::panel {
private: private:
void update(); void update();
void update_texture(); void update_texture();
void initTexture();
rss_utils::rss _rss; rss_utils::rss _rss;
std::chrono::time_point<std::chrono::high_resolution_clock> _last_update; std::chrono::time_point<std::chrono::high_resolution_clock> _last_update;

View File

@ -16,5 +16,6 @@ namespace dashboard::panel {
//How long should we wait between updates? in ms //How long should we wait between updates? in ms
//Default 1 hour //Default 1 hour
constexpr size_t UPDATE_INTERVAL = 3600000; //constexpr size_t UPDATE_INTERVAL = 3600000;
constexpr size_t UPDATE_INTERVAL = 5000;
} }