Results 1 to 6 of 6

Thread: Colorkey

  1. #1

    Default Colorkey

    Hello,

    how could I set the colorkey of a surface?

    Only in the resource file?

  2. #2
    Administrator Seth's Avatar
    Join Date
    Jul 2002
    Location
    Japan
    Posts
    5,343

    Default

    You can set the color key in the resource file or by code. You're asking how by code, correct? This should work:

    Code:
    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:

    Code:
    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.
    Seth A. Robinson
    Robinson Technologies

  3. #3

    Default It work's

    Thanks a lot!

  4. #4
    ClanLib Developer
    Join Date
    Sep 2006
    Location
    Bergen, Norway
    Posts
    588

    Default

    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.

  5. #5

    Default

    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.

  6. #6

    Default

    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);
    }

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •