#include "WidgetApplication.h" #include #include #include #include "CursorTask.h" #include "Widget.h" #include #include #include #include bool is_existing( const std::string & file ) { std::ifstream fichier( file.c_str() ); return !fichier.fail(); } void takescreenshot( SDL_Surface* screen ) { std::string namebegin = "/documents/widget/scrsh"; std::string nameend = ".bmp.tns"; std::string namemiddle = "000"; bool screenshotdone = false; int i=0; do { std::string fullname = namebegin + namemiddle + nameend; if (!is_existing( fullname )) { SDL_SaveBMP( screen, fullname.c_str() ); screenshotdone = true; } else { i++; namemiddle = std::to_string( i ); if (namemiddle.length()==1) { namemiddle = "00" + namemiddle; }; if (namemiddle.length()==2) { namemiddle = "0" + namemiddle; }; } } while (!screenshotdone); } WidgetApplication::WidgetApplication() { if(SDL_Init(SDL_INIT_VIDEO) == -1) { printf("Couldn't initialize SDL: %s\n", SDL_GetError()); exit(EXIT_FAILURE); } screen = SDL_SetVideoMode(320, 240, 16, SDL_SWSURFACE); if(screen == NULL) { printf("Couldn't initialize display: %s\n", SDL_GetError()); SDL_Quit(); exit(EXIT_FAILURE); } currentfont = nSDL_LoadFont(NSDL_FONT_THIN, 0, 0, 0); mouse = new CursorTask(); } WidgetApplication::~WidgetApplication() { nSDL_FreeFont( currentfont ); SDL_FreeSurface( screen ); if (background_image) SDL_FreeSurface( background_image ); delete mouse; rootwidgets.clear(); SDL_Quit(); printf("Exited cleanly\n"); } void WidgetApplication::addchild( Widget *root ) { rootwidgets.push_back( root ); } void WidgetApplication::render( void ) { if (!uniform_background && !background_wallpaper) SDL_FillRect(screen, 0, 0x0000); if (uniform_background && !background_wallpaper) SDL_FillRect( screen, 0, rgb_background); if (!uniform_background && background_wallpaper) SDL_BlitSurface( background_image, NULL, screen, &position_background); for (auto& c : rootwidgets ) c->render( screen, currentfont ); mouse->render( screen ); SDL_Flip(screen); } void WidgetApplication::logic( void ) { mouse->logic(); // This is to take a screenshot to be strore in the Widget folder. if (isKeyPressed(KEY_NSPIRE_CTRL) && isKeyPressed(KEY_NSPIRE_PERIOD)) { takescreenshot( screen ); } for (auto& c : rootwidgets ) c->logic( mouse ); } void WidgetApplication::setuniformbackgroundcolor( Uint8 r, Uint8 g, Uint8 b) { uniform_background = true; background_wallpaper = false; r_background = r; g_background = g; b_background = b; rgb_background = SDL_MapRGB(screen->format, r_background, g_background, g_background); } void WidgetApplication::setbackgroundpicture( char *filename ) { uniform_background = false; background_wallpaper = true; position_background.x = 0; position_background.y = 0; position_background.w = 320; position_background.h = 240; background_image = IMG_Load( filename ); }