Verified Commit 32cba599 authored by JohnMH's avatar JohnMH

Move render utils to a separate class

parent 7d0fa88d
Pipeline #2498 passed with stages
in 19 minutes and 2 seconds
......@@ -7,6 +7,7 @@ obtype.h \
utility.h \
OBException.h \
OBEngine.h \
OBRenderUtils.h \
OBInputEventReceiver.h \
config.h \
oblibconfig.h \
......
/*
* Copyright (C) 2016 John M. Harris, Jr. <johnmh@openblox.org>
* Copyright (C) 2016-2018 John M. Harris, Jr. <johnmh@openblox.org>
*
* This file is part of OpenBlox.
*
......@@ -38,6 +38,7 @@
#include "AssetLocator.h"
#include "OBSerializer.h"
#include "PluginManager.h"
#include "OBRenderUtils.h"
#include "OBInputEventReceiver.h"
......@@ -75,9 +76,11 @@ namespace OB{
#ifndef OB_PLUGINMANAGER
class PluginManager;
#endif
#ifndef OB_OBRENDERUTILS
class OBRenderUtils;
#endif
#if HAVE_IRRLICHT
//typedef void (*post_render_func_t)(irr::video::IVideoDriver*);
typedef std::function<void(irr::video::IVideoDriver*)> post_render_func_t;
#endif
......@@ -185,28 +188,6 @@ namespace OB{
*/
void render();
/**
* Prepares the rendering pipeline for 2D rendering.
*
* @author John M. Harris, Jr.
*/
void prepare2DMode();
/**
* Ends a 2D context
*
* @author John M. Harris, Jr.
*/
void end2DMode();
/**
* Save an image of the last frame to a file.
*
* @param file File to write to
* @author John M. Harris, Jr.
*/
bool saveScreenshot(std::string file);
/**
* Returns the global Lua state, which all states
* used by the engine are coroutines of.
......@@ -375,6 +356,14 @@ namespace OB{
* @author John M. Harris, Jr.
*/
void setPostRenderFunc(post_render_func_t prf);
/**
* Returns the OBRenderUtils instance
*
* @returns OBRenderUtils
* @author John M. Harris, Jr.
*/
shared_ptr<OBRenderUtils> getRenderUtils();
#endif
/**
......@@ -441,17 +430,17 @@ namespace OB{
#endif
#if HAVE_IRRLICHT
bool cached2DMode;
post_render_func_t custPostRender;
post_render_func_t custPostRender;
irr::IrrlichtDevice* irrDev;
irr::video::IVideoDriver* irrDriv;
irr::scene::ISceneManager* irrSceneMgr;
OBInputEventReceiver* eventReceiver;
shared_ptr<OBRenderUtils> renderUtils;
#endif
OBInputEventReceiver* eventReceiver;
shared_ptr<TaskScheduler> taskSched;
shared_ptr<TaskScheduler> secondaryTaskSched;
shared_ptr<AssetLocator> assetLocator;
......
/*
* Copyright (C) 2018 John M. Harris, Jr. <johnmh@openblox.org>
*
* This file is part of OpenBlox.
*
* OpenBlox is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenBlox is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the Lesser GNU General Public License
* along with OpenBlox. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef OB_OBRENDERUTILS
#define OB_OBRENDERUTILS
#include "oblibconfig.h"
#include "obtype.h"
#include "mem.h"
#if HAVE_IRRLICHT
#include <irrlicht/irrlicht.h>
namespace OB{
#ifndef OB_OBENGINE
class OBEngine;
#endif
/**
* Rendering related utilities and complete hacks
*
* @author John M. Harris, Jr.
* @date November 2018
*/
class OBRenderUtils{
public:
OBRenderUtils(OBEngine* eng);
virtual ~OBRenderUtils();
/**
* Returns the OBEngine instance
*
* @returns Instance of OBEngine
* @author John M. Harris, Jr.
*/
OBEngine* getEngine();
/**
* Prepares the rendering pipeline for 2D rendering.
*
* @author John M. Harris, Jr.
*/
void prepare2DMode();
/**
* Ends a 2D context
*
* @author John M. Harris, Jr.
*/
void end2DMode();
/**
* Save an image of the last frame to a file.
*
* @param file File to write to
* @author John M. Harris, Jr.
*/
bool saveScreenshot(std::string file);
/**
* Returns the currently active Irrlicht device, if any.
*
* @returns Irrlicht device
* @author John M. Harris, Jr.
*/
irr::IrrlichtDevice* getIrrlichtDevice();
private:
OBEngine* eng;
bool cached2DMode;
irr::IrrlichtDevice* irrDev;
irr::video::IVideoDriver* irrDriv;
irr::scene::ISceneManager* irrSceneMgr;
};
}
#endif // HAVE_IRRLICHT
#endif // OB_OBRENDERUTILS
// Local Variables:
// mode: c++
// End:
......@@ -17,6 +17,7 @@ TaskScheduler.cpp \
AssetLocator.cpp \
PluginManager.cpp \
OBEngine.cpp \
OBRenderUtils.cpp \
OBInputEventReceiver.cpp \
lua/OBLua.cpp \
lua/OBLua_OBBase.cpp \
......
......@@ -27,6 +27,8 @@
#include "TaskScheduler.h"
#include "ClassFactory.h"
#include "OBRenderUtils.h"
#include "lua/OBLua.h"
#include <string>
......@@ -78,14 +80,14 @@ namespace OB{
custPostRender = NULL;
#if HAVE_IRRLICHT
cached2DMode = false;
irrDev = NULL;
irrDriv = NULL;
irrSceneMgr = NULL;
#endif
eventReceiver = new OBInputEventReceiver(this);
#endif
renderUtils = NULL;
#if HAVE_ENET
enet_initialize();
......@@ -267,6 +269,8 @@ namespace OB{
irrDriv = irrDev->getVideoDriver();
irrSceneMgr = irrDev->getSceneManager();
renderUtils = make_shared<OBRenderUtils>(this);
//Log at INFO level
std::string renderTag("[RENDERER] ");
......@@ -439,35 +443,6 @@ namespace OB{
#endif
}
bool OBEngine::saveScreenshot(std::string file){
if(doRendering){
if(irrDriv){
irr::video::IImage* img = irrDriv->createScreenShot();
if(img){
return irrDriv->writeImageToFile(img, irr::io::path(file.c_str()));
}
}
}
return false;
}
void OBEngine::prepare2DMode(){
#if HAVE_IRRLICHT
if(!cached2DMode && doRendering && irrDriv){
cached2DMode = true;
// Dirty hack
irrDriv->drawPixel(0, 0, irr::video::SColor(0, 255, 255, 255));
}
#endif
}
void OBEngine::end2DMode(){
#if HAVE_IRRLICHT
cached2DMode = false;
#endif
}
lua_State* OBEngine::getGlobalLuaState(){
return globalState;
}
......@@ -570,6 +545,10 @@ namespace OB{
void OBEngine::setPostRenderFunc(post_render_func_t prf){
custPostRender = prf;
}
shared_ptr<OBRenderUtils> OBEngine::getRenderUtils(){
return renderUtils;
}
#endif
shared_ptr<Instance::DataModel> OBEngine::getDataModel(){
......
/*
* Copyright (C) 2018 John M. Harris, Jr. <johnmh@openblox.org>
*
* This file is part of OpenBlox.
*
* OpenBlox is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenBlox is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the Lesser GNU General Public License
* along with OpenBlox. If not, see <https://www.gnu.org/licenses/>.
*/
#include "OBRenderUtils.h"
#include "OBEngine.h"
#if HAVE_IRRLICHT
namespace OB{
OBRenderUtils::OBRenderUtils(OBEngine* eng){
this->eng = eng;
irrDev = eng->getIrrlichtDevice();
irrDriv = irrDev->getVideoDriver();
irrSceneMgr = irrDev->getSceneManager();
cached2DMode = false;
}
OBRenderUtils::~OBRenderUtils(){}
OBEngine* OBRenderUtils::getEngine(){
return eng;
}
void OBRenderUtils::prepare2DMode(){
if(!cached2DMode && eng->doesRendering() && irrDriv){
cached2DMode = true;
// Dirty hack, maybe just set a material in the future?
irrDriv->drawPixel(0, 0, irr::video::SColor(0, 255, 255, 255));
}
}
void OBRenderUtils::end2DMode(){
cached2DMode = false;
}
bool OBRenderUtils::saveScreenshot(std::string file){
if(eng->doesRendering()){
if(irrDriv){
irr::video::IImage* img = irrDriv->createScreenShot();
if(img){
return irrDriv->writeImageToFile(img, irr::io::path(file.c_str()));
}
}
}
return false;
}
}
#endif
......@@ -23,6 +23,8 @@
#include "instance/NetworkServer.h"
#include "type/Vector3.h"
#include "OBRenderUtils.h"
namespace OB{
namespace Instance{
DEFINE_CLASS(Camera, true, false, Instance){
......@@ -57,7 +59,11 @@ namespace OB{
}
bool Camera::SaveScreenshot(std::string file){
return getEngine()->saveScreenshot(file);
shared_ptr<OBRenderUtils> renderUtils = getEngine()->getRenderUtils();
if(renderUtils){
return renderUtils->saveScreenshot(file);
}
return false;
}
void Camera::updateCFrame(){
......
......@@ -21,6 +21,8 @@
#include "type/Color3.h"
#include "OBRenderUtils.h"
#if HAVE_IRRLICHT
#include "GL/gl.h"
#endif
......@@ -52,7 +54,7 @@ namespace OB{
clone->Position = Position;
clone->Size = Size;
clone->ZIndex = ZIndex;
return clone;
}
......@@ -61,32 +63,35 @@ namespace OB{
if(Visible){
if(irr::IrrlichtDevice* irrDev = getEngine()->getIrrlichtDevice()){
if(irr::video::IVideoDriver* irrDriv = irrDev->getVideoDriver()){
getEngine()->prepare2DMode();
shared_ptr<OBRenderUtils> renderUtils = getEngine()->getRenderUtils();
if(renderUtils){
renderUtils->prepare2DMode();
shared_ptr<Type::Color3> bgColor = BackgroundColor3;
double bgTrans = BackgroundTransparency;
shared_ptr<Type::Color3> borderColor = BorderColor3;
int borderSize = BorderSizePixel;
shared_ptr<Type::Vector2> pos = getAbsolutePosition();
shared_ptr<Type::Vector2> siz = getAbsoluteSize()->add(pos);
shared_ptr<Type::Color3> bgColor = BackgroundColor3;
double bgTrans = BackgroundTransparency;
shared_ptr<Type::Color3> borderColor = BorderColor3;
int borderSize = BorderSizePixel;
shared_ptr<Type::Vector2> pos = getAbsolutePosition();
shared_ptr<Type::Vector2> siz = getAbsoluteSize()->add(pos);
glColor4d(bgColor->getR(), bgColor->getG(), bgColor->getB(), 1 - bgTrans);
glColor4d(bgColor->getR(), bgColor->getG(), bgColor->getB(), 1 - bgTrans);
glRectd(pos->getX(), pos->getY(), siz->getX(), siz->getY());
glRectd(pos->getX(), pos->getY(), siz->getX(), siz->getY());
glColor4d(borderColor->getR(), borderColor->getG(), borderColor->getB(), 1 - bgTrans);
//Border Top
glRectd(pos->getX() - borderSize, pos->getY() - borderSize, siz->getX() + borderSize, pos->getY());
//Border Left
glRectd(pos->getX() - borderSize, pos->getY() - borderSize, pos->getX(), siz->getY() + borderSize);
//Border Right
glRectd(siz->getX(), pos->getY() - borderSize, siz->getX() + borderSize, siz->getY() + borderSize);
//Border Bottom
glRectd(pos->getX() - borderSize, siz->getY(), siz->getX() + borderSize, siz->getY() + borderSize);
glColor4d(borderColor->getR(), borderColor->getG(), borderColor->getB(), 1 - bgTrans);
//Border Top
glRectd(pos->getX() - borderSize, pos->getY() - borderSize, siz->getX() + borderSize, pos->getY());
//Border Left
glRectd(pos->getX() - borderSize, pos->getY() - borderSize, pos->getX(), siz->getY() + borderSize);
//Border Right
glRectd(siz->getX(), pos->getY() - borderSize, siz->getX() + borderSize, siz->getY() + borderSize);
//Border Bottom
glRectd(pos->getX() - borderSize, siz->getY(), siz->getX() + borderSize, siz->getY() + borderSize);
getEngine()->end2DMode();
renderUtils->end2DMode();
GuiObject::render();
GuiObject::render();
}
}
}
}
......
......@@ -24,6 +24,8 @@
#include "instance/ScreenGui.h"
#include "OBRenderUtils.h"
#include <algorithm>
#if HAVE_IRRLICHT
......@@ -212,7 +214,10 @@ namespace OB{
}
}
getEngine()->end2DMode();
shared_ptr<OBRenderUtils> renderUtils = getEngine()->getRenderUtils();
if(renderUtils){
renderUtils->end2DMode();
}
#endif
}
......
......@@ -176,54 +176,57 @@ namespace OB{
if(Visible){
if(irr::IrrlichtDevice* irrDev = getEngine()->getIrrlichtDevice()){
if(irr::video::IVideoDriver* irrDriv = irrDev->getVideoDriver()){
getEngine()->prepare2DMode();
shared_ptr<OBRenderUtils> renderUtils = getEngine()->getRenderUtils();
if(renderUtils){
renderUtils->prepare2DMode();
shared_ptr<Type::Color3> bgColor = BackgroundColor3;
double bgTrans = BackgroundTransparency;
shared_ptr<Type::Color3> borderColor = BorderColor3;
int borderSize = BorderSizePixel;
shared_ptr<Type::Vector2> pos = getAbsolutePosition();
shared_ptr<Type::Vector2> siz = getAbsoluteSize()->add(pos);
shared_ptr<Type::Color3> bgColor = BackgroundColor3;
double bgTrans = BackgroundTransparency;
shared_ptr<Type::Color3> borderColor = BorderColor3;
int borderSize = BorderSizePixel;
shared_ptr<Type::Vector2> pos = getAbsolutePosition();
shared_ptr<Type::Vector2> siz = getAbsoluteSize()->add(pos);
shared_ptr<Type::Color3> imgColor = ImageColor3;
shared_ptr<Type::Color3> imgColor = ImageColor3;
glColor4d(bgColor->getR(), bgColor->getG(), bgColor->getB(), 1 - bgTrans);
glColor4d(bgColor->getR(), bgColor->getG(), bgColor->getB(), 1 - bgTrans);
glRectd(pos->getX(), pos->getY(), siz->getX(), siz->getY());
glRectd(pos->getX(), pos->getY(), siz->getX(), siz->getY());
if(img){
int imgTrans = (1 - ImageTransparency) * 255;
if(imgTrans > 255){ imgTrans = 255; }
if(imgTrans < 0){ imgTrans = 0; }
if(img){
int imgTrans = (1 - ImageTransparency) * 255;
if(imgTrans > 255){ imgTrans = 255; }
if(imgTrans < 0){ imgTrans = 0; }
irr::video::SColor colorPtr[4];
colorPtr[0] = irr::video::SColor(imgTrans, imgColor->getRi(), imgColor->getGi(), imgColor->getBi());
colorPtr[1] = irr::video::SColor(imgTrans, imgColor->getRi(), imgColor->getGi(), imgColor->getBi());
colorPtr[2] = irr::video::SColor(imgTrans, imgColor->getRi(), imgColor->getGi(), imgColor->getBi());
colorPtr[3] = irr::video::SColor(imgTrans, imgColor->getRi(), imgColor->getGi(), imgColor->getBi());
irr::video::SColor colorPtr[4];
colorPtr[0] = irr::video::SColor(imgTrans, imgColor->getRi(), imgColor->getGi(), imgColor->getBi());
colorPtr[1] = irr::video::SColor(imgTrans, imgColor->getRi(), imgColor->getGi(), imgColor->getBi());
colorPtr[2] = irr::video::SColor(imgTrans, imgColor->getRi(), imgColor->getGi(), imgColor->getBi());
colorPtr[3] = irr::video::SColor(imgTrans, imgColor->getRi(), imgColor->getGi(), imgColor->getBi());
irr::core::rect<irr::s32> imgPos = irr::core::rect<irr::s32>(pos->getX(), pos->getY(), siz->getX(), siz->getY());
irr::core::rect<irr::s32> imgPos = irr::core::rect<irr::s32>(pos->getX(), pos->getY(), siz->getX(), siz->getY());
//TODO: Implement ImageSize, ImagePosition
irr::core::dimension2d<irr::u32> imgSize = img->getSize();
irr::core::rect<irr::s32> imgSiz = irr::core::rect<irr::s32>(0, 0, imgSize.Width, imgSize.Height);
//TODO: Implement ImageSize, ImagePosition
irr::core::dimension2d<irr::u32> imgSize = img->getSize();
irr::core::rect<irr::s32> imgSiz = irr::core::rect<irr::s32>(0, 0, imgSize.Width, imgSize.Height);
irrDriv->draw2DImage(img, imgPos, imgSiz, 0, colorPtr, img->hasAlpha());
}
irrDriv->draw2DImage(img, imgPos, imgSiz, 0, colorPtr, img->hasAlpha());
}
glColor4d(borderColor->getR(), borderColor->getG(), borderColor->getB(), 1 - bgTrans);
// Border Top
glRectd(pos->getX() - borderSize, pos->getY() - borderSize, siz->getX() + borderSize, pos->getY());
// Border Left
glRectd(pos->getX() - borderSize, pos->getY() - borderSize, pos->getX(), siz->getY() + borderSize);
// Border Right
glRectd(siz->getX(), pos->getY() - borderSize, siz->getX() + borderSize, siz->getY() + borderSize);
// Border Bottom
glRectd(pos->getX() - borderSize, siz->getY(), siz->getX() + borderSize, siz->getY() + borderSize);
glColor4d(borderColor->getR(), borderColor->getG(), borderColor->getB(), 1 - bgTrans);
// Border Top
glRectd(pos->getX() - borderSize, pos->getY() - borderSize, siz->getX() + borderSize, pos->getY());
// Border Left
glRectd(pos->getX() - borderSize, pos->getY() - borderSize, pos->getX(), siz->getY() + borderSize);
// Border Right
glRectd(siz->getX(), pos->getY() - borderSize, siz->getX() + borderSize, siz->getY() + borderSize);
// Border Bottom
glRectd(pos->getX() - borderSize, siz->getY(), siz->getX() + borderSize, siz->getY() + borderSize);
getEngine()->end2DMode();
renderUtils->end2DMode();
GuiObject::render();
GuiObject::render();
}
}
}
}
......
......@@ -456,12 +456,12 @@ namespace OB{
if(serializer){
shared_ptr<Instance> tInst = serializer->GetByID(sid);
if(!tInst){
printf("Created %s with id %s\n", stype.c_str(), sid.c_str());
tInst = ClassFactory::createReplicate(stype, eng);
serializer->SetID(tInst, sid);
}
if(tInst){
serializer->SetID(tInst, sid);
tInst->setParent(shared_from_this(), true);
tInst->deserializeCreate(cinst);
......@@ -539,8 +539,11 @@ namespace OB{
shared_ptr<Instance> kid = kids[i];
if(kid){
if(serializer->HasID(kid)){
printf("%s has id\n", kid->getName().c_str());
pugi::xml_node cinst = thisNode.find_child_by_attribute("instance", "id", kid->serializedID().c_str());
kid->deserializeProperties(cinst);
}else{
printf("%s does not have an id\n", kid->getName().c_str());
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment