Pixel.cpp
1 /******************************************************************************
2 
3  nomlib - C++11 cross-platform game engine
4 
5 Copyright (c) 2013, Jeffrey Carpenter <jeffrey.carp@gmail.com>
6 All rights reserved.
7 
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10 
11 1. Redistributions of source code must retain the above copyright notice, this
12  list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 ******************************************************************************/
29 #include "nomlib/graphics/Pixel.hpp"
30 
31 namespace nom {
32 
33 Pixel::Pixel ( void ) {}
34 
35 Pixel::~Pixel ( void ) {}
36 
37 Pixel::Pixel ( const Coords& coords, const Color& color )
38 {
39  this->coords = coords;
40  this->color = color;
41 }
42 
43 Pixel::Pixel ( int32 x, int32 y, const Color& color )
44 {
45  this->coords = Coords ( x, y );
46  this->color = color;
47 }
48 
49 void Pixel::Update ( void ) {}
50 
51 void Pixel::Draw ( SDL_Surface* video_buffer ) const
52 {
53  // FIXME: Change me to Display::getCanvasColorDepth or such when we get
54  // around to implementing our object wrapper for our rendering context --
55  // SDL_SDL_Surface* screen (nom::Display is our future candidate...)
56  switch ( video_buffer->format->BytesPerPixel )
57  {
58  default:
59  {
60 NOM_LOG_ERR ( NOM, "Could not determine color depth -- aborting call." );
61  return;
62  }
63  break; // Unsupported color depth?
64 
65  case 8:
66  {
67  uint8* pixels = static_cast<uint8*> ( video_buffer->pixels );
68  uint32 pixel_color = RGB ( this->color, video_buffer->format );
69 
70  pixels[ ( this->coords.y * video_buffer->pitch ) + this->coords.x ] = pixel_color;
71  }
72  break;
73 
74  case 2: // 15/16-bit BPP
75  {
76  uint16* pixels = static_cast<uint16*> ( video_buffer->pixels );
77  uint32 pixel_color = RGB ( this->color, video_buffer->format );
78 
79  pixels[ ( this->coords.y * video_buffer->pitch/2 ) + this->coords.x ] = pixel_color;
80  }
81  break;
82 
83  case 3: // 24-bit BPP
84  {
85  uint8* pixels = static_cast<uint8*> ( video_buffer->pixels );
86  uint32 pixel_color = RGB ( this->color, video_buffer->format );
87 
88  pixels[ ( this->coords.y * video_buffer->pitch ) + this->coords.x ] = pixel_color;
89  *(pixels + video_buffer->format->Rshift/8 ) = this->color.red;
90  *(pixels + video_buffer->format->Gshift/8 ) = this->color.green;
91  *(pixels + video_buffer->format->Bshift/8 ) = this->color.blue;
92  //*(pixels + video_buffer->format->Ashift/8 ) = this->color.alpha;
93  }
94  break;
95 
96  case 4: // 32-bit BPP
97  {
98  uint32* pixels = static_cast<uint32*> ( video_buffer->pixels );
99  uint32 pixel_color = RGB ( this->color, video_buffer->format );
100 
101  pixels[ ( this->coords.y * video_buffer->pitch/4 ) + this->coords.x ] = pixel_color;
102  }
103  break;
104  } // end switch
105 }
106 
107 
108 } // namespace nom
void Draw(SDL_Surface *video_buffer) const
Definition: Pixel.cpp:51