2013-09-25 247 views
1

我想将我的函数收到的任何SDL_Surface转换为RGBA8888格式表面,做stuff,并在返回之前将其转换回原始格式。顺便说一句,我正在C工作。如何将任何SDL_Surface转换为RGBA8888格式并返回?

//------------------------------------------------ 
#include <stdio.h> 
#include <stdlib.h> 
#include <stdint.h> 
#include <SDL/SDL.h> 
#include <SDL/SDL_image.h> 
//------------------------------------------------ 
SDL_Surface* formattedSurf = SDL_ConvertSurfaceFormat(surf, 
                 SDL_PIXELFORMAT_RGBA8888, 
                 0); 

收益率:从Eclipse CDT的和GCC类似rsponses: “问题描述符号 'SDL_PIXELFORMAT_RGBA8888' 无法解析”。

[Thu 13/09/26 14:40 PDT][pts/3][x86_64/linux-gnu/3.11.1-1-ARCH][5.0.2] 
<[email protected]:/tmp> 
zsh/2 1821 % pkg-config --cflags --libs sdl 
-D_GNU_SOURCE=1 -D_REENTRANT -I/usr/include/SDL -lSDL -lpthread 
+0

什么是'RGBA8888'格式? – this

+1

他意味着每个通道8位的32位RGBA – emartel

+0

请参阅@ Zamallad的建议。这正是我想要做的。显然,无法获得包含的权利。 – justinzane

回答

1

我从来没有使用它,但它看起来像SDL_ConvertSurfacelink)会怎么做,如果你创建一个SDL_PixelFormatlink),你需要什么对应于您的RGBA8888格式。

+0

有关我的问题的更多具体信息,请参见上面的内容。谢谢。 – justinzane

3

假设你有表面被称作surface和有效,即不为NULL

// Store the current pixel format flag 
Uint32 currFormat = surface->format->format; 

// Convert the surface to a new one with RGBA8888 format 
SDL_Surface* formattedSurf = SDL_ConvertSurfaceFormat(surf, 
                 SDL_PIXELFORMAT_RGBA8888, 
                 0); 

if (formattedSurf != NULL) { 

    // Free original surface 
    SDL_FreeSurface(surface); 

    //////////////////////// 
    // DO YOUR STUFF HERE // 
    //////////////////////// 

    // Re-create original surface with original format 
    surface = SDL_ConvertSurfaceFormat(formattedSurf, currFormat, NULL); 

    // Free the formatted surface 
    SDL_FreeSurface(formattedSurf); 

} 
else { 

    ///////////////////////////////////// 
    // LOG A WARNING OR SOMETHING HERE // 
    ///////////////////////////////////// 

} 
+0

谢谢!也许我会遇到一个#include问题,因为我无法得到任何东西 - Eclipse CDT,scons,gcc - 来识别“SDL_PIXELFORMAT_RGBA8888”。你的建议几乎和我最初尝试的一样。该片段包含哪些内容? – justinzane

+0

你刚刚做了一个更正(为了兼容性的原因,在convert表面函数中将最后一个参数从NULL改为0)。包括SDL.h应该足以实现此功能。也许你可以在pastebin上发布完整的代码? – Zammalad

+0

它也可能是一个链接器问题。你是否设法让任何东西在SDL中运行 - 只是一个普通的窗口或者绘制位图(网上有大量的教程)。 – Zammalad

相关问题