0
\$\begingroup\$

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: https://bitbucket.org/dandago/gigilabs/src/master/Sdl2PixelDrawing/Sdl2PixelDrawing/main.cpp //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; } 
\$\endgroup\$
5
  • 1
    \$\begingroup\$ You mean this pixels[mouseY * height+ mouseX] = 0; pixel color? Choose a value other than 0 then. \$\endgroup\$ Commented Aug 6, 2022 at 17:13
  • \$\begingroup\$ I tried that, a whole range of numbers between 0-255. .. oh wait pixels is uint32, snap, I might not have noticed a difference at that scale. \$\endgroup\$ Commented Aug 6, 2022 at 17:55
  • \$\begingroup\$ FFFF--... Thanks, that was it. \$\endgroup\$ Commented Aug 6, 2022 at 17:58
  • \$\begingroup\$ oh wait pixels is uint32 Yeah, its SDL_PIXELFORMAT_ARGB8888 which means alpha byte, red byte, green byte and blue byte, stuffed together in an uint32. Might be reversed though due to little endian. \$\endgroup\$ Commented Aug 6, 2022 at 17:59
  • 1
    \$\begingroup\$ If you've solved your problem, be sure to post your solution as an answer below. \$\endgroup\$ Commented Aug 6, 2022 at 19:20

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.