Saturday, August 6, 2022
HomeGame Developmentc++ - how do I chage pixel coloration? (SDL2)

c++ – how do I chage pixel coloration? (SDL2)


After a very long time away, I am coming again to SDL, utilizing model SDL2.
I’ve some instance code that does just about precisely what I would like it to do, however I wish to change the pixel coloration. Ive tried altering the operate of *pixels, to make pixels a floor the place I may need the choice to alter coloration, however I cant appear to determine how one can change the colour.

#embody <iostream>
#embody <SDL2/SDL.h>
//Supply: https://bitbucket.org/dandago/gigilabs/src/grasp/Sdl2PixelDrawing/Sdl2PixelDrawing/primary.cpp
//Outline window peak, width
#outline peak 1280
#outline width   760
int primary(int argc, char ** argv){
    bool leftMouseButtonDown = false;
    bool stop = false;
    SDL_Event occasion;
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window * window = SDL_CreateWindow("SDL2 Pixel Drawing",  SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, peak, width, SDL_WINDOW_FULLSCREEN);

    SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, 0);
    SDL_Texture * texture = SDL_CreateTexture(renderer,  SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, peak, width);
    Uint32 * pixels = new Uint32[height * width];
 
//SDL_Surface* pixels = SDL_CreateRGBSurfaceWithFormat(0, width, peak,32, SDL_PIXELFORMAT_RGBX8888);

 
    memset(pixels, 255, peak * width * sizeof(Uint32)); //int is bg coloration

    whereas (!stop){
        SDL_UpdateTexture(texture, NULL, pixels, peak * sizeof(Uint32));
        SDL_WaitEvent(&occasion);

        change (occasion.kind){
            case SDL_QUIT:
                stop = true;
                break;
            case SDL_MOUSEBUTTONUP:
                if (occasion.button.button == SDL_BUTTON_LEFT)
                    leftMouseButtonDown = true;
                break;
            case SDL_MOUSEBUTTONDOWN:
                if (occasion.button.button == SDL_BUTTON_LEFT)
                    leftMouseButtonDown = true;
            case SDL_MOUSEMOTION:
                if (leftMouseButtonDown){
                    int mouseX = occasion.movement.x;
                    int mouseY = occasion.movement.y;
                    
                    pixels[mouseY * height+ mouseX] = 0;
                     }
                break;
        }
        SDL_RenderClear(renderer);
        SDL_RenderCopy(renderer, texture, NULL, NULL);
        SDL_RenderPresent(renderer);
    }

    delete[] pixels;
    SDL_DestroyTexture(texture);
    SDL_DestroyRenderer(renderer);

    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments