_________ __                 __
        /   _____//  |_____________ _/  |______     ____  __ __  ______
        \_____  \\   __\_  __ \__  \\   __\__  \   / ___\|  |  \/  ___/
        /        \|  |  |  | \// __ \|  |  / __ \_/ /_/  >  |  /\___ \
       /_______  /|__|  |__|  (____  /__| (____  /\___  /|____//____  >
               \/                  \/          \//_____/            \/
    ______________________                           ______________________
                          T H E   W A R   B E G I N S
                   Stratagus - A free fantasy real time strategy game engine

fow_utils.h
Go to the documentation of this file.
1 // _________ __ __
2 // / _____// |_____________ _/ |______ ____ __ __ ______
3 // \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/
4 // / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ |
5 // /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ >
6 // \/ \/ \//_____/ \/
7 // ______________________ ______________________
8 // T H E W A R B E G I N S
9 // Stratagus - A free fantasy real time strategy game engine
10 //
12 //
13 // (c) Copyright 2020-2021 by Alyokhin
14 //
15 // This program is free software; you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation; only version 2 of the License.
18 //
19 // This program is distributed in the hope that it will be useful,
20 // but WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 // GNU General Public License for more details.
23 //
24 // You should have received a copy of the GNU General Public License
25 // along with this program; if not, write to the Free Software
26 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 // 02111-1307, USA.
28 //
29 
30 #ifndef __FOW_UTILS_H__
31 #define __FOW_UTILS_H__
32 
33 #include <cstdint>
34 #include <vector>
35 #include "SDL.h"
36 
37 
39 
40 
41 class CViewport;
42 
43 
44 /*----------------------------------------------------------------------------
45 -- Declarations
46 ----------------------------------------------------------------------------*/
48 {
49 public:
50  void Init(const uint16_t width, const uint16_t height, const uint8_t numOfSteps);
51  void Clean();
52 
53  void SetNumOfSteps(const uint8_t num);
54  void PushNext(const bool forcedShowNext = false);
55  void DrawRegion(uint8_t *target, const uint16_t trgWidth, const uint16_t x0, const uint16_t y0, const SDL_Rect &srcRect);
56  uint8_t GetPixel(const uint16_t x, const uint16_t y);
57 
58  bool isFullyEased() const { return CurrentStep == EasingStepsNum ? true : false; }
59  void Ease() { if (CurrentStep < EasingStepsNum) CurrentStep++; }
60 
61  uint8_t *GetCurrent() { return Frames[Prev].data(); }
62  uint8_t *GetNext() { return Frames[Next].data(); }
63  uint16_t GetWidth() const { return Width; }
64  uint16_t GetHeight() const { return Height; }
65 
66 private:
67  void CalcDeltas();
68  void SwapFrames() { const uint8_t swap = Prev; Prev = Curr; Curr = Next; Next = swap; }
69 
70 private:
71  uint8_t CurrentStep {0};
72  uint8_t EasingStepsNum {0};
73  uint16_t Width {0};
74  uint16_t Height {0};
75  size_t TextureSize {0}; // Width * Height
76 
77  std::vector<uint8_t> Frames[3];
78  uint8_t Prev {0};
79  uint8_t Curr {1};
80  uint8_t Next {2};
81 
82  std::vector<int16_t> Deltas;
83 };
84 
86 class CBlurer
87 {
88 public:
89  void Init(const uint16_t textureWidth, const uint16_t textureHeight, const float radius, const int numOfIterations);
90  void PrecalcParameters(const float radius, const int numOfIterations);
91 
92  void Clean();
93  void Blur(uint8_t *const texture);
94 private:
95  void ProceedIteration(uint8_t *source, uint8_t *target, const uint8_t radius);
96 
97 private:
98  float Radius {2};
99  uint8_t NumOfIterations {3};
101 
102  std::vector<uint8_t> HalfBoxes;
103  std::vector<uint8_t> WorkingTexture;
104  uint16_t TextureWidth {0};
105  uint16_t TextureHeight {0};
106 };
107 
109 inline uint8_t CEasedTexture::GetPixel(const uint16_t x, const uint16_t y)
110 {
111  const size_t textureIndex = y * Width + x;
112 
113  if (CurrentStep == 0 || CurrentStep == EasingStepsNum) {
114  const uint8_t currFrame = CurrentStep ? Curr : Prev;
115  const uint8_t *curr = Frames[currFrame].data();
116 
117  return curr[textureIndex];
118 
119  } else {
120  const uint8_t *prev = Frames[Prev].data();
121  const int16_t *deltas = Deltas.data();
122 
123  return prev[textureIndex] + deltas[textureIndex] * CurrentStep;
124 
125  }
126 }
127 
128 #endif // !__FOW_UTILS_H__
CEasedTexture::isFullyEased
bool isFullyEased() const
Definition: fow_utils.h:58
CEasedTexture::Clean
void Clean()
Definition: fow_utils.cpp:89
CEasedTexture::DrawRegion
void DrawRegion(uint8_t *target, const uint16_t trgWidth, const uint16_t x0, const uint16_t y0, const SDL_Rect &srcRect)
Definition: fow_utils.cpp:139
CEasedTexture::SetNumOfSteps
void SetNumOfSteps(const uint8_t num)
Definition: fow_utils.cpp:110
CEasedTexture::Ease
void Ease()
Definition: fow_utils.h:59
CViewport
Definition: viewport.h:63
CBlurer::Init
void Init(const uint16_t textureWidth, const uint16_t textureHeight, const float radius, const int numOfIterations)
Definition: fow_utils.cpp:198
CEasedTexture
Definition: fow_utils.h:47
CEasedTexture::GetCurrent
uint8_t * GetCurrent()
Definition: fow_utils.h:61
CEasedTexture::Init
void Init(const uint16_t width, const uint16_t height, const uint8_t numOfSteps)
Definition: fow_utils.cpp:64
CEasedTexture::GetNext
uint8_t * GetNext()
Definition: fow_utils.h:62
CEasedTexture::GetPixel
uint8_t GetPixel(const uint16_t x, const uint16_t y)
returns pixel value for current frame
Definition: fow_utils.h:109
CBlurer::Blur
void Blur(uint8_t *const texture)
Definition: fow_utils.cpp:258
CEasedTexture::GetWidth
uint16_t GetWidth() const
Definition: fow_utils.h:63
CBlurer
Class for box blur algorithm. Used to blur 4x4 upscaled FOW texture.
Definition: fow_utils.h:86
CEasedTexture::PushNext
void PushNext(const bool forcedShowNext=false)
Definition: fow_utils.cpp:122
CBlurer::PrecalcParameters
void PrecalcParameters(const float radius, const int numOfIterations)
Definition: fow_utils.cpp:215
CEasedTexture::GetHeight
uint16_t GetHeight() const
Definition: fow_utils.h:64
CBlurer::Clean
void Clean()
Definition: fow_utils.cpp:243
(C) Copyright 1998-2012 by The Stratagus Project under the GNU General Public License.
All trademarks and copyrights on this page are owned by their respective owners.