static int SW_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect, Uint32 format, void * pixels, int pitch) { SDL_Surface *surface = SW_ActivateRenderer(renderer); Uint32 src_format; void *src_pixels; if (!surface) { return -1; } if (rect->x < 0 || rect->x+rect->w > surface->w || rect->y < 0 || rect->y+rect->h > surface->h) { SDL_SetError("Tried to read outside of surface bounds"); return -1; } src_format = SDL_MasksToPixelFormatEnum( surface->format->BitsPerPixel, surface->format->Rmask, surface->format->Gmask, surface->format->Bmask, surface->format->Amask); src_pixels = (void*)((Uint8 *) surface->pixels + rect->y * surface->pitch + rect->x * surface->format->BytesPerPixel); return SDL_ConvertPixels(rect->w, rect->h, src_format, src_pixels, surface->pitch, format, pixels, pitch); }
SDL_Surface * SDL_DisplayFormatAlpha(SDL_Surface * surface) { SDL_PixelFormat *vf; SDL_PixelFormat *format; SDL_Surface *converted; /* default to ARGB8888 */ Uint32 amask = 0xff000000; Uint32 rmask = 0x00ff0000; Uint32 gmask = 0x0000ff00; Uint32 bmask = 0x000000ff; if (!SDL_PublicSurface) { SDL_SetError("No video mode has been set"); return NULL; } vf = SDL_PublicSurface->format; switch (vf->BytesPerPixel) { case 2: /* For XGY5[56]5, use, AXGY8888, where {X, Y} = {R, B}. For anything else (like ARGB4444) it doesn't matter since we have no special code for it anyway */ if ((vf->Rmask == 0x1f) && (vf->Bmask == 0xf800 || vf->Bmask == 0x7c00)) { rmask = 0xff; bmask = 0xff0000; } break; case 3: case 4: /* Keep the video format, as long as the high 8 bits are unused or alpha */ if ((vf->Rmask == 0xff) && (vf->Bmask == 0xff0000)) { rmask = 0xff; bmask = 0xff0000; } break; default: /* We have no other optimised formats right now. When/if a new optimised alpha format is written, add the converter here */ break; } format = SDL_AllocFormat(SDL_MasksToPixelFormatEnum(32, rmask, gmask, bmask, amask)); if (!format) { return NULL; } converted = SDL_ConvertSurface(surface, format, SDL_RLEACCEL); SDL_FreeFormat(format); return converted; }
static int lua_SDL_MasksToPixelFormatEnum(State & state) { Stack * stack = state.stack; if (stack->is<LUA_TNUMBER>(1) && stack->is<LUA_TNUMBER>(2) && stack->is<LUA_TNUMBER>(3) && stack->is<LUA_TNUMBER>(4) && stack->is<LUA_TNUMBER>(5)) { int bpp = stack->to<int>(1); Uint32 Rmask = stack->to<int>(1); Uint32 Gmask = stack->to<int>(2); Uint32 Bmask = stack->to<int>(3); Uint32 Amask = stack->to<int>(4); Uint32 format = SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask); stack->push<int>(format); return 1; } return 0; }
/* * 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) { Uint32 format; /* Get the pixel format */ format = SDL_MasksToPixelFormatEnum(depth, Rmask, Gmask, Bmask, Amask); if (format == SDL_PIXELFORMAT_UNKNOWN) { SDL_SetError("Unknown pixel format"); return NULL; } return SDL_CreateRGBSurfaceWithFormat(flags, width, height, depth, format); }
/* * 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; }
int WIN_CreateWindowFramebuffer(_THIS, SDL_Window * window, Uint32 * format, void ** pixels, int *pitch) { SDL_WindowData *data = (SDL_WindowData *) window->driverdata; size_t size; LPBITMAPINFO info; HBITMAP hbm; /* Free the old framebuffer surface */ if (data->mdc) { DeleteDC(data->mdc); } if (data->hbm) { DeleteObject(data->hbm); } /* Find out the format of the screen */ size = sizeof(BITMAPINFOHEADER) + 256 * sizeof (RGBQUAD); info = (LPBITMAPINFO)SDL_stack_alloc(Uint8, size); SDL_memset(info, 0, size); info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); /* The second call to GetDIBits() fills in the bitfields */ hbm = CreateCompatibleBitmap(data->hdc, 1, 1); GetDIBits(data->hdc, hbm, 0, 0, NULL, info, DIB_RGB_COLORS); GetDIBits(data->hdc, hbm, 0, 0, NULL, info, DIB_RGB_COLORS); DeleteObject(hbm); *format = SDL_PIXELFORMAT_UNKNOWN; if (info->bmiHeader.biCompression == BI_BITFIELDS) { int bpp; Uint32 *masks; bpp = info->bmiHeader.biPlanes * info->bmiHeader.biBitCount; masks = (Uint32*)((Uint8*)info + info->bmiHeader.biSize); *format = SDL_MasksToPixelFormatEnum(bpp, masks[0], masks[1], masks[2], 0); } if (*format == SDL_PIXELFORMAT_UNKNOWN) { /* We'll use RGB format for now */ *format = SDL_PIXELFORMAT_RGB888; /* Create a new one */ SDL_memset(info, 0, size); info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); info->bmiHeader.biPlanes = 1; info->bmiHeader.biBitCount = 32; info->bmiHeader.biCompression = BI_RGB; } /* Fill in the size information */ *pitch = (((window->w * SDL_BYTESPERPIXEL(*format)) + 3) & ~3); info->bmiHeader.biWidth = window->w; info->bmiHeader.biHeight = -window->h; /* negative for topdown bitmap */ info->bmiHeader.biSizeImage = window->h * (*pitch); data->mdc = CreateCompatibleDC(data->hdc); data->hbm = CreateDIBSection(data->hdc, info, DIB_RGB_COLORS, pixels, NULL, 0); SDL_stack_free(info); if (!data->hbm) { return WIN_SetError("Unable to create DIB"); } SelectObject(data->mdc, data->hbm); return 0; }
int video_set_mode(s_videomodes videomodes) { //if(memcmp(&stored_videomodes, &videomodes, sizeof(videomodes))==0) return 1; stored_videomodes = videomodes; int b; int allocTextureWidth, allocTextureHeight; savedata.screen[videoMode][0] = 0; savedata.fullscreen = 1; bytes_per_pixel = videomodes.pixel; b = bytes_per_pixel - 1; //destroy all if(bscreen) { SDL_FreeSurface(bscreen); } bscreen = NULL; if(texture) { SDL_DestroyTexture(texture); } texture = NULL; if(buttons) { SDL_DestroyTexture(buttons); } buttons = NULL; //mysterious crash in sdl 2.0 if these are recreated, so leave them alone for now //if(renderer) SDL_DestroyRenderer(renderer); //renderer = NULL; //if(window) SDL_DestroyWindow(window); //window = NULL; if(videomodes.hRes == 0 && videomodes.vRes == 0) { return 0; } viewportWidth = nativeWidth; viewportHeight = nativeHeight; if(!window && !(window = SDL_CreateWindow("OpenBOR", 0, 0, nativeWidth, nativeHeight, SDL_WINDOW_SHOWN | SDL_WINDOW_FULLSCREEN))) { printf("error: %s\n", SDL_GetError()); return 0; } if(!renderer && !(renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED))) { printf("error: %s\n", SDL_GetError()); return 0; } SDL_RendererInfo info; SDL_GetRendererInfo(renderer, &info); printf("SDL video Renderer: %s \n", info.name); SDL_SetHintWithPriority(SDL_HINT_RENDER_SCALE_QUALITY, savedata.glfilter[savedata.fullscreen] ? "nearest" : "linear", SDL_HINT_DEFAULT); // now create a texture textureWidth = videomodes.hRes; textureHeight = videomodes.vRes; allocTextureWidth = nextpowerof2(textureWidth); allocTextureHeight = nextpowerof2(textureHeight); int format = SDL_MasksToPixelFormatEnum (textureDepths[b], masks[b][0], masks[b][1], masks[b][2], masks[b][3]); if(!(texture = SDL_CreateTexture(renderer, format, SDL_TEXTUREACCESS_STREAMING, allocTextureWidth, allocTextureHeight))) { printf("error: %s\n", SDL_GetError()); return 0; } setup_touch(); if(!buttons) { bscreen = pngToSurface(buttonpng); if(!bscreen || !(buttons = SDL_CreateTextureFromSurface(renderer, bscreen))) { printf("error: %s\n", SDL_GetError()); return 0; } SDL_FreeSurface(bscreen); bscreen = NULL; } //create a buffer for 8bit mode, masks don't really matter but anyway set them if(bytes_per_pixel == 1) { bscreen = SDL_CreateRGBSurface(SDL_SWSURFACE, textureWidth, textureHeight, textureDepths[b], masks[b][0], masks[b][1], masks[b][2], masks[b][3]); } video_clearscreen(); return 1; }
static int Init() { nInitedSubsytems = SDL_WasInit(SDL_INIT_VIDEO); if (!(nInitedSubsytems & SDL_INIT_VIDEO)) { SDL_InitSubSystem(SDL_INIT_VIDEO); } nUseBlitter = 0; nGameWidth = nVidImageWidth; nGameHeight = nVidImageHeight; nRotateGame = 0; //nVidRotationAdjust = 1; // add_shin if (bDrvOkay) { // Get the game screen size BurnDrvGetVisibleSize(&nGameWidth, &nGameHeight); printf( "vid_sdlfx.Init: nGame: %ix%i", nGameWidth, nGameHeight ); if (BurnDrvGetFlags() & BDF_ORIENTATION_VERTICAL) { if (nVidRotationAdjust & 1) { int n = nGameWidth; nGameWidth = nGameHeight; nGameHeight = n; nRotateGame |= (nVidRotationAdjust & 2); } else { nRotateGame |= 1; } } if (BurnDrvGetFlags() & BDF_ORIENTATION_FLIPPED) { nRotateGame ^= 2; } } nSize = VidSoftFXGetZoom(nUseBlitter); bVidScanlines = 0; // !!! if( ( sdlFramebuf = SDL_SetVideoMode(nGameWidth * nSize, nGameHeight * nSize, 0, SDL_RESIZABLE | SDL_HWSURFACE ) ) == NULL ) return 1; printf("android_sdlfx.Init: SDL_SetVideoMode( %i, %i )", nGameWidth * nSize, nGameHeight * nSize ); printf("android_sdlfx.Init: sdlFramebuf PixelFormat = %s", SDL_GetPixelFormatName( SDL_MasksToPixelFormatEnum( sdlFramebuf->format->BitsPerPixel, sdlFramebuf->format->Rmask, sdlFramebuf->format->Gmask, sdlFramebuf->format->Bmask, sdlFramebuf->format->Amask) ) ); printf("android_sdlfx.Init: ( %i, %u, %u, %u, %u )", sdlFramebuf->format->BitsPerPixel, sdlFramebuf->format->Rmask, sdlFramebuf->format->Gmask, sdlFramebuf->format->Bmask, sdlFramebuf->format->Amask ); SDL_SetClipRect(sdlFramebuf, NULL); // Initialize the buffer surfaces BlitFXInit(); if (VidSoftFXInit(nUseBlitter, nRotateGame)) { if (VidSoftFXInit(0, nRotateGame)) { Exit(); return 1; } } printf( "vid_sdlfx.Init: nRotateGame=%i", nRotateGame ); return 0; }