testmouse: Allow drawing rectangles as well as lines

main
Cameron Cawley 2022-09-05 16:27:25 +01:00 committed by Sam Lantinga
parent a932581775
commit cf040f8882
1 changed files with 61 additions and 20 deletions

View File

@ -28,16 +28,19 @@
static SDL_Window *window;
typedef struct _Line {
struct _Line *next;
typedef struct _Object {
struct _Object *next;
int x1, y1, x2, y2;
Uint8 r, g, b;
} Line;
static Line *active = NULL;
static Line *lines = NULL;
SDL_bool isRect;
} Object;
static Object *active = NULL;
static Object *objects = NULL;
static int buttons = 0;
static SDL_bool isRect = SDL_FALSE;
static SDL_bool wheel_x_active = SDL_FALSE;
static SDL_bool wheel_y_active = SDL_FALSE;
@ -47,33 +50,57 @@ static float wheel_y = SCREEN_HEIGHT * 0.5f;
static SDL_bool done = SDL_FALSE;
void
DrawLine(SDL_Renderer * renderer, Line * line)
DrawObject(SDL_Renderer * renderer, Object * object)
{
SDL_SetRenderDrawColor(renderer, line->r, line->g, line->b, 255);
SDL_RenderDrawLine(renderer, line->x1, line->y1, line->x2, line->y2);
SDL_SetRenderDrawColor(renderer, object->r, object->g, object->b, 255);
if (object->isRect) {
SDL_Rect rect;
if (object->x1 > object->x2) {
rect.x = object->x2;
rect.w = object->x1 - object->x2;
} else {
rect.x = object->x1;
rect.w = object->x2 - object->x1;
}
if (object->y1 > object->y2) {
rect.y = object->y2;
rect.h = object->y1 - object->y2;
} else {
rect.y = object->y1;
rect.h = object->y2 - object->y1;
}
/* SDL_RenderDrawRect(renderer, &rect); */
SDL_RenderFillRect(renderer, &rect);
} else {
SDL_RenderDrawLine(renderer, object->x1, object->y1, object->x2, object->y2);
}
}
void
DrawLines(SDL_Renderer * renderer)
DrawObjects(SDL_Renderer * renderer)
{
Line *next = lines;
Object *next = objects;
while (next != NULL) {
DrawLine(renderer, next);
DrawObject(renderer, next);
next = next->next;
}
}
void
AppendLine(Line *line)
AppendObject(Object *object)
{
if (lines) {
Line *next = lines;
if (objects) {
Object *next = objects;
while (next->next != NULL) {
next = next->next;
}
next->next = line;
next->next = object;
} else {
lines = line;
objects = object;
}
}
@ -118,6 +145,7 @@ loop(void *arg)
active = SDL_calloc(1, sizeof(*active));
active->x1 = active->x2 = event.button.x;
active->y1 = active->y2 = event.button.y;
active->isRect = isRect;
}
switch (event.button.button) {
@ -128,6 +156,7 @@ loop(void *arg)
case SDL_BUTTON_X2: active->g = 255; active->b = 255; buttons |= SDL_BUTTON_X2MASK; break;
}
break;
case SDL_MOUSEBUTTONUP:
if (!active)
break;
@ -141,11 +170,23 @@ loop(void *arg)
}
if (buttons == 0) {
AppendLine(active);
AppendObject(active);
active = NULL;
}
break;
case SDL_KEYDOWN:
case SDL_KEYUP:
switch (event.key.keysym.sym) {
case SDLK_LSHIFT:
isRect = (event.key.state == SDL_PRESSED);
if (active) {
active->isRect = isRect;
}
break;
}
break;
case SDL_QUIT:
done = SDL_TRUE;
break;
@ -167,10 +208,10 @@ loop(void *arg)
SDL_RenderDrawLine(renderer, 0, wheel_y, SCREEN_WIDTH, wheel_y);
}
/* Lines from mouse clicks */
DrawLines(renderer);
/* Objects from mouse clicks */
DrawObjects(renderer);
if (active)
DrawLine(renderer, active);
DrawObject(renderer, active);
SDL_RenderPresent(renderer);