SDL (C++) Paint Program

Hello,

I'm attempting to make a Paint program, and my main problem is that I cannot seem to get the paint to "stick".

What I'm doing is this:

1) Draw an ellipse around the mouse
2) Draw a black rectangle to the screen to clear it
3) Draw an ellipse around the mouse

In a very fast loop to make it appear as though the ellipse is following the mouse. This is what makes up the "paintbrush".

When the left mouse button is held down, the screen stops re-drawing the rectangle (clearing the screen) to allow for a filled ellipse to begin being drawn in the loop (to simulate "painting"). BUT AS SOON AS THE LEFT MOUSE BUTTON IS RELEASED, THE SCREEN BEGINS REDRAWING ITSELF WHICH ERASES THE PAINT THAT WAS PUT DOWN.

Here is the basic code:

----------------------------------Main Game Loop --------------------------

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int CApp::OnExecute()
{
	if (onInit() == false) // If any of the components fail to load
	{
		return -1; // return -1 and exit the program
	}

	SDL_Event event; // declare an SDL_Event

	while (running) // While the application is running
	{
		while (SDL_PollEvent(&event)) // . . . Place an event in the Event Structure while there are still events in the queue
		{
			onEvent(&event); // Examine the event for action and act upon it
		}

		onLoop();
		onRender();
	}

	onCleanup();

	return 0;
}


------------------------------------- On Event --------------------------

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
void CApp::onMouseMove(int mX, int mY, int relX, int relY, bool Left, bool Right, bool Middle)
{
	paintbrush.setXCoordinate(mX);
	paintbrush.setYCoordinate(mY);

	if (Left)
	{
		paintbrush.setPainting(true);
	}
	else
	{
		paintbrush.setPainting(false);
	}

}

void CApp::onKeyDown(SDLKey sym, SDLMod mod, Uint16 unicode)
{
	switch (sym)
	{
	case SDLK_9:
		paintbrush.setXRadius(paintbrush.getXRadius() - 5);
		paintbrush.setYRadius(paintbrush.getYRadius() - 5);
		break;
	case SDLK_0:
		paintbrush.setXRadius(paintbrush.getXRadius() + 5);
		paintbrush.setYRadius(paintbrush.getYRadius() + 5);
		break;
	}
	
}


------------------------ On Loop ----------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "CApp.h"

void CApp::onLoop()
{
	if (paintbrush.isPainting())
	{
		filledEllipseRGBA(surfDisplay, paintbrush.getXCoordinate(), paintbrush.getYCoordinate(),
			paintbrush.getXRadius(), paintbrush.getYRadius(), paintbrush.getRed(), paintbrush.getGreen(), paintbrush.getBlue(), paintbrush.getTransparency());
	}
	else
	{
		SDL_FillRect(surfDisplay, NULL, 0x000000);
		ellipseRGBA(surfDisplay, paintbrush.getXCoordinate(), paintbrush.getYCoordinate(),
			paintbrush.getXRadius(), paintbrush.getYRadius(), paintbrush.getRed(), paintbrush.getGreen(), paintbrush.getBlue(), paintbrush.getTransparency());
	}
}


----------------------------Render --------------------------

1
2
3
4
void CApp::onRender()
{
	SDL_Flip(surfDisplay);
}
You should not be using the display to record the image. You should have a separate surface which contains the drawn image.

Drawing the screen should involve:
1) Clearing
2) Drawing the secondary surface
3) Drawing any mouseover tools or anything you want displayed that are not actually part of the image.


When the user clicks/drags, you should make the necessary changes to the secondary surface (the one that records their drawing).
On which surface should I draw the "brush" that moves with the mouse?

This is how it looks now:

Video Link: http://screencast.com/t/HtikqayZ

But ideally, I'd like to have an indicator around the mouse (The "Brush" that shows how big the paint mark is going to be and where it will be positioned).

Any ideas?
On which surface should I draw the "brush" that moves with the mouse?


On the display.

If it's not part of the image the user is creating, you don't draw it on the offscreen surface. That offscreen surface is just to capture the changes the user makes.
Topic archived. No new replies allowed.