| 1 |
/* |
| 2 |
alpha test example |
| 3 |
*/ |
| 4 |
|
| 5 |
#include <ClanLib/core.h> |
| 6 |
#include <ClanLib/application.h> |
| 7 |
#include <ClanLib/display.h> |
| 8 |
#include <ClanLib/gl.h> |
| 9 |
|
| 10 |
class SurfaceApp : public CL_ClanApplication |
| 11 |
{ |
| 12 |
private: |
| 13 |
|
| 14 |
bool quit; |
| 15 |
|
| 16 |
public: |
| 17 |
void on_input_up(const CL_InputEvent &key) |
| 18 |
{ |
| 19 |
if(key.id == CL_KEY_ESCAPE) |
| 20 |
quit = true; |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
virtual int main(int, char **) |
| 25 |
{ |
| 26 |
quit = false; |
| 27 |
|
| 28 |
// Create a console window for text-output if not available |
| 29 |
CL_ConsoleWindow console("Console"); |
| 30 |
console.redirect_stdio(); |
| 31 |
|
| 32 |
try |
| 33 |
{ |
| 34 |
CL_SetupCore::init(); |
| 35 |
CL_SetupDisplay::init(); |
| 36 |
CL_SetupGL::init(); |
| 37 |
|
| 38 |
// Set mode: 640x480 16 bpp |
| 39 |
CL_DisplayWindow window("Alpha Blending Test", 800, 600); |
| 40 |
|
| 41 |
// Connect a keyboard handler to on_input_up() |
| 42 |
CL_Slot slot_input_up = window.get_ic()->get_keyboard().sig_key_up().connect( |
| 43 |
this, &SurfaceApp::on_input_up); |
| 44 |
|
| 45 |
// Create a gradient surface |
| 46 |
CL_Canvas *canvas = new CL_Canvas(CL_Size(100, 640)); |
| 47 |
CL_PixelBuffer &pb = canvas->get_pixelbuffer(); |
| 48 |
pb.lock(); |
| 49 |
unsigned int* data = (unsigned int*) pb.get_data(); |
| 50 |
for (int y=0; y<480; y++) |
| 51 |
{ |
| 52 |
for (int x=0; x<100; x++) |
| 53 |
{ |
| 54 |
data[x + 100 * y] = (int)CL_Color( |
| 55 |
255, |
| 56 |
0, |
| 57 |
0, |
| 58 |
y/480); |
| 59 |
} |
| 60 |
} |
| 61 |
pb.unlock(); |
| 62 |
|
| 63 |
// Load surface from jpg file: |
| 64 |
CL_JPEGProvider jpeg("back.jpg"); |
| 65 |
CL_Surface surface(&jpeg); |
| 66 |
|
| 67 |
int alpha = 0; |
| 68 |
|
| 69 |
// Loop indefinetly |
| 70 |
while (!quit) |
| 71 |
{ |
| 72 |
// Draw surface |
| 73 |
surface.draw(0, 0, window.get_gc()); |
| 74 |
|
| 75 |
// Change the alpha |
| 76 |
alpha += 1; |
| 77 |
if (alpha > 255) |
| 78 |
alpha -= 255; |
| 79 |
|
| 80 |
// Draw some green alpha lines |
| 81 |
for (int y = 10; y < 80; y += 10) |
| 82 |
CL_Display::draw_line(0, y, 640, y, CL_Color(0,255,0, 255 - alpha)); |
| 83 |
|
| 84 |
// Draw a blue alpha box |
| 85 |
CL_Display::fill_rect(CL_Rect(30, 30, 290, 170), CL_Color(0,0,255,alpha)); |
| 86 |
|
| 87 |
// Draw the red gradient |
| 88 |
canvas->draw(0,0); |
| 89 |
|
| 90 |
// Flip front and backbuffer. This makes the changes visible: |
| 91 |
CL_Display::flip(); |
| 92 |
|
| 93 |
// Update keyboard input and handle system events: |
| 94 |
// Exits the loop if ClanLib requests shutdown - for instance if |
| 95 |
// someone closes the window. |
| 96 |
CL_System::keep_alive(); |
| 97 |
} |
| 98 |
|
| 99 |
// De-Initialize the ClanLib components |
| 100 |
CL_SetupDisplay::deinit(); |
| 101 |
CL_SetupCore::deinit(); |
| 102 |
} |
| 103 |
catch(CL_Error error) |
| 104 |
{ |
| 105 |
std::cout << "Exception caught : " << error.message.c_str() << std::endl; |
| 106 |
|
| 107 |
// Display console close message and wait for a key |
| 108 |
console.display_close_message(); |
| 109 |
} |
| 110 |
|
| 111 |
return 0; |
| 112 |
} |
| 113 |
} app; |