PageRenderTime 59ms CodeModel.GetById 25ms app.highlight 29ms RepoModel.GetById 1ms app.codeStats 0ms

/src/ftk_sprite.c

http://ftk.googlecode.com/
C | 247 lines | 171 code | 47 blank | 29 comment | 37 complexity | d24be2eee998df4e7fa59a812ff7af4e MD5 | raw file
  1/*
  2 * File:    ftk_sprite.c
  3 * Author:  Li XianJing <xianjimli@hotmail.com>
  4 * Brief:   screen sprite
  5 *
  6 * Copyright (c) 2009 - 2010  Li XianJing <xianjimli@hotmail.com>
  7 *
  8 * Licensed under the Academic Free License version 2.1
  9 *
 10 * This program is free software; you can redistribute it and/or modify
 11 * it under the terms of the GNU General Public License as published by
 12 * the Free Software Foundation; either version 2 of the License, or
 13 * (at your option) any later version.
 14 *
 15 * This program is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 18 * GNU General Public License for more details.
 19 *
 20 * You should have received a copy of the GNU General Public License
 21 * along with this program; if not, write to the Free Software
 22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 23 */
 24
 25/*
 26 * History:
 27 * ================================================================
 28 * 2009-11-21 Li XianJing <xianjimli@hotmail.com> created
 29 *
 30 */
 31
 32#include "ftk_log.h"
 33#include "ftk_sprite.h"
 34#include "ftk_globals.h"
 35
 36struct _FtkSprite
 37{
 38	int x;
 39	int y;
 40	int visible;
 41	FtkBitmap* icon;
 42	FtkBitmap* snap;
 43	void* listener_ctx;
 44	FtkListener listener;
 45};
 46
 47FtkSprite* ftk_sprite_create(void)
 48{
 49	FtkSprite* thiz = FTK_NEW(FtkSprite);
 50	
 51	return thiz;
 52}
 53
 54static Ret  ftk_sprite_backup(FtkSprite* thiz)
 55{
 56	FtkRect rect = {0};
 57	FtkDisplay* display = ftk_default_display();
 58	return_val_if_fail(thiz != NULL, RET_FAIL);
 59	return_val_if_fail(thiz->snap != NULL, RET_FAIL);
 60	
 61	rect.x = thiz->x;
 62	rect.y = thiz->y;
 63	rect.width = ftk_bitmap_width(thiz->snap);
 64	rect.height = ftk_bitmap_height(thiz->snap);
 65
 66	return ftk_display_snap(display, &rect, thiz->snap);
 67}
 68
 69static Ret  ftk_sprite_restore(FtkSprite* thiz)
 70{
 71	FtkRect rect = {0};
 72	FtkDisplay* display = ftk_default_display();
 73	return_val_if_fail(thiz != NULL, RET_FAIL);
 74	return_val_if_fail(thiz->snap != NULL, RET_FAIL);
 75	
 76	rect.width = ftk_bitmap_width(thiz->snap);
 77	rect.height = ftk_bitmap_height(thiz->snap);
 78
 79	return ftk_display_update(display, thiz->snap, &rect, thiz->x, thiz->y);
 80}
 81
 82static Ret  ftk_sprite_paint(FtkSprite* thiz, int notify)
 83{
 84	Ret ret = RET_OK;
 85	FtkRect rect = {0};
 86	FtkDisplay* display = ftk_default_display();
 87	return_val_if_fail(thiz != NULL, RET_FAIL);
 88	return_val_if_fail(thiz->icon != NULL, RET_FAIL);
 89	
 90	rect.width = ftk_bitmap_width(thiz->icon);
 91	rect.height = ftk_bitmap_height(thiz->icon);
 92
 93	if(notify)
 94	{
 95		ret = ftk_display_update_and_notify(display, thiz->icon, &rect, thiz->x, thiz->y);
 96	}
 97	else
 98	{
 99		ret = ftk_display_update(display, thiz->icon, &rect, thiz->x, thiz->y);
100	}
101
102	return ret;
103}
104
105Ret  ftk_sprite_set_icon(FtkSprite* thiz, FtkBitmap* icon)
106{
107	Ret ret = RET_OK;
108	FtkColor bg = {0};
109	return_val_if_fail(thiz != NULL && icon != NULL, RET_FAIL);
110	
111	bg.a = 0xff;
112	thiz->icon = icon;
113	if(thiz->snap != NULL)
114	{
115		ftk_bitmap_unref(thiz->snap);
116	}
117	thiz->snap = ftk_bitmap_create(ftk_bitmap_width(icon), ftk_bitmap_height(icon), bg);
118
119	if(thiz->visible)
120	{
121		ftk_sprite_restore(thiz);
122		ret = ftk_sprite_paint(thiz, 1);
123	}
124
125	return ret;
126}
127
128Ret ftk_sprite_on_display_update(void* ctx, FtkDisplay* display, int before,
129	FtkBitmap* bitmap, FtkRect* rect, int xoffset, int yoffset)
130{
131	FtkSprite* thiz = (FtkSprite*)ctx;
132	return_val_if_fail(thiz != NULL && bitmap != NULL, RET_FAIL);
133
134	if(bitmap == thiz->icon || bitmap == thiz->snap)
135	{
136		return RET_OK;
137	}
138
139	if(before)
140	{
141		ftk_sprite_restore(thiz);
142	}
143	else
144	{
145		ftk_sprite_backup(thiz);
146		ftk_sprite_paint(thiz, 0);
147	}
148
149	return RET_OK;
150}
151
152Ret  ftk_sprite_show(FtkSprite* thiz, int show)
153{
154	Ret ret = RET_FAIL;
155	return_val_if_fail(thiz != NULL, RET_FAIL);
156	return_val_if_fail(thiz->visible != show, RET_FAIL);
157
158	thiz->visible = show;
159	
160	if(thiz->visible)
161	{
162		ftk_sprite_backup(thiz);
163		ret = ftk_sprite_paint(thiz, 1);
164		ftk_display_reg_update_listener(ftk_default_display(), ftk_sprite_on_display_update, thiz);
165	}
166	else
167	{
168		ftk_display_unreg_update_listener(ftk_default_display(), ftk_sprite_on_display_update, thiz);
169		ret = ftk_sprite_restore(thiz);
170	}
171
172	return ret;
173}
174
175int  ftk_sprite_is_visible(FtkSprite* thiz)
176{
177	return_val_if_fail(thiz != NULL, 0);
178
179	return thiz->visible;
180}
181
182Ret  ftk_sprite_move(FtkSprite* thiz, int x, int y)
183{
184	Ret ret = RET_FAIL;
185	return_val_if_fail(thiz != NULL, RET_FAIL);
186	
187	if(thiz->x == x && thiz->y == y)
188	{
189		return RET_OK;
190	}
191
192	if(thiz->visible)
193	{
194		ftk_sprite_restore(thiz);	
195	}
196
197	thiz->x = x;
198	thiz->y = y;
199	
200	if(thiz->visible)
201	{
202		ftk_sprite_backup(thiz);
203		ret = ftk_sprite_paint(thiz, 1);
204	}
205
206	FTK_CALL_LISTENER(thiz->listener, thiz->listener_ctx, thiz);
207
208	return ret;
209}
210
211int  ftk_sprite_get_x(FtkSprite* thiz)
212{
213	return_val_if_fail(thiz != NULL, 0);
214
215	return thiz->x;
216}
217
218int  ftk_sprite_get_y(FtkSprite* thiz)
219{
220	return_val_if_fail(thiz != NULL, 0);
221
222	return thiz->y;
223}
224
225Ret  ftk_sprite_set_move_listener(FtkSprite* thiz, FtkListener listener, void* ctx)
226{
227	return_val_if_fail(thiz != NULL, RET_FAIL);
228
229	thiz->listener = listener;
230	thiz->listener_ctx = ctx;
231
232	return RET_OK;
233}
234
235void ftk_sprite_destroy(FtkSprite* thiz)
236{
237	if(thiz != NULL)
238	{
239		ftk_bitmap_unref(thiz->icon);
240		ftk_bitmap_unref(thiz->snap);
241		FTK_ZFREE(thiz, sizeof(*thiz));
242	}
243
244	return;
245}
246
247