| 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 | |
| 21 | #ifdef DISABLE_EXCEPTIONS |
| 22 | #define throw (void)0, |
| 23 | #endif |
| 24 | |
| 25 | |
| 26 | MDFN_PixelFormat::MDFN_PixelFormat() |
| 27 | { |
| 28 | bpp = 0; |
| 29 | colorspace = 0; |
| 30 | |
| 31 | Rshift = 0; |
| 32 | Gshift = 0; |
| 33 | Bshift = 0; |
| 34 | Ashift = 0; |
| 35 | } |
| 36 | |
| 37 | 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) |
| 38 | { |
| 39 | #if defined(WANT_16BPP) |
| 40 | bpp = 16; |
| 41 | #else |
| 42 | bpp = 32; |
| 43 | #endif |
| 44 | colorspace = p_colorspace; |
| 45 | |
| 46 | Rshift = p_rs; |
| 47 | Gshift = p_gs; |
| 48 | Bshift = p_bs; |
| 49 | Ashift = p_as; |
| 50 | } |
| 51 | |
| 52 | MDFN_Surface::MDFN_Surface() |
| 53 | { |
| 54 | memset(&format, 0, sizeof(format)); |
| 55 | |
| 56 | pixels = NULL; |
| 57 | pixels16 = NULL; |
| 58 | pitchinpix = 0; |
| 59 | w = 0; |
| 60 | h = 0; |
| 61 | } |
| 62 | |
| 63 | MDFN_Surface::MDFN_Surface(void *const p_pixels, const uint32 p_width, const uint32 p_height, const uint32 p_pitchinpix, const MDFN_PixelFormat &nf) |
| 64 | { |
| 65 | Init(p_pixels, p_width, p_height, p_pitchinpix, nf); |
| 66 | } |
| 67 | |
| 68 | void MDFN_Surface::Init(void *const p_pixels, const uint32 p_width, const uint32 p_height, const uint32 p_pitchinpix, const MDFN_PixelFormat &nf) |
| 69 | { |
| 70 | void *rpix = NULL; |
| 71 | assert(nf.bpp == 16 || nf.bpp == 32); |
| 72 | |
| 73 | format = nf; |
| 74 | |
| 75 | pixels16 = NULL; |
| 76 | pixels = NULL; |
| 77 | #if defined(WANT_8BPP) |
| 78 | palette = NULL; |
| 79 | #endif |
| 80 | |
| 81 | if(!(rpix = calloc(1, p_pitchinpix * p_height * (nf.bpp / 8)))) |
| 82 | throw(1); |
| 83 | |
| 84 | #if defined(WANT_8BPP) |
| 85 | //if(nf.bpp == 8) |
| 86 | { |
| 87 | pixels8 = (uint8 *)rpix; |
| 88 | palette = (MDFN_PaletteEntry*)calloc(sizeof(MDFN_PaletteEntry), 256); |
| 89 | } |
| 90 | #elif defined(WANT_16BPP) |
| 91 | //if(nf.bpp == 16) |
| 92 | pixels16 = (uint16 *)rpix; |
| 93 | #elif defined(WANT_32BPP) |
| 94 | //else |
| 95 | pixels = (uint32 *)rpix; |
| 96 | #endif |
| 97 | |
| 98 | w = p_width; |
| 99 | h = p_height; |
| 100 | |
| 101 | pitchinpix = p_pitchinpix; |
| 102 | } |
| 103 | |
| 104 | // When we're converting, only convert the w*h area(AKA leave the last part of the line, pitch32 - w, alone), |
| 105 | // for places where we store auxillary information there(graphics viewer in the debugger), and it'll be faster |
| 106 | // to boot. |
| 107 | void MDFN_Surface::SetFormat(const MDFN_PixelFormat &nf, bool convert) |
| 108 | { |
| 109 | format = nf; |
| 110 | } |
| 111 | |
| 112 | MDFN_Surface::~MDFN_Surface() |
| 113 | { |
| 114 | #if defined(WANT_16BPP) |
| 115 | if(pixels16) |
| 116 | free(pixels16); |
| 117 | #elif defined(WANT_32BPP) |
| 118 | if(pixels) |
| 119 | free(pixels); |
| 120 | #elif defined(WANT_8BPP) |
| 121 | pixels8 = NULL; |
| 122 | if(palette) |
| 123 | free(palette); |
| 124 | palette = NULL; |
| 125 | #endif |
| 126 | } |
| 127 |