2014-05-27 05:27:42 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include "SDL.h"
|
|
|
|
|
|
|
|
/* !!! FIXME: rewrite this to be wired in to test framework. */
|
|
|
|
|
2014-05-28 05:22:47 +00:00
|
|
|
const SDL_Rect drag_areas[] = {
|
|
|
|
{ 20, 20, 100, 100 },
|
|
|
|
{ 200, 70, 100, 100 },
|
|
|
|
{ 400, 90, 100, 100 }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const SDL_Rect *areas = drag_areas;
|
|
|
|
static int numareas = SDL_arraysize(drag_areas);
|
|
|
|
|
|
|
|
static SDL_HitTestResult
|
|
|
|
hitTest(SDL_Window *window, const SDL_Point *pt, void *data)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < numareas; i++) {
|
|
|
|
if (SDL_PointInRect(pt, &areas[i])) {
|
2014-05-30 05:48:08 +00:00
|
|
|
SDL_Log("HIT-TEST: DRAGGABLE\n");
|
2014-05-28 05:22:47 +00:00
|
|
|
return SDL_HITTEST_DRAGGABLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-30 05:48:08 +00:00
|
|
|
SDL_Log("HIT-TEST: NORMAL\n");
|
2014-05-28 05:22:47 +00:00
|
|
|
return SDL_HITTEST_NORMAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-27 05:27:42 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int done = 0;
|
|
|
|
SDL_Window *window;
|
|
|
|
SDL_Renderer *renderer;
|
|
|
|
|
|
|
|
/* !!! FIXME: check for errors. */
|
|
|
|
SDL_Init(SDL_INIT_VIDEO);
|
|
|
|
window = SDL_CreateWindow("Drag the red boxes", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_BORDERLESS);
|
|
|
|
renderer = SDL_CreateRenderer(window, -1, 0);
|
|
|
|
|
2014-05-28 05:22:47 +00:00
|
|
|
if (SDL_SetWindowHitTest(window, hitTest, NULL) == -1) {
|
2014-05-30 05:48:08 +00:00
|
|
|
SDL_Log("Enabling hit-testing failed!\n");
|
2014-05-27 05:27:42 +00:00
|
|
|
SDL_Quit();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!done)
|
|
|
|
{
|
2014-05-30 05:48:26 +00:00
|
|
|
SDL_Event e;
|
|
|
|
int nothing_to_do = 1;
|
|
|
|
|
2014-05-27 05:27:42 +00:00
|
|
|
SDL_SetRenderDrawColor(renderer, 0, 0, 127, 255);
|
|
|
|
SDL_RenderClear(renderer);
|
|
|
|
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
|
|
|
|
SDL_RenderFillRects(renderer, areas, SDL_arraysize(drag_areas));
|
|
|
|
SDL_RenderPresent(renderer);
|
|
|
|
|
|
|
|
while (SDL_PollEvent(&e)) {
|
|
|
|
nothing_to_do = 0;
|
|
|
|
|
|
|
|
switch (e.type)
|
|
|
|
{
|
|
|
|
case SDL_MOUSEBUTTONDOWN:
|
2014-05-30 05:48:08 +00:00
|
|
|
SDL_Log("button down!\n");
|
2014-05-27 05:27:42 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_MOUSEBUTTONUP:
|
2014-05-30 05:48:08 +00:00
|
|
|
SDL_Log("button up!\n");
|
2014-05-27 05:27:42 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_WINDOWEVENT:
|
|
|
|
if (e.window.event == SDL_WINDOWEVENT_MOVED) {
|
2014-05-30 05:48:08 +00:00
|
|
|
SDL_Log("Window event moved to (%d, %d)!\n", (int) e.window.data1, (int) e.window.data2);
|
2014-05-27 05:27:42 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_KEYDOWN:
|
|
|
|
if (e.key.keysym.sym == SDLK_ESCAPE) {
|
|
|
|
done = 1;
|
|
|
|
} else if (e.key.keysym.sym == SDLK_x) {
|
|
|
|
if (!areas) {
|
|
|
|
areas = drag_areas;
|
|
|
|
numareas = SDL_arraysize(drag_areas);
|
|
|
|
} else {
|
|
|
|
areas = NULL;
|
|
|
|
numareas = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_QUIT:
|
|
|
|
done = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nothing_to_do) {
|
|
|
|
SDL_Delay(50);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Quit();
|
|
|
|
return 0;
|
|
|
|
}
|