2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2007-12-23 10:56:02 +00:00
|
|
|
/** @file gfx_func.h Functions related to the gfx engine. */
|
2007-03-01 01:24:44 +00:00
|
|
|
|
2007-09-09 10:13:17 +00:00
|
|
|
/**
|
|
|
|
* @defgroup dirty Dirty
|
|
|
|
*
|
|
|
|
* Handles the repaint of some part of the screen.
|
|
|
|
*
|
|
|
|
* Some places in the code are called functions which makes something "dirty".
|
|
|
|
* This has nothing to do with making a Tile or Window darker or less visible.
|
|
|
|
* This term comes from memory caching and is used to define an object must
|
|
|
|
* be repaint. If some data of an object (like a Tile, Window, Vehicle, whatever)
|
|
|
|
* are changed which are so extensive the object must be repaint its marked
|
|
|
|
* as "dirty". The video driver repaint this object instead of the whole screen
|
|
|
|
* (this is btw. also possible if needed). This is used to avoid a
|
|
|
|
* flickering of the screen by the video driver constantly repainting it.
|
|
|
|
*
|
2020-02-21 19:45:07 +00:00
|
|
|
* This whole mechanism was controlled by an rectangle defined in #_invalid_rect. This
|
2007-09-09 10:13:17 +00:00
|
|
|
* rectangle defines the area on the screen which must be repaint. If a new object
|
|
|
|
* needs to be repainted this rectangle is extended to 'catch' the object on the
|
2013-01-08 22:46:42 +00:00
|
|
|
* screen. At some point (which is normally uninteresting for patch writers) this
|
2007-09-09 10:13:17 +00:00
|
|
|
* rectangle is send to the video drivers method
|
|
|
|
* VideoDriver::MakeDirty and it is truncated back to an empty rectangle. At some
|
2013-01-08 22:46:42 +00:00
|
|
|
* later point (which is uninteresting, too) the video driver
|
2007-09-09 10:13:17 +00:00
|
|
|
* repaints all these saved rectangle instead of the whole screen and drop the
|
2019-09-29 20:27:32 +00:00
|
|
|
* rectangle information. Then a new round begins by marking objects "dirty".
|
2007-09-09 10:13:17 +00:00
|
|
|
*
|
|
|
|
* @see VideoDriver::MakeDirty
|
|
|
|
* @see _screen
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2007-12-23 10:56:02 +00:00
|
|
|
#ifndef GFX_FUNC_H
|
|
|
|
#define GFX_FUNC_H
|
|
|
|
|
|
|
|
#include "gfx_type.h"
|
2023-12-28 16:01:08 +00:00
|
|
|
#include "palette_func.h"
|
2007-12-23 10:56:02 +00:00
|
|
|
#include "strings_type.h"
|
2013-08-05 20:36:36 +00:00
|
|
|
#include "string_type.h"
|
2023-07-03 22:34:52 +00:00
|
|
|
#include <vector>
|
2007-01-13 14:43:46 +00:00
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void GameLoop();
|
2007-01-13 14:43:46 +00:00
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void CreateConsole();
|
2007-01-13 14:43:46 +00:00
|
|
|
|
2007-03-01 01:24:44 +00:00
|
|
|
extern byte _dirkeys; ///< 1 = left, 2 = up, 4 = right, 8 = down
|
2007-01-13 14:43:46 +00:00
|
|
|
extern bool _fullscreen;
|
2014-04-27 12:15:14 +00:00
|
|
|
extern byte _support8bpp;
|
2007-01-13 14:43:46 +00:00
|
|
|
extern CursorVars _cursor;
|
2007-03-01 01:24:44 +00:00
|
|
|
extern bool _ctrl_pressed; ///< Is Ctrl pressed?
|
|
|
|
extern bool _shift_pressed; ///< Is Shift pressed?
|
2019-09-05 22:21:41 +00:00
|
|
|
extern bool _invert_ctrl;
|
|
|
|
extern bool _invert_shift;
|
2024-01-07 16:41:53 +00:00
|
|
|
extern uint16_t _game_speed;
|
|
|
|
extern uint8_t _milliseconds_per_tick;
|
2023-05-22 17:40:46 +00:00
|
|
|
extern float _ticks_per_second;
|
2007-01-13 14:43:46 +00:00
|
|
|
|
|
|
|
extern bool _left_button_down;
|
|
|
|
extern bool _left_button_clicked;
|
|
|
|
extern bool _right_button_down;
|
|
|
|
extern bool _right_button_clicked;
|
|
|
|
|
|
|
|
extern DrawPixelInfo _screen;
|
2008-01-11 17:12:41 +00:00
|
|
|
extern bool _screen_disable_anim; ///< Disable palette animation (important for 32bpp-anim blitter during giant screenshot)
|
2007-01-13 14:43:46 +00:00
|
|
|
|
2019-04-12 16:46:49 +00:00
|
|
|
extern std::vector<Dimension> _resolutions;
|
2008-06-16 19:38:41 +00:00
|
|
|
extern Dimension _cur_resolution;
|
2011-12-08 18:13:29 +00:00
|
|
|
extern Palette _cur_palette; ///< Current palette
|
2007-01-13 14:43:46 +00:00
|
|
|
|
2022-11-10 18:58:33 +00:00
|
|
|
extern DrawPixelInfo *_cur_dpi;
|
|
|
|
|
2021-08-19 16:39:44 +00:00
|
|
|
void HandleToolbarHotkey(int hotkey);
|
2024-01-07 16:41:53 +00:00
|
|
|
void HandleKeypress(uint keycode, char32_t key);
|
2019-04-10 21:07:06 +00:00
|
|
|
void HandleTextInput(const char *str, bool marked = false, const char *caret = nullptr, const char *insert_location = nullptr, const char *replacement_end = nullptr);
|
2008-02-17 17:00:43 +00:00
|
|
|
void HandleCtrlChanged();
|
2019-09-05 22:21:41 +00:00
|
|
|
void HandleShiftChanged();
|
2007-03-07 11:47:46 +00:00
|
|
|
void HandleMouseEvents();
|
|
|
|
void UpdateWindows();
|
2021-02-28 14:41:03 +00:00
|
|
|
void ChangeGameSpeed(bool enable_fast_forward);
|
2023-05-22 17:40:46 +00:00
|
|
|
void SetupTickRate();
|
2007-01-13 14:43:46 +00:00
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void DrawMouseCursor();
|
|
|
|
void ScreenSizeChanged();
|
|
|
|
void GameSizeChanged();
|
|
|
|
void UndrawMouseCursor();
|
2007-01-13 14:43:46 +00:00
|
|
|
|
2022-12-06 18:29:55 +00:00
|
|
|
enum AdjustGUIZoomMode {
|
|
|
|
AGZM_MANUAL,
|
|
|
|
AGZM_AUTOMATIC,
|
|
|
|
AGZM_STARTUP,
|
|
|
|
};
|
|
|
|
bool AdjustGUIZoom(AdjustGUIZoomMode mode);
|
|
|
|
|
2024-01-15 17:51:33 +00:00
|
|
|
void FontChanged();
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
void RedrawScreenRect(int left, int top, int right, int bottom);
|
2005-07-17 15:54:57 +00:00
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
Dimension GetSpriteSize(SpriteID sprid, Point *offset = nullptr, ZoomLevel zoom = ZOOM_LVL_GUI);
|
2022-09-23 12:35:32 +00:00
|
|
|
Dimension GetScaledSpriteSize(SpriteID sprid); /* widget.cpp */
|
2022-11-09 22:24:31 +00:00
|
|
|
struct SpritePointerHolder;
|
|
|
|
void DrawSpriteViewport(const SpritePointerHolder &sprite_store, const DrawPixelInfo *dpi, SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub = nullptr);
|
2023-08-22 21:16:04 +00:00
|
|
|
void PrepareDrawSpriteViewportSpriteStore(SpritePointerHolder &sprite_store, const DrawPixelInfo *dpi, SpriteID img, PaletteID pal);
|
2019-04-10 21:07:06 +00:00
|
|
|
void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub = nullptr, ZoomLevel zoom = ZOOM_LVL_GUI);
|
2023-12-04 18:19:55 +00:00
|
|
|
void DrawSpriteIgnorePadding(SpriteID img, PaletteID pal, const Rect &r, StringAlignment align); /* widget.cpp */
|
2023-05-08 17:01:06 +00:00
|
|
|
std::unique_ptr<uint32_t[]> DrawSpriteToRgbaBuffer(SpriteID spriteId, ZoomLevel zoom = ZOOM_LVL_GUI);
|
2005-09-10 07:38:03 +00:00
|
|
|
|
2023-05-06 17:53:02 +00:00
|
|
|
int DrawString(int left, int right, int top, std::string_view str, TextColour colour = TC_FROMSTRING, StringAlignment align = SA_LEFT, bool underline = false, FontSize fontsize = FS_NORMAL);
|
2011-11-20 11:50:12 +00:00
|
|
|
int DrawString(int left, int right, int top, StringID str, TextColour colour = TC_FROMSTRING, StringAlignment align = SA_LEFT, bool underline = false, FontSize fontsize = FS_NORMAL);
|
2023-05-06 17:53:02 +00:00
|
|
|
int DrawStringMultiLine(int left, int right, int top, int bottom, std::string_view str, TextColour colour = TC_FROMSTRING, StringAlignment align = (SA_TOP | SA_LEFT), bool underline = false, FontSize fontsize = FS_NORMAL);
|
2011-11-20 11:50:12 +00:00
|
|
|
int DrawStringMultiLine(int left, int right, int top, int bottom, StringID str, TextColour colour = TC_FROMSTRING, StringAlignment align = (SA_TOP | SA_LEFT), bool underline = false, FontSize fontsize = FS_NORMAL);
|
2009-03-21 18:28:01 +00:00
|
|
|
|
2024-01-07 16:41:53 +00:00
|
|
|
void DrawCharCentered(char32_t c, const Rect &r, TextColour colour);
|
2008-03-26 10:08:17 +00:00
|
|
|
|
2022-11-10 18:58:33 +00:00
|
|
|
void GfxFillRect(const DrawPixelInfo *dpi, int left, int top, int right, int bottom, int colour, FillRectMode mode = FILLRECT_OPAQUE);
|
|
|
|
inline void GfxFillRect(int left, int top, int right, int bottom, int colour, FillRectMode mode = FILLRECT_OPAQUE) { GfxFillRect(_cur_dpi, left, top, right, bottom, colour, mode); }
|
2022-05-18 23:12:17 +00:00
|
|
|
void GfxFillPolygon(const std::vector<Point> &shape, int colour, FillRectMode mode = FILLRECT_OPAQUE, GfxFillRectModeFunctor *fill_functor = nullptr);
|
2022-11-10 18:58:33 +00:00
|
|
|
void GfxDrawLine(const DrawPixelInfo *dpi, int left, int top, int right, int bottom, int colour, int width = 1, int dash = 0);
|
|
|
|
inline void GfxDrawLine(int left, int top, int right, int bottom, int colour, int width = 1, int dash = 0) { GfxDrawLine(_cur_dpi, left, top, right, bottom, colour, width, dash); }
|
|
|
|
void DrawBox(const DrawPixelInfo *dpi, int x, int y, int dx1, int dy1, int dx2, int dy2, int dx3, int dy3);
|
2023-11-30 18:10:07 +00:00
|
|
|
void DrawRectOutline(const Rect &r, int colour, int width = 1, int dash = 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2022-09-28 16:16:46 +00:00
|
|
|
/* Versions of DrawString/DrawStringMultiLine that accept a Rect instead of separate left, right, top and bottom parameters. */
|
2024-01-06 11:19:27 +00:00
|
|
|
inline int DrawString(const Rect &r, std::string_view str, TextColour colour = TC_FROMSTRING, StringAlignment align = SA_LEFT, bool underline = false, FontSize fontsize = FS_NORMAL)
|
2022-09-28 16:16:46 +00:00
|
|
|
{
|
|
|
|
return DrawString(r.left, r.right, r.top, str, colour, align, underline, fontsize);
|
|
|
|
}
|
|
|
|
|
2024-01-06 11:19:27 +00:00
|
|
|
inline int DrawString(const Rect &r, StringID str, TextColour colour = TC_FROMSTRING, StringAlignment align = SA_LEFT, bool underline = false, FontSize fontsize = FS_NORMAL)
|
2022-09-28 16:16:46 +00:00
|
|
|
{
|
|
|
|
return DrawString(r.left, r.right, r.top, str, colour, align, underline, fontsize);
|
|
|
|
}
|
|
|
|
|
2024-01-06 11:19:27 +00:00
|
|
|
inline int DrawStringMultiLine(const Rect &r, std::string_view str, TextColour colour = TC_FROMSTRING, StringAlignment align = (SA_TOP | SA_LEFT), bool underline = false, FontSize fontsize = FS_NORMAL)
|
2022-09-28 16:16:46 +00:00
|
|
|
{
|
|
|
|
return DrawStringMultiLine(r.left, r.right, r.top, r.bottom, str, colour, align, underline, fontsize);
|
|
|
|
}
|
|
|
|
|
2024-01-06 11:19:27 +00:00
|
|
|
inline int DrawStringMultiLine(const Rect &r, StringID str, TextColour colour = TC_FROMSTRING, StringAlignment align = (SA_TOP | SA_LEFT), bool underline = false, FontSize fontsize = FS_NORMAL)
|
2022-09-28 16:16:46 +00:00
|
|
|
{
|
|
|
|
return DrawStringMultiLine(r.left, r.right, r.top, r.bottom, str, colour, align, underline, fontsize);
|
|
|
|
}
|
|
|
|
|
2024-01-06 11:19:27 +00:00
|
|
|
inline void GfxFillRect(const Rect &r, int colour, FillRectMode mode = FILLRECT_OPAQUE)
|
2022-09-28 16:16:46 +00:00
|
|
|
{
|
|
|
|
GfxFillRect(r.left, r.top, r.right, r.bottom, colour, mode);
|
|
|
|
}
|
|
|
|
|
2023-05-06 17:53:02 +00:00
|
|
|
Dimension GetStringBoundingBox(std::string_view str, FontSize start_fontsize = FS_NORMAL);
|
2022-08-20 10:33:04 +00:00
|
|
|
Dimension GetStringBoundingBox(StringID strid, FontSize start_fontsize = FS_NORMAL);
|
2022-12-27 18:39:37 +00:00
|
|
|
uint GetStringListWidth(const StringID *list, FontSize fontsize = FS_NORMAL);
|
2023-05-06 17:53:02 +00:00
|
|
|
int GetStringHeight(std::string_view str, int maxw, FontSize fontsize = FS_NORMAL);
|
2008-07-22 19:25:47 +00:00
|
|
|
int GetStringHeight(StringID str, int maxw);
|
2013-06-09 12:21:44 +00:00
|
|
|
int GetStringLineCount(StringID str, int maxw);
|
2009-09-02 20:40:56 +00:00
|
|
|
Dimension GetStringMultiLineBoundingBox(StringID str, const Dimension &suggestion);
|
2023-05-06 17:53:02 +00:00
|
|
|
Dimension GetStringMultiLineBoundingBox(std::string_view str, const Dimension &suggestion);
|
2011-12-10 12:56:37 +00:00
|
|
|
void LoadStringWidthTable(bool monospace = false);
|
2023-05-06 17:53:02 +00:00
|
|
|
Point GetCharPosInString(std::string_view str, const char *ch, FontSize start_fontsize = FS_NORMAL);
|
|
|
|
ptrdiff_t GetCharAtPosition(std::string_view str, int x, FontSize start_fontsize = FS_NORMAL);
|
2007-09-09 10:13:17 +00:00
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void DrawDirtyBlocks();
|
2004-08-09 17:04:08 +00:00
|
|
|
void SetDirtyBlocks(int left, int top, int right, int bottom);
|
2020-03-23 00:22:46 +00:00
|
|
|
void SetPendingDirtyBlocks(int left, int top, int right, int bottom);
|
2020-02-21 19:45:07 +00:00
|
|
|
void UnsetDirtyBlocks(int left, int top, int right, int bottom);
|
2007-03-07 11:47:46 +00:00
|
|
|
void MarkWholeScreenDirty();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2014-01-03 08:51:49 +00:00
|
|
|
void CheckBlitter();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-01-10 00:31:47 +00:00
|
|
|
bool FillDrawPixelInfo(DrawPixelInfo *n, int left, int top, int width, int height);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2024-01-06 11:19:27 +00:00
|
|
|
inline bool FillDrawPixelInfo(DrawPixelInfo *n, const Rect &r)
|
2023-12-19 23:21:12 +00:00
|
|
|
{
|
|
|
|
return FillDrawPixelInfo(n, r.left, r.top, r.Width(), r.Height());
|
|
|
|
}
|
|
|
|
|
2017-03-23 22:00:00 +00:00
|
|
|
/**
|
|
|
|
* Determine where to draw a centred object inside a widget.
|
|
|
|
* @param min The top or left coordinate.
|
|
|
|
* @param max The bottom or right coordinate.
|
|
|
|
* @param size The height or width of the object to draw.
|
|
|
|
* @return Offset of where to start drawing the object.
|
|
|
|
*/
|
2024-01-06 11:19:27 +00:00
|
|
|
inline int CenterBounds(int min, int max, int size)
|
2017-03-23 22:00:00 +00:00
|
|
|
{
|
2022-08-30 07:57:19 +00:00
|
|
|
return (min + max - size + 1) / 2;
|
2017-03-23 22:00:00 +00:00
|
|
|
}
|
|
|
|
|
2007-03-01 01:24:44 +00:00
|
|
|
/* window.cpp */
|
2020-10-02 01:10:09 +00:00
|
|
|
enum DrawOverlappedWindowFlags {
|
|
|
|
DOWF_NONE = 0,
|
|
|
|
DOWF_MARK_DIRTY = 1 << 0,
|
|
|
|
DOWF_SHOW_DEBUG = 1 << 1,
|
|
|
|
};
|
|
|
|
DECLARE_ENUM_AS_BIT_SET(DrawOverlappedWindowFlags)
|
2004-08-09 17:04:08 +00:00
|
|
|
void DrawOverlappedWindowForAll(int left, int top, int right, int bottom);
|
|
|
|
|
2016-08-15 18:32:48 +00:00
|
|
|
void SetMouseCursorBusy(bool busy);
|
2010-01-21 01:38:13 +00:00
|
|
|
void SetMouseCursor(CursorID cursor, PaletteID pal);
|
2007-02-24 15:05:14 +00:00
|
|
|
void SetAnimatedMouseCursor(const AnimCursor *table);
|
2007-03-07 11:47:46 +00:00
|
|
|
void CursorTick();
|
2010-08-05 19:23:19 +00:00
|
|
|
void UpdateCursorSize();
|
2022-08-20 09:02:31 +00:00
|
|
|
void UpdateRouteStepSpriteSize();
|
2004-08-09 17:04:08 +00:00
|
|
|
bool ChangeResInGame(int w, int h);
|
2019-04-12 16:46:49 +00:00
|
|
|
void SortResolutions();
|
2008-01-01 14:20:48 +00:00
|
|
|
bool ToggleFullScreen(bool fs);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-01 01:24:44 +00:00
|
|
|
/* gfx.cpp */
|
2024-01-07 16:41:53 +00:00
|
|
|
byte GetCharacterWidth(FontSize size, char32_t key);
|
2009-12-20 16:42:35 +00:00
|
|
|
byte GetDigitWidth(FontSize size = FS_NORMAL);
|
2013-06-01 15:10:32 +00:00
|
|
|
void GetBroadestDigit(uint *front, uint *next, FontSize size = FS_NORMAL);
|
2024-01-07 16:41:53 +00:00
|
|
|
uint64_t GetBroadestDigitsValue(uint count, FontSize size = FS_NORMAL);
|
2006-05-09 13:23:04 +00:00
|
|
|
|
2016-01-01 21:47:34 +00:00
|
|
|
extern int font_height_cache[FS_END];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get height of a character for a given font size.
|
|
|
|
* @param size Font size to get height of
|
|
|
|
* @return Height of characters in the given font (pixels)
|
|
|
|
*/
|
|
|
|
inline int GetCharacterHeight(FontSize size)
|
|
|
|
{
|
|
|
|
return font_height_cache[size];
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2024-01-18 00:00:16 +00:00
|
|
|
/* Scoped temporary screen pitch override */
|
|
|
|
struct TemporaryScreenPitchOverride {
|
|
|
|
int old_pitch;
|
|
|
|
|
|
|
|
TemporaryScreenPitchOverride(int new_pitch) : old_pitch(_screen.pitch)
|
|
|
|
{
|
|
|
|
_screen.pitch = new_pitch;
|
|
|
|
}
|
|
|
|
|
|
|
|
~TemporaryScreenPitchOverride()
|
|
|
|
{
|
|
|
|
_screen.pitch = this->old_pitch;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-12-23 10:56:02 +00:00
|
|
|
#endif /* GFX_FUNC_H */
|