OpenTTD Source: src/blitter/8bpp_simple.cpp Source File

OpenTTD Source 15.3

8bpp_simple.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#include "../stdafx.h"
11#include "../zoom_func.h"
12#include "8bpp_simple.hpp"
13
14#include "../safeguards.h"
15
17 static FBlitter_8bppSimple iFBlitter_8bppSimple;
18
19 void Blitter_8bppSimple::Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom)
20{
21 const uint8_t *src, *src_line;
22 uint8_t *dst, *dst_line;
23
24 /* Find where to start reading in the source sprite */
25 src_line = (const uint8_t *)bp->sprite + (bp->skip_top * bp->sprite_width + bp->skip_left) * ScaleByZoom(1, zoom);
26 dst_line = (uint8_t *)bp->dst + bp->top * bp->pitch + bp->left;
27
28 for (int y = 0; y < bp->height; y++) {
29 dst = dst_line;
30 dst_line += bp->pitch;
31
32 src = src_line;
33 src_line += bp->sprite_width * ScaleByZoom(1, zoom);
34
35 for (int x = 0; x < bp->width; x++) {
36 uint colour = 0;
37
38 switch (mode) {
39 case BlitterMode::ColourRemap:
40 case BlitterMode::CrashRemap:
41 colour = bp->remap[*src];
42 break;
43
44 case BlitterMode::Transparent:
45 case BlitterMode::TransparentRemap:
46 if (*src != 0) colour = bp->remap[*dst];
47 break;
48
49 case BlitterMode::BlackRemap:
50 if (*src != 0) *dst = 0;
51 break;
52
53 default:
54 colour = *src;
55 break;
56 }
57 if (colour != 0) *dst = colour;
58 dst++;
59 src += ScaleByZoom(1, zoom);
60 }
61 }
62}
63
64 Sprite *Blitter_8bppSimple::Encode(SpriteType, const SpriteLoader::SpriteCollection &sprite, SpriteAllocator &allocator)
65{
66 const auto &root_sprite = sprite.Root();
67 Sprite *dest_sprite;
68 dest_sprite = allocator.Allocate<Sprite>(sizeof(*dest_sprite) + static_cast<size_t>(root_sprite.height) * static_cast<size_t>(root_sprite.width));
69
70 dest_sprite->height = root_sprite.height;
71 dest_sprite->width = root_sprite.width;
72 dest_sprite->x_offs = root_sprite.x_offs;
73 dest_sprite->y_offs = root_sprite.y_offs;
74
75 /* Copy over only the 'remap' channel, as that is what we care about in 8bpp */
76 uint8_t *dst = reinterpret_cast<uint8_t *>(dest_sprite->data);
77 for (int i = 0; i < root_sprite.height * root_sprite.width; i++) {
78 dst[i] = root_sprite.data[i].m;
79 }
80
81 return dest_sprite;
82}
iFBlitter_8bppSimple
static FBlitter_8bppSimple iFBlitter_8bppSimple
Instantiation of the simple 8bpp blitter factory.
Definition 8bpp_simple.cpp:17
8bpp_simple.hpp
Simple (and slow) 8 bpp blitter.
BlitterMode
BlitterMode
The modes of blitting we can do.
Definition base.hpp:17
BlitterMode::Transparent
@ Transparent
Perform transparency darkening remapping.
BlitterMode::CrashRemap
@ CrashRemap
Perform a crash remapping.
BlitterMode::BlackRemap
@ BlackRemap
Perform remapping to a completely blackened sprite.
BlitterMode::TransparentRemap
@ TransparentRemap
Perform transparency colour remapping.
BlitterMode::ColourRemap
@ ColourRemap
Perform a colour remapping.
Blitter_8bppSimple::Draw
void Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom) override
Draw an image to the screen, given an amount of params defined above.
Definition 8bpp_simple.cpp:19
Blitter_8bppSimple::Encode
Sprite * Encode(SpriteType sprite_type, const SpriteLoader::SpriteCollection &sprite, SpriteAllocator &allocator) override
Convert a sprite from the loader to our own format.
Definition 8bpp_simple.cpp:64
FBlitter_8bppSimple
Factory for the most trivial 8bpp blitter.
Definition 8bpp_simple.hpp:26
SpriteAllocator
Interface for something that can allocate memory for a sprite.
Definition spriteloader.hpp:104
SpriteAllocator::Allocate
T * Allocate(size_t size)
Allocate memory for a sprite.
Definition spriteloader.hpp:115
SpriteCollMap
Map zoom level to data.
Definition spriteloader.hpp:34
SpriteType
SpriteType
Types of sprites that might be loaded.
Definition gfx_type.h:357
Blitter::BlitterParams
Parameters related to blitting.
Definition base.hpp:32
Blitter::BlitterParams::skip_top
int skip_top
How much pixels of the source to skip on the top (based on zoom of dst)
Definition base.hpp:37
Blitter::BlitterParams::dst
void * dst
Destination buffer.
Definition base.hpp:45
Blitter::BlitterParams::left
int left
The left offset in the 'dst' in pixels to start drawing.
Definition base.hpp:42
Blitter::BlitterParams::pitch
int pitch
The pitch of the destination buffer.
Definition base.hpp:46
Blitter::BlitterParams::sprite_width
int sprite_width
Real width of the sprite.
Definition base.hpp:40
Blitter::BlitterParams::skip_left
int skip_left
How much pixels of the source to skip on the left (based on zoom of dst)
Definition base.hpp:36
Blitter::BlitterParams::height
int height
The height in pixels that needs to be drawn to dst.
Definition base.hpp:39
Blitter::BlitterParams::remap
const uint8_t * remap
XXX – Temporary storage for remap array.
Definition base.hpp:34
Blitter::BlitterParams::width
int width
The width in pixels that needs to be drawn to dst.
Definition base.hpp:38
Blitter::BlitterParams::sprite
const void * sprite
Pointer to the sprite how ever the encoder stored it.
Definition base.hpp:33
Blitter::BlitterParams::top
int top
The top offset in the 'dst' in pixels to start drawing.
Definition base.hpp:43
Sprite
Data structure describing a sprite.
Definition spritecache_type.h:16
Sprite::width
uint16_t width
Width of the sprite.
Definition spritecache_type.h:18
Sprite::height
uint16_t height
Height of the sprite.
Definition spritecache_type.h:17
Sprite::y_offs
int16_t y_offs
Number of pixels to shift the sprite downwards.
Definition spritecache_type.h:20
Sprite::data
std::byte data[]
Sprite data.
Definition spritecache_type.h:21
Sprite::x_offs
int16_t x_offs
Number of pixels to shift the sprite to the right.
Definition spritecache_type.h:19
ScaleByZoom
int ScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift left (when zoom > ZoomLevel::Min) When shifting right,...
Definition zoom_func.h:22
ZoomLevel
ZoomLevel
All zoom levels we know.
Definition zoom_type.h:20