how do I chage pixel color? (SDL2)


After a long time away, I’m coming back to SDL, using version SDL2.
I have some example code that does pretty much exactly what I want it to do, but I want to change the pixel color. Ive tried changing the function of *pixels, to make pixels a surface where I might have the option to change color, but I cant seem to figure out how to change the color.

#include <iostream>
#include <SDL2/SDL.h>
//Source: 
//Define window height, width
#define height 1280
#define width   760
int main(int argc, char ** argv)
    bool leftMouseButtonDown = false;
    bool quit = false;
    SDL_Event event;
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window * window = SDL_CreateWindow("SDL2 Pixel Drawing",  SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, height, width, SDL_WINDOW_FULLSCREEN);

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

 
    memset(pixels, 255, height * width * sizeof(Uint32)); //int is bg color

    while (!quit)
        SDL_UpdateTexture(texture, NULL, pixels, height * sizeof(Uint32));
        SDL_WaitEvent(&event);

        switch (event.type)
            case SDL_QUIT:
                quit = true;
                break;
            case SDL_MOUSEBUTTONUP:
                if (event.button.button == SDL_BUTTON_LEFT)
                    leftMouseButtonDown = true;
                break;
            case SDL_MOUSEBUTTONDOWN:
                if (event.button.button == SDL_BUTTON_LEFT)
                    leftMouseButtonDown = true;
            case SDL_MOUSEMOTION:
                if (leftMouseButtonDown)
                    int mouseX = event.motion.x;
                    int mouseY = event.motion.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;



Source link

Leave a Reply

Your email address will not be published.

Previous Article

How to use matrices in 2D

Next Article

Activision Blizzard's mobile games make more money than console and PC sales combined

Related Posts