Posted October 24, 2020 by Oblerion Studio
void setPixel(SDL_Surface *surface, int x, int y, SDL_Color col) { Uint32 pixel = SDL_MapRGBA(surface->format, col.r, col.g, col.b, col.a); Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * surface->format->BytesPerPixel; if(surface->format->BytesPerPixel == 3) { if(SDL_BYTEORDER == SDL_BIG_ENDIAN) { p[0] = (pixel >> 16) & 0xff; p[1] = (pixel >> 8) & 0xff; p[2] = pixel & 0xff; } else { p[0] = pixel & 0xff; p[1] = (pixel >> 8) & 0xff; p[2] = (pixel >> 16) & 0xff; } } if(surface->format->BytesPerPixel == 4) *(Uint32 *)p = pixel; }
SDL_Texture* convertData(vector<vector<SDL_Color>> tab) { SDL_Texture *texture; // ne peut generer que des surface noire SDL_Surface *mysurface = SDL_CreateRGBSurface(0, tab.size(), tab[0].size(), 32,0,0,0,0); if(mysurface == nullptr) Test("null"); SDL_LockSurface(mysurface); /*On bloque la surface*/ for(unsigned int i=0;i< tab.size();i++) { for(unsigned int j=0;j< tab[0].size();j++) { if(tab[i][j].a != 0) setPixel(mysurface,i,j, {tab[i][j].r,tab[i][j].g,tab[i][j].b,tab[i][j].a}); else setPixel(mysurface,i,j,{1,1,1,0}); } } Uint32 colorkey = SDL_MapRGBA( mysurface->format,1,1,1,0);//pixel a suprimer SDL_SetColorKey(mysurface, SDL_TRUE,colorkey);// supression pixel SDL_UnlockSurface(mysurface); /*On libère la surface, elle peut être utilisée*/ texture = SDL_CreateTextureFromSurface(renderer,mysurface); SDL_FreeSurface(mysurface); return texture; }