123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- /*!
- * \file src/WindowSDL.cpp
- * \brief SDL windowing implementation
- *
- * \author xythobuz
- */
-
- #include <cstdio>
- #include <assert.h>
-
- #include "config.h"
- #include "utils/strings.h"
- #include "WindowSDL.h"
-
- #define SUBSYSTEMS_USED (SDL_INIT_VIDEO | SDL_INIT_EVENTS)
-
- WindowSDL::WindowSDL() {
- mInit = false;
- mDriver = NULL;
- mWidth = DEFAULT_WIDTH;
- mHeight = DEFAULT_HEIGHT;
- mFullscreen = false;
- mMousegrab = false;
- mWindow = NULL;
- mGLContext = NULL;
- mFontInit = false;
- mFontName = NULL;
- mFont = NULL;
-
- #ifdef WIN32
- setDriver("libGL32.dll");
- #elif !defined(__APPLE__)
- setDriver("/usr/lib/libGL.so.1");
- #endif
- }
-
- WindowSDL::~WindowSDL() {
- if (mInit) {
- SDL_QuitSubSystem(SUBSYSTEMS_USED);
- SDL_Quit();
- }
-
- if (mFont)
- TTF_CloseFont(mFont);
-
- if (mFontInit)
- TTF_Quit();
-
- if (mDriver)
- delete [] mDriver;
-
- if (mFontName)
- delete [] mFontName;
- }
-
- void WindowSDL::setDriver(const char *driver) {
- assert(driver != NULL);
- assert(driver[0] != '\0');
- assert(mInit == false);
-
- mDriver = bufferString("%s", driver);
- }
-
- void WindowSDL::setSize(unsigned int width, unsigned int height) {
- assert(width > 0);
- assert(height > 0);
-
- mWidth = width;
- mHeight = height;
-
- if (mInit == true) {
- SDL_SetWindowSize(mWindow, mWidth, mHeight);
- resizeGL();
- }
- }
-
- void WindowSDL::setFullscreen(bool fullscreen) {
- mFullscreen = fullscreen;
-
- if (mInit == true) {
- if (mFullscreen)
- SDL_SetWindowFullscreen(mWindow, SDL_WINDOW_FULLSCREEN_DESKTOP);
- else
- SDL_SetWindowFullscreen(mWindow, 0);
- }
- }
-
- void WindowSDL::setMousegrab(bool grab) {
- mMousegrab = grab;
-
- if (mInit == true) {
- if (mMousegrab) {
- SDL_SetRelativeMouseMode(SDL_TRUE);
- } else {
- SDL_SetRelativeMouseMode(SDL_FALSE);
- SDL_ShowCursor(1);
- }
- }
- }
-
- int WindowSDL::initialize() {
- assert(mInit == false);
-
- if (SDL_Init(SUBSYSTEMS_USED) != 0) {
- printf("SDL_Init Error: %s\n", SDL_GetError());
- return -1;
- }
-
- #ifndef __APPLE__
- assert(mDriver != NULL);
- assert(mDriver[0] != '\0');
-
- if (SDL_GL_LoadLibrary(mDriver) < 0) {
- SDL_ClearError();
- if (SDL_GL_LoadLibrary("libGL.so") < 0) {
- SDL_ClearError();
- if (SDL_GL_LoadLibrary("libGL.so.1") < 0) {
- printf("Could not load OpenGL driver!\n");
- printf("SDL_GL_LoadLibrary Error: %s\n", SDL_GetError());
- return -2;
- }
- }
- }
- #endif
-
- int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;
- if (mFullscreen)
- flags |= SDL_WINDOW_FULLSCREEN;
-
- mInit = true;
- setMousegrab(mMousegrab);
-
- if ((SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5) != 0)
- || (SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5) != 0)
- || (SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5) != 0)
- || (SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16) != 0)
- || (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) != 0)) {
- printf("SDL_GL_SetAttribute Error: %s\n", SDL_GetError());
- mInit = false;
- return -3;
- }
-
- mWindow = SDL_CreateWindow(VERSION, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
- mWidth, mHeight, flags);
- if (mWindow == NULL) {
- printf("SDL_CreateWindow Error: %s\n", SDL_GetError());
- mInit = false;
- return -4;
- }
-
- mGLContext = SDL_GL_CreateContext(mWindow);
- if (mGLContext == NULL) {
- printf("SDL_GL_CreateContext Error: %s\n", SDL_GetError());
- mInit = false;
- return -5;
- }
-
- setSize(mWidth, mHeight);
-
- return 0;
- }
-
- void WindowSDL::eventHandling() {
- SDL_Event event;
-
- assert(mInit == true);
-
- while(SDL_PollEvent(&event)) {
- switch (event.type) {
- case SDL_MOUSEMOTION:
-
- break;
-
- case SDL_MOUSEBUTTONDOWN:
- case SDL_MOUSEBUTTONUP:
-
- break;
-
- case SDL_KEYDOWN:
- case SDL_KEYUP:
-
- break;
-
- case SDL_WINDOWEVENT:
- if (event.window.event == SDL_WINDOWEVENT_RESIZED)
- setSize(event.window.data1, event.window.data2);
- break;
-
- case SDL_QUIT:
- exit(0);
- }
- }
- }
-
- void WindowSDL::delay(clock_t ms) {
- assert(mInit == true);
-
- SDL_Delay(ms);
- }
-
- void WindowSDL::swapBuffersGL() {
- assert(mInit == true);
-
- SDL_GL_SwapWindow(mWindow);
- }
-
- void WindowSDL::setFont(const char *font) {
- assert(font != NULL);
- assert(font[0] != '\0');
- assert(mFontInit == false);
-
- mFontName = fullPath(font, 0);
- }
-
- int WindowSDL::initializeFont() {
- assert(mFontInit == false);
- assert(mFontName != NULL);
- assert(mFontName[0] != '\0');
-
- if (TTF_Init() != 0) {
- printf("Could not initialize SDL-TTF!\n");
- return -1;
- }
-
- mFont = TTF_OpenFont(mFontName, 24);
- if (mFont == NULL) {
- printf("TTF_OpenFont Error: %s\n", TTF_GetError());
- return -2;
- }
-
- glGenTextures(1, &mFontTexture);
-
- mFontInit = true;
- return 0;
- }
-
- void WindowSDL::writeString(WindowString *s) {
- assert(s != NULL);
- assert(s->text != NULL);
- assert(mInit == true);
-
- SDL_Color color;
- color.r = s->color[0];
- color.g = s->color[1];
- color.b = s->color[2];
- color.a = s->color[3];
-
- SDL_Surface *surface = TTF_RenderUTF8_Blended(mFont, s->text, color);
- if (surface == NULL) {
- printf("TTF_RenderUTF8_Blended Error: %s\n", TTF_GetError());
- return;
- }
-
- if (TTF_SizeUTF8(mFont, s->text, &s->w, &s->h) != 0) {
- printf("TTF_SizeUTF8 Error: %s\n", TTF_GetError());
- // Don't need to abort
- }
- s->w = (int)((float)s->w * s->scale);
- s->h = (int)((float)s->h * s->scale);
-
- GLenum textureFormat;
- if (surface->format->BytesPerPixel == 4) {
- if (surface->format->Rmask == 0x000000FF)
- textureFormat = GL_RGBA;
- else
- textureFormat = GL_BGRA_EXT;
- } else {
- textureFormat = GL_RGB;
- }
-
- glBindTexture(GL_TEXTURE_2D, mFontTexture);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexImage2D(GL_TEXTURE_2D, 0, surface->format->BytesPerPixel, surface->w, surface->h, 0, textureFormat, GL_UNSIGNED_BYTE, surface->pixels);
-
- GLuint xMin = s->x;
- GLuint yMin = s->y;
- GLuint xMax = xMin + (int)((float)surface->w * s->scale);
- GLuint yMax = yMin + (int)((float)surface->h * s->scale);
-
- glColor4f(color.r / 256.0f, color.g / 256.0f, color.b / 256.0f, color.a / 256.0f);
-
- glBegin(GL_QUADS);
- glTexCoord2f(0.0f, 0.0f);
- glVertex2i(xMin, yMin);
-
- glTexCoord2f(0.0f, 1.0f);
- glVertex2i(xMin, yMax);
-
- glTexCoord2f(1.0f, 1.0f);
- glVertex2i(xMax, yMax);
-
- glTexCoord2f(1.0f, 0.0f);
- glVertex2i(xMax, yMin);
- glEnd();
-
- SDL_FreeSurface(surface);
- }
|