2012-09-10 18:48:00 +00:00
|
|
|
#include "Renderer.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
|
2013-05-16 19:26:19 +00:00
|
|
|
#include "../data/Resources.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
#include "ImageIO.h"
|
|
|
|
#include "Log.h"
|
2013-06-17 19:01:03 +00:00
|
|
|
#include "Settings.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
#include <SDL.h>
|
2013-05-13 19:53:28 +00:00
|
|
|
|
2013-09-28 01:17:41 +00:00
|
|
|
#ifdef USE_OPENGL_ES
|
|
|
|
#define glOrtho glOrthof
|
|
|
|
#endif
|
|
|
|
|
2012-09-10 18:48:00 +00:00
|
|
|
namespace Renderer
|
|
|
|
{
|
2013-05-24 13:08:53 +00:00
|
|
|
static bool initialCursorState;
|
|
|
|
|
2017-12-01 17:42:27 +00:00
|
|
|
unsigned int windowWidth = 0;
|
|
|
|
unsigned int windowHeight = 0;
|
|
|
|
unsigned int screenWidth = 0;
|
|
|
|
unsigned int screenHeight = 0;
|
|
|
|
unsigned int screenOffsetX = 0;
|
|
|
|
unsigned int screenOffsetY = 0;
|
2018-01-18 17:30:00 +00:00
|
|
|
unsigned int screenRotate = 0;
|
2017-12-01 17:42:27 +00:00
|
|
|
|
|
|
|
unsigned int getWindowWidth() { return windowWidth; }
|
|
|
|
unsigned int getWindowHeight() { return windowHeight; }
|
|
|
|
unsigned int getScreenWidth() { return screenWidth; }
|
|
|
|
unsigned int getScreenHeight() { return screenHeight; }
|
|
|
|
unsigned int getScreenOffsetX() { return screenOffsetX; }
|
|
|
|
unsigned int getScreenOffsetY() { return screenOffsetY; }
|
2018-01-18 17:30:00 +00:00
|
|
|
unsigned int getScreenRotate() { return screenRotate; }
|
2012-09-10 18:48:00 +00:00
|
|
|
|
2013-08-18 17:17:24 +00:00
|
|
|
SDL_Window* sdlWindow = NULL;
|
|
|
|
SDL_GLContext sdlContext = NULL;
|
2012-09-10 18:48:00 +00:00
|
|
|
|
2013-08-18 17:17:24 +00:00
|
|
|
bool createSurface()
|
2012-09-10 18:48:00 +00:00
|
|
|
{
|
2013-01-04 23:31:51 +00:00
|
|
|
LOG(LogInfo) << "Creating surface...";
|
2012-09-10 18:48:00 +00:00
|
|
|
|
2013-06-20 14:14:10 +00:00
|
|
|
if(SDL_Init(SDL_INIT_VIDEO) != 0)
|
2012-09-10 18:48:00 +00:00
|
|
|
{
|
2013-01-04 23:31:51 +00:00
|
|
|
LOG(LogError) << "Error initializing SDL!\n " << SDL_GetError();
|
2012-09-10 18:48:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-09-27 18:38:32 +00:00
|
|
|
//hide mouse cursor early
|
|
|
|
initialCursorState = SDL_ShowCursor(0) == 1;
|
|
|
|
|
2012-09-10 18:48:00 +00:00
|
|
|
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
2013-10-24 16:43:54 +00:00
|
|
|
|
2014-03-19 20:03:23 +00:00
|
|
|
// multisample anti-aliasing
|
|
|
|
//SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
|
|
|
|
//SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 2);
|
|
|
|
|
2013-10-24 16:47:09 +00:00
|
|
|
#ifdef USE_OPENGL_ES
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
|
|
|
|
#endif
|
2013-08-18 17:17:24 +00:00
|
|
|
|
2013-08-21 20:59:11 +00:00
|
|
|
SDL_DisplayMode dispMode;
|
2013-09-28 01:17:41 +00:00
|
|
|
SDL_GetDesktopDisplayMode(0, &dispMode);
|
2017-12-01 17:42:27 +00:00
|
|
|
windowWidth = Settings::getInstance()->getInt("WindowWidth") ? Settings::getInstance()->getInt("WindowWidth") : dispMode.w;
|
|
|
|
windowHeight = Settings::getInstance()->getInt("WindowHeight") ? Settings::getInstance()->getInt("WindowHeight") : dispMode.h;
|
|
|
|
screenWidth = Settings::getInstance()->getInt("ScreenWidth") ? Settings::getInstance()->getInt("ScreenWidth") : windowWidth;
|
|
|
|
screenHeight = Settings::getInstance()->getInt("ScreenHeight") ? Settings::getInstance()->getInt("ScreenHeight") : windowHeight;
|
|
|
|
screenOffsetX = Settings::getInstance()->getInt("ScreenOffsetX") ? Settings::getInstance()->getInt("ScreenOffsetX") : 0;
|
|
|
|
screenOffsetY = Settings::getInstance()->getInt("ScreenOffsetY") ? Settings::getInstance()->getInt("ScreenOffsetY") : 0;
|
2018-01-18 17:30:00 +00:00
|
|
|
screenRotate = Settings::getInstance()->getInt("ScreenRotate") ? Settings::getInstance()->getInt("ScreenRotate") : 0;
|
2013-08-21 20:59:11 +00:00
|
|
|
|
2013-08-18 17:17:24 +00:00
|
|
|
sdlWindow = SDL_CreateWindow("EmulationStation",
|
|
|
|
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
|
2017-12-01 17:42:27 +00:00
|
|
|
windowWidth, windowHeight,
|
2014-03-08 19:00:18 +00:00
|
|
|
SDL_WINDOW_OPENGL | (Settings::getInstance()->getBool("Windowed") ? 0 : SDL_WINDOW_FULLSCREEN));
|
2012-09-10 18:48:00 +00:00
|
|
|
|
2013-08-18 17:17:24 +00:00
|
|
|
if(sdlWindow == NULL)
|
2012-09-10 18:48:00 +00:00
|
|
|
{
|
2014-06-10 17:36:03 +00:00
|
|
|
LOG(LogError) << "Error creating SDL window!\n\t" << SDL_GetError();
|
2012-09-10 18:48:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-18 17:17:24 +00:00
|
|
|
LOG(LogInfo) << "Created window successfully.";
|
|
|
|
|
2018-01-18 17:30:00 +00:00
|
|
|
//support screen rotation
|
|
|
|
if((screenRotate == 1) || (screenRotate == 3))
|
|
|
|
{
|
|
|
|
int temp;
|
|
|
|
temp = windowWidth;
|
|
|
|
windowWidth = windowHeight;
|
|
|
|
windowHeight = temp;
|
|
|
|
temp = screenWidth;
|
|
|
|
screenWidth = screenHeight;
|
|
|
|
screenHeight = temp;
|
|
|
|
temp = screenOffsetX;
|
|
|
|
screenOffsetX = screenOffsetY;
|
|
|
|
screenOffsetY = temp;
|
|
|
|
}
|
|
|
|
|
2013-08-18 17:17:24 +00:00
|
|
|
//set an icon for the window
|
|
|
|
size_t width = 0;
|
|
|
|
size_t height = 0;
|
2014-05-28 21:43:23 +00:00
|
|
|
std::vector<unsigned char> rawData = ImageIO::loadFromMemoryRGBA32(window_icon_256_png_data, window_icon_256_png_size, width, height);
|
2013-08-18 17:17:24 +00:00
|
|
|
if (!rawData.empty())
|
|
|
|
{
|
2014-05-28 21:43:23 +00:00
|
|
|
ImageIO::flipPixelsVert(rawData.data(), width, height);
|
|
|
|
|
2013-08-18 17:17:24 +00:00
|
|
|
//SDL interprets each pixel as a 32-bit number, so our masks must depend on the endianness (byte order) of the machine
|
|
|
|
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
|
|
|
|
Uint32 rmask = 0xff000000; Uint32 gmask = 0x00ff0000; Uint32 bmask = 0x0000ff00; Uint32 amask = 0x000000ff;
|
|
|
|
#else
|
|
|
|
Uint32 rmask = 0x000000ff; Uint32 gmask = 0x0000ff00; Uint32 bmask = 0x00ff0000; Uint32 amask = 0xff000000;
|
|
|
|
#endif
|
|
|
|
//try creating SDL surface from logo data
|
2017-11-17 14:58:52 +00:00
|
|
|
SDL_Surface * logoSurface = SDL_CreateRGBSurfaceFrom((void *)rawData.data(), (int)width, (int)height, 32, (int)(width * 4), rmask, gmask, bmask, amask);
|
2013-08-18 17:17:24 +00:00
|
|
|
if (logoSurface != NULL)
|
|
|
|
{
|
|
|
|
SDL_SetWindowIcon(sdlWindow, logoSurface);
|
|
|
|
SDL_FreeSurface(logoSurface);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sdlContext = SDL_GL_CreateContext(sdlWindow);
|
|
|
|
|
2014-11-22 17:00:11 +00:00
|
|
|
// vsync
|
|
|
|
if(Settings::getInstance()->getBool("VSync"))
|
|
|
|
{
|
|
|
|
// SDL_GL_SetSwapInterval(0) for immediate updates (no vsync, default),
|
|
|
|
// 1 for updates synchronized with the vertical retrace,
|
|
|
|
// or -1 for late swap tearing.
|
|
|
|
// SDL_GL_SetSwapInterval returns 0 on success, -1 on error.
|
2017-10-27 01:03:13 +00:00
|
|
|
// if vsync is requested, try normal vsync; if that doesn't work, try late swap tearing
|
2014-11-22 17:00:11 +00:00
|
|
|
// if that doesn't work, report an error
|
2017-10-27 01:03:13 +00:00
|
|
|
if(SDL_GL_SetSwapInterval(1) != 0 && SDL_GL_SetSwapInterval(-1) != 0)
|
2014-11-22 17:00:11 +00:00
|
|
|
LOG(LogWarning) << "Tried to enable vsync, but failed! (" << SDL_GetError() << ")";
|
|
|
|
}
|
2017-10-23 12:43:06 +00:00
|
|
|
else
|
|
|
|
SDL_GL_SetSwapInterval(0);
|
2014-11-22 17:00:11 +00:00
|
|
|
|
2012-09-10 18:48:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void destroySurface()
|
|
|
|
{
|
2013-08-18 17:17:24 +00:00
|
|
|
SDL_GL_DeleteContext(sdlContext);
|
|
|
|
sdlContext = NULL;
|
|
|
|
|
|
|
|
SDL_DestroyWindow(sdlWindow);
|
|
|
|
sdlWindow = NULL;
|
2013-05-24 13:08:53 +00:00
|
|
|
|
|
|
|
//show mouse cursor
|
|
|
|
SDL_ShowCursor(initialCursorState);
|
|
|
|
|
2012-09-10 18:48:00 +00:00
|
|
|
SDL_Quit();
|
|
|
|
}
|
|
|
|
|
2017-12-01 17:42:27 +00:00
|
|
|
bool init()
|
2012-09-10 18:48:00 +00:00
|
|
|
{
|
2017-12-01 17:42:27 +00:00
|
|
|
if(!createSurface())
|
2012-09-10 18:48:00 +00:00
|
|
|
return false;
|
|
|
|
|
2017-12-01 17:42:27 +00:00
|
|
|
//gotta flip y since y=0 is at the bottom
|
2018-01-18 17:30:00 +00:00
|
|
|
switch(screenRotate)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
glViewport(screenOffsetX, windowHeight - screenHeight - screenOffsetY, screenWidth, screenHeight);
|
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glOrtho(0, screenWidth, screenHeight, 0, -1.0, 1.0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
glViewport(screenOffsetY, windowWidth - screenWidth - screenOffsetX, screenHeight, screenWidth);
|
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glOrtho(0, screenHeight, screenWidth, 0, -1.0, 1.0);
|
|
|
|
glRotatef(90, 0, 0, 1);
|
|
|
|
glTranslatef(0, screenHeight * -1.0f, 0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
{
|
|
|
|
glViewport(screenOffsetX, windowHeight - screenHeight - screenOffsetY, screenWidth, screenHeight);
|
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glOrtho(0, screenWidth, screenHeight, 0, -1.0, 1.0);
|
|
|
|
glRotatef(180, 0, 0, 1);
|
|
|
|
glTranslatef(screenWidth * -1.0f, screenHeight * -1.0f, 0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
{
|
|
|
|
glViewport(screenOffsetY, windowWidth - screenWidth - screenOffsetX, screenHeight, screenWidth);
|
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glOrtho(0, screenHeight, screenWidth, 0, -1.0, 1.0);
|
|
|
|
glRotatef(270, 0, 0, 1);
|
|
|
|
glTranslatef(screenWidth * -1.0f, 0, 0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
glMatrixMode(GL_MODELVIEW);
|
2017-05-17 12:01:15 +00:00
|
|
|
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
2012-09-10 18:48:00 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void deinit()
|
|
|
|
{
|
|
|
|
destroySurface();
|
|
|
|
}
|
2017-12-01 17:42:27 +00:00
|
|
|
|
|
|
|
void swapBuffers()
|
|
|
|
{
|
|
|
|
SDL_GL_SwapWindow(sdlWindow);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
}
|
2012-09-10 18:48:00 +00:00
|
|
|
};
|