mirror of https://github.com/encounter/SDL.git
testmouse: Allow drawing rectangles as well as lines
This commit is contained in:
parent
a932581775
commit
cf040f8882
|
@ -28,16 +28,19 @@
|
||||||
|
|
||||||
static SDL_Window *window;
|
static SDL_Window *window;
|
||||||
|
|
||||||
typedef struct _Line {
|
typedef struct _Object {
|
||||||
struct _Line *next;
|
struct _Object *next;
|
||||||
|
|
||||||
int x1, y1, x2, y2;
|
int x1, y1, x2, y2;
|
||||||
Uint8 r, g, b;
|
Uint8 r, g, b;
|
||||||
} Line;
|
|
||||||
|
|
||||||
static Line *active = NULL;
|
SDL_bool isRect;
|
||||||
static Line *lines = NULL;
|
} Object;
|
||||||
|
|
||||||
|
static Object *active = NULL;
|
||||||
|
static Object *objects = NULL;
|
||||||
static int buttons = 0;
|
static int buttons = 0;
|
||||||
|
static SDL_bool isRect = SDL_FALSE;
|
||||||
|
|
||||||
static SDL_bool wheel_x_active = SDL_FALSE;
|
static SDL_bool wheel_x_active = SDL_FALSE;
|
||||||
static SDL_bool wheel_y_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;
|
static SDL_bool done = SDL_FALSE;
|
||||||
|
|
||||||
void
|
void
|
||||||
DrawLine(SDL_Renderer * renderer, Line * line)
|
DrawObject(SDL_Renderer * renderer, Object * object)
|
||||||
{
|
{
|
||||||
SDL_SetRenderDrawColor(renderer, line->r, line->g, line->b, 255);
|
SDL_SetRenderDrawColor(renderer, object->r, object->g, object->b, 255);
|
||||||
SDL_RenderDrawLine(renderer, line->x1, line->y1, line->x2, line->y2);
|
|
||||||
|
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
|
void
|
||||||
DrawLines(SDL_Renderer * renderer)
|
DrawObjects(SDL_Renderer * renderer)
|
||||||
{
|
{
|
||||||
Line *next = lines;
|
Object *next = objects;
|
||||||
while (next != NULL) {
|
while (next != NULL) {
|
||||||
DrawLine(renderer, next);
|
DrawObject(renderer, next);
|
||||||
next = next->next;
|
next = next->next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
AppendLine(Line *line)
|
AppendObject(Object *object)
|
||||||
{
|
{
|
||||||
if (lines) {
|
if (objects) {
|
||||||
Line *next = lines;
|
Object *next = objects;
|
||||||
while (next->next != NULL) {
|
while (next->next != NULL) {
|
||||||
next = next->next;
|
next = next->next;
|
||||||
}
|
}
|
||||||
next->next = line;
|
next->next = object;
|
||||||
} else {
|
} else {
|
||||||
lines = line;
|
objects = object;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -118,6 +145,7 @@ loop(void *arg)
|
||||||
active = SDL_calloc(1, sizeof(*active));
|
active = SDL_calloc(1, sizeof(*active));
|
||||||
active->x1 = active->x2 = event.button.x;
|
active->x1 = active->x2 = event.button.x;
|
||||||
active->y1 = active->y2 = event.button.y;
|
active->y1 = active->y2 = event.button.y;
|
||||||
|
active->isRect = isRect;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (event.button.button) {
|
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;
|
case SDL_BUTTON_X2: active->g = 255; active->b = 255; buttons |= SDL_BUTTON_X2MASK; break;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SDL_MOUSEBUTTONUP:
|
case SDL_MOUSEBUTTONUP:
|
||||||
if (!active)
|
if (!active)
|
||||||
break;
|
break;
|
||||||
|
@ -141,11 +170,23 @@ loop(void *arg)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (buttons == 0) {
|
if (buttons == 0) {
|
||||||
AppendLine(active);
|
AppendObject(active);
|
||||||
active = NULL;
|
active = NULL;
|
||||||
}
|
}
|
||||||
break;
|
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:
|
case SDL_QUIT:
|
||||||
done = SDL_TRUE;
|
done = SDL_TRUE;
|
||||||
break;
|
break;
|
||||||
|
@ -167,10 +208,10 @@ loop(void *arg)
|
||||||
SDL_RenderDrawLine(renderer, 0, wheel_y, SCREEN_WIDTH, wheel_y);
|
SDL_RenderDrawLine(renderer, 0, wheel_y, SCREEN_WIDTH, wheel_y);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Lines from mouse clicks */
|
/* Objects from mouse clicks */
|
||||||
DrawLines(renderer);
|
DrawObjects(renderer);
|
||||||
if (active)
|
if (active)
|
||||||
DrawLine(renderer, active);
|
DrawObject(renderer, active);
|
||||||
|
|
||||||
SDL_RenderPresent(renderer);
|
SDL_RenderPresent(renderer);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue