2 * (C) GraÅžvydas "notaz" Ignotas, 2010
4 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
5 * See the COPYING file in the top-level directory.
16 static struct SDL_PrivateVideoData state;
17 static SDL_Surface *g_screen;
18 static void *g_screen_fbp;
19 static Uint16 g_8bpp_pal[256];
20 static Uint32 g_start_ticks;
22 static inline int min(int v1, int v2)
24 return v1 < v2 ? v1 : v2;
27 static SDL_Surface *alloc_surface(int w, int h, int bpp)
29 // SDL has some pointer overuse, every surface has a format,
30 // why make format accessible through pointer?
36 ret = calloc(1, sizeof(*ret));
40 ret->s.format = &ret->f;
41 ret->f.BitsPerPixel = bpp;
42 ret->f.BytesPerPixel = (bpp + 7) / 8;
46 ret->s.pitch = w * ret->f.BytesPerPixel;
53 SDL_Init(Uint32 flags)
57 if (g_start_ticks == 0) {
63 g_start_ticks = SDL_GetTicks();
73 osdl_video_finish(&state);
77 SDL_InitSubSystem(Uint32 flags)
84 SDL_QuitSubSystem(Uint32 flags)
89 DECLSPEC Uint32 SDLCALL
95 gettimeofday(&tv, NULL);
96 ret = tv.tv_sec * 1000;
97 ret += tv.tv_usec * 1048 >> 20;
100 dbg(" SDL_GetTicks %d", ret);
104 DECLSPEC SDL_Surface * SDLCALL
105 SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags)
109 trace("%d, %d, %d, %08x", width, height, bpp, flags);
114 if (bpp != 8 && bpp != 16) {
115 err("unsupported bpp: %d\n", bpp);
119 // XXX: destroy old g_screen?
121 g_screen = alloc_surface(width, height, bpp);
122 if (g_screen == NULL)
125 ret = osdl_video_set_mode(&state, width, height, 16);
127 goto fail_fbdev_init;
129 g_screen_fbp = osdl_video_flip(&state);
131 g_screen->pixels = g_screen_fbp;
133 // we have to fake this for now..
134 g_screen->pixels = calloc(g_screen->pitch * height, 1);
136 if (g_screen->pixels == NULL) {
141 dbg("returning %p", g_screen);
145 osdl_video_finish(&state);
153 SDL_Flip(SDL_Surface *screen)
157 if (screen != g_screen) {
158 err("flip not on screen surface?");
162 if (screen->format->BitsPerPixel == 8) {
163 int l = screen->pitch * screen->h;
165 do_clut(g_screen_fbp, screen->pixels, g_8bpp_pal, l);
167 Uint16 *d = g_screen_fbp;
168 Uint8 *s = screen->pixels;
170 // XXX: perhaps optimize this sh*t
171 for (; l > 0; d++, s++, l--)
176 g_screen_fbp = osdl_video_flip(&state);
178 if (screen->format->BitsPerPixel != 8)
179 screen->pixels = g_screen_fbp;
184 static int do_rect_clip(const SDL_Surface *s, SDL_Rect *r)
186 int x = r->x, y = r->y, w = r->w, h = r->h;
211 r->x = x; r->y = y; r->w = w; r->h = h;
212 return (w > 0 && h > 0) ? 0 : -1;
216 SDL_UpperBlit(SDL_Surface *src, SDL_Rect *srcrect,
217 SDL_Surface *dst, SDL_Rect *dstrect)
219 int sx = 0, sy = 0, sw, sh;
221 SDL_Rect tmprect = { 0, };
223 trace("%p, %p, %p, %p", src, srcrect, dst, dstrect);
225 if (src == NULL || dst == NULL)
228 if (srcrect != NULL) {
229 // XXX: dst pos may need to be adjusted in some corner cases
230 if (do_rect_clip(src, srcrect) < 0)
244 // SDL just uses source w and h
247 if (do_rect_clip(dst, dstrect) < 0)
254 dbg("(%d %d %d %d) -> (%d %d %d %d)", sx, sy, sw, sh, dx, dy, sw, sh);
256 if (src->format->BitsPerPixel == dst->format->BitsPerPixel) {
257 int Bpp = src->format->BytesPerPixel;
258 int dpitch = dst->pitch;
259 int spitch = src->pitch;
260 Uint8 *d = (Uint8 *)dst->pixels + dpitch * dy + dx * Bpp;
261 Uint8 *s = (Uint8 *)src->pixels + spitch * sy + sx * Bpp;
263 for (sw *= Bpp; sh > 0; d += dpitch, s += spitch, sh--)
272 DECLSPEC SDL_Surface * SDLCALL
273 SDL_CreateRGBSurface(Uint32 flags, int width, int height, int depth,
274 Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
278 trace("%08x, %d, %d, %d, %04x, %04x, %04x, %04x",
279 flags, width, height, depth, Rmask, Gmask, Bmask, Amask);
281 ret = alloc_surface(width, height, depth);
285 ret->pixels = calloc(ret->pitch * height, 1);
286 if (ret->pixels == NULL)
289 dbg("returning %p", ret);
298 SDL_SetColors(SDL_Surface *surface, SDL_Color *colors, int firstcolor, int ncolors)
302 trace("%p, %p, %d, %d", surface, colors, firstcolor, ncolors);
304 if (surface != g_screen)
307 to = min(ARRAY_SIZE(g_8bpp_pal), firstcolor + ncolors);
308 for (i = firstcolor; i < to; i++) {
309 SDL_Color *c = &colors[i - firstcolor];
310 int r = c->r, g = c->g, b = c->b;
311 // noiz2sa gets a good deal darker that real SDL,
312 // don't know what's going on there
314 if (r < 0xf8 && (r & 7) > 3) r += 8;
315 if (g < 0xfc && (g & 3) > 1) g += 4;
316 if (b < 0xf8 && (b & 7) > 3) b += 8;
318 g_8bpp_pal[i] = ((r << 8) & 0xf800) | ((g << 3) & 0x07e0) | (b >> 3);
319 // g_8bpp_pal[i] += 0x0821;
326 SDL_FillRect(SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color)
328 trace("%p, %p, %04x", dst, dstrect, color);
330 if (dst->format->BytesPerPixel != 1) {
335 memset(dst->pixels, color, dst->pitch * dst->h);
341 DECLSPEC SDL_Surface * SDLCALL
342 SDL_CreateRGBSurfaceFrom(void *pixels, int width, int height, int depth, int pitch,
343 Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
348 DECLSPEC SDL_RWops * SDLCALL
349 SDL_RWFromFile(const char *file, const char *mode)
354 trace("%s, %s", file, mode);
356 f = fopen(file, mode);
360 ret = calloc(1, sizeof(*ret));
365 ret->hidden.stdio.fp = f;
370 DECLSPEC void SDLCALL
371 SDL_FreeRW(SDL_RWops *area)
373 if (area->hidden.stdio.fp) {
374 fclose(area->hidden.stdio.fp);
375 area->hidden.stdio.fp = NULL;
380 DECLSPEC SDL_Surface * SDLCALL
381 SDL_LoadBMP_RW(SDL_RWops *src, int freesrc)
384 unsigned char magic[2];
394 uint32_t compress_type;
400 } __attribute__((packed)) bmp;
401 SDL_Surface *ret = NULL;
402 int data_size, read_size;
403 uint8_t *tmp_buf = NULL;
407 trace("%p, %d", src, freesrc);
409 f = src->hidden.stdio.fp;
413 if (fread(&bmp, 1, sizeof(bmp), f) != sizeof(bmp)) {
414 err("bmp read error");
418 if (bmp.magic[0] != 'B' || bmp.magic[1] != 'M' || bmp.header_sz != 40) {
419 err("unhandled BMP format, sz=%d", bmp.header_sz);
423 if ((unsigned)bmp.width > 0xffff || (unsigned)bmp.height > 0xffff) {
424 err("BMP size %dx%d out of range", bmp.width, bmp.height);
428 if (bmp.bitspp != 4) {
429 err("BMP unhandled bpp: %d", bmp.bitspp);
433 bytespp = bmp.bitspp < 8 ? 1 : (bmp.bitspp + 7) / 8;
434 data_size = bmp.width * bmp.height * bytespp;
435 tmp_buf = malloc(data_size);
439 if (fseek(f, bmp.bmp_offset, SEEK_SET) != 0) {
440 err("BMP seek error");
444 read_size = bmp.width * bmp.bitspp / 8;
445 for (i = bmp.height - 1; i > 0; i--) {
446 if (fread((char *)tmp_buf + i * read_size, 1, read_size, f) != read_size) {
447 err("BMP read error");
452 if (bmp.bitspp == 4) {
453 // just convert to 8bpp now
454 int c = bmp.width * bmp.height / 2;
455 uint8_t *p = tmp_buf;
456 for (i = c - 1; i >= 0; i--) {
457 p[i * 2 + 1] = p[i] & 0xf;
458 p[i * 2] = p[i] >> 4;
462 ret = alloc_surface(bmp.width, bmp.height, 8);
466 ret->pixels = tmp_buf;
478 DECLSPEC SDL_Surface * SDLCALL
479 SDL_ConvertSurface(SDL_Surface *src, SDL_PixelFormat *fmt, Uint32 flags)
484 trace("%p, %p, %08x", src, fmt, flags);
486 if (src->format->BitsPerPixel != fmt->BitsPerPixel) {
487 err("convert: not implemented");
491 data = malloc(src->pitch * src->h);
494 memcpy(data, src->pixels, src->pitch * src->h);
496 ret = alloc_surface(src->w, src->h, fmt->BitsPerPixel);
506 SDL_SetColorKey(SDL_Surface *surface, Uint32 flag, Uint32 key)
508 trace("%p, %08x, %04x", surface, flag, key);
512 DECLSPEC void SDLCALL
513 SDL_WM_SetCaption(const char *title, const char *icon)
515 trace("%s, %s", title, icon);
519 SDL_ShowCursor(int toggle)
525 DECLSPEC char * SDLCALL