Home Game Development sdl2 – How can I instantly write pixel information in an SDL window utilizing surfaces?

sdl2 – How can I instantly write pixel information in an SDL window utilizing surfaces?

0
sdl2 – How can I instantly write pixel information in an SDL window utilizing surfaces?

[ad_1]

I’d wish to instantly modify pixels in my window in SDL 2. I anticipate this code to show a wholly pink window, however as an alternative I’m getting an all black window.

I’m in a position to get it working if I instantly write to window_surface with the identical method, however not by blitting one other floor. I’d wish to get it working this fashion no less than only for the needs of understanding.

#embrace <SDL.h>

int foremost()
{
    SDL_Init(SDL_INIT_EVERYTHING);

    SDL_Window *window = SDL_CreateWindow("Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1600, 900, 0);

    SDL_Surface *window_surface = SDL_GetWindowFloor(window);

    SDL_Surface *canvas = SDL_CreateRGBSurfaceWithFormat(
        0, 1600, 900, 32, SDL_PIXELFORMAT_RGBA8888
    );

    Uint32 *buffer = (Uint32*) canvas->pixels;
    int row    = 0;
    int column = 0;
    int offset;
    Uint32 colour;
    SDL_UnlockSurface(canvas);
    whereas (row < 900)
    {
        column = 0;
        whereas (column < 1600)
        {
            offset = row * 1600 + column;
            colour = SDL_MapRGBA(canvas->format, 255, 0, 0, 255);
            buffer[offset] = colour;
            column++;
        }
        row++;
    }
    SDL_LockSurface(canvas);

    int give up = 0;
    SDL_Event occasion;
    whereas (!give up)
    {
        whereas (SDL_PollEvent(&occasion))
        {
            if (occasion.sort == SDL_QUIT)
            {
                give up = 1;
            }
        }

        SDL_BlitSurface(canvas, 0, window_surface, 0);
        SDL_UpdateWindowFloor(window);

        SDL_Delay(10);
    }

    SDL_Quit();

    return 0;
}

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here