#include "common.h" #include using std::cout; using std::endl; bool init_sdl(SDL_Window **window) { if (nullptr == window) { cout << "Nowhere to store window" << endl; return false; } // SDL Initialization if (SDL_Init(SDL_INIT_VIDEO) < 0) { cout << "SDL could not initialize! SDL - " << SDL_GetError() << endl; return false; } (*window) = SDL_CreateWindow("DMM Display", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_SHOWN); if (nullptr == (*window)) { cout << "Window could not be created! SDL - " << SDL_GetError() << endl; SDL_Quit(); return false; } if (0 != TTF_Init()) { cout << "Could not init TTF! SDL - "<< SDL_GetError() << endl; SDL_DestroyWindow(*window); SDL_Quit(); return false; } return true; } void quit_sdl(SDL_Window *window) { TTF_Quit(); SDL_DestroyWindow(window); SDL_Quit(); } bool check_quit() { SDL_Event e; while(SDL_PollEvent(&e)) { if (SDL_QUIT == e.type) { return true; } } return false; } void calc_text(std::string& text, std::vector& readings) { if (readings.empty()) { return; } Reading last = *(readings.rbegin()); text = ""; if (last.is_neg) { text += '-'; } else { text += ' '; } if (last.is_inf) { text += " O L "; } else { for (int i = 0; i < 4; ++i) { text += ('0' + last.value[i]); text += ' '; } text.erase(text.size() - 1); } if (last.dot_pos != 0) { text[last.dot_pos * 2] = '.'; } switch (last.modifier) { case -9: text += 'n'; break; case -6: text += "μ"; break; case -3: text += 'm'; break; case 0: text += ' '; break; case 3: text += 'k'; break; case 6: text += 'M'; break; default: text += ' '; break; } switch (last.type) { case Reading::reading_percent: text += "% "; break; case Reading::reading_volts: text += "V "; break; case Reading::reading_amps: text += "A "; break; case Reading::reading_ohms: text += "Ω "; break; case Reading::reading_hFE: text += "hFE"; break; case Reading::reading_hertz: text += "Hz "; break; case Reading::reading_farads: text += "F "; break; case Reading::reading_celsius: text += "°C "; break; case Reading::reading_fahrenheit: text += "°F "; break; default: text += "???"; break; } } void draw_text(const std::string& text, SDL_Surface *surface, TTF_Font *font) { SDL_Surface *txt_surface; SDL_Rect rect; SDL_Color fg; SDL_Color bg; fg.r = 0; fg.g = 0; fg.b = 0; bg.r = 255; bg.g = 255; bg.b = 255; if ((NULL == surface) || (NULL == font) || (0 == text.size())) { return; } txt_surface = TTF_RenderUTF8_Shaded(font, text.c_str(), fg, bg); rect.x = 0; rect.y = surface->h / 2 - txt_surface->h / 2; SDL_BlitSurface(txt_surface, NULL, surface, &rect); SDL_FreeSurface(txt_surface); }