Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "SDL.h"
- #include "SDL_ttf.h"
- #include <SDL_opengl.h>
- #include <fstream>
- const int SCREEN_WIDTH = 800;
- const int SCREEN_HEIGHT = 600;
- const int SCREEN_BPP = 32;
- SDL_Surface* screen = NULL;
- TTF_Font *fntCourier;
- void drawText(char * text);
- void render();
- bool init();
- void render() {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glLoadIdentity();
- drawText("some text");
- SDL_GL_SwapBuffers();
- }
- void drawText(char * text) {
- glLoadIdentity();
- SDL_Color clrFg = {60,60,60};
- SDL_Surface *sText;
- sText = TTF_RenderUTF8_Solid( fntCourier, "lols", clrFg);
- GLuint texture[1]; // done because glGenTextures expects an array, even though array[1] == *GLuint
- if(sText == NULL) {
- return;
- }
- glGenTextures(1, texture);
- glBindTexture(GL_TEXTURE_2D, *texture);
- std::fstream myfile;
- myfile.open("lol.txt");
- for(int i = 0; i < sText->w; ++i) {
- for(int j = 0; j < sText->h; ++j) { // this will print out BGR
- 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) << " ";
- }
- myfile << std::endl;
- }
- myfile.close();
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, sText->w, sText->h, 0, GL_RGB, GL_UNSIGNED_BYTE, sText->pixels);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glBegin(GL_QUADS); {
- glTexCoord2i(0, 0); glVertex3f(0, 0, 0);
- glTexCoord2i(1, 0); glVertex3f(sText->w, 0, 0);
- glTexCoord2i(1, 1); glVertex3f(sText->w, sText->h, 0);
- glTexCoord2i(0, 1); glVertex3f(0, sText->h, 0);
- } glEnd();
- SDL_FreeSurface( sText );
- glDeleteTextures( 1, texture );
- }
- bool init() {
- SDL_Init(SDL_INIT_EVERYTHING);
- TTF_Init();
- 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_ALPHA_SIZE, 8);
- SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
- SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
- SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE, 8);
- SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE, 8);
- SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, 8);
- SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, 8);
- SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
- SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 2);
- screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_HWSURFACE | SDL_GL_DOUBLEBUFFER | SDL_OPENGL);
- if(screen == NULL) return false;
- glClearColor(0, 0, 0, 0);
- glClearDepth(1.0f);
- glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0, SCREEN_WIDTH, 0, SCREEN_HEIGHT, 1, -1);
- glMatrixMode(GL_MODELVIEW);
- glEnable(GL_TEXTURE_2D);
- glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
- glLoadIdentity();
- SDL_WM_SetCaption("drawText", NULL);
- fntCourier = TTF_OpenFont( "Fonts\\cour.ttf", 26 );
- if(fntCourier == NULL) return false;
- return true;
- }
- int main( int argc, char* argv[] ) {
- if(init() == false) return 1;
- render();
- system("pause");
- SDL_FreeSurface(screen);
- TTF_CloseFont(fntCourier);
- TTF_Quit();
- SDL_Quit();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement