Create a perfect brush SDL

I'm making a program which helps to create drawings. The problem is that the brush is not perfect. When the mouse moves slow , the brush is good , but when it moves faster the brush leaves a lot of space.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
while(SDL_PollEvent( &event ) )
        {
            //While there is an event happening
            if(event.type == SDL_MOUSEBUTTONDOWN)
            {
                //If the event is mouse button down

                SDL_Rect drawing; //Declare a rectangle which will be drawn to screen (that variable name tho :P )
                drawing.x = event.button.x; //The x value of the rectangle is the x location of the mouse
                drawing.y = event.button.y; //The y value of the rectangle is the y location of the mouse
                if(eraser == 1) //If the eraser is enabled
                {
                    drawing.w = 50; //Set the width of the rectangle to 50 (for fast erase)
                    drawing.h = 50; //Set the height of the rectangle to 50 (for fast erase)
                }
                else //If the eraser is not enabled
                {
                    drawing.w = 1; //Set the width of the drawing to 1 pixel
                    drawing.h = 1; //Set the height of the drawing to 1 pixel
                }
                //Fill the screen (1st argument) , with the drawing's properties (rectangle). We use the MapRGB function to take the color.
                //We take the screen's format and set the red , green and blue values to the variables we declared before
                SDL_FillRect(screen,&drawing,SDL_MapRGB(screen->format,red,green,blue));

                SDL_Flip(screen); //Refresh the screen
                mouseDown = 0; //Report that the mouse button is pressed.
            }
            else if(event.type == SDL_MOUSEBUTTONUP)
            {
                //If the mouse is left
                mouseDown = 1; //Report that the mouse button IS NOT pressed.
            }
            else if(event.type == SDL_MOUSEMOTION)
            {
                //If the mouse moves
                if(mouseDown == 0)
                {
                    //Check if the mouse is pressed (check the variable we declared and changed/not changed before)
                SDL_Rect drawing; //Declare a rectangle which will be drawn to screen (that variable name tho :P )
                drawing.x = event.button.x; //The x value of the rectangle is the x location of the mouse
                drawing.y = event.button.y; //The y value of the rectangle is the y location of the mouse
                if(eraser == 1)//If the eraser is enabled
                {
                    drawing.w = 50; //Set the width of the rectangle to 50 (for fast erase)
                    drawing.h = 50; //Set the height of the rectangle to 50 (for fast erase)
                }
                else //If the eraser is not enabled
                {
                    drawing.w = 1; //Set the width of the drawing to 1 pixel
                    drawing.h = 1; //Set the height of the drawing to 1 pixel
                }
                //Fill the screen (1st argument) , with the drawing's properties (rectangle). We use the MapRGB function to take the color.
                //We take the screen's format and set the red , green and blue values to the variables we declared before
                SDL_FillRect(screen,&drawing,SDL_MapRGB(screen->format,red,green,blue));

                SDL_Flip(screen); //Refresh the screen
                }
            }

Last edited on
You will not receive an event for every pixel that the mouse is moved. What you could do is to go through and draw all pixels from the old drawing position to the new drawing position. That way you don't miss any pixels. I don't know if SDL2 has any built in functions for drawing lines, but otherwise you should have a look at Bresenham's line algorithm how to do it.

https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm

I think drawing a straight line will probably be good enough. I guess it's possible to do something more advanced and try to estimate the drawing path more exactly by taking multiple drawing positions and the timing of the events into consideration, but this is much more complicated and would require much more work to get right.
Last edited on
Alright , I'll try that and come back. Thanks

EDIT : Works :D
Last edited on
Topic archived. No new replies allowed.