added startup status messages for connection and map loading

This commit is contained in:
nathan 2022-10-21 17:37:39 -07:00
parent e6e372c019
commit 03fd9ec4df
7 changed files with 121 additions and 73 deletions

View file

@ -45,7 +45,6 @@ class AppData {
//
struct client *c;
bool connected;
int fd;
char pk_buf[8];
@ -57,6 +56,8 @@ class AppData {
void updateStatus();
AppData();
bool connected;
AircraftList aircraftList;
Modes modes;
@ -72,4 +73,4 @@ class AppData {
double msgRateAccumulate;
};
#endif
#endif

View file

@ -4,7 +4,7 @@
#
CXXFLAGS=-O2 -std=c++11 -g
LIBS= -lm -lSDL2 -lSDL2_ttf -lSDL2_gfx -g
LIBS= -lm -lSDL2 -lSDL2_ttf -lSDL2_gfx -lpthread -g
CXX=g++
all: viz1090

67
Map.cpp
View file

@ -36,7 +36,7 @@
#include <fstream>
#include <string>
#include <iostream>
#include <cmath>
bool Map::QTInsert(QuadTree *tree, Line *line, int depth) {
// if(depth > 25) {
@ -186,7 +186,8 @@ std::vector<Line*> Map::getLines(float screen_lat_min, float screen_lat_max, flo
return getLinesRecursive(&root, screen_lat_min, screen_lat_max, screen_lon_min, screen_lon_max);
};
Map::Map() {
void Map::load() {
FILE *fileptr;
if((fileptr = fopen("mapdata.bin", "rb"))) {
@ -205,9 +206,31 @@ Map::Map() {
fclose(fileptr);
printf("Read %d map points.\n",mapPoints_count / 2);
}
// load quad tree
if((fileptr = fopen("airportdata.bin", "rb"))) {
fseek(fileptr, 0, SEEK_END);
airportPoints_count = ftell(fileptr) / sizeof(float);
rewind(fileptr);
airportPoints = (float *)malloc(airportPoints_count * sizeof(float));
if(!fread(airportPoints, sizeof(float), airportPoints_count, fileptr)){
printf("Map read error\n");
exit(0);
}
fclose(fileptr);
printf("Read %d airport points.\n",airportPoints_count / 2);
}
int total = mapPoints_count / 2 + airportPoints_count / 2;
int processed = 0;
// load quad tree
if(mapPoints_count > 0) {
for(int i = 0; i < mapPoints_count; i+=2) {
if(mapPoints[i] == 0)
continue;
@ -248,6 +271,10 @@ Map::Map() {
// printf("inserting [%f %f] -> [%f %f]\n",currentPoint.lon,currentPoint.lat,nextPoint.lon,nextPoint.lat);
QTInsert(&root, new Line(currentPoint, nextPoint), 0);
processed++;
loaded = floor(100.0f * (float)processed / (float)total);
}
} else {
printf("No map file found\n");
@ -255,23 +282,9 @@ Map::Map() {
//
if((fileptr = fopen("airportdata.bin", "rb"))) {
fseek(fileptr, 0, SEEK_END);
airportPoints_count = ftell(fileptr) / sizeof(float);
rewind(fileptr);
airportPoints = (float *)malloc(airportPoints_count * sizeof(float));
if(!fread(airportPoints, sizeof(float), airportPoints_count, fileptr)){
printf("Map read error\n");
exit(0);
}
fclose(fileptr);
printf("Read %d airport points.\n",airportPoints_count / 2);
// load quad tree
if(airportPoints_count > 0) {
for(int i = 0; i < airportPoints_count; i+=2) {
if(airportPoints[i] == 0)
continue;
@ -312,6 +325,10 @@ Map::Map() {
//printf("inserting [%f %f] -> [%f %f]\n",currentPoint.lon,currentPoint.lat,nextPoint.lon,nextPoint.lat);
QTInsert(&airport_root, new Line(currentPoint, nextPoint), 0);
processed++;
loaded = floor(100.0f * (float)processed / (float)total);
}
} else {
printf("No airport file found\n");
@ -379,7 +396,17 @@ Map::Map() {
infile.close();
printf("done\n");
loaded = 100;
}
Map::Map() {
loaded = 0;
mapPoints_count = 0;
mapPoints = NULL;
airportPoints_count = 0;
airportPoints = NULL;
}

6
Map.h
View file

@ -128,7 +128,9 @@ public:
std::vector<MapLabel*> mapnames;
std::vector<MapLabel*> airportnames;
Map();
void load();
int loaded;
Map();
int mapPoints_count;
float *mapPoints;
@ -137,4 +139,4 @@ public:
float *airportPoints;
};
#endif
#endif

104
View.cpp
View file

@ -233,7 +233,6 @@ void View::font_init() {
//
void View::SDL_init() {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("Could not initialize SDL: %s\n", SDL_GetError());
exit(1);
@ -322,27 +321,56 @@ void View::drawStatusBox(int *left, int *top, std::string label, std::string mes
*left = *left + labelWidth + messageWidth + PAD;
}
void View::drawCenteredStatusBox(std::string label, std::string message, SDL_Color color) {
int labelWidth = (label.length() + ((label.length() > 0 ) ? 1 : 0)) * labelFontWidth;
int messageWidth = (message.length() + ((message.length() > 0 ) ? 1 : 0)) * messageFontWidth;
int left = (screen_width - (labelWidth + messageWidth)) / 2;
int top = (screen_height - labelFontHeight) / 2;
drawStatusBox(&left, &top, label, message, color);
}
void View::drawStatus() {
int left = PAD;
int top = screen_height - messageFontHeight - PAD;
char strLoc[20] = " ";
snprintf(strLoc, 20, "%3.3fN %3.3f%c", centerLat, fabs(centerLon),(centerLon > 0) ? 'E' : 'W');
drawStatusBox(&left, &top, "loc", strLoc, style.buttonColor);
if(fps) {
char fps[60] = " ";
snprintf(fps,40,"%.1f", 1000.0 / lastFrameTime);
char strPlaneCount[10] = " ";
snprintf(strPlaneCount, 10,"%d/%d", appData->numVisiblePlanes, appData->numPlanes);
drawStatusBox(&left, &top, "disp", strPlaneCount, style.buttonColor);
drawStatusBox(&left, &top, "fps", fps, style.white);
}
char strMsgRate[18] = " ";
snprintf(strMsgRate, 18,"%.0f/s", appData->msgRate);
drawStatusBox(&left, &top, "rate", strMsgRate, style.buttonColor);
char strSig[18] = " ";
snprintf(strSig, 18, "%.0f%%", 100.0 * appData->avgSig / 1024.0);
drawStatusBox(&left, &top, "sAvg", strSig, style.buttonColor);
if(!appData->connected) {
drawStatusBox(&left,&top,"init", "connecting", style.white);
} else {
char strLoc[20] = " ";
snprintf(strLoc, 20, "%3.3fN %3.3f%c", centerLat, fabs(centerLon),(centerLon > 0) ? 'E' : 'W');
drawStatusBox(&left, &top, "loc", strLoc, style.buttonColor);
char strPlaneCount[10] = " ";
snprintf(strPlaneCount, 10,"%d/%d", appData->numVisiblePlanes, appData->numPlanes);
drawStatusBox(&left, &top, "disp", strPlaneCount, style.buttonColor);
char strMsgRate[18] = " ";
snprintf(strMsgRate, 18,"%.0f/s", appData->msgRate);
drawStatusBox(&left, &top, "rate", strMsgRate, style.buttonColor);
char strSig[18] = " ";
snprintf(strSig, 18, "%.0f%%", 100.0 * appData->avgSig / 1024.0);
drawStatusBox(&left, &top, "sAvg", strSig, style.buttonColor);
}
if(map.loaded < 100) {
char loaded[20] = " ";
snprintf(loaded, 20, "loading map %d%%", map.loaded);
drawStatusBox(&left,&top,"init", loaded, style.white);
}
}
//
@ -413,7 +441,7 @@ void View::drawPlaneIcon(int x, int y, float heading, SDL_Color planeColor)
float wing = 6.0 * screen_uiscale;
float wingThick = 0.5;
float tail = 3.0 * screen_uiscale;
float tailThick = 0.15;
float tailThick = 0.35;
float bodyWidth = screen_uiscale;
float vec[3];
@ -435,15 +463,15 @@ void View::drawPlaneIcon(int x, int y, float heading, SDL_Color planeColor)
x2 = x + round(bodyWidth*out[0]);
y2 = y + round(bodyWidth*out[1]);
//trigonRGBA (renderer, x1, y1, x2, y2, x+round(-body * vec[0]), y+round(-body*vec[1]),planeColor.r,planeColor.g,planeColor.b,SDL_ALPHA_OPAQUE);
//trigonRGBA (renderer, x1, y1, x2, y2, x+round(body * vec[0]), y+round(body*vec[1]),planeColor.r,planeColor.g,planeColor.b,SDL_ALPHA_OPAQUE);
filledTrigonRGBA (renderer, x1, y1, x2, y2, x+round(-body * vec[0]), y+round(-body*vec[1]),planeColor.r,planeColor.g,planeColor.b,SDL_ALPHA_OPAQUE);
filledTrigonRGBA (renderer, x1, y1, x2, y2, x+round(body * vec[0]), y+round(body*vec[1]),planeColor.r,planeColor.g,planeColor.b,SDL_ALPHA_OPAQUE);
x1 = x + round(1*vec[0]);
y1 = y + round(1*vec[1]);
x2 = x + round(10*vec[0]);
y2 = y + round(10*vec[1]);
//x1 = x + round(8*vec[0]);
//y1 = y + round(8*vec[1]);
//x2 = x + round(16*vec[0]);
//y2 = y + round(16*vec[1]);
lineRGBA(renderer,x1,y1,x2,y2,planeColor.r,planeColor.g,planeColor.b,SDL_ALPHA_OPAQUE);
//lineRGBA(renderer,x1,y1,x2,y2, style.white.r, style.white.g, style.white.b, SDL_ALPHA_OPAQUE);
// x1 = x + round(-body*vec[0] + bodyWidth*out[0]);
// y1 = y + round(-body*vec[1] + bodyWidth*out[1]);
@ -464,10 +492,10 @@ void View::drawPlaneIcon(int x, int y, float heading, SDL_Color planeColor)
filledTrigonRGBA(renderer, x1, y1, x2, y2, x+round(body*wingThick*vec[0]), y+round(body*wingThick*vec[1]),planeColor.r,planeColor.g,planeColor.b,SDL_ALPHA_OPAQUE);
//tail
x1 = x + round(-body*.5*vec[0] - tail*out[0]);
y1 = y + round(-body*.5*vec[1] - tail*out[1]);
x2 = x + round(-body*.5*vec[0] + tail*out[0]);
y2 = y + round(-body*.5*vec[1] + tail*out[1]);
x1 = x + round(-body*.75*vec[0] - tail*out[0]);
y1 = y + round(-body*.75*vec[1] - tail*out[1]);
x2 = x + round(-body*.75*vec[0] + tail*out[0]);
y2 = y + round(-body*.75*vec[1] + tail*out[1]);
filledTrigonRGBA (renderer, x1, y1, x2, y2, x+round(-body*tailThick*vec[0]), y+round(-body*tailThick*vec[1]),planeColor.r,planeColor.g,planeColor.b,SDL_ALPHA_OPAQUE);
}
@ -1123,10 +1151,9 @@ void View::draw() {
SDL_Delay(static_cast<Uint32>(targetFrameTime - lastFrameTime));
}
//SDL_Delay(targetFrameTime);
moveMapToTarget();
zoomMapToTarget();
drawGeography();
for(int i = 0; i < 8; i++) {
@ -1144,32 +1171,16 @@ void View::draw() {
//drawMouse();
drawClick();
if(fps) {
char fps[60] = " ";
snprintf(fps,40,"%.1f", 1000.0 / lastFrameTime);
int left = 5;
int top = 5;
drawStatusBox(&left, &top, "fps", fps, style.white);
}
SDL_RenderPresent(renderer);
//if (elapsed(lastFrameTime) < targetFrameTime) {
//std::this_thread::sleep_for(fmilliseconds{targetFrameTime - elapsed(lastFrameTime)});
//std::this_thread::sleep_for(fmilliseconds{100.0f});
//}
lastFrameTime = elapsed(drawStartTime);
}
View::View(AppData *appData){
this->appData = appData;
startupState = 0;
// Display options
screen_uiscale = 1;
screen_width = 0;
@ -1190,6 +1201,9 @@ View::View(AppData *appData){
mapRedraw = 1;
selectedAircraft = NULL;
std::thread t1(&Map::load, &map);
t1.detach();
}
View::~View() {

3
View.h
View file

@ -84,6 +84,7 @@ class View {
SDL_Rect drawString(std::string text, int x, int y, TTF_Font *font, SDL_Color color);
SDL_Rect drawStringBG(std::string text, int x, int y, TTF_Font *font, SDL_Color color, SDL_Color bgColor);
void drawStatusBox(int *left, int *top, std::string label, std::string message, SDL_Color color);
void drawCenteredStatusBox(std::string label, std::string message, SDL_Color color);
void drawStatus();
void moveLabels(float dx, float dy);
@ -133,6 +134,8 @@ class View {
bool fps;
int startupState;
float maxDist;
float currentMaxDist;

View file

@ -73,6 +73,7 @@ int main(int argc, char **argv) {
AppData appData;
View view(&appData);
Input input(&appData,&view);
signal(SIGINT, SIG_DFL); // reset signal handler - bit extra safety
@ -120,15 +121,15 @@ int main(int argc, char **argv) {
view.SDL_init();
view.font_init();
go = 1;
while (go == 1)
{
appData.connect();
input.getInput();
appData.update();
view.draw();
appData.connect();
appData.update();
}
appData.disconnect();