2012-09-10 18:48:00 +00:00
|
|
|
#include "Renderer.h"
|
|
|
|
#include <iostream>
|
|
|
|
#include "platform.h"
|
2013-05-13 19:53:28 +00:00
|
|
|
|
|
|
|
#ifdef _WINDOWS_
|
|
|
|
#include <Windows.h>
|
|
|
|
#endif
|
|
|
|
|
2012-09-10 18:48:00 +00:00
|
|
|
#include GLHEADER
|
2013-05-13 19:53:28 +00:00
|
|
|
|
2012-09-10 18:48:00 +00:00
|
|
|
#include "Font.h"
|
2013-05-13 19:53:28 +00:00
|
|
|
#include <SDL.h>
|
2012-09-14 18:22:01 +00:00
|
|
|
#include "InputManager.h"
|
2013-01-04 23:31:51 +00:00
|
|
|
#include "Log.h"
|
2013-05-16 19:26:19 +00:00
|
|
|
#include "ImageIO.h"
|
|
|
|
#include "../data/Resources.h"
|
|
|
|
#include "EmulationStation.h"
|
2012-09-10 18:48:00 +00:00
|
|
|
|
2013-05-13 19:53:28 +00:00
|
|
|
extern bool WINDOWED;
|
|
|
|
|
2012-09-10 18:48:00 +00:00
|
|
|
namespace Renderer
|
|
|
|
{
|
2013-05-24 13:08:53 +00:00
|
|
|
static bool initialCursorState;
|
|
|
|
|
2012-09-16 19:18:11 +00:00
|
|
|
unsigned int display_width = 0;
|
|
|
|
unsigned int display_height = 0;
|
|
|
|
|
|
|
|
unsigned int getScreenWidth() { return display_width; }
|
|
|
|
unsigned int getScreenHeight() { return display_height; }
|
2012-09-10 18:48:00 +00:00
|
|
|
|
|
|
|
SDL_Surface* sdlScreen = NULL;
|
|
|
|
|
|
|
|
bool createSurface() //unsigned int display_width, unsigned int display_height)
|
|
|
|
{
|
2013-01-04 23:31:51 +00:00
|
|
|
LOG(LogInfo) << "Creating surface...";
|
2012-09-10 18:48:00 +00:00
|
|
|
|
2013-04-10 17:29:07 +00:00
|
|
|
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 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;
|
|
|
|
}
|
|
|
|
|
2013-05-16 19:26:19 +00:00
|
|
|
//ATM it is best to just leave the window icon alone on windows.
|
2013-05-29 17:50:41 +00:00
|
|
|
//When compiled as a Windows application, ES at least has an icon in the taskbar
|
2013-05-16 19:26:19 +00:00
|
|
|
//The method below looks pretty shite as alpha isn't taken into account...
|
|
|
|
#ifndef WIN32
|
|
|
|
//try loading PNG from memory
|
|
|
|
size_t width = 0;
|
|
|
|
size_t height = 0;
|
|
|
|
std::vector<unsigned char> rawData = ImageIO::loadFromMemoryRGBA32(es_logo_32_data, es_logo_32_data_len, width, height);
|
|
|
|
if (!rawData.empty()) {
|
|
|
|
//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
|
2013-05-29 17:50:41 +00:00
|
|
|
Uint32 rmask = 0xff000000; Uint32 gmask = 0x0000ff00; Uint32 bmask = 0x00ff0000; Uint32 amask = 0x000000ff;
|
2013-05-16 19:26:19 +00:00
|
|
|
#else
|
2013-05-29 17:50:41 +00:00
|
|
|
Uint32 rmask = 0x000000ff; Uint32 gmask = 0x00ff0000; Uint32 bmask = 0x0000ff00; Uint32 amask = 0xff000000;
|
2013-05-16 19:26:19 +00:00
|
|
|
#endif
|
|
|
|
//try creating SDL surface from logo data
|
|
|
|
SDL_Surface * logoSurface = SDL_CreateRGBSurfaceFrom((void *)rawData.data(), width, height, 32, width*4, rmask, gmask, bmask, amask);
|
|
|
|
if (logoSurface != nullptr) {
|
|
|
|
//change window icon. this sucks atm, but there's nothing better we can do. SDL 1.3 or 2.0 should sort this out...
|
|
|
|
SDL_WM_SetIcon(logoSurface, nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
SDL_WM_SetCaption("EmulationStation", "EmulationStation");
|
|
|
|
|
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-05-13 19:53:28 +00:00
|
|
|
sdlScreen = SDL_SetVideoMode(display_width, display_height, 16, SDL_OPENGL | (WINDOWED ? 0 : SDL_FULLSCREEN) | SDL_DOUBLEBUF);
|
2012-09-10 18:48:00 +00:00
|
|
|
|
|
|
|
if(sdlScreen == NULL)
|
|
|
|
{
|
2013-01-04 23:31:51 +00:00
|
|
|
LOG(LogError) << "Error creating SDL video surface!";
|
2012-09-10 18:48:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//usually display width/height are not specified, i.e. zero, which SDL automatically takes as "native resolution"
|
|
|
|
//so, since other things rely on the size of the screen (damn currently unnormalized coordinate system), we set it here
|
|
|
|
//even though the system was already initialized
|
|
|
|
display_width = sdlScreen->w;
|
|
|
|
display_height = sdlScreen->h;
|
|
|
|
|
2013-01-04 23:31:51 +00:00
|
|
|
LOG(LogInfo) << "Created surface successfully.";
|
2012-09-10 18:48:00 +00:00
|
|
|
|
2013-05-24 13:08:53 +00:00
|
|
|
//hide mouse cursor
|
2013-06-02 19:34:50 +00:00
|
|
|
initialCursorState = SDL_ShowCursor(0) == 1;
|
2013-05-24 13:08:53 +00:00
|
|
|
|
2012-09-10 18:48:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void swapBuffers()
|
|
|
|
{
|
|
|
|
SDL_GL_SwapBuffers();
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void destroySurface()
|
|
|
|
{
|
|
|
|
SDL_FreeSurface(sdlScreen);
|
|
|
|
sdlScreen = 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();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool init(int w, int h)
|
|
|
|
{
|
|
|
|
if(w)
|
|
|
|
display_width = w;
|
|
|
|
if(h)
|
|
|
|
display_height = h;
|
|
|
|
|
|
|
|
bool createdSurface = createSurface();
|
|
|
|
|
|
|
|
if(!createdSurface)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
glViewport(0, 0, display_width, display_height);
|
|
|
|
glOrtho(0, display_width, display_height, 0, -1.0, 1.0);
|
|
|
|
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
|
|
|
|
|
2013-04-12 02:59:19 +00:00
|
|
|
onInit();
|
2013-04-10 17:29:07 +00:00
|
|
|
|
2012-09-10 18:48:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void deinit()
|
|
|
|
{
|
2013-04-12 02:59:19 +00:00
|
|
|
onDeinit();
|
2013-04-10 17:29:07 +00:00
|
|
|
|
2012-09-10 18:48:00 +00:00
|
|
|
destroySurface();
|
|
|
|
}
|
|
|
|
};
|