pbivens67 said:
All I want to do is click and drag a sprite
A good exercise regarding realtime applications.
It's clear that you need some persistent data that lives not just for a single frame, or within a single function call.
Examples are the sprites position and the state of the mouse button.
SDL already does the latter for you, but you need to cara about the sprite. The easiest way would be to use a global variable. (Global variables are bad practice in general. It works for the simple test case, but if the program gets more complex global variables usually become a nightmare.)
Your code looks like you can click within some area, and if you do so it draws a rectangle over that area. But you can not yet move it, i guess.
I can give some example, but i don't know SDL, so you'd need to do some things differently, at the right callbacks SDL provides.
struct Sprite
{
SDL_Rect rect;
SDL_Color color;
};
Sprite globalSprite;
Sprite *dragSprite = NULL;
void main()
{
globalSprite.rect.left = 522;
globalSprite.color = white;
while (true)
{
if (SDL_MouseButtonPressed())
{
if (dragSprite == NULL && MouseCoordsInsideRectangle(globalSprite))
{
dragSprite = &globalSprite;
dragSprite->color = RED;
}
}
if (SDL_MouseButtonReleased())
{
if (dragSprite) dragSprite->color = WHITE;
dragSprite = NULL;
}
if (dragSprite && SDL_MouseMoved())
{
dragSprite->rect.left += SDL_MouseDeltaX();
dragSprite->rect.up += SDL_MouseDeltaY();
}
SDL_RenderRect (globalSprite.rect, globalSprite.color);
SDL_Wait(60 fps);
}
}
You need to figure out how to do this with SDL, but the code should help to see what global state is needed, and where in the loop this state is affected by what.
By using a pointer (not just a bool for example), the example can be easily extended to handle… multiple sprites! \:D/