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 * 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 * 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) * 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 * 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) * 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" #include "CECInput.h"
#ifdef HAVE_LIBCEC #ifdef HAVE_LIBCEC
#include "Log.h" #include "Log.h"
#include <iostream> // Bad bad cecloader.
#include <libcec/cec.h> #include <libcec/cec.h>
#include <iostream> // bad bad cecloader
#include <libcec/cecloader.h> #include <libcec/cecloader.h>
#include <SDL_events.h> #include <SDL_events.h>
#ifdef _RPI_ #ifdef _RPI_
@ -15,24 +22,24 @@ extern "C" {
#endif // _RPI_ #endif // _RPI_
#endif // HAVE_LIBCEC #endif // HAVE_LIBCEC
// hack for cec support // Hack for CEC support.
extern int SDL_USER_CECBUTTONDOWN; extern int SDL_USER_CECBUTTONDOWN;
extern int SDL_USER_CECBUTTONUP; extern int SDL_USER_CECBUTTONUP;
CECInput* CECInput::sInstance = nullptr; CECInput* CECInput::sInstance = nullptr;
#ifdef HAVE_LIBCEC #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); LOG(LogDebug) << "CECInput::onAlert type: " << CECInput::getAlertTypeString(type) <<
" parameter: " << (char*)(param.paramData);
} // onAlert }
static void onCommand(void* /*cbParam*/, const CEC::cec_command* command) static void onCommand(void* /*cbParam*/, const CEC::cec_command* command)
{ {
LOG(LogDebug) << "CECInput::onCommand opcode: " << CECInput::getOpCodeString(command->opcode); LOG(LogDebug) << "CECInput::onCommand opcode: " << CECInput::getOpCodeString(command->opcode);
}
} // onCommand
static void onKeyPress(void* /*cbParam*/, const CEC::cec_keypress* key) 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.type = (key->duration > 0) ? SDL_USER_CECBUTTONUP : SDL_USER_CECBUTTONDOWN;
event.user.code = key->keycode; event.user.code = key->keycode;
SDL_PushEvent(&event); SDL_PushEvent(&event);
}
} // onKeyPress
static void onLogMessage(void* /*cbParam*/, const CEC::cec_log_message* message) static void onLogMessage(void* /*cbParam*/, const CEC::cec_log_message* message)
{ {
LOG(LogDebug) << "CECInput::onLogMessage message: " << message->message; LOG(LogDebug) << "CECInput::onLogMessage message: " << message->message;
}
} // onLogMessage
#ifdef _RPI_ #ifdef _RPI_
static void vchi_tv_and_cec_init() 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_tv_init(vchi_instance, &vchi_connection, 1);
vc_vchi_cec_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() static void vchi_tv_and_cec_deinit()
{ {
vc_vchi_cec_stop(); vc_vchi_cec_stop();
vc_vchi_tv_stop(); vc_vchi_tv_stop();
}
} // vchi_tv_and_cec_deinit
#endif // _RPI_ #endif // _RPI_
#endif // HAVE_LIBCEC #endif // HAVE_LIBCEC
@ -76,25 +79,22 @@ void CECInput::init()
{ {
if (!sInstance) if (!sInstance)
sInstance = new CECInput(); sInstance = new CECInput();
}
} // init
void CECInput::deinit() void CECInput::deinit()
{ {
if(sInstance) if (sInstance) {
{
delete sInstance; delete sInstance;
sInstance = nullptr; sInstance = nullptr;
} }
}
} // deinit
CECInput::CECInput() : mlibCEC(nullptr) CECInput::CECInput() : mlibCEC(nullptr)
{ {
#ifdef HAVE_LIBCEC #ifdef HAVE_LIBCEC
#ifdef _RPI_ #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_deinit();
vchi_tv_and_cec_init(); vchi_tv_and_cec_init();
#endif // _RPI_ #endif // _RPI_
@ -117,8 +117,7 @@ CECInput::CECInput() : mlibCEC(nullptr)
mlibCEC = LibCecInitialise(&config); mlibCEC = LibCecInitialise(&config);
if(!mlibCEC) if (!mlibCEC) {
{
LOG(LogError) << "CECInput::LibCecInitialise failed"; LOG(LogError) << "CECInput::LibCecInitialise failed";
return; return;
} }
@ -126,8 +125,7 @@ CECInput::CECInput() : mlibCEC(nullptr)
CEC::cec_adapter_descriptor adapters[10]; CEC::cec_adapter_descriptor adapters[10];
int numAdapters = mlibCEC->DetectAdapters(adapters, 10, nullptr, true); int numAdapters = mlibCEC->DetectAdapters(adapters, 10, nullptr, true);
if(numAdapters <= 0) if (numAdapters <= 0) {
{
LOG(LogError) << "CECInput::mAdapter->DetectAdapters failed"; LOG(LogError) << "CECInput::mAdapter->DetectAdapters failed";
UnloadLibCec(mlibCEC); UnloadLibCec(mlibCEC);
mlibCEC = nullptr; mlibCEC = nullptr;
@ -135,43 +133,38 @@ CECInput::CECInput() : mlibCEC(nullptr)
} }
for (int i = 0; i < numAdapters; ++i) 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"; LOG(LogError) << "CECInput::mAdapter->Open failed";
UnloadLibCec(mlibCEC); UnloadLibCec(mlibCEC);
mlibCEC = nullptr; mlibCEC = nullptr;
return; return;
} }
#endif // HAVE_LIBCEC #endif // HAVE_LIBCEC
}
} // CECInput
CECInput::~CECInput() CECInput::~CECInput()
{ {
#ifdef HAVE_LIBCEC #ifdef HAVE_LIBCEC
if(mlibCEC) if (mlibCEC) {
{
mlibCEC->Close(); mlibCEC->Close();
UnloadLibCec(mlibCEC); UnloadLibCec(mlibCEC);
mlibCEC = nullptr; mlibCEC = nullptr;
} }
#ifdef _RPI_ #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(); vchi_tv_and_cec_deinit();
#endif // _RPI_ #endif // _RPI_
#endif // HAVE_LIBCEC #endif // HAVE_LIBCEC
}
} // ~CECInput
std::string CECInput::getAlertTypeString(const unsigned int _type) std::string CECInput::getAlertTypeString(const unsigned int _type)
{ {
switch(_type) switch (_type) {
{
#ifdef HAVE_LIBCEC #ifdef HAVE_LIBCEC
case CEC::CEC_ALERT_SERVICE_DEVICE: { return "Service-Device"; } break; case CEC::CEC_ALERT_SERVICE_DEVICE: { return "Service-Device"; } break;
case CEC::CEC_ALERT_CONNECTION_LOST: { return "Connection-Lost"; } 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 #else // HAVE_LIBCEC
case 0: case 0:
#endif // HAVE_LIBCEC #endif // HAVE_LIBCEC
default: { return "Unknown"; } break; default: { return "Unknown"; } break;
} }
}
} // getAlertTypeString
std::string CECInput::getOpCodeString(const unsigned int _opCode) std::string CECInput::getOpCodeString(const unsigned int _opCode)
{ {
switch(_opCode) switch (_opCode) {
{
#ifdef HAVE_LIBCEC #ifdef HAVE_LIBCEC
case CEC::CEC_OPCODE_ACTIVE_SOURCE: { return "Active-Source"; } break; case CEC::CEC_OPCODE_ACTIVE_SOURCE: { return "Active-Source"; } break;
case CEC::CEC_OPCODE_IMAGE_VIEW_ON: { return "Image-View-On"; } 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 #else // HAVE_LIBCEC
case 0: case 0:
#endif // HAVE_LIBCEC #endif // HAVE_LIBCEC
default: { return "Unknown"; } break; default: { return "Unknown"; } break;
} }
}
} // getOpCodeString
std::string CECInput::getKeyCodeString(const unsigned int _keyCode) std::string CECInput::getKeyCodeString(const unsigned int _keyCode)
{ {
switch(_keyCode) switch (_keyCode) {
{
#ifdef HAVE_LIBCEC #ifdef HAVE_LIBCEC
case CEC::CEC_USER_CONTROL_CODE_SELECT: { return "Select"; } break; case CEC::CEC_USER_CONTROL_CODE_SELECT: { return "Select"; } break;
case CEC::CEC_USER_CONTROL_CODE_UP: { return "Up"; } 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 #else // HAVE_LIBCEC
case 0: case 0:
#endif // HAVE_LIBCEC #endif // HAVE_LIBCEC
default: { return "Unknown"; } break; default: { return "Unknown"; } break;
} }
}
} // getKeyCodeString

View file

@ -1,3 +1,9 @@
//
// CECInput.h
//
// CEC (Consumer Electronics Control).
//
#pragma once #pragma once
#ifndef ES_CORE_CECINPUT_H #ifndef ES_CORE_CECINPUT_H
#define ES_CORE_CECINPUT_H #define ES_CORE_CECINPUT_H
@ -9,7 +15,6 @@ namespace CEC { class ICECAdapter; }
class CECInput class CECInput
{ {
public: public:
static void init(); static void init();
static void deinit(); static void deinit();
static std::string getAlertTypeString(const unsigned int _type); static std::string getAlertTypeString(const unsigned int _type);
@ -17,14 +22,12 @@ public:
static std::string getKeyCodeString(const unsigned int _keyCode); static std::string getKeyCodeString(const unsigned int _keyCode);
private: private:
CECInput(); CECInput();
~CECInput(); ~CECInput();
static CECInput* sInstance; static CECInput* sInstance;
CEC::ICECAdapter* mlibCEC; CEC::ICECAdapter* mlibCEC;
};
}; // CECInput
#endif // ES_CORE_CECINPUT_H #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 #pragma once
#ifndef ES_CORE_HELP_PROMPT_H #ifndef ES_CORE_HELP_PROMPT_H
#define 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 "HelpStyle.h"
#include "resources/Font.h" #include "resources/Font.h"
@ -22,7 +29,8 @@ void HelpStyle::applyTheme(const std::shared_ptr<ThemeData>& theme, const std::s
return; return;
if(elem->has("pos")) 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")) if(elem->has("origin"))
origin = elem->get<Vector2f>("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 #pragma once
#ifndef ES_CORE_HELP_STYLE_H #ifndef ES_CORE_HELP_STYLE_H
#define ES_CORE_HELP_STYLE_H #define ES_CORE_HELP_STYLE_H
@ -9,15 +16,14 @@
class Font; class Font;
class ThemeData; class ThemeData;
struct HelpStyle struct HelpStyle {
{
Vector2f position; Vector2f position;
Vector2f origin; Vector2f origin;
unsigned int iconColor; unsigned int iconColor;
unsigned int textColor; unsigned int textColor;
std::shared_ptr<Font> font; std::shared_ptr<Font> font;
HelpStyle(); // default values HelpStyle(); // Default values.
void applyTheme(const std::shared_ptr<ThemeData>& theme, const std::string& view); 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 "ImageIO.h"
#include "Log.h" #include "Log.h"
#include <FreeImage.h> #include <FreeImage.h>
#include <string.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; std::vector<unsigned char> rawData;
width = 0; width = 0;
height = 0; height = 0;
FIMEMORY * fiMemory = FreeImage_OpenMemory((BYTE *)data, (DWORD)size); FIMEMORY * fiMemory = FreeImage_OpenMemory((BYTE *)data, (DWORD)size);
if (fiMemory != nullptr) { if (fiMemory != nullptr) {
//detect the filetype from data // Detect the filetype from data.
FREE_IMAGE_FORMAT format = FreeImage_GetFileTypeFromMemory(fiMemory); FREE_IMAGE_FORMAT format = FreeImage_GetFileTypeFromMemory(fiMemory);
if (format != FIF_UNKNOWN && FreeImage_FIFSupportsReading(format)) if (format != FIF_UNKNOWN && FreeImage_FIFSupportsReading(format)) {
{ // File type is supported. load image,
//file type is supported. load image
FIBITMAP * fiBitmap = FreeImage_LoadFromMemory(format, fiMemory); FIBITMAP * fiBitmap = FreeImage_LoadFromMemory(format, fiMemory);
if (fiBitmap != nullptr) if (fiBitmap != nullptr) {
{ // Loaded. convert to 32bit if necessary.
//loaded. convert to 32bit if necessary if (FreeImage_GetBPP(fiBitmap) != 32) {
if (FreeImage_GetBPP(fiBitmap) != 32)
{
FIBITMAP * fiConverted = FreeImage_ConvertTo32Bits(fiBitmap); FIBITMAP * fiConverted = FreeImage_ConvertTo32Bits(fiBitmap);
if (fiConverted != nullptr) if (fiConverted != nullptr) {
{
//free original bitmap data //free original bitmap data
FreeImage_Unload(fiBitmap); FreeImage_Unload(fiBitmap);
fiBitmap = fiConverted; fiBitmap = fiConverted;
} }
} }
if (fiBitmap != nullptr) if (fiBitmap != nullptr) {
{
width = FreeImage_GetWidth(fiBitmap); width = FreeImage_GetWidth(fiBitmap);
height = FreeImage_GetHeight(fiBitmap); height = FreeImage_GetHeight(fiBitmap);
//loop through scanlines and add all pixel data to the return vector // Loop through scanlines and add all pixel data to the return vector.
//this is necessary, because width*height*bpp might not be == pitch // This is necessary, because width*height*bpp might not be == pitch.
unsigned char * tempData = new unsigned char[width * height * 4]; 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); const BYTE * scanLine = FreeImage_GetScanLine(fiBitmap, (int)i);
memcpy(tempData + (i * width * 4), scanLine, width * 4); memcpy(tempData + (i * width * 4), scanLine, width * 4);
} }
//convert from BGRA to RGBA // Convert from BGRA to RGBA.
for(size_t i = 0; i < width*height; i++) for (size_t i = 0; i < width*height; i++) {
{
RGBQUAD bgra = ((RGBQUAD *)tempData)[i]; RGBQUAD bgra = ((RGBQUAD *)tempData)[i];
RGBQUAD rgba; RGBQUAD rgba;
rgba.rgbBlue = bgra.rgbRed; rgba.rgbBlue = bgra.rgbRed;
@ -54,21 +55,20 @@ std::vector<unsigned char> ImageIO::loadFromMemoryRGBA32(const unsigned char * d
((RGBQUAD *)tempData)[i] = rgba; ((RGBQUAD *)tempData)[i] = rgba;
} }
rawData = std::vector<unsigned char>(tempData, tempData + width * height * 4); rawData = std::vector<unsigned char>(tempData, tempData + width * height * 4);
//free bitmap data // Free bitmap data.
FreeImage_Unload(fiBitmap); FreeImage_Unload(fiBitmap);
delete[] tempData; delete[] tempData;
} }
} }
else else {
{
LOG(LogError) << "Error - Failed to load image from memory!"; LOG(LogError) << "Error - Failed to load image from memory!";
} }
} }
else else {
{ LOG(LogError) << "Error - File type " <<
LOG(LogError) << "Error - File type " << (format == FIF_UNKNOWN ? "unknown" : "unsupported") << "!"; (format == FIF_UNKNOWN ? "unknown" : "unsupported") << "!";
} }
//free FIMEMORY again // Free fiMemory again
FreeImage_CloseMemory(fiMemory); FreeImage_CloseMemory(fiMemory);
} }
return rawData; return rawData;
@ -78,10 +78,8 @@ void ImageIO::flipPixelsVert(unsigned char* imagePx, const size_t& width, const
{ {
unsigned int temp; unsigned int temp;
unsigned int* arr = (unsigned int*)imagePx; unsigned int* arr = (unsigned int*)imagePx;
for(size_t y = 0; y < height / 2; y++) for (size_t y = 0; y < height / 2; y++) {
{ for (size_t x = 0; x < width; x++) {
for(size_t x = 0; x < width; x++)
{
temp = arr[x + (y * width)]; temp = arr[x + (y * width)];
arr[x + (y * width)] = arr[x + (height * width) - ((y + 1) * width)]; arr[x + (y * width)] = arr[x + (height * width) - ((y + 1) * width)];
arr[x + (height * width) - ((y + 1) * width)] = temp; arr[x + (height * width) - ((y + 1) * width)] = temp;

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -1,3 +1,9 @@
//
// PowerSaver.h
//
// Power saving functions.
//
#pragma once #pragma once
#ifndef ES_CORE_POWER_SAVER_H #ifndef ES_CORE_POWER_SAVER_H
#define ES_CORE_POWER_SAVER_H #define ES_CORE_POWER_SAVER_H
@ -5,32 +11,37 @@
class PowerSaver class PowerSaver
{ {
public: 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(); 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(); static int getTimeout();
// Update currently set timeouts after User changes Timeout settings // Update currently set timeouts after user changes timeout settings.
static void updateTimeouts(); 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(); static mode getMode();
// Called when user changes mode from Settings // Called when user changes mode from settings menu.
static void updateMode(); 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(); 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); static void setState(bool state);
// Paired calls when you want to pause PS briefly till you finish animating // Paired calls when you want to pause PS briefly until you finish animating
// or processing over cycles // or processing over cycles.
static void pause() { setState(false); } static void pause() { setState(false); }
static void resume() { setState(true); } 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 void runningScreenSaver(bool state);
static bool isScreenSaverActive(); static bool isScreenSaverActive();

View file

@ -1,3 +1,9 @@
//
// Animation.h
//
// Animation base class.
//
#pragma once #pragma once
#ifndef ES_CORE_ANIMATIONS_ANIMATION_H #ifndef ES_CORE_ANIMATIONS_ANIMATION_H
#define 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/AnimationController.h"
#include "animations/Animation.h" #include "animations/Animation.h"
AnimationController::AnimationController(Animation* anim, int delay, std::function<void()> finishedCallback, bool reverse) AnimationController::AnimationController(
: mAnimation(anim), mFinishedCallback(finishedCallback), mReverse(reverse), mTime(-delay), mDelay(delay) 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; mTime += deltaTime;
if(mTime < 0) // are we still in delay? if(mTime < 0) // Are we still in delay?
return false; return false;
float t = (float)mTime / mAnimation->getDuration(); float t = (float)mTime / mAnimation->getDuration();

View file

@ -1,3 +1,9 @@
//
// AnimationController.h
//
// Basic animation controls.
//
#pragma once #pragma once
#ifndef ES_CORE_ANIMATIONS_ANIMATION_CONTROLLER_H #ifndef ES_CORE_ANIMATIONS_ANIMATION_CONTROLLER_H
#define ES_CORE_ANIMATIONS_ANIMATION_CONTROLLER_H #define ES_CORE_ANIMATIONS_ANIMATION_CONTROLLER_H
@ -10,7 +16,8 @@ class AnimationController
{ {
public: public:
// Takes ownership of anim (will delete in destructor). // 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(); virtual ~AnimationController();
// Returns true if the animation is complete. // 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 #pragma once
#ifndef ES_CORE_ANIMATIONS_LAMBDA_ANIMATION_H #ifndef ES_CORE_ANIMATIONS_LAMBDA_ANIMATION_H
#define ES_CORE_ANIMATIONS_LAMBDA_ANIMATION_H #define ES_CORE_ANIMATIONS_LAMBDA_ANIMATION_H
#include "animations/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 class LambdaAnimation : public Animation
{ {
public: 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; virtual ~LambdaAnimation() = default;

View file

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

View file

@ -1,3 +1,9 @@
//
// Misc.h
//
// Miscellaneous math functions.
//
#pragma once #pragma once
#ifndef ES_CORE_MATH_MISC_H #ifndef ES_CORE_MATH_MISC_H
#define ES_CORE_MATH_MISC_H #define ES_CORE_MATH_MISC_H
@ -8,7 +14,7 @@
namespace Math 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 cosf(const float _num);
float sinf(const float _num); float sinf(const float _num);
float floorf(const float _num); float floorf(const float _num);
@ -26,11 +32,11 @@ namespace Math
namespace Scroll namespace Scroll
{ {
float bounce(const float _delayTime, const float _scrollTime, const float _currentTime, const float _scrollLength); float bounce(const float _delayTime, const float _scrollTime,
float loop (const float _delayTime, const float _scrollTime, const float _currentTime, const float _scrollLength); const float _currentTime, const float _scrollLength);
float loop(const float _delayTime, const float _scrollTime,
} // Scroll:: const float _currentTime, const float _scrollLength);
}
} // Math:: }
#endif // ES_CORE_MATH_MISC_H #endif // ES_CORE_MATH_MISC_H

View file

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

View file

@ -1,3 +1,9 @@
//
// Transform4x4f.h
//
// 4x4 transform functions.
//
#pragma once #pragma once
#ifndef ES_CORE_MATH_TRANSFORM4X4F_H #ifndef ES_CORE_MATH_TRANSFORM4X4F_H
#define ES_CORE_MATH_TRANSFORM4X4F_H #define ES_CORE_MATH_TRANSFORM4X4F_H
@ -8,13 +14,21 @@
class Transform4x4f class Transform4x4f
{ {
public: public:
Transform4x4f() {} 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 Transform4x4f operator*(const Transform4x4f& _other) const;
const Vector3f operator*(const Vector3f& _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& r0() { return mR0; }
inline Vector4f& r1() { return mR1; } inline Vector4f& r1() { return mR1; }
@ -25,7 +39,13 @@ public:
inline const Vector4f& r2() const { return mR2; } inline const Vector4f& r2() const { return mR2; }
inline const Vector4f& r3() const { return mR3; } 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& invert(const Transform4x4f& _other);
Transform4x4f& scale(const Vector3f& _scale); Transform4x4f& scale(const Vector3f& _scale);
Transform4x4f& rotate(const float _angle, const Vector3f& _axis); Transform4x4f& rotate(const float _angle, const Vector3f& _axis);
@ -38,15 +58,14 @@ public:
inline Vector3f& translation() { return mR3.v3(); } inline Vector3f& translation() { return mR3.v3(); }
inline const Vector3f& translation() const { 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: protected:
Vector4f mR0; Vector4f mR0;
Vector4f mR1; Vector4f mR1;
Vector4f mR2; Vector4f mR2;
Vector4f mR3; Vector4f mR3;
};
}; // Transform4x4f
#endif // ES_CORE_MATH_TRANSFORM4X4F_H #endif // ES_CORE_MATH_TRANSFORM4X4F_H

View file

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

View file

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

View file

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

View file

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

View file

@ -1,3 +1,9 @@
//
// Vector3f.cpp
//
// 3-dimensional floating point vector functions.
//
#include "math/Vector3f.h" #include "math/Vector3f.h"
Vector3f& Vector3f::round() Vector3f& Vector3f::round()
@ -7,8 +13,7 @@ Vector3f& Vector3f::round()
mZ = (float)(int)(mZ + 0.5f); mZ = (float)(int)(mZ + 0.5f);
return *this; return *this;
}
} // round
Vector3f& Vector3f::lerp(const Vector3f& _start, const Vector3f& _end, const float _fraction) 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); mZ = Math::lerp(_start.z(), _end.z(), _fraction);
return *this; return *this;
}
} // lerp

View file

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

View file

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

View file

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

View file

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

View file

@ -1,3 +1,9 @@
//
// Renderer.h
//
// Rendering functions.
//
#pragma once #pragma once
#ifndef ES_CORE_RENDERER_RENDERER_H #ifndef ES_CORE_RENDERER_RENDERER_H
#define ES_CORE_RENDERER_RENDERER_H #define ES_CORE_RENDERER_RENDERER_H
@ -12,8 +18,7 @@ namespace Renderer
{ {
namespace Blend namespace Blend
{ {
enum Factor enum Factor {
{
ZERO = 0, ZERO = 0,
ONE = 1, ONE = 1,
SRC_COLOR = 2, SRC_COLOR = 2,
@ -24,50 +29,62 @@ namespace Renderer
ONE_MINUS_DST_COLOR = 7, ONE_MINUS_DST_COLOR = 7,
DST_ALPHA = 8, DST_ALPHA = 8,
ONE_MINUS_DST_ALPHA = 9 ONE_MINUS_DST_ALPHA = 9
};
}; // Factor }
} // Blend::
namespace Texture namespace Texture
{ {
enum Type enum Type {
{
RGBA = 0, RGBA = 0,
ALPHA = 1 ALPHA = 1
};
}
}; // Type struct Rect {
Rect(
} // Texture:: const int _x,
const int _y,
struct Rect const int _w,
{ const int _h)
Rect(const int _x, const int _y, const int _w, const int _h) : x(_x), y(_y), w(_w), h(_h) { } : x(_x),
y(_y),
w(_w),
h(_h) {}
int x; int x;
int y; int y;
int w; int w;
int h; int h;
};
}; // Rect
struct Vertex struct Vertex
{ {
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 pos;
Vector2f tex; Vector2f tex;
unsigned int col; unsigned int col;
};
}; // Vertex
bool init(); bool init();
void deinit(); void deinit();
void pushClipRect(const Vector2i& _pos, const Vector2i& _size); void pushClipRect(const Vector2i& _pos, const Vector2i& _size);
void popClipRect(); 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(); SDL_Window* getSDLWindow();
int getWindowWidth(); int getWindowWidth();
int getWindowHeight(); int getWindowHeight();
@ -77,18 +94,39 @@ namespace Renderer
int getScreenOffsetY(); int getScreenOffsetY();
int getScreenRotate(); int getScreenRotate();
// API specific // API specific.
unsigned int convertColor(const unsigned int _color); unsigned int convertColor(const unsigned int _color);
unsigned int getWindowFlags(); unsigned int getWindowFlags();
void setupWindow(); void setupWindow();
void createContext(); void createContext();
void destroyContext(); 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 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 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 drawLines(
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); 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 setProjection(const Transform4x4f& _projection);
void setMatrix(const Transform4x4f& _matrix); void setMatrix(const Transform4x4f& _matrix);
void setViewport(const Rect& _viewport); void setViewport(const Rect& _viewport);
@ -96,6 +134,6 @@ namespace Renderer
void setSwapInterval(); void setSwapInterval();
void swapBuffers(); void swapBuffers();
} // Renderer:: }
#endif // ES_CORE_RENDERER_RENDERER_H #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) #if defined(USE_OPENGL_21)
#include "renderers/Renderer.h" #include "renderers/Renderer.h"
@ -10,7 +16,6 @@
namespace Renderer namespace Renderer
{ {
#if defined(_DEBUG) #if defined(_DEBUG)
#define GL_CHECK_ERROR(Function) (Function, _GLCheckError(#Function)) #define GL_CHECK_ERROR(Function) (Function, _GLCheckError(#Function))
@ -19,7 +24,8 @@ namespace Renderer
const GLenum errorCode = glGetError(); const GLenum errorCode = glGetError();
if (errorCode != GL_NO_ERROR) { 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 #else
@ -31,8 +37,7 @@ namespace Renderer
static GLenum convertBlendFactor(const Blend::Factor _blendFactor) static GLenum convertBlendFactor(const Blend::Factor _blendFactor)
{ {
switch(_blendFactor) switch (_blendFactor) {
{
case Blend::ZERO: { return GL_ZERO; } break; case Blend::ZERO: { return GL_ZERO; } break;
case Blend::ONE: { return GL_ONE; } break; case Blend::ONE: { return GL_ONE; } break;
case Blend::SRC_COLOR: { return GL_SRC_COLOR; } 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; case Blend::ONE_MINUS_DST_ALPHA: { return GL_ONE_MINUS_DST_ALPHA; } break;
default: { return GL_ZERO; } default: { return GL_ZERO; }
} }
}
} // convertBlendFactor
static GLenum convertTextureType(const Texture::Type _type) static GLenum convertTextureType(const Texture::Type _type)
{ {
switch(_type) switch (_type) {
{
case Texture::RGBA: { return GL_RGBA; } break; case Texture::RGBA: { return GL_RGBA; } break;
case Texture::ALPHA: { return GL_ALPHA; } break; case Texture::ALPHA: { return GL_ALPHA; } break;
default: { return GL_ZERO; } default: { return GL_ZERO; }
} }
}
} // convertTextureType
unsigned int convertColor(const unsigned int _color) 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 r = ((_color & 0xff000000) >> 24) & 255;
unsigned char g = ((_color & 0x00ff0000) >> 16) & 255; unsigned char g = ((_color & 0x00ff0000) >> 16) & 255;
unsigned char b = ((_color & 0x0000ff00) >> 8) & 255; unsigned char b = ((_color & 0x0000ff00) >> 8) & 255;
unsigned char a = ((_color & 0x000000ff) ) & 255; unsigned char a = ((_color & 0x000000ff) ) & 255;
return ((a << 24) | (b << 16) | (g << 8) | (r)); return ((a << 24) | (b << 16) | (g << 8) | (r));
}
} // convertColor
unsigned int getWindowFlags() unsigned int getWindowFlags()
{ {
return SDL_WINDOW_OPENGL; return SDL_WINDOW_OPENGL;
}
} // getWindowFlags
void setupWindow() void setupWindow()
{ {
@ -89,25 +89,31 @@ namespace Renderer
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1); SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
}
} // setupWindow
void createContext() void createContext()
{ {
sdlContext = SDL_GL_CreateContext(getSDLWindow()); sdlContext = SDL_GL_CreateContext(getSDLWindow());
SDL_GL_MakeCurrent(getSDLWindow(), sdlContext); SDL_GL_MakeCurrent(getSDLWindow(), sdlContext);
std::string vendor = glGetString(GL_VENDOR) ? (const char*)glGetString(GL_VENDOR) : ""; std::string vendor = glGetString(GL_VENDOR) ?
std::string renderer = glGetString(GL_RENDERER) ? (const char*)glGetString(GL_RENDERER) : ""; (const char*)glGetString(GL_VENDOR) : "";
std::string version = glGetString(GL_VERSION) ? (const char*)glGetString(GL_VERSION) : ""; std::string renderer = glGetString(GL_RENDERER) ?
std::string extensions = glGetString(GL_EXTENSIONS) ? (const char*)glGetString(GL_EXTENSIONS) : ""; (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 vendor: " << vendor;
LOG(LogInfo) << "GL renderer: " << renderer; LOG(LogInfo) << "GL renderer: " << renderer;
LOG(LogInfo) << "GL version: " << version; LOG(LogInfo) << "GL version: " << version;
LOG(LogInfo) << "Checking available OpenGL extensions..."; LOG(LogInfo) << "Checking available OpenGL extensions...";
std::string glExts = glGetString(GL_EXTENSIONS) ? (const char*)glGetString(GL_EXTENSIONS) : ""; std::string glExts = glGetString(GL_EXTENSIONS) ?
LOG(LogInfo) << "ARB_texture_non_power_of_two: " << (extensions.find("ARB_texture_non_power_of_two") != std::string::npos ? "ok" : "MISSING"); (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}; uint8_t data[4] = {255, 255, 255, 255};
whiteTexture = createTexture(Texture::RGBA, false, true, 1, 1, data); 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_VERTEX_ARRAY));
GL_CHECK_ERROR(glEnableClientState(GL_TEXTURE_COORD_ARRAY)); GL_CHECK_ERROR(glEnableClientState(GL_TEXTURE_COORD_ARRAY));
GL_CHECK_ERROR(glEnableClientState(GL_COLOR_ARRAY)); GL_CHECK_ERROR(glEnableClientState(GL_COLOR_ARRAY));
}
} // createContext
void destroyContext() void destroyContext()
{ {
SDL_GL_DeleteContext(sdlContext); SDL_GL_DeleteContext(sdlContext);
sdlContext = nullptr; sdlContext = nullptr;
}
} // destroyContext unsigned int createTexture(
const Texture::Type _type,
unsigned int createTexture(const Texture::Type _type, const bool _linear, const bool _repeat, const unsigned int _width, const unsigned int _height, void* _data) const bool _linear,
const bool _repeat,
const unsigned int _width,
const unsigned int _height,
void* _data)
{ {
const GLenum type = convertTextureType(_type); const GLenum type = convertTextureType(_type);
unsigned int texture; unsigned int texture;
@ -138,71 +148,87 @@ namespace Renderer
GL_CHECK_ERROR(glGenTextures(1, &texture)); GL_CHECK_ERROR(glGenTextures(1, &texture));
GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, 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_S, _repeat ?
GL_CHECK_ERROR(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, _repeat ? GL_REPEAT : GL_CLAMP_TO_EDGE)); GL_REPEAT : GL_CLAMP_TO_EDGE));
GL_CHECK_ERROR(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, _repeat ?
GL_CHECK_ERROR(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, _linear ? GL_LINEAR : GL_NEAREST)); 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(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; return texture;
}
} // createTexture
void destroyTexture(const unsigned int _texture) void destroyTexture(const unsigned int _texture)
{ {
GL_CHECK_ERROR(glDeleteTextures(1, &_texture)); GL_CHECK_ERROR(glDeleteTextures(1, &_texture));
}
} // destroyTexture void updateTexture(
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) 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); const GLenum type = convertTextureType(_type);
GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, _texture)); 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)); GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, whiteTexture));
}
} // updateTexture
void bindTexture(const unsigned int _texture) void bindTexture(const unsigned int _texture)
{ {
if(_texture == 0) GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, whiteTexture)); if (_texture == 0)
else GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, _texture)); GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, whiteTexture));
else
GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, _texture));
}
} // bindTexture void drawLines(
const Vertex* _vertices,
void drawLines(const Vertex* _vertices, const unsigned int _numVertices, const Blend::Factor _srcBlendFactor, const Blend::Factor _dstBlendFactor) 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(glVertexPointer(2, GL_FLOAT, sizeof(Vertex), &_vertices[0].pos));
GL_CHECK_ERROR(glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &_vertices[0].tex)); 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(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)); GL_CHECK_ERROR(glDrawArrays(GL_LINES, 0, _numVertices));
}
} // drawLines void drawTriangleStrips(
const Vertex* _vertices,
void drawTriangleStrips(const Vertex* _vertices, const unsigned int _numVertices, const Blend::Factor _srcBlendFactor, const Blend::Factor _dstBlendFactor) 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(glVertexPointer(2, GL_FLOAT, sizeof(Vertex), &_vertices[0].pos));
GL_CHECK_ERROR(glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &_vertices[0].tex)); 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(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)); GL_CHECK_ERROR(glDrawArrays(GL_TRIANGLE_STRIP, 0, _numVertices));
}
} // drawTriangleStrips
void setProjection(const Transform4x4f& _projection) void setProjection(const Transform4x4f& _projection)
{ {
GL_CHECK_ERROR(glMatrixMode(GL_PROJECTION)); GL_CHECK_ERROR(glMatrixMode(GL_PROJECTION));
GL_CHECK_ERROR(glLoadMatrixf((GLfloat*)&_projection)); GL_CHECK_ERROR(glLoadMatrixf((GLfloat*)&_projection));
}
} // setProjection
void setMatrix(const Transform4x4f& _matrix) void setMatrix(const Transform4x4f& _matrix)
{ {
@ -211,57 +237,51 @@ namespace Renderer
GL_CHECK_ERROR(glMatrixMode(GL_MODELVIEW)); GL_CHECK_ERROR(glMatrixMode(GL_MODELVIEW));
GL_CHECK_ERROR(glLoadMatrixf((GLfloat*)&matrix)); GL_CHECK_ERROR(glLoadMatrixf((GLfloat*)&matrix));
}
} // setMatrix
void setViewport(const Rect& _viewport) void setViewport(const Rect& _viewport)
{ {
// glViewport starts at the bottom left of the window // glViewport starts at the bottom left of the window.
GL_CHECK_ERROR(glViewport( _viewport.x, getWindowHeight() - _viewport.y - _viewport.h, _viewport.w, _viewport.h)); GL_CHECK_ERROR(glViewport( _viewport.x, getWindowHeight() -
_viewport.y - _viewport.h, _viewport.w, _viewport.h));
} // setViewport }
void setScissor(const Rect& _scissor) 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)); GL_CHECK_ERROR(glDisable(GL_SCISSOR_TEST));
} }
else else {
{ // glScissor starts at the bottom left of the window.
// glScissor starts at the bottom left of the window GL_CHECK_ERROR(glScissor(_scissor.x, getWindowHeight() -
GL_CHECK_ERROR(glScissor(_scissor.x, getWindowHeight() - _scissor.y - _scissor.h, _scissor.w, _scissor.h)); _scissor.y - _scissor.h, _scissor.w, _scissor.h));
GL_CHECK_ERROR(glEnable(GL_SCISSOR_TEST)); GL_CHECK_ERROR(glEnable(GL_SCISSOR_TEST));
} }
}
} // setScissor
void setSwapInterval() void setSwapInterval()
{ {
// vsync // vsync.
if(Settings::getInstance()->getBool("VSync")) if (Settings::getInstance()->getBool("VSync")) {
{
// SDL_GL_SetSwapInterval(0) for immediate updates (no vsync, default), // SDL_GL_SetSwapInterval(0) for immediate updates (no vsync, default),
// 1 for updates synchronized with the vertical retrace, // 1 for updates synchronized with the vertical retrace,
// or -1 for late swap tearing. // or -1 for late swap tearing.
// SDL_GL_SetSwapInterval returns 0 on success, -1 on error. // 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 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) { if (SDL_GL_SetSwapInterval(1) != 0 && SDL_GL_SetSwapInterval(-1) != 0) {
LOG(LogWarning) << "Tried to enable vsync, but failed! (" << SDL_GetError() << ")"; LOG(LogWarning) << "Tried to enable vsync, but failed! (" << SDL_GetError() << ")";
} }
} }
else else
SDL_GL_SetSwapInterval(0); SDL_GL_SetSwapInterval(0);
}
} // setSwapInterval
void swapBuffers() void swapBuffers()
{ {
SDL_GL_SwapWindow(getSDLWindow()); SDL_GL_SwapWindow(getSDLWindow());
GL_CHECK_ERROR(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)); GL_CHECK_ERROR(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
}
} // swapBuffers
} // Renderer:: } // Renderer::

View file

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