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
@@ -7,35 +7,28 @@
using VideoEngine::cImage;
using UtilityEngine::cUtility;
cImage::cImage( const cString& filename, const cString& dir /*= ""*/, const bool transparent /*= false*/,
const unsigned char red /*= 0*/, const unsigned char blue /*= 0*/, const unsigned char green /*= 255*/,
cImage::cImage( const bool transparent /*= false*/, const unsigned char red /*= 0*/, const unsigned char blue /*= 0*/, const unsigned char green /*= 255*/,
const unsigned char translevel /*= 255*/, const bool isSurface /*= false*/ )
: mp_texture(nullptr), mp_surface(nullptr), m_dir(dir), m_fileName(filename), m_transparent(transparent),
: mp_texture(nullptr), mp_surface(nullptr), m_transparent(transparent),
m_transRed(red), m_transBlue(blue), m_transGreen(green), m_transLevel(translevel), m_isSurface(isSurface)
{
if (m_fileName != (char*)"")
LoadImage();
if (mp_texture != nullptr)
TransparentSetup();
}
{}
cImage::cImage( SDL_Surface* surface )
: mp_texture(nullptr), mp_surface(nullptr), m_dir(""), m_fileName(""), m_transparent(false),
: mp_texture(nullptr), mp_surface(nullptr), m_transparent(false),
m_transRed(0), m_transBlue(0), m_transGreen(255), m_transLevel(255)
{
setImage(surface);
}
cImage::cImage( SDL_Texture* texture )
: mp_texture(nullptr), mp_surface(nullptr), m_dir(""), m_fileName(""), m_transparent(false),
: mp_texture(nullptr), mp_surface(nullptr), m_transparent(false),
m_transRed(0), m_transBlue(0), m_transGreen(255), m_transLevel(255)
{
setImage(texture);
}
cImage::cImage( const cImage& copy, const bool surfaceCopy /*= true*/)
: mp_texture(nullptr), mp_surface(nullptr), m_dir(copy.getDir()),
m_fileName(copy.getFileName()), m_transparent(copy.getTransparent()), m_transRed(copy.getRedTrans()),
: mp_texture(nullptr), mp_surface(nullptr), m_transparent(copy.getTransparent()), m_transRed(copy.getRedTrans()),
m_transBlue(copy.getBlueTrans()), m_transGreen(copy.getGreenTrans()), m_transLevel(copy.getLevelTrans())
{
if (surfaceCopy == true)
@@ -78,23 +71,6 @@ const cImage* cImage::NewImage( SDL_Rect& area ) const
}
///Sets
void cImage::setDir( const cString& dir )
{
m_dir = dir;
}
void cImage::setFileName( const cString& filename )
{
m_fileName = filename;
LoadImage();
}
void cImage::setFileNameandDir( const cString& filename, const cString& dir /*= ""*/ )
{
setDir(dir);
setFileName(filename);
}
void cImage::setTransparent( const bool transparent /*= true*/ )
{
m_transparent = transparent;
@@ -117,8 +93,17 @@ void cImage::setLevelTrans( const unsigned char translevel /*= 255*/ )
///Gets
SDL_Surface* cImage::getSurface() const
{
return mp_surface;
//return cRenderer::Inst().TextureToSurface(mp_texture);
SDL_Surface* rtn = nullptr;
if (m_isSurface == false)
rtn = cRenderer::Inst().TextureToSurface(mp_texture);
else
rtn = mp_surface;
return rtn;
}
SDL_Texture* cImage::getTexture() const
{
return mp_texture;
}
SDL_Texture* cImage::getImage() const
@@ -165,16 +150,6 @@ void cImage::getWH( unsigned long int& w, unsigned long int& h ) const
h = height;
}
const cString cImage::getDir() const
{
return m_dir;
}
const cString cImage::getFileName() const
{
return m_fileName;
}
const bool cImage::getTransparent() const
{
return m_transparent;
@@ -217,9 +192,12 @@ void cImage::setImage( SDL_Surface* surface )
{
UnloadImage();
mp_texture = cRenderer::Inst().SurfaceToTexture(surface);
SDL_FreeSurface(surface);
if (m_isSurface == false) {
mp_texture = cRenderer::Inst().SurfaceToTexture(surface);
SDL_FreeSurface(surface);
}
else
mp_surface = surface;
}
void cImage::setImage( SDL_Texture* texture )
@@ -232,46 +210,26 @@ void cImage::setImage( SDL_Texture* texture )
///Functions
void cImage::TransparentSetup()
{
if( (m_transparent == true) && (mp_texture != nullptr) ) {
if (m_transparent == false)
return;
if( mp_texture != nullptr ) {
if ((SDL_SetTextureColorMod(mp_texture, m_transRed, m_transGreen, m_transBlue) | SDL_SetTextureAlphaMod(mp_texture, m_transLevel)) < 0)
cUtility::Inst().Message("Unable to set Transparent. SDL_SetTextureColorMod:", __AT__, cUtility::eTypeSDL::SDL);
}
//LoadImage();
}
void cImage::LoadImage()
{
UnloadImage();
SDL_Texture* tempText = nullptr;
SDL_Surface* tempSurface = nullptr;
if (m_fileName != "")
{
cString temp = m_dir + m_fileName;
if (m_isSurface == false) {
if ((tempText = IMG_LoadTexture(cRenderer::Inst().getRenderer(), temp.c_str())) == nullptr)
cUtility::Inst().Message("Unable to load necessary image file. " + temp + " IMG_LoadTexture():", __AT__, cUtility::eTypeSDL::IMAGE);
else
mp_texture = tempText;
}
else {
if ((tempSurface = IMG_Load(temp.c_str())) == nullptr)
cUtility::Inst().Message("Unable to load necessary image file. " + temp + " IMG_Load():", __AT__, cUtility::eTypeSDL::IMAGE);
else
mp_surface = tempSurface;
}
}
if ((m_isSurface == true) && (mp_surface != nullptr)) {
if ((SDL_SetSurfaceColorMod(mp_surface, m_transRed, m_transGreen, m_transBlue) | SDL_SetSurfaceAlphaMod(mp_surface, m_transLevel)) < 0)
cUtility::Inst().Message("Unable to set Transparent. SDL_SetSurfaceColorMod:", __AT__, cUtility::eTypeSDL::SDL);
}
}
void cImage::UnloadImage()
{
if (mp_texture != nullptr)
{
SDL_DestroyTexture(mp_texture);
mp_texture = nullptr;
}
}
if (mp_surface != nullptr)
SDL_FreeSurface(mp_surface);
mp_texture = nullptr;
mp_surface = nullptr;
}
@@ -17,8 +17,7 @@ namespace VideoEngine {
class EXPORT_FROM_MYDLL cImage
{
public:
cImage( const cString& filename = "", const cString& dir = "", const bool transparent = false,
const unsigned char red = 0, const unsigned char blue = 0, const unsigned char green = 255,
cImage( const bool transparent = false, const unsigned char red = 0, const unsigned char blue = 0, const unsigned char green = 255,
const unsigned char translevel = 255, const bool isSurface = false );
cImage( SDL_Surface* surface );
cImage( SDL_Texture* texture );
@@ -32,13 +31,6 @@ namespace VideoEngine {
const cImage* NewImage( SDL_Rect& area ) const;
///Sets
/* Sets the Directory of the image */
void setDir( const cString& dir );
/* Sets the File Name of the image */
void setFileName( const cString& filename );
/* Sets the File Name and the Directory of the image */
void setFileNameandDir( const cString& filename, const cString& dir = "" );
/* Sets Transparent on if set to true */
void setTransparent( const bool transparent = true );
/* Sets the colour to be made transparent */
@@ -50,6 +42,8 @@ namespace VideoEngine {
/* Gets a pointer to the SDL_Surface */
SDL_Surface* getSurface() const;
/* Gets a pointer to the SDL_Texture */
SDL_Texture* getTexture() const;
/* Gets a pointer to the SDL_Texture */
SDL_Texture* getImage() const;
/* Gets the width of the image */
const unsigned long int getWidth() const;
@@ -59,12 +53,7 @@ namespace VideoEngine {
const SDL_Rect getWH() const;
/* Gets the Width and Height of the image */
void getWH( unsigned long int& w, unsigned long int& h ) const;
/* Gets the Directory of the image */
const cString getDir() const;
/* Gets the File Name of the image */
const cString getFileName() const;
/* Gets return true if transparent on false if off */
const bool getTransparent() const;
/* Gets return the colour to be made transparent */
@@ -89,20 +78,13 @@ namespace VideoEngine {
private:
///Functions
void TransparentSetup();
virtual void LoadImage(); //cFont over rides this
void UnloadImage();
private:
///Variables
SDL_Texture* mp_texture;// = nullptr
SDL_Surface* mp_surface;// = nullptr
cString m_dir;// = ""
cString m_fileName;// = ""
//char m_dir[255];
//char m_fileName[255];
bool m_transparent;// = false
unsigned char m_transRed;// = 0
@@ -0,0 +1,78 @@
#include "cImageFile.hpp"
/*** Custom Header Files ***/
#include "cRenderer.hpp"
#include "../UtilityEngine/cUtility.hpp"
using VideoEngine::cImageFile;
using UtilityEngine::cUtility;
cImageFile::cImageFile(const cString& filename, const cString& dir /*= ""*/, const bool transparent /*= false*/,
const unsigned char red /*= 0*/, const unsigned char blue /*= 0*/, const unsigned char green /*= 255*/,
const unsigned char translevel /*= 255*/, const bool isSurface /*= false*/)
: cImage(transparent, red, blue, green, translevel, isSurface), m_dir(dir), m_fileName(filename)
{
if (m_fileName != (char*)"")
LoadImage();
}
cImageFile::cImageFile(const cImageFile& copy, const bool surfaceCopy /*= true*/)
: cImage(copy)
{}
cImageFile::~cImageFile()
{}
///Sets
void cImageFile::setDir(const cString& dir)
{
m_dir = dir;
}
void cImageFile::setFileName(const cString& filename)
{
m_fileName = filename;
LoadImage();
}
void cImageFile::setFileNameandDir(const cString& filename, const cString& dir /*= ""*/)
{
setDir(dir);
setFileName(filename);
}
const cString& cImageFile::getDir() const
{
return m_dir;
}
const cString& cImageFile::getFileName() const
{
return m_fileName;
}
///private
///Functions
void cImageFile::LoadImage()
{
SDL_Texture* tempText = nullptr;
SDL_Surface* tempSurface = nullptr;
if (m_fileName != "")
{
cString temp = m_dir + m_fileName;
if (this->getIsSurface() == false) {
if ((tempText = IMG_LoadTexture(cRenderer::Inst().getRenderer(), temp.c_str())) == nullptr)
cUtility::Inst().Message("Unable to load necessary image file. " + temp + " IMG_LoadTexture():", __AT__, cUtility::eTypeSDL::IMAGE);
else
this->setImage(tempText);
}
else {
if ((tempSurface = IMG_Load(temp.c_str())) == nullptr)
cUtility::Inst().Message("Unable to load necessary image file. " + temp + " IMG_Load():", __AT__, cUtility::eTypeSDL::IMAGE);
else
this->setImage(tempSurface);
}
}
}
@@ -0,0 +1,52 @@
#ifndef _CIMAGEFILE_HPP_
#define _CIMAGEFILE_HPP_
/*** SDL Header Files ***/
#include <SDL.h>
#include <SDL_image.h>
/*** DLL Header File ***/
#include "dllExport.h"
/*** Custom Header Files ***/
#include "cImage.hpp"
#include "../UtilityEngine/cString.hpp"
using UtilityEngine::cString;
namespace VideoEngine {
class EXPORT_FROM_MYDLL cImageFile : public cImage
{
public:
cImageFile(const cString& filename = "", const cString& dir = "", const bool transparent = false,
const unsigned char red = 0, const unsigned char blue = 0, const unsigned char green = 255,
const unsigned char translevel = 255, const bool isSurface = false);
/* Copy constructor */
cImageFile(const cImageFile& copy, const bool surfaceCopy = true);
~cImageFile();
///Sets
/* Sets the Directory of the image */
void setDir(const cString& dir);
/* Sets the File Name of the image */
void setFileName(const cString& filename);
/* Sets the File Name and the Directory of the image */
void setFileNameandDir(const cString& filename, const cString& dir = "");
///Gets
/* Gets the Directory of the image */
const cString& getDir() const;
/* Gets the File Name of the image */
const cString& getFileName() const;
private:
///Functions
void LoadImage();
private:
///Variables
cString m_dir;// = ""
cString m_fileName;// = ""
};/// END CLASS DEFINITION cImageFile
}/// END NAMESPACE DEFINITION VideoEngine
#endif/// END IFNDEF _CIMAGEFILE_HPP_
@@ -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;
}
@@ -47,23 +47,28 @@ namespace VideoEngine {
SDL_Texture* SurfaceToTexture( SDL_Surface* surface );
SDL_Surface* TextureToSurface( SDL_Texture* texture );
void CopyTexture( SDL_Texture* src, SDL_Texture* dst );
void CopyTexture( SDL_Texture* src, SDL_Texture* dst, bool software = false);
SDL_Surface* CopySurface( SDL_Surface* src );
SDL_Surface* NewSurface( long int width = -1, long int height = -1 ) const;
SDL_Texture* NewTexture( long int width = -1, long int height = -1, const unsigned long int access = SDL_TEXTUREACCESS_TARGET ) const;
SDL_Texture* NewTexture( long int width = -1, long int height = -1, const bool copyRenderer = false, const unsigned long int access = SDL_TEXTUREACCESS_TARGET ) const;
void SetRenderTarget( SDL_Texture* target = nullptr );
const bool AreEqual(SDL_Surface& one, SDL_Surface& two);
void SetRenderTarget(SDL_Renderer* renderer, SDL_Texture* target = nullptr);
void SetRenderTarget(SDL_Texture* target = nullptr);
void ResetRenderTarget();
///Sets
///Gets
SDL_Renderer* getRenderer();
SDL_Renderer* getRenderer();
SDL_Renderer* getRendererCopy();
private:
SDL_Renderer* mp_renderer;
SDL_Renderer* mp_rendererSoftware;
static cRenderer* sp_inst;// = nullptr
};/// END CLASS DEFINITION cRenderer
@@ -67,7 +67,7 @@ const bool cVideo::Setup()
CleanUp();
VideoSettings();
mp_window = SDL_CreateWindow("Hello World!", 100, 100, m_width, m_height, SDL_WINDOW_SHOWN);
mp_window = SDL_CreateWindow("Hello World!", m_xPos, m_yPos, m_width, m_height, SDL_WINDOW_SHOWN);
if (mp_window == nullptr)
cUtility::Inst().Message("Unable to set Window. SDL_CreateWindow():", __AT__, cUtility::eTypeSDL::SDL);
else