File: loading_graphics.xml

package info (click to toggle)
clanlib 0.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 28,372 kB
  • ctags: 16,520
  • sloc: cpp: 101,145; sh: 8,752; xml: 6,410; makefile: 1,740; ansic: 463; perl: 424; php: 247
file content (113 lines) | stat: -rw-r--r-- 3,611 bytes parent folder | download | duplicates (7)
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
<xml>
<head>
<title>Loading graphics</title>
</head>
<body>

<h3>Before you load graphics</h3>

<p>Before loading any graphics in ClanLib, you <b>MUST</b> have created a displaywindow first.
When surfaces are created, they are converted to the most optimal format for fast
blitting to the display, and therefore you need a displaywindow before loading any graphics.</p>

<h3>Loading from files</h3>

<p>Most ClanLib games will usually use resources to load its graphics, but
we will start showing how the inner parts of the resource manager loads its
graphics. Hopefully this should make it more clear how the whole stuff
works.</p>

<p>To construct a surface or texture in ClanLib, we first need to load it
into a surface provider. There are surface providers for many common image
formats, including: CL_PCXProvider, CL_TargaProvider, CL_PNGProvider and
CL_JPEGProvider.</p>

<p>To use these providers, simple create an instance of it, eg.</p>

<code>
// Create a provider:
CL_TargaProvider provider("image.tga");

// Load it:
CL_Surface surface(provider);
</code>

<p>Luckily, ClanLib provides a shortcut for the lazy among us.
It can automatically figure out which provider to use based on the
file-extension. Use it like this:</p>

<code>
CL_Surface surface2("image.tga");
</code>

<h3>Loading from resources</h3>

<p>Although the above loading mechanism in itself is quite simple, it is
often far better to seperate the description of an image from the actual
game code. Instead of specifying an image by its filename and type, you use
an name making more sense for the game code.</p>

<code>
CL_ResourceManager resources("resources.xml");

CL_Surface surface("InGame/Level1/background", &resources);
</code>

<p>The image may be in any format supported by the ClanLib surface
providers, and the resource description may include transparency, subarrays
and other fancy stuff. The resources.xml file includes a description of the
resource that may look like this:</p>

<code>
<resources>
	<section name="InGame">
		<section name="Level1">
			<surface name="background" file="background1.tga" />
			<surface name="walking_man" file="man.pcx" tcol="0,1,2,3" />
		</section>
	</section>
</resources>
</code>

<p>As you can probably see, the game code no longer needs to know how the
resource is physically loaded. And it is possible to change the image
without recompiling the application.</p>

<p>The resource manager may also be attached to something else than physical
files, it may be loading them from a network server, or a zipfile. The nice
thing is that the game code does not need to know.</p>

<p>If you want more information about resources, have a look at the <a
href="resources-1.html">resource overview</a>.</p>

<!--
<h3>Creating from scratch</h3>

<p>NOTE THAT CANVAS IS NOT FULLY TESTED IN ClanLib 0.7!</p>

<p>If you need to create an image from scratch, or want to compose an image
from multiple other images, or modify an image in some way, ClanLib provides
a surface type called CL_Canvas. At construction time, you tell how large
and in what format the canvas should be in, and then afterwards you modify
the contents of it by drawling lines, boxes, surfaces on it.</p>

<code>
// Load an image:
CL_Surface *surf = new CL_Surface("image.tga");

// Create a canvas, size 320x200, default image format:
CL_Canvas *canvas = new CL_Canvas(CL_Size(320, 200));

// Draw a line on it:
canvas->draw_line(0,0,100,100);

// Draw the surface on it:
surf->draw(10, 10, canvas);

// Construct a surface from it:
CL_Surface *canvas_surf = CL_Surface::Create(canvas, true);
</code>
-->

</body>
</xml>