2012-09-10 18:48:00 +00:00
|
|
|
#include "Renderer.h"
|
|
|
|
#include <iostream>
|
|
|
|
#include "platform.h"
|
|
|
|
#include GLHEADER
|
|
|
|
#include "Font.h"
|
|
|
|
#include <SDL/SDL.h>
|
2012-09-14 18:22:01 +00:00
|
|
|
#include "InputManager.h"
|
2012-09-10 18:48:00 +00:00
|
|
|
|
|
|
|
namespace Renderer
|
|
|
|
{
|
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)
|
|
|
|
{
|
2012-09-16 19:18:11 +00:00
|
|
|
std::cout << "Creating surface...";
|
2012-09-10 18:48:00 +00:00
|
|
|
|
2012-10-13 18:29:53 +00:00
|
|
|
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO) != 0)
|
2012-09-10 18:48:00 +00:00
|
|
|
{
|
|
|
|
std::cerr << "Error initializing SDL!\n";
|
2012-10-13 18:29:53 +00:00
|
|
|
std::cerr << " " << SDL_GetError() << "\n";
|
2012-09-10 18:48:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2012-09-23 21:01:56 +00:00
|
|
|
sdlScreen = SDL_SetVideoMode(display_width, display_height, 16, SDL_OPENGL | SDL_FULLSCREEN);
|
2012-09-10 18:48:00 +00:00
|
|
|
|
|
|
|
if(sdlScreen == NULL)
|
|
|
|
{
|
|
|
|
std::cout << "Error creating SDL video surface!\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-09-14 18:22:01 +00:00
|
|
|
//we need to reload input too since SDL shut down
|
|
|
|
InputManager::loadConfig();
|
|
|
|
|
2012-09-10 18:48:00 +00:00
|
|
|
//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;
|
|
|
|
|
2012-09-16 19:18:11 +00:00
|
|
|
std::cout << "success!\n";
|
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;
|
|
|
|
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;
|
|
|
|
|
|
|
|
Font::initLibrary();
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
onInit();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void unloadFonts(); //defined in Renderer_draw_gl.cpp
|
|
|
|
void deinit()
|
|
|
|
{
|
|
|
|
onDeinit();
|
|
|
|
|
|
|
|
unloadFonts();
|
|
|
|
destroySurface();
|
|
|
|
}
|
|
|
|
};
|