Add top/bottom bars

This commit is contained in:
Tyler Perkins 2021-12-20 15:13:12 -05:00
parent 282edfc7aa
commit ad7478cb31
8 changed files with 221 additions and 2 deletions

View File

@ -395,6 +395,9 @@ void board::start(){
//call draw on the current panel
PANELS[i]->draw();
//draw the overlay panel
OVERLAY->draw();
if(fcount % 10 == 0)
std::cerr << "Frame : " << fcount << "\n";

View File

@ -13,6 +13,10 @@ dashboard::panel::panel* PANELS[] = {
};
size_t PANELS_LENGTH = sizeof(PANELS)/sizeof(PANELS[0]);
// OVERLAY
// This is a special panel that is overlayed on top of all other panels
dashboard::panel::panel* OVERLAY = new dashboard::panel::def_overlay();
// IMAGE_LOCATIONS
// Locations of all static images used
const char* IMAGE_LOCATIONS[] = {

View File

@ -76,9 +76,14 @@ constexpr int IMG_FLAGS = 0
// Be sure to include the panel definition below, then add it to the system
#include "panel/panel.hpp"
#include "panel/weather.hpp"
#include "panel/def_overlay.hpp"
extern dashboard::panel::panel* PANELS[];
extern size_t PANELS_LENGTH;
// OVERLAY
// This is a special panel that is overlayed on top of all other panels
extern dashboard::panel::panel* OVERLAY;
// DATA_DIR
// Where all resources will be
// Keep this as DATA to use the install dir set in the makefile

137
src/panel/def_overlay.cpp Normal file
View File

@ -0,0 +1,137 @@
///////////////////////////////////////////////////////////////////////////////
// Tyler Perkins
// 20-12-21
// def_overlay implementation
//
#include "def_overlay.hpp"
#include "def_overlay_config.hpp"
using namespace dashboard::panel;
///////////////////////////////////////////////////////////////////////////////
// Constructors ///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
def_overlay::def_overlay(){
std::cerr << "DEF_OVERLAY CONSTRUCTOR\n";
//dont init time on screen, its always on screen
_time_on_screen = 0;
_update_interval = std::chrono::milliseconds{DEF_OVERLAY_UPDATE_INTERVAL};
_texture = nullptr;
//let set to default, will make it so it updates the texture ASAP
//_last_update;
}
def_overlay::~def_overlay(){
std::cerr << "DEF_OVERLAY DECONSTRUCTOR\n";
if(_texture != nullptr){
SDL_DestroyTexture(_texture);
}
}
///////////////////////////////////////////////////////////////////////////////
// Draw function //////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void def_overlay::draw(){
//create the texture if this is the first time running draw
if(_texture == nullptr){
std::cerr << "IN FIRST SETUP\n";
initTexture();
update();
update_texture();
}
//check if its time to update
if((std::chrono::high_resolution_clock::now() - _last_update)
> _update_interval){
update();
update_texture();
}
SDL_RenderCopy(board::getRenderer(), _texture, NULL, NULL);
}
///////////////////////////////////////////////////////////////////////////////
// Helper functions ///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////
// Update the information of the obj
// This DOES NOT update the display
void def_overlay::update() {
std::cerr << "DEF_OVERLAY::UPDATE\n";
_last_update = std::chrono::high_resolution_clock::now();
//get current date and time
std::time_t curr_time = std::chrono::system_clock::to_time_t(
std::chrono::system_clock::now());
date_time = std::ctime(&curr_time);
}
///////////////////////////////////////
// Update the texture that is being
// displayed, based on system data
void def_overlay::update_texture() {
std::cerr << "DEF_OVERLAY::UPDATE_TEXTURE\n";
uint8_t o_red, o_green, o_blue, o_alpha;
SDL_Rect tgt;
//save the old colors
SDL_GetRenderDrawColor(board::getRenderer(), &o_red,
&o_green, &o_blue, &o_alpha);
SDL_SetRenderTarget(board::getRenderer(), _texture);
//clear the texture as pure alpha
SDL_SetRenderDrawColor(board::getRenderer(),
0x00, 0x00, 0x00, 0x00);
//set the new color
SDL_SetRenderDrawColor(board::getRenderer(),
DEF_OVERLAY_BAR_RED, DEF_OVERLAY_BAR_GREEN,
DEF_OVERLAY_BAR_BLUE, DEF_OVERLAY_BAR_ALPHA);
//draw the top rectangle
tgt.x = 0; tgt.y = 0;
tgt.w = SCREEN_WIDTH;
tgt.h = DEF_OVERLAY_BAR_HEIGHT;
SDL_RenderFillRect(board::getRenderer(), &tgt);
//draw the bottom rectangle
tgt.x = 0; tgt.y = SCREEN_HEIGHT - DEF_OVERLAY_BAR_HEIGHT;
tgt.w = SCREEN_WIDTH;
tgt.h = DEF_OVERLAY_BAR_HEIGHT;
SDL_RenderFillRect(board::getRenderer(), &tgt);
SDL_SetRenderTarget(board::getRenderer(), NULL);
//reset back to the old render color
SDL_SetRenderDrawColor(board::getRenderer(),
o_red, o_green, o_blue, o_alpha);
std::cerr << "Leaving overlay texture\n";
}
///////////////////////////////////////
// Lazy load the texture object
// This is to make sure that all of SDL
// is init before we attempt to draw anything
void def_overlay::initTexture(){
if(_texture == nullptr){
_texture = SDL_CreateTexture(board::getRenderer(),
SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_TARGET,
SCREEN_WIDTH, SCREEN_HEIGHT);
SDL_SetTextureBlendMode(_texture, SDL_BLENDMODE_BLEND);
}
}

40
src/panel/def_overlay.hpp Normal file
View File

@ -0,0 +1,40 @@
///////////////////////////////////////////////////////////////////////////////
// Tyler Perkins
// 20-12-21
// def_overlay definition
//
#pragma once
#include "panel.hpp"
#include <iostream>
#include <chrono>
#include <ctime>
#include <string>
#include <SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
namespace dashboard::panel {
class def_overlay : public panel {
public:
def_overlay();
~def_overlay();
void draw();
private:
void update();
void update_texture();
void initTexture();
std::string date_time;
std::chrono::time_point<std::chrono::high_resolution_clock> _last_update;
std::chrono::milliseconds _update_interval;
};
}
#include "../board.hpp"

View File

@ -0,0 +1,29 @@
///////////////////////////////////////////////////////////////////////////////
// Tyler Perkins
// 20-12-21
// def overlay configuration
//
#pragma once
#include <SDL.h>
namespace dashboard::panel {
//How long should we wait between updates? in ms
//Default 10 s
constexpr size_t DEF_OVERLAY_UPDATE_INTERVAL = 10000;
//Height of the bar on the top and bottom, in pixels
constexpr size_t DEF_OVERLAY_BAR_HEIGHT = 45;
//Text color
constexpr uint8_t DEF_OVERLAY_TEXT_RED = 0xCC;
constexpr uint8_t DEF_OVERLAY_TEXT_GREEN = 0xCC;
constexpr uint8_t DEF_OVERLAY_TEXT_BLUE = 0xCC;
constexpr uint8_t DEF_OVERLAY_TEXT_ALPHA = 0xFF;
constexpr uint8_t DEF_OVERLAY_BAR_RED = 0xAA;
constexpr uint8_t DEF_OVERLAY_BAR_GREEN = 0xAA;
constexpr uint8_t DEF_OVERLAY_BAR_BLUE = 0xAA;
constexpr uint8_t DEF_OVERLAY_BAR_ALPHA = 0xFF;
}

View File

@ -16,7 +16,7 @@ using namespace dashboard::panel;
weather::weather(){
std::cerr << "WEATHER CONSTRUCTOR\n";
_time_on_screen = WEATHER_DEFAULT_ON_SCREEN_TIME;
_update_interval = std::chrono::milliseconds{UPDATE_INTERVAL};
_update_interval = std::chrono::milliseconds{WEATHER_UPDATE_INTERVAL};
//let set to default, will make it so it updates the texture ASAP
//_last_update;
_rss = rss_utils::rss(WEATHER_URL_SOURCE);
@ -86,6 +86,7 @@ void weather::update_texture(){
SDL_Rect tgt;
SDL_SetRenderTarget(board::getRenderer(), _texture);
SDL_RenderClear(board::getRenderer());
//title
tgt.x = 50;

View File

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