File: alphatest.cpp

package info (click to toggle)
clanlib 0.5.4-1-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 10,320 kB
  • ctags: 10,893
  • sloc: cpp: 76,056; xml: 3,281; sh: 2,961; perl: 1,204; asm: 837; makefile: 775
file content (101 lines) | stat: -rw-r--r-- 2,435 bytes parent folder | download
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
/*
    alpha test example
*/

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

class SurfaceApp : public CL_ClanApplication
{
public:
	virtual char *get_title() { return "Alpha test application"; }

	virtual int main(int, char **)
	{
		// Create a console window for text-output if not available
		CL_ConsoleWindow console("Console");
		console.redirect_stdio();

		try
		{
			CL_SetupCore::init();
			CL_SetupJPEG::init();
			CL_SetupDisplay::init();
			
			// Set mode: 640x480 16 bpp
			CL_Display::set_videomode(640, 480, 16, false);
		
			// Create a gradient surface
			CL_Canvas *canvas = new CL_Canvas(100, 640);
			canvas->lock();
			unsigned int* data = (unsigned int*) canvas->get_data();
			for (int y=0; y<480; y++)
			{
				for (int x=0; x<100; x++)
				{
					data[x + 100 * y] = CL_Color::get_color(RGBA8888, 
					1.0f,
					0.0f,
					0.0f,
					float (y)/ 480.0f); 
				}
			}			
			canvas->unlock();
			CL_Surface *gradient = CL_Surface::create(canvas, true);
		
			// Load surface from jpg file:
			CL_Surface *surface = CL_JPEGProvider::create("back.jpg", NULL, RGB565);
		
			float alpha = 0.0f;

			// Loop until the user hits escape:
			while (CL_Keyboard::get_keycode(CL_KEY_ESCAPE) == false)
			{
				// Draw surface
				surface->put_screen(0, 0);
				
				// Change the alpha
				alpha += 0.01f;
				if (alpha > 1.0)
					alpha -= 1.0f;

				// Draw some green alpha lines
				for (int y = 10; y < 80; y += 10)
					CL_Display::draw_line(0, y, 640, y, 0.0, 1.0, 0.0, 1.0f - alpha);

				// Draw a blue alpha box				
				CL_Display::fill_rect(30, 30, 290, 170, 0.0, 0.0, 1.0, alpha);

				// Draw the red gradient
				gradient->put_screen(0,0);
			
				// Flip front and backbuffer. This makes the changes visible:
				CL_Display::flip_display();
			
				// Update keyboard input and handle system events:
				// Exits the loop if ClanLib requests shutdown - for instance if
				// someone closes the window.
				CL_System::keep_alive();
			}

			delete surface;
			delete gradient;

			// De-Initialize the ClanLib components
			CL_SetupDisplay::deinit();
			CL_SetupJPEG::deinit();
			CL_SetupCore::deinit();
		}
		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 0;
	}
} app;