1 /*
2 	This file is part of Hikari, a library that allows developers
3 	to use Flash in their Ogre3D applications.
4 
5 	Copyright (C) 2008 Adam J. Simmons
6 
7 	This library is free software; you can redistribute it and/or
8 	modify it under the terms of the GNU Lesser General Public
9 	License as published by the Free Software Foundation; either
10 	version 2.1 of the License, or (at your option) any later version.
11 
12 	This library 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 GNU
15 	Lesser General Public License for more details.
16 
17 	You should have received a copy of the GNU Lesser General Public
18 	License along with this library; if not, write to the Free Software
19 	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20 */
21 
22 #include "RenderBuffer.h"
23 
24 using namespace Hikari;
25 
RenderBuffer(int width,int height)26 RenderBuffer::RenderBuffer(int width, int height) : width(0), height(0), buffer(0), rowSpan(0)
27 {
28 	reserve(width, height);
29 }
30 
~RenderBuffer()31 RenderBuffer::~RenderBuffer()
32 {
33 	if (buffer)
34 	{
35 		delete [] buffer;
36 		buffer = 0;
37 	}
38 }
39 
reserve(int width,int height)40 void RenderBuffer::reserve(int width, int height)
41 {
42 	if (this->width != width || this->height != height)
43 	{
44 		this->width = width;
45 		this->height = height;
46 
47 		rowSpan = width * 4;
48 
49 		if (buffer)
50 		{
51 			delete [] buffer;
52 			buffer = 0;
53 		}
54 
55 		buffer = new unsigned char[width * height * 4];
56 		memset(buffer, 255, width * height * 4);
57 	}
58 }
59 
copyFrom(unsigned char * srcBuffer,int srcRowSpan)60 void RenderBuffer::copyFrom(unsigned char* srcBuffer, int srcRowSpan)
61 {
62 	for (int row = 0; row < height; row++)
63 		memcpy(buffer + row * rowSpan, srcBuffer + row * srcRowSpan, rowSpan);
64 }
65 
copyArea(RECT srcRect,unsigned char * srcBuffer,int srcRowSpan)66 void RenderBuffer::copyArea(RECT srcRect, unsigned char* srcBuffer, int srcRowSpan)
67 {
68 	if (srcRect.right <= width && srcRect.bottom <= height)
69 	{
70 		int srcWidth = srcRect.right - srcRect.left;
71 		int srcHeight = srcRect.bottom - srcRect.top;
72 
73 		for (int row = 0; row < srcHeight; row++)
74 			memcpy(buffer + (row + srcRect.top) * rowSpan + (srcRect.left * 4), srcBuffer + row * srcRowSpan, srcWidth * 4);
75 	}
76 }
77 
blitBGR(unsigned char * destBuffer,int destRowSpan,int destDepth)78 void RenderBuffer::blitBGR(unsigned char* destBuffer, int destRowSpan, int destDepth)
79 {
80 	if (destDepth == 3)
81 	{
82 		for (int row = 0; row < height; row++)
83 			for (int col = 0; col < width; col++)
84 				memcpy(destBuffer + row * destRowSpan + col * 3, buffer + row * rowSpan + col * 4, 3);
85 	}
86 	else if (destDepth == 4)
87 	{
88 		for (int row = 0; row < height; row++)
89 			memcpy(destBuffer + row * destRowSpan, buffer + row * rowSpan, rowSpan);
90 	}
91 }
92