blob: df5ed53cc2e945e6c257af01f38a00e263dd2150 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001
2/*
3 DirectFB Tutorials
4
5 (c) Copyright 2000-2002 convergence integrated media GmbH.
6 (c) Copyright 2002 convergence GmbH.
7 All rights reserved.
8
9 Written by Denis Oliver Kropp <dok@directfb.org>,
10 Andreas Hundt <andi@fischlustig.de> and
11 Sven Neumann <neo@directfb.org>.
12
13 This file is subject to the terms and conditions of the MIT License:
14
15 Permission is hereby granted, free of charge, to any person
16 obtaining a copy of this software and associated documentation
17 files (the "Software"), to deal in the Software without restriction,
18 including without limitation the rights to use, copy, modify, merge,
19 publish, distribute, sublicense, and/or sell copies of the Software,
20 and to permit persons to whom the Software is furnished to do so,
21 subject to the following conditions:
22
23 The above copyright notice and this permission notice shall be
24 included in all copies or substantial portions of the Software.
25
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
29 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
30 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
31 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
32 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33*/
34
35/**
36 * image.c
37 *
38 * Image loading and displaying
39 */
40
41#include <stdio.h>
42#include <unistd.h>
43#include <string.h>
44
45#include <directfb.h>
46
47int ParseArgs( int argc, char *argv[] );
48/*
49 * (Globals)
50 */
51static IDirectFB *dfb = NULL;
52static IDirectFBSurface *primary = NULL;
53static int screen_width = 0;
54static int screen_height = 0;
55#define DFBCHECK(x...) \
56 { \
57 DFBResult err = x; \
58 \
59 if (err != DFB_OK) \
60 { \
61 fprintf( stderr, "%s <%d>:\n\t", __FILE__, __LINE__ ); \
62 DirectFBErrorFatal( #x, err ); \
63 } \
64 }
65
66#define MAX_FILE_NAME_SIZE 100
67/*
68 * The image is to be loaded into a surface that we can blit from.
69 */
70static IDirectFBSurface *logo = NULL;
71static char m_filename[MAX_FILE_NAME_SIZE];
72
73int ParseArgs( int argc, char *argv[] )
74{
75 /* Parse the command line. */
76 if (argc < 2 || !argv[1] || !argv[1][0]) {
77 printf("Usage: %s <filename>\n", argv[0]);
78 exit(1);
79 }
80 strlcpy(m_filename, argv[1], MAX_FILE_NAME_SIZE);
81 printf("filename %s\n", m_filename);
82
83 return 0;
84}
85
86
87int main (int argc, char **argv)
88{
89 int i;
90
91 /*
92 * (Locals)
93 */
94 DFBSurfaceDescription dsc;
95 DFBImageDescription m_dsc;
96 DFBSurfaceBlittingFlags m_flags = 0;
97
98 /*
99 * Loading an image is done with an Image Provider.
100 */
101 IDirectFBImageProvider *provider;
102
103 /*
104 * (Initialize)
105 */
106 ParseArgs(argc,argv);
107
108 DFBCHECK (DirectFBInit (&argc, &argv));
109 DFBCHECK (DirectFBCreate (&dfb));
110 DFBCHECK (dfb->SetCooperativeLevel (dfb, DFSCL_FULLSCREEN));
111 dsc.flags = DSDESC_CAPS;
112 dsc.caps = DSCAPS_PRIMARY | DSCAPS_FLIPPING;
113 DFBCHECK (dfb->CreateSurface( dfb, &dsc, &primary ));
114 DFBCHECK (primary->GetSize (primary, &screen_width, &screen_height));
115
116 /*
117 * First we need to create an Image Provider by passing a filename.
118 * DirectFB will find (or not) an Image Provider for the file type.
119 */
120 DFBCHECK (dfb->CreateImageProvider (dfb, m_filename, &provider));
121 DFBCHECK (provider->GetImageDescription (provider, &m_dsc));
122
123 /*
124 * Get a surface description from the provider. It will contain the width,
125 * height, bits per pixel and the flag for an alphachannel if the image has
126 * one. If the image has no alphachannel the bits per pixel is set to the
127 * bits per pixel of the primary layer to use simple blitting without pixel
128 * format conversion.
129 */
130 DFBCHECK (provider->GetSurfaceDescription (provider, &dsc));
131
132 /*
133 * Create a surface based on the description of the provider.
134 */
135 DFBCHECK (dfb->CreateSurface( dfb, &dsc, &logo ));
136
137 /*
138 * Let the provider render to our surface. Image providers are supposed to
139 * support every destination pixel format and size. If the size differs the
140 * image will be scaled (bilinear). The last parameter allows to specify
141 * an optional destination rectangle. We use NULL here so that our image
142 * covers the whole logo surface.
143 */
144 DFBCHECK (provider->RenderTo (provider, logo, NULL));
145
146 /*
147 * Release the provider, we don't need it anymore.
148 */
149 provider->Release (provider);
150
151 if (m_dsc.caps & DICAPS_ALPHACHANNEL)
152 m_flags |= DSBLIT_BLEND_ALPHACHANNEL;
153 if (m_dsc.caps & DICAPS_COLORKEY) {
154 m_flags |= DSBLIT_SRC_COLORKEY;
155 DFBCHECK (logo->SetSrcColorKey(logo, m_dsc.colorkey_r, m_dsc.colorkey_g, m_dsc.colorkey_b));
156 }
157 DFBCHECK (logo->SetBlittingFlags(logo, m_flags));
158 DFBCHECK (primary->SetBlittingFlags(primary, m_flags));
159 /*
160 * We want to let the logo slide in on the left and slide out on the right.
161 */
162 for (i = -dsc.width; i < screen_width; i++)
163 {
164 /*
165 * Clear the screen.
166 */
167 DFBCHECK (primary->Clear(primary, 0,0,0,0));
168 //DFBCHECK (primary->FillRectangle (primary, 0, 0, screen_width, screen_height));
169
170 /*
171 * Blit the logo vertically centered with "i" as the X coordinate.
172 * NULL means that we want to blit the whole surface.
173 */
174 DFBCHECK (primary->Blit (primary, logo, NULL, i, (screen_height - dsc.height) / 2));
175
176 /*
177 * Flip the front and back buffer, but wait for the vertical retrace to
178 * avoid tearing.
179 */
180 DFBCHECK (primary->Flip (primary, NULL, DSFLIP_WAITFORSYNC));
181 }
182
183 /*
184 * Release the image.
185 */
186 logo->Release (logo);
187
188 /*
189 * (Release)
190 */
191 primary->Release (primary);
192 dfb->Release (dfb);
193
194 return 23;
195}
196