This commit is contained in:
2018-07-31 10:50:06 -04:00
parent c040135305
commit 58fa6f0503
45 changed files with 504 additions and 295 deletions
@@ -12,6 +12,7 @@ using UtilityEngine::cUtility;
//private:
cRenderer::cRenderer()
: mp_renderer(nullptr), mp_rendererSoftware(nullptr)
{
Setup();
}
@@ -42,7 +43,11 @@ const bool cRenderer::Setup()
if (cVideo::Inst().IsInit() == false)
cUtility::Inst().Message("Unable to setup until SDL_Video is initialized.");
else {
CleanUp();
mp_renderer = cVideo::Inst().CreateRender();
SDL_Surface* surface = SDL_GetWindowSurface(cVideo::Inst().getWindow());
mp_rendererSoftware = SDL_CreateSoftwareRenderer(surface);
//mp_rendererCopy = cVideo::Inst().CreateRender();
if (mp_renderer != nullptr)
rtn = true;
}
@@ -51,8 +56,13 @@ const bool cRenderer::Setup()
void cRenderer::CleanUp()
{
SDL_DestroyRenderer(mp_renderer);
if (mp_renderer != nullptr)
SDL_DestroyRenderer(mp_renderer);
if (mp_rendererSoftware != nullptr)
SDL_DestroyRenderer(mp_rendererSoftware);
mp_renderer = nullptr;
mp_rendererSoftware = nullptr;
}
void cRenderer::Render( SDL_Texture* texture, SDL_Rect* srcrect, SDL_Rect* dstrect )
@@ -111,24 +121,12 @@ void cRenderer::RenderDrawRect( const SDL_Rect& rect, SDL_Texture* dst /*= nullp
void cRenderer::ScreenShot( const cString& filename, const cString& dir /*= ""*/ )
{
SDL_Surface* infoSurface = SDL_GetWindowSurface(VideoEngine::cVideo::Inst().getWindow());
SDL_Surface* saveSurface = NewSurface();
if (infoSurface == nullptr) {
cUtility::Inst().Message("Failed to create info surface from window.\n", __AT__, cUtility::eTypeSDL::SDL);
} else {
if (SDL_RenderReadPixels(mp_renderer, &infoSurface->clip_rect, infoSurface->format->format, saveSurface->pixels, infoSurface->w * infoSurface->format->BytesPerPixel) != 0)
cUtility::Inst().Message("Failed to read pixel data from SDL_Renderer object.\n", __AT__, cUtility::eTypeSDL::SDL);
else
SaveSurface(saveSurface, filename, dir);
SDL_FreeSurface(infoSurface);
infoSurface = nullptr;
}
SDL_FreeSurface(saveSurface);
saveSurface = nullptr;
SDL_Surface *sshot = NewSurface();
SDL_RenderReadPixels(getRenderer(), nullptr, SDL_PIXELFORMAT_RGBA32, sshot->pixels, sshot->pitch);
SaveSurface(sshot, filename, dir);
SDL_FreeSurface(sshot);
}
void cRenderer::SaveSurface( SDL_Surface* surface, const cString& fileName, const cString& dir /*= ""*/ )
{
cString temp = dir + fileName;
@@ -169,57 +167,27 @@ SDL_Surface* cRenderer::TextureToSurface( SDL_Texture* texture )
rtn = NewSurface(w, h);
void* srcPixels = nullptr;
int srcPitch = 0;
SDL_Rect rec = { 0, 0, w, h };
SDL_Texture* tmp_texture = NewTexture(w, h, true); // Create a new texture with Software Renderer
CopyTexture(texture, tmp_texture, true); // Copy old texture to the Software Renderer Texture renderer
Render(tmp_texture, nullptr, mp_rendererSoftware, &rec); // Render to the mp_rendererSoftware
//Lock texture for manipulation
if (SDL_LockTexture( texture, nullptr, &srcPixels, &srcPitch ) < 0)
cUtility::Inst().Message("Unable to lock texture. SDL_LockTexture():", __AT__, cUtility::eTypeSDL::SDL);
if (SDL_LockSurface(rtn))
cUtility::Inst().Message("Unable to lock surface rtn. SDL_LockSurface():", __AT__, cUtility::eTypeSDL::SDL);
//Copy loaded/formatted surface pixels
memcpy( rtn->pixels, srcPixels, srcPitch * h );
//Unlock texture to update
SDL_UnlockSurface(rtn);
SDL_UnlockTexture( texture );
srcPixels = nullptr;
SDL_RenderReadPixels(mp_rendererSoftware, nullptr, SDL_PIXELFORMAT_RGBA32, rtn->pixels, rtn->pitch);
return rtn;
}
void cRenderer::CopyTexture( SDL_Texture* src, SDL_Texture* dst )
void cRenderer::CopyTexture( SDL_Texture* src, SDL_Texture* dst, bool software /*= false*/ )
{
void* srcPixels = nullptr;
int srcPitch = 0;
void* dstPixels = nullptr;
int dstPitch = 0;
int height = 0;
if (SDL_QueryTexture( src, nullptr, nullptr, nullptr, &height ) < 0)
cUtility::Inst().Message("Unable to query texture. SDL_QueryTexture():", __AT__, cUtility::eTypeSDL::SDL);
//Lock texture for manipulation
if (SDL_LockTexture( src, nullptr, &srcPixels, &srcPitch ) < 0)
cUtility::Inst().Message("Unable to lock texture src. SDL_LockTexture():", __AT__, cUtility::eTypeSDL::SDL);
if (SDL_LockTexture( dst, nullptr, &dstPixels, &dstPitch ) < 0)
cUtility::Inst().Message("Unable to lock texture dst. SDL_LockTexture():", __AT__, cUtility::eTypeSDL::SDL);
//Copy loaded/formatted surface pixels
memcpy( dstPixels, srcPixels, srcPitch * height );
//Unlock texture to update
SDL_UnlockTexture( dst );
SDL_UnlockTexture( src );
srcPixels = nullptr;
dstPixels = nullptr;
SDL_Renderer* render = mp_renderer;
if (software == true)
render = mp_rendererSoftware;
if (SDL_SetRenderTarget(render, dst) < 0)
cUtility::Inst().Message("Unable to set render target to dst. SDL_SetRenderTarget():", __AT__, cUtility::eTypeSDL::SDL);
Render(src, nullptr, render, nullptr);
ResetRenderTarget();
}
SDL_Surface* cRenderer::CopySurface( SDL_Surface* src )
@@ -242,19 +210,72 @@ SDL_Surface* cRenderer::NewSurface( long int width /*= -1*/, long int height /*=
return rtn;
}
SDL_Texture* cRenderer::NewTexture( long int width, long int height, const unsigned long int access /*= SDL_TEXTUREACCESS_TARGET*/ ) const
SDL_Texture* cRenderer::NewTexture( long int width, long int height, const bool copyRenderer /*= false*/, const unsigned long int access /*= SDL_TEXTUREACCESS_TARGET*/ ) const
{
if (width < 0)
width = cVideo::Inst().getWidth();
if (height < 0)
height = cVideo::Inst().getHeight();
SDL_Texture* rtn = SDL_CreateTexture(mp_renderer, SDL_PIXELFORMAT_ABGR8888, access, width, height);
SDL_Renderer* renderer = nullptr;
if (copyRenderer == false)
renderer = mp_renderer;
else
renderer = mp_rendererSoftware;
SDL_Texture* rtn = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ABGR8888, access, width, height);
if (rtn == nullptr)
cUtility::Inst().Message("Unable to create texture. SDL_CreateTexture:", __AT__, cUtility::eTypeSDL::SDL);
return rtn;
}
const bool cRenderer::AreEqual(SDL_Surface& one, SDL_Surface& two)
{
bool rtn = false;
int x = 0;
int y = 0;
if (SDL_MUSTLOCK(&one) > 0)
SDL_LockSurface(&one);
if (SDL_MUSTLOCK(&two) > 0)
SDL_LockSurface(&two);
if ((one.pitch == two.pitch) && (one.h == two.h)) {
unsigned char *one_pixels = (unsigned char *)one.pixels;
unsigned char *two_pixels = (unsigned char *)two.pixels;
rtn = true; // we are equal so far....
for (x = 0; x < one.pitch; x++) {
for (y = 0; y < one.h; y++) {
if (one_pixels[y + x] != two_pixels[y + x]) {
rtn = false;
x = one.pitch + two.pitch + 1; // break out of the top for loop
y = one.h + two.h + 1;
break; // we found a pixel that is not equal no need to continue
}
}
}
}
if (SDL_MUSTLOCK(&one) > 0)
SDL_UnlockSurface(&one);
if (SDL_MUSTLOCK(&two) > 0)
SDL_UnlockSurface(&two);
return rtn;
}
void cRenderer::SetRenderTarget(SDL_Renderer* renderer, SDL_Texture* target /*= nullptr*/)
{
if (SDL_SetRenderTarget(renderer, target) < 0)
cUtility::Inst().Message("Unable to set render target to dst. SDL_SetRenderTarget():", __AT__, cUtility::eTypeSDL::SDL);
}
void cRenderer::SetRenderTarget( SDL_Texture* target /*= nullptr*/ )
{
if (SDL_SetRenderTarget(mp_renderer, target) < 0)
@@ -271,3 +292,8 @@ SDL_Renderer* cRenderer::getRenderer()
{
return mp_renderer;
}
SDL_Renderer* cRenderer::getRendererCopy()
{
return mp_rendererSoftware;
}