2017-04-06 4 views
0

Ich versuche herauszufinden, wie ein farbiges Rechteck auf einem unsigned char Pixel-Array aus dem pixels Mitglied eines gesperrten SDL_Surface gezeichnet wird.Zeichnen eines Rechtecks ​​auf SDL_Surface Pixel

Die folgende Funktion soll die ehemalige tun:

void draw_rectangle(SDL_Surface* surface, int x, int y, int width, int height) 
{ 
    SDL_LockSurface(surface); 
    //Make each pixel black 
    std::vector<uint8_t> pixels(surface->h * surface->pitch, 0); 

    for (int dy = y; dy < height; dy++) { 
     for (int dx = x; dx < width; dx++) { 
      pixels[dx + dy] = 0; 
      pixels[dx + dy + 1] = 255; 
      pixels[dx + dy + 2] = 0; 
     } 
    } 
    memcpy(surface->pixels, pixels.data(), surface->pitch * surface->h); 
    SDL_UnlockSurface(surface); 
} 

Es funktioniert, aber wenn es durch die Umwandlung der modifizierten Oberfläche auf eine Textur Prüfung SDL_CreateTextureFromSurface und Kopieren der Textur auf dem Bildschirm verwendet wird, zeigt es nur eine grüner Pixel:

window screenshot

Antwort

0

ich erkannt, dass meine Pointer-Arithmetik war falsch, ich brauchte die vertikalen Versatz zu berücksichtigen, da ich 2D-Werte zu eindimensional Vam Abbilden alues. Es hilft, Ideen auf Papier zu entwerfen.

Hier ist der Code:

void draw_rectangle(SDL_Surface* surface, int x, int y, int width, int height) 
{ 
    SDL_LockSurface(surface); 
    std::vector<uint8_t> pixels(surface->h * surface->pitch, 0); 

    int dy, dx; 
    int maxwidth = width * 3; 
    for (dy = y; dy < height; dy++) { 
     for (dx = x; dx < maxwidth; dx += 3) { 
      pixels[dx + (dy * surface->pitch)] = 0; 
      pixels[dx + (dy * surface->pitch) + 1] = 255; 
      pixels[dx + (dy * surface->pitch) + 2] = 0; 
     } 
    } 
    memcpy(surface->pixels, pixels.data(), surface->pitch * surface->h); 

    SDL_UnlockSurface(surface); 
} 

screenshot

Verwandte Themen