OpenTTD Source: OpenGLBackend Class Reference

OpenTTD Source 13.2.1

Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes
OpenGLBackend Class Reference

Platform-independent back-end class for OpenGL video drivers. More...

`#include <opengl.h>`

Inheritance diagram for OpenGLBackend:

## Public Member Functions
void | PrepareContext ()
std::string | GetDriverName ()
void | UpdatePalette (const Colour *pal, uint first, uint length)
| Update the stored palette. More...
bool | Resize (int w, int h, bool force=false)
| Change the size of the drawing window and allocate matching resources. More...
void | Paint ()
| Render video buffer to the screen.
void | DrawMouseCursor ()
| Draw mouse cursor on screen.
void | PopulateCursorCache ()
void | ClearCursorCache ()
| Queue a request for cursor cache clear.
void * | GetVideoBuffer ()
| Get a pointer to the memory for the video driver to draw to. More...
uint8 * | GetAnimBuffer ()
| Get a pointer to the memory for the separate animation buffer. More...
void | ReleaseVideoBuffer (const Rect &update_rect)
| Update video buffer texture after the video buffer was filled. More...
void | ReleaseAnimBuffer (const Rect &update_rect)
| Update animation buffer texture after the animation buffer was filled. More...
bool | Is32BppSupported () override
| Can the sprite encoder make use of RGBA sprites?
uint | GetSpriteAlignment () override
| Get the value which the height and width on a sprite have to be aligned by. More...
Sprite * | Encode (const SpriteLoader::Sprite *sprite, AllocatorProc *allocator) override
| Convert a sprite from the loader to our own format.
Public Member Functions inherited from ZeroedMemoryAllocator
void * | operator new (size_t size)
| Memory allocator for a single class instance. More...
void * | operator new[] (size_t size)
| Memory allocator for an array of class instances. More...
void | operator delete (void *ptr)
| Memory release for a single class instance. More...
void | operator delete[] (void *ptr)
| Memory release for an array of class instances. More...

## Static Public Member Functions
static OpenGLBackend * | Get ()
| Get singleton instance of this class.
static const char * | Create (GetOGLProcAddressProc get_proc, const Dimension &screen_res)
| Create and initialize the singleton back-end class. More...
static void | Destroy ()
| Free resources and destroy singleton back-end class.

## Private Member Functions
| OpenGLBackend ()
| Construct OpenGL back-end class.
| ~OpenGLBackend ()
| Free allocated resources.
const char * | Init (const Dimension &screen_res)
| Check for the needed OpenGL functionality and allocate all resources. More...
bool | InitShaders ()
| Create all needed shader programs. More...
void | InternalClearCursorCache ()
| Clear all cached cursor sprites.
void | RenderOglSprite (OpenGLSprite *gl_sprite, PaletteID pal, int x, int y, ZoomLevel zoom)
| Render a sprite to the back buffer. More...

## Private Attributes
bool | persistent_mapping_supported
| Persistent pixel buffer mapping supported.
GLsync | sync_vid_mapping
| Sync object for the persistently mapped video buffer.
GLsync | sync_anim_mapping
| Sync object for the persistently mapped animation buffer.
void * | vid_buffer
| Pointer to the mapped video buffer.
GLuint | vid_pbo
| Pixel buffer object storing the memory used for the video driver to draw to.
GLuint | vid_texture
| Texture handle for the video buffer texture.
GLuint | vid_program
| Shader program for rendering a RGBA video buffer.
GLuint | pal_program
| Shader program for rendering a paletted video buffer.
GLuint | vao_quad
| Vertex array object storing the rendering state for the fullscreen quad.
GLuint | vbo_quad
| Vertex buffer with a fullscreen quad.
GLuint | pal_texture
| Palette lookup texture.
void * | anim_buffer
| Pointer to the mapped animation buffer.
GLuint | anim_pbo
| Pixel buffer object storing the memory used for the animation buffer.
GLuint | anim_texture
| Texture handle for the animation buffer texture.
GLuint | remap_program
| Shader program for blending and rendering a RGBA + remap texture.
GLint | remap_sprite_loc
| Uniform location for sprite parameters.
GLint | remap_screen_loc
| Uniform location for screen size;.
GLint | remap_zoom_loc
| Uniform location for sprite zoom;.
GLint | remap_rgb_loc
| Uniform location for RGB mode flag;.
GLuint | sprite_program
| Shader program for blending and rendering a sprite to the video buffer.
GLint | sprite_sprite_loc
| Uniform location for sprite parameters.
GLint | sprite_screen_loc
| Uniform location for screen size;.
GLint | sprite_zoom_loc
| Uniform location for sprite zoom;.
GLint | sprite_rgb_loc
| Uniform location for RGB mode flag;.
GLint | sprite_crash_loc
| Uniform location for crash remap mode flag;.
LRUCache< SpriteID, Sprite > | cursor_cache
| Cache of encoded cursor sprites.
PaletteID | last_sprite_pal = (PaletteID)-1
| Last uploaded remap palette.
bool | clear_cursor_cache = false
| A clear of the cursor cache is pending.
Point | cursor_pos
| Cursor position.
bool | cursor_in_window
| Cursor inside this window.
PalSpriteID | cursor_sprite_seq [16]
| Current image of cursor.
Point | cursor_sprite_pos [16]
| Relative position of individual cursor sprites.
uint | cursor_sprite_count
| Number of cursor sprites to draw.

## Static Private Attributes
static OpenGLBackend * | instance = nullptr
| Singleton instance pointer.

## Detailed Description

Platform-independent back-end class for OpenGL video drivers.

Definition at line 28 of file opengl.h.

## Member Function Documentation

## ◆ Create()

const char * OpenGLBackend::Create | ( | GetOGLProcAddressProc | get_proc,
|  | const Dimension & | screen_res
| ) |  |
| static

Create and initialize the singleton back-end class.

Parameters

  get_proc | Callback to get an OpenGL function from the OS driver.
  screen_res | Current display resolution.

Returns
  nullptr on success, error message otherwise.

Definition at line 469 of file opengl.cpp.

## ◆ GetAnimBuffer()

uint8 * OpenGLBackend::GetAnimBuffer | ( |  | ) |

Get a pointer to the memory for the separate animation buffer.

Returns
  Pointer to draw on.

Definition at line 1181 of file opengl.cpp.

References anim_buffer, anim_pbo, persistent_mapping_supported, and sync_anim_mapping.

Referenced by VideoDriver_SDL_OpenGL::GetVideoPointer().

## ◆ GetSpriteAlignment()

uint OpenGLBackend::GetSpriteAlignment | ( |  | ) |
| inlineoverridevirtual

Get the value which the height and width on a sprite have to be aligned by.

Returns
  The needed alignment or 0 if any alignment is accepted.

Reimplemented from SpriteEncoder.

Definition at line 111 of file opengl.h.

References ZOOM_LVL_COUNT.

## ◆ GetVideoBuffer()

void * OpenGLBackend::GetVideoBuffer | ( |  | ) |

Get a pointer to the memory for the video driver to draw to.

Returns
  Pointer to draw on.

Definition at line 1159 of file opengl.cpp.

References persistent_mapping_supported, sync_vid_mapping, vid_buffer, and vid_pbo.

Referenced by VideoDriver_SDL_OpenGL::GetVideoPointer().

## ◆ Init()

const char * OpenGLBackend::Init | ( | const Dimension & | screen_res | ) |
| private

Check for the needed OpenGL functionality and allocate all resources.

Parameters

  screen_res | Current display resolution.

Returns
  Error string or nullptr if successful.

Definition at line 527 of file opengl.cpp.

References anim_pbo, anim_texture, Debug, InitShaders(), IsOpenGLVersionAtLeast(), pal_program, pal_texture, persistent_mapping_supported, remap_program, remap_rgb_loc, remap_screen_loc, remap_sprite_loc, remap_zoom_loc, sprite_crash_loc, sprite_program, sprite_rgb_loc, sprite_screen_loc, sprite_sprite_loc, sprite_zoom_loc, vao_quad, vbo_quad, vid_pbo, vid_program, and vid_texture.

## ◆ InitShaders()

bool OpenGLBackend::InitShaders | ( |  | ) |
| private

Create all needed shader programs.

Returns
  True if successful, false otherwise.

Definition at line 806 of file opengl.cpp.

References _frag_shader_direct, _frag_shader_direct_150, _frag_shader_palette, _frag_shader_palette_150, _frag_shader_rgb_mask_blend, _frag_shader_rgb_mask_blend_150, _frag_shader_sprite_blend, _frag_shader_sprite_blend_150, _vertex_shader_sprite, _vertex_shader_sprite_150, IsOpenGLVersionAtLeast(), lengthof, pal_program, remap_program, sprite_program, and vid_program.

Referenced by Init().

## ◆ ReleaseAnimBuffer()

void OpenGLBackend::ReleaseAnimBuffer | ( | const Rect & | update_rect | ) |

Update animation buffer texture after the animation buffer was filled.

Parameters

  update_rect | Rectangle encompassing the dirty region of the animation buffer.

Definition at line 1246 of file opengl.cpp.

References anim_buffer, anim_pbo, anim_texture, persistent_mapping_supported, and sync_anim_mapping.

Referenced by VideoDriver_SDL_OpenGL::ReleaseVideoPointer().

## ◆ ReleaseVideoBuffer()

void OpenGLBackend::ReleaseVideoBuffer | ( | const Rect & | update_rect | ) |

Update video buffer texture after the video buffer was filled.

Parameters

  update_rect | Rectangle encompassing the dirty region of the video buffer.

Definition at line 1204 of file opengl.cpp.

References IsEmptyRect(), persistent_mapping_supported, sync_vid_mapping, vid_buffer, vid_pbo, and vid_texture.

Referenced by VideoDriver_SDL_OpenGL::ReleaseVideoPointer().

## ◆ RenderOglSprite()

void OpenGLBackend::RenderOglSprite | ( | OpenGLSprite * | gl_sprite,
|  | PaletteID | pal,
|  | int | x,
|  | int | y,
|  | ZoomLevel | zoom
| ) |  |
| private

Render a sprite to the back buffer.

Parameters

  gl_sprite | Sprite to render.
  x | X position of the sprite.
  y | Y position of the sprite.
  zoom | Zoom level to use.

Definition at line 1304 of file opengl.cpp.

## ◆ Resize()

bool OpenGLBackend::Resize | ( | int | w,
|  | int | h,
|  | bool | force = `false`
| ) |  |

Change the size of the drawing window and allocate matching resources.

Parameters

  w | New width of the window.
  h | New height of the window.
  force | Recreate resources even if size didn't change.
  False | if nothing had to be done, true otherwise.

Definition at line 915 of file opengl.cpp.

## ◆ UpdatePalette()

void OpenGLBackend::UpdatePalette | ( | const Colour * | pal,
|  | uint | first,
|  | uint | length
| ) |  |

Update the stored palette.

Parameters

  pal | Palette array with at least 256 elements.
  first | First entry to update.
  length | Number of entries to update.

Definition at line 1030 of file opengl.cpp.

References pal_texture.

Referenced by VideoDriver_SDL_Default::Paint(), and VideoDriver_SDL_OpenGL::Paint().

The documentation for this class was generated from the following files:

- src/video/opengl.h
- src/video/opengl.cpp