int main(int argc, const char * argv[]) { init(); loadMedia(); bool quit = false; SDL_Event e; int frame = 0; int scrollingOffset = gameState.scrollingOffset; while (!quit) { while (SDL_PollEvent(&e) != 0) { if (e.type == SDL_QUIT) { quit = true; } batman->handleEvent(e); } SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF); SDL_RenderClear(gRenderer); // load background first with offset if (batman->isMovingFwd()) { -- scrollingOffset; if (scrollingOffset < -gBackGroundTexture.getWidth()) { scrollingOffset = 0; } } gBackGroundTexture.render(scrollingOffset, 0, gRenderer); gBackGroundTexture.render(scrollingOffset + gBackGroundTexture.getWidth(), 0, gRenderer); batman->render(50, SCREEN_HEIGHT - 60); SDL_RenderPresent(gRenderer); } // save game state gameState.scrollingOffset = scrollingOffset; printf("saving game state: scrolling offset %d\n", gameState.scrollingOffset); // close close(); return 0; }
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; }
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; } } //Clear screen SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF ); SDL_RenderClear( gRenderer ); //Render current frame gTextTexture.render( ( SCREEN_WIDTH - gTextTexture.getWidth() ) / 2, ( SCREEN_HEIGHT - gTextTexture.getHeight() ) / 2 ); //Update screen SDL_RenderPresent( gRenderer ); } } } //Free resources and close SDL close(); return 0; }
bool ifShipHit(int projX, int projY, int shipX, int shipY) { if ( projX >= shipX && (projX < (shipX + (gShipTexture.getWidth()) ) ) && (projY > shipY ) && (projY < (shipY + (gShipTexture.getHeight()) ) ) ) { return true; } else return false; }
void loop () { //Handle events on queue while( SDL_PollEvent( &e ) != 0 ) { //User requests quit if( e.type == SDL_QUIT ) { quit = true; } //Handle input for the dot dot.handleEvent( e ); } //Move the dot dot.move(); //Scroll background --scrollingOffset; if( scrollingOffset < -gBGTexture.getWidth() ) { scrollingOffset = 0; } //Clear screen SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF ); SDL_RenderClear( gRenderer ); //Render background gBGTexture.render( scrollingOffset, 0 ); gBGTexture.render( scrollingOffset + gBGTexture.getWidth(), 0 ); //Render objects dot.render(); //Update screen SDL_RenderPresent( gRenderer ); }
Dot::Dot() { //Initialize the offsets mPosX = 0; mPosY = 0; //Set collision box dimension mCollider.w = gDotTexture.getWidth(); mCollider.h = gDotTexture.getHeight(); //Initialize the velocity mVelX = 0; mVelY = 0; }
bool ifHitPlayer(int playerX, int playerY, int shipProjX, int shipProjY) { //cout << "px " << playerX << "py " << playerY << "sX" << shipProjX << "sY " << shipProjY << endl; if( shipProjX > playerX && shipProjX < playerX + gPlayerTexture.getWidth() && shipProjY > playerY - gPlayerTexture.getHeight() ) { cout << "curna TRUE"; return true; } else { return false; } }
void loop () { //Handle events on queue while( SDL_PollEvent( &e ) != 0 ) { //User requests quit if( e.type == SDL_QUIT ) { quit = true; } //Reset start time on return keypress else if( e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_RETURN ) { startTime = SDL_GetTicks(); } } //Set text to be rendered timeText.str( "" ); timeText << "Milliseconds since start time " << SDL_GetTicks() - startTime; //Render text if( !gTimeTextTexture.loadFromRenderedText( timeText.str().c_str(), textColor ) ) { printf( "Unable to render time texture!\n" ); } //Clear screen SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF ); SDL_RenderClear( gRenderer ); //Render textures gPromptTextTexture.render( ( SCREEN_WIDTH - gPromptTextTexture.getWidth() ) / 2, 0 ); gTimeTextTexture.render( ( SCREEN_WIDTH - gPromptTextTexture.getWidth() ) / 2, ( SCREEN_HEIGHT - gPromptTextTexture.getHeight() ) / 2 ); //Update screen SDL_RenderPresent( gRenderer ); }
Ball::Ball(int x, int y, int velX, int velY){ //Initialize the offsets mPosX = x; mPosY = y; //Set collision circle size mCollider.r = gBallTexture.getWidth() / 2; //Initialize the velocity mVelX = velX; mVelY = velY; //Move collider relative to the circle shiftColliders(); }
bool ifScreenEndHit() { //int firstLeftAlive = 0, firstRightAlive = 0; bool endHit = false; vector<vector<Ships*>>::iterator it = ships.begin(); for(vector<Ships*>::reverse_iterator itt = (*it).rbegin(); itt != (*it).rend(); ++itt) { if( (*itt)->mPosX == 0 || (*itt)->mPosX == SCREEN_WIDTH - gShipTexture.getWidth() ) { return true; } } return endHit; }
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 loop () { //Handle events on queue while( SDL_PollEvent( &e ) != 0 ) { //User requests quit if( e.type == SDL_QUIT ) { quit = true; } } //Calculate and correct fps float avgFPS = countedFrames / ( fpsTimer.getTicks() / 1000.f ); if( avgFPS > 2000000 ) { avgFPS = 0; } //Set text to be rendered timeText.str( "" ); timeText << "Average Frames Per Second " << avgFPS; //Render text if( !gFPSTextTexture.loadFromRenderedText( timeText.str().c_str(), textColor ) ) { printf( "Unable to render FPS texture!\n" ); } //Clear screen SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF ); SDL_RenderClear( gRenderer ); //Render textures gFPSTextTexture.render( ( SCREEN_WIDTH - gFPSTextTexture.getWidth() ) / 2, ( SCREEN_HEIGHT - gFPSTextTexture.getHeight() ) / 2 ); //Update screen SDL_RenderPresent( gRenderer ); ++countedFrames; }
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; //Angle of rotation double degrees = 0; //Flip type SDL_RendererFlip flipType = SDL_FLIP_NONE; //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; } else if( e.type == SDL_KEYDOWN ) { switch( e.key.keysym.sym ) { case SDLK_a: degrees -= 60; break; case SDLK_d: degrees += 60; break; case SDLK_q: flipType = SDL_FLIP_HORIZONTAL; break; case SDLK_w: flipType = SDL_FLIP_NONE; break; case SDLK_e: flipType = SDL_FLIP_VERTICAL; break; } } } //Clear screen SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF ); SDL_RenderClear( gRenderer ); //Render arrow gArrowTexture.render( ( SCREEN_WIDTH - gArrowTexture.getWidth() ) / 2, ( SCREEN_HEIGHT - gArrowTexture.getHeight() ) / 2, NULL, degrees, NULL, flipType ); //Update screen SDL_RenderPresent( gRenderer ); } } } //Free resources and close SDL close(); return 0; }
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; //Set text color as black SDL_Color textColor = { 0, 0, 0, 255 }; //The application timer LTimer timer; //In memory text stream std::stringstream timeText; //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; } //Reset start time on return keypress else if( e.type == SDL_KEYDOWN ) { //Start/stop if( e.key.keysym.sym == SDLK_s ) { if( timer.isStarted() ) { timer.stop(); } else { timer.start(); } } //Pause/unpause else if( e.key.keysym.sym == SDLK_p ) { if( timer.isPaused() ) { timer.unpause(); } else { timer.pause(); } } } } //Set text to be rendered timeText.str( "" ); timeText << "Seconds since start time " << ( timer.getTicks() / 1000.f ) ; //Render text if( !gTimeTextTexture.loadFromRenderedText( timeText.str().c_str(), textColor ) ) { printf( "Unable to render time texture!\n" ); } //Clear screen SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF ); SDL_RenderClear( gRenderer ); //Render textures gStartPromptTexture.render( ( SCREEN_WIDTH - gStartPromptTexture.getWidth() ) / 2, 0 ); gPausePromptTexture.render( ( SCREEN_WIDTH - gPausePromptTexture.getWidth() ) / 2, gStartPromptTexture.getHeight() ); gTimeTextTexture.render( ( SCREEN_WIDTH - gTimeTextTexture.getWidth() ) / 2, ( SCREEN_HEIGHT - gTimeTextTexture.getHeight() ) / 2 ); //Update screen SDL_RenderPresent( gRenderer ); } } } //Free resources and close SDL close(); return 0; }
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; //The dot that will be moving around on the screen Dot dot; //The background scrolling offset int scrollingOffset = 0; //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 input for the dot dot.handleEvent( e ); } //Move the dot dot.move(); //Scroll background --scrollingOffset; if( scrollingOffset < -gBGTexture.getWidth() ) { scrollingOffset = 0; } //Clear screen SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF ); SDL_RenderClear( gRenderer ); //Render background gBGTexture.render( scrollingOffset, 0 ); gBGTexture.render( scrollingOffset + gBGTexture.getWidth(), 0 ); //Render objects dot.render(); //Update screen SDL_RenderPresent( gRenderer ); } } } //Free resources and close SDL close(); return 0; }
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; //Text rendering color SDL_Color textColor = { 0, 0, 0, 0xFF }; SDL_Color highlightColor = { 0xFF, 0, 0, 0xFF }; //Current input point int currentData = 0; //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; } else if( e.type == SDL_KEYDOWN ) { switch( e.key.keysym.sym ) { //Previous data entry case SDLK_UP: //Rerender previous entry input point gDataTextures[ currentData ].loadFromRenderedText( std::to_string( (_Longlong)gData[ currentData ] ), textColor ); --currentData; if( currentData < 0 ) { currentData = TOTAL_DATA - 1; } //Rerender current entry input point gDataTextures[ currentData ].loadFromRenderedText( std::to_string( (_Longlong)gData[ currentData ] ), highlightColor ); break; //Next data entry case SDLK_DOWN: //Rerender previous entry input point gDataTextures[ currentData ].loadFromRenderedText( std::to_string( (_Longlong)gData[ currentData ] ), textColor ); ++currentData; if( currentData == TOTAL_DATA ) { currentData = 0; } //Rerender current entry input point gDataTextures[ currentData ].loadFromRenderedText( std::to_string( (_Longlong)gData[ currentData ] ), highlightColor ); break; //Decrement input point case SDLK_LEFT: --gData[ currentData ]; gDataTextures[ currentData ].loadFromRenderedText( std::to_string( (_Longlong)gData[ currentData ] ), highlightColor ); break; //Increment input point case SDLK_RIGHT: ++gData[ currentData ]; gDataTextures[ currentData ].loadFromRenderedText( std::to_string( (_Longlong)gData[ currentData ] ), highlightColor ); break; } } } //Clear screen SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF ); SDL_RenderClear( gRenderer ); //Render text textures gPromptTextTexture.render( ( SCREEN_WIDTH - gPromptTextTexture.getWidth() ) / 2, 0 ); for( int i = 0; i < TOTAL_DATA; ++i ) { gDataTextures[ i ].render( ( SCREEN_WIDTH - gDataTextures[ i ].getWidth() ) / 2, gPromptTextTexture.getHeight() + gDataTextures[ 0 ].getHeight() * i ); } //Update screen SDL_RenderPresent( gRenderer ); } } } //Free resources and close SDL close(); return 0; }
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; //Set text color as black SDL_Color textColor = { 0, 0, 0, 255 }; //The frames per second timer LTimer fpsTimer; //In memory text stream std::stringstream timeText; //Start counting frames per second int countedFrames = 0; fpsTimer.start(); //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; } } //Calculate and correct fps float avgFPS = countedFrames / ( fpsTimer.getTicks() / 1000.f ); if( avgFPS > 2000000 ) { avgFPS = 0; } //Set text to be rendered timeText.str( "" ); timeText << "Average Frames Per Second " << avgFPS; //Render text if( !gFPSTextTexture.loadFromRenderedText( timeText.str().c_str(), textColor ) ) { printf( "Unable to render FPS texture!\n" ); } //Clear screen SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF ); SDL_RenderClear( gRenderer ); //Render textures gFPSTextTexture.render( ( SCREEN_WIDTH - gFPSTextTexture.getWidth() ) / 2, ( SCREEN_HEIGHT - gFPSTextTexture.getHeight() ) / 2 ); //Update screen SDL_RenderPresent( gRenderer ); ++countedFrames; } } } //Free resources and close SDL close(); return 0; }
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; //In memory text stream stringstream timeText; //Start global timer gTimer.start(); //The frames per second timer LTimer fpsTimer; //Start counting frames per second int countedFrames = 0; fpsTimer.start(); //Count of balls in screen int nBalls = 4; //Set text color as black SDL_Color textColor = {0, 0, 0, 255}; //loadBalls in vector gBalls loadBalls(nBalls); //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; } } //Clear screen SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF); SDL_RenderClear(gRenderer); //The ball in the center of the screen to which all other ball collides with Ball ball(SCREEN_WIDTH/2, SCREEN_HEIGHT/2, 0, 0); ball.render(); //Calculate and correct fps float avgFPS = countedFrames/(fpsTimer.getTicks()/1000.f); if(avgFPS > 2000000){ avgFPS = 0; } //Set text to be rendered timeText.str(""); timeText << "Average Frames Per Second " << avgFPS; //Render text if(!gFPSTextTexture.loadFromRenderedText(timeText.str().c_str(), textColor)){ printf("Unable to render FPS texture!\n"); } gFPSTextTexture.render((SCREEN_WIDTH-gFPSTextTexture.getWidth())/2, 0); //Move and render the balls inside the vector gBalls for(int i = 0; i < nBalls; i++){ //bug: Needs to include all the balls inside the gBalls for the move method gBalls.at(i).move(ball.getCollider()); gBalls.at(i).render(); } //Update screen SDL_RenderPresent(gRenderer); ++countedFrames; } } } //Free resources and close SDL close(); return 0; }
void LTexture::renderRelToScrnRel2Object(double x, double y, LTexture& obj2, double angle) { render( MidPositionRelative(SCREEN_WIDTH, obj2.getWidth(), getWidth(), x), MidPositionRelative(SCREEN_HEIGHT, obj2.getHeight(), getHeight(), y), angle); }
//Displays rules and how to play instructions void Application::instructionsScreen() { if (gWindow == NULL) { printf("Failed to load media!\n"); } //Main loop flag bool quit = false; //Text Color SDL_Color textColor = { 0x00, 0xCC, 0xFF }; //Back button Button back(160, 50, 370, 600); //Textures LTexture backgroundTexture; LTexture arrows; LTexture backText; LTexture instructions; LTexture instructionsTwo; LTexture instructionsUp; LTexture instructionsBack; LTexture instructionsLeft; LTexture instructionsLeftOne; LTexture instructionsLeftTwo; LTexture instructionsRight; LTexture instructionsRightOne; LTexture instructionsRightTwo; LTexture instructionsChoice; LTexture slogan; LTexture sloganTwo; //-------------BUTTON SET POS & BCKGROUND---------- backgroundTexture.loadFromFile("img/menu.png", gRenderer); back.loadMedia("img/buttons/button.png", *gRenderer); arrows.loadFromFile("img/arrowKeys.png", gRenderer); //------------------TEXT LOADING------------------- backText.loadFromRenderedText("Back", textColor, *gRenderer); instructions.loadFromRenderedText("The goal is to reach the finish line before the time runs out!", textColor, *gRenderer); instructionsTwo.loadFromRenderedText("Get moving around using the arrow keys as follows :", textColor, *gRenderer); instructionsUp.loadFromRenderedText("Up - accelerate the car towards it's fornt.", textColor, *gRenderer); instructionsBack.loadFromRenderedText("Down - start driving the car in reverse.", textColor, *gRenderer); instructionsLeftOne.loadFromRenderedText("Left:", textColor, *gRenderer); instructionsLeft.loadFromRenderedText("Hold it at least 0.3 sec and release", textColor, *gRenderer); instructionsLeftTwo.loadFromRenderedText("to turn the car in this direction.", textColor, *gRenderer); instructionsRightOne.loadFromRenderedText("Right:", textColor, *gRenderer); instructionsRight.loadFromRenderedText("Hold it atleast 0.3 sec and release", textColor, *gRenderer); instructionsRightTwo.loadFromRenderedText("to turn the car in this direction.", textColor, *gRenderer); slogan.loadFromRenderedText("Good luck!", textColor, *gRenderer); sloganTwo.loadFromRenderedText("And may the Gods be ever in your favour!", textColor, *gRenderer); instructionsChoice.loadFromRenderedText("When you choose your car, press enter to begin the game", textColor, *gRenderer); //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; } else if (onMusicButton->handleEvent(&e)) { if (isMusicOn) { isMusicOn = false; Mix_PauseMusic(); } else { isMusicOn = true; Mix_ResumeMusic(); } } else if (e.key.keysym.sym == SDLK_ESCAPE) { return; } else if (e.type == SDL_MOUSEBUTTONUP) { if (back.handleEvent(&e)) { return; } } } //--------------CLEAR SCREEN----------- SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF); SDL_RenderClear(gRenderer); //---------------RENDER TEXT---------------- backgroundTexture.render(*gRenderer, 0, 0); arrows.render(*gRenderer, ((SCREEN_WIDTH - arrows.getWidth()) / 2), 200); back.render(*gRenderer); backText.render(*gRenderer, ((SCREEN_WIDTH - backText.getWidth()) / 2), 614); instructions.render(*gRenderer, ((SCREEN_WIDTH - instructions.getWidth()) / 2), 50); instructionsTwo.render(*gRenderer, ((SCREEN_WIDTH - instructionsTwo.getWidth()) / 2), 100); instructionsUp.render(*gRenderer, ((SCREEN_WIDTH - instructionsUp.getWidth()) / 2), 150); instructionsBack.render(*gRenderer, ((SCREEN_WIDTH - instructionsUp.getWidth()) / 2), 370); instructionsLeft.render(*gRenderer, 0, 230); instructionsLeftOne.render(*gRenderer, ((SCREEN_WIDTH - instructionsLeftOne.getWidth()) / 6), 200); instructionsLeftTwo.render(*gRenderer, 10, 270); instructionsRight.render(*gRenderer, 540, 230); instructionsRightOne.render(*gRenderer, 680, 200); instructionsRightTwo.render(*gRenderer, 560, 270); slogan.render(*gRenderer, ((SCREEN_WIDTH - slogan.getWidth()) / 2), 510); sloganTwo.render(*gRenderer, ((SCREEN_WIDTH - sloganTwo.getWidth()) / 2), 540); instructionsChoice.render(*gRenderer, ((SCREEN_WIDTH - instructionsChoice.getWidth()) / 2), 450); if (isMusicOn) { onMusicButton->render(*gRenderer); } else { offMusicButton->render(*gRenderer); } //------------- UPDATE SCREEN---------------- SDL_RenderPresent(gRenderer); } }
//Displays last ten games on screen void Application::lastTenGamesScreen(std::fstream& fileToRead) { if (gWindow == NULL) { printf("Unable to open window"); } bool quit = false; //Main loop flag SDL_Color textColor = { 0x00, 0xCC, 0xFF }; LTexture backgroundTexture; Button back(160, 50, 370, 600); LTexture backText; LTexture description; std::string descriptionText = "Player Credit"; descriptionText.append(" "); descriptionText.append("Time"); descriptionText.append(" "); descriptionText.append("Player Bet"); descriptionText.append(" "); descriptionText.append("Player Win"); descriptionText.append(" "); descriptionText.append("Difficulty"); std::vector<PreviousGame> lastTenGames = lastTenGamesManager.readLastTenGames(fileToRead); LTexture lastTenGamesCredit[10]; LTexture lastTenGamesTime[10]; LTexture lastTenGamesBet[10]; LTexture lastTenGamesWin[10]; LTexture lastTenGamesDiff[10]; for (int i = 0; i < 10; i++) { std::string tempCredit = std::to_string((int)lastTenGames[i].getCredit()); std::string tempTime = std::to_string(lastTenGames[i].getTime()); std::string tempBet = std::to_string((int)lastTenGames[i].getPlayerBet()); std::string tempWin = std::to_string((int)lastTenGames[i].getPlayerWin()); std::string tempDiff = lastTenGames[i].getDifficulty(); lastTenGamesCredit[i].loadFromRenderedText(tempCredit, textColor, *gRenderer); lastTenGamesTime[i].loadFromRenderedText(tempTime, textColor, *gRenderer); lastTenGamesBet[i].loadFromRenderedText(tempBet, textColor, *gRenderer); lastTenGamesWin[i].loadFromRenderedText(tempWin, textColor, *gRenderer); lastTenGamesDiff[i].loadFromRenderedText(tempDiff, textColor, *gRenderer); } //----------------TEXTURES LOADING ------------------ backgroundTexture.loadFromFile("img/menu.png", gRenderer); back.setPosition(370, 600); back.loadMedia("img/buttons/button.png", *gRenderer); //------------------TEXT LOADING------------------- backText.loadFromRenderedText("Back", textColor, *gRenderer); description.loadFromRenderedText(descriptionText, textColor, *gRenderer); //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; } else if (onMusicButton->handleEvent(&e)) { checkMusic(); } else if (e.key.keysym.sym == SDLK_ESCAPE) { return; } else if (e.type == SDL_MOUSEBUTTONUP) { if (back.handleEvent(&e)) { return; } } } //--------------CLEAR SCREEN----------- SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF); SDL_RenderClear(gRenderer); //---------------RENDER TEXT---------------- backgroundTexture.render(*gRenderer, 0, 0); back.render(*gRenderer); backText.render(*gRenderer, ((SCREEN_WIDTH - backText.getWidth()) / 2), 614); description.render(*gRenderer, 20, 60); int xPos = 0; for (int i = 0; i < 10; i++) { if (lastTenGames[i].getCredit() < 10) { lastTenGamesCredit[i].render(*gRenderer, 85, (xPos + 100)); } else { lastTenGamesCredit[i].render(*gRenderer, 80, (xPos + 100)); } lastTenGamesTime[i].render(*gRenderer, 240, (xPos + 100)); if (lastTenGames[i].getPlayerBet() < 10) { lastTenGamesBet[i].render(*gRenderer, 417, (xPos + 100)); } else { lastTenGamesBet[i].render(*gRenderer, 410, (xPos + 100)); } lastTenGamesWin[i].render(*gRenderer, 630, (xPos + 100)); if (lastTenGames[i].getDifficulty() != "Medium") { lastTenGamesDiff[i].render(*gRenderer, 795, (xPos + 100)); } else { lastTenGamesDiff[i].render(*gRenderer, 780, (xPos + 100)); } xPos += 45; } renderMusicButton(); //------------- UPDATE SCREEN---------------- SDL_RenderPresent(gRenderer); } }
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; //The background scrolling offset int scrollingOffset = 0; //Angle of rotation double degrees = 0; //The dot that will be moving around on the screen Dot dot ( 100, 300); //Opening Fonts and message TTF_Font *times; times = TTF_OpenFont("arial.ttf", 14); SDL_Color white = {255, 255, 255}; std::stringstream health; health <<"Health:" << dot.hp << " Points: " << POINTS; const std::string str = health.str(); SDL_Surface *surface = TTF_RenderText_Solid(times, health.str().c_str(), white); SDL_Texture * texture = SDL_CreateTextureFromSurface(gRenderer, surface); int texW = 0; int texH = 0; SDL_QueryTexture(texture, NULL, NULL, &texW, &texH); SDL_Rect dstrect = { 0, 0, texW, texH }; //creating enemies std::vector<Enemy*> enemies; for (int i = 0; i < NumberEnemies; i++) { enemies.push_back(new Enemy(i*100, 0)); } //Flip type SDL_RendererFlip flipType = SDL_FLIP_NONE; //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; } dot.handleEvent(e); } //rotating degrees += 1; //collision between enemies and bullets for (int i = 0; i < enemies.size(); i++) { for (int j = 0; j < bulletsUp.size(); j++) { enemies[i]->move(bulletsUp[j]->getColliders()); } for (int j = 0; j < bulletsLeft.size(); j++) { enemies[i]->move(bulletsLeft[j]->getColliders()); } for (int j = 0; j < bulletsRight.size(); j++) { enemies[i]->move(bulletsRight[j]->getColliders()); } enemies[i]->move(dot.getColliders()); enemies[i]->shoot(enemies[i]->mPosX, enemies[i]->mPosY); dot.move( enemies[i]->getColliders()); } //moving of bullets for (int i = 0; i < bulletsUp.size(); i++) { bulletsUp[i]->moveUp(dot.getColliders()); } for (int i = 0; i < bulletsLeft.size(); i++) { bulletsLeft[i]->moveLeft(dot.getColliders()); } for (int i = 0; i < bulletsRight.size(); i++) { bulletsRight[i]->moveRight(dot.getColliders()); } for (int i = 0; i < enemyBullets.size(); i++) { enemyBullets[i]->enemyMove(dot.getColliders()); } //Scroll background --scrollingOffset; if( scrollingOffset < -gBGTexture.getWidth() ) { scrollingOffset = 0; } //Clear screen SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF ); SDL_RenderClear( gRenderer ); //Render background gBGTexture.render( scrollingOffset, 0 ); SDL_RenderCopy(gRenderer, texture, NULL, &dstrect); gBGTexture.render( scrollingOffset + gBGTexture.getWidth(), 0 ); //Render Dot dot.render(); //displaying enemeies and bullets for (int i = 0; i < enemies.size(); i++) { enemies[i]->render(degrees,flipType); } for (int i = 0; i < bulletsUp.size(); i++) { bulletsUp[i]->render(); } for (int i = 0; i < bulletsLeft.size(); i++) { bulletsLeft[i]->render(); } for (int i = 0; i < bulletsRight.size(); i++) { bulletsRight[i]->render(); } for (int i = 0; i < enemyBullets.size(); i++) { enemyBullets[i]->render(); } //Update screen SDL_RenderPresent( gRenderer ); } } } //Free resources and close SDL close(); return 0; }
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; //Set text color as black SDL_Color textColor = { 0, 0, 0, 0xFF }; //The current input text. std::string inputText = "Some Text"; gInputTextTexture.loadFromRenderedText( inputText.c_str(), textColor ); //Enable text input SDL_StartTextInput(); //While application is running while( !quit ) { //The rerender text flag bool renderText = false; //Handle events on queue while( SDL_PollEvent( &e ) != 0 ) { //User requests quit if( e.type == SDL_QUIT ) { quit = true; } //Special key input else if( e.type == SDL_KEYDOWN ) { //Handle backspace if( e.key.keysym.sym == SDLK_BACKSPACE && inputText.length() > 0 ) { //lop off character inputText.pop_back(); renderText = true; } //Handle copy else if( e.key.keysym.sym == SDLK_c && SDL_GetModState() & KMOD_CTRL ) { SDL_SetClipboardText( inputText.c_str() ); } //Handle paste else if( e.key.keysym.sym == SDLK_v && SDL_GetModState() & KMOD_CTRL ) { inputText = SDL_GetClipboardText(); renderText = true; } } //Special text input event else if( e.type == SDL_TEXTINPUT ) { //Not copy or pasting if( !( ( e.text.text[ 0 ] == 'c' || e.text.text[ 0 ] == 'C' ) && ( e.text.text[ 0 ] == 'v' || e.text.text[ 0 ] == 'V' ) && SDL_GetModState() & KMOD_CTRL ) ) { //Append character inputText += e.text.text; renderText = true; } } } //Rerender text if needed if( renderText ) { //Text is not empty if( inputText != "" ) { //Render new text gInputTextTexture.loadFromRenderedText( inputText.c_str(), textColor ); } //Text is empty else { //Render space texture gInputTextTexture.loadFromRenderedText( " ", textColor ); } } //Clear screen SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF ); SDL_RenderClear( gRenderer ); //Render text textures gPromptTextTexture.render( ( SCREEN_WIDTH - gPromptTextTexture.getWidth() ) / 2, 0 ); gInputTextTexture.render( ( SCREEN_WIDTH - gInputTextTexture.getWidth() ) / 2, gPromptTextTexture.getHeight() ); //Update screen SDL_RenderPresent( gRenderer ); } //Disable text input SDL_StopTextInput(); } } //Free resources and close SDL close(); return 0; }
//User chooses preferred car int Application::chooseCar(SDL_Renderer* gRenderer) { if (gWindow == NULL) { printf("Failed to load screen!\n"); } //Main loop flag bool quit = false; SDL_Color textColor = { 0x00, 0xCC, 0xFF }; Button back(160, 50, 370, 600); LTexture backgroundTexture; LTexture header; LTexture cursor; LTexture attributesCarOne; LTexture attributesCarTwo; LTexture attributesCarThree; LTexture backText; LTexture text; LTexture carOne; LTexture carTwo; LTexture carThree; int cursorX = 190; int cursorY = 90; //--------------BACKGROUND IMG----------------- backgroundTexture.loadFromFile("img/menu.png", gRenderer); //--------------CURSOR IMG -------------------- cursor.loadFromFile("img/cars/cursor.png", gRenderer); //----------------ATTRIBUTES-------------- attributesCarOne.loadFromFile("img/cars/fastest.png", gRenderer); attributesCarTwo.loadFromFile("img/cars/medium.png", gRenderer); attributesCarThree.loadFromFile("img/cars/slowest.png", gRenderer); //-------------BUTTON SET POS & BCKGROUND---------- back.setPosition(370, 600); back.loadMedia("img/buttons/button.png", *gRenderer); //---------------CAR PICTURES---------------- carOne.loadFromFile("img/cars/carOneLarge.png", gRenderer); carTwo.loadFromFile("img/cars/carTwoLarge.png", gRenderer); carThree.loadFromFile("img/cars/carThreeLarge.png", gRenderer); //------------------TEXT LOADING------------------- backText.loadFromRenderedText("Back", textColor, *gRenderer); header.loadFromRenderedText("Choose your vehicle", textColor, *gRenderer); //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; } else if (onMusicButton->handleEvent(&e)) { checkMusic(); } else if (e.type == SDL_MOUSEBUTTONUP) { if (back.handleEvent(&e)) { return 0; } } else if (e.type == SDL_KEYUP) { switch (e.key.keysym.sym) { case SDLK_RIGHT: cursorX += 200; if (cursorX > 590) { cursorX = 590; } break; case SDLK_LEFT: cursorX -= 200; if (cursorX < 190) { cursorX = 190; } break; case SDLK_RETURN: if (cursorX == 190) { return 1; } else if (cursorX == 390) { return 2; } else { return 3; } break; case SDLK_ESCAPE: return 0; break; default: break; } } } //--------------CLEAR SCREEN----------- SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF); SDL_RenderClear(gRenderer); //---------------RENDER TEXT AND TEXTURES---------------- backgroundTexture.render(*gRenderer, 0, 0); header.render(*gRenderer, ((SCREEN_WIDTH - header.getWidth()) / 2), 30); back.render(*gRenderer); backText.render(*gRenderer, ((SCREEN_WIDTH - backText.getWidth()) / 2), 614); carOne.render(*gRenderer, 200, 100); carTwo.render(*gRenderer, 400, 100); carThree.render(*gRenderer, 600, 100); cursor.render(*gRenderer, cursorX, cursorY); renderMusicButton(); if (cursorX == 190) { attributesCarOne.render(*gRenderer, ((SCREEN_WIDTH - attributesCarOne.getWidth()) / 2), 300); } else if (cursorX == 390) { attributesCarTwo.render(*gRenderer, ((SCREEN_WIDTH - attributesCarTwo.getWidth()) / 2), 300); } else { attributesCarThree.render(*gRenderer, ((SCREEN_WIDTH - attributesCarThree.getWidth()) / 2), 300); } //------------- UPDATE SCREEN---------------- SDL_RenderPresent(gRenderer); } return 0; }
bool loadLetters() { bool success = true; if (!gFontTexture.loadFromFile("font.png")) { printf("Failed to load corner texture!\n"); success = false; } else { if (!gFontTexture.lockTexture()) { printf("Unable to lock font texture!\n"); } else { Uint32* pixels = (Uint32*)gFontTexture.getPixels(); int pixelCount = (gFontTexture.getPitch() / 4) * gFontTexture.getHeight(); int width = gFontTexture.getWidth(); int height = gFontTexture.getHeight(); std::vector<std::vector<Uint32>> pixelGrid; pixelGrid.resize(height, std::vector<Uint32>(width)); Uint32 white = SDL_MapRGB(SDL_GetWindowSurface(gWindow)->format, 255, 255, 255); Uint32 cyan = SDL_MapRGB(SDL_GetWindowSurface(gWindow)->format, 0, 255, 255); int k = 0; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { pixelGrid[i][j] = pixels[k]; k++; } } std::vector<SDL_Rect> boxes; int current = 0; int rightEdge = 0; int leftEdge = 0; int state = 1; int rowCount = height / LINE_HEIGHT; printf("%d\n", rowCount); for (int i = 0; i < rowCount; i++) { for (int j = 0; j < width; j++) { printf("%d %d\n", i, j); int value = hasBlack(pixelGrid, i, j, white, cyan); if (value == 1) { if (state == 1) { leftEdge = j; state = 2; } if (state == 2) { rightEdge = j; } } if (value == 2) { SDL_Rect currentBox; currentBox.x = leftEdge; currentBox.y = i * LINE_HEIGHT; currentBox.w = rightEdge - leftEdge; currentBox.h = LINE_HEIGHT - 1; if (currentBox.w < 10) { currentBox.w = 10; } printf("%d %d %d %d\n", currentBox.x, currentBox.y, currentBox.w, currentBox.h); boxes.push_back(currentBox); state = 1; } } } convert(boxes); gFontTexture.unlockTexture(); } } return success; }
int main(int argc, char *argv[]) { int quit = 0; /* event handler */ SDL_Event event; /* set text color as black */ SDL_Color textColor = { 0, 0, 0, 255 }; /* current time start time */ Uint32 startTime = 0; char timeText[128]; /* starts up SDL and create window */ if (init_sdl() == -1) { fprintf(stderr, "Failed to initialize SDL!\n"); return -1; } /* load media */ if (load_media() == -1) { fprintf(stderr, "Failed to load media!\n"); return -2; } /* main loop */ while (quit == 0) { /* handle events on queue */ while (SDL_PollEvent(&event) != 0) { /* users request quit */ if (event.type == SDL_QUIT) quit = 1; /* reset start time on return key press */ else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_RETURN) { startTime = SDL_GetTicks(); } } /* set text to be rendered */ snprintf(timeText, sizeof(timeText), "%d", SDL_GetTicks() - startTime); /* render text */ if (gTimeTextTexture.loadFromRenderedText(timeText, textColor) == -1) fprintf(stderr, "Unable to render time texture!\n"); /* clear screen */ SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF); SDL_RenderClear(gRenderer); /* render texture */ gPromptTexture.render((SCREEN_WIDTH - gPromptTexture.getWidth()) / 2, 0); gTimeTextTexture.render((SCREEN_WIDTH - gTimeTextTexture.getWidth()) / 2, (SCREEN_HEIGHT - gTimeTextTexture.getHeight()) / 2); /* update screen */ SDL_RenderPresent(gRenderer); } /* free resources and close */ close_sdl(); return 0; }