From 5cc1e108e602b7755da81e45fc43954b5b03a8f3 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sun, 8 May 2022 23:45:26 +0200 Subject: [PATCH] Add method to split surface into 4 pieces --- src/quadtree.cpp | 55 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/src/quadtree.cpp b/src/quadtree.cpp index 0b9c686..3876ce8 100644 --- a/src/quadtree.cpp +++ b/src/quadtree.cpp @@ -10,6 +10,10 @@ QuadTree::QuadTree(SDL_Surface * image, short niveau_p): niveau(niveau_p), coule nord_est = new QuadTree(image_coupe.at(1), niveau + 1); sud_ouest = new QuadTree(image_coupe.at(2), niveau + 1); sud_est = new QuadTree(image_coupe.at(3), niveau + 1); + + for(SDL_Surface * it: image_coupe) { + SDL_FreeSurface(it); + } } } @@ -46,4 +50,53 @@ SDL_Color QuadTree::calculeCouleur(SDL_Surface *) { /* return SDL_Color(); */ } bool QuadTree::verificationEgalitee(SDL_Surface *) { /* return true; */ } -std::array QuadTree::coupeEnQuatre(SDL_Surface *) { /* return std::array(); */ } +std::array QuadTree::coupeEnQuatre(SDL_Surface * s) { + // Coordonnées des extrémités de chaque quadrant + std::array, 4> coordonnes_quadrants; + coordonnes_quadrants[0] = {0 , 0 , s->w / 2, s->h / 2}; + coordonnes_quadrants[1] = {0 , s->h / 2, s->w / 2, s->h}; + coordonnes_quadrants[2] = {s->w / 2, 0 , s->w , s->h / 2}; + coordonnes_quadrants[3] = {s->w / 2, s->h / 2, s->w , s->h}; + + // Masque dépend de l'ordre des octets de la machine + Uint32 r_mask, g_mask, b_mask, a_mask; + if(SDL_BYTEORDER == SDL_BIG_ENDIAN) { + r_mask = 0xff000000; + g_mask = 0x00ff0000; + b_mask = 0x0000ff00; + a_mask = 0x000000ff; + } else { + r_mask = 0x000000ff; + g_mask = 0x0000ff00; + b_mask = 0x00ff0000; + a_mask = 0xff000000; + } + + std::array resultat; + for(Uint32 i = 0; i < 4; ++i) { + // Création d'une nouvelle surface + // Copie pixel par pixel de la surface mère vers la surface fille + // Stockage de la nouvelle surface dans l'array `resultat` + SDL_Surface * nouvelle_image = SDL_CreateRGBSurface(0, + coordonnes_quadrants[i][2] - coordonnes_quadrants[i][0], + coordonnes_quadrants[i][3] - coordonnes_quadrants[i][1], + 32, r_mask, g_mask, b_mask, a_mask); + + if(SDL_LockSurface(nouvelle_image) == 0) { + for(int x = 0; x < nouvelle_image->w; ++x) { + for(int y = 0; y < nouvelle_image->h; ++y) { + *reinterpret_cast( + static_cast(nouvelle_image->pixels) + y * nouvelle_image->pitch + x * nouvelle_image->format->BytesPerPixel + ) = *reinterpret_cast( + static_cast(s->pixels) + (coordonnes_quadrants[i][1] + y) * s->pitch + (coordonnes_quadrants[i][0] + x) * s->format->BytesPerPixel + ); + } + } + + SDL_UnlockSurface(nouvelle_image); + resultat[i] = nouvelle_image; + } + } + + return resultat; +}