void SDL_DelPaletteWatch(SDL_Palette * palette, SDL_PaletteChangedFunc callback, void *userdata) { SDL_PaletteWatch *prev, *watch; if (!palette) { return; } for (prev = NULL, watch = palette->watch; watch; prev = watch, watch = watch->next) { if (watch->callback == callback && watch->userdata == userdata) { if (prev) { prev->next = watch->next; } else { palette->watch = watch->next; } SDL_free(watch); SDL_FreePalette(palette); return; } } }
static void osinterface_close_window() { if (_window != NULL) SDL_DestroyWindow(_window); if (_surface != NULL) SDL_FreeSurface(_surface); if (_palette != NULL) SDL_FreePalette(_palette); }
void EmuinoSDL::quit() { for (int i = 0; i < NUM_PALETTES; i++) { if (palettes[i] != NULL) SDL_FreePalette(palettes[i]); } SDL_DestroyWindow(window); SDL_Quit(); }
static void osinterface_resize(int width, int height) { rct_drawpixelinfo *screenDPI; int newScreenBufferSize; void *newScreenBuffer; if (_surface != NULL) SDL_FreeSurface(_surface); if (_palette != NULL) SDL_FreePalette(_palette); _surface = SDL_CreateRGBSurface(0, width, height, 8, 0, 0, 0, 0); _palette = SDL_AllocPalette(256); SDL_SetSurfacePalette(_surface, _palette); newScreenBufferSize = _surface->pitch * _surface->h; newScreenBuffer = malloc(newScreenBufferSize); if (_screenBuffer == NULL) { memset(newScreenBuffer, 0, newScreenBufferSize); } else { memcpy(newScreenBuffer, _screenBuffer, min(_screenBufferSize, newScreenBufferSize)); if (newScreenBufferSize - _screenBufferSize > 0) memset((uint8*)newScreenBuffer + _screenBufferSize, 0, newScreenBufferSize - _screenBufferSize); free(_screenBuffer); } _screenBuffer = newScreenBuffer; _screenBufferSize = newScreenBufferSize; RCT2_GLOBAL(RCT2_ADDRESS_SCREEN_WIDTH, sint16) = width; RCT2_GLOBAL(RCT2_ADDRESS_SCREEN_HEIGHT, sint16) = height; screenDPI = RCT2_ADDRESS(RCT2_ADDRESS_SCREEN_DPI, rct_drawpixelinfo); screenDPI->bits = _screenBuffer; screenDPI->x = 0; screenDPI->y = 0; screenDPI->width = width; screenDPI->height = height; screenDPI->pitch = _surface->pitch - _surface->w; RCT2_GLOBAL(0x009ABDF0, uint8) = 6; RCT2_GLOBAL(0x009ABDF1, uint8) = 3; RCT2_GLOBAL(0x009ABDF2, uint8) = 1; RCT2_GLOBAL(RCT2_ADDRESS_DIRTY_BLOCK_WIDTH, sint16) = 64; RCT2_GLOBAL(RCT2_ADDRESS_DIRTY_BLOCK_HEIGHT, sint16) = 8; RCT2_GLOBAL(RCT2_ADDRESS_DIRTY_BLOCK_COLUMNS, sint32) = (width >> 6) + 1; RCT2_GLOBAL(RCT2_ADDRESS_DIRTY_BLOCK_ROWS, sint32) = (height >> 3) + 1; RCT2_CALLPROC_EBPSAFE(0x0066B905); // resize_gui() gfx_invalidate_screen(); }
void SDLImageLoader::init(Point SpriteSize, Point Margins, Point FullSize, SDL_Color *pal) { //Init image image->surf = SDL_CreateRGBSurface(SDL_SWSURFACE, SpriteSize.x, SpriteSize.y, 8, 0, 0, 0, 0); image->margins = Margins; image->fullSize = FullSize; //Prepare surface SDL_Palette * p = SDL_AllocPalette(256); SDL_SetPaletteColors(p, pal, 0, 256); SDL_SetSurfacePalette(image->surf, p); SDL_FreePalette(p); SDL_LockSurface(image->surf); lineStart = position = (ui8*)image->surf->pixels; }
static void sdl2_init_font(sdl2_video_t *vid, const char *font_path, unsigned font_size) { int i, r, g, b; SDL_Color colors[256]; SDL_Surface *tmp = NULL; SDL_Palette *pal = NULL; const struct font_atlas *atlas = NULL; settings_t *settings = config_get_ptr(); if (!settings->video.font_enable) return; if (!font_renderer_create_default(&vid->font_driver, &vid->font_data, *font_path ? font_path : NULL, font_size)) { RARCH_WARN("[SDL]: Could not initialize fonts.\n"); return; } r = settings->video.msg_color_r * 255; g = settings->video.msg_color_g * 255; b = settings->video.msg_color_b * 255; r = (r < 0) ? 0 : (r > 255 ? 255 : r); g = (g < 0) ? 0 : (g > 255 ? 255 : g); b = (b < 0) ? 0 : (b > 255 ? 255 : b); vid->font_r = r; vid->font_g = g; vid->font_b = b; atlas = vid->font_driver->get_atlas(vid->font_data); tmp = SDL_CreateRGBSurfaceFrom(atlas->buffer, atlas->width, atlas->height, 8, atlas->width, 0, 0, 0, 0); for (i = 0; i < 256; ++i) { colors[i].r = colors[i].g = colors[i].b = i; colors[i].a = 255; } pal = SDL_AllocPalette(256); SDL_SetPaletteColors(pal, colors, 0, 256); SDL_SetSurfacePalette(tmp, pal); SDL_SetColorKey(tmp, SDL_TRUE, 0); vid->font.tex = SDL_CreateTextureFromSurface(vid->renderer, tmp); if (vid->font.tex) { vid->font.w = atlas->width; vid->font.h = atlas->height; vid->font.active = true; SDL_SetTextureBlendMode(vid->font.tex, SDL_BLENDMODE_ADD); } else RARCH_WARN("[SDL]: Failed to initialize font texture: %s\n", SDL_GetError()); SDL_FreePalette(pal); SDL_FreeSurface(tmp); }
/* * Create an empty RGB surface of the appropriate depth */ SDL_Surface * SDL_CreateRGBSurface(Uint32 flags, int width, int height, int depth, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask) { SDL_Surface *surface; Uint32 format; /* The flags are no longer used, make the compiler happy */ (void)flags; /* Get the pixel format */ format = SDL_MasksToPixelFormatEnum(depth, Rmask, Gmask, Bmask, Amask); if (format == SDL_PIXELFORMAT_UNKNOWN) { SDL_SetError("Unknown pixel format"); return NULL; } /* Allocate the surface */ surface = (SDL_Surface *) SDL_calloc(1, sizeof(*surface)); if (surface == NULL) { SDL_OutOfMemory(); return NULL; } surface->format = SDL_AllocFormat(format); if (!surface->format) { SDL_FreeSurface(surface); return NULL; } surface->w = width; surface->h = height; surface->pitch = SDL_CalculatePitch(surface); SDL_SetClipRect(surface, NULL); if (SDL_ISPIXELFORMAT_INDEXED(surface->format->format)) { SDL_Palette *palette = SDL_AllocPalette((1 << surface->format->BitsPerPixel)); if (!palette) { SDL_FreeSurface(surface); return NULL; } if (palette->ncolors == 2) { /* Create a black and white bitmap palette */ palette->colors[0].r = 0xFF; palette->colors[0].g = 0xFF; palette->colors[0].b = 0xFF; palette->colors[1].r = 0x00; palette->colors[1].g = 0x00; palette->colors[1].b = 0x00; } SDL_SetSurfacePalette(surface, palette); SDL_FreePalette(palette); } /* Get the pixels */ if (surface->w && surface->h) { surface->pixels = SDL_malloc(surface->h * surface->pitch); if (!surface->pixels) { SDL_FreeSurface(surface); SDL_OutOfMemory(); return NULL; } /* This is important for bitmaps */ SDL_memset(surface->pixels, 0, surface->h * surface->pitch); } /* Allocate an empty mapping */ surface->map = SDL_AllocBlitMap(); if (!surface->map) { SDL_FreeSurface(surface); return NULL; } /* By default surface with an alpha mask are set up for blending */ if (Amask) { SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND); } /* The surface is ready to go */ surface->refcount = 1; return surface; }
/* * Sets the window icon */ static void SetSDLIcon() { SDL_Surface *icon; SDL_Color transColor, solidColor; Uint8 *ptr; int i; int mask; icon = SDL_CreateRGBSurface(SDL_SWSURFACE, q2icon_width, q2icon_height, 8, 0, 0, 0, 0); if (icon == NULL) { return; } SDL_SetColorKey(icon, SDL_SRCCOLORKEY, 0); transColor.r = 255; transColor.g = 255; transColor.b = 255; solidColor.r = 0; solidColor.g = 16; solidColor.b = 0; #if SDL_VERSION_ATLEAST(2, 0, 0) // only SDL2 has alphas there and they must be set apparently transColor.a = 0; solidColor.a = 255; SDL_Palette* palette = SDL_AllocPalette(256); SDL_SetPaletteColors(palette, &transColor, 0, 1); SDL_SetPaletteColors(palette, &solidColor, 1, 1); SDL_SetSurfacePalette(icon, palette); #else SDL_SetColors(icon, &transColor, 0, 1); SDL_SetColors(icon, &solidColor, 1, 1); #endif ptr = (Uint8 *)icon->pixels; for (i = 0; i < sizeof(q2icon_bits); i++) { for (mask = 1; mask != 0x100; mask <<= 1) { *ptr = (q2icon_bits[i] & mask) ? 1 : 0; ptr++; } } #if SDL_VERSION_ATLEAST(2, 0, 0) SDL_SetWindowIcon(window, icon); SDL_FreePalette(palette); #else SDL_WM_SetIcon(icon, NULL); #endif SDL_FreeSurface(icon); }
bool sdl_set_video_mode(struct graphics_data *graphics, int width, int height, int depth, bool fullscreen, bool resize) { struct sdl_render_data *render_data = graphics->render_data; #if SDL_VERSION_ATLEAST(2,0,0) bool matched = false; Uint32 fmt; render_data->window = SDL_CreateWindow("MegaZeux", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, sdl_flags(depth, fullscreen, resize)); if(!render_data->window) { warn("Failed to create window: %s\n", SDL_GetError()); goto err_out; } render_data->renderer = SDL_CreateRenderer(render_data->window, -1, SDL_RENDERER_SOFTWARE); if(!render_data->renderer) { warn("Failed to create renderer: %s\n", SDL_GetError()); goto err_destroy_window; } render_data->screen = SDL_GetWindowSurface(render_data->window); if(!render_data->screen) { warn("Failed to get window surface: %s\n", SDL_GetError()); goto err_destroy_renderer; } /* SDL 2.0 allows the window system to offer up buffers of a specific * format, and expects the application to perform a blit if the format * doesn't match what the app wanted. To accomodate this, allocate a * shadow screen that we render to in the case the formats do not match, * and blit it at present time. */ fmt = SDL_GetWindowPixelFormat(render_data->window); switch(depth) { case 8: matched = fmt == SDL_PIXELFORMAT_INDEX8; fmt = SDL_PIXELFORMAT_INDEX8; break; case 16: switch(fmt) { case SDL_PIXELFORMAT_RGB565: case SDL_PIXELFORMAT_BGR565: matched = true; break; default: fmt = SDL_PIXELFORMAT_RGB565; break; } break; case 32: switch(fmt) { case SDL_PIXELFORMAT_RGB888: case SDL_PIXELFORMAT_RGBX8888: case SDL_PIXELFORMAT_RGBA8888: case SDL_PIXELFORMAT_BGRX8888: case SDL_PIXELFORMAT_BGRA8888: case SDL_PIXELFORMAT_ARGB8888: case SDL_PIXELFORMAT_ABGR8888: matched = true; break; default: fmt = SDL_PIXELFORMAT_RGBA8888; break; } break; } if(!matched) { Uint32 Rmask, Gmask, Bmask, Amask; int bpp; SDL_PixelFormatEnumToMasks(fmt, &bpp, &Rmask, &Gmask, &Bmask, &Amask); render_data->shadow = SDL_CreateRGBSurface(0, width, height, bpp, Rmask, Gmask, Bmask, Amask); debug("Blitting enabled. Rendering performance will be reduced.\n"); } else { render_data->shadow = NULL; } if(fmt == SDL_PIXELFORMAT_INDEX8) { SDL_PixelFormat *format = render_data->shadow ? render_data->shadow->format : render_data->screen->format; render_data->palette = SDL_AllocPalette(SMZX_PAL_SIZE); if(!render_data->palette) { warn("Failed to allocate palette: %s\n", SDL_GetError()); goto err_destroy_renderer; } if(SDL_SetPixelFormatPalette(format, render_data->palette)) { warn("Failed to set pixel format palette: %s\n", SDL_GetError()); goto err_free_palette; } } else { render_data->palette = NULL; } sdl_window_id = SDL_GetWindowID(render_data->window); #else // !SDL_VERSION_ATLEAST(2,0,0) render_data->screen = SDL_SetVideoMode(width, height, depth, sdl_flags(depth, fullscreen, resize)); if(!render_data->screen) return false; render_data->shadow = NULL; #endif // !SDL_VERSION_ATLEAST(2,0,0) return true; #if SDL_VERSION_ATLEAST(2,0,0) err_free_palette: SDL_FreePalette(render_data->palette); render_data->palette = NULL; err_destroy_renderer: SDL_DestroyRenderer(render_data->renderer); render_data->renderer = NULL; err_destroy_window: SDL_DestroyWindow(render_data->window); render_data->window = NULL; err_out: return false; #endif // SDL_VERSION_ATLEAST(2,0,0) }
/* * Create an empty RGB surface of the appropriate depth using the given * enum SDL_PIXELFORMAT_* format */ SDL_Surface * SDL_CreateRGBSurfaceWithFormat(Uint32 flags, int width, int height, int depth, Uint32 format) { SDL_Surface *surface; /* The flags are no longer used, make the compiler happy */ (void)flags; /* Allocate the surface */ surface = (SDL_Surface *) SDL_calloc(1, sizeof(*surface)); if (surface == NULL) { SDL_OutOfMemory(); return NULL; } surface->format = SDL_AllocFormat(format); if (!surface->format) { SDL_FreeSurface(surface); return NULL; } surface->w = width; surface->h = height; surface->pitch = SDL_CalculatePitch(format, width); SDL_SetClipRect(surface, NULL); if (SDL_ISPIXELFORMAT_INDEXED(surface->format->format)) { SDL_Palette *palette = SDL_AllocPalette((1 << surface->format->BitsPerPixel)); if (!palette) { SDL_FreeSurface(surface); return NULL; } if (palette->ncolors == 2) { /* Create a black and white bitmap palette */ palette->colors[0].r = 0xFF; palette->colors[0].g = 0xFF; palette->colors[0].b = 0xFF; palette->colors[1].r = 0x00; palette->colors[1].g = 0x00; palette->colors[1].b = 0x00; } SDL_SetSurfacePalette(surface, palette); SDL_FreePalette(palette); } /* Get the pixels */ if (surface->w && surface->h) { /* Assumptions checked in surface_size_assumptions assert above */ Sint64 size = ((Sint64)surface->h * surface->pitch); if (size < 0 || size > SDL_MAX_SINT32) { /* Overflow... */ SDL_FreeSurface(surface); SDL_OutOfMemory(); return NULL; } surface->pixels = SDL_malloc((size_t)size); if (!surface->pixels) { SDL_FreeSurface(surface); SDL_OutOfMemory(); return NULL; } /* This is important for bitmaps */ SDL_memset(surface->pixels, 0, surface->h * surface->pitch); } /* Allocate an empty mapping */ surface->map = SDL_AllocBlitMap(); if (!surface->map) { SDL_FreeSurface(surface); return NULL; } /* By default surface with an alpha mask are set up for blending */ if (surface->format->Amask) { SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND); } /* The surface is ready to go */ surface->refcount = 1; return surface; }
/** * @brief Call to SDL_AllocPalette and SDL_FreePalette * * @sa http://wiki.libsdl.org/moin.fcg/SDL_AllocPalette * @sa http://wiki.libsdl.org/moin.fcg/SDL_FreePalette */ int pixels_allocFreePalette(void *arg) { const char *expectedError1 = "Parameter 'ncolors' is invalid"; const char *expectedError2 = "Parameter 'palette' is invalid"; const char *error; int variation; int i; int ncolors; SDL_Palette* result; /* Allocate palette */ for (variation = 1; variation <= 3; variation++) { switch (variation) { /* Just one color */ case 1: ncolors = 1; break; /* Two colors */ case 2: ncolors = 2; break; /* More than two colors */ case 3: ncolors = SDLTest_RandomIntegerInRange(8, 16); break; } result = SDL_AllocPalette(ncolors); SDLTest_AssertPass("Call to SDL_AllocPalette(%d)", ncolors); SDLTest_AssertCheck(result != NULL, "Verify result is not NULL"); if (result != NULL) { SDLTest_AssertCheck(result->ncolors == ncolors, "Verify value of result.ncolors; expected: %u, got %u", ncolors, result->ncolors); if (result->ncolors > 0) { SDLTest_AssertCheck(result->colors != NULL, "Verify value of result.colors is not NULL"); if (result->colors != NULL) { for(i = 0; i < result->ncolors; i++) { SDLTest_AssertCheck(result->colors[i].r == 255, "Verify value of result.colors[%d].r; expected: 255, got %u", i, result->colors[i].r); SDLTest_AssertCheck(result->colors[i].g == 255, "Verify value of result.colors[%d].g; expected: 255, got %u", i, result->colors[i].g); SDLTest_AssertCheck(result->colors[i].b == 255, "Verify value of result.colors[%d].b; expected: 255, got %u", i, result->colors[i].b); } } } /* Deallocate again */ SDL_FreePalette(result); SDLTest_AssertPass("Call to SDL_FreePalette()"); } } /* Negative cases */ /* Invalid number of colors */ for (ncolors = 0; ncolors > -3; ncolors--) { SDL_ClearError(); SDLTest_AssertPass("Call to SDL_ClearError()"); result = SDL_AllocPalette(ncolors); SDLTest_AssertPass("Call to SDL_AllocPalette(%d)", ncolors); SDLTest_AssertCheck(result == NULL, "Verify result is NULL"); error = SDL_GetError(); SDLTest_AssertPass("Call to SDL_GetError()"); SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL"); if (error != NULL) { SDLTest_AssertCheck(SDL_strcmp(error, expectedError1) == 0, "Validate error message, expected: '%s', got: '%s'", expectedError1, error); } } /* Invalid free pointer */ SDL_ClearError(); SDLTest_AssertPass("Call to SDL_ClearError()"); SDL_FreePalette(NULL); SDLTest_AssertPass("Call to SDL_FreePalette(NULL)"); error = SDL_GetError(); SDLTest_AssertPass("Call to SDL_GetError()"); SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL"); if (error != NULL) { SDLTest_AssertCheck(SDL_strcmp(error, expectedError2) == 0, "Validate error message, expected: '%s', got: '%s'", expectedError2, error); } return TEST_COMPLETED; }