Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDL2 color cursors are offset on OS X #1966

Closed
SDLBugzilla opened this issue Feb 11, 2021 · 3 comments
Closed

SDL2 color cursors are offset on OS X #1966

SDLBugzilla opened this issue Feb 11, 2021 · 3 comments
Assignees
Milestone

Comments

@SDLBugzilla
Copy link
Collaborator

This bug report was migrated from our old Bugzilla tracker.

These attachments are available in the static archive:

Reported in version: HG 2.0
Reported for operating system, platform: Mac OS X (All), x86_64

Comments on the original bug report:

On 2015-09-06 19:59:07 +0000, historic_bruno wrote:

Created attachment 2258
Test case

As reported here: https://forums.libsdl.org/viewtopic.php?t=11308

On every version of OS X I've tested, SDL2 color cursors (created by SDL_CreateColorCursor and SDL_CreateRGBSurfaceFrom) appear offset by about 3 pixels in both x and y directions. I'll attach some sample code, which works perfectly with no offsets on Linux and Windows. I'll also attach a screenshot from the game 0 A.D. with overlays to show the effect on our GUI interaction.

I tried to work around the offset by decreasing the x and y hotspot values passed to SDL_CreateColorCursor, but this caused an ugly black rectangle to follow the cursor on Yosemite (perhaps another bug?)

On 2015-09-06 20:01:58 +0000, historic_bruno wrote:

Created attachment 2259
Screenshot of game 0 A.D. showing effect of bug

Here is a screenshot showing a button in the game 0 A.D.'s UI, with a green overlay of where the cursor should activate the button, as it does on Linux and Windows. The red overlay shows where the cursor appears to activate the button on OS X.

@icculus icculus removed the bug label Nov 30, 2021
@icculus icculus added this to the 2.0.20 milestone Nov 30, 2021
@slouken slouken modified the milestones: 2.0.22, 2.0.20 Dec 18, 2021
@icculus icculus self-assigned this Dec 28, 2021
@icculus
Copy link
Collaborator

icculus commented Dec 29, 2021

Updated test program, draws a green box that is centered on the cursor's hotspot.

// Test for color cursor behavior on OS X

#include "SDL.h"
#include <stdio.h>

// 8x8 red sprite for test cursor
unsigned int pixels[] = {
    0xFFFF0000, 0xFFFF0000, 0xFFFF0000, 0xFFFF0000,
    0xFFFF0000, 0xFFFF0000, 0xFFFF0000, 0xFFFF0000,
    0xFFFF0000, 0xFFFF0000, 0xFFFF0000, 0xFFFF0000,
    0xFFFF0000, 0xFFFF0000, 0xFFFF0000, 0xFFFF0000,
    0xFFFF0000, 0xFFFF0000, 0xFFFF0000, 0xFFFF0000,
    0xFFFF0000, 0xFFFF0000, 0xFFFF0000, 0xFFFF0000,
    0xFFFF0000, 0xFFFF0000, 0xFFFF0000, 0xFFFF0000,
    0xFFFF0000, 0xFFFF0000, 0xFFFF0000, 0xFFFF0000,
    0xFFFF0000, 0xFFFF0000, 0xFFFF0000, 0xFFFF0000,
    0xFFFF0000, 0xFFFF0000, 0xFFFF0000, 0xFFFF0000,
    0xFFFF0000, 0xFFFF0000, 0xFFFF0000, 0xFFFF0000,
    0xFFFF0000, 0xFFFF0000, 0xFFFF0000, 0xFFFF0000,
    0xFFFF0000, 0xFFFF0000, 0xFFFF0000, 0xFFFF0000,
    0xFFFF0000, 0xFFFF0000, 0xFFFF0000, 0xFFFF0000,
    0xFFFF0000, 0xFFFF0000, 0xFFFF0000, 0xFFFF0000,
    0xFFFF0000, 0xFFFF0000, 0xFFFF0000, 0xFFFF0000
};

int main(int argc, char* argv[]) {

    SDL_Window *window;

    SDL_Init(SDL_INIT_VIDEO);

    window = SDL_CreateWindow(
                              "SDL2 Color Cursor Test",
                              SDL_WINDOWPOS_UNDEFINED,
                              SDL_WINDOWPOS_UNDEFINED,
                              150,
                              100,
                              SDL_WINDOW_OPENGL
                              );

    if (window == NULL) {
        printf("Could not create window: %s\n", SDL_GetError());
        return 1;
    }

    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    if (renderer == NULL) {
        printf("Could not create renderer: %s\n", SDL_GetError());
        return 1;
    }

    SDL_Surface* surface = SDL_CreateRGBSurfaceFrom(pixels, 8, 8, 32, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
    if (surface == NULL) {
        printf("Could not create surface: %s\n", SDL_GetError());
        return 1;
    }
    // Hotspot of (3,3) here "fixes" the bug on OS X
    SDL_Cursor* cursor = SDL_CreateColorCursor(surface, 0, 0);
    if (cursor == NULL) {
        printf("Could not create cursor: %s\n", SDL_GetError());
        return 1;
    }
    SDL_SetCursor(cursor);

    while (1) {
        SDL_Event event;
        SDL_Rect r;

        if (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT)
                break;
            else if (event.type == SDL_WINDOWEVENT) {
                switch (event.window.event) {
                    case SDL_WINDOWEVENT_ENTER:
                        printf("Mouse entered window %d\n",
                               event.window.windowID);
                        break;
                    case SDL_WINDOWEVENT_LEAVE:
                        printf("Mouse left window %d\n", event.window.windowID);
                        break;
                    default:
                        break;
                }
            }
        }

        SDL_GetMouseState(&r.x, &r.y);
        r.w = r.h = 6;
        r.x -= r.w / 2;
        r.y -= r.h / 2;

        SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
        SDL_RenderClear(renderer);
        SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
        SDL_RenderFillRect(renderer, &r);
        SDL_RenderPresent(renderer);
    }

    SDL_FreeCursor(cursor);
    SDL_FreeSurface(surface);

    SDL_DestroyWindow(window);
    SDL_DestroyRenderer(renderer);

    SDL_Quit();
    return 0;
}

@icculus
Copy link
Collaborator

icculus commented Dec 29, 2021

This appears to work now...the rectangle we're drawing is centered on the cursor's hotspot (picture taken on phone both to zoom in and capture the cursor).

image

@icculus
Copy link
Collaborator

icculus commented Dec 29, 2021

Tested with non-zero hotspot coords and it works, too. This clearly got fixed at some point.

@icculus icculus closed this as completed Dec 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants