PageRenderTime 31ms CodeModel.GetById 18ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/src/backend/sdl/ftk_display_sdl.c

http://ftk.googlecode.com/
C | 180 lines | 147 code | 30 blank | 3 comment | 16 complexity | c49a0d414fa8067c4dbd52b7b9881c59 MD5 | raw file
  1
  2#include "ftk_typedef.h"
  3#include "ftk.h"
  4#include "ftk_event.h"
  5#include "ftk_globals.h"
  6#include "ftk_display_rotate.h"
  7#include "ftk_display_sdl.h"
  8#include <SDL.h>
  9
 10#ifdef WEBOS
 11#include <PDL.h>
 12#endif
 13
 14typedef struct _PrivInfo
 15{
 16	SDL_Surface* screen;
 17	SDL_Surface* surface;
 18	int width;
 19	int height;
 20	FtkEvent event;
 21}PrivInfo;
 22
 23static Ret ftk_display_sdl_update(FtkDisplay* thiz, FtkBitmap* bitmap, FtkRect* rect, int xoffset, int yoffset)
 24{
 25	SDL_Rect paint_rect;
 26	DECL_PRIV(thiz, priv);
 27
 28#if 1
 29	SDL_LockSurface(priv->surface);
 30	ftk_bitmap_copy_to_data_rgba32(bitmap, rect, priv->surface->pixels, xoffset, yoffset, priv->width, priv->height);
 31	SDL_UnlockSurface(priv->surface);
 32
 33#if 0
 34	paint_rect.x = xoffset;
 35	paint_rect.y = xoffset;
 36	paint_rect.w = (xoffset + rect->width) >= priv->width ? priv->width - xoffset : rect->width;
 37	paint_rect.h = (yoffset + rect->height) >= priv->height ? priv->height - yoffset : rect->height;
 38#else
 39	paint_rect.x = 0;
 40	paint_rect.y = 0;
 41	paint_rect.w = priv->width;
 42	paint_rect.h = priv->height;
 43#endif
 44
 45	SDL_BlitSurface(priv->surface, &paint_rect, priv->screen, &paint_rect);
 46
 47	if((priv->screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF)
 48	{
 49		SDL_Flip(priv->screen);
 50	}
 51	else
 52	{
 53		SDL_UpdateRects(priv->screen, 1, &paint_rect);
 54	}
 55#else
 56	SDL_GL_Lock();
 57	ftk_bitmap_copy_to_data_rgba32(bitmap, rect, priv->surface->pixels, xoffset, yoffset, priv->width, priv->height);
 58	SDL_GL_Unlock();
 59
 60	paint_rect.x = 0;
 61	paint_rect.y = 0;
 62	paint_rect.w = priv->width;
 63	paint_rect.h = priv->height;
 64
 65	SDL_BlitSurface(priv->surface, &paint_rect, priv->screen, &paint_rect);
 66
 67	if((priv->screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF)
 68	{
 69		SDL_GL_SwapBuffers();
 70	}
 71	else
 72	{
 73		SDL_GL_UpdateRects(1, &paint_rect);
 74	}
 75#endif
 76
 77	return RET_OK;
 78}
 79
 80static Ret ftk_display_sdl_snap(FtkDisplay* thiz, FtkRect* r, FtkBitmap* bitmap)
 81{
 82#if 0
 83	int i = 0;
 84	int xoffset = r->x;
 85	int yoffset = r->y;
 86	int width = r->width;
 87	int height = r->height;
 88	DECL_PRIV(thiz, priv);
 89	FtkColor* dst = ftk_bitmap_lock(bitmap);
 90	FtkColor* src = ftk_bitmap_lock(priv->bitmap);
 91
 92	/*TODO*/
 93#endif
 94
 95	return RET_OK;
 96}
 97
 98static int ftk_display_sdl_width(FtkDisplay* thiz)
 99{
100	DECL_PRIV(thiz, priv);
101	return_val_if_fail(priv != NULL, 0);
102	return priv->width;
103}
104
105static int ftk_display_sdl_height(FtkDisplay* thiz)
106{
107	DECL_PRIV(thiz, priv);
108	return_val_if_fail(priv != NULL, 0);
109	return priv->height;
110}
111
112static void ftk_display_sdl_destroy(FtkDisplay* thiz)
113{
114	if(thiz != NULL)
115	{
116		DECL_PRIV(thiz, priv);
117		SDL_FreeSurface(priv->surface);
118		FTK_ZFREE(thiz, sizeof(FtkDisplay) + sizeof(PrivInfo));
119
120#ifdef WEBOS
121		PDL_Quit();
122#endif
123		SDL_Quit();
124	}
125}
126
127FtkDisplay* ftk_display_sdl_create(void)
128{
129	SDL_VideoInfo *vi;
130	FtkDisplay* thiz;
131
132	thiz = (FtkDisplay*)FTK_ZALLOC(sizeof(FtkDisplay) + sizeof(PrivInfo));
133	if(thiz != NULL)
134	{
135		DECL_PRIV(thiz, priv);
136		thiz->update = ftk_display_sdl_update;
137		thiz->width = ftk_display_sdl_width;
138		thiz->height = ftk_display_sdl_height;
139		thiz->snap = ftk_display_sdl_snap;
140		thiz->destroy = ftk_display_sdl_destroy;
141
142		priv->width = 320;
143		priv->height = 480;
144
145		ftk_logd("%d-%d\n", priv->width, priv->height);
146
147		SDL_Init(SDL_INIT_VIDEO/*|SDL_INIT_NOPARACHUTE*/);
148#ifdef WEBOS
149		PDL_Init(0);
150#endif
151
152		/* SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); */
153
154		vi = (SDL_VideoInfo*)SDL_GetVideoInfo();
155		if(vi == NULL)
156		{
157			ftk_loge("SDL_GetVideoInfo() failed\n");
158			return NULL;
159		}
160
161		ftk_logd("bpp: %d\n", vi->vfmt->BitsPerPixel);
162
163		priv->screen = SDL_SetVideoMode(priv->width, priv->height, vi->vfmt->BitsPerPixel, SDL_HWSURFACE/*SDL_OPENGL*/);
164		if(priv->screen == NULL)
165		{
166			ftk_loge("SDL_SetVideoMode() failed\n");
167			/* TODO: cleanup */
168			return NULL;
169		}
170
171		priv->surface = SDL_CreateRGBSurface(SDL_SWSURFACE, priv->width, priv->height, 32, 0, 0, 0, 0);
172		if(priv->surface == NULL)
173		{
174			ftk_loge("SDL_CreateRGBSurface() failed\n");
175			return NULL;
176		}
177	}
178
179	return thiz;
180}