1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
#include <ClanLib/core.h>
#include <ClanLib/application.h>
#include <ClanLib/display.h>
#include <ClanLib/gl.h>
class ExampleCanvas : public CL_ClanApplication
{
public:
virtual int main(int argc, char **argv)
{
// Create a console window for text-output if not available
CL_ConsoleWindow console("Console");
console.redirect_stdio();
quit = false;
try
{
// Initialize ClanLib base components
CL_SetupCore setup_core;
// Initialize the ClanLib display component
CL_SetupDisplay setup_display;
// Initilize the OpenGL drivers
CL_SetupGL setup_gl;
// Set a videomode - 640x480
CL_DisplayWindow window("ClanLib Canvas Example", 1024, 256);
// Connect the Window close event
CL_Slot slot_quit = window.sig_window_close().connect(this, &ExampleCanvas::on_window_close);
// Connect a keyboard handler to on_key_up()
CL_Slot slot_keyboard_up = CL_Keyboard::sig_key_up().connect(this, &ExampleCanvas::on_keyboard_up);
// Connect a mouse handler to on_key_down()
CL_Slot slot_mouse_down = CL_Mouse::sig_key_up().connect(this, &ExampleCanvas::on_mouse_down);
// Load the surfaces
ground = CL_Surface("ground.png");
ground_back = CL_Surface("ground.png");
cutter = CL_Surface("circle1.png");
cutter.set_alignment(origin_center);
canvas_ground = new CL_Canvas(ground);
int width = CL_Display::get_width();
int height = CL_Display::get_height();
scale = 1.0f;
int back_pos = 0;
// Run until someone presses escape
while (!quit)
{
// Draw a nice blue gradient in the background
CL_Display::fill_rect(
CL_Rect(0, 0, width, height),
CL_Gradient(CL_Color::aliceblue, CL_Color::aliceblue, CL_Color::cornflowerblue, CL_Color::cornflowerblue));
// Draw the moving background
back_pos++;
ground_back.draw(int(sinf(back_pos / 100.0f) * 100.0f), 0);
// Draw the ground
ground.draw(0, 0);
// Draw mousecursor
cutter.draw(CL_Mouse::get_x(), CL_Mouse::get_y());
// Flip the display, showing on the screen what we have drawed
// since last call to flip_display()
CL_Display::flip();
// This call updates input and performs other "housekeeping"
// call this each frame
CL_System::keep_alive(10);
}
delete canvas_ground;
ground = CL_Surface();
cutter = CL_Surface();
ground_back = CL_Surface();
}
catch(CL_Error error)
{
std::cout << "Exception caught : " << error.message.c_str() << std::endl;
// Display console close message and wait for a key
console.display_close_message();
return -1;
}
return 0;
}
private:
void on_keyboard_up(const CL_InputEvent &key)
{
if(key.id == CL_KEY_ESCAPE)
quit = true;
}
void on_mouse_down(const CL_InputEvent &key)
{
if(key.id == CL_MOUSE_LEFT)
{
// Set the cutter surface to extract instead of replace colours
cutter.set_blend_func(blend_zero, blend_one_minus_src_alpha);
cutter.draw(CL_Mouse::get_x(), CL_Mouse::get_y(), canvas_ground->get_gc());
// Set cutter back to normal (for showing the mousecursor)
cutter.set_blend_func(blend_src_alpha, blend_one_minus_src_alpha);
}
if(key.id == CL_MOUSE_WHEEL_UP)
{
scale = scale * 1.1f;
cutter.set_scale(scale, scale);
}
if(key.id == CL_MOUSE_WHEEL_DOWN)
{
scale = scale * 0.9f;
cutter.set_scale(scale, scale);
}
}
void on_window_close()
{
quit = true;
}
bool quit;
float scale;
CL_Surface ground;
CL_Surface cutter;
CL_Surface ground_back;
CL_Canvas *canvas_ground;
} my_app;
|