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

Commit

Permalink
Don't free the surface since the application might be still using it.
Browse files Browse the repository at this point in the history
Also experimented with texture rectangle updates, but I think at this point the full rect update gives the most consistent results.
  • Loading branch information
slouken committed Feb 4, 2011
1 parent 86ef610 commit 950d8c4
Showing 1 changed file with 38 additions and 8 deletions.
46 changes: 38 additions & 8 deletions src/video/SDL_video.c
Expand Up @@ -104,6 +104,7 @@ typedef struct {
SDL_Texture *texture;
void *pixels;
int pitch;
int bytes_per_pixel;
} SDL_WindowTextureData;

static int
Expand Down Expand Up @@ -176,7 +177,8 @@ SDL_CreateWindowTexture(_THIS, SDL_Window * window, Uint32 * format, void ** pix
}

/* Create framebuffer data */
data->pitch = (((window->w * SDL_BYTESPERPIXEL(*format)) + 3) & ~3);
data->bytes_per_pixel = SDL_BYTESPERPIXEL(*format);
data->pitch = (((window->w * data->bytes_per_pixel) + 3) & ~3);
data->pixels = SDL_malloc(window->h * data->pitch);
if (!data->pixels) {
SDL_OutOfMemory();
Expand All @@ -192,19 +194,46 @@ static int
SDL_UpdateWindowTexture(_THIS, SDL_Window * window, int numrects, SDL_Rect * rects)
{
SDL_WindowTextureData *data;
#ifdef UPDATE_TEXTURE_SUBRECTS
void *src, *dst;
int src_pitch;
int dst_pitch;
int i, row, length;
#endif

data = SDL_GetWindowData(window, SDL_WINDOWTEXTUREDATA);
if (!data || !data->texture) {
SDL_SetError("No window texture data");
return -1;
}

#ifdef UPDATE_TEXTURE_SUBRECTS
src_pitch = data->pitch;
for (i = 0; i < numrects; ++i) {
src = (void *)((Uint8 *)data->pixels +
rects[i].y * src_pitch +
rects[i].x * data->bytes_per_pixel);
if (SDL_LockTexture(data->texture, &rects[i], &dst, &dst_pitch) < 0) {
return -1;
}
length = rects[i].w * data->bytes_per_pixel;
for (row = rects[i].h; row--; ) {
SDL_memcpy(dst, src, length);
src = (Uint8*)src + src_pitch;
dst = (Uint8*)dst + dst_pitch;
}
SDL_UnlockTexture(data->texture);
}
#else
if (SDL_UpdateTexture(data->texture, NULL, data->pixels, data->pitch) < 0) {
return -1;
}
#endif

if (SDL_RenderCopy(data->renderer, data->texture, NULL, NULL) < 0) {
return -1;
}

SDL_RenderPresent(data->renderer);
return 0;
}
Expand Down Expand Up @@ -1389,9 +1418,14 @@ SDL_GetWindowSurface(SDL_Window * window)
{
CHECK_WINDOW_MAGIC(window, NULL);

if (!window->surface) {
if (!window->surface_valid) {
if (window->surface) {
window->surface->refcount = 0;
SDL_FreeSurface(window->surface);
}
window->surface = SDL_CreateWindowFramebuffer(window);
if (window->surface) {
window->surface_valid = SDL_TRUE;
window->surface->refcount = 0x7FFFFFF;
}
}
Expand All @@ -1418,7 +1452,7 @@ SDL_UpdateWindowSurfaceRects(SDL_Window * window,
{
CHECK_WINDOW_MAGIC(window, -1);

if (!window->surface) {
if (!window->surface_valid) {
SDL_SetError("Window surface is invalid, please call SDL_GetWindowSurface() to get a new surface");
return -1;
}
Expand Down Expand Up @@ -1474,11 +1508,7 @@ SDL_OnWindowHidden(SDL_Window * window)
void
SDL_OnWindowResized(SDL_Window * window)
{
if (window->surface) {
window->surface->refcount = 0;
SDL_FreeSurface(window->surface);
window->surface = NULL;
}
window->surface_valid = SDL_FALSE;
}

void
Expand Down

0 comments on commit 950d8c4

Please sign in to comment.