| 1 |
<xml> |
| 2 |
<head> |
| 3 |
<title>Loading graphics</title> |
| 4 |
</head> |
| 5 |
<body> |
| 6 |
|
| 7 |
<h3>Before you load graphics</h3> |
| 8 |
|
| 9 |
<p>Before loading any graphics in ClanLib, you <b>MUST</b> have created a displaywindow first. |
| 10 |
When surfaces are created, they are converted to the most optimal format for fast |
| 11 |
blitting to the display, and therefore you need a displaywindow before loading any graphics.</p> |
| 12 |
|
| 13 |
<h3>Loading from files</h3> |
| 14 |
|
| 15 |
<p>Most ClanLib games will usually use resources to load its graphics, but |
| 16 |
we will start showing how the inner parts of the resource manager loads its |
| 17 |
graphics. Hopefully this should make it more clear how the whole stuff |
| 18 |
works.</p> |
| 19 |
|
| 20 |
<p>To construct a surface or texture in ClanLib, we first need to load it |
| 21 |
into a surface provider. There are surface providers for many common image |
| 22 |
formats, including: CL_PCXProvider, CL_TargaProvider, CL_PNGProvider and |
| 23 |
CL_JPEGProvider.</p> |
| 24 |
|
| 25 |
<p>To use these providers, simple create an instance of it, eg.</p> |
| 26 |
|
| 27 |
<code> |
| 28 |
// Create a provider: |
| 29 |
CL_TargaProvider provider("image.tga"); |
| 30 |
|
| 31 |
// Load it: |
| 32 |
CL_Surface surface(provider); |
| 33 |
</code> |
| 34 |
|
| 35 |
<p>Luckily, ClanLib provides a shortcut for the lazy among us. |
| 36 |
It can automatically figure out which provider to use based on the |
| 37 |
file-extension. Use it like this:</p> |
| 38 |
|
| 39 |
<code> |
| 40 |
CL_Surface surface2("image.tga"); |
| 41 |
</code> |
| 42 |
|
| 43 |
<h3>Loading from resources</h3> |
| 44 |
|
| 45 |
<p>Although the above loading mechanism in itself is quite simple, it is |
| 46 |
often far better to seperate the description of an image from the actual |
| 47 |
game code. Instead of specifying an image by its filename and type, you use |
| 48 |
an name making more sense for the game code.</p> |
| 49 |
|
| 50 |
<code> |
| 51 |
CL_ResourceManager resources("resources.xml"); |
| 52 |
|
| 53 |
CL_Surface surface("InGame/Level1/background", &resources); |
| 54 |
</code> |
| 55 |
|
| 56 |
<p>The image may be in any format supported by the ClanLib surface |
| 57 |
providers, and the resource description may include transparency, subarrays |
| 58 |
and other fancy stuff. The resources.xml file includes a description of the |
| 59 |
resource that may look like this:</p> |
| 60 |
|
| 61 |
<code> |
| 62 |
<resources> |
| 63 |
<section name="InGame"> |
| 64 |
<section name="Level1"> |
| 65 |
<surface name="background" file="background1.tga" /> |
| 66 |
<surface name="walking_man" file="man.pcx" tcol="0,1,2,3" /> |
| 67 |
</section> |
| 68 |
</section> |
| 69 |
</resources> |
| 70 |
</code> |
| 71 |
|
| 72 |
<p>As you can probably see, the game code no longer needs to know how the |
| 73 |
resource is physically loaded. And it is possible to change the image |
| 74 |
without recompiling the application.</p> |
| 75 |
|
| 76 |
<p>The resource manager may also be attached to something else than physical |
| 77 |
files, it may be loading them from a network server, or a zipfile. The nice |
| 78 |
thing is that the game code does not need to know.</p> |
| 79 |
|
| 80 |
<p>If you want more information about resources, have a look at the <a |
| 81 |
href="resources-1.html">resource overview</a>.</p> |
| 82 |
|
| 83 |
<!-- |
| 84 |
<h3>Creating from scratch</h3> |
| 85 |
|
| 86 |
<p>NOTE THAT CANVAS IS NOT FULLY TESTED IN ClanLib 0.7!</p> |
| 87 |
|
| 88 |
<p>If you need to create an image from scratch, or want to compose an image |
| 89 |
from multiple other images, or modify an image in some way, ClanLib provides |
| 90 |
a surface type called CL_Canvas. At construction time, you tell how large |
| 91 |
and in what format the canvas should be in, and then afterwards you modify |
| 92 |
the contents of it by drawling lines, boxes, surfaces on it.</p> |
| 93 |
|
| 94 |
<code> |
| 95 |
// Load an image: |
| 96 |
CL_Surface *surf = new CL_Surface("image.tga"); |
| 97 |
|
| 98 |
// Create a canvas, size 320x200, default image format: |
| 99 |
CL_Canvas *canvas = new CL_Canvas(CL_Size(320, 200)); |
| 100 |
|
| 101 |
// Draw a line on it: |
| 102 |
canvas->draw_line(0,0,100,100); |
| 103 |
|
| 104 |
// Draw the surface on it: |
| 105 |
surf->draw(10, 10, canvas); |
| 106 |
|
| 107 |
// Construct a surface from it: |
| 108 |
CL_Surface *canvas_surf = CL_Surface::Create(canvas, true); |
| 109 |
</code> |
| 110 |
--> |
| 111 |
|
| 112 |
</body> |
| 113 |
</xml> |