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 "titanic/support/direct_draw_surface.h"
24 
25 namespace Titanic {
26 
DirectDrawSurface()27 DirectDrawSurface::DirectDrawSurface() : _surface(nullptr),
28 		_disposeAfterUse(DisposeAfterUse::YES) {
29 }
30 
~DirectDrawSurface()31 DirectDrawSurface::~DirectDrawSurface() {
32 	free();
33 }
34 
create(Graphics::ManagedSurface * surface)35 void DirectDrawSurface::create(Graphics::ManagedSurface *surface) {
36 	free();
37 	_surface = surface;
38 	_disposeAfterUse = DisposeAfterUse::NO;
39 }
40 
create(int w,int h,int bpp)41 void DirectDrawSurface::create(int w, int h, int bpp) {
42 	assert(bpp == 16 || bpp == 32);
43 	Graphics::PixelFormat pixelFormat = (bpp == 32) ?
44 		Graphics::PixelFormat(4, 8, 8, 8, 8, 24, 16, 8, 0) :
45 		Graphics::PixelFormat(2, 5, 6, 5, 0, 11, 5, 0, 0);
46 
47 	_surface = new Graphics::ManagedSurface(w, h, pixelFormat);
48 	_disposeAfterUse = DisposeAfterUse::YES;
49 }
50 
free()51 void DirectDrawSurface::free() {
52 	if (_disposeAfterUse == DisposeAfterUse::YES)
53 		delete _surface;
54 	_surface = nullptr;
55 	_disposeAfterUse = DisposeAfterUse::NO;
56 }
57 
lock(const Rect * bounds,int flags)58 Graphics::ManagedSurface *DirectDrawSurface::lock(const Rect *bounds, int flags) {
59 	assert(!_surface->empty());
60 	return _surface;
61 }
62 
unlock()63 void DirectDrawSurface::unlock() {
64 	assert(_surface->w != 0 && _surface->h != 0);
65 }
66 
fill(const Rect * bounds,uint32 color)67 void DirectDrawSurface::fill(const Rect *bounds, uint32 color) {
68 	Rect tempBounds;
69 
70 	assert(_surface);
71 	if (bounds) {
72 		// Bounds are provided, clip them to the bounds of this surface
73 		tempBounds = *bounds;
74 		tempBounds.clip(Rect(0, 0, _surface->w, _surface->h));
75 	} else {
76 		// No bounds provided, so use the entire surface
77 		tempBounds = Rect(0, 0, _surface->w, _surface->h);
78 	}
79 
80 	// Fill the area
81 	_surface->fillRect(tempBounds, color);
82 }
83 
fillRect(Rect * rect,byte r,byte g,byte b)84 void DirectDrawSurface::fillRect(Rect *rect, byte r, byte g, byte b) {
85 	uint color = _surface->format.RGBToColor(r, g, b);
86 	Rect tempRect = rect ? *rect : Rect(0, 0, getWidth(), getHeight());
87 
88 	_surface->fillRect(tempRect, color);
89 }
90 
blit(const Rect & destRect,DirectDrawSurface * srcSurface,Rect & srcRect)91 void DirectDrawSurface::blit(const Rect &destRect, DirectDrawSurface *srcSurface, Rect &srcRect) {
92 	assert(srcSurface);
93 	if (!destRect.isEmpty())
94 		_surface->transBlitFrom(*srcSurface->_surface, srcRect, destRect, (uint)-1);
95 }
96 
blit(const Point & destPos,DirectDrawSurface * srcSurface,Rect * bounds)97 void DirectDrawSurface::blit(const Point &destPos, DirectDrawSurface *srcSurface, Rect *bounds) {
98 	if (bounds)
99 		_surface->blitFrom(*srcSurface->_surface, *bounds, destPos);
100 	else
101 		_surface->blitFrom(*srcSurface->_surface, destPos);
102 }
103 
104 } // End of namespace Titanic
105