Another code cleanup and code documentation update.

This commit is contained in:
Leon Styhre 2020-06-26 17:17:35 +02:00
parent ee4a55e9d6
commit 1f74723533
36 changed files with 2254 additions and 1984 deletions

View file

@ -25,6 +25,7 @@ v1.0.0
* All required fonts bundled with the application, no dependencies on the OS to provide them any longer
* Made pugixml an external dependency instead of bundling it
* Updated the cmake/cpack install and package build script to work as expected (can now generate .deb and .rpm installation packages)
* Added support for Clang/LLVM, made the application build with no errors or warnings using this compiler
* License files included for all the libraries and resources that are bundled with the application
* Updated the MAME ROM index files to include ROMs up to MAME version 0.221 (and created scripts to easily generate these index files in the future)

View file

@ -1,9 +1,16 @@
//
// CECInput.cpp
//
// CEC (Consumer Electronics Control).
//
#include "CECInput.h"
#ifdef HAVE_LIBCEC
#include "Log.h"
#include <iostream> // Bad bad cecloader.
#include <libcec/cec.h>
#include <iostream> // bad bad cecloader
#include <libcec/cecloader.h>
#include <SDL_events.h>
#ifdef _RPI_
@ -15,24 +22,24 @@ extern "C" {
#endif // _RPI_
#endif // HAVE_LIBCEC
// hack for cec support
// Hack for CEC support.
extern int SDL_USER_CECBUTTONDOWN;
extern int SDL_USER_CECBUTTONUP;
CECInput* CECInput::sInstance = nullptr;
#ifdef HAVE_LIBCEC
static void onAlert(void* /*cbParam*/, const CEC::libcec_alert type, const CEC::libcec_parameter param)
static void onAlert(void* /*cbParam*/, const CEC::libcec_alert type,
const CEC::libcec_parameter param)
{
LOG(LogDebug) << "CECInput::onAlert type: " << CECInput::getAlertTypeString(type) << " parameter: " << (char*)(param.paramData);
} // onAlert
LOG(LogDebug) << "CECInput::onAlert type: " << CECInput::getAlertTypeString(type) <<
" parameter: " << (char*)(param.paramData);
}
static void onCommand(void* /*cbParam*/, const CEC::cec_command* command)
{
LOG(LogDebug) << "CECInput::onCommand opcode: " << CECInput::getOpCodeString(command->opcode);
} // onCommand
}
static void onKeyPress(void* /*cbParam*/, const CEC::cec_keypress* key)
{
@ -42,14 +49,12 @@ static void onKeyPress(void* /*cbParam*/, const CEC::cec_keypress* key)
event.type = (key->duration > 0) ? SDL_USER_CECBUTTONUP : SDL_USER_CECBUTTONDOWN;
event.user.code = key->keycode;
SDL_PushEvent(&event);
} // onKeyPress
}
static void onLogMessage(void* /*cbParam*/, const CEC::cec_log_message* message)
{
LOG(LogDebug) << "CECInput::onLogMessage message: " << message->message;
} // onLogMessage
}
#ifdef _RPI_
static void vchi_tv_and_cec_init()
@ -60,15 +65,13 @@ static void vchi_tv_and_cec_init()
vc_vchi_tv_init(vchi_instance, &vchi_connection, 1);
vc_vchi_cec_init(vchi_instance, &vchi_connection, 1);
} // vchi_tv_and_cec_init
}
static void vchi_tv_and_cec_deinit()
{
vc_vchi_cec_stop();
vc_vchi_tv_stop();
} // vchi_tv_and_cec_deinit
}
#endif // _RPI_
#endif // HAVE_LIBCEC
@ -76,25 +79,22 @@ void CECInput::init()
{
if (!sInstance)
sInstance = new CECInput();
} // init
}
void CECInput::deinit()
{
if(sInstance)
{
if (sInstance) {
delete sInstance;
sInstance = nullptr;
}
} // deinit
}
CECInput::CECInput() : mlibCEC(nullptr)
{
#ifdef HAVE_LIBCEC
#ifdef _RPI_
// restart vchi tv and cec in case we just came back from another app using cec (like Kodi)
// Restart vchi tv and CEC in case we just came back from another app using CEC (like Kodi).
vchi_tv_and_cec_deinit();
vchi_tv_and_cec_init();
#endif // _RPI_
@ -117,8 +117,7 @@ CECInput::CECInput() : mlibCEC(nullptr)
mlibCEC = LibCecInitialise(&config);
if(!mlibCEC)
{
if (!mlibCEC) {
LOG(LogError) << "CECInput::LibCecInitialise failed";
return;
}
@ -126,8 +125,7 @@ CECInput::CECInput() : mlibCEC(nullptr)
CEC::cec_adapter_descriptor adapters[10];
int numAdapters = mlibCEC->DetectAdapters(adapters, 10, nullptr, true);
if(numAdapters <= 0)
{
if (numAdapters <= 0) {
LOG(LogError) << "CECInput::mAdapter->DetectAdapters failed";
UnloadLibCec(mlibCEC);
mlibCEC = nullptr;
@ -135,43 +133,38 @@ CECInput::CECInput() : mlibCEC(nullptr)
}
for (int i = 0; i < numAdapters; ++i)
LOG(LogDebug) << "CEC adapter: " << i << " path: " << adapters[i].strComPath << " name: " << adapters[i].strComName;
LOG(LogDebug) << "CEC adapter: " << i << " path: " << adapters[i].strComPath <<
" name: " << adapters[i].strComName;
if(!mlibCEC->Open(adapters[0].strComName))
{
if (!mlibCEC->Open(adapters[0].strComName)) {
LOG(LogError) << "CECInput::mAdapter->Open failed";
UnloadLibCec(mlibCEC);
mlibCEC = nullptr;
return;
}
#endif // HAVE_LIBCEC
} // CECInput
}
CECInput::~CECInput()
{
#ifdef HAVE_LIBCEC
if(mlibCEC)
{
if (mlibCEC) {
mlibCEC->Close();
UnloadLibCec(mlibCEC);
mlibCEC = nullptr;
}
#ifdef _RPI_
// deinit vchi tv and cec in case we are going to launch another app using cec (like Kodi)
// Deinit vchi tv and CEC in case we are going to launch another app using CEC (like Kodi).
vchi_tv_and_cec_deinit();
#endif // _RPI_
#endif // HAVE_LIBCEC
} // ~CECInput
}
std::string CECInput::getAlertTypeString(const unsigned int _type)
{
switch(_type)
{
switch (_type) {
#ifdef HAVE_LIBCEC
case CEC::CEC_ALERT_SERVICE_DEVICE: { return "Service-Device"; } break;
case CEC::CEC_ALERT_CONNECTION_LOST: { return "Connection-Lost"; } break;
@ -182,17 +175,13 @@ std::string CECInput::getAlertTypeString(const unsigned int _type)
#else // HAVE_LIBCEC
case 0:
#endif // HAVE_LIBCEC
default: { return "Unknown"; } break;
}
} // getAlertTypeString
}
std::string CECInput::getOpCodeString(const unsigned int _opCode)
{
switch(_opCode)
{
switch (_opCode) {
#ifdef HAVE_LIBCEC
case CEC::CEC_OPCODE_ACTIVE_SOURCE: { return "Active-Source"; } break;
case CEC::CEC_OPCODE_IMAGE_VIEW_ON: { return "Image-View-On"; } break;
@ -267,17 +256,13 @@ std::string CECInput::getOpCodeString(const unsigned int _opCode)
#else // HAVE_LIBCEC
case 0:
#endif // HAVE_LIBCEC
default: { return "Unknown"; } break;
}
} // getOpCodeString
}
std::string CECInput::getKeyCodeString(const unsigned int _keyCode)
{
switch(_keyCode)
{
switch (_keyCode) {
#ifdef HAVE_LIBCEC
case CEC::CEC_USER_CONTROL_CODE_SELECT: { return "Select"; } break;
case CEC::CEC_USER_CONTROL_CODE_UP: { return "Up"; } break;
@ -369,8 +354,6 @@ std::string CECInput::getKeyCodeString(const unsigned int _keyCode)
#else // HAVE_LIBCEC
case 0:
#endif // HAVE_LIBCEC
default: { return "Unknown"; } break;
}
} // getKeyCodeString
}

View file

@ -1,3 +1,9 @@
//
// CECInput.h
//
// CEC (Consumer Electronics Control).
//
#pragma once
#ifndef ES_CORE_CECINPUT_H
#define ES_CORE_CECINPUT_H
@ -9,7 +15,6 @@ namespace CEC { class ICECAdapter; }
class CECInput
{
public:
static void init();
static void deinit();
static std::string getAlertTypeString(const unsigned int _type);
@ -17,14 +22,12 @@ public:
static std::string getKeyCodeString(const unsigned int _keyCode);
private:
CECInput();
~CECInput();
static CECInput* sInstance;
CEC::ICECAdapter* mlibCEC;
}; // CECInput
};
#endif // ES_CORE_CECINPUT_H

View file

@ -1,3 +1,9 @@
//
// HelpPrompt.h
//
// Definition of the pair used by help prompts to display a button and its mapped function.
//
#pragma once
#ifndef ES_CORE_HELP_PROMPT_H
#define ES_CORE_HELP_PROMPT_H

View file

@ -1,3 +1,10 @@
//
// HelpStyle.cpp
//
// Style (default colors, position and origin) for the help system.
// Also theme handling.
//
#include "HelpStyle.h"
#include "resources/Font.h"
@ -22,7 +29,8 @@ void HelpStyle::applyTheme(const std::shared_ptr<ThemeData>& theme, const std::s
return;
if(elem->has("pos"))
position = elem->get<Vector2f>("pos") * Vector2f((float)Renderer::getScreenWidth(), (float)Renderer::getScreenHeight());
position = elem->get<Vector2f>("pos") *
Vector2f((float)Renderer::getScreenWidth(), (float)Renderer::getScreenHeight());
if(elem->has("origin"))
origin = elem->get<Vector2f>("origin");

View file

@ -1,3 +1,10 @@
//
// HelpStyle.h
//
// Style (default colors, position and origin) for the help system.
// Also theme handling.
//
#pragma once
#ifndef ES_CORE_HELP_STYLE_H
#define ES_CORE_HELP_STYLE_H
@ -9,15 +16,14 @@
class Font;
class ThemeData;
struct HelpStyle
{
struct HelpStyle {
Vector2f position;
Vector2f origin;
unsigned int iconColor;
unsigned int textColor;
std::shared_ptr<Font> font;
HelpStyle(); // default values
HelpStyle(); // Default values.
void applyTheme(const std::shared_ptr<ThemeData>& theme, const std::string& view);
};

View file

@ -1,50 +1,51 @@
//
// ImageIO.cpp
//
// Image I/O functions.
//
#include "ImageIO.h"
#include "Log.h"
#include <FreeImage.h>
#include <string.h>
std::vector<unsigned char> ImageIO::loadFromMemoryRGBA32(const unsigned char * data, const size_t size, size_t & width, size_t & height)
std::vector<unsigned char> ImageIO::loadFromMemoryRGBA32(const unsigned char * data,
const size_t size, size_t & width, size_t & height)
{
std::vector<unsigned char> rawData;
width = 0;
height = 0;
FIMEMORY * fiMemory = FreeImage_OpenMemory((BYTE *)data, (DWORD)size);
if (fiMemory != nullptr) {
//detect the filetype from data
// Detect the filetype from data.
FREE_IMAGE_FORMAT format = FreeImage_GetFileTypeFromMemory(fiMemory);
if (format != FIF_UNKNOWN && FreeImage_FIFSupportsReading(format))
{
//file type is supported. load image
if (format != FIF_UNKNOWN && FreeImage_FIFSupportsReading(format)) {
// File type is supported. load image,
FIBITMAP * fiBitmap = FreeImage_LoadFromMemory(format, fiMemory);
if (fiBitmap != nullptr)
{
//loaded. convert to 32bit if necessary
if (FreeImage_GetBPP(fiBitmap) != 32)
{
if (fiBitmap != nullptr) {
// Loaded. convert to 32bit if necessary.
if (FreeImage_GetBPP(fiBitmap) != 32) {
FIBITMAP * fiConverted = FreeImage_ConvertTo32Bits(fiBitmap);
if (fiConverted != nullptr)
{
if (fiConverted != nullptr) {
//free original bitmap data
FreeImage_Unload(fiBitmap);
fiBitmap = fiConverted;
}
}
if (fiBitmap != nullptr)
{
if (fiBitmap != nullptr) {
width = FreeImage_GetWidth(fiBitmap);
height = FreeImage_GetHeight(fiBitmap);
//loop through scanlines and add all pixel data to the return vector
//this is necessary, because width*height*bpp might not be == pitch
// Loop through scanlines and add all pixel data to the return vector.
// This is necessary, because width*height*bpp might not be == pitch.
unsigned char * tempData = new unsigned char[width * height * 4];
for (size_t i = 0; i < height; i++)
{
for (size_t i = 0; i < height; i++) {
const BYTE * scanLine = FreeImage_GetScanLine(fiBitmap, (int)i);
memcpy(tempData + (i * width * 4), scanLine, width * 4);
}
//convert from BGRA to RGBA
for(size_t i = 0; i < width*height; i++)
{
// Convert from BGRA to RGBA.
for (size_t i = 0; i < width*height; i++) {
RGBQUAD bgra = ((RGBQUAD *)tempData)[i];
RGBQUAD rgba;
rgba.rgbBlue = bgra.rgbRed;
@ -54,21 +55,20 @@ std::vector<unsigned char> ImageIO::loadFromMemoryRGBA32(const unsigned char * d
((RGBQUAD *)tempData)[i] = rgba;
}
rawData = std::vector<unsigned char>(tempData, tempData + width * height * 4);
//free bitmap data
// Free bitmap data.
FreeImage_Unload(fiBitmap);
delete[] tempData;
}
}
else
{
else {
LOG(LogError) << "Error - Failed to load image from memory!";
}
}
else
{
LOG(LogError) << "Error - File type " << (format == FIF_UNKNOWN ? "unknown" : "unsupported") << "!";
else {
LOG(LogError) << "Error - File type " <<
(format == FIF_UNKNOWN ? "unknown" : "unsupported") << "!";
}
//free FIMEMORY again
// Free fiMemory again
FreeImage_CloseMemory(fiMemory);
}
return rawData;
@ -78,10 +78,8 @@ void ImageIO::flipPixelsVert(unsigned char* imagePx, const size_t& width, const
{
unsigned int temp;
unsigned int* arr = (unsigned int*)imagePx;
for(size_t y = 0; y < height / 2; y++)
{
for(size_t x = 0; x < width; x++)
{
for (size_t y = 0; y < height / 2; y++) {
for (size_t x = 0; x < width; x++) {
temp = arr[x + (y * width)];
arr[x + (y * width)] = arr[x + (height * width) - ((y + 1) * width)];
arr[x + (height * width) - ((y + 1) * width)] = temp;

View file

@ -1,3 +1,9 @@
//
// ImageIO.h
//
// Image I/O functions.
//
#pragma once
#ifndef ES_CORE_IMAGE_IO
#define ES_CORE_IMAGE_IO
@ -8,7 +14,8 @@
class ImageIO
{
public:
static std::vector<unsigned char> loadFromMemoryRGBA32(const unsigned char * data, const size_t size, size_t & width, size_t & height);
static std::vector<unsigned char> loadFromMemoryRGBA32(const unsigned char * data,
const size_t size, size_t & width, size_t & height);
static void flipPixelsVert(unsigned char* imagePx, const size_t& width, const size_t& height);
};

View file

@ -1,3 +1,4 @@
//
// InputManager.cpp
//
// Low-level input handling.

View file

@ -1,12 +1,19 @@
//
// Log.cpp
//
// Log handling.
//
#include "Log.h"
#include "utils/FileSystemUtil.h"
#include "Platform.h"
#include <iostream>
#include <iomanip>
LogLevel Log::reportingLevel = LogInfo;
FILE* Log::file = NULL; //fopen(getLogPath().c_str(), "w");
FILE* Log::file = nullptr; // fopen(getLogPath().c_str(), "w");
LogLevel Log::getReportingLevel()
{
@ -15,8 +22,7 @@ LogLevel Log::getReportingLevel()
std::string Log::getLogPath()
{
std::string home = Utils::FileSystem::getHomePath();
return home + "/.emulationstation/es_log.txt";
return Utils::FileSystem::getHomePath() + "/.emulationstation/es_log.txt";
}
void Log::setReportingLevel(LogLevel level)
@ -27,7 +33,7 @@ void Log::setReportingLevel(LogLevel level)
void Log::init()
{
remove((getLogPath() + ".bak").c_str());
// rename previous log file
// Rename previous log file.
rename(getLogPath().c_str(), (getLogPath() + ".bak").c_str());
return;
}
@ -54,7 +60,7 @@ void Log::flush()
void Log::close()
{
fclose(file);
file = NULL;
file = nullptr;
}
FILE* Log::getOutput()
@ -66,18 +72,18 @@ Log::~Log()
{
os << std::endl;
if(getOutput() == NULL)
{
if (getOutput() == nullptr) {
// not open yet, print to stdout
std::cerr << "ERROR - tried to write to log file before it was open! The following won't be logged:\n";
std::cerr << "ERROR - tried to write to log file before it was open! "
"The following won't be logged:\n";
std::cerr << os.str();
return;
}
fprintf(getOutput(), "%s", os.str().c_str());
//if it's an error, also print to console
//print all messages if using --debug
// If it's an error, also print to console.
// Print all messages if using --debug.
if (messageLevel == LogError || reportingLevel >= LogDebug)
fprintf(stderr, "%s", os.str().c_str());
}

View file

@ -1,3 +1,9 @@
//
// Log.h
//
// Log handling.
//
#pragma once
#ifndef ES_CORE_LOG_H
#define ES_CORE_LOG_H
@ -8,7 +14,12 @@
if(level > Log::getReportingLevel()); \
else Log().get(level)
enum LogLevel { LogError, LogWarning, LogInfo, LogDebug };
enum LogLevel {
LogError,
LogWarning,
LogInfo,
LogDebug
};
class Log
{
@ -26,9 +37,11 @@ public:
static void init();
static void open();
static void close();
protected:
std::ostringstream os;
static FILE* file;
private:
static LogLevel reportingLevel;
static FILE* getOutput();

View file

@ -89,7 +89,7 @@ MameNames::MameNames()
mMameBioses.push_back(bios);
}
// Read devices file.
// Read device file.
xmlpath = ResourceManager::getInstance()->getResourcePath(":/MAME/mamedevices.xml");
if (!Utils::FileSystem::exists(xmlpath))
@ -110,8 +110,7 @@ MameNames::MameNames()
std::string device = deviceNode.text().get();
mMameDevices.push_back(device);
}
} // MameNames.
}
MameNames::~MameNames()
{

View file

@ -18,7 +18,6 @@
class MameNames
{
public:
static void init();
static void deinit();
static MameNames* getInstance();
@ -28,7 +27,6 @@ public:
const bool isDevice(const std::string& _deviceName);
private:
struct NamePair {
std::string mameName;
std::string realName;
@ -46,7 +44,6 @@ private:
std::vector<std::string> mMameDevices;
const bool find(const std::vector<std::string> devices, const std::string& name);
}; // MameNames
};
#endif // ES_CORE_MAMENAMES_H

View file

@ -1,3 +1,9 @@
//
// PowerSaver.cpp
//
// Power saving functions.
//
#include "PowerSaver.h"
#include "AudioManager.h"
@ -27,13 +33,13 @@ int PowerSaver::getTimeout()
void PowerSaver::loadWakeupTime()
{
// TODO : Move this to Screensaver Class
// TODO : Move this to Screensaver Class.
std::string behaviour = Settings::getInstance()->getString("ScreenSaverBehavior");
if (behaviour == "random video")
mWakeupTimeout = Settings::getInstance()->getInt("ScreenSaverSwapVideoTimeout") - getMode();
else if (behaviour == "slideshow")
mWakeupTimeout = Settings::getInstance()->getInt("ScreenSaverSwapImageTimeout") - getMode();
else // Dim and Blank
else // Dim and Blank.
mWakeupTimeout = -1;
}
@ -55,11 +61,14 @@ void PowerSaver::updateMode()
if (mode == "disabled") {
mMode = DISABLED;
} else if (mode == "instant") {
}
else if (mode == "instant") {
mMode = INSTANT;
} else if (mode == "enhanced") {
}
else if (mode == "enhanced") {
mMode = ENHANCED;
} else {
}
else {
mMode = DEFAULT;
}
updateTimeouts();
@ -79,9 +88,8 @@ void PowerSaver::setState(bool state)
void PowerSaver::runningScreenSaver(bool state)
{
mRunningScreenSaver = state;
if (mWakeupTimeout < mMode)
{
// Disable PS if wake up time is less than mode as PS will never trigger
if (mWakeupTimeout < mMode) {
// Disable PS if wake up time is less than mode as PS will never trigger.
setState(!state);
}
}

View file

@ -1,3 +1,9 @@
//
// PowerSaver.h
//
// Power saving functions.
//
#pragma once
#ifndef ES_CORE_POWER_SAVER_H
#define ES_CORE_POWER_SAVER_H
@ -5,32 +11,37 @@
class PowerSaver
{
public:
enum mode : int { DISABLED = -1, INSTANT = 200, ENHANCED = 3000, DEFAULT = 10000 };
enum mode : int {
DISABLED = -1,
INSTANT = 200,
ENHANCED = 3000,
DEFAULT = 10000
};
// Call when you want PS to reload all state and settings
// Call when you want PS to reload all states and settings.
static void init();
// Get timeout to wake up from for the next event
// Get timeout to wake up from for the next event.
static int getTimeout();
// Update currently set timeouts after User changes Timeout settings
// Update currently set timeouts after user changes timeout settings.
static void updateTimeouts();
// Use this to check which mode you are in or get the mode timeout
// Use this to check which mode you are in or get the mode timeout.
static mode getMode();
// Called when user changes mode from Settings
// Called when user changes mode from settings menu.
static void updateMode();
// Get current state of PS. Not to be confused with Mode
// Get current state of PS. Not to be confused with mode.
static bool getState();
// State is used to temporarily pause and resume PS
// State is used to temporarily pause and resume PS.
static void setState(bool state);
// Paired calls when you want to pause PS briefly till you finish animating
// or processing over cycles
// Paired calls when you want to pause PS briefly until you finish animating
// or processing over cycles.
static void pause() { setState(false); }
static void resume() { setState(true); }
// This is used by ScreenSaver to let PS know when to switch to SS timeouts
// This is used by ScreenSaver to let PS know when to switch to SS timeouts.
static void runningScreenSaver(bool state);
static bool isScreenSaverActive();

View file

@ -1,3 +1,9 @@
//
// Animation.h
//
// Animation base class.
//
#pragma once
#ifndef ES_CORE_ANIMATIONS_ANIMATION_H
#define ES_CORE_ANIMATIONS_ANIMATION_H

View file

@ -1,9 +1,23 @@
//
// AnimationController.cpp
//
// Basic animation controls.
//
#include "animations/AnimationController.h"
#include "animations/Animation.h"
AnimationController::AnimationController(Animation* anim, int delay, std::function<void()> finishedCallback, bool reverse)
: mAnimation(anim), mFinishedCallback(finishedCallback), mReverse(reverse), mTime(-delay), mDelay(delay)
AnimationController::AnimationController(
Animation* anim,
int delay,
std::function<void()> finishedCallback,
bool reverse)
: mAnimation(anim),
mFinishedCallback(finishedCallback),
mReverse(reverse),
mTime(-delay),
mDelay(delay)
{
}
@ -19,7 +33,7 @@ bool AnimationController::update(int deltaTime)
{
mTime += deltaTime;
if(mTime < 0) // are we still in delay?
if(mTime < 0) // Are we still in delay?
return false;
float t = (float)mTime / mAnimation->getDuration();

View file

@ -1,3 +1,9 @@
//
// AnimationController.h
//
// Basic animation controls.
//
#pragma once
#ifndef ES_CORE_ANIMATIONS_ANIMATION_CONTROLLER_H
#define ES_CORE_ANIMATIONS_ANIMATION_CONTROLLER_H
@ -10,7 +16,8 @@ class AnimationController
{
public:
// Takes ownership of anim (will delete in destructor).
AnimationController(Animation* anim, int delay = 0, std::function<void()> finishedCallback = nullptr, bool reverse = false);
AnimationController(Animation* anim, int delay = 0,
std::function<void()> finishedCallback = nullptr, bool reverse = false);
virtual ~AnimationController();
// Returns true if the animation is complete.

View file

@ -1,14 +1,22 @@
//
// LambdaAnimation.h
//
// Basic animation controls, to be used in lambda expressions.
//
#pragma once
#ifndef ES_CORE_ANIMATIONS_LAMBDA_ANIMATION_H
#define ES_CORE_ANIMATIONS_LAMBDA_ANIMATION_H
#include "animations/Animation.h"
// Useful for simple one-off animations, you can supply the animation's apply(t) method right in the constructor as a lambda.
// Useful for simple one-off animations, you can supply the animation's apply(t)
// function directly in the constructor as a lambda.
class LambdaAnimation : public Animation
{
public:
LambdaAnimation(const std::function<void(float t)>& func, int duration) : mFunction(func), mDuration(duration) {}
LambdaAnimation(const std::function<void(float t)>& func, int duration)
: mFunction(func), mDuration(duration) {}
virtual ~LambdaAnimation() = default;

View file

@ -1,141 +1,127 @@
//
// Misc.cpp
//
// Miscellaneous math functions.
//
#include "math/Misc.h"
#include <math.h>
namespace Math
{
// added here to avoid including math.h whenever these are used
// Added here to avoid including math.h whenever these are used.
float cosf(const float _num)
{
return ::cosf(_num);
} // Math::cos
}
float sinf(const float _num)
{
return ::sinf(_num);
} // Math::sin
}
float floorf(const float _num)
{
return ::floorf(_num);
} // Math::floor
}
float ceilf(const float _num)
{
return ::ceilf(_num);
} // Math::ceil
}
int min(const int _num1, const int _num2)
{
return (_num1 < _num2) ? _num1 : _num2;
} // Math::min
}
int max(const int _num1, const int _num2)
{
return (_num1 > _num2) ? _num1 : _num2;
} // Math::max
}
float min(const float _num1, const float _num2)
{
return (_num1 < _num2) ? _num1 : _num2;
} // Math::min
}
float max(const float _num1, const float _num2)
{
return (_num1 > _num2) ? _num1 : _num2;
} // Math::max
}
float clamp(const float _min, const float _max, const float _num)
{
return max(min(_num, _max), _min);
} // Math::clamp
}
float round(const float _num)
{
return (float)(int)(_num + 0.5);
} // Math::round
}
float lerp(const float _start, const float _end, const float _fraction)
{
return (_start + ((_end - _start) * clamp(0, 1, _fraction)));
} // Math::lerp
}
float smoothStep(const float _left, const float _right, const float _x)
{
const float x = clamp(0, 1, (_x - _left)/(_right - _left));
return x * x * (3 - (2 * x));
} // Math::smoothStep
}
float smootherStep(const float _left, const float _right, const float _x)
{
const float x = clamp(0, 1, (_x - _left)/(_right - _left));
return x * x * x * (x * ((x * 6) - 15) + 10);
} // Math::smootherStep
}
namespace Scroll
{
float bounce(const float _delayTime, const float _scrollTime, const float _currentTime, const float _scrollLength)
float bounce(const float _delayTime, const float _scrollTime,
const float _currentTime, const float _scrollLength)
{
if(_currentTime < _delayTime)
{
// wait
if(_currentTime < _delayTime) {
// Wait.
return 0;
}
else if(_currentTime < (_delayTime + _scrollTime))
{
// lerp from 0 to scrollLength
else if(_currentTime < (_delayTime + _scrollTime)) {
// Lerp from 0 to scrollLength.
const float fraction = (_currentTime - _delayTime) / _scrollTime;
return lerp(0.0f, _scrollLength, smootherStep(0, 1, fraction));
}
else if(_currentTime < (_delayTime + _scrollTime + _delayTime))
{
// wait some more
else if(_currentTime < (_delayTime + _scrollTime + _delayTime)) {
// Wait some more.
return _scrollLength;
}
else if(_currentTime < (_delayTime + _scrollTime + _delayTime + _scrollTime))
{
// lerp back from scrollLength to 0
const float fraction = (_currentTime - _delayTime - _scrollTime - _delayTime) / _scrollTime;
else if(_currentTime < (_delayTime + _scrollTime + _delayTime + _scrollTime)) {
// Lerp back from scrollLength to 0.
const float fraction = (_currentTime - _delayTime - _scrollTime -
_delayTime) / _scrollTime;
return lerp(_scrollLength, 0.0f, smootherStep(0, 1, fraction));
}
// and back to waiting
return 0;
} // Math::Scroll::bounce
float loop(const float _delayTime, const float _scrollTime, const float _currentTime, const float _scrollLength)
{
if(_currentTime < _delayTime)
{
// wait
// And back to waiting.
return 0;
}
else if(_currentTime < (_delayTime + _scrollTime))
float loop(const float _delayTime, const float _scrollTime,
const float _currentTime, const float _scrollLength)
{
// lerp from 0 to scrollLength
if(_currentTime < _delayTime) {
// Wait.
return 0;
}
else if(_currentTime < (_delayTime + _scrollTime)) {
// Lerp from 0 to scrollLength.
const float fraction = (_currentTime - _delayTime) / _scrollTime;
return lerp(0.0f, _scrollLength, fraction);
}
// and back to waiting
// And back to waiting.
return 0;
} // Math::Scroll::loop
} // Math::Scroll::
} // Math::

View file

@ -1,3 +1,9 @@
//
// Misc.h
//
// Miscellaneous math functions.
//
#pragma once
#ifndef ES_CORE_MATH_MISC_H
#define ES_CORE_MATH_MISC_H
@ -8,7 +14,7 @@
namespace Math
{
// added here to avoid including math.h whenever these are used
// Added here to avoid including math.h whenever these are used.
float cosf(const float _num);
float sinf(const float _num);
float floorf(const float _num);
@ -26,11 +32,11 @@ namespace Math
namespace Scroll
{
float bounce(const float _delayTime, const float _scrollTime, const float _currentTime, const float _scrollLength);
float loop (const float _delayTime, const float _scrollTime, const float _currentTime, const float _scrollLength);
} // Scroll::
} // Math::
float bounce(const float _delayTime, const float _scrollTime,
const float _currentTime, const float _scrollLength);
float loop(const float _delayTime, const float _scrollTime,
const float _currentTime, const float _scrollLength);
}
}
#endif // ES_CORE_MATH_MISC_H

View file

@ -1,3 +1,9 @@
//
// Transform4x4f.cpp
//
// 4x4 transform functions.
//
#include "math/Transform4x4f.h"
const Transform4x4f Transform4x4f::operator*(const Transform4x4f& _other) const
@ -32,8 +38,7 @@ const Transform4x4f Transform4x4f::operator*(const Transform4x4f& _other) const
1
}
};
} // operator*
}
const Vector3f Transform4x4f::operator*(const Vector3f& _other) const
{
@ -46,10 +51,15 @@ const Vector3f Transform4x4f::operator*(const Vector3f& _other) const
tm[ 1] * ov[0] + tm[ 5] * ov[1] + tm[ 9] * ov[2] + tm[13],
tm[ 2] * ov[0] + tm[ 6] * ov[1] + tm[10] * ov[2] + tm[14]
};
}
} // operator*
Transform4x4f& Transform4x4f::orthoProjection(float _left, float _right, float _bottom, float _top, float _near, float _far)
Transform4x4f& Transform4x4f::orthoProjection(
float _left,
float _right,
float _bottom,
float _top,
float _near,
float _far)
{
float* tm = (float*)this;
const float o[6] = { 2 / (_right - _left),
@ -85,8 +95,7 @@ Transform4x4f& Transform4x4f::orthoProjection(float _left, float _right, float _
tm[14] = temp[11];
return *this;
} // orthoProjection
}
Transform4x4f& Transform4x4f::invert(const Transform4x4f& _other)
{
@ -111,7 +120,7 @@ Transform4x4f& Transform4x4f::invert(const Transform4x4f& _other)
// tm[14] = -((om[ 0] * (om[ 5] * om[14] - om[ 6] * om[13])) - (om[ 4] * (om[ 1] * om[14] - om[ 2] * om[13])) + (om[12] * (om[ 1] * om[ 6] - om[ 2] * om[ 5])));
// tm[15] = ((om[ 0] * (om[ 5] * om[10] - om[ 6] * om[ 9])) - (om[ 4] * (om[ 1] * om[10] - om[ 2] * om[ 9])) + (om[ 8] * (om[ 1] * om[ 6] - om[ 2] * om[ 5])));
// Optimized invert ( om[3, 7 and 11] is always 0, and om[15] is always 1 )
// Optimized invert ( om[3, 7 and 11] is always 0, and om[15] is always 1 ).
tm[ 0] = ((om[ 5] * om[10]) - (om[ 9] * om[ 6]));
tm[ 1] = -((om[ 1] * om[10]) - (om[ 9] * om[ 2]));
tm[ 2] = ((om[ 1] * om[ 6]) - (om[ 5] * om[ 2]));
@ -151,8 +160,7 @@ Transform4x4f& Transform4x4f::invert(const Transform4x4f& _other)
tm[14] *= Determinant;
return *this;
} // invert
}
Transform4x4f& Transform4x4f::scale(const Vector3f& _scale)
{
@ -170,8 +178,7 @@ Transform4x4f& Transform4x4f::scale(const Vector3f& _scale)
tm[10] *= sv[2];
return *this;
} // scale
}
Transform4x4f& Transform4x4f::rotate(const float _angle, const Vector3f& _axis)
{
@ -219,8 +226,7 @@ Transform4x4f& Transform4x4f::rotate(const float _angle, const Vector3f& _axis)
tm[10] = temp[8];
return *this;
}; // rotate
}
Transform4x4f& Transform4x4f::rotateX(const float _angle)
{
@ -242,8 +248,7 @@ Transform4x4f& Transform4x4f::rotateX(const float _angle)
tm[10] = temp[5];
return *this;
}; // rotateX
}
Transform4x4f& Transform4x4f::rotateY(const float _angle)
{
@ -265,8 +270,7 @@ Transform4x4f& Transform4x4f::rotateY(const float _angle)
tm[10] = temp[5];
return *this;
}; // rotateY
}
Transform4x4f& Transform4x4f::rotateZ(const float _angle)
{
@ -288,8 +292,7 @@ Transform4x4f& Transform4x4f::rotateZ(const float _angle)
tm[ 6] = temp[5];
return *this;
}; // rotateZ
}
Transform4x4f& Transform4x4f::translate(const Vector3f& _translation)
{
@ -301,8 +304,7 @@ Transform4x4f& Transform4x4f::translate(const Vector3f& _translation)
tm[14] += tm[ 2] * tv[0] + tm[ 6] * tv[1] + tm[10] * tv[2];
return *this;
} // translate
}
Transform4x4f& Transform4x4f::round()
{
@ -313,5 +315,4 @@ Transform4x4f& Transform4x4f::round()
tm[14] = (float)(int)(tm[14] + 0.5f);
return *this;
} // round
}

View file

@ -1,3 +1,9 @@
//
// Transform4x4f.h
//
// 4x4 transform functions.
//
#pragma once
#ifndef ES_CORE_MATH_TRANSFORM4X4F_H
#define ES_CORE_MATH_TRANSFORM4X4F_H
@ -8,13 +14,21 @@
class Transform4x4f
{
public:
Transform4x4f() {}
Transform4x4f(const Vector4f& _r0, const Vector4f& _r1, const Vector4f& _r2, const Vector4f& _r3) : mR0(_r0), mR1(_r1), mR2(_r2), mR3(_r3) { }
Transform4x4f(
const Vector4f& _r0,
const Vector4f& _r1,
const Vector4f& _r2,
const Vector4f& _r3)
: mR0(_r0),
mR1(_r1),
mR2(_r2),
mR3(_r3) {}
const Transform4x4f operator*(const Transform4x4f& _other) const;
const Vector3f operator*(const Vector3f& _other) const;
Transform4x4f& operator*=(const Transform4x4f& _other) { *this = *this * _other; return *this; }
Transform4x4f& operator*=(const Transform4x4f& _other)
{ *this = *this * _other; return *this; }
inline Vector4f& r0() { return mR0; }
inline Vector4f& r1() { return mR1; }
@ -25,7 +39,13 @@ public:
inline const Vector4f& r2() const { return mR2; }
inline const Vector4f& r3() const { return mR3; }
Transform4x4f& orthoProjection(float _left, float _right, float _bottom, float _top, float _near, float _far);
Transform4x4f& orthoProjection(
float _left,
float _right,
float _bottom,
float _top,
float _near,
float _far);
Transform4x4f& invert(const Transform4x4f& _other);
Transform4x4f& scale(const Vector3f& _scale);
Transform4x4f& rotate(const float _angle, const Vector3f& _axis);
@ -38,15 +58,14 @@ public:
inline Vector3f& translation() { return mR3.v3(); }
inline const Vector3f& translation() const { return mR3.v3(); }
static const Transform4x4f Identity() { return { { 1, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 0, 1, 0 }, { 0, 0, 0, 1 } }; }
static const Transform4x4f Identity()
{ return { { 1, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 0, 1, 0 }, { 0, 0, 0, 1 } }; }
protected:
Vector4f mR0;
Vector4f mR1;
Vector4f mR2;
Vector4f mR3;
}; // Transform4x4f
};
#endif // ES_CORE_MATH_TRANSFORM4X4F_H

View file

@ -1,3 +1,9 @@
//
// Vector2f.cpp
//
// 2-dimensional floating point vector functions.
//
#include "math/Vector2f.h"
Vector2f& Vector2f::round()
@ -6,8 +12,7 @@ Vector2f& Vector2f::round()
mY = (float)(int)(mY + 0.5f);
return *this;
} // round
}
Vector2f& Vector2f::lerp(const Vector2f& _start, const Vector2f& _end, const float _fraction)
{
@ -15,5 +20,4 @@ Vector2f& Vector2f::lerp(const Vector2f& _start, const Vector2f& _end, const flo
mY = Math::lerp(_start.y(), _end.y(), _fraction);
return *this;
} // lerp
}

View file

@ -1,8 +1,15 @@
//
// Vector2f.h
//
// 2-dimensional floating point vector functions.
//
#pragma once
#ifndef ES_CORE_MATH_VECTOR2F_H
#define ES_CORE_MATH_VECTOR2F_H
#include "math/Misc.h"
#include <assert.h>
class Vector3f;
@ -11,20 +18,25 @@ class Vector4f;
class Vector2f
{
public:
Vector2f() {}
Vector2f(const float _f) : mX(_f), mY(_f) {}
Vector2f(const float _x, const float _y) : mX(_x), mY(_y) {}
explicit Vector2f(const Vector3f& _v) : mX(((Vector2f&)_v).mX), mY(((Vector2f&)_v).mY) {}
explicit Vector2f(const Vector4f& _v) : mX(((Vector2f&)_v).mX), mY(((Vector2f&)_v).mY) {}
const bool operator==(const Vector2f& _other) const { return ((mX == _other.mX) && (mY == _other.mY)); }
const bool operator!=(const Vector2f& _other) const { return ((mX != _other.mX) || (mY != _other.mY)); }
const bool operator==(const Vector2f& _other) const
{ return ((mX == _other.mX) && (mY == _other.mY)); }
const bool operator!=(const Vector2f& _other) const
{ return ((mX != _other.mX) || (mY != _other.mY)); }
const Vector2f operator+ (const Vector2f& _other) const { return { mX + _other.mX, mY + _other.mY }; }
const Vector2f operator- (const Vector2f& _other) const { return { mX - _other.mX, mY - _other.mY }; }
const Vector2f operator* (const Vector2f& _other) const { return { mX * _other.mX, mY * _other.mY }; }
const Vector2f operator/ (const Vector2f& _other) const { return { mX / _other.mX, mY / _other.mY }; }
const Vector2f operator+(const Vector2f& _other) const
{ return { mX + _other.mX, mY + _other.mY }; }
const Vector2f operator-(const Vector2f& _other) const
{ return { mX - _other.mX, mY - _other.mY }; }
const Vector2f operator*(const Vector2f& _other) const
{ return { mX * _other.mX, mY * _other.mY }; }
const Vector2f operator/(const Vector2f& _other) const
{ return { mX / _other.mX, mY / _other.mY }; }
const Vector2f operator+(const float& _other) const { return { mX + _other, mY + _other }; }
const Vector2f operator-(const float& _other) const { return { mX - _other, mY - _other }; }
@ -43,8 +55,10 @@ public:
Vector2f& operator*=(const float& _other) { *this = *this * _other; return *this; }
Vector2f& operator/=(const float& _other) { *this = *this / _other; return *this; }
float& operator[](const int _index) { assert(_index < 2 && "index out of range"); return (&mX)[_index]; }
const float& operator[](const int _index) const { assert(_index < 2 && "index out of range"); return (&mX)[_index]; }
float& operator[](const int _index)
{ assert(_index < 2 && "index out of range"); return (&mX)[_index]; }
const float& operator[](const int _index) const
{ assert(_index < 2 && "index out of range"); return (&mX)[_index]; }
inline float& x() { return mX; }
inline float& y() { return mY; }
@ -59,10 +73,8 @@ public:
static const Vector2f UnitY() { return { 0, 1 }; }
private:
float mX;
float mY;
}; // Vector2f
};
#endif // ES_CORE_MATH_VECTOR2F_H

View file

@ -1 +1,7 @@
//
// Vector2i.cpp
//
// 2-dimensional integer vector functions.
//
#include "math/Vector2i.h"

View file

@ -1,3 +1,9 @@
//
// Vector2i.h
//
// 2-dimensional integer vector functions.
//
#pragma once
#ifndef ES_CORE_MATH_VECTOR2I_H
#define ES_CORE_MATH_VECTOR2I_H
@ -7,18 +13,23 @@
class Vector2i
{
public:
Vector2i() {}
Vector2i(const int _i) : mX(_i), mY(_i) {}
Vector2i(const int _x, const int _y) : mX(_x), mY(_y) {}
const bool operator==(const Vector2i& _other) const { return ((mX == _other.mX) && (mY == _other.mY)); }
const bool operator!=(const Vector2i& _other) const { return ((mX != _other.mX) || (mY != _other.mY)); }
const bool operator==(const Vector2i& _other) const
{ return ((mX == _other.mX) && (mY == _other.mY)); }
const bool operator!=(const Vector2i& _other) const
{ return ((mX != _other.mX) || (mY != _other.mY)); }
const Vector2i operator+ (const Vector2i& _other) const { return { mX + _other.mX, mY + _other.mY }; }
const Vector2i operator- (const Vector2i& _other) const { return { mX - _other.mX, mY - _other.mY }; }
const Vector2i operator* (const Vector2i& _other) const { return { mX * _other.mX, mY * _other.mY }; }
const Vector2i operator/ (const Vector2i& _other) const { return { mX / _other.mX, mY / _other.mY }; }
const Vector2i operator+(const Vector2i& _other) const
{ return { mX + _other.mX, mY + _other.mY }; }
const Vector2i operator-(const Vector2i& _other) const
{ return { mX - _other.mX, mY - _other.mY }; }
const Vector2i operator*(const Vector2i& _other) const
{ return { mX * _other.mX, mY * _other.mY }; }
const Vector2i operator/(const Vector2i& _other) const
{ return { mX / _other.mX, mY / _other.mY }; }
const Vector2i operator+(const int& _other) const { return { mX + _other, mY + _other }; }
const Vector2i operator-(const int& _other) const { return { mX - _other, mY - _other }; }
@ -37,8 +48,10 @@ public:
Vector2i& operator*=(const int& _other) { *this = *this * _other; return *this; }
Vector2i& operator/=(const int& _other) { *this = *this / _other; return *this; }
int& operator[](const int _index) { assert(_index < 2 && "index out of range"); return (&mX)[_index]; }
const int& operator[](const int _index) const { assert(_index < 2 && "index out of range"); return (&mX)[_index]; }
int& operator[](const int _index)
{ assert(_index < 2 && "index out of range"); return (&mX)[_index]; }
const int& operator[](const int _index) const
{ assert(_index < 2 && "index out of range"); return (&mX)[_index]; }
inline int& x() { return mX; }
inline int& y() { return mY; }
@ -50,10 +63,8 @@ public:
static const Vector2i UnitY() { return { 0, 1 }; }
private:
int mX;
int mY;
}; // Vector2i
};
#endif // ES_CORE_MATH_VECTOR2I_H

View file

@ -1,3 +1,9 @@
//
// Vector3f.cpp
//
// 3-dimensional floating point vector functions.
//
#include "math/Vector3f.h"
Vector3f& Vector3f::round()
@ -7,8 +13,7 @@ Vector3f& Vector3f::round()
mZ = (float)(int)(mZ + 0.5f);
return *this;
} // round
}
Vector3f& Vector3f::lerp(const Vector3f& _start, const Vector3f& _end, const float _fraction)
{
@ -17,5 +22,4 @@ Vector3f& Vector3f::lerp(const Vector3f& _start, const Vector3f& _end, const flo
mZ = Math::lerp(_start.z(), _end.z(), _fraction);
return *this;
} // lerp
}

View file

@ -1,8 +1,15 @@
//
// Vector3f.h
//
// 3-dimensional floating point vector functions.
//
#pragma once
#ifndef ES_CORE_MATH_VECTOR3F_H
#define ES_CORE_MATH_VECTOR3F_H
#include "math/Misc.h"
#include <assert.h>
class Vector2f;
@ -11,26 +18,38 @@ class Vector4f;
class Vector3f
{
public:
Vector3f() {}
Vector3f(const float _f) : mX(_f), mY(_f), mZ(_f) {}
Vector3f(const float _x, const float _y, const float _z) : mX(_x), mY(_y), mZ(_z) {}
explicit Vector3f(const Vector2f& _v) : mX(((Vector3f&)_v).mX), mY(((Vector3f&)_v).mY), mZ(0) { }
explicit Vector3f(const Vector2f& _v, const float _z) : mX(((Vector3f&)_v).mX), mY(((Vector3f&)_v).mY), mZ(_z) { }
explicit Vector3f(const Vector4f& _v) : mX(((Vector3f&)_v).mX), mY(((Vector3f&)_v).mY), mZ(((Vector3f&)_v).mZ) { }
explicit Vector3f(const Vector2f& _v)
: mX(((Vector3f&)_v).mX), mY(((Vector3f&)_v).mY), mZ(0) {}
explicit Vector3f(const Vector2f& _v, const float _z)
: mX(((Vector3f&)_v).mX), mY(((Vector3f&)_v).mY), mZ(_z) {}
explicit Vector3f(const Vector4f& _v)
: mX(((Vector3f&)_v).mX), mY(((Vector3f&)_v).mY), mZ(((Vector3f&)_v).mZ) {}
const bool operator==(const Vector3f& _other) const { return ((mX == _other.mX) && (mY == _other.mY) && (mZ == _other.mZ)); }
const bool operator!=(const Vector3f& _other) const { return ((mX != _other.mX) || (mY != _other.mY) || (mZ != _other.mZ)); }
const bool operator==(const Vector3f& _other) const
{ return ((mX == _other.mX) && (mY == _other.mY) && (mZ == _other.mZ)); }
const bool operator!=(const Vector3f& _other) const
{ return ((mX != _other.mX) || (mY != _other.mY) || (mZ != _other.mZ)); }
const Vector3f operator+ (const Vector3f& _other) const { return { mX + _other.mX, mY + _other.mY, mZ + _other.mZ }; }
const Vector3f operator- (const Vector3f& _other) const { return { mX - _other.mX, mY - _other.mY, mZ - _other.mZ }; }
const Vector3f operator* (const Vector3f& _other) const { return { mX * _other.mX, mY * _other.mY, mZ * _other.mZ }; }
const Vector3f operator/ (const Vector3f& _other) const { return { mX / _other.mX, mY / _other.mY, mZ / _other.mZ }; }
const Vector3f operator+(const Vector3f& _other) const
{ return { mX + _other.mX, mY + _other.mY, mZ + _other.mZ }; }
const Vector3f operator-(const Vector3f& _other) const
{ return { mX - _other.mX, mY - _other.mY, mZ - _other.mZ }; }
const Vector3f operator*(const Vector3f& _other) const
{ return { mX * _other.mX, mY * _other.mY, mZ * _other.mZ }; }
const Vector3f operator/(const Vector3f& _other) const
{ return { mX / _other.mX, mY / _other.mY, mZ / _other.mZ }; }
const Vector3f operator+ (const float& _other) const { return { mX + _other, mY + _other, mZ + _other }; }
const Vector3f operator- (const float& _other) const { return { mX - _other, mY - _other, mZ - _other }; }
const Vector3f operator* (const float& _other) const { return { mX * _other, mY * _other, mZ * _other }; }
const Vector3f operator/ (const float& _other) const { return { mX / _other, mY / _other, mZ / _other }; }
const Vector3f operator+(const float& _other) const
{ return { mX + _other, mY + _other, mZ + _other }; }
const Vector3f operator-(const float& _other) const
{ return { mX - _other, mY - _other, mZ - _other }; }
const Vector3f operator*(const float& _other) const
{ return { mX * _other, mY * _other, mZ * _other }; }
const Vector3f operator/(const float& _other) const
{ return { mX / _other, mY / _other, mZ / _other }; }
const Vector3f operator-() const { return { -mX , -mY, -mZ }; }
@ -44,8 +63,10 @@ public:
Vector3f& operator*=(const float& _other) { *this = *this * _other; return *this; }
Vector3f& operator/=(const float& _other) { *this = *this / _other; return *this; }
float& operator[](const int _index) { assert(_index < 3 && "index out of range"); return (&mX)[_index]; }
const float& operator[](const int _index) const { assert(_index < 3 && "index out of range"); return (&mX)[_index]; }
float& operator[](const int _index)
{ assert(_index < 3 && "index out of range"); return (&mX)[_index]; }
const float& operator[](const int _index) const
{ assert(_index < 3 && "index out of range"); return (&mX)[_index]; }
inline float& x() { return mX; }
inline float& y() { return mY; }
@ -66,11 +87,9 @@ public:
static const Vector3f UnitZ() { return { 0, 0, 1 }; }
private:
float mX;
float mY;
float mZ;
}; // Vector3f
};
#endif // ES_CORE_MATH_VECTOR3F_H

View file

@ -1,3 +1,9 @@
//
// Vector4f.cpp
//
// 4-dimensional floating point vector functions.
//
#include "math/Vector4f.h"
Vector4f& Vector4f::round()
@ -8,8 +14,7 @@ Vector4f& Vector4f::round()
mW = (float)(int)(mW + 0.5f);
return *this;
} // round
}
Vector4f& Vector4f::lerp(const Vector4f& _start, const Vector4f& _end, const float _fraction)
{
@ -19,5 +24,4 @@ Vector4f& Vector4f::lerp(const Vector4f& _start, const Vector4f& _end, const flo
mW = Math::lerp(_start.w(), _end.w(), _fraction);
return *this;
} // lerp
}

View file

@ -1,8 +1,15 @@
//
// Vector4f.h
//
// 4-dimensional floating point vector functions.
//
#pragma once
#ifndef ES_CORE_MATH_VECTOR4F_H
#define ES_CORE_MATH_VECTOR4F_H
#include "math/Misc.h"
#include <assert.h>
class Vector2f;
@ -11,28 +18,45 @@ class Vector3f;
class Vector4f
{
public:
Vector4f() {}
Vector4f(const float _f) : mX(_f), mY(_f), mZ(_f), mW(_f) {}
Vector4f(const float _x, const float _y, const float _z, const float _w) : mX(_x), mY(_y), mZ(_z), mW(_w) { }
explicit Vector4f(const Vector2f& _v) : mX(((Vector4f&)_v).mX), mY(((Vector4f&)_v).mY), mZ(0), mW(0) { }
explicit Vector4f(const Vector2f& _v, const float _z) : mX(((Vector4f&)_v).mX), mY(((Vector4f&)_v).mY), mZ(_z), mW(0) { }
explicit Vector4f(const Vector2f& _v, const float _z, const float _w) : mX(((Vector4f&)_v).mX), mY(((Vector4f&)_v).mY), mZ(_z), mW(_w) { }
explicit Vector4f(const Vector3f& _v) : mX(((Vector4f&)_v).mX), mY(((Vector4f&)_v).mY), mZ(((Vector4f&)_v).mZ), mW(0) { }
explicit Vector4f(const Vector3f& _v, const float _w) : mX(((Vector4f&)_v).mX), mY(((Vector4f&)_v).mY), mZ(((Vector4f&)_v).mZ), mW(_w) { }
Vector4f(const float _x, const float _y, const float _z, const float _w)
: mX(_x), mY(_y), mZ(_z), mW(_w) {}
explicit Vector4f(const Vector2f& _v)
: mX(((Vector4f&)_v).mX), mY(((Vector4f&)_v).mY), mZ(0), mW(0) {}
explicit Vector4f(const Vector2f& _v, const float _z)
: mX(((Vector4f&)_v).mX), mY(((Vector4f&)_v).mY), mZ(_z), mW(0) {}
explicit Vector4f(const Vector2f& _v, const float _z, const float _w)
: mX(((Vector4f&)_v).mX), mY(((Vector4f&)_v).mY), mZ(_z), mW(_w) {}
explicit Vector4f(const Vector3f& _v)
: mX(((Vector4f&)_v).mX), mY(((Vector4f&)_v).mY), mZ(((Vector4f&)_v).mZ), mW(0) {}
explicit Vector4f(const Vector3f& _v, const float _w)
: mX(((Vector4f&)_v).mX), mY(((Vector4f&)_v).mY), mZ(((Vector4f&)_v).mZ), mW(_w) {}
const bool operator==(const Vector4f& _other) const { return ((mX == _other.mX) && (mY == _other.mY) && (mZ == _other.mZ) && (mW == _other.mW)); }
const bool operator!=(const Vector4f& _other) const { return ((mX != _other.mX) || (mY != _other.mY) || (mZ != _other.mZ) || (mW != _other.mW)); }
const bool operator==(const Vector4f& _other) const
{ return ((mX == _other.mX) && (mY == _other.mY) &&
(mZ == _other.mZ) && (mW == _other.mW)); }
const bool operator!=(const Vector4f& _other) const
{ return ((mX != _other.mX) || (mY != _other.mY) ||
(mZ != _other.mZ) || (mW != _other.mW)); }
const Vector4f operator+ (const Vector4f& _other) const { return { mX + _other.mX, mY + _other.mY, mZ + _other.mZ, mW + _other.mW }; }
const Vector4f operator- (const Vector4f& _other) const { return { mX - _other.mX, mY - _other.mY, mZ - _other.mZ, mW - _other.mW }; }
const Vector4f operator* (const Vector4f& _other) const { return { mX * _other.mX, mY * _other.mY, mZ * _other.mZ, mW * _other.mW }; }
const Vector4f operator/ (const Vector4f& _other) const { return { mX / _other.mX, mY / _other.mY, mZ / _other.mZ, mW / _other.mW }; }
const Vector4f operator+(const Vector4f& _other) const
{ return { mX + _other.mX, mY + _other.mY, mZ + _other.mZ, mW + _other.mW }; }
const Vector4f operator-(const Vector4f& _other) const
{ return { mX - _other.mX, mY - _other.mY, mZ - _other.mZ, mW - _other.mW }; }
const Vector4f operator*(const Vector4f& _other) const
{ return { mX * _other.mX, mY * _other.mY, mZ * _other.mZ, mW * _other.mW }; }
const Vector4f operator/(const Vector4f& _other) const
{ return { mX / _other.mX, mY / _other.mY, mZ / _other.mZ, mW / _other.mW }; }
const Vector4f operator+ (const float& _other) const { return { mX + _other, mY + _other, mZ + _other, mW + _other }; }
const Vector4f operator- (const float& _other) const { return { mX - _other, mY - _other, mZ - _other, mW - _other }; }
const Vector4f operator* (const float& _other) const { return { mX * _other, mY * _other, mZ * _other, mW * _other }; }
const Vector4f operator/ (const float& _other) const { return { mX / _other, mY / _other, mZ / _other, mW / _other }; }
const Vector4f operator+(const float& _other) const
{ return { mX + _other, mY + _other, mZ + _other, mW + _other }; }
const Vector4f operator-(const float& _other) const
{ return { mX - _other, mY - _other, mZ - _other, mW - _other }; }
const Vector4f operator*(const float& _other) const
{ return { mX * _other, mY * _other, mZ * _other, mW * _other }; }
const Vector4f operator/(const float& _other) const
{ return { mX / _other, mY / _other, mZ / _other, mW / _other }; }
const Vector4f operator-() const { return {-mX , -mY, -mZ, -mW }; }
@ -46,8 +70,10 @@ public:
Vector4f& operator*=(const float& _other) { *this = *this * _other; return *this; }
Vector4f& operator/=(const float& _other) { *this = *this / _other; return *this; }
float& operator[](const int _index) { assert(_index < 4 && "index out of range"); return (&mX)[_index]; }
const float& operator[](const int _index) const { assert(_index < 4 && "index out of range"); return (&mX)[_index]; }
float& operator[](const int _index)
{ assert(_index < 4 && "index out of range"); return (&mX)[_index]; }
const float& operator[](const int _index) const
{ assert(_index < 4 && "index out of range"); return (&mX)[_index]; }
inline float& x() { return mX; }
inline float& y() { return mY; }
@ -74,12 +100,10 @@ public:
static const Vector4f UnitW() { return { 0, 0, 0, 1 }; }
private:
float mX;
float mY;
float mZ;
float mW;
}; // Vector4f
};
#endif // ES_CORE_MATH_VECTOR4F_H

View file

@ -1,3 +1,9 @@
//
// Renderer.cpp
//
// Rendering functions.
//
#include "renderers/Renderer.h"
#include "math/Transform4x4f.h"
@ -27,11 +33,12 @@ namespace Renderer
{
size_t width = 0;
size_t height = 0;
ResourceData resData = ResourceManager::getInstance()->getFileData(":/graphics/window_icon_256.png");
std::vector<unsigned char> rawData = ImageIO::loadFromMemoryRGBA32(resData.ptr.get(), resData.length, width, height);
ResourceData resData =
ResourceManager::getInstance()->getFileData(":/graphics/window_icon_256.png");
std::vector<unsigned char> rawData =
ImageIO::loadFromMemoryRGBA32(resData.ptr.get(), resData.length, width, height);
if(!rawData.empty())
{
if (!rawData.empty()) {
ImageIO::flipPixelsVert(rawData.data(), width, height);
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
@ -45,24 +52,23 @@ namespace Renderer
unsigned int bmask = 0x00FF0000;
unsigned int amask = 0xFF000000;
#endif
// try creating SDL surface from logo data
SDL_Surface* logoSurface = SDL_CreateRGBSurfaceFrom((void*)rawData.data(), (int)width, (int)height, 32, (int)(width * 4), rmask, gmask, bmask, amask);
if(logoSurface != nullptr)
{
// Try creating SDL surface from logo data.
SDL_Surface* logoSurface = SDL_CreateRGBSurfaceFrom((void*)rawData.data(),
(int)width, (int)height, 32, (int)(width * 4), rmask, gmask, bmask, amask);
if (logoSurface != nullptr) {
SDL_SetWindowIcon(sdlWindow, logoSurface);
SDL_FreeSurface(logoSurface);
}
}
} // setIcon
}
static bool createWindow()
{
LOG(LogInfo) << "Creating window...";
if(SDL_Init(SDL_INIT_VIDEO) != 0)
{
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
LOG(LogError) << "Error initializing SDL!\n " << SDL_GetError();
return false;
}
@ -71,16 +77,23 @@ namespace Renderer
SDL_DisplayMode dispMode;
SDL_GetDesktopDisplayMode(0, &dispMode);
windowWidth = Settings::getInstance()->getInt("WindowWidth") ? Settings::getInstance()->getInt("WindowWidth") : dispMode.w;
windowHeight = Settings::getInstance()->getInt("WindowHeight") ? Settings::getInstance()->getInt("WindowHeight") : dispMode.h;
screenWidth = Settings::getInstance()->getInt("ScreenWidth") ? Settings::getInstance()->getInt("ScreenWidth") : windowWidth;
screenHeight = Settings::getInstance()->getInt("ScreenHeight") ? Settings::getInstance()->getInt("ScreenHeight") : windowHeight;
screenOffsetX = Settings::getInstance()->getInt("ScreenOffsetX") ? Settings::getInstance()->getInt("ScreenOffsetX") : 0;
screenOffsetY = Settings::getInstance()->getInt("ScreenOffsetY") ? Settings::getInstance()->getInt("ScreenOffsetY") : 0;
screenRotate = Settings::getInstance()->getInt("ScreenRotate") ? Settings::getInstance()->getInt("ScreenRotate") : 0;
windowWidth = Settings::getInstance()->getInt("WindowWidth") ?
Settings::getInstance()->getInt("WindowWidth") : dispMode.w;
windowHeight = Settings::getInstance()->getInt("WindowHeight") ?
Settings::getInstance()->getInt("WindowHeight") : dispMode.h;
screenWidth = Settings::getInstance()->getInt("ScreenWidth") ?
Settings::getInstance()->getInt("ScreenWidth") : windowWidth;
screenHeight = Settings::getInstance()->getInt("ScreenHeight") ?
Settings::getInstance()->getInt("ScreenHeight") : windowHeight;
screenOffsetX = Settings::getInstance()->getInt("ScreenOffsetX") ?
Settings::getInstance()->getInt("ScreenOffsetX") : 0;
screenOffsetY = Settings::getInstance()->getInt("ScreenOffsetY") ?
Settings::getInstance()->getInt("ScreenOffsetY") : 0;
screenRotate = Settings::getInstance()->getInt("ScreenRotate") ?
Settings::getInstance()->getInt("ScreenRotate") : 0;
// Prevent ES window from minimizing when switching windows
// (when launching games or when manually switching windows using task switcher)
// Prevent ES window from minimizing when switching windows (when launching
// games or when manually switching windows using task switcher).
SDL_SetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0");
setupWindow();
@ -88,21 +101,14 @@ namespace Renderer
unsigned int windowFlags;
if (Settings::getInstance()->getBool("Windowed"))
{
windowFlags = getWindowFlags();
}
else if (Settings::getInstance()->getString("FullscreenMode") == "borderless")
{
windowFlags = SDL_WINDOW_BORDERLESS | SDL_WINDOW_ALWAYS_ON_TOP | getWindowFlags();
}
else
{
windowFlags = SDL_WINDOW_FULLSCREEN | getWindowFlags();
}
if((sdlWindow = SDL_CreateWindow("EmulationStation", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, windowWidth, windowHeight, windowFlags)) == nullptr)
{
if ((sdlWindow = SDL_CreateWindow("EmulationStation", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, windowWidth, windowHeight, windowFlags)) == nullptr) {
LOG(LogError) << "Error creating SDL window!\n\t" << SDL_GetError();
return false;
}
@ -114,21 +120,18 @@ namespace Renderer
setSwapInterval();
return true;
} // createWindow
}
static void destroyWindow()
{
destroyContext();
SDL_DestroyWindow(sdlWindow);
sdlWindow = nullptr;
SDL_ShowCursor(initialCursorState);
SDL_Quit();
} // destroyWindow
}
bool init()
{
@ -138,52 +141,40 @@ namespace Renderer
Transform4x4f projection = Transform4x4f::Identity();
Rect viewport = Rect(0, 0, 0, 0);
switch(screenRotate)
{
case 0:
{
switch (screenRotate) {
case 0: {
viewport.x = screenOffsetX;
viewport.y = screenOffsetY;
viewport.w = screenWidth;
viewport.h = screenHeight;
projection.orthoProjection(0, screenWidth, screenHeight, 0, -1.0, 1.0);
}
break;
case 1:
{
case 1: {
viewport.x = windowWidth - screenOffsetY - screenHeight;
viewport.y = screenOffsetX;
viewport.w = screenHeight;
viewport.h = screenWidth;
projection.orthoProjection(0, screenHeight, screenWidth, 0, -1.0, 1.0);
projection.rotate((float)ES_DEG_TO_RAD(90), {0, 0, 1});
projection.translate({0, screenHeight * -1.0f, 0});
}
break;
case 2:
{
case 2: {
viewport.x = windowWidth - screenOffsetX - screenWidth;
viewport.y = windowHeight - screenOffsetY - screenHeight;
viewport.w = screenWidth;
viewport.h = screenHeight;
projection.orthoProjection(0, screenWidth, screenHeight, 0, -1.0, 1.0);
projection.rotate((float)ES_DEG_TO_RAD(180), {0, 0, 1});
projection.translate({screenWidth * -1.0f, screenHeight * -1.0f, 0});
}
break;
case 3:
{
case 3: {
viewport.x = screenOffsetY;
viewport.y = windowHeight - screenOffsetX - screenWidth;
viewport.w = screenHeight;
viewport.h = screenWidth;
projection.orthoProjection(0, screenHeight, screenWidth, 0, -1.0, 1.0);
projection.rotate((float)ES_DEG_TO_RAD(270), {0, 0, 1});
projection.translate({screenWidth * -1.0f, 0, 0});
@ -196,33 +187,46 @@ namespace Renderer
swapBuffers();
return true;
} // init
}
void deinit()
{
destroyWindow();
} // deinit
}
void pushClipRect(const Vector2i& _pos, const Vector2i& _size)
{
Rect box(_pos.x(), _pos.y(), _size.x(), _size.y());
if(box.w == 0) box.w = screenWidth - box.x;
if(box.h == 0) box.h = screenHeight - box.y;
if (box.w == 0)
box.w = screenWidth - box.x;
if (box.h == 0)
box.h = screenHeight - box.y;
switch(screenRotate)
{
case 0: { box = Rect(screenOffsetX + box.x, screenOffsetY + box.y, box.w, box.h); } break;
case 1: { box = Rect(windowWidth - screenOffsetY - box.y - box.h, screenOffsetX + box.x, box.h, box.w); } break;
case 2: { box = Rect(windowWidth - screenOffsetX - box.x - box.w, windowHeight - screenOffsetY - box.y - box.h, box.w, box.h); } break;
case 3: { box = Rect(screenOffsetY + box.y, windowHeight - screenOffsetX - box.x - box.w, box.h, box.w); } break;
switch (screenRotate) {
case 0: {
box = Rect(screenOffsetX + box.x, screenOffsetY + box.y, box.w, box.h);
}
break;
case 1: {
box = Rect(windowWidth - screenOffsetY - box.y - box.h, screenOffsetX +
box.x, box.h, box.w);
}
break;
case 2: {
box = Rect(windowWidth - screenOffsetX - box.x - box.w, windowHeight -
screenOffsetY - box.y - box.h, box.w, box.h);
}
break;
case 3: {
box = Rect(screenOffsetY + box.y, windowHeight - screenOffsetX - box.x -
box.w, box.h, box.w);
}
break;
}
// make sure the box fits within clipStack.top(), and clip further accordingly
if(clipStack.size())
{
// Make sure the box fits within clipStack.top(), and clip further accordingly.
if (clipStack.size()) {
const Rect& top = clipStack.top();
if ( top.x > box.x) box.x = top.x;
if ( top.y > box.y) box.y = top.y;
@ -236,25 +240,33 @@ namespace Renderer
clipStack.push(box);
setScissor(box);
} // pushClipRect
}
void popClipRect()
{
if(clipStack.empty())
{
if (clipStack.empty()) {
LOG(LogError) << "Tried to popClipRect while the stack was empty!";
return;
}
clipStack.pop();
if(clipStack.empty()) setScissor(Rect(0, 0, 0, 0));
else setScissor(clipStack.top());
if (clipStack.empty())
setScissor(Rect(0, 0, 0, 0));
else
setScissor(clipStack.top());
}
} // popClipRect
void drawRect(const float _x, const float _y, const float _w, const float _h, const unsigned int _color, const unsigned int _colorEnd, bool horizontalGradient, const Blend::Factor _srcBlendFactor, const Blend::Factor _dstBlendFactor)
void drawRect(
const float _x,
const float _y,
const float _w,
const float _h,
const unsigned int _color,
const unsigned int _colorEnd,
bool horizontalGradient,
const Blend::Factor _srcBlendFactor,
const Blend::Factor _dstBlendFactor)
{
const unsigned int color = convertColor(_color);
const unsigned int colorEnd = convertColor(_colorEnd);
@ -265,14 +277,13 @@ namespace Renderer
vertices[2] = { { _x + _w,_y }, { 0.0f, 0.0f }, horizontalGradient ? color : colorEnd };
vertices[3] = { { _x + _w,_y + _h }, { 0.0f, 0.0f }, colorEnd };
// round vertices
// Round vertices.
for (int i = 0; i < 4; ++i)
vertices[i].pos.round();
bindTexture(0);
drawTriangleStrips(vertices, 4, _srcBlendFactor, _dstBlendFactor);
} // drawRect
}
SDL_Window* getSDLWindow() { return sdlWindow; }
int getWindowWidth() { return windowWidth; }

View file

@ -1,3 +1,9 @@
//
// Renderer.h
//
// Rendering functions.
//
#pragma once
#ifndef ES_CORE_RENDERER_RENDERER_H
#define ES_CORE_RENDERER_RENDERER_H
@ -12,8 +18,7 @@ namespace Renderer
{
namespace Blend
{
enum Factor
{
enum Factor {
ZERO = 0,
ONE = 1,
SRC_COLOR = 2,
@ -24,50 +29,62 @@ namespace Renderer
ONE_MINUS_DST_COLOR = 7,
DST_ALPHA = 8,
ONE_MINUS_DST_ALPHA = 9
}; // Factor
} // Blend::
};
}
namespace Texture
{
enum Type
{
enum Type {
RGBA = 0,
ALPHA = 1
};
}
}; // Type
} // Texture::
struct Rect
{
Rect(const int _x, const int _y, const int _w, const int _h) : x(_x), y(_y), w(_w), h(_h) { }
struct Rect {
Rect(
const int _x,
const int _y,
const int _w,
const int _h)
: x(_x),
y(_y),
w(_w),
h(_h) {}
int x;
int y;
int w;
int h;
}; // Rect
};
struct Vertex
{
Vertex() {}
Vertex(const Vector2f& _pos, const Vector2f& _tex, const unsigned int _col) : pos(_pos), tex(_tex), col(_col) { }
Vertex(
const Vector2f& _pos,
const Vector2f& _tex,
const unsigned int _col)
: pos(_pos),
tex(_tex),
col(_col) { }
Vector2f pos;
Vector2f tex;
unsigned int col;
}; // Vertex
};
bool init();
void deinit();
void pushClipRect(const Vector2i& _pos, const Vector2i& _size);
void popClipRect();
void drawRect (const float _x, const float _y, const float _w, const float _h, const unsigned int _color, const unsigned int _colorEnd, bool horizontalGradient = false, const Blend::Factor _srcBlendFactor = Blend::SRC_ALPHA, const Blend::Factor _dstBlendFactor = Blend::ONE_MINUS_SRC_ALPHA);
void drawRect(
const float _x,
const float _y,
const float _w,
const float _h,
const unsigned int _color,
const unsigned int _colorEnd,
bool horizontalGradient = false,
const Blend::Factor _srcBlendFactor = Blend::SRC_ALPHA,
const Blend::Factor _dstBlendFactor = Blend::ONE_MINUS_SRC_ALPHA);
SDL_Window* getSDLWindow();
int getWindowWidth();
int getWindowHeight();
@ -77,18 +94,39 @@ namespace Renderer
int getScreenOffsetY();
int getScreenRotate();
// API specific
// API specific.
unsigned int convertColor(const unsigned int _color);
unsigned int getWindowFlags();
void setupWindow();
void createContext();
void destroyContext();
unsigned int createTexture (const Texture::Type _type, const bool _linear, const bool _repeat, const unsigned int _width, const unsigned int _height, void* _data);
unsigned int createTexture(
const Texture::Type _type,
const bool _linear,
const bool _repeat,
const unsigned int _width,
const unsigned int _height,
void* _data);
void destroyTexture(const unsigned int _texture);
void updateTexture (const unsigned int _texture, const Texture::Type _type, const unsigned int _x, const unsigned _y, const unsigned int _width, const unsigned int _height, void* _data);
void updateTexture(
const unsigned int _texture,
const Texture::Type _type,
const unsigned int _x,
const unsigned _y,
const unsigned int _width,
const unsigned int _height,
void* _data);
void bindTexture(const unsigned int _texture);
void drawLines (const Vertex* _vertices, const unsigned int _numVertices, const Blend::Factor _srcBlendFactor = Blend::SRC_ALPHA, const Blend::Factor _dstBlendFactor = Blend::ONE_MINUS_SRC_ALPHA);
void drawTriangleStrips(const Vertex* _vertices, const unsigned int _numVertices, const Blend::Factor _srcBlendFactor = Blend::SRC_ALPHA, const Blend::Factor _dstBlendFactor = Blend::ONE_MINUS_SRC_ALPHA);
void drawLines(
const Vertex* _vertices,
const unsigned int _numVertices,
const Blend::Factor _srcBlendFactor = Blend::SRC_ALPHA,
const Blend::Factor _dstBlendFactor = Blend::ONE_MINUS_SRC_ALPHA);
void drawTriangleStrips(
const Vertex* _vertices,
const unsigned int _numVertices,
const Blend::Factor _srcBlendFactor = Blend::SRC_ALPHA,
const Blend::Factor _dstBlendFactor = Blend::ONE_MINUS_SRC_ALPHA);
void setProjection(const Transform4x4f& _projection);
void setMatrix(const Transform4x4f& _matrix);
void setViewport(const Rect& _viewport);
@ -96,6 +134,6 @@ namespace Renderer
void setSwapInterval();
void swapBuffers();
} // Renderer::
}
#endif // ES_CORE_RENDERER_RENDERER_H

View file

@ -1,3 +1,9 @@
//
// Renderer_GL21.cpp
//
// OpenGL 2.1 rendering functions.
//
#if defined(USE_OPENGL_21)
#include "renderers/Renderer.h"
@ -10,7 +16,6 @@
namespace Renderer
{
#if defined(_DEBUG)
#define GL_CHECK_ERROR(Function) (Function, _GLCheckError(#Function))
@ -19,7 +24,8 @@ namespace Renderer
const GLenum errorCode = glGetError();
if (errorCode != GL_NO_ERROR) {
LOG(LogError) << "OpenGLES error: " << _funcName << " failed with error code: " << errorCode;
LOG(LogError) << "OpenGLES error: " << _funcName <<
" failed with error code: " << errorCode;
}
}
#else
@ -31,8 +37,7 @@ namespace Renderer
static GLenum convertBlendFactor(const Blend::Factor _blendFactor)
{
switch(_blendFactor)
{
switch (_blendFactor) {
case Blend::ZERO: { return GL_ZERO; } break;
case Blend::ONE: { return GL_ONE; } break;
case Blend::SRC_COLOR: { return GL_SRC_COLOR; } break;
@ -45,37 +50,32 @@ namespace Renderer
case Blend::ONE_MINUS_DST_ALPHA: { return GL_ONE_MINUS_DST_ALPHA; } break;
default: { return GL_ZERO; }
}
} // convertBlendFactor
}
static GLenum convertTextureType(const Texture::Type _type)
{
switch(_type)
{
switch (_type) {
case Texture::RGBA: { return GL_RGBA; } break;
case Texture::ALPHA: { return GL_ALPHA; } break;
default: { return GL_ZERO; }
}
} // convertTextureType
}
unsigned int convertColor(const unsigned int _color)
{
// convert from rgba to abgr
// Convert from rgba to abgr.
unsigned char r = ((_color & 0xff000000) >> 24) & 255;
unsigned char g = ((_color & 0x00ff0000) >> 16) & 255;
unsigned char b = ((_color & 0x0000ff00) >> 8) & 255;
unsigned char a = ((_color & 0x000000ff) ) & 255;
return ((a << 24) | (b << 16) | (g << 8) | (r));
} // convertColor
}
unsigned int getWindowFlags()
{
return SDL_WINDOW_OPENGL;
} // getWindowFlags
}
void setupWindow()
{
@ -89,25 +89,31 @@ namespace Renderer
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
} // setupWindow
}
void createContext()
{
sdlContext = SDL_GL_CreateContext(getSDLWindow());
SDL_GL_MakeCurrent(getSDLWindow(), sdlContext);
std::string vendor = glGetString(GL_VENDOR) ? (const char*)glGetString(GL_VENDOR) : "";
std::string renderer = glGetString(GL_RENDERER) ? (const char*)glGetString(GL_RENDERER) : "";
std::string version = glGetString(GL_VERSION) ? (const char*)glGetString(GL_VERSION) : "";
std::string extensions = glGetString(GL_EXTENSIONS) ? (const char*)glGetString(GL_EXTENSIONS) : "";
std::string vendor = glGetString(GL_VENDOR) ?
(const char*)glGetString(GL_VENDOR) : "";
std::string renderer = glGetString(GL_RENDERER) ?
(const char*)glGetString(GL_RENDERER) : "";
std::string version = glGetString(GL_VERSION) ?
(const char*)glGetString(GL_VERSION) : "";
std::string extensions = glGetString(GL_EXTENSIONS) ?
(const char*)glGetString(GL_EXTENSIONS) : "";
LOG(LogInfo) << "GL vendor: " << vendor;
LOG(LogInfo) << "GL renderer: " << renderer;
LOG(LogInfo) << "GL version: " << version;
LOG(LogInfo) << "Checking available OpenGL extensions...";
std::string glExts = glGetString(GL_EXTENSIONS) ? (const char*)glGetString(GL_EXTENSIONS) : "";
LOG(LogInfo) << "ARB_texture_non_power_of_two: " << (extensions.find("ARB_texture_non_power_of_two") != std::string::npos ? "ok" : "MISSING");
std::string glExts = glGetString(GL_EXTENSIONS) ?
(const char*)glGetString(GL_EXTENSIONS) : "";
LOG(LogInfo) << "ARB_texture_non_power_of_two: " <<
(extensions.find("ARB_texture_non_power_of_two") !=
std::string::npos ? "ok" : "MISSING");
uint8_t data[4] = {255, 255, 255, 255};
whiteTexture = createTexture(Texture::RGBA, false, true, 1, 1, data);
@ -120,17 +126,21 @@ namespace Renderer
GL_CHECK_ERROR(glEnableClientState(GL_VERTEX_ARRAY));
GL_CHECK_ERROR(glEnableClientState(GL_TEXTURE_COORD_ARRAY));
GL_CHECK_ERROR(glEnableClientState(GL_COLOR_ARRAY));
} // createContext
}
void destroyContext()
{
SDL_GL_DeleteContext(sdlContext);
sdlContext = nullptr;
}
} // destroyContext
unsigned int createTexture(const Texture::Type _type, const bool _linear, const bool _repeat, const unsigned int _width, const unsigned int _height, void* _data)
unsigned int createTexture(
const Texture::Type _type,
const bool _linear,
const bool _repeat,
const unsigned int _width,
const unsigned int _height,
void* _data)
{
const GLenum type = convertTextureType(_type);
unsigned int texture;
@ -138,71 +148,87 @@ namespace Renderer
GL_CHECK_ERROR(glGenTextures(1, &texture));
GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, texture));
GL_CHECK_ERROR(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, _repeat ? GL_REPEAT : GL_CLAMP_TO_EDGE));
GL_CHECK_ERROR(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, _repeat ? GL_REPEAT : GL_CLAMP_TO_EDGE));
GL_CHECK_ERROR(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, _linear ? GL_LINEAR : GL_NEAREST));
GL_CHECK_ERROR(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, _repeat ?
GL_REPEAT : GL_CLAMP_TO_EDGE));
GL_CHECK_ERROR(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, _repeat ?
GL_REPEAT : GL_CLAMP_TO_EDGE));
GL_CHECK_ERROR(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, _linear ?
GL_LINEAR : GL_NEAREST));
GL_CHECK_ERROR(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
GL_CHECK_ERROR(glTexImage2D(GL_TEXTURE_2D, 0, type, _width, _height, 0, type, GL_UNSIGNED_BYTE, _data));
GL_CHECK_ERROR(glTexImage2D(GL_TEXTURE_2D, 0, type, _width, _height, 0, type,
GL_UNSIGNED_BYTE, _data));
return texture;
} // createTexture
}
void destroyTexture(const unsigned int _texture)
{
GL_CHECK_ERROR(glDeleteTextures(1, &_texture));
}
} // destroyTexture
void updateTexture(const unsigned int _texture, const Texture::Type _type, const unsigned int _x, const unsigned _y, const unsigned int _width, const unsigned int _height, void* _data)
void updateTexture(
const unsigned int _texture,
const Texture::Type _type,
const unsigned int _x,
const unsigned _y,
const unsigned int _width,
const unsigned int _height,
void* _data)
{
const GLenum type = convertTextureType(_type);
GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, _texture));
GL_CHECK_ERROR(glTexSubImage2D(GL_TEXTURE_2D, 0, _x, _y, _width, _height, type, GL_UNSIGNED_BYTE, _data));
GL_CHECK_ERROR(glTexSubImage2D(GL_TEXTURE_2D, 0, _x, _y, _width, _height,
type, GL_UNSIGNED_BYTE, _data));
GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, whiteTexture));
} // updateTexture
}
void bindTexture(const unsigned int _texture)
{
if(_texture == 0) GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, whiteTexture));
else GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, _texture));
if (_texture == 0)
GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, whiteTexture));
else
GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, _texture));
}
} // bindTexture
void drawLines(const Vertex* _vertices, const unsigned int _numVertices, const Blend::Factor _srcBlendFactor, const Blend::Factor _dstBlendFactor)
void drawLines(
const Vertex* _vertices,
const unsigned int _numVertices,
const Blend::Factor _srcBlendFactor,
const Blend::Factor _dstBlendFactor)
{
GL_CHECK_ERROR(glVertexPointer(2, GL_FLOAT, sizeof(Vertex), &_vertices[0].pos));
GL_CHECK_ERROR(glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &_vertices[0].tex));
GL_CHECK_ERROR(glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(Vertex), &_vertices[0].col));
GL_CHECK_ERROR(glBlendFunc(convertBlendFactor(_srcBlendFactor), convertBlendFactor(_dstBlendFactor)));
GL_CHECK_ERROR(glBlendFunc(convertBlendFactor(_srcBlendFactor),
convertBlendFactor(_dstBlendFactor)));
GL_CHECK_ERROR(glDrawArrays(GL_LINES, 0, _numVertices));
}
} // drawLines
void drawTriangleStrips(const Vertex* _vertices, const unsigned int _numVertices, const Blend::Factor _srcBlendFactor, const Blend::Factor _dstBlendFactor)
void drawTriangleStrips(
const Vertex* _vertices,
const unsigned int _numVertices,
const Blend::Factor _srcBlendFactor,
const Blend::Factor _dstBlendFactor)
{
GL_CHECK_ERROR(glVertexPointer(2, GL_FLOAT, sizeof(Vertex), &_vertices[0].pos));
GL_CHECK_ERROR(glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &_vertices[0].tex));
GL_CHECK_ERROR(glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), &_vertices[0].col));
GL_CHECK_ERROR(glBlendFunc(convertBlendFactor(_srcBlendFactor), convertBlendFactor(_dstBlendFactor)));
GL_CHECK_ERROR(glBlendFunc(convertBlendFactor(_srcBlendFactor),
convertBlendFactor(_dstBlendFactor)));
GL_CHECK_ERROR(glDrawArrays(GL_TRIANGLE_STRIP, 0, _numVertices));
} // drawTriangleStrips
}
void setProjection(const Transform4x4f& _projection)
{
GL_CHECK_ERROR(glMatrixMode(GL_PROJECTION));
GL_CHECK_ERROR(glLoadMatrixf((GLfloat*)&_projection));
} // setProjection
}
void setMatrix(const Transform4x4f& _matrix)
{
@ -211,57 +237,51 @@ namespace Renderer
GL_CHECK_ERROR(glMatrixMode(GL_MODELVIEW));
GL_CHECK_ERROR(glLoadMatrixf((GLfloat*)&matrix));
} // setMatrix
}
void setViewport(const Rect& _viewport)
{
// glViewport starts at the bottom left of the window
GL_CHECK_ERROR(glViewport( _viewport.x, getWindowHeight() - _viewport.y - _viewport.h, _viewport.w, _viewport.h));
} // setViewport
// glViewport starts at the bottom left of the window.
GL_CHECK_ERROR(glViewport( _viewport.x, getWindowHeight() -
_viewport.y - _viewport.h, _viewport.w, _viewport.h));
}
void setScissor(const Rect& _scissor)
{
if((_scissor.x == 0) && (_scissor.y == 0) && (_scissor.w == 0) && (_scissor.h == 0))
{
if ((_scissor.x == 0) && (_scissor.y == 0) && (_scissor.w == 0) && (_scissor.h == 0)) {
GL_CHECK_ERROR(glDisable(GL_SCISSOR_TEST));
}
else
{
// glScissor starts at the bottom left of the window
GL_CHECK_ERROR(glScissor(_scissor.x, getWindowHeight() - _scissor.y - _scissor.h, _scissor.w, _scissor.h));
else {
// glScissor starts at the bottom left of the window.
GL_CHECK_ERROR(glScissor(_scissor.x, getWindowHeight() -
_scissor.y - _scissor.h, _scissor.w, _scissor.h));
GL_CHECK_ERROR(glEnable(GL_SCISSOR_TEST));
}
} // setScissor
}
void setSwapInterval()
{
// vsync
if(Settings::getInstance()->getBool("VSync"))
{
// vsync.
if (Settings::getInstance()->getBool("VSync")) {
// SDL_GL_SetSwapInterval(0) for immediate updates (no vsync, default),
// 1 for updates synchronized with the vertical retrace,
// or -1 for late swap tearing.
// SDL_GL_SetSwapInterval returns 0 on success, -1 on error.
// if vsync is requested, try normal vsync; if that doesn't work, try late swap tearing
// if that doesn't work, report an error
// if that doesn't work, report an error.
if (SDL_GL_SetSwapInterval(1) != 0 && SDL_GL_SetSwapInterval(-1) != 0) {
LOG(LogWarning) << "Tried to enable vsync, but failed! (" << SDL_GetError() << ")";
}
}
else
SDL_GL_SetSwapInterval(0);
} // setSwapInterval
}
void swapBuffers()
{
SDL_GL_SwapWindow(getSDLWindow());
GL_CHECK_ERROR(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
} // swapBuffers
}
} // Renderer::

View file

@ -1,3 +1,9 @@
//
// Renderer_GLES10.cpp
//
// OpenGL ES 1.0 rendering functions.
//
#if defined(USE_OPENGLES_10)
#include "renderers/Renderer.h"
@ -10,7 +16,6 @@
namespace Renderer
{
#if defined(_DEBUG)
#define GL_CHECK_ERROR(Function) (Function, _GLCheckError(#Function))
@ -18,8 +23,10 @@ namespace Renderer
{
const GLenum errorCode = glGetError();
if(errorCode != GL_NO_ERROR)
LOG(LogError) << "OpenGLES error: " << _funcName << " failed with error code: " << errorCode;
if (errorCode != GL_NO_ERROR) {
LOG(LogError) << "OpenGLES error: " << _funcName <<
" failed with error code: " << errorCode;
}
}
#else
#define GL_CHECK_ERROR(Function) (Function)
@ -30,8 +37,7 @@ namespace Renderer
static GLenum convertBlendFactor(const Blend::Factor _blendFactor)
{
switch(_blendFactor)
{
switch (_blendFactor) {
case Blend::ZERO: { return GL_ZERO; } break;
case Blend::ONE: { return GL_ONE; } break;
case Blend::SRC_COLOR: { return GL_SRC_COLOR; } break;
@ -44,37 +50,32 @@ namespace Renderer
case Blend::ONE_MINUS_DST_ALPHA: { return GL_ONE_MINUS_DST_ALPHA; } break;
default: { return GL_ZERO; }
}
} // convertBlendFactor
}
static GLenum convertTextureType(const Texture::Type _type)
{
switch(_type)
{
switch (_type) {
case Texture::RGBA: { return GL_RGBA; } break;
case Texture::ALPHA: { return GL_ALPHA; } break;
default: { return GL_ZERO; }
}
} // convertTextureType
}
unsigned int convertColor(const unsigned int _color)
{
// convert from rgba to abgr
// Convert from rgba to abgr.
unsigned char r = ((_color & 0xff000000) >> 24) & 255;
unsigned char g = ((_color & 0x00ff0000) >> 16) & 255;
unsigned char b = ((_color & 0x0000ff00) >> 8) & 255;
unsigned char a = ((_color & 0x000000ff) ) & 255;
return ((a << 24) | (b << 16) | (g << 8) | (r));
} // convertColor
}
unsigned int getWindowFlags()
{
return SDL_WINDOW_OPENGL;
} // getWindowFlags
}
void setupWindow()
{
@ -88,25 +89,31 @@ namespace Renderer
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
} // setupWindow
}
void createContext()
{
sdlContext = SDL_GL_CreateContext(getSDLWindow());
SDL_GL_MakeCurrent(getSDLWindow(), sdlContext);
std::string vendor = glGetString(GL_VENDOR) ? (const char*)glGetString(GL_VENDOR) : "";
std::string renderer = glGetString(GL_RENDERER) ? (const char*)glGetString(GL_RENDERER) : "";
std::string version = glGetString(GL_VERSION) ? (const char*)glGetString(GL_VERSION) : "";
std::string extensions = glGetString(GL_EXTENSIONS) ? (const char*)glGetString(GL_EXTENSIONS) : "";
std::string vendor = glGetString(GL_VENDOR) ?
(const char*)glGetString(GL_VENDOR) : "";
std::string renderer = glGetString(GL_RENDERER) ?
(const char*)glGetString(GL_RENDERER) : "";
std::string version = glGetString(GL_VERSION) ?
(const char*)glGetString(GL_VERSION) : "";
std::string extensions = glGetString(GL_EXTENSIONS) ?
(const char*)glGetString(GL_EXTENSIONS) : "";
LOG(LogInfo) << "GL vendor: " << vendor;
LOG(LogInfo) << "GL renderer: " << renderer;
LOG(LogInfo) << "GL version: " << version;
LOG(LogInfo) << "Checking available OpenGL extensions...";
std::string glExts = glGetString(GL_EXTENSIONS) ? (const char*)glGetString(GL_EXTENSIONS) : "";
LOG(LogInfo) << "ARB_texture_non_power_of_two: " << (extensions.find("ARB_texture_non_power_of_two") != std::string::npos ? "ok" : "MISSING");
std::string glExts = glGetString(GL_EXTENSIONS) ?
(const char*)glGetString(GL_EXTENSIONS) : "";
LOG(LogInfo) << "ARB_texture_non_power_of_two: " <<
(extensions.find("ARB_texture_non_power_of_two") !=
std::string::npos ? "ok" : "MISSING");
uint8_t data[4] = {255, 255, 255, 255};
whiteTexture = createTexture(Texture::RGBA, false, true, 1, 1, data);
@ -119,17 +126,21 @@ namespace Renderer
GL_CHECK_ERROR(glEnableClientState(GL_VERTEX_ARRAY));
GL_CHECK_ERROR(glEnableClientState(GL_TEXTURE_COORD_ARRAY));
GL_CHECK_ERROR(glEnableClientState(GL_COLOR_ARRAY));
} // createContext
}
void destroyContext()
{
SDL_GL_DeleteContext(sdlContext);
sdlContext = nullptr;
}
} // destroyContext
unsigned int createTexture(const Texture::Type _type, const bool _linear, const bool _repeat, const unsigned int _width, const unsigned int _height, void* _data)
unsigned int createTexture(
const Texture::Type _type,
const bool _linear,
const bool _repeat,
const unsigned int _width,
const unsigned int _height,
void* _data)
{
const GLenum type = convertTextureType(_type);
unsigned int texture;
@ -137,71 +148,87 @@ namespace Renderer
GL_CHECK_ERROR(glGenTextures(1, &texture));
GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, texture));
GL_CHECK_ERROR(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, _repeat ? GL_REPEAT : GL_CLAMP_TO_EDGE));
GL_CHECK_ERROR(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, _repeat ? GL_REPEAT : GL_CLAMP_TO_EDGE));
GL_CHECK_ERROR(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, _linear ? GL_LINEAR : GL_NEAREST));
GL_CHECK_ERROR(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, _repeat ?
GL_REPEAT : GL_CLAMP_TO_EDGE));
GL_CHECK_ERROR(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, _repeat ?
GL_REPEAT : GL_CLAMP_TO_EDGE));
GL_CHECK_ERROR(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, _linear ?
GL_LINEAR : GL_NEAREST));
GL_CHECK_ERROR(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
GL_CHECK_ERROR(glTexImage2D(GL_TEXTURE_2D, 0, type, _width, _height, 0, type, GL_UNSIGNED_BYTE, _data));
GL_CHECK_ERROR(glTexImage2D(GL_TEXTURE_2D, 0, type, _width, _height, 0, type,
GL_UNSIGNED_BYTE, _data));
return texture;
} // createTexture
}
void destroyTexture(const unsigned int _texture)
{
GL_CHECK_ERROR(glDeleteTextures(1, &_texture));
}
} // destroyTexture
void updateTexture(const unsigned int _texture, const Texture::Type _type, const unsigned int _x, const unsigned _y, const unsigned int _width, const unsigned int _height, void* _data)
void updateTexture(
const unsigned int _texture,
const Texture::Type _type,
const unsigned int _x,
const unsigned _y,
const unsigned int _width,
const unsigned int _height,
void* _data)
{
const GLenum type = convertTextureType(_type);
GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, _texture));
GL_CHECK_ERROR(glTexSubImage2D(GL_TEXTURE_2D, 0, _x, _y, _width, _height, type, GL_UNSIGNED_BYTE, _data));
GL_CHECK_ERROR(glTexSubImage2D(GL_TEXTURE_2D, 0, _x, _y, _width, _height, type,
GL_UNSIGNED_BYTE, _data));
GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, whiteTexture));
} // updateTexture
}
void bindTexture(const unsigned int _texture)
{
if(_texture == 0) GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, whiteTexture));
else GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, _texture));
if (_texture == 0)
GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, whiteTexture));
else
GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, _texture));
}
} // bindTexture
void drawLines(const Vertex* _vertices, const unsigned int _numVertices, const Blend::Factor _srcBlendFactor, const Blend::Factor _dstBlendFactor)
void drawLines(
const Vertex* _vertices,
const unsigned int _numVertices,
const Blend::Factor _srcBlendFactor,
const Blend::Factor _dstBlendFactor)
{
GL_CHECK_ERROR(glVertexPointer(2, GL_FLOAT, sizeof(Vertex), &_vertices[0].pos));
GL_CHECK_ERROR(glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &_vertices[0].tex));
GL_CHECK_ERROR(glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), &_vertices[0].col));
GL_CHECK_ERROR(glBlendFunc(convertBlendFactor(_srcBlendFactor), convertBlendFactor(_dstBlendFactor)));
GL_CHECK_ERROR(glBlendFunc(convertBlendFactor(_srcBlendFactor),
convertBlendFactor(_dstBlendFactor)));
GL_CHECK_ERROR(glDrawArrays(GL_LINES, 0, _numVertices));
}
} // drawLines
void drawTriangleStrips(const Vertex* _vertices, const unsigned int _numVertices, const Blend::Factor _srcBlendFactor, const Blend::Factor _dstBlendFactor)
void drawTriangleStrips(
const Vertex* _vertices,
const unsigned int _numVertices,
const Blend::Factor _srcBlendFactor,
const Blend::Factor _dstBlendFactor)
{
GL_CHECK_ERROR(glVertexPointer(2, GL_FLOAT, sizeof(Vertex), &_vertices[0].pos));
GL_CHECK_ERROR(glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &_vertices[0].tex));
GL_CHECK_ERROR(glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), &_vertices[0].col));
GL_CHECK_ERROR(glBlendFunc(convertBlendFactor(_srcBlendFactor), convertBlendFactor(_dstBlendFactor)));
GL_CHECK_ERROR(glBlendFunc(convertBlendFactor(_srcBlendFactor),
convertBlendFactor(_dstBlendFactor)));
GL_CHECK_ERROR(glDrawArrays(GL_TRIANGLE_STRIP, 0, _numVertices));
} // drawTriangleStrips
}
void setProjection(const Transform4x4f& _projection)
{
GL_CHECK_ERROR(glMatrixMode(GL_PROJECTION));
GL_CHECK_ERROR(glLoadMatrixf((GLfloat*)&_projection));
} // setProjection
}
void setMatrix(const Transform4x4f& _matrix)
{
@ -210,56 +237,52 @@ namespace Renderer
GL_CHECK_ERROR(glMatrixMode(GL_MODELVIEW));
GL_CHECK_ERROR(glLoadMatrixf((GLfloat*)&matrix));
} // setMatrix
}
void setViewport(const Rect& _viewport)
{
// glViewport starts at the bottom left of the window
GL_CHECK_ERROR(glViewport( _viewport.x, getWindowHeight() - _viewport.y - _viewport.h, _viewport.w, _viewport.h));
} // setViewport
// glViewport starts at the bottom left of the window.
GL_CHECK_ERROR(glViewport( _viewport.x, getWindowHeight() -
_viewport.y - _viewport.h, _viewport.w, _viewport.h));
}
void setScissor(const Rect& _scissor)
{
if((_scissor.x == 0) && (_scissor.y == 0) && (_scissor.w == 0) && (_scissor.h == 0))
{
if ((_scissor.x == 0) && (_scissor.y == 0) && (_scissor.w == 0) && (_scissor.h == 0)) {
GL_CHECK_ERROR(glDisable(GL_SCISSOR_TEST));
}
else
{
// glScissor starts at the bottom left of the window
GL_CHECK_ERROR(glScissor(_scissor.x, getWindowHeight() - _scissor.y - _scissor.h, _scissor.w, _scissor.h));
else {
// glScissor starts at the bottom left of the window.
GL_CHECK_ERROR(glScissor(_scissor.x, getWindowHeight() -
_scissor.y - _scissor.h, _scissor.w, _scissor.h));
GL_CHECK_ERROR(glEnable(GL_SCISSOR_TEST));
}
} // setScissor
}
void setSwapInterval()
{
// vsync
if(Settings::getInstance()->getBool("VSync"))
{
// vsync.
if (Settings::getInstance()->getBool("VSync")) {
// SDL_GL_SetSwapInterval(0) for immediate updates (no vsync, default),
// 1 for updates synchronized with the vertical retrace,
// or -1 for late swap tearing.
// SDL_GL_SetSwapInterval returns 0 on success, -1 on error.
// if vsync is requested, try normal vsync; if that doesn't work, try late swap tearing
// if that doesn't work, report an error
if(SDL_GL_SetSwapInterval(1) != 0 && SDL_GL_SetSwapInterval(-1) != 0)
// if that doesn't work, report an error.
if (SDL_GL_SetSwapInterval(1) != 0 && SDL_GL_SetSwapInterval(-1) != 0) {
LOG(LogWarning) << "Tried to enable vsync, but failed! (" << SDL_GetError() << ")";
}
else
}
else {
SDL_GL_SetSwapInterval(0);
} // setSwapInterval
}
}
void swapBuffers()
{
SDL_GL_SwapWindow(getSDLWindow());
GL_CHECK_ERROR(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
} // swapBuffers
}
} // Renderer::