Add method to split surface into 4 pieces

This commit is contained in:
Mylloon 2022-05-08 23:45:26 +02:00
parent 4b0521bdfa
commit 5cc1e108e6
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

View file

@ -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<SDL_Surface *, 4> QuadTree::coupeEnQuatre(SDL_Surface *) { /* return std::array<SDL_Surface *, 4>(); */ }
std::array<SDL_Surface *, 4> QuadTree::coupeEnQuatre(SDL_Surface * s) {
// Coordonnées des extrémités de chaque quadrant
std::array<std::array<int, 4>, 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<SDL_Surface *, 4> 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<Uint32 *>(
static_cast<Uint8 *>(nouvelle_image->pixels) + y * nouvelle_image->pitch + x * nouvelle_image->format->BytesPerPixel
) = *reinterpret_cast<Uint32 *>(
static_cast<Uint8 *>(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;
}