testmouse: Allow drawing rectangles as well as lines
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
diff --git a/test/testmouse.c b/test/testmouse.c
index 11a1760..27b34f3 100644
--- a/test/testmouse.c
+++ b/test/testmouse.c
@@ -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);