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 ); //Copy frame from buffer gStreamingTexture.lockTexture(); gStreamingTexture.copyPixels( gDataStream.getBuffer() ); gStreamingTexture.unlockTexture(); //Render frame gStreamingTexture.render( ( SCREEN_WIDTH - gStreamingTexture.getWidth() ) / 2, ( SCREEN_HEIGHT - gStreamingTexture.getHeight() ) / 2 ); //Update screen SDL_RenderPresent( gRenderer ); } } } //Free resources and close SDL close(); 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; }
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; }