int main( int argc, char* args[] ) { //Start up SDL and create window if( !init() ) { printf( "Failed to initialize!\n" ); } else { //Load media if( !loadMedia() ) { printf( "Failed to load media!\n" ); } else { //Main loop flag bool quit = false; //Event handler SDL_Event e; //While application is running while( !quit ) { //Handle events on queue while( SDL_PollEvent( &e ) != 0 ) { //User requests quit if( e.type == SDL_QUIT ) { quit = true; } //Handle window events gWindow.handleEvent( e ); } //Only draw when not minimized if( !gWindow.isMinimized() ) { //Clear screen SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF ); SDL_RenderClear( gRenderer ); //Render text textures gSceneTexture.render( ( gWindow.getWidth() - gSceneTexture.getWidth() ) / 2, ( gWindow.getHeight() - gSceneTexture.getHeight() ) / 2 ); //Update screen SDL_RenderPresent( gRenderer ); } } } } //Free resources and close SDL close(); return 0; }
Boolean UPropertyInspector::SelectInspectorWindow( DMElement* inElement) { // Validate pointers. ValidateObject_(inElement); // See if there's an inspector for this element. PIInspectorTable* table = (PIInspectorTable*) inElement->FindUserInterfaceObject(PIInspectorTable::object_ID); if (table == nil) return false; // There's a table. Select its window. LWindow* window = LWindow::FetchWindowObject(GetWindowFromPort(table->GetMacPort())); ValidateObject_(window); window->Select(); return true; }
//=============== // PUBLIC SLOTS //=============== void LWindowManager::NewWindow(WId win, bool requested){ //Verify that this window can/should be managed first //if(DEBUG){ qDebug() << "New Window:" << LWM::SYSTEM->WM_ICCCM_GetClass(win); } QString wclass = LWM::SYSTEM->WM_ICCCM_GetClass(win); if( wclass.contains("lumina-wm",Qt::CaseInsensitive) ){ return; } //just in case: prevent recursion else{ bool ok = (wclass.isEmpty() ? false : LWM::SYSTEM->WM_ManageWindow(win, requested) ); if(!ok){ //See if this window is just a transient pointing to some other window WId tran = LWM::SYSTEM->WM_ICCCM_GetTransientFor(win); if(tran!=win && tran!=0){ win = tran; ok = LWM::SYSTEM->WM_ManageWindow(win); } } if(!ok){ return; } } if(DEBUG){ qDebug() << "New Managed Window:" << LWM::SYSTEM->WM_ICCCM_GetClass(win); } LWindow *lwin = new LWindow(win); connect(lwin, SIGNAL(Finished(WId)), this, SLOT(FinishedWindow(WId)) ); WINS << lwin; if(lwin->hasFrame()){ lwin->frame()->windowChanged(LWM::Show); //Make sure to show it right away }else{ LWM::SYSTEM->WM_ShowWindow(win); //just map the window right now } }
void PIInspectorTable::ListenToMessage( MessageT inMessage, void* ioParam) { switch (inMessage) { // Grow zone: Property inspector is expendable, close it if low on memory. case msg_GrowZone: LWindow* window; window = LWindow::FetchWindowObject(GetWindowFromPort(GetMacPort())); ValidateObject_(window); window->DoClose(); break; // Detach PI button: Detach this inspector from the selection. case 'DTCH': DetachFromSelection(); break; // Other messages: Pass along to table. default: OVTable::ListenToMessage(inMessage, ioParam); } }
void TutorialBuilderApp::OpenDocument(FSSpec* inMacFSSpec) { short srcRefNum = ::FSpOpenResFile(inMacFSSpec, fsRdPerm); LStr255 dstName = "Galactica Tutorial"; //inMacFSSpec->name; // dstName += ".rsrc"; FSSpec dstSpec; ::FSMakeFSSpec(inMacFSSpec->vRefNum, inMacFSSpec->parID, dstName, &dstSpec); ::FSpCreateResFile(&dstSpec, 'TrŽ5', 'rsrc', 0); short dstRefNum = ::FSpOpenResFile(&dstSpec, fsWrPerm); ::UseResFile(srcRefNum); short ppobCount = ::Count1Resources('PPob'); mPrevPageHadButton = false; for (int i = 1; i <= ppobCount; i++) { Handle resH = ::Get1IndResource('PPob', i); ResIDT theID; ResType theType; Str255 theResName; ::GetResInfo(resH, &theID, &theType, theResName); if (theID > 0) { // wouldn't work with many negative numbers anyway LWindow* wind = LWindow::CreateWindow(theID, this); wind->SetDescriptor(theResName); wind->Show(); wind->UpdatePort(); SavePageToResFile(wind, dstRefNum); delete wind; } ::ReleaseResource(resH); } ::CloseResFile(srcRefNum); ::CloseResFile(dstRefNum); }
void UPropertyInspector::InspectSelection( DMSelection* inSelection, LCommander* inUndoHost, Boolean inUseHostAsCommander, Boolean inPlaceInWindowMenu) { // Validate pointers. if (inSelection != nil) ValidateObject_(inSelection); if (inUndoHost != nil) ValidateObject_(inUndoHost); // See if there's a free-floating inspector window for this object. if (inSelection != nil) { if (inSelection->GetSelectedElements().GetCount() >= 1) { DMElement* element = nil; inSelection->GetSelectedElements().FetchItemAt(1, &element); ValidateObject_(element); if (SelectInspectorWindow(element)) return; } } // Create an inspector window if none is already open. Boolean createdWindow = false; if (sInspectorWindow == nil) { sInspectorWindow = CreateInspectorWindow( (inUseHostAsCommander) ? inUndoHost : nil,inPlaceInWindowMenu); createdWindow = true; } // Sanity check: Make sure we now have an inspector window. ValidateObject_(sInspectorWindow); LWindow* newWindow = sInspectorWindow; // Configure the inspector to use the named selection. ConfigureInspectorWindow(inSelection, inUndoHost); // Position & show the window. if (createdWindow) { newWindow->DoSetZoom(true); newWindow->ProcessCommand(cmd_PositionWindow); newWindow->Show(); } else newWindow->Select(); }
int main(int argc, char* args[]) { printf("commands: \n"); printf("(i) integrate \n"); printf("(z) zoom \n"); printf("(x) delete \n"); //Start up SDL and create window if (!init()) { printf("Failed to initialize!\n"); } else { //Load media if (!loadMedia()) { printf("Failed to load media!\n"); } else { //Main loop flag bool quit = false; //Event handler SDL_Event e; //Spectra spectra("export.txt"); if (!gWindow.isMinimized()){ gWindow.draw(); } //While application is running while (!quit){ //Handle events on queue while (SDL_PollEvent(&e) != 0){ //User requests quit if (e.type == SDL_QUIT){ quit = true; } gWindow.handleEvent(e); } } } } //Free resources and close SDL close(); return 0; }
bool init() { //Initialization flag bool success = true; //Initialize SDL if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) { printf( "SDL could not initialize! SDL Error: %s\n", SDL_GetError() ); success = false; } else { //Set texture filtering to linear if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) ) { printf( "Warning: Linear texture filtering not enabled!" ); } //Create window if( !gWindow.init() ) { printf( "Window could not be created! SDL Error: %s\n", SDL_GetError() ); success = false; } else { //Create renderer for window gRenderer = gWindow.createRenderer(); if( gRenderer == NULL ) { printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() ); success = false; } else { //Initialize renderer color SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF ); //Initialize PNG loading int imgFlags = IMG_INIT_PNG; if( !( IMG_Init( imgFlags ) & imgFlags ) ) { printf( "SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError() ); success = false; } } } } return success; }
bool init() { bool success = true; if (SDL_Init( SDL_INIT_VIDEO) < 0) { printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError()); success = false; } else { if (!SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1")) { printf("Warning: Linear texture filtering not enabled!"); } if (!gWindow.init()) { printf("Window could not be created! SDL Error: %s\n", SDL_GetError()); success = false; } else { gRenderer = gWindow.createRenderer(); if (gRenderer == NULL) { printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError()); success = false; } else { SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF); int imgFlags = IMG_INIT_PNG; if (!(IMG_Init(imgFlags) & imgFlags)) { printf( "SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError()); success = false; } if (TTF_Init() == -1) { printf("SDL_ttf could not initialize! SDL_ttf Error: %s\n", TTF_GetError()); success = false; } } } } return success; }
void close() { gSceneTexture.free(); gHelp.free(); gPtrHelp = NULL; board.freeBoard(); for (int i = 0; i < NUMBER_OF_TILE * 4; ++i) { gTile[i].freeButton(); } for (int i = 0; i < NUMBER_OF_BUTTON; ++i) { gButton[i].freeButton(); } TTF_CloseFont(gFont); gFont = NULL; SDL_DestroyRenderer(gRenderer); gRenderer = NULL; gWindow.free(); TTF_Quit(); IMG_Quit(); SDL_Quit(); }
void CProgressMac::SetWindowTitle(const char * title) { LStr255 ptitle; if (title) ptitle = title; fWindow->SetDescriptor(ptitle); }
void CProgressMac::SetCancelCallback(PW_CancelCallback incancelcb, /* Callback function for cancel */ void * incancelClosure) { fCancelcb = incancelcb; fCancelClosure = incancelClosure; LButton * cancelButton = (LButton *)fWindow->FindPaneByID('CNCL'); cancelButton->AddListener(this); }
void PIInspectorTable::DetachFromSelection() { // Validate pointers. ValidateThis_(); // Make sure we're currently inspecting an object. // If not, the detach command is ignored. if (mElement == nil) return; ValidateObject_(mElement.GetObject()); // Disable the detach button. if (mDetachPIButton != nil) { ValidateObject_(mDetachPIButton); mDetachPIButton->Disable(); } // Disconnect from selection. mFollowSelection = nil; mIsDetached = true; // Retitle the window to match our object's title. LWindow* window = LWindow::FetchWindowObject(GetWindowFromPort(GetMacPort())); ValidateObject_(window); LStr255 displayableName; mElement->GetDisplayableName(displayableName); if (displayableName.Length() > 0) window->SetDescriptor(displayableName); // Tell the property inspector manager that we're no longer following selection. LWindow* myWindow = LWindow::FetchWindowObject(GetWindowFromPort(GetMacPort())); ValidateObject_(myWindow); UPropertyInspector::ClosingInspectorWindow(myWindow); }
void PIInspectorTable::FinishCreateSelf() { // Validate pointers. ValidateThis_(); // Do inherited initialization. OVTable::FinishCreateSelf(); // Set up default column widths. InsertCols(2, 0, nil, 0, false); SetColWidth(130, 1, 1); SetColWidth(150, 2, 2); // Find inspected object caption. LWindow* window = LWindow::FetchWindowObject(GetWindowFromPort(GetMacPort())); ValidateObject_(window); mInspectedObjectCaption = dynamic_cast<LStaticText*>(window->FindPaneByID(Pane_InspectedObject)); if (mInspectedObjectCaption != nil) ValidateObject_(mInspectedObjectCaption); // Find detach selection button. mDetachPIButton = (dynamic_cast<LBevelButton*>(window->FindPaneByID('DTCH'))); if (mDetachPIButton != nil) { ValidateObject_(mDetachPIButton); mDetachPIButton->AddListener(this); } // Listen to the grow zone. LGrowZone* growZone = LGrowZone::GetGrowZone(); ThrowIfNil_(growZone); growZone->AddListener(this); }
int main() { if(!init()) { printf("Failed to initialize!\n"); return -1; } if(!loadMedia()) { printf("Failed to load media!\n"); return -1; } bool quit = false; SDL_Event e; //While application is running while(!quit) { //Handle events on queue while(SDL_PollEvent(&e) != 0) { //User request quit if(e.type == SDL_QUIT) { quit = true; } //Handle window events g_window.handleEvent(e); } //Only draw when not minimized if(!g_window.isMinimized()) { //Clear screen SDL_SetRenderDrawColor(g_renderer, 0xFF, 0xFF, 0xFF, 0xFF); SDL_RenderClear(g_renderer); //Render text textures g_sceneTexture.render((g_window.getWidth() - g_sceneTexture.getWidth()) / 2, (g_window.getHeight() - g_sceneTexture.getHeight()) / 2 ); //Update screen SDL_RenderPresent(g_renderer); } } close(); return 0; }
void RESession::ShowCantOpenDialog() { // Show the can't open dialog. StApplicationContext appContext; StDialogHandler dialog(PPob_CantOpenResource, 0); LWindow* window = dialog.GetDialog(); ValidateObject_(dialog.GetDialog()); window->Show(); // Wait until OK is hit. MessageT message; do { message = dialog.DoDialog(); } while (message == msg_Nothing); }
void close() { //Destroy window gWindow.free(); //Deallocate bounds delete[] gDisplayBounds; gDisplayBounds = NULL; //Quit SDL subsystems SDL_Quit(); }
void close() { //Free loaded images gSceneTexture.free(); //Destroy window SDL_DestroyRenderer( gRenderer ); gWindow.free(); //Quit SDL subsystems IMG_Quit(); SDL_Quit(); }
void close() { //Free loadded images g_sceneTexture.free(); //Destroy window SDL_DestroyRenderer(g_renderer); g_window.free(); //Quit SDL subsystem TTF_Quit(); IMG_Quit(); SDL_Quit(); }
Boolean CProfileManager::DoNewProfileDialog(char *outName, UInt32 bufSize) { Boolean confirmed; StDialogHandler theHandler(dlog_NewProfile, LCommander::GetTopCommander()); LWindow *theDialog = theHandler.GetDialog(); ThrowIfNil_(theDialog); LEditText *responseText = dynamic_cast<LEditText*>(theDialog->FindPaneByID('Name')); ThrowIfNil_(responseText); theDialog->SetLatentSub(responseText); theDialog->Show(); theDialog->Select(); while (true) // This is our modal dialog event loop { MessageT hitMessage = theHandler.DoDialog(); if (hitMessage == msg_OK) { Str255 pStr; UInt32 outLen; responseText->GetDescriptor(pStr); outLen = pStr[0] >= bufSize ? bufSize - 1 : pStr[0]; memcpy(outName, &pStr[1], outLen); outName[outLen] = '\0'; confirmed = PR_TRUE; break; } else if (hitMessage == msg_Cancel) { confirmed = PR_FALSE; break; } } return confirmed; }
void UMemoryUtils::ShowLowMemoryAlert() { if (sShowingAlert) return; sShowingAlert = true; try { // Consider this a critical section, but make sure // there's a reasonable amount of memory left. StCriticalSection crit; // Show the can't open dialog. StApplicationContext appContext; StDialogHandler dialog(PPob_LowMemory, LCommander::GetTopCommander()); LWindow* window = dialog.GetDialog(); ValidateObject_(dialog.GetDialog()); window->Show(); // Wait until OK is hit. MessageT message; do { message = dialog.DoDialog(); } while (message == msg_Nothing); } catch(...) { } sShowingAlert = false; }
void close() { //Destroy window SDL_DestroyRenderer(gRenderer); //SDL_DestroyWindow(gWindow); gWindow.free(); //gWindow = NULL; gRenderer = NULL; //Quit SDL subsystems SDL_Quit(); }
//cierra el programa void close() { //Free loaded images gTextTexture.free(); //Free global font TTF_CloseFont( gFont ); gFont = NULL; //Destroy window SDL_DestroyRenderer( gRenderer ); gWindow.free(); gRenderer = NULL; //Quit SDL subsystems TTF_Quit(); IMG_Quit(); SDL_Quit(); }
bool init() { //Initialization flag bool success = true; //Initialize SDL if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) { printf( "SDL could not initialize! SDL Error: %s\n", SDL_GetError() ); success = false; } else { //Set texture filtering to linear if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) ) { printf( "Warning: Linear texture filtering not enabled!" ); } //Get number of displays gTotalDisplays = SDL_GetNumVideoDisplays(); if( gTotalDisplays < 2 ) { printf( "Warning: Only one display connected!" ); } //Get bounds of each display gDisplayBounds = new SDL_Rect[ gTotalDisplays ]; for( int i = 0; i < gTotalDisplays; ++i ) { SDL_GetDisplayBounds( i, &gDisplayBounds[ i ] ); } //Create window if( !gWindow.init() ) { printf( "Window could not be created!\n" ); success = false; } } return success; }
/*================================= DoSizeDialog ==================================*/ Boolean PTFontSizeDialog::DoSizeDialog( SInt16 inSize, SInt16 *outSize ) { try { /*************************** ensure that the window is created properly (Window Mgr bug) ****************************/ StSaveGWorld aSaver; ::SetGDevice( ::GetMainDevice() ); /*************************** create the dialog ****************************/ StApplicationContext appContext; // must load from application file StDialogHandler theHandler( PPob_TextSizeDialog, LCommander::GetTopCommander() ); LWindow * theWindow = theHandler.GetDialog(); LEditText * sizeField = dynamic_cast<LEditText*> (theWindow->FindPaneByID( PaneID_Size )); ThrowIf_( !sizeField ); /******************************************** initialize the values & show the window ********************************************/ sizeField->SetValue( inSize ); theWindow->Show(); /******************************************** go into an event loop ********************************************/ Boolean done = false; MessageT theMessage; SInt16 newSize; while( !done ) { theMessage = theHandler.DoDialog(); if ( theMessage == msg_Cancel ) done = true; else if ( theMessage == msg_OK ) { newSize = sizeField->GetValue(); if ( (newSize < 4) || (newSize > 512) ) { theWindow->SwitchTarget( sizeField ); sizeField->SelectAll(); SUErrors::DisplayError( err_BadFontSize ); } else done = true; } }; /******************************************** return values to the caller ********************************************/ if ( theMessage == msg_OK ) { *outSize = newSize; return( true ); } else return( false ); } catch( ... ) { SUErrors::DisplayError( err_Generic ); return( false ); } }
void CProgressMac::Hide() { fWindow->Hide(); }
void CProgressMac::Show() { fWindow->Show(); UDesktop::SelectDeskWindow(fWindow); }
int main(int argc, char* args[]) { if (!init()) { printf("Failed to initialize!\n"); } else { if (!loadMedia()) { printf("Failed to load media!\n"); } else { bool quit = false; SDL_Event e; LTexture word; SDL_Color textColor = { 0, 0, 0, 255 }; std::stringstream timeText; LButton* ptrButton[NUMBER_OF_BUTTON]; for (int i = 0; i < NUMBER_OF_BUTTON; ++i) { ptrButton[i] = &gButton[i]; } bool temp[NUMBER_OF_BUTTON] = { false }; while (!quit) { while (SDL_PollEvent(&e) != 0) { if (e.type == SDL_QUIT) { quit = true; } for (int i = 0; i < NUMBER_OF_BUTTON; ++i) { temp[i] = ptrButton[i]->handleEvent(e); } for (int i = 0; i < NUMBER_OF_TILE * 4; i++) { gTile[i].handleEvent(e); } //Handle window events gWindow.handleEvent(e); if (gWindow.isMinimized()) { gTimer.pause(); } else { gTimer.unpause(); } } logicButtons(temp); // for (int i = 0; i < 5; ++i){ // for (int g = 0; g < 6; ++g){ // board.unblock(i, g); // } // } // for (int i = 0; i < 5; ++i){ // for (int g = 0; g < 6; ++g){ // board.unblock(i, g); // } // } if (!gWindow.isMinimized()) { SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF); SDL_RenderClear(gRenderer); renderTexture(&gSceneTexture, 0, 0, gWindow.getWidth(), gWindow.getHeight()); for (int i = 0; i < NUMBER_OF_BUTTON; ++i) { ptrButton[i]->setPosition(650, 50 + 40 * i); ptrButton[i]->renderButton(); } timeText.str(""); timeText << "Time: " << std::fixed << std::setprecision(1) << (gTimer.getTicks() / 1000.f); //Render text if (!word.loadFromRenderedText(timeText.str().c_str(), textColor)) { printf("Unable to render time texture!\n"); } word.render(20, 550); if (gPtrHelp == NULL) { if (board.isStarted()) { drowGame(board); } } else { gPtrHelp->render(25, 50); } SDL_RenderPresent(gRenderer); } } } } close(); return 0; }
void CAPrintingAttachment::DoPrintJob() { // Validate pointers. ValidateThis_(); // Find the view that will be installed in the printout. LWindow* ourWindow = (dynamic_cast<LWindow*>(mOwnerHost)); ValidateObject_(ourWindow); LPane* printedPane = nil; if (mPrintedPaneID != 0) printedPane = ourWindow->FindPaneByID(mPrintedPaneID); LPane* printedTitleBar = nil; if (mPrintedTitleBarID != 0) printedTitleBar = ourWindow->FindPaneByID(mPrintedTitleBarID); LPrintout* thePrintout = nil; try { // Create the printout view. { StApplicationContext appContext; thePrintout = LPrintout::CreatePrintout(mPrintoutPPobID); ValidateObject_(thePrintout); } thePrintout->SetPrintSpec(sPrintRecordSpec); // Move the printed pane to the printout. LPlaceHolder* thePlaceHolder = (dynamic_cast<LPlaceHolder*>(thePrintout->FindPaneByID(mPrintoutPlaceholderID))); ValidateObject_(thePlaceHolder); thePlaceHolder->InstallOccupant(printedPane); // Move the title bar to the printout (if any). if (printedTitleBar != nil) { LPlaceHolder* theTBarHolder = (dynamic_cast<LPlaceHolder*>(thePrintout->FindPaneByID(mPrintoutTbarPlaceholderID))); ValidateObject_(theTBarHolder); theTBarHolder->InstallOccupant(printedTitleBar); } // Add document and window titles to printout header. LCaption* fDocumentName = (LCaption*) thePrintout->FindPaneByID(Pane_DocumentName); ValidateObject_(fDocumentName); LCaption* fResourceID = (LCaption*) thePrintout->FindPaneByID(Pane_ResourceID); ValidateObject_(fResourceID); LDocument* document = nil; LCommander* commander = ourWindow; while (commander != nil) { // ValidateObject_(commander); // can't validate, since commander might be stack-based (application) document = (dynamic_cast<LDocument*>(commander)); if (document != nil) break; commander = commander->GetSuperCommander(); } LStr255 windowName; ourWindow->GetDescriptor(windowName); LStr255 docName; if (document != nil) { ValidateObject_(document); document->GetDescriptor(docName); } if (docName.Length() == 0) docName = windowName; if (windowName == docName) windowName[(UInt8)0] = 0; fDocumentName->SetDescriptor(docName); fResourceID->SetDescriptor(windowName); // Add date and time to printout header. LCaption* fDateField = (LCaption*) thePrintout->FindPaneByID(Pane_DateField); ValidateObject_(fDateField); LStr255 tempStr; LStr255 tempStr2; unsigned long dateTime; // set date & time ::GetDateTime(&dateTime); ::DateString(dateTime, abbrevDate, tempStr, nil); { StApplicationContext appContext; tempStr += LStr255(STR_PrintoutStrings, str_DateTimeSeparator); } ::TimeString(dateTime, false, tempStr2, nil); tempStr += tempStr2; fDateField->SetDescriptor(tempStr); // Print it. #if PP_Target_Carbon StClipRgnState noFlash(nil); #else StVisRgn noFlash(ourWindow->GetMacPort()); #endif thePrintout->DoPrintJob(); delete thePrintout; } catch(...) { if (thePrintout) delete thePrintout; } // Make sure printed pane is redrawn properly. printedPane->Refresh(); if (printedTitleBar != nil) printedTitleBar->Refresh(); }
namespace SDL37ExampleNamespace { //Screen dimension constants const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; class LWindow { public: //Intializes internals LWindow(); //Creates window bool init(); //Handles window events void handleEvent( SDL_Event& e ); //Focuses on window void focus(); //Shows windows contents void render(); //Deallocates internals void free(); //Window dimensions int getWidth(); int getHeight(); //Window focii bool hasMouseFocus(); bool hasKeyboardFocus(); bool isMinimized(); bool isShown(); private: //Window data SDL_Window* mWindow; SDL_Renderer* mRenderer; int mWindowID; int mWindowDisplayID; //Window dimensions int mWidth; int mHeight; //Window focus bool mMouseFocus; bool mKeyboardFocus; bool mFullScreen; bool mMinimized; bool mShown; }; //Starts up SDL and creates window bool init(); //Frees media and shuts down SDL void close(); //Our custom window LWindow gWindow; //Display data int gTotalDisplays = 0; SDL_Rect* gDisplayBounds = NULL; LWindow::LWindow() { //Initialize non-existant window mWindow = NULL; mRenderer = NULL; mMouseFocus = false; mKeyboardFocus = false; mFullScreen = false; mShown = false; mWindowID = -1; mWindowDisplayID = -1; mWidth = 0; mHeight = 0; } bool LWindow::init() { //Create window mWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE ); if( mWindow != NULL ) { mMouseFocus = true; mKeyboardFocus = true; mWidth = SCREEN_WIDTH; mHeight = SCREEN_HEIGHT; //Create renderer for window mRenderer = SDL_CreateRenderer( mWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC ); if( mRenderer == NULL ) { printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() ); SDL_DestroyWindow( mWindow ); mWindow = NULL; } else { //Initialize renderer color SDL_SetRenderDrawColor( mRenderer, 0xFF, 0xFF, 0xFF, 0xFF ); //Grab window identifiers mWindowID = SDL_GetWindowID( mWindow ); mWindowDisplayID = SDL_GetWindowDisplayIndex( mWindow ); //Flag as opened mShown = true; } } else { printf( "Window could not be created! SDL Error: %s\n", SDL_GetError() ); } return mWindow != NULL && mRenderer != NULL; } void LWindow::handleEvent( SDL_Event& e ) { //Caption update flag bool updateCaption = false; //If an event was detected for this window if( e.type == SDL_WINDOWEVENT && e.window.windowID == mWindowID ) { switch( e.window.event ) { //Window moved case SDL_WINDOWEVENT_MOVED: mWindowDisplayID = SDL_GetWindowDisplayIndex( mWindow ); updateCaption = true; break; //Window appeared case SDL_WINDOWEVENT_SHOWN: mShown = true; break; //Window disappeared case SDL_WINDOWEVENT_HIDDEN: mShown = false; break; //Get new dimensions and repaint case SDL_WINDOWEVENT_SIZE_CHANGED: mWidth = e.window.data1; mHeight = e.window.data2; SDL_RenderPresent( mRenderer ); break; //Repaint on expose case SDL_WINDOWEVENT_EXPOSED: SDL_RenderPresent( mRenderer ); break; //Mouse enter case SDL_WINDOWEVENT_ENTER: mMouseFocus = true; updateCaption = true; break; //Mouse exit case SDL_WINDOWEVENT_LEAVE: mMouseFocus = false; updateCaption = true; break; //Keyboard focus gained case SDL_WINDOWEVENT_FOCUS_GAINED: mKeyboardFocus = true; updateCaption = true; break; //Keyboard focus lost case SDL_WINDOWEVENT_FOCUS_LOST: mKeyboardFocus = false; updateCaption = true; break; //Window minimized case SDL_WINDOWEVENT_MINIMIZED: mMinimized = true; break; //Window maxized case SDL_WINDOWEVENT_MAXIMIZED: mMinimized = false; break; //Window restored case SDL_WINDOWEVENT_RESTORED: mMinimized = false; break; //Hide on close case SDL_WINDOWEVENT_CLOSE: SDL_HideWindow( mWindow ); break; } } else if( e.type == SDL_KEYDOWN ) { //Display change flag bool switchDisplay = false; //Cycle through displays on up/down switch( e.key.keysym.sym ) { case SDLK_UP: ++mWindowDisplayID; switchDisplay = true; break; case SDLK_DOWN: --mWindowDisplayID; switchDisplay = true; break; } //Display needs to be updated if( switchDisplay ) { //Bound display index if( mWindowDisplayID < 0 ) { mWindowDisplayID = gTotalDisplays - 1; } else if( mWindowDisplayID >= gTotalDisplays ) { mWindowDisplayID = 0; } //Move window to center of next display SDL_SetWindowPosition( mWindow, gDisplayBounds[ mWindowDisplayID ].x + ( gDisplayBounds[ mWindowDisplayID ].w - mWidth ) / 2, gDisplayBounds[ mWindowDisplayID ].y + ( gDisplayBounds[ mWindowDisplayID ].h - mHeight ) / 2 ); updateCaption = true; } } //Update window caption with new data if( updateCaption ) { std::stringstream caption; caption << "SDL Tutorial - ID: " << mWindowID << " Display: " << mWindowDisplayID << " MouseFocus:" << ( ( mMouseFocus ) ? "On" : "Off" ) << " KeyboardFocus:" << ( ( mKeyboardFocus ) ? "On" : "Off" ); SDL_SetWindowTitle( mWindow, caption.str().c_str() ); } } void LWindow::focus() { //Restore window if needed if( !mShown ) { SDL_ShowWindow( mWindow ); } //Move window forward SDL_RaiseWindow( mWindow ); } void LWindow::render() { if( !mMinimized ) { //Clear screen SDL_SetRenderDrawColor( mRenderer, 0xFF, 0xFF, 0xFF, 0xFF ); SDL_RenderClear( mRenderer ); //Update screen SDL_RenderPresent( mRenderer ); } } void LWindow::free() { if( mWindow != NULL ) { SDL_DestroyWindow( mWindow ); } mMouseFocus = false; mKeyboardFocus = false; mWidth = 0; mHeight = 0; } int LWindow::getWidth() { return mWidth; } int LWindow::getHeight() { return mHeight; } bool LWindow::hasMouseFocus() { return mMouseFocus; } bool LWindow::hasKeyboardFocus() { return mKeyboardFocus; } bool LWindow::isMinimized() { return mMinimized; } bool LWindow::isShown() { return mShown; } bool init() { //Initialization flag bool success = true; //Initialize SDL if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) { printf( "SDL could not initialize! SDL Error: %s\n", SDL_GetError() ); success = false; } else { //Set texture filtering to linear if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) ) { printf( "Warning: Linear texture filtering not enabled!" ); } //Get number of displays gTotalDisplays = SDL_GetNumVideoDisplays(); if( gTotalDisplays < 2 ) { printf( "Warning: Only one display connected!" ); } //Get bounds of each display gDisplayBounds = new SDL_Rect[ gTotalDisplays ]; for( int i = 0; i < gTotalDisplays; ++i ) { SDL_GetDisplayBounds( i, &gDisplayBounds[ i ] ); } //Create window if( !gWindow.init() ) { printf( "Window could not be created!\n" ); success = false; } } return success; } void close() { //Destroy window gWindow.free(); //Deallocate bounds delete[] gDisplayBounds; gDisplayBounds = NULL; //Quit SDL subsystems SDL_Quit(); } }