Yes, SDL provides functions to hide the default system cursor and display your own custom cursor graphics.
SDL_CreateCursor()
: Creates a cursor using monochrome (2-color) bitmap data. This is an older style but universally supported. You provide pointers to the pixel data and a mask, along with the cursor's dimensions and hot-spot coordinates (the precise point of the cursor that corresponds to the mouse position).SDL_CreateColorCursor()
: Creates a cursor from an SDL_Surface
. This is more flexible as it allows full-color cursors with alpha transparency. You provide the surface containing the cursor image and the hot-spot coordinates. The surface should typically be small (e.g., 32x32 pixels).SDL_SetCursor()
: Sets the currently active cursor for the application. You pass the SDL_Cursor*
returned by one of the creation functions. To revert to the default system cursor, you can call SDL_SetCursor(SDL_GetDefaultCursor());
.SDL_ShowCursor()
: Toggles the visibility of the cursor. Use SDL_ShowCursor(SDL_DISABLE)
to hide it and SDL_ShowCursor(SDL_ENABLE)
to show it. You usually hide the system cursor before setting your custom one if you don't want both potentially visible.SDL_FreeCursor()
: Frees the memory associated with an SDL_Cursor
that you created. You should call this when you no longer need the custom cursor.SDL_GetDefaultCursor()
: Returns a pointer to the default system cursor.SDL_CreateColorCursor
)This example assumes you have an SDL_Surface* cursorSurface
loaded with your desired cursor image (e.g., a crosshair loaded from a BMP or PNG file using SDL_LoadBMP
or the SDL_image library).
#include <SDL.h>
// Assume SDL_image is linked for PNG/other formats
// #include <SDL_image.h>
// Function to load a surface (implement elsewhere)
SDL_Surface* LoadSurface(const char* path);
int main(int argc, char** argv) {
// ... SDL Initialization ...
SDL_Window* window = SDL_CreateWindow(/*...*/);
// ... Error handling ...
SDL_Surface* cursorSurface = LoadSurface("crosshair.png"); // Load your image
if (!cursorSurface) {
// Handle error: image not loaded
return 1;
}
// Define hotspot (e.g., center of a 32x32 image)
int hot_x = cursorSurface->w / 2;
int hot_y = cursorSurface->h / 2;
SDL_Cursor* customCursor = SDL_CreateColorCursor(
cursorSurface, hot_x, hot_y
);
if (!customCursor) {
std::cerr << "Failed to create color cursor: "
<< SDL_GetError() << '\\n';
SDL_FreeSurface(cursorSurface);
// ... Cleanup ...
return 1;
}
SDL_SetCursor(customCursor);
// --- Main Loop ---
SDL_Event e;
bool quit = false;
while (!quit) {
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) quit = true;
// ... Handle other events ...
}
// ... Update and Render ...
}
// --- End Loop ---
SDL_FreeCursor(customCursor); Release the cursor resource
SDL_FreeSurface(cursorSurface); // Release the surface memory
// ... Other cleanup (Destroy Window, SDL_Quit) ...
return 0;
}
SDL_Surface
used for SDL_CreateColorCursor
has an appropriate format, often one with an alpha channel for transparency (like SDL_PIXELFORMAT_RGBA32
).SDL_FreeCursor
and the surface using SDL_FreeSurface
when they are no longer needed to avoid memory leaks.We cover surface loading, manipulation, and more advanced cursor handling, including system cursors - SDL_CreateSystemCursor()
- in greater detail later in the course.
Answers to questions are automatically generated and may not have been reviewed.
Discover how to process mouse input, including position tracking and button presses
Comprehensive course covering advanced concepts, and how to use them on large-scale projects.
View Course