File: example2d.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 (120 lines) | stat: -rw-r--r-- 3,832 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <ClanLib/core.h>
#include <ClanLib/application.h>
#include <ClanLib/display.h>

class Example2D : public CL_ClanApplication
{
public:
	// This function returns the name of your game
	virtual char *get_title()
	{
		return "ClanLib 2D API example";
	}

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

		try
		{
			// Initialize ClanLib base components
			CL_SetupCore::init();

			// Initialize the ClanLib display component
			CL_SetupDisplay::init();

			// Set a videomode - 640x480x16bpp
			// false means that this program should not run full-screen
			CL_Display::set_videomode(640, 480, 16, false);

			// Load a surface from a targa-file using the targa-file
			// image loader. The targa-file image loader creates a 
			// surface from "logo.tga".
			CL_Surface *sur_logo = CL_TargaProvider::create("logo.tga", NULL);

			double sin_count = 0.0f;
			float ypos = 0.0f;
			float ydir = 3.0f;

			// Run until someone presses escape
			while (!CL_Keyboard::get_keycode(CL_KEY_ESCAPE))
			{
				// Clear the display in a dark blue nuance
				// The four arguments are red, green, blue and alpha
				// All color nuances in ClanLib are measured in the interval 0->1
				CL_Display::clear_display(0, 0, 0.2f, 1.0f);

				ypos += ydir;
				if (ypos+200 >= CL_Display::get_height() || ypos < 0)
				{
					ydir *= -1;
				}
				
				CL_Display::draw_line(0, (int)(ypos-1), CL_Display::get_width(), (int)(ypos-1), 1, 0, 0);
				CL_Display::draw_line(0, (int)(ypos+198), CL_Display::get_width(), (int)(ypos+198), 1, 0, 0);

				// Show the logo surface.
				// Use the get_width() and get_height() functions of both
				// CL_Display and CL_Surface, to show the surface in the bottom right corner
				sur_logo->put_screen(
					CL_Display::get_width()-sur_logo->get_width(),
					CL_Display::get_height()-sur_logo->get_height());

				CL_Display::push_clip_rect(CL_ClipRect(0, (int)(ypos), CL_Display::get_width(), (int)(ypos+198)));

				// Draw a rectangle in the center of the screen
				// going from (240, 140) -> (440, 340) _not_ including the 
				// pixels in the right-most column and bottom-most row (440, 340)
				CL_Display::fill_rect(240, 140, 440, 340, 1.0f, 1.0f, 1.0f, 1.0f);

				// Frame the rectangle with red lines
				CL_Display::draw_line(240, 140, 440, 140, 1.0f, 0, 0);
				CL_Display::draw_line(240, 340, 440, 340, 1.0f, 0, 0);
				CL_Display::draw_line(240, 140, 240, 340, 1.0f, 0, 0);
				CL_Display::draw_line(440, 140, 440, 340, 1.0f, 0, 0);

				// Show a few alpha-blending moving rectangles that moves in circles
				float x = cos(sin_count)*120;
				float y = sin(sin_count)*120;
				sin_count += 0.05;
				CL_Display::fill_rect((int)(320+x-30), (int)(240+y-30), (int)(320+x+30), (int)(240+y+30), 0, 1, 0, 0.5);
				x = cos(sin_count+3.14159)*120;
				y = sin(sin_count+3.14159)*120;
				CL_Display::fill_rect((int)(320+x-30),(int)(240+y-30),(int)(320+x+30),(int)(240+y+30), 1, 1, 0, 0.5);

				CL_Display::pop_clip_rect();

				// Flip the display, showing on the screen what we have drawed
				// since last call to flip_display()
				CL_Display::flip_display();

				// This call updates input and performs other "housekeeping"
				// call this each frame
				CL_System::keep_alive();
			}

			//Clean-up and remove the logo from memory
			delete sur_logo;
			
			// De-Initialize the ClanLib display component
			CL_SetupDisplay::deinit();

			// De-Initialize ClanLib base components
			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 -1;
		}

		return 0;
	}
} my_app;