Browse Source

Texture format autoselection.

* Frontend now selects the texture format based on what the renderer
      supports.
    * Corrected some file header comments.
    * Added a function to retrieve the pixel format from the video
      implementation.
    * Various cleanup/clang-tidy appeasement.
master
Ian Burgmyer 4 years ago
parent
commit
70b97d01e0
  1. 11
      libplip/PlipVideo.h
  2. 103
      plip-sdl/SdlWindow.cpp
  3. 14
      plip-sdl/SdlWindow.h

11
libplip/PlipVideo.h

@ -8,8 +8,19 @@
#include <string>
namespace Plip {
enum class PlipVideoFormat {
Unknown,
RGB888,
BGR888,
ARGB8888,
ABGR8888,
RGBA8888,
BGRA8888
};
class PlipVideo {
public:
virtual PlipVideoFormat GetFormat() = 0;
virtual void Resize(int width, int height) = 0;
virtual void SetTitle(std::string title) = 0;

103
plip-sdl/SdlWindow.cpp

@ -1,6 +1,6 @@
/* SdlWindow.cpp
*
* Describe this file here.
* Implements a SDL2 rendering window.
*/
#include <sstream>
@ -10,7 +10,7 @@
#include "SdlWindow.h"
namespace PlipSdl {
SdlWindow::SdlWindow(int scale, std::string title) {
SdlWindow::SdlWindow(int scale, const std::string &title) {
std::stringstream error;
m_scale = scale;
@ -21,7 +21,7 @@ namespace PlipSdl {
m_width * m_scale, m_height * m_scale, 0);
if(m_window == nullptr) {
error << "Unable to create SDL window: " << SDL_GetError() << "\n";
error << "Unable to create SDL window: " << SDL_GetError();
throw Plip::PlipVideoException(error.str().c_str());
}
@ -29,19 +29,33 @@ namespace PlipSdl {
m_renderer = SDL_CreateRenderer(m_window, -1, 0);
if(m_renderer == nullptr) {
error << "Unable to create SDL renderer: " << SDL_GetError() << "\n";
error << "Unable to create SDL renderer: " << SDL_GetError();
throw Plip::PlipVideoException(error.str().c_str());
}
// Try to create a small starting texture. This will be removed and
// recreated later, but we want to ensure that we can make one in the
// first place.
m_texture = SDL_CreateTexture(m_renderer, 0, 0, m_width, m_height);
SDL_RendererInfo rendererInfo;
if(SDL_GetRendererInfo(m_renderer, &rendererInfo)) {
error << "Unable to retrieve SDL renderer info: " << SDL_GetError();
throw Plip::PlipVideoException(error.str().c_str());
}
if(m_texture == nullptr) {
error << "Unable to create SDL texture: " << SDL_GetError() << "\n";
auto found = false;
for(auto i = 0; i < rendererInfo.num_texture_formats; i++) {
if(SelectFormat(rendererInfo.texture_formats[i])) {
found = true;
break;
}
}
if(!found) {
error << "No supported texture formats!";
throw Plip::PlipVideoException(error.str().c_str());
}
// Try to create a small starting texture. This will be removed and
// recreated later, but we want to ensure that we can make one in the
// first place.
CreateTexture();
}
SdlWindow::~SdlWindow() {
@ -50,6 +64,47 @@ namespace PlipSdl {
if(m_window != nullptr) SDL_DestroyWindow(m_window);
}
void SdlWindow::CreateTexture() {
if(m_texture != nullptr) SDL_DestroyTexture(m_texture);
uint32_t pixelFormat;
switch(m_format) {
case Plip::PlipVideoFormat::RGB888:
pixelFormat = SDL_PIXELFORMAT_RGB888;
break;
case Plip::PlipVideoFormat::BGR888:
pixelFormat = SDL_PIXELFORMAT_BGR888;
break;
case Plip::PlipVideoFormat::ARGB8888:
pixelFormat = SDL_PIXELFORMAT_ARGB8888;
break;
case Plip::PlipVideoFormat::ABGR8888:
pixelFormat = SDL_PIXELFORMAT_ABGR8888;
break;
case Plip::PlipVideoFormat::RGBA8888:
pixelFormat = SDL_PIXELFORMAT_RGBA8888;
break;
case Plip::PlipVideoFormat::BGRA8888:
pixelFormat = SDL_PIXELFORMAT_BGRA8888;
break;
}
m_texture = nullptr;
m_texture = SDL_CreateTexture(m_renderer,
pixelFormat, SDL_TEXTUREACCESS_STREAMING,
m_width, m_height);
if(m_texture == nullptr) {
std::stringstream error;
error << "Unable to create SDL texture: " << SDL_GetError();
throw Plip::PlipVideoException(error.str().c_str());
}
}
Plip::PlipVideoFormat SdlWindow::GetFormat() {
return m_format;
}
void SdlWindow::Resize(int width, int height) {
m_width = width;
m_height = height;
@ -58,8 +113,32 @@ namespace PlipSdl {
SDL_SetWindowSize(m_window, m_width * m_scale, m_height * m_scale);
// Destroy and recreate the texture.
SDL_DestroyTexture(m_texture);
SDL_CreateTexture(m_renderer, 0, 0, m_width, m_height);
CreateTexture();
}
bool SdlWindow::SelectFormat(uint32_t format) {
switch(format) {
case SDL_PIXELFORMAT_RGB888:
m_format = Plip::PlipVideoFormat::RGB888;
return true;
case SDL_PIXELFORMAT_BGR888:
m_format = Plip::PlipVideoFormat::BGR888;
return true;
case SDL_PIXELFORMAT_ARGB8888:
m_format = Plip::PlipVideoFormat::ARGB8888;
return true;
case SDL_PIXELFORMAT_ABGR8888:
m_format = Plip::PlipVideoFormat::ABGR8888;
return true;
case SDL_PIXELFORMAT_RGBA8888:
m_format = Plip::PlipVideoFormat::RGBA8888;
return true;
case SDL_PIXELFORMAT_BGRA8888:
m_format = Plip::PlipVideoFormat::BGRA8888;
return true;
default:
return false;
}
}
void SdlWindow::SetScale(int scale) {

14
plip-sdl/SdlWindow.h

@ -1,6 +1,6 @@
/* SdlWindow.h
*
* Describe this file here.
* Implements a SDL2 rendering window.
*/
#pragma once
@ -12,14 +12,18 @@
namespace PlipSdl {
class SdlWindow : Plip::PlipVideo {
public:
SdlWindow(int scale = 1, std::string title = "");
explicit SdlWindow(int scale = 1, const std::string &title = "");
~SdlWindow();
void Resize(int width, int height);
Plip::PlipVideoFormat GetFormat() override;
void Resize(int width, int height) override;
void SetScale(int scale);
void SetTitle(std::string title);
void SetTitle(std::string title) override;
private:
void CreateTexture();
bool SelectFormat(uint32_t format);
const int m_init_width = 64;
const int m_init_height = 64;
@ -30,6 +34,8 @@ namespace PlipSdl {
SDL_Renderer *m_renderer = nullptr;
SDL_Texture *m_texture = nullptr;
Plip::PlipVideoFormat m_format = Plip::PlipVideoFormat::Unknown;
int m_scale;
};
}

Loading…
Cancel
Save