void close_sdl(void) { printf("close_sdl()\n"); /* free loaded image */ gDotTexture.free(); /* destroy window */ SDL_DestroyRenderer(gRenderer); SDL_DestroyWindow(gWindow); gWindow = NULL; gRenderer = NULL; /* Quit SDL subsystem */ #ifdef _SDL_TTF_H TTF_Quit(); #endif IMG_Quit(); SDL_Quit(); }
void loadMedia(){ batman->loadMedia(); // load background gBackGroundTexture.loadFromFile("media/gotham.jpg", gRenderer); // load game file SDL_RWops* file = SDL_RWFromFile("gamestates/01.bin", "r+b"); if (file == NULL) { printf("game state file not exist!\n"); file = SDL_RWFromFile("gamestates/01.bin", "w+b"); printf("new game state file created!\n"); gameState.scrollingOffset = 0; SDL_RWwrite(file, &gameState, sizeof(gameState), 1); SDL_RWclose(file); } else{ printf("previous game state found, reading file..\n"); SDL_RWread(file, &gameState, sizeof(gameState), 1); SDL_RWclose(file); } }
bool loadMedia() { //Loading success flag bool success = true; //Load sprite sheet texture if( !gSpriteSheetTexture.loadFromFile( "foo_animation.png" ) ) { printf( "Failed to load walking animation texture!\n" ); success = false; } else { //Set sprite clips gSpriteClips[ 0 ].x = 0; gSpriteClips[ 0 ].y = 0; gSpriteClips[ 0 ].w = 64; gSpriteClips[ 0 ].h = 205; gSpriteClips[ 1 ].x = 64; gSpriteClips[ 1 ].y = 0; gSpriteClips[ 1 ].w = 64; gSpriteClips[ 1 ].h = 205; gSpriteClips[ 2 ].x = 128; gSpriteClips[ 2 ].y = 0; gSpriteClips[ 2 ].w = 64; gSpriteClips[ 2 ].h = 205; gSpriteClips[ 3 ].x = 196; gSpriteClips[ 3 ].y = 0; gSpriteClips[ 3 ].w = 64; gSpriteClips[ 3 ].h = 205; } return success; }
void PlayerDisplayingHitsAndMiss (char AImap [10][10]) { int xchange = gScreenRect.w/23.9473684211; int ychange = ceil(gScreenRect.h/11.9512195122); int rows=0; for (int ys=gScreenRect.h/5.76470588235; ys<gScreenRect.h/1.00926879506; ys+=ychange) { int cols=0; for (int xs=ceil(gScreenRect.w/2.82608695652)+10; xs<gScreenRect.w/1.2962962963; xs+=xchange) { if (AImap[cols][rows]=='X') { gSplashTexture.render(xs,ys,Miss,xchange, ychange); } cols++; } rows++; } }
void close() { //Open data for writing SDL_RWops* file = SDL_RWFromFile( "33_file_reading_and_writing/nums.bin", "w+b" ); if( file != NULL ) { //Save data for( int i = 0; i < TOTAL_DATA; ++i ) { SDL_RWwrite( file, &gData[ i ], sizeof(Sint32), 1 ); } //Close file handler SDL_RWclose( file ); } else { printf( "Error: Unable to save file! %s\n", SDL_GetError() ); } //Free loaded images gPromptTextTexture.free(); for( int i = 0; i < TOTAL_DATA; ++i ) { gDataTextures[ i ].free(); } //Free global font TTF_CloseFont( gFont ); gFont = NULL; //Destroy window SDL_DestroyRenderer( gRenderer ); SDL_DestroyWindow( gWindow ); gWindow = NULL; gRenderer = NULL; //Quit SDL subsystems TTF_Quit(); IMG_Quit(); SDL_Quit(); }
int load_media(void) { /* open the font */ gFont = TTF_OpenFont("lazy.ttf", 28); if (gFont == NULL) { fprintf(stderr, "Failed to load lazy font! SDL_ttf Error: %s\n", TTF_GetError()); return -1; } /* set text color as black */ SDL_Color textColor = { 0, 0, 0, 255 }; /* load prompt texture */ if (gPromptTexture.loadFromRenderedText( "Please Enter to Reset Start Time.", textColor) == -1) { fprintf(stderr, "Unable to render prompt texture!\n"); return -1; } return 0; }
bool loadMedia() { //Loading success flag bool success = true; //Load foo' texture if( !gFooTexture.loadFromFile( "40_texture_manipulation/foo.png" ) ) { printf( "Failed to load corner texture!\n" ); success = false; } else { //Lock texture if( !gFooTexture.lockTexture() ) { printf( "Unable to lock Foo' texture!\n" ); } //Manual color key else { //Get pixel data Uint32* pixels = (Uint32*)gFooTexture.getPixels(); int pixelCount = ( gFooTexture.getPitch() / 4 ) * gFooTexture.getHeight(); //Map colors Uint32 colorKey = SDL_MapRGB( SDL_GetWindowSurface( gWindow )->format, 0, 0xFF, 0xFF ); Uint32 transparent = SDL_MapRGBA( SDL_GetWindowSurface( gWindow )->format, 0xFF, 0xFF, 0xFF, 0x00 ); //Color key pixels for( int i = 0; i < pixelCount; ++i ) { if( pixels[ i ] == colorKey ) { pixels[ i ] = transparent; } } //Unlock texture gFooTexture.unlockTexture(); } } return success; }
void Dot::render() { //Show the dot gDotTexture.render( mPosX, mPosY ); }
int main(int argc, char *argv[]) { int quit = 0; /* event handler */ SDL_Event event; /* modulation components */ Uint8 r = 255; Uint8 g = 255; Uint8 b = 255; /* 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; /* on key press change rgb values */ else if (event.type == SDL_KEYDOWN) { switch (event.key.keysym.sym) { case SDLK_q: /* increase red */ r += 32; break; case SDLK_w: /* increase green */ g += 32; break; case SDLK_e: /* increase blue */ b += 32; break; case SDLK_a: /* decrease red */ r -= 32; break; case SDLK_s: /* decrease green */ g -= 32; break; case SDLK_d: /* decrease blue */ b -= 32; break; } } } /* clear screen */ SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF); SDL_RenderClear(gRenderer); /* modulate and render texture */ gModulatedTexture.setColor(r, g, b); gModulatedTexture.render(0, 0); /* update screen */ SDL_RenderPresent(gRenderer); } /* free resources and close */ close_sdl(); return 0; }
int main( int argc, char* args[]) { SDL_Color textColor = { 255, 255, 200, 255 }; // int i = 0; int timer = 0; int direction = 1; // 1 : left moving / 2 : right bool screenHit = false; int PlayerLifes = 3; std::stringstream PlayerLifesText; if( !init() ) { printf( "Failed to initialize!\n" ); } else { if( !loadMedia() ) { printf( "Failed to load media!\n" ); } else { bool quit = false; SDL_Event e; Player player(0, SCREEN_HEIGHT - gPlayerTexture.getHeight()); player.setTex(&gPlayerTexture); gameObjects.push_back(&player); createArmy(100, 50, 0); while( !quit ) { while( SDL_PollEvent( &e ) != 0 ) { if( e.type == SDL_QUIT ) { quit = true; } //if(e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_n) //{ // PlayerLifes = 1; // deleteShips(); // createArmy(100, 50, 0); //} bool shouldShoot = player.handleEvent( e ); if (shouldShoot) addProj(player); } if(PlayerLifes <= 0) { SDL_SetRenderDrawColor( gRenderer, 0x00, 0x00, 0x00, 0x00 ); SDL_RenderClear( gRenderer ); gLoseScreen.render(0, 0, gRenderer); } else { PlayerLifesText.str(""); PlayerLifesText << "Lifes " << PlayerLifes; gPlayerLifesTexture.loadFromRenderedText( PlayerLifesText.str().c_str(), textColor, gRenderer, gFont ); if( timer > 100 ) timer = 0; timer += 1; if( timer % 3 == 0) { if(ifScreenEndHit()) { screenHit = true; direction += 1; } movingShips(direction, screenHit); if(screenHit) screenHit = false; } if( timer % 50 == 0) { shipFire(); } ifProjCollShip(); projCollPlayer(player, PlayerLifes); //cout << PlayerLifes << endl; for(std::list<GameObject*>::iterator it = gameObjects.begin(); it != gameObjects.end();) { if( (*it)->alive == true ) (*it)->update(); ++it; } for(vector<Projectile*>::iterator it = shipProjects.begin(); it != shipProjects.end(); ++it) { if( (*it)->alive == true) (*it)->move(); } // RENDERING SDL_SetRenderDrawColor( gRenderer, 0x00, 0x00, 0x00, 0x00 ); SDL_RenderClear( gRenderer ); for(vector<Projectile*>::iterator it = shipProjects.begin(); it != shipProjects.end(); ++it) { if( (*it)->alive == true) (*it)->render(gRenderer); } for(std::list<GameObject*>::iterator it = gameObjects.begin(); it != gameObjects.end();) { if ((*it)->alive == true ) (*it)->render(gRenderer); ++it; } for(vector<vector<Ships*>>::iterator it = ships.begin(); it != ships.end(); ++it ) { for(vector<Ships*>::iterator itt = (*it).begin(); itt != (*it).end(); ++itt) { if( (*itt)->alive == true ) (*itt)->render(gRenderer); } } } gPlayerLifesTexture.render(5, 5, gRenderer); SDL_RenderPresent( gRenderer ); //deleteDeadShips(); m } } } close(); //system("pause"); 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 camera area SDL_Rect camera = { 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT }; //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(); //Center the camera over the dot camera.x = ( dot.getPosX() + Dot::DOT_WIDTH / 2 ) - SCREEN_WIDTH / 2; camera.y = ( dot.getPosY() + Dot::DOT_HEIGHT / 2 ) - SCREEN_HEIGHT / 2; //Keep the camera in bounds if( camera.x < 0 ) { camera.x = 0; } if( camera.y < 0 ) { camera.y = 0; } if( camera.x > LEVEL_WIDTH - camera.w ) { camera.x = LEVEL_WIDTH - camera.w; } if( camera.y > LEVEL_HEIGHT - camera.h ) { camera.y = LEVEL_HEIGHT - camera.h; } //Clear screen SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF ); SDL_RenderClear( gRenderer ); //Render background gBGTexture.render( 0, 0, &camera ); //Render objects dot.render( camera.x, camera.y ); //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; }
//Fonction principale du programme int main(int argc, char* args[]) { bool quit = false; //flag pour la boucle principale. Elle permet de déterminer si l'on doit quitter le programme int indexImageCharset = 0; //Cette variable nous permet de sélectionner une image du charset de l'homme int compteur = 0; //Ce compteur est utilisé pour déterminer à quel moment changer d'image dans 'homeCharset' SDL_Event e; //Cette variable nous permet de détecter l'événement courant que le programme doit gérer (click, touche du clavier, etc.) SDL_Rect* currentHommeRect = &hommeRect[indexImageCharset]; //Ce rectangle correspond à celui qu'on prendra pour afficher l'une des images du charset 'homeCharset' //On définit la position initial de l'image int positionHommeX = (LARGEUR_FENETRE - LARGEUR_IMAGE_HOMME_CHARSET) / 2; int positionHommeY = (HAUTEUR_FENETRE - HAUTEUR_IMAGE_HOMME_CHARSET) / 2; //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 { printf("Programme OK!\n"); //Netoyyer l'écran (Clear Screen) SDL_RenderClear(rendererFenetre); //Il est possible de définir une couleur de background du 'renderer' SDL_SetRenderDrawColor(rendererFenetre, 0xFF, 0xCC, 0xCC, 0xCC); //On dessine cette texture hommeTexture.render(0, 0, currentHommeRect); //Mise à jour de 'rendererFenetre' (on redesine les images (contenues dans leurs textures) dans le renderer) SDL_RenderPresent(rendererFenetre); //Le programme reste ouvert tant que quit != true while (!quit) { //Handle events on queue while (SDL_PollEvent(&e) != 0) { //Si l'utilisateur appuiie sur le bouton 'fermer' de la fenêtre if (e.type == SDL_QUIT) { quit = true; } //Si l'utilisateur fait un click de souris else if (e.type == SDL_MOUSEBUTTONDOWN) { quit = true; } //Si l'utilisateur dépplace la souris else if (e.type == SDL_MOUSEMOTION) { //On définit comme nouvelle position du fantôme celle de la souris positionHommeX = e.motion.x; positionHommeY = e.motion.y; } //Si une touche du clavier a été appuyée else if (e.type == SDL_KEYDOWN) { //Avec ce switch on indique qoui faire lorsque l'utilisateur appuie sur une touche switch (e.key.keysym.sym) { case SDLK_w: //La touche 'w' a été appuyée positionHommeX = 0; positionHommeY = 0; break; case SDLK_b: ////La touche 'b' a été appuyée positionHommeX = 250; positionHommeY = 250; break; case SDLK_UP: positionHommeY -= 10; break; case SDLK_DOWN: positionHommeY += 10; break; case SDLK_LEFT: positionHommeX -= 10; break; case SDLK_RIGHT: positionHommeX += 10; break; default: break; }//end switch }//end else if (e.type == SDL_KEYDOWN) }//end while (SDL_PollEvent(&e) != 0) //Clear screen SDL_RenderClear(rendererFenetre); /*std::cout << "indexImageCharset = " << indexImageCharset << std::endl; std::cout << "compteur = " << compteur << std::endl;*/ currentHommeRect = &hommeRect[indexImageCharset]; hommeTexture.render(positionHommeX, positionHommeY, currentHommeRect); //Update screen SDL_RenderPresent(rendererFenetre); compteur++; //On détermine si on change d'image, selon le facteur de ralentissement if (compteur % FACTEUR_RALENTISSEMENT_ANIMATION == 0) { indexImageCharset++; //Si l'index est égal à la taille du tableau de rectangles if (indexImageCharset == NOMBRE_IMAGES_HOMME_CHARSET) { indexImageCharset = 0; compteur = 0; } } //SDL_Delay(1000); }//end while (!quit) } } //system("pause"); //Free resources and close SDL close(); return 0; }
int main( int argc, char* args[] ) { bool inside = true; //Start up SDL and create window if( !init() ) { printf( "Failed to initialize!\n" ); } else { 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 button events for( int i = 0; i < 4; ++i ) { gButtons[ i ].handleEvent( &e ); } } //Clear screen SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF ); SDL_RenderClear( gRenderer ); //Render background texture to screen gBackgroundTexture.render( 0, 0 ); //Render Foo' to the screen gFooTexture.render( 0, 0 ); //Render buttons gButtonSpriteSheetTexture.loadFromFile( "res/menu/555.png" ); for( int i = 0; i < TOTAL_BUTTONS; ++i ) { gButtons[ 0 ].render(); /*switch(e.type) { case SDL_MOUSEBUTTONDOWN: SDL_Quit(); break; }*/ } gButtonSpriteSheetTexture.loadFromFile( "res/menu/options.png" ); for( int i = 0; i < TOTAL_BUTTONS; ++i ) { gButtons[ 1 ].render(); } gButtonSpriteSheetTexture.loadFromFile( "res/menu/exit.png" ); for( int i = 0; i < TOTAL_BUTTONS; ++i ) { gButtons[ 2 ].render(); /*if (event->type == SDL_MOUSEBUTTONDOWN) { if (event->button.button == SDL_BUTTON_LEFT) { //if it is pressed then play1 becomes true which you could use to initiate the newgame play1 = true; } } { LButton().mCurrentSprite = BUTTON_SPRITE_MOUSE_OUT; } else { switch(e.type) { case SDL_MOUSEBUTTONUP: SDL_Quit(); break; } }*/ break; } gButtonSpriteSheetTexture.loadFromFile( "res/menu/options.png" ); for( int i = 0; i < TOTAL_BUTTONS; ++i ) { gButtons[ 3 ].render(); } //Update screen SDL_RenderPresent( gRenderer ); } } } 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, 0xaa, 0xFF, 0xFF, 0xFF ); SDL_RenderClear( gRenderer ); //Render top left sprite gSpriteSheetTexture.render( 20, 30, &gSpriteClips[ 0 ] ); gSpriteSheetTexture.render( 200, 150, &gSpriteClips[ 0 ] ); //Render top right sprite gSpriteSheetTexture.render( SCREEN_WIDTH - gSpriteClips[ 1 ].w, 0, &gSpriteClips[ 1 ] ); //Render bottom left sprite gSpriteSheetTexture.render( 0, SCREEN_HEIGHT - gSpriteClips[ 2 ].h, &gSpriteClips[ 2 ] ); //Render bottom right sprite gSpriteSheetTexture.render( SCREEN_WIDTH - gSpriteClips[ 3 ].w, SCREEN_HEIGHT - gSpriteClips[ 3 ].h, &gSpriteClips[ 3 ] ); //Update screen SDL_RenderPresent( gRenderer ); } } } //Free resources and close SDL close(); return 0; }
void Dot::render() { //Show the dot gDotTexture.render( (int)mPosX, (int)mPosY ); }
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 key press else if( e.type == SDL_KEYDOWN ) { switch( e.key.keysym.sym ) { //Play high sound effect case SDLK_1: Mix_PlayChannel( -1, gHigh, 0 ); break; //Play medium sound effect case SDLK_2: Mix_PlayChannel( -1, gMedium, 0 ); break; //Play low sound effect case SDLK_3: Mix_PlayChannel( -1, gLow, 0 ); break; //Play scratch sound effect case SDLK_4: Mix_PlayChannel( -1, gScratch, 0 ); break; case SDLK_9: //If there is no music playing if( Mix_PlayingMusic() == 0 ) { //Play the music Mix_PlayMusic( gMusic, -1 ); } //If music is being played else { //If the music is paused if( Mix_PausedMusic() == 1 ) { //Resume the music Mix_ResumeMusic(); } //If the music is playing else { //Pause the music Mix_PauseMusic(); } } break; case SDLK_0: //Stop the music Mix_HaltMusic(); break; } } } //Clear screen SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF ); SDL_RenderClear( gRenderer ); //Render prompt gPromptTexture.render( 0, 0 ); //Update screen SDL_RenderPresent( gRenderer ); } } } //Free resources and close SDL close(); return 0; }
void LButton::render() { //Show current button sprite gButtonSpriteSheetTexture.render( mPosition.x, mPosition.y, &gSpriteClips[ mCurrentSprite ] ); }
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; //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; } if( e.type == SDL_MOUSEBUTTONUP) { ::get_subrect_center_point(&e,&selected_point); //通过距离判断两个subrect的相邻关系 if(::distance(&::selected_point, &::space_point) == 1) { //更新subrect_texture_index subrect_texture_index[::space_point.get_y() * 3 + ::space_point.get_x()] = subrect_texture_index[::selected_point.get_y() * 3 + ::selected_point.get_x()]; ::subrect_texture_index[::selected_point.get_y() * 3 + ::selected_point.get_x()] = 8; //更新space_point ::space_point.set_x(::selected_point.get_x()); ::space_point.set_y(::selected_point.get_y()); } } //Clear screen SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF ); SDL_RenderClear( gRenderer ); for(int i = 0; i <= 2; i++) for(int j = 0; j <= 2; j++) { int tmp = subrect_texture_index[j * 3 + i]; gSpriteSheetTexture.render(i * 250, j*200, &gSpriteClips[tmp]); } SDL_RenderPresent( gRenderer ); } } } } //Free resources and close SDL close(); return 0; }
void Dot::render( int camX, int camY ) { //Show the dot relative to the camera gDotTexture.render( mPosX - camX, mPosY - camY ); }
int main( int argc, char* args[] ) { bool inside = true; //Start up SDL and create window if( !init() ) { printf( "Failed to initialize!\n" ); } else { 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 button events for( int i = 0; i < 4; ++i ) { gButtons[ i ].handleEvent( &e ); } } //Clear screen SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF ); SDL_RenderClear( gRenderer ); //Render background texture to screen gBackgroundTexture.render( 0, 0 ); //Render Foo' to the screen gFooTexture.render( 0, 0 ); gButtonSpriteSheetTexture.loadFromFile( "res/menu/control.png" ); for( int i = 0; i < TOTAL_BUTTONS; ++i ) { gButtons[ 0 ].render(); } SDL_RenderPresent( gRenderer ); } } } 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; //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; }
void Ball::render(){ //Show the ball gBallTexture.render(mPosX, mPosY); }
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); }
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; }
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* 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; }
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; //Run the threads srand( SDL_GetTicks() ); SDL_Thread* threadA = SDL_CreateThread( worker, "Thread A", (void*)"Thread A" ); SDL_Delay( 16 + rand() % 32 ); SDL_Thread* threadB = SDL_CreateThread( worker, "Thread B", (void*)"Thread B" ); //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 splash gSplashTexture.render( 0, 0 ); //Update screen SDL_RenderPresent( gRenderer ); } //Wait for threads to finish SDL_WaitThread( threadA, NULL ); SDL_WaitThread( threadB, NULL ); } } //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; //Current rendered texture LTexture* currentTexture = NULL; //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; } } //Set texture based on current keystate const Uint8* currentKeyStates = SDL_GetKeyboardState( NULL ); if( currentKeyStates[ SDL_SCANCODE_UP ] ) { currentTexture = &gUpTexture; } else if( currentKeyStates[ SDL_SCANCODE_DOWN ] ) { currentTexture = &gDownTexture; } else if( currentKeyStates[ SDL_SCANCODE_LEFT ] ) { currentTexture = &gLeftTexture; } else if( currentKeyStates[ SDL_SCANCODE_RIGHT ] ) { currentTexture = &gRightTexture; } else { currentTexture = &gPressTexture; } //Clear screen SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF ); SDL_RenderClear( gRenderer ); //Render current texture currentTexture->render( 0, 0 ); //Update screen SDL_RenderPresent( gRenderer ); } } } close(); return 0; }