Created
August 29, 2012 14:12
-
-
Save finlaybob/3513165 to your computer and use it in GitHub Desktop.
SDL_ttf with OpenGL 3.3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "UIWindow.h" | |
| UIWindow::UIWindow(int x, int y, int width, int height,string fontName): | |
| x(x),y(y),height(height),width(width)//,titleFontName(fontName) | |
| { | |
| this->myType = Utils::UI_WINDOW; | |
| //build window | |
| ScreenQuad* sq = new ScreenQuad(x,y-height,width,height); | |
| quads.push_back(sq); | |
| sq = new ScreenQuad(x-32,y,32,32); | |
| quads.push_back(sq); | |
| sq = new ScreenQuad(x,y,width,32); | |
| quads.push_back(sq); | |
| sq = new ScreenQuad(x+width,y,32,32); | |
| quads.push_back(sq); | |
| sq = new ScreenQuad(x-32,y-height,32,height); | |
| quads.push_back(sq); | |
| sq = new ScreenQuad(x+width,y-height,32,height); | |
| quads.push_back(sq); | |
| sq = new ScreenQuad(x-32,y-height-32,32,32); | |
| quads.push_back(sq); | |
| sq = new ScreenQuad(x,y-height-32,width,32); | |
| quads.push_back(sq); | |
| sq = new ScreenQuad(x+width,y-height-32,32,32); | |
| quads.push_back(sq); | |
| Texture* t; | |
| t = TextureManager::loadTexture("UI/defBase",COLORMAP); | |
| textures.push_back(t); | |
| t = TextureManager::loadTexture("UI/defBlc",COLORMAP); | |
| textures.push_back(t); | |
| t = TextureManager::loadTexture("UI/defTop",COLORMAP); | |
| textures.push_back(t); | |
| t = TextureManager::loadTexture("UI/defBrc",COLORMAP); | |
| textures.push_back(t); | |
| t = TextureManager::loadTexture("UI/defLeft",COLORMAP); | |
| textures.push_back(t); | |
| t = TextureManager::loadTexture("UI/defRight",COLORMAP); | |
| textures.push_back(t); | |
| t = TextureManager::loadTexture("UI/defTlc",COLORMAP); | |
| textures.push_back(t); | |
| titleBarTex = TextureManager::loadTexture("UI/defBottom",COLORMAP); | |
| textures.push_back(titleBarTex); | |
| t = TextureManager::loadTexture("UI/defTrc",COLORMAP); | |
| textures.push_back(t); | |
| shader = "ui"; | |
| } | |
| UIWindow::~UIWindow(void) | |
| { | |
| } | |
| void UIWindow::setTitleText(string text) | |
| { | |
| GLenum error = glGetError(); | |
| while (error != GL_NO_ERROR) | |
| error = glGetError(); | |
| TTF_Font* titleFont = TTF_OpenFont("Font/Ubuntu.ttf",48); | |
| if(titleFont == NULL) | |
| { | |
| string ttferr = TTF_GetError(); | |
| CEConsole::cecErr("can't load. SDL_ttf says: \"" + ttferr + "\""); | |
| } | |
| SDL_Color fg = {255,128,0,255}; | |
| SDL_Color bg = {0,0,0,0}; | |
| SDL_Surface *surface = TTF_RenderText_Blended(titleFont,text.c_str(),fg); | |
| if (surface == NULL) | |
| CEConsole::cecErr("Surface NULL!"); | |
| GLuint colors = surface->format->BytesPerPixel; | |
| GLuint format; | |
| if (colors == 4) { // alpha | |
| if (surface->format->Rmask == 0x000000ff) | |
| format = GL_RGBA; | |
| else | |
| format = GL_BGRA; | |
| } else { // no alpha | |
| if (surface->format->Rmask == 0x000000ff) | |
| format = GL_RGB; | |
| else | |
| format = GL_BGR; | |
| } | |
| GLubyte *pixies = (GLubyte *)(surface->pixels); | |
| Texture* newTex = new Texture(); | |
| GLuint hTex; | |
| glGenTextures(1,&hTex); | |
| glActiveTexture(GL_TEXTURE0); | |
| glBindTexture(GL_TEXTURE_2D,hTex); | |
| // Must match if using mipmaps or not. | |
| glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); | |
| glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
| glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
| glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
| //glTexImage2D( GL_TEXTURE_2D,0,colors, | |
| // surface->w,surface->h,0, | |
| // format,GL_UNSIGNED_BYTE, | |
| // surface->pixels | |
| // ); | |
| error = glGetError(); | |
| while (error != GL_NO_ERROR) | |
| error = glGetError(); | |
| // Can use "4" on old OpenGL, MUST use "GL_RGBA/GL_RGB" on core( tested on 3.3 ) Note: GL_BGRA and GL_BGR are NOT supported here! | |
| glTexImage2D( GL_TEXTURE_2D,0,GL_RGBA, | |
| surface->w,surface->h,0, | |
| format,GL_UNSIGNED_BYTE, | |
| pixies | |
| ); | |
| error = glGetError(); | |
| if (error == GL_INVALID_ENUM) | |
| CEConsole::cecErr("Invalid Enum error. Again."); | |
| // Must match GL_TEXTURE_MIN_FILTER parameter. (mip or no mips) | |
| glGenerateMipmap(GL_TEXTURE_2D); | |
| newTex->setUnit(0); | |
| newTex->setHandle(hTex); | |
| newTex->setName("newUitex"); | |
| TextureManager::getInstance()->addTexture(newTex); | |
| textures[0] = newTex; | |
| SDL_FreeSurface(surface); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment