Home2L - C/C++ API  v1.2-2-ga4fe (2023-04-15)
Smart Tools for a Private Home
Classes | Macros
ui_base.H File Reference
#include <SDL.h>
#include <SDL_ttf.h>
#include "env.H"
Include dependency graph for ui_base.H:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  CNetpbmReader
 Reader for Netpbm image files. More...
 
class  CTextFormat
 
class  CTextSet
 Typesetter for longer formatted texts. More...
 

Macros

#define UI_RES_X   1024
 UI resolution in pixels (X)
 
#define UI_RES_Y   600
 UI resolution in pixels (Y)
 
Color palette ...
#define WHITE   ToColor (0xff, 0xff, 0xff)
 
#define LIGHT_GREY   ToColor (0xc0, 0xc0, 0xc0)
 
#define GREY   ToColor (0x80, 0x80, 0x80)
 
#define DARK_GREY   ToColor (0x40, 0x40, 0x40)
 
#define DARK_DARK_GREY   ToColor (0x20, 0x20, 0x20)
 
#define BLACK   ToColor (0x00, 0x00, 0x00)
 
#define RED   ToColor (0xff, 0x00, 0x00)
 
#define GREEN   ToColor (0x00, 0xff, 0x00)
 
#define BLUE   ToColor (0x00, 0x00, 0xff)
 
#define CYAN   ToColor (0x00, 0xff, 0xff)
 
#define MAGENTA   ToColor (0xff, 0x00, 0xff)
 
#define YELLOW   ToColor (0xff, 0xff, 0x00)
 
#define DARK_RED   ToColor (0x80, 0x00, 0x00)
 
#define DARK_GREEN   ToColor (0x00, 0x80, 0x00)
 
#define DARK_BLUE   ToColor (0x00, 0x00, 0x80)
 
#define DARK_CYAN   ToColor (0x00, 0x80, 0x80)
 
#define DARK_MAGENTA   ToColor (0x80, 0x00, 0x80)
 
#define DARK_YELLOW   ToColor (0x80, 0x80, 0x00)
 
#define LIGHT_RED   ToColor (0xff, 0x80, 0x80)
 
#define LIGHT_GREEN   ToColor (0x80, 0xff, 0x80)
 
#define LIGHT_BLUE   ToColor (0x80, 0x80, 0xff)
 
#define LIGHT_CYAN   ToColor (0x80, 0xff, 0xff)
 
#define LIGHT_MAGENTA   ToColor (0xff, 0x80, 0xff)
 
#define LIGHT_YELLOW   ToColor (0xff, 0xff, 0x80)
 
#define BROWN   ToColor (0x80, 0x40, 0x00)
 
#define LIGHT_BROWN   ToColor (0xc0, 0x80, 0x40)
 
#define ORANGE   ToColor (0xc0, 0x40, 0x00)
 
#define TRANSPARENT   ToColor (0x00, 0x00, 0x00, 0x00)
 

Functions

Geometry helpers ...
static SDL_Rect Rect (int x, int y, int w, int h)
 
static SDL_Rect Rect (int w, int h)
 
static SDL_Rect Rect (SDL_Surface *surf)
 
static SDL_Rect Rect (SDL_Texture *tex)
 
static SDL_Rect RectScreen ()
 Rectangle representing the whole UI screen.
 
static void RectMove (SDL_Rect *rect, int dx, int dy)
 
static void RectGrow (SDL_Rect *rect, int dx, int dy)
 
void RectAlign (SDL_Rect *rect, SDL_Rect container, int hAlign=-1, int vAlign=-1)
 Align the rectangle 'rect' with an outer rectangle 'container'. More...
 
static void RectCenter (SDL_Rect *rect, SDL_Rect container)
 Center the rectangle 'rect' with respect to an outer rectangle 'container'.
 
static bool RectContains (SDL_Rect *rect, int x, int y)
 
Texture helpers ...
static SDL_Texture * CreateTexture (SDL_Surface *surf)
 
static void TextureSet (SDL_Texture **pTex, SDL_Texture *newTex)
 
static void TextureFree (SDL_Texture **pTex)
 
static void TextureRender (SDL_Texture *tex, SDL_Rect *srcRect=NULL, SDL_Rect *dstRect=NULL)
 
Icon handling ...
SDL_Surface * IconGet (const char *name, TColor color=WHITE, TColor bgColor=TRANSPARENT, int scaleDown=1, int orient=0, bool preserveThinLines=false)
 Get a reference to an icon from the applications share/icons/ folder. More...
 
Complex text formatting ...

See also: CTextFormat, CTextSet

SDL_Surface * TextRender (const char *text, CTextFormat fmt, SDL_Surface *dst=NULL, SDL_Rect *dstRect=NULL, bool *retAbbreviated=NULL)
 Easy-to-use function to typeset a text with a single format.
 

SDL audio ...

Audio playing functionality. The following functions are not thread-safe and must be called from the main thread, which is the one executing timers.

#define AUDIO_FOREVER   -1
 Constant to specify an infinite number of repetitions.
 
void AudioStart (const char *fileName, int repetitions=1, TTicks repetitionGap=0)
 Start playing audio. More...
 
void AudioStop ()
 Stop playing audio immediately.
 
bool AudioIsPlaying ()
 Query if audio is still playing. If 'false' is returned, the audio device is surely in a stopped state.
 

Color helpers ...

#define SELECTED_SDL_PIXELFORMAT   SDL_PIXELFORMAT_ARGB8888
 Selected (SDL) pixel format. More...
 
#define COL_MASK_R   0x00FF0000
 
#define COL_MASK_G   0x0000FF00
 
#define COL_MASK_B   0x000000FF
 
#define COL_MASK_A   0xFF000000
 
#define COL_MASK_RGB   (COL_MASK_R | COL_MASK_G | COL_MASK_B)
 
static TColor ToColor (Uint8 r, Uint8 g, Uint8 b, Uint8 a=255)
 
static TColor ToColor (Uint32 val)
 
static Uint32 ToUint32 (TColor color)
 
static Uint32 ToUint32 (Uint8 r, Uint8 g, Uint8 b, Uint8 a=255)
 
static TColor ToColor (SDL_Color sdlCol)
 
static bool operator== (const TColor &col1, const TColor &col2)
 
static bool operator!= (const TColor &col1, const TColor &col2)
 
static SDL_Color ToSDL_Color (TColor color)
 
TColor ColorSum (TColor col1, TColor col2)
 Add two colors (with saturation).
 
TColor ColorBrighter (TColor color, int d)
 Increase all color components by d with saturation; d may be negative.
 
static TColor ColorDarker (TColor color, int d)
 Decreas all color components by d with saturation; d may be negative.
 
TColor ColorScale (TColor color, int factor)
 Multiply all components with a factor. 'factor' is a fixed point number with 8 fractional bits (1.0 == 0x100).
 
TColor ColorBlend (TColor color0, TColor color1, int weight1)
 Blend two colors; 'weight' is a fixed point number with 8 fractional bits (1.0 == 0x100) and refers to 'color1'.
 

Surface helpers ...

See also: CNetpbmReader

#define ORIENT(FLIPH, ROT)   (((FLIPH) ? 4 : 0) | ((ROT) & 3))
 Get orientation identifier based on horizontal flipping and a rotation in 90 degree steps clock-wise.
 
#define ORIENT_WITH_FLIPV(FLIPH, ROT)   (((FLIPH) ? 0 : 4) | (((ROT) + 2) & 3))
 Get orientation identifier with implied vertical flipping first.
 
#define ORIENT_FLIPH(ORI)   (((ORI) & 4) ? true : false)
 Get whether horizontal flipping is included in the orientation.
 
#define ORIENT_ROT(ORI)   ((ORI) & 3)
 Get the number of rotations.
 
#define NETPBM_IDLE   -3
 State code "idle" for CNetpbmReader.
 
#define NETPBM_ERROR   -2
 State code "error" for CNetpbmReader.
 
#define NETPBM_SUCCESS   -1
 State code "success" for CNetpbmReader.
 
static SDL_Surface * CreateSurface (int w, int h)
 
static SDL_Surface * CreateSurface (SDL_Rect r)
 
static SDL_Surface * CreateSurface (SDL_Rect *r)
 
static SDL_Surface * CreateSurfaceFrom (int w, int h, void *_pixels, int _pitch)
 
static SDL_Surface * SurfaceSet (SDL_Surface **pSurf, SDL_Surface *newSurf)
 
static void SurfaceFree (SDL_Surface **pSurf)
 
static void SurfaceFree (SDL_Surface *surf)
 
static SDL_Surface * SurfaceDup (SDL_Surface *src)
 
void SurfaceNormalize (SDL_Surface **pSurf)
 Normalize pixel format to SELECTED_SDL_PIXELFORMAT.
 
void SurfaceRecolor (SDL_Surface *surf, TColor color)
 Replace all RGB values with the passed color while leaving alpha values untouched.
 
SDL_Surface * SurfaceGetOpaqueCopy (SDL_Surface *surf, TColor backColor)
 Create an opaque copy; caller becomes owner of returned surface.
 
void SurfaceMakeTransparentMono (SDL_Surface *surf, Uint8 opaqueLevel=0xff)
 Copy the R values to the A channel and make all RGB values white. More...
 
void SurfaceBlit (SDL_Surface *src, SDL_Rect *srcRect, SDL_Surface *dst, SDL_Rect *dstRect=NULL, int hAlign=0, int vAlign=0, SDL_BlendMode blendMode=SDL_BLENDMODE_NONE)
 Blit a surface; It is allowed to leave 'src == NULL', in which case nothing happens. More...
 
static void SurfaceFill (SDL_Surface *surf, TColor col)
 
static void SurfaceFillRect (SDL_Surface *surf, SDL_Rect r, TColor col)
 
static void SurfaceFillRect (SDL_Surface *surf, SDL_Rect *r, TColor col)
 
SDL_Surface * SurfaceGetScaledDownCopy (SDL_Surface *surf, int factor, bool preserveThinLines=false)
 Get a copy of the surface, scaled down by a factor in both dimensions. More...
 
SDL_Surface * SurfaceGetFlippedAndRotatedCopy (SDL_Surface *surf, int orient)
 Get a copy of the surface, which is eventually flipped and then rotated in 90° steps clockwise 'surf' must conform to SELECTED_SDL_PIXELFORMAT (see SurfaceNormalize() ).
 
SDL_Surface * SurfaceReadBmp (const char *fileName)
 Read a .bmp file. More...
 

Events ...

enum  EUserEvent
 
void UiIterate (bool noWait=false)
 
static void UiIterateNoWait ()
 Iterate the application's main event loop; Does not sleep, but returns immediately ("yield"-functionality for longer running processes in the UI thread).
 
void UiQuit ()
 Quit the application.
 
bool UiIsClosed ()
 Is the application quitting?
 
void UiPushUserEvent (EUserEvent code, void *data1=NULL, void *data2=NULL)
 
static void MainThreadCallback (void(*func)(void *), void *data=NULL)
 Request function 'func' to be called by main thread at next occasion.
 

Font handling ...

enum  EFontStyle {
  fntNormal = 0 , fntBold , fntItalic , fntBoldItalic ,
  fntLight , fntMono , fntMonoBold , fntMonoItalic ,
  fntMonoBoldItalic , fntEND
}
 Supported font styles. More...
 
TTF_Font * FontGet (EFontStyle style, int size)
 Get a reference to a font of a specific style and size.
 
SDL_Surface * FontRenderText (TTF_Font *font, const char *text, TColor color)
 Render a text using 'blended' mode of the SDL2_ttf library. The returned surface is a new surface of minimal size and must later be free'd using SurfaceFree().
 
SDL_Surface * FontRenderText (TTF_Font *font, const char *text, TColor color, TColor bgColor)
 Render a text using 'shaded' mode of the SDL2_ttf library. This mode is considered faster than 'blended' mode. The returned surface is a new surface of minimal size and must later be free'd using SurfaceFree().
 
static int FontGetLineSkip (TTF_Font *font)
 
static int FontGetHeight (TTF_Font *font)
 
int FontGetWidth (TTF_Font *font, const char *text, int textLen=-1)
 

General functions ...

void UiInit (const char *windowTitle="Home2L")
 
void UiDone ()
 
static SDL_Renderer * UiGetSdlRenderer ()
 
void UiGetWindowSize (int *w, int *h)
 
void UiSetWindowSize (int w, int h)
 
bool UiGetWindowFullScreen ()
 
void UiSetWindowFullScreen (bool _fullScreen)
 
void UiToggleWindowFullScreen ()
 
bool UiGetWindowResizable ()
 
void UiSetWindowResizable (bool _resizable)
 
void UiToggleWindowResizable ()