1 /* Mednafen - Multi-system Emulator
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18 #include "../mednafen.h"
19 #include "surface.h"
20
MDFN_PixelFormat()21 MDFN_PixelFormat::MDFN_PixelFormat()
22 {
23 bpp = 0;
24 colorspace = 0;
25
26 Rshift = 0;
27 Gshift = 0;
28 Bshift = 0;
29 Ashift = 0;
30 }
31
MDFN_PixelFormat(const unsigned int p_colorspace,const uint8 p_rs,const uint8 p_gs,const uint8 p_bs,const uint8 p_as)32 MDFN_PixelFormat::MDFN_PixelFormat(const unsigned int p_colorspace, const uint8 p_rs, const uint8 p_gs, const uint8 p_bs, const uint8 p_as)
33 {
34 #if defined(WANT_16BPP)
35 bpp = 16;
36 #else
37 bpp = 32;
38 #endif
39 colorspace = p_colorspace;
40
41 Rshift = p_rs;
42 Gshift = p_gs;
43 Bshift = p_bs;
44 Ashift = p_as;
45 }
46
MDFN_Surface()47 MDFN_Surface::MDFN_Surface()
48 {
49 format = MDFN_PixelFormat();
50
51 pixels = NULL;
52 pixels16 = NULL;
53 pitchinpix = 0;
54 w = 0;
55 h = 0;
56 }
57
MDFN_Surface(void * const p_pixels,const uint32 p_width,const uint32 p_height,const uint32 p_pitchinpix,const MDFN_PixelFormat & nf)58 MDFN_Surface::MDFN_Surface(void *const p_pixels, const uint32 p_width, const uint32 p_height, const uint32 p_pitchinpix, const MDFN_PixelFormat &nf)
59 {
60 Init(p_pixels, p_width, p_height, p_pitchinpix, nf);
61 }
62
Init(void * const p_pixels,const uint32 p_width,const uint32 p_height,const uint32 p_pitchinpix,const MDFN_PixelFormat & nf)63 void MDFN_Surface::Init(void *const p_pixels, const uint32 p_width, const uint32 p_height, const uint32 p_pitchinpix, const MDFN_PixelFormat &nf)
64 {
65 void *rpix = NULL;
66 assert(nf.bpp == 16 || nf.bpp == 32);
67
68 format = nf;
69
70 pixels16 = NULL;
71 pixels = NULL;
72 #if defined(WANT_8BPP)
73 palette = NULL;
74 #endif
75
76 if(!(rpix = calloc(1, p_pitchinpix * p_height * (nf.bpp / 8))))
77 throw(1);
78
79 #if defined(WANT_8BPP)
80 //if(nf.bpp == 8)
81 {
82 pixels8 = (uint8 *)rpix;
83 palette = (MDFN_PaletteEntry*)calloc(sizeof(MDFN_PaletteEntry), 256);
84 }
85 #elif defined(WANT_16BPP)
86 //if(nf.bpp == 16)
87 pixels16 = (uint16 *)rpix;
88 #elif defined(WANT_32BPP)
89 //else
90 pixels = (uint32 *)rpix;
91 #endif
92
93 w = p_width;
94 h = p_height;
95
96 pitchinpix = p_pitchinpix;
97 }
98
99 // When we're converting, only convert the w*h area(AKA leave the last part of the line, pitch32 - w, alone),
100 // for places where we store auxillary information there(graphics viewer in the debugger), and it'll be faster
101 // to boot.
SetFormat(const MDFN_PixelFormat & nf,bool convert)102 void MDFN_Surface::SetFormat(const MDFN_PixelFormat &nf, bool convert)
103 {
104 format = nf;
105 }
106
~MDFN_Surface()107 MDFN_Surface::~MDFN_Surface()
108 {
109 #if defined(WANT_16BPP)
110 if(pixels16)
111 free(pixels16);
112 #elif defined(WANT_32BPP)
113 if(pixels)
114 free(pixels);
115 #elif defined(WANT_8BPP)
116 pixels8 = NULL;
117 if(palette)
118 free(palette);
119 palette = NULL;
120 #endif
121 }
122
123