diff --git a/source/code/BlockGameSdl.hpp b/source/code/BlockGameSdl.hpp index 5b4a676..17768d2 100644 --- a/source/code/BlockGameSdl.hpp +++ b/source/code/BlockGameSdl.hpp @@ -234,121 +234,8 @@ private: } } - /** - * Creates a new texture for a garabge block of size size_x*size_y at x,y and crops it. - * Also sets globalData.screen to point to the texture, so Draw now draws on the Texture. - */ - SDL_Texture* CreateGarbageTexture(int x, int y, int size_x, int size_y, SDL_Rect& dstrect, SDL_Rect& srcrect) const { - dstrect = { x, y, size_x*bsize, size_y*bsize }; - srcrect = { 0, 0, size_x*bsize, size_y*bsize }; - SDL_Rect bound = {topx, topy, BOARD_WIDTH, BOARD_HEIGHT}; - CropTexture (dstrect, srcrect, bound); - SDL_Texture* mTexture = SDL_CreateTexture( globalData.screen, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, size_x*bsize, size_y*bsize ); - SDL_SetRenderTarget( globalData.screen, mTexture ); - - SDL_SetRenderDrawBlendMode(globalData.screen, SDL_BLENDMODE_NONE); - SDL_SetTextureBlendMode(mTexture, SDL_BLENDMODE_BLEND); - SDL_SetRenderDrawColor(globalData.screen, 0, 0, 0, 0); - SDL_RenderFillRect(globalData.screen, NULL); - SDL_SetRenderDrawBlendMode(globalData.screen, SDL_BLENDMODE_BLEND); - return mTexture; - } - - /** - Paints a garbage block of size size_x*size_y (size in blocks) at x,y (coordinates in pixels). - The garbage block will be painted on an internal texture and rendered to screen to protect against tearing. - The result will be cropped to the playable area. - */ - void PaintGarbageBlock(int x, int y, int size_x, int size_y) const { - SDL_Rect dstrect ; - SDL_Rect srcrect ; - SDL_Texture* mTexture = CreateGarbageTexture(x, y, size_x, size_y, dstrect, srcrect); - const Sint32 draw_time = SDL_GetTicks(); - - if (size_y == 1) { - //single line - globalData.garbageML.Draw(globalData.screen, draw_time, 0, 0); - for (int i = 1; i < size_x-1; ++i) { - globalData.garbageM.Draw(globalData.screen, draw_time, i*bsize, 0); - } - globalData.garbageMR.Draw(globalData.screen, draw_time, (size_x-1)*bsize, 0); - } - if (size_y > 1) { - // Top line - globalData.garbageTL.Draw(globalData.screen, draw_time, 0, 0); - for (int i = 1; i < size_x-1; ++i) { - globalData.garbageT.Draw(globalData.screen, draw_time, i*bsize, 0); - } - globalData.garbageTR.Draw(globalData.screen, draw_time, (size_x-1)*bsize, 0); - // Middle lines (if any) - for (int j=1; j < size_y-1; ++j) { - globalData.garbageL.Draw(globalData.screen, draw_time, 0, j*bsize); - for (int i = 1; i < size_x-1; ++i) { - globalData.garbageFill.Draw(globalData.screen, draw_time, i*bsize, j*bsize); - } - globalData.garbageR.Draw(globalData.screen, draw_time, (size_x-1)*bsize, j*bsize); - } - //Buttom line - globalData.garbageBL.Draw(globalData.screen, draw_time, 0, (size_y-1)*bsize); - for (int i = 1; i < size_x-1; ++i) { - globalData.garbageB.Draw(globalData.screen, draw_time, i*bsize, (size_y-1)*bsize); - } - globalData.garbageBR.Draw(globalData.screen, draw_time, (size_x-1)*bsize, (size_y-1)*bsize); - } - - SDL_SetRenderTarget( globalData.screen, nullptr ); - SDL_RenderCopy( globalData.screen, mTexture, &srcrect, &dstrect ); - SDL_DestroyTexture(mTexture); - } - - /** - Paints a grey garbage block of size size_x (size in blocks, always 6) at x,y (coordinates in pixels). - The result will be cropped to the playable area. - */ - void PaintGreyGarbageBlock(int x, int y, int size_x) const { - const int size_y = bsize; - SDL_Rect dstrect ; - SDL_Rect srcrect ; - SDL_Texture* mTexture = CreateGarbageTexture(x, y, size_x, size_y, dstrect, srcrect); - const Sint32 draw_time = SDL_GetTicks(); - - for (int j=0; j < size_y; ++j) { - if (j==0) { - globalData.garbageGML.Draw(globalData.screen, draw_time, j*bsize, 0); - } - else if (j==5) { - globalData.garbageGMR.Draw(globalData.screen, draw_time, j*bsize, 0); - } - else { - globalData.garbageGM.Draw(globalData.screen, draw_time, j*bsize, 0); - } - } - - SDL_SetRenderTarget( globalData.screen, nullptr ); - SDL_RenderCopy( globalData.screen, mTexture, &srcrect, &dstrect ); - SDL_DestroyTexture(mTexture); - } - - std::pair getGarbageSize(int topx, int topy) const { - int size_x = 0; - int size_y = 0; - int number = board[topx][topy]; - for (int i=topy; i < 30 ; ++i) { - if (number == board[topx][i]) { - ++size_y; - } - } - for (int i=topx; i < 7 ; ++i) { - if (number == board[i][topy]) { - ++size_x; - } - } - return std::make_pair(size_x, size_y); - } - //Draws all the bricks to the board (including garbage) void PaintBricks() const { - int lastGarbageNumber = 0; for (int i=0; i<13; ++i) { for (int j=0; j<6; ++j) { int basicBrick = board[j][i]%10; //The basic brick, stored on the least significant digit @@ -363,19 +250,78 @@ private: } if ((board[j][i]/1000000)%10==1) { + int left, right, over, under; int number = board[j][i]; - if (number != lastGarbageNumber) { - const auto& garbage_size = getGarbageSize(j, i); - lastGarbageNumber = number; - PaintGarbageBlock(topx+j*bsize, topy+12*bsize-i*bsize-(garbage_size.second-1)*bsize -pixels, garbage_size.first, garbage_size.second); + if (j<1) { + left = -1; + } + else { + left = board[j-1][i]; + } + if (j>=5) { + right = -1; + } + else { + right = board[j+1][i]; + } + if (i>28) { + over = -1; + } + else { + over = board[j][i+1]; + } + if (i<1) { + under = -1; + } + else { + under = board[j][i-1]; + } + if ((left == number)&&(right == number)&&(over == number)&&(under == number)) { + DrawImgBoardBounded(globalData.garbageFill, j*bsize, bsize*12-i*bsize-pixels); + } + if ((left != number)&&(right == number)&&(over == number)&&(under == number)) { + DrawImgBoardBounded(globalData.garbageL, j*bsize, bsize*12-i*bsize-pixels); + } + if ((left == number)&&(right != number)&&(over == number)&&(under == number)) { + DrawImgBoardBounded(globalData.garbageR, j*bsize, bsize*12-i*bsize-pixels); + } + if ((left == number)&&(right == number)&&(over != number)&&(under == number)) { + DrawImgBoardBounded(globalData.garbageT, j*bsize, bsize*12-i*bsize-pixels); + } + if ((left == number)&&(right == number)&&(over == number)&&(under != number)) { + DrawImgBoardBounded(globalData.garbageB, j*bsize, bsize*12-i*bsize-pixels); + } + if ((left != number)&&(right == number)&&(over != number)&&(under == number)) { + DrawImgBoardBounded(globalData.garbageTL, j*bsize, bsize*12-i*bsize-pixels); + } + if ((left != number)&&(right == number)&&(over == number)&&(under != number)) { + DrawImgBoardBounded(globalData.garbageBL, j*bsize, bsize*12-i*bsize-pixels); + } + if ((left == number)&&(right != number)&&(over != number)&&(under == number)) { + DrawImgBoardBounded(globalData.garbageTR, j*bsize, bsize*12-i*bsize-pixels); + } + if ((left == number)&&(right != number)&&(over == number)&&(under != number)) { + DrawImgBoardBounded(globalData.garbageBR, j*bsize, bsize*12-i*bsize-pixels); + } + if ((left == number)&&(right != number)&&(over != number)&&(under != number)) { + DrawImgBoardBounded(globalData.garbageMR, j*bsize, bsize*12-i*bsize-pixels); + } + if ((left == number)&&(right == number)&&(over != number)&&(under != number)) { + DrawImgBoardBounded(globalData.garbageM, j*bsize, bsize*12-i*bsize-pixels); + } + if ((left != number)&&(right == number)&&(over != number)&&(under != number)) { + DrawImgBoardBounded(globalData.garbageML, j*bsize, bsize*12-i*bsize-pixels); } } if ((board[j][i]/1000000)%10==2) { - int number = board[j][i]; - if (number != lastGarbageNumber) { - const auto& garbage_size = getGarbageSize(j, i); - lastGarbageNumber = number; - PaintGreyGarbageBlock(topx+j*bsize, topy+12*bsize-i*bsize-(garbage_size.second-1)*bsize -pixels, garbage_size.first); + if (j==0) { + DrawImgBoardBounded(globalData.garbageGML, j*bsize, bsize*12-i*bsize-pixels); + } + else if (j==5) { + DrawImgBoardBounded(globalData.garbageGMR, j*bsize, bsize*12-i*bsize-pixels); + } + else { + DrawImgBoardBounded(globalData.garbageGM, j*bsize, bsize*12-i*bsize-pixels); } } }