I have a C program that uses SDL2
. I had a working program when the contents of PenTool
were not in a function and were instead where the function is called, however putting it into a function has caused an issue I cannot get my head around. Avoiding global variables, I have passed all the variables I need to manipulate by reference including the function within PenTool, bresline
. The code is supposed to draw a continuous line from the mouse by making the current mouse coords the end point and the previous mouse coords the start point of a line. However, in this functional layout, current mouse coords are recognized and drawn correctly on the screen, but previous mouse coords stay the same, resulting in every line drawn starting from the same point, instead of the actual previous mouse position. Furthermore, the print function shows all variables as unchanging while the mouse is moving, despite the running program drawing where my mouse is.
The issue is with bresline
. When I comment it out, the print function shows all the variables updating correctly with mouse movement, so could someone please explain why bresline
is preventing previous mouse coords from being assigned properly, and why it's preventing printf
from showing correct mouse coords.
I have looked at other questions on StackOverflow and elsewhere on proper passing by reference and from what I can tell, I have passed my variables correctly and I don't know how to read C++/Java so those answers don't help, so I really don't know where I am going wrong.
void bresline(SDL_Renderer *,int *, int *, int *, int *, Uint32 *);
void PenTool(int, int *, int *, SDL_Point, int *, int *, SDL_Renderer *, Uint32 *);
int main(void)
{
int mouseX = 0, mouseY = 0, prevMouseX = 0, prevMouseY = 0;
int penSize = 1;
...
case SDL_MOUSEMOTION:
if(leftMouseButtonDown)
{
PenTool(penSize, &mouseX, &mouseY, mouse_position, &prevMouseX,
&prevMouseY, renderer, pixels);
}
...
void bresline(SDL_Renderer *renderer, int *x0, int *y0, int *xn, int *yn, Uint32 *pixels)
{
int dx = abs(*xn-*x0), sx = *x0<*xn ? 1 : -1;
int dy = abs(*yn-*y0), sy = *y0<*yn ? 1 : -1;
int error = (dx>dy ? dx : -dy)/2, e2;
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
while(1)
{
pixels[*y0 * 1000 + *x0] = 0;
if(*x0==*xn && *y0==*yn) break;
e2 = error;
if(e2 >-dx)
{
error -= dy;
*x0 += sx;
}
if(e2 < dy)
{
error += dx;
*y0 += sy;
}
}
}
void PenTool(int penSize, int *mouseX, int *mouseY, SDL_Point mouse_position,
int *prevMouseX, int *prevMouseY, SDL_Renderer *renderer, Uint32 *pixels)
{
if(penSize == 1)
{
*mouseX = mouse_position.x;
*mouseY = mouse_position.y;
if((*prevMouseX == 0) && (*prevMouseY == 0))
{
*prevMouseX = *mouseX;
*prevMouseY = *mouseY;
}
bresline(renderer, mouseX, mouseY, prevMouseX, prevMouseY, pixels);
*prevMouseX = *mouseX;
*prevMouseY = *mouseY;
printf("M1_x: %d, M1_y: %d, M2_x: %d, M2_y: %d \n", *mouseX, *mouseY, *prevMouseX, *prevMouseY);
}
}
By the way, I would change bresline for:
void bresline(SDL_Renderer *,int , int , int , int , Uint32 *);
void bresline(SDL_Renderer *renderer, int x0, int y0, int xn, int yn, Uint32 *pixels)
{
int dx = abs(xn-x0), sx = x0<xn ? 1 : -1;
int dy = abs(yn-y0), sy = y0<yn ? 1 : -1;
int error = (dx>dy ? dx : -dy)/2, e2;
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
while(1)
{
pixels[y0 * 1000 + x0] = 0;
if(x0==xn && y0==yn) break;
e2 = error;
if(e2 >-dx)
{
error -= dy;
x0 += sx;
}
if(e2 < dy)
{
error += dx;
y0 += sy;
}
}
}
In your code you are changing the values inside the Bresenham's algorithm function, so that, MouseX and MouseY always will be PrevMouseX and PrevMouseY, when the function exit. That could explain your issue that always start the line from the same point.
And change how you do invoke the function for this:
bresline(renderer, *mouseX, *mouseY, *prevMouseX, *prevMouseY, pixels);