support for english language rendering added
authorIvan Hernanez <iturtleman128@gmail.com>
Mon, 28 May 2012 06:10:44 +0000 (01:10 -0500)
committerIvan Hernanez <iturtleman128@gmail.com>
Mon, 28 May 2012 06:10:44 +0000 (01:10 -0500)
Level.cpp

index 961fed5..2a01403 100644 (file)
--- a/Level.cpp
+++ b/Level.cpp
@@ -1,4 +1,4 @@
-#include "Level.h"
+#include "Level.h"
 #include "Game.h"
 #include "fns.h"
 #include <GL/gl.h>
@@ -46,6 +46,77 @@ void Level::DrawIMG(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2
        SDL_BlitSurface(img, &src, mScreen, &dest);
 }
 
+int round(double x)
+{
+       return (int)(x + 0.5);
+}
+
+void SDL_GL_RenderText(string s, 
+       TTF_Font *font,
+       SDL_Color color,
+       SDL_Rect *location)
+{
+       const char* text = s.c_str();
+       SDL_Surface *initial;
+       SDL_Surface *intermediary;
+       SDL_Rect rect;
+       int w,h;
+       Texture texture;
+
+       /* Use SDL_TTF to render our text */
+       initial = TTF_RenderText_Blended(font, text, color);
+
+       /* Convert the rendered text to a known format */
+       w = initial->w;
+       h = initial->h;
+
+       intermediary = SDL_CreateRGBSurface(0, w, h, 32, 
+               0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
+
+       SDL_BlitSurface(initial, 0, intermediary, 0);
+
+       /* Tell GL about our new texture */
+       glGenTextures(1, &texture);
+       glBindTexture(GL_TEXTURE_2D, texture);
+       glTexImage2D(GL_TEXTURE_2D, 0, 4, w, h, 0, GL_BGRA, 
+               GL_UNSIGNED_BYTE, intermediary->pixels );
+
+       /* GL_NEAREST looks horrible, if scaled... */
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);       
+
+       /* prepare to render our texture */
+       glEnable(GL_TEXTURE_2D);
+       glBindTexture(GL_TEXTURE_2D, texture);
+       glColor3f(1.0f, 1.0f, 1.0f);
+
+       /* Draw a quad at location */
+       glBegin(GL_QUADS);
+       /* Recall that the origin is in the lower-left corner
+       That is why the TexCoords specify different corners
+       than the Vertex coors seem to. */
+       glTexCoord2f(0.0f, 0.0f); 
+       glVertex2f(location->x    , location->y);
+       glTexCoord2f(1.0f, 0.0f); 
+       glVertex2f(location->x + w, location->y);
+       glTexCoord2f(1.0f, 1.0f); 
+       glVertex2f(location->x + w, location->y + h);
+       glTexCoord2f(0.0f, 1.0f); 
+       glVertex2f(location->x    , location->y + h);
+       glEnd();
+
+       /* Bad things happen if we delete the texture before it finishes */
+       glFinish();
+
+       /* return the deltas in the unused w,h part of the rect */
+       location->w = initial->w;
+       location->h = initial->h;
+
+       /* Clean up */
+       SDL_FreeSurface(initial);
+       SDL_FreeSurface(intermediary);
+       glDeleteTextures(1, &texture);
+}
 void Level::drawScene()
 {
 
@@ -55,16 +126,6 @@ void Level::drawScene()
        /** Create a black mBackgroundground using the mScreen pixel format (32 bpp) */
        glClear(GL_COLOR_BUFFER_BIT);
 
-       /** Draw BG */
-       if(mBackground!=NULL)
-               mBackground->draw();
-
-       /** Draw Sprites*/
-       DrawSprites();
-
-       /** Draw Boundary Data */
-       DrawCollisions();
-
        // Load a font
        TTF_Font *font;
        font = TTF_OpenFont("FreeSans.ttf", 24);
@@ -72,30 +133,43 @@ void Level::drawScene()
        if(font){
                // Write text to surface
                SDL_Surface *text;
-               SDL_Color text_color = {255, 255, 255};
+               SDL_Color text_color = {0xFF, 0xFF, 0xFF};
                string txt="A journey of a thousand miles begins with a single step.";
-               text = TTF_RenderText_Solid(font, txt.c_str(), text_color);
-
-               if (text == NULL)
-               {
-                       cerr << "TTF_RenderText_Solid() Failed: " << TTF_GetError() << endl;
-                       TTF_Quit();
-                       SDL_Quit();
-                       exit(1);
-               }
+               SDL_Rect rect = {0,0,100,100};
+               SDL_GL_RenderText(txt, font, text_color, &rect);
+       }
+       else
+               cerr << "TTF_OpenFont() Failed: " << TTF_GetError() << endl;
 
-               // Apply the text to the display
-               if (SDL_BlitSurface(text, NULL, Game::game()->Screen(), NULL) != 0)
-               {
-                       cerr << "SDL_BlitSurface() Failed: " << SDL_GetError() << endl;
-               }
+       TTF_CloseFont(font);
+
+       // Load a font
+       TTF_Font *font;
+       font = TTF_OpenFont("FreeSans.ttf", 24);
 
-               SDL_FreeSurface(text);
+       if(font){
+               // Write text to surface
+               SDL_Surface *text;
+               SDL_Color text_color = {0xFF, 0xFF, 0xFF};
+               string txt="日本語";
+               SDL_Rect rect = {0,100,100,100};
+               SDL_GL_RenderText(txt, font, text_color, &rect);
        }
        else
                cerr << "TTF_OpenFont() Failed: " << TTF_GetError() << endl;
 
        TTF_CloseFont(font);
+
+       /** Draw BG */
+       if(mBackground!=NULL)
+               mBackground->draw();
+
+       /** Draw Sprites*/
+       DrawSprites();
+
+       /** Draw Boundary Data */
+       DrawCollisions();
+
        /** Flip the working image buffer with the mScreen buffer */
        SDL_GL_SwapBuffers();
 }