File: intro.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 (154 lines) | stat: -rw-r--r-- 3,038 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
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
146
147
148
149
150
151
152
153
154
#include <ClanLib/core.h>
#include <ClanLib/display.h>
#include <ClanLib/sound.h>
#include "intro.h"
#include "texture.h"

/////////////////////////////////////////////////////////////////////////////
// Construction:

Intro::Intro(CL_ResourceManager *resources)
{
	font = new CL_Font("Intro/font", resources);

	background = Texture::load("Intro/background", resources);

	sample_write = new CL_SoundBuffer("Intro/Sounds/write", resources);
	sample_background = new CL_SoundBuffer("Intro/Sounds/background", resources);

	session_write = sample_write->prepare();
	session_background = sample_background->prepare(true);

	line[0] = "In the year 2045...";
	line[1] = "Technology has reached";
	line[2] = "a point where we fight wars";
	line[3] = "deep in space.";
	line[4] = "They call the fight...ehrm...";
	line[5] = "NetObjects Example.";

	current_time = 0;
	current_line = 0;
	current_pos = 0;

	state = 0;

	back_x = -512.0f;
	back_y = -384.0f;
	alpha = -0.2f;
}

Intro::~Intro()
{
}

/////////////////////////////////////////////////////////////////////////////
// Operations:

void Intro::run()
{
	bool quit = false;
	float last_time = CL_System::get_time();

	do 
	{
		if(CL_Keyboard::get_keycode(CL_KEY_ESCAPE))
			quit = true;

		float delta_time = (CL_System::get_time() - last_time) / 1000.0f;
		last_time = CL_System::get_time();

		if(update(delta_time) == false)
			quit = true;
		else
			draw();

		CL_Display::flip_display();

		CL_System::keep_alive();
		CL_System::sleep(10);
	} while (quit == false);
}

/////////////////////////////////////////////////////////////////////////////
// Implementation:

bool Intro::update(float time_elapsed)
{
	current_time += time_elapsed;

	back_x += time_elapsed * 9;
	back_y += time_elapsed * 8; 

	alpha += 0.05f * time_elapsed;

	switch(state)
	{
	case 0:		// Waiting for a new line to start
		if(current_time > 1.0f)
		{
			current_time = 0.0f;
			state++;
		}
		break;
	case 1:		// Writing text
		while(current_time > 0.05f)
		{
			current_time -= 0.05f;
			current_pos++;

			if(session_write.is_playing() == false)
				if(current_pos < line[current_line].size() - 3)
					session_write.play();

			if(current_pos > line[current_line].size())
			{
				current_time = 0.0f;
				state++;
				break;
			}
		}
		break;
	case 2:		// Waiting for last line to show a while
		if(current_time > 3.0f)
		{
			current_time = 0.0f;
			current_pos = 0;
			current_line++;
			state = 0;
			if(current_line == 6)
				return false;
		}
		break;
	}

	return true;
}

void Intro::draw()
{
	session_background.play();

	RGBA col[4];
	for(int j=0; j<4; j++)
	{
		col[j].rgba[0] = 1.0f;
		col[j].rgba[1] = 1.0f;
		col[j].rgba[2] = 1.0f;
		col[j].rgba[3] = alpha;
	}

	CL_Display::clear_display(0.0f, 0.0f, 0.0f);
	background->put_screen(-back_x, -back_y, 0.0f, 1.0f, col);

	glColor4f(1.0f, 1.0f, 1.0f, 1.0f);

	char cur[2] = {0};
	int x = 0;
	for(int i=0; i < current_pos; i++)
	{
		cur[0] = line[current_line][i];

		font->print_left(10 + x, 440, cur);
		x += font->get_char_width(cur[0]);
	}
}