Advertisement

(Tao C#) Help with rendering textured TTF fonts in OGL via SDL_TTF

Started by January 13, 2012 10:21 PM
0 comments, last by ste3e 12 years, 9 months ago
I'm working with the Tao Framework in C# trying to use SDL's TrueType along with OpenGL to render text on the screen.
I've lifted some code from various tutorials or translated code from c++/CLI to try to do this. I've already rendered a test which worked fine(using the tutorial from http://www.sdltutori...ngl-with-csharp)

Now I'm trying to change my program to render some simple text via SDL's Truetype library creating a texture based on a string. The program compiles fine, but I don't get any sort of rendering.

I've never worked with SDL or OGL before, but I generally prefer to learn on my feet, changing a program as I go and adding to it, rather than testing individual things one by one, which doesn't suit reading tutorials all that well.

I haven't exactly done much programming lately so I'm a bit rusty, but I'm still familiar with C#.

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Tao.Sdl;
using Tao.OpenGl;
using System.Runtime.InteropServices;
using Tao.Platform.Windows; //Is this even needed?
namespace TaoTest
{
public class Program
{
//For attaching a console, which apparently doesn't work, but does no harm either
[DllImport("kernel32.dll")]
static extern bool AttachConsole(int dwProcessId);
private const int ATTACH_PARENT_PROCESS = -1;
//Member variables
public static Sdl.SDL_Surface surface = new Sdl.SDL_Surface(); //holds our buffer surface
private Sdl.SDL_VideoInfo videoinfo;
private int height = 768; //default resolution
private int width = 1366;
private int bpp = 16; //16 bit graphics
private int flags = Sdl.SDL_OPENGL | Sdl.SDL_GL_DOUBLEBUFFER | Sdl.SDL_NOFRAME | Sdl.SDL_HWSURFACE;// | Sdl.SDL_FULLSCREEN;
private int TimeCatcher = 0; //used for timing
private Game game;

public Program()
{
if (Sdl.SDL_Init(Sdl.SDL_INIT_VIDEO) < 0) //This seems to throw an exception from not finding Apple.Foundation.
{
Console.WriteLine("Error initializing SDL");
MessageBox.Show("Error Initializing SDL");
Sdl.SDL_Quit();
return;
}

//Comments mostly from tutorial
/* Rather than set the video properties up in the constructor, I set
them in setVideo. The reason for this is that 2 pointers are used
to interact with SDL structures. Once used they convert their
handles into vidInfo and surface tamer variables. That this
occurs inside the function means the pointers will release
their memory on function exit.
*/
setSDLVideo();
/* openGL is not part of SDL, rather it runs in a window handled
by SDL. here we set up some openGL state
*/
setOpenGL();
game = new Game();
//initialize game
while (!game.finP)
{
tick(Sdl.SDL_GetTicks() - TimeCatcher);//updates the game object
TimeCatcher = Sdl.SDL_GetTicks();//stores the current time
Sdl.SDL_Delay(1);//release the thread
}
//When that loop is done this gets executed immediately
Sdl.SDL_Quit();
return;
}
private void setSDLVideo()
{
Sdl.SDL_putenv("SDL_VIDEO_CENTERED=center");
IntPtr ptr = IntPtr.Zero;
ptr = Sdl.SDL_GetVideoInfo();
if (ptr == IntPtr.Zero)
{
Console.WriteLine("Error querying video info");
Sdl.SDL_Quit();
return;
}
videoinfo = (Sdl.SDL_VideoInfo)Marshal.PtrToStructure(ptr, typeof(Sdl.SDL_VideoInfo));
/* according to the SDL documentaion, the flags parameter passed to setVideoMode
affects only the 2D SDL surface, not the openGL. To set their properties
use the syntax below. We enable vsync because we are running the loop
unfettered and we don't want the loop redrawing the buffer
while it is being written to screen
*/
//This should run without explicitly setting it.. hopefully
//Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_SWAP_CONTROL, 1);//enable vsync How do I do this in windows?
Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_RED_SIZE, 8);
Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_GREEN_SIZE, 8);
Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_BLUE_SIZE, 8);
Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_DEPTH_SIZE, 16);
Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_MULTISAMPLEBUFFERS, 1);
Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_MULTISAMPLESAMPLES, 2);
Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_DOUBLEBUFFER, 1);

/* the setVideoMode function returns the current frame buffer as an
SDL_Surface. Again, we grab a pointer to it, then place its
content into the non pointery surface variable. I say 'non-pointery',
but this SDL variable must have a pointer in it because it can
access the current pixels in the framebuffer.
*/
ptr=IntPtr.Zero;
ptr=Sdl.SDL_SetVideoMode(width, height, bpp, flags);
if(ptr==IntPtr.Zero){
Console.WriteLine("Error qsetting the video mode");
Sdl.SDL_Quit();
return;
}
surface=(Sdl.SDL_Surface)Marshal.PtrToStructure(ptr, typeof(Sdl.SDL_Surface));
}
private void setOpenGL()
{
resize(width, height);
Gl.glMatrixMode(Gl.GL_MODELVIEW);
Gl.glLoadIdentity();
Gl.glEnable(Gl.GL_DOUBLEBUFFER);
Gl.glEnable(Gl.GL_DEPTH_TEST);
Gl.glShadeModel(Gl.GL_SMOOTH);
Gl.glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
Gl.glClearDepth(1.0f);
Gl.glDepthFunc(Gl.GL_LEQUAL);
Gl.glHint(Gl.GL_PERSPECTIVE_CORRECTION_HINT, Gl.GL_NICEST);
}
private void resize(int width, int height)
{
if (height == 0) height = 1;//avoid divide by zero
/* For some reason my system is squashing the cube. To rectify
the situation I have altered the width/height ratio by
0.09. I think the problem is either that I am running
dual monitors with different resolutions, or Tao.SDL is
playing funny. The same nehe cube runs true when run
SDL on C++.
*/
float ratio = (float)(width / height) + 0.09f;
Gl.glViewport(0, 0, width, height);
Gl.glMatrixMode(Gl.GL_PROJECTION);
Gl.glLoadIdentity();
Glu.gluPerspective(55.0f, ratio, 0.1f, 50.0f);
Gl.glMatrixMode(Gl.GL_MODELVIEW);
Gl.glLoadIdentity();
}
private void tick(long delay)
{
Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
Gl.glLoadIdentity();
game.tick(delay);
Gl.glFlush();
Sdl.SDL_GL_SwapBuffers();
}
[STAThread]
public static void Main()
{
//for attaching a console
AttachConsole(ATTACH_PARENT_PROCESS);
//regular code
Program program = new Program();
}
}
}


Game.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tao.Sdl;
using Tao.OpenGl;
using System.Runtime.InteropServices;
namespace TaoTest
{
class Game
{
private bool fin = false;
private Sdl.SDL_Event e;
// used to rotate the cube
private float xrot=0.0f;
private float yrot=0.0f;
private short xmot;
private short ymot;


/* The constructor is called from the Program constructor prior to the
setting up of the game loop. Here we use it to hide the cursor.
The first two functions should be called together if you want
to both hide the cursor, and keep it centered on screen. If the
second function is omitted the cursor will still move about the
screen, it will not be seen, but when it hits a screen edge and can
go no further, it will stop firing events that would push it off the
screen; not such a good idea when the monster is behind you and you
need to be able to move the mouse in that direction and have your
avatar turn around.
A new event structure is initialized here and re-used for all events
rather than constructing a new event every game loop
*/
public Game(){
Sdl.SDL_ShowCursor(Sdl.SDL_DISABLE);
Sdl.SDL_WM_GrabInput(Sdl.SDL_GRAB_ON);
e=new Sdl.SDL_Event();
}
/* Use a C# property to get and set the loop variable fin. Properties,
as oppesed to methods are compiled as assignments rather than pass
by value functions. Kinda cooler even than Java.
*/
public bool finP
{
get { return fin; }
set { fin = value; }
}
/* pollEvents is called from the tick function in this class.
It will pop events off the queue untill it is empty. Any event of
interest will be caught in the switch statement and its case
handled. Actually, the SDL_QUIT will never eventuate because the window
is frameless and therefore has no quit button. You need to press
escape

At this stage the events, when triggered, write to the console at the
bottom of the MonoDevelop IDE. To make better sense of the output
you might like to comment the first two Console.WriteLine statements
pertaining to dx and dy because thay are called every loop and somewhat
swamp the key and mouse button presses in the Console. Also note that
I do not use wasd, I use efa and space.
*/
private void pollEvents()
{
while (Sdl.SDL_PollEvent(out e) == 1)
{
switch (e.type)
{
case Sdl.SDL_QUIT:
fin = true;
break;
case Sdl.SDL_KEYDOWN:
if (e.key.keysym.sym == Sdl.SDLK_ESCAPE) finP = true;//works
if (e.key.keysym.sym == Sdl.SDLK_e) Console.WriteLine("Moving ford");
if (e.key.keysym.sym == Sdl.SDLK_f) Console.WriteLine("Moving back");
if (e.key.keysym.sym == Sdl.SDLK_a) Console.WriteLine("Strafing left");
if (e.key.keysym.sym == Sdl.SDLK_SPACE) Console.WriteLine("Strafing right");
break;
case Sdl.SDL_MOUSEBUTTONDOWN:
if (e.button.button == Sdl.SDL_BUTTON_LEFT) Console.WriteLine("Let the shoosting begin");
if (e.button.button == Sdl.SDL_BUTTON_RIGHT) Console.WriteLine("Using an object");
break;
case Sdl.SDL_MOUSEMOTION:
//Console.WriteLine("Heading to {0}", e.motion.xrel);
//Console.WriteLine("Pitch to {0}", e.motion.yrel);
xmot = e.motion.xrel;
ymot = e.motion.yrel;
break;
}
}
}
private void draw()
{
Gl.glEnable(Gl.GL_TEXTURE_2D);
IntPtr fontptr = SdlTtf.TTF_OpenFont("c:\\windows\\fonts\\calibri.ttf",12);
//SdlTtf.TTF_Font font = (SdlTtf.TTF_Font)Marshal.PtrToStructure(fontptr,typeof(SdlTtf.TTF_Font));
Sdl.SDL_Color color = new Tao.Sdl.Sdl.SDL_Color(255, 255, 255);
int[] texIDs = new int[1];
Gl.glGenTextures(1, texIDs);
IntPtr surfptr = SdlTtf.TTF_RenderText_Blended(fontptr, "test", color);
//Program.surface = (Sdl.SDL_Surface)Marshal.PtrToStructure(surfptr, typeof(Sdl.SDL_Surface));
Gl.glBindTexture(Gl.GL_TEXTURE_2D, texIDs[0]);
Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, 4, Program.surface.w, Program.surface.h, 0, Gl.GL_BGRA, Gl.GL_UNSIGNED_BYTE, Program.surface.pixels);
//These set bilinear processing on both large and small textures
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR);
Gl.glEnable(Gl.GL_TEXTURE_2D);

float textWidth = 12.0f;
float textHeight = 1.5f;
float textXOrigin = 0.0f;
float textYOrigin = 0.0f;
float textZOrigin = 0.0f;

//Gl.glTranslatef(0.0f, 0.0f, -10.0f);
//Gl.glRotatef(xrot += 0.03f, (float)xmot, 0.0f, 0.0f);//md with event info from mouse
//Gl.glRotatef(yrot += 0.03f, 0.0f, (float)ymot, 0.0f);
Gl.glBegin(Gl.GL_QUADS);
/*
// Front Face
Gl.glNormal3f(0.0f, 0.0f, 1.0f);
Gl.glColor3f(0.2f, 0.5f, 0.2f);
Gl.glVertex3f(-1.0f, -1.0f, 1.0f);
Gl.glVertex3f(1.0f, -1.0f, 1.0f);
Gl.glVertex3f(1.0f, 1.0f, 1.0f);
Gl.glVertex3f(-1.0f, 1.0f, 1.0f);
// Back Face
Gl.glNormal3f(0.0f, 0.0f, -1.0f);
Gl.glColor3f(0.5f, 0.2f, 0.5f);
Gl.glVertex3f(-1.0f, -1.0f, -1.0f);
Gl.glVertex3f(-1.0f, 1.0f, -1.0f);
Gl.glVertex3f(1.0f, 1.0f, -1.0f);
Gl.glVertex3f(1.0f, -1.0f, -1.0f);
// Top Face
Gl.glNormal3f(0.0f, 1.0f, 0.0f);
Gl.glColor3f(0.7f, 0.5f, 0.2f);
Gl.glVertex3f(-1.0f, 1.0f, -1.0f);
Gl.glVertex3f(-1.0f, 1.0f, 1.0f);
Gl.glVertex3f(1.0f, 1.0f, 1.0f);
Gl.glVertex3f(1.0f, 1.0f, -1.0f);
// Bottom Face
Gl.glNormal3f(0.0f, -1.0f, 0.0f);
Gl.glColor3f(0.2f, 0.2f, 0.8f);
Gl.glVertex3f(-1.0f, -1.0f, -1.0f);
Gl.glVertex3f(1.0f, -1.0f, -1.0f);
Gl.glVertex3f(1.0f, -1.0f, 1.0f);
Gl.glVertex3f(-1.0f, -1.0f, 1.0f);
// Right face
Gl.glNormal3f(1.0f, 0.0f, 0.0f);
Gl.glColor3f(0.2f, 0.5f, 0.5f);
Gl.glVertex3f(1.0f, -1.0f, -1.0f);
Gl.glVertex3f(1.0f, 1.0f, -1.0f);
Gl.glVertex3f(1.0f, 1.0f, 1.0f);
Gl.glVertex3f(1.0f, -1.0f, 1.0f);
// Left Face
Gl.glNormal3f(-1.0f, 0.0f, 0.0f);
Gl.glColor3f(0.8f, 0.2f, 0.6f);
Gl.glVertex3f(-1.0f, -1.0f, -1.0f);
Gl.glVertex3f(-1.0f, -1.0f, 1.0f);
Gl.glVertex3f(-1.0f, 1.0f, 1.0f);
Gl.glVertex3f(-1.0f, 1.0f, -1.0f);
*/
//Textstuffs
Gl.glTexCoord2f(0.0f, 0.0f); Gl.glVertex2f(textXOrigin, textYOrigin);
Gl.glTexCoord2f(0.0f, 1.0f); Gl.glVertex2f(textXOrigin, textYOrigin + 2);
Gl.glTexCoord2f(1.0f, 1.0f); Gl.glVertex2f(textXOrigin - 12, textYOrigin + 2);
Gl.glTexCoord2f(0.0f, 1.0f); Gl.glVertex2f(textXOrigin - 12, textYOrigin);
Gl.glDisable(Gl.GL_TEXTURE_2D);

Gl.glEnd();

}
/* Events will basically drive the program logic, which in
turn determines what the game class decides to draw. Once the
logic is in place, the drawing can begin.
*/
public void tick(long delay)
{
pollEvents();
draw();
}
}
}
It could be because you are using the SDL_Surface of the main screen rather than "surfPtr" used to hold the font. surfPtr will hold info about the size of the font and RGB or RGBA.

By the way, if someone knows how to access this info from the IntPtr please tell. In C++ you would use surfPtr->w, and more importantly you can grab surfPtr->Amask to read weather the image is RGB or RGBA.

This topic is closed to new replies.

Advertisement