[SDL] Pixel färben



  • Hi, ich wolte wissen, wie man mit hilfe von SDL einen einzelnen Pixel färbt ?

    gibts da ne Funktion? oder sollte ich ein Bild mit 1 px x 1px hin blitten?

    MFG



  • Da gibts in einem Tutorial eine Funktion.
    Wenn du mehrere Pixel malst, solltest du aber unbedingt das SDL_LockSurface außerhalb der Funktion schreiben.

    void DrawPixel(SDL_Surface *screen, int x, int y,
    Uint8 R, Uint8 G,
    Uint8 B)
    {
        Uint32 color = SDL_MapRGB(screen->format, R, G, B);
    
        if ( SDL_MUSTLOCK(screen) ) {
            if ( SDL_LockSurface(screen) < 0 ) {
                return;
            }
        }
        switch (screen->format->BytesPerPixel) {
            case 1: { /* vermutlich 8 Bit */
                Uint8 *bufp;
    
                bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
                *bufp = color;
            }
            break;
    
            case 2: { /* vermutlich 15 Bit oder 16 Bit */
                Uint16 *bufp;
    
                bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;
                *bufp = color;
            }
            break;
    
            case 3: { /* langsamer 24-Bit-Modus, selten verwendet */
                Uint8 *bufp;
    
                bufp = (Uint8 *)screen->pixels + y*screen->pitch + x * 3;
                if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {
                    bufp[0] = color;
                    bufp[1] = color >> 8;
                    bufp[2] = color >> 16;
                } else {
                    bufp[2] = color;
                    bufp[1] = color >> 8;
                    bufp[0] = color >> 16;
                }
            }
            break;
    
            case 4: { /* vermutlich 32 Bit */
                Uint32 *bufp;
    
                bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;
                *bufp = color;
            }
            break;
        }
        if ( SDL_MUSTLOCK(screen) ) {
            SDL_UnlockSurface(screen);
        }
        SDL_UpdateRect(screen, x, y, 1, 1);
    }
    


  • thx,

    hab aber selbst schneller was probiert, und siehe da es funtzt:

    ...
    
    SDL_Surface *screen;
    
    SDL_Rect rect;
    
    rect.x=100;
    rect.y=100;
    rect.h=1;
    rect.w=1;
    
    Uint32 color = 0xFFFFFFFF;
    
    SDL_FillRect(screen, &rect, color);
    

    ist das langsamer?


Anmelden zum Antworten