Advertisement
Serdalis

Untitled

Oct 20th, 2011
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.45 KB | None | 0 0
  1. #include "SDL.h"
  2. #include "SDL_ttf.h"
  3. #include <SDL_opengl.h>
  4. #include <fstream>
  5.  
  6. const int SCREEN_WIDTH = 800;
  7. const int SCREEN_HEIGHT = 600;
  8. const int SCREEN_BPP = 32;
  9.  
  10. SDL_Surface* screen = NULL;
  11. TTF_Font *fntCourier;
  12.  
  13. void drawText(char * text);
  14. void render();
  15. bool init();
  16.  
  17. void render() {
  18.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  19.     glLoadIdentity();
  20.  
  21.     drawText("some text");
  22.  
  23.     SDL_GL_SwapBuffers();
  24. }
  25.  
  26. void drawText(char * text) {
  27.     glLoadIdentity();
  28.     SDL_Color clrFg = {60,60,60};
  29.     SDL_Surface *sText;
  30.     sText = TTF_RenderUTF8_Solid( fntCourier, "lols", clrFg);
  31.     GLuint texture[1]; // done because glGenTextures expects an array, even though array[1] == *GLuint
  32.     if(sText == NULL) {
  33.         return;
  34.     }
  35.     glGenTextures(1, texture);
  36.     glBindTexture(GL_TEXTURE_2D, *texture);
  37.     std::fstream myfile;
  38.     myfile.open("lol.txt");
  39.     for(int i = 0; i < sText->w; ++i) {
  40.         for(int j = 0; j < sText->h; ++j) { // this will print out BGR
  41.             myfile << (((int*)(sText->pixels))[j*sText->h+i] & 0xFF) << ((((int*)(sText->pixels))[j*sText->h+i] >> 8) & 0xFF) << ((((int*)(sText->pixels))[j*sText->h+i] >> 16) & 0xFF) << " ";
  42.         }
  43.         myfile << std::endl;
  44.     }
  45.     myfile.close();
  46.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, sText->w, sText->h, 0, GL_RGB, GL_UNSIGNED_BYTE, sText->pixels);
  47.  
  48.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  49.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  50.  
  51.     glBegin(GL_QUADS); {
  52.             glTexCoord2i(0, 0); glVertex3f(0, 0, 0);
  53.             glTexCoord2i(1, 0); glVertex3f(sText->w, 0, 0);
  54.             glTexCoord2i(1, 1); glVertex3f(sText->w, sText->h, 0);
  55.             glTexCoord2i(0, 1); glVertex3f(0, sText->h, 0);
  56.     } glEnd();
  57.     SDL_FreeSurface( sText );
  58.     glDeleteTextures( 1, texture );
  59. }
  60.  
  61. bool init() {
  62.     SDL_Init(SDL_INIT_EVERYTHING);
  63.     TTF_Init();
  64.  
  65.     SDL_GL_SetAttribute(SDL_GL_RED_SIZE,   8);
  66.     SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
  67.     SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,  8);
  68.     SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
  69.  
  70.     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,  16);
  71.     SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
  72.  
  73.     SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE,   8);
  74.     SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE, 8);
  75.     SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE,  8);
  76.     SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, 8);
  77.  
  78.     SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
  79.     SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 2);
  80.  
  81.     screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_HWSURFACE | SDL_GL_DOUBLEBUFFER | SDL_OPENGL);
  82.     if(screen == NULL) return false;
  83.  
  84.     glClearColor(0, 0, 0, 0);
  85.     glClearDepth(1.0f);
  86.     glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
  87.     glMatrixMode(GL_PROJECTION);
  88.     glLoadIdentity();
  89.     glOrtho(0, SCREEN_WIDTH, 0, SCREEN_HEIGHT, 1, -1);
  90.     glMatrixMode(GL_MODELVIEW);
  91.  
  92.     glEnable(GL_TEXTURE_2D);
  93.     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  94.  
  95.     glLoadIdentity();
  96.  
  97.     SDL_WM_SetCaption("drawText", NULL);
  98.  
  99.     fntCourier = TTF_OpenFont( "Fonts\\cour.ttf", 26 );
  100.     if(fntCourier == NULL) return false;
  101.  
  102.     return true;
  103. }
  104.  
  105. int main( int argc, char* argv[] ) {
  106.  
  107.         if(init() == false) return 1;
  108.  
  109.         render();
  110.  
  111.         system("pause");
  112.  
  113.         SDL_FreeSurface(screen);
  114.         TTF_CloseFont(fntCourier);
  115.         TTF_Quit();
  116.         SDL_Quit();
  117.         return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement