OpenTTD
8bpp_base.cpp
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8  */
9 
12 #include "../stdafx.h"
13 #include "../gfx_func.h"
14 #include "8bpp_base.hpp"
15 #include "common.hpp"
16 
17 #include "../safeguards.h"
18 
19 void Blitter_8bppBase::DrawColourMappingRect(void *dst, int width, int height, PaletteID pal)
20 {
21  const uint8 *ctab = GetNonSprite(pal, ST_RECOLOUR) + 1;
22 
23  do {
24  for (int i = 0; i != width; i++) *((uint8 *)dst + i) = ctab[((uint8 *)dst)[i]];
25  dst = (uint8 *)dst + _screen.pitch;
26  } while (--height);
27 }
28 
29 void *Blitter_8bppBase::MoveTo(void *video, int x, int y)
30 {
31  return (uint8 *)video + x + y * _screen.pitch;
32 }
33 
34 void Blitter_8bppBase::SetPixel(void *video, int x, int y, uint8 colour)
35 {
36  *((uint8 *)video + x + y * _screen.pitch) = colour;
37 }
38 
39 void Blitter_8bppBase::DrawLine(void *video, int x, int y, int x2, int y2, int screen_width, int screen_height, uint8 colour, int width, int dash)
40 {
41  this->DrawLineGeneric(x, y, x2, y2, screen_width, screen_height, width, dash, [=](int x, int y) {
42  *((uint8 *)video + x + y * _screen.pitch) = colour;
43  });
44 }
45 
46 void Blitter_8bppBase::DrawRect(void *video, int width, int height, uint8 colour)
47 {
48  do {
49  memset(video, colour, width);
50  video = (uint8 *)video + _screen.pitch;
51  } while (--height);
52 }
53 
54 void Blitter_8bppBase::CopyFromBuffer(void *video, const void *src, int width, int height)
55 {
56  uint8 *dst = (uint8 *)video;
57  const uint8 *usrc = (const uint8 *)src;
58 
59  for (; height > 0; height--) {
60  memcpy(dst, usrc, width * sizeof(uint8));
61  usrc += width;
62  dst += _screen.pitch;
63  }
64 }
65 
66 void Blitter_8bppBase::CopyToBuffer(const void *video, void *dst, int width, int height)
67 {
68  uint8 *udst = (uint8 *)dst;
69  const uint8 *src = (const uint8 *)video;
70 
71  for (; height > 0; height--) {
72  memcpy(udst, src, width * sizeof(uint8));
73  src += _screen.pitch;
74  udst += width;
75  }
76 }
77 
78 void Blitter_8bppBase::CopyImageToBuffer(const void *video, void *dst, int width, int height, int dst_pitch)
79 {
80  uint8 *udst = (uint8 *)dst;
81  const uint8 *src = (const uint8 *)video;
82 
83  for (; height > 0; height--) {
84  memcpy(udst, src, width * sizeof(uint8));
85  src += _screen.pitch;
86  udst += dst_pitch;
87  }
88 }
89 
90 void Blitter_8bppBase::ScrollBuffer(void *video, int &left, int &top, int &width, int &height, int scroll_x, int scroll_y)
91 {
92  const uint8 *src;
93  uint8 *dst;
94 
95  if (scroll_y > 0) {
96  /* Calculate pointers */
97  dst = (uint8 *)video + left + (top + height - 1) * _screen.pitch;
98  src = dst - scroll_y * _screen.pitch;
99 
100  /* Decrease height and increase top */
101  top += scroll_y;
102  height -= scroll_y;
103  assert(height > 0);
104 
105  /* Adjust left & width */
106  if (scroll_x >= 0) {
107  dst += scroll_x;
108  left += scroll_x;
109  width -= scroll_x;
110  } else {
111  src -= scroll_x;
112  width += scroll_x;
113  }
114 
115  for (int h = height; h > 0; h--) {
116  memcpy(dst, src, width * sizeof(uint8));
117  src -= _screen.pitch;
118  dst -= _screen.pitch;
119  }
120  } else {
121  /* Calculate pointers */
122  dst = (uint8 *)video + left + top * _screen.pitch;
123  src = dst - scroll_y * _screen.pitch;
124 
125  /* Decrease height. (scroll_y is <=0). */
126  height += scroll_y;
127  assert(height > 0);
128 
129  /* Adjust left & width */
130  if (scroll_x >= 0) {
131  dst += scroll_x;
132  left += scroll_x;
133  width -= scroll_x;
134  } else {
135  src -= scroll_x;
136  width += scroll_x;
137  }
138 
139  /* the y-displacement may be 0 therefore we have to use memmove,
140  * because source and destination may overlap */
141  for (int h = height; h > 0; h--) {
142  memmove(dst, src, width * sizeof(uint8));
143  src += _screen.pitch;
144  dst += _screen.pitch;
145  }
146  }
147 }
148 
149 int Blitter_8bppBase::BufferSize(int width, int height)
150 {
151  return width * height;
152 }
153 
155 {
156  /* Video backend takes care of the palette animation */
157 }
158 
160 {
162 }
void DrawRect(void *video, int width, int height, uint8 colour)
Make a single horizontal line in a single colour on the video-buffer.
Definition: 8bpp_base.cpp:46
uint32 PaletteID
The number of the palette.
Definition: gfx_type.h:20
Information about the currently used palette.
Definition: gfx_type.h:309
void SetPixel(void *video, int x, int y, uint8 colour)
Draw a pixel with a given colour on the video-buffer.
Definition: 8bpp_base.cpp:34
void * MoveTo(void *video, int x, int y)
Move the destination pointer the requested amount x and y, keeping in mind any pitch and bpp of the r...
Definition: 8bpp_base.cpp:29
Palette animation should be done by video backend (8bpp only!)
Definition: base.hpp:53
void PaletteAnimate(const Palette &palette)
Called when the 8bpp palette is changed; you should redraw all pixels on the screen that are equal to...
Definition: 8bpp_base.cpp:154
void DrawColourMappingRect(void *dst, int width, int height, PaletteID pal)
Draw a colourtable to the screen.
Definition: 8bpp_base.cpp:19
Common functionality for all blitter implementations.
Base for all 8 bpp blitters.
void DrawLine(void *video, int x, int y, int x2, int y2, int screen_width, int screen_height, uint8 colour, int width, int dash)
Draw a line with a given colour.
Definition: 8bpp_base.cpp:39
void ScrollBuffer(void *video, int &left, int &top, int &width, int &height, int scroll_x, int scroll_y)
Scroll the videobuffer some &#39;x&#39; and &#39;y&#39; value.
Definition: 8bpp_base.cpp:90
void CopyImageToBuffer(const void *video, void *dst, int width, int height, int dst_pitch)
Copy from the screen to a buffer in a palette format for 8bpp and RGBA format for 32bpp...
Definition: 8bpp_base.cpp:78
void CopyFromBuffer(void *video, const void *src, int width, int height)
Copy from a buffer to the screen.
Definition: 8bpp_base.cpp:54
Blitter::PaletteAnimation UsePaletteAnimation()
Check if the blitter uses palette animation at all.
Definition: 8bpp_base.cpp:159
void CopyToBuffer(const void *video, void *dst, int width, int height)
Copy from the screen to a buffer.
Definition: 8bpp_base.cpp:66
PaletteAnimation
Types of palette animation.
Definition: base.hpp:51
Recolour sprite.
Definition: gfx_type.h:301
int BufferSize(int width, int height)
Calculate how much memory there is needed for an image of this size in the video-buffer.
Definition: 8bpp_base.cpp:149