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);
}
|