Cursor mask was uninitialized data, causing problems.
authorRyan C. Gordon <icculus@icculus.org>
Sun, 21 Jul 2013 00:07:43 -0400
changeset 7489 30ec6dae748b
parent 7488 a74f1f664047
child 7490 fc864efbc1a6
Cursor mask was uninitialized data, causing problems. Fixes Bugzilla #1461.
src/video/windows/SDL_windowsmouse.c
--- a/src/video/windows/SDL_windowsmouse.c	Sat Jul 20 21:22:37 2013 -0400
+++ b/src/video/windows/SDL_windowsmouse.c	Sun Jul 21 00:07:43 2013 -0400
@@ -49,11 +49,16 @@
 static SDL_Cursor *
 WIN_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
 {
+    /* msdn says cursor mask has to be padded out to word alignment. Not sure
+        if that means machine word or WORD, but this handles either case. */
+    const size_t pad = (sizeof (size_t) * 8);  /* 32 or 64, or whatever. */
     SDL_Cursor *cursor;
     HICON hicon;
     HDC hdc;
     BITMAPV4HEADER bmh;
     LPVOID pixels;
+    LPVOID maskbits;
+    size_t maskbitslen;
     ICONINFO ii;
 
     SDL_zero(bmh);
@@ -68,14 +73,25 @@
     bmh.bV4GreenMask = 0x0000FF00;
     bmh.bV4BlueMask  = 0x000000FF;
 
+    maskbitslen = ((surface->w + (pad - (surface->w % pad))) / 8) * surface->h;
+    maskbits = SDL_stack_alloc(Uint8,maskbitslen);
+    if (maskbits == NULL) {
+        SDL_OutOfMemory();
+        return NULL;
+    }
+
+    /* AND the cursor against full bits: no change. We already have alpha. */
+    SDL_memset(maskbits, 0xFF, maskbitslen);
+
     hdc = GetDC(NULL);
     SDL_zero(ii);
     ii.fIcon = FALSE;
     ii.xHotspot = (DWORD)hot_x;
     ii.yHotspot = (DWORD)hot_y;
     ii.hbmColor = CreateDIBSection(hdc, (BITMAPINFO*)&bmh, DIB_RGB_COLORS, &pixels, NULL, 0);
-    ii.hbmMask = CreateBitmap(surface->w, surface->h, 1, 1, NULL);
+    ii.hbmMask = CreateBitmap(surface->w, surface->h, 1, 1, maskbits);
     ReleaseDC(NULL, hdc);
+    SDL_stack_free(maskbits);
 
     SDL_assert(surface->format->format == SDL_PIXELFORMAT_ARGB8888);
     SDL_assert(surface->pitch == surface->w * 4);