1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "ultima/nuvie/misc/sdl_compat.h"
24 #include "ultima/nuvie/core/events.h"
25 #include "common/system.h"
26 #include "common/events.h"
27 #include "common/file.h"
28 #include "common/textconsole.h"
29 #include "graphics/managed_surface.h"
30 #include "image/bmp.h"
31 
32 namespace Ultima {
33 namespace Nuvie {
34 
SDL_GetTicks()35 uint32 SDL_GetTicks() {
36 	return g_system->getMillis();
37 }
38 
SDL_FreeSurface(Graphics::ManagedSurface * & s)39 void SDL_FreeSurface(Graphics::ManagedSurface *&s) {
40 	delete s;
41 	s = nullptr;
42 }
43 
SDL_ShowCursor(bool show)44 void SDL_ShowCursor(bool show) {
45 	g_system->showMouse(show);
46 }
47 
SDL_MapRGB(Graphics::PixelFormat & format,byte r,byte g,byte b)48 uint32 SDL_MapRGB(Graphics::PixelFormat &format, byte r, byte g, byte b) {
49 	return format.RGBToColor(r, g, b);
50 }
51 
SDL_BlitSurface(const Graphics::ManagedSurface * src,const Common::Rect * srcrect,Graphics::ManagedSurface * dst,Common::Rect * dstrect)52 int SDL_BlitSurface(const Graphics::ManagedSurface *src, const Common::Rect *srcrect,
53 		Graphics::ManagedSurface *dst, Common::Rect *dstrect) {
54 	Common::Rect srcRect = srcrect ? *srcrect : Common::Rect(0, 0, src->w, src->h);
55 	Common::Point destPos = dstrect ? Common::Point(dstrect->left, dstrect->top) : Common::Point();
56 
57 	dst->transBlitFrom(*src, srcRect, destPos, (uint)-1);
58 
59 	if (dstrect) {
60 		dstrect->setWidth(srcRect.width());
61 		dstrect->setHeight(srcRect.height());
62 	}
63 
64 	return 0;
65 }
66 
SDL_FillRect(Graphics::ManagedSurface * surf,Common::Rect * rect,uint color)67 int SDL_FillRect(Graphics::ManagedSurface *surf, Common::Rect *rect, uint color) {
68 	surf->fillRect(rect ? *rect : Common::Rect(0, 0, surf->w, surf->h), color);
69 	return 0;
70 }
71 
SDL_UpdateRect(Graphics::ManagedSurface * surf,int x,int y,int w,int h)72 void SDL_UpdateRect(Graphics::ManagedSurface *surf, int x, int y, int w, int h) {
73 	Common::Rect r(x, y, x + w, y + h);
74 	if (r.isEmpty())
75 		r = Common::Rect(0, 0, surf->w, surf->h);
76 
77 	g_system->copyRectToScreen(surf->getPixels(), surf->pitch, r.left, r.top, r.width(), r.height());
78 }
79 
SDL_UpdateRects(Graphics::ManagedSurface * surf,int count,Common::Rect * rects)80 void SDL_UpdateRects(Graphics::ManagedSurface *surf, int count, Common::Rect *rects) {
81 	while (count-- > 0)
82 		g_system->copyRectToScreen(surf->getPixels(), surf->pitch, rects->left, rects->top,
83 			rects->width(), rects->height());
84 }
85 
SDL_LoadBMP(const char * filename)86 Graphics::ManagedSurface *SDL_LoadBMP(const char *filename) {
87 	Common::File f;
88 	Image::BitmapDecoder decoder;
89 
90 	if (!f.open(filename))
91 		error("Could not open file - %s", filename);
92 	if (!decoder.loadStream(f))
93 		error("Could not load bitmap - %s", filename);
94 
95 	const Graphics::Surface *src = decoder.getSurface();
96 	Graphics::ManagedSurface *dest = new Graphics::ManagedSurface(src->w, src->h, src->format);
97 	dest->blitFrom(*src);
98 
99 	return dest;
100 }
101 
SDL_SetColorKey(Graphics::ManagedSurface * surface,int flag,uint32 key)102 int SDL_SetColorKey(Graphics::ManagedSurface *surface, int flag, uint32 key) {
103 	if (flag)
104 		surface->setTransparentColor(key);
105 	else
106 		surface->clearTransparentColor();
107 
108 	return 0;
109 }
110 
SDL_SetColors(Graphics::ManagedSurface * surface,const SDL_Color * colors,int firstcolor,int ncolors)111 int SDL_SetColors(Graphics::ManagedSurface *surface, const SDL_Color *colors, int firstcolor, int ncolors) {
112 	surface->setPalette(colors, firstcolor, ncolors);
113 	return 0;
114 }
115 
SDL_WaitEvent(Common::Event * event)116 int SDL_WaitEvent(Common::Event *event) {
117 	while (!Events::get()->pollEvent(*event))
118 		g_system->delayMillis(5);
119 	return 0;
120 }
121 
SDL_PollEvent(Common::Event * event)122 int SDL_PollEvent(Common::Event *event) {
123 	return Events::get()->pollEvent(*event);
124 }
125 
SDL_LockSurface(Graphics::ManagedSurface * surface)126 int SDL_LockSurface(Graphics::ManagedSurface *surface) {
127 	return 0;
128 }
129 
SDL_UnlockSurface(Graphics::ManagedSurface * surface)130 int SDL_UnlockSurface(Graphics::ManagedSurface *surface) {
131 	return 0;
132 }
133 
SDL_ConvertSurface(Graphics::ManagedSurface * src,const Graphics::PixelFormat & fmt,uint32 flags)134 Graphics::ManagedSurface *SDL_ConvertSurface(Graphics::ManagedSurface *src,
135 		const Graphics::PixelFormat &fmt, uint32 flags) {
136 	Graphics::ManagedSurface *dest = new Graphics::ManagedSurface(src->w, src->h, fmt);
137 	dest->blitFrom(*src);
138 
139 	return dest;
140 }
141 
142 } // End of namespace Nuvie
143 } // End of namespace Ultima
144