Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Extended SDL_SetWindowData() to allow arbitrary named values.
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Feb 3, 2011
1 parent 85ee651 commit 3ccc895
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 16 deletions.
25 changes: 20 additions & 5 deletions include/SDL_video.h
Expand Up @@ -483,24 +483,39 @@ extern DECLSPEC void SDLCALL SDL_SetWindowIcon(SDL_Window * window,
SDL_Surface * icon);

/**
* \brief Associate an arbitrary pointer with a window.
* \brief Associate an arbitrary named pointer with a window.
*
* \param window The window to associate with the pointer.
* \param name The name of the pointer.
* \param userdata The associated pointer.
*
* \return The previous value associated with 'name'
*
* \note The name is case-sensitive.
*
* \sa SDL_GetWindowData()
*/
extern DECLSPEC void SDLCALL SDL_SetWindowData(SDL_Window * window,
void *userdata);
extern DECLSPEC void* SDLCALL SDL_SetWindowData(SDL_Window * window,
const char *name,
void *userdata);

/**
* \brief Retrieve the data pointer associated with a window.
*
* \param window The window to query.
* \param name The name of the pointer.
*
* \return The value associated with 'name'
*
* \sa SDL_SetWindowData()
*/
extern DECLSPEC void *SDLCALL SDL_GetWindowData(SDL_Window * window);
extern DECLSPEC void *SDLCALL SDL_GetWindowData(SDL_Window * window,
const char *name);

/**
* \brief Set the position of a window.
*
* \param window The window to reposition.
* \param window The window to reposition.
* \param x The x coordinate of the window, ::SDL_WINDOWPOS_CENTERED, or
::SDL_WINDOWPOS_UNDEFINED.
* \param y The y coordinate of the window, ::SDL_WINDOWPOS_CENTERED, or
Expand Down
10 changes: 9 additions & 1 deletion src/video/SDL_sysvideo.h
Expand Up @@ -59,6 +59,13 @@ struct SDL_ShapeDriver
int (*ResizeWindowShape)(SDL_Window *window);
};

typedef struct SDL_WindowUserData
{
char *name;
void *data;
struct SDL_WindowUserData *next;
} SDL_WindowUserData;

/* Define the SDL window structure, corresponding to toplevel windows */
struct SDL_Window
{
Expand All @@ -75,7 +82,8 @@ struct SDL_Window

SDL_WindowShaper *shaper;

void *userdata;
SDL_WindowUserData *data;

void *driverdata;

SDL_Window *prev;
Expand Down
68 changes: 58 additions & 10 deletions src/video/SDL_video.c
Expand Up @@ -982,20 +982,60 @@ SDL_SetWindowIcon(SDL_Window * window, SDL_Surface * icon)
}
}

void
SDL_SetWindowData(SDL_Window * window, void *userdata)
void*
SDL_SetWindowData(SDL_Window * window, const char *name, void *userdata)
{
CHECK_WINDOW_MAGIC(window, );
SDL_WindowUserData *prev, *data;

CHECK_WINDOW_MAGIC(window, NULL);

/* See if the named data already exists */
prev = NULL;
for (data = window->data; data; prev = data, data = data->next) {
if (SDL_strcmp(data->name, name) == 0) {
void *last_value = data->data;

if (userdata) {
/* Set the new value */
data->data = userdata;
} else {
/* Delete this value */
if (prev) {
prev->next = data->next;
} else {
window->data = data->next;
}
SDL_free(data->name);
SDL_free(data);
}
return last_value;
}
}

window->userdata = userdata;
/* Add new data to the window */
if (userdata) {
data = (SDL_WindowUserData *)SDL_malloc(sizeof(*data));
data->name = SDL_strdup(name);
data->data = userdata;
data->next = window->data;
window->data = data;
}
return NULL;
}

void *
SDL_GetWindowData(SDL_Window * window)
SDL_GetWindowData(SDL_Window * window, const char *name)
{
SDL_WindowUserData *data;

CHECK_WINDOW_MAGIC(window, NULL);

return window->userdata;
for (data = window->data; data; data = data->next) {
if (SDL_strcmp(data->name, name) == 0) {
return data->data;
}
}
return NULL;
}

void
Expand Down Expand Up @@ -1293,10 +1333,6 @@ SDL_DestroyWindow(SDL_Window * window)

CHECK_WINDOW_MAGIC(window, );

if (window->title) {
SDL_free(window->title);
}

/* Restore video mode, etc. */
SDL_UpdateFullscreenMode(window, SDL_FALSE);

Expand All @@ -1310,6 +1346,18 @@ SDL_DestroyWindow(SDL_Window * window)
/* Now invalidate magic */
window->magic = NULL;

/* Free memory associated with the window */
if (window->title) {
SDL_free(window->title);
}
while (window->data) {
SDL_WindowUserData *data = window->data;

window->data = data->next;
SDL_free(data->name);
SDL_free(data);
}

/* Unlink the window from the list */
display = window->display;
if (window->next) {
Expand Down

0 comments on commit 3ccc895

Please sign in to comment.