Added Android custom cursor implementation

This is commented out in SDLActivity.java, with the note #CURSORIMPLEENTATION because it requires API 24, which is higher than the minimum required SDK
This commit is contained in:
Sam Lantinga 2018-03-15 18:22:48 -07:00
parent 548940ff18
commit b5b3a139dc
5 changed files with 291 additions and 8 deletions

View file

@ -73,6 +73,24 @@ init_color_cursor(const char *file)
SDL_Cursor *cursor = NULL;
SDL_Surface *surface = SDL_LoadBMP(file);
if (surface) {
if (surface->format->palette) {
SDL_SetColorKey(surface, 1, *(Uint8 *) surface->pixels);
} else {
switch (surface->format->BitsPerPixel) {
case 15:
SDL_SetColorKey(surface, 1, (*(Uint16 *)surface->pixels) & 0x00007FFF);
break;
case 16:
SDL_SetColorKey(surface, 1, *(Uint16 *)surface->pixels);
break;
case 24:
SDL_SetColorKey(surface, 1, (*(Uint32 *)surface->pixels) & 0x00FFFFFF);
break;
case 32:
SDL_SetColorKey(surface, 1, *(Uint32 *)surface->pixels);
break;
}
}
cursor = SDL_CreateColorCursor(surface, 0, 0);
SDL_FreeSurface(surface);
}
@ -116,7 +134,9 @@ init_system_cursor(const char *image[])
static SDLTest_CommonState *state;
int done;
SDL_Cursor *cursor = NULL;
static SDL_Cursor *cursors[1+SDL_NUM_SYSTEM_CURSORS];
static int current_cursor;
static int show_cursor;
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void
@ -134,6 +154,18 @@ loop()
/* Check for events */
while (SDL_PollEvent(&event)) {
SDLTest_CommonEvent(state, &event, &done);
if (event.type == SDL_MOUSEBUTTONDOWN) {
if (event.button.button == SDL_BUTTON_LEFT) {
++current_cursor;
if (current_cursor == SDL_arraysize(cursors)) {
current_cursor = 0;
}
SDL_SetCursor(cursors[current_cursor]);
} else {
show_cursor = !show_cursor;
SDL_ShowCursor(show_cursor);
}
}
}
for (i = 0; i < state->num_windows; ++i) {
@ -188,15 +220,22 @@ main(int argc, char *argv[])
}
if (color_cursor) {
cursor = init_color_cursor(color_cursor);
cursors[0] = init_color_cursor(color_cursor);
} else {
cursor = init_system_cursor(arrow);
cursors[0] = init_system_cursor(arrow);
}
if (!cursor) {
if (!cursors[0]) {
SDL_Log("Error, couldn't create cursor\n");
quit(2);
}
SDL_SetCursor(cursor);
for (i = 0; i < SDL_NUM_SYSTEM_CURSORS; ++i) {
cursors[1+i] = SDL_CreateSystemCursor((SDL_SystemCursor)i);
if (!cursors[1+i]) {
SDL_Log("Error, couldn't create system cursor %d\n", i);
quit(2);
}
}
SDL_SetCursor(cursors[0]);
/* Main render loop */
done = 0;
@ -208,9 +247,13 @@ main(int argc, char *argv[])
}
#endif
SDL_FreeCursor(cursor);
for (i = 0; i < SDL_arraysize(cursors); ++i) {
SDL_FreeCursor(cursors[i]);
}
quit(0);
/* keep the compiler happy ... */
return(0);
}
/* vi: set ts=4 sw=4 expandtab: */