PDA

View Full Version : Colorkey



andreaszdw
11-22-2006, 11:35 AM
Hello,

how could I set the colorkey of a surface?

Only in the resource file?

Seth
11-23-2006, 01:34 AM
You can set the color key in the resource file or by code. You're asking how by code, correct? This should work:



CL_PixelBuffer pbuf = CL_ProviderFactory::load("some_image.png"); //load the image from disk and put it in a pixel buffer (any image format and bit depth is ok)
pbuf.set_colorkey(true, CL_Color(255,255,0).color); //cyan color key

//convert it into a surface which will get put into video ram during its first use.
//internally, the color key will cause all cyan pixels to get full alpha transparancy
//during the conversion to the video card's image format

CL_Surface surf(pbuf);

//draw it

surf.draw(0,0);

Or, if the image is already in a surface and you want to convert it, it's very similar:


CL_Surface test("some_image.png");

CL_PixelBuffer pbuf = test.get_pixeldata();
pbuf.set_colorkey(true, (CL_Color::white).color);
test.set_pixeldata(pbuf);

Note, if you have any problems please try the latest SVN 0.8, because I recently fixed some bugs in the colorkey conversions.

andreaszdw
11-23-2006, 08:30 AM
Thanks a lot!

sphair
11-23-2006, 03:52 PM
If you can use 32bit images, its also a solution to save the images as PNG or TGA with alpha channels. Then you won't have to do any post-loading processing.

Nikitto46
04-16-2007, 09:40 AM
Hello!
This is my code :

#include <ClanLib/core.h>
#include <ClanLib/application.h>
#include <ClanLib/display.h>
#include <ClanLib/gl.h>

class MyApp : public CL_ClanApplication
{
public:
virtual int main(int argc, char **argv)
{
CL_ConsoleWindow console("Console");
console.redirect_stdio();
try
{
CL_SetupCore setup_core;
CL_SetupDisplay setup_display;
CL_SetupGL setup_gl;
CL_DisplayWindow window("ClanLib application", 640, 480);

CL_PixelBuffer pbuf = CL_ProviderFactory::load("Image.png");
pbuf.set_colorkey(true, CL_Color::magenta.color);

CL_Surface surf(pbuf);

while(!CL_Keyboard::get_keycode(CL_KEY_ESCAPE))
{
CL_Display::clear(CL_Color(0, 0, 50));

surf.draw(0,0);

CL_Display::flip();

CL_System::keep_alive(10);
}
}
catch (CL_Error err)
{
std::cout << err.message.c_str() << std::endl;
}
console.display_close_message();
return 0;
}
public:
} app;

but when I run it , the result is that the Image.png is draw without the transparent in magenta color..why??

I use clanlib 8.0 , visual c 6 and win xp

sorry for my english , please answer me.

Nikitto46
04-16-2007, 11:03 AM
I found the solution :

I set the alpha color :

CL_Color colore;
colore=CL_Color::magenta;
colore.set_alpha(0);

CL_PixelBuffer pbuf = CL_ProviderFactory::load("Image.png");
pbuf.set_colorkey(true, colore.color);

CL_Surface surf(pbuf);

while(!CL_Keyboard::get_keycode(CL_KEY_ESCAPE))
{
surf.draw(0,100);

CL_Display::flip();

CL_System::keep_alive(10);
}