• Advertisement

Archived

This topic is now archived and is closed to further replies.

Textures are crashing my program!?!

This topic is 5363 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to write a 2D engine. I've got some textured test objects (textured quads) and a font (to print out FPS). Both (quads and font) are objects (classes). In constructor I load (and set all parameters etc.) textures and save theirs ID. In draw function I bind the id (glBindTexture (GL_TEXTURE_2D, texture.id); ). When theese textures are the same (the same file, different ID's) it works OK. But when I use one texture for font and other one for that quad the font doesn't work. It just displays the first letter. What's even stranger - sometimes it works OK (font is displayed correctly)! I'm a newbie, so this is probably very stupid, but I really don't know what's wrong. Oxygenium [edited by - oxygenium on August 17, 2003 10:03:03 AM]

Share this post


Link to post
Share on other sites
Advertisement
Sure . Also another question: Isn't there somwhere a sourcecode for 2D engine (for learning purposes - free)? Just asking...

Oxygenium

[edited by - oxygenium on August 17, 2003 1:54:56 PM]

Share this post


Link to post
Share on other sites
It has 45k and is divided into 19 files... . And have czech comments. But some code:

font.h:

/*
soubor: font.h

popis: obsahuje tridu Font. Ta obstarava
vypisovani textu na obrazovku

programator: Oxyd <oxygenium@volny.cz>

datum: 11.8.2003
*/


#ifndef FONT_H
#define FONT_H

#include "object.h"
#include "opengl.h"
#include "TextureLoader.h"
#include <string>

/*
trida Font - obstarava vypisovani textu
- je to normalni objekt (potomek tridy Object)
*/

class Font : public Object {
public:
/*
konstruktor
_x, _y, _l - pozice textu (levej horni roh)
text - textura s pismem
s - retezec - defaultni retezec
*/

Font (int _x, int _y, int _l, std::string text, std::string s = "");

Font (const Font &f);

//destruktor

inline ~Font ();

/*
zmeni text
- s - novej text
*/

inline void setText (std::string s) { str = s; }

//vykresli text na obrazovku

void draw () const;

//inicializuje pismo

void init ();

//uvolni texturu

inline void free ();
private:
std::string str, text;
static TextureLoader::TextureInfo texture;
static unsigned base;
};

//destruktor

Font::~Font ()
{
//uvolnim textury

free ();
}

void Font::free ()
{
TextureLoader tx;
tx.destroy (texture);

if (base != 0) {
glDeleteLists (128, base);
base = 0;
}
}

#endif


Font.cpp


/*
soubor: font.cpp

popis: implementace tridy Font

programator: Oxyd <oxygenium@volny.cz>

datum: 11.8.2003
*/


#include "font.h"
#include "opengl.h"
#include "exceptions.h"
#include <windows.h>
using namespace std;

TextureLoader::TextureInfo Font::texture;
unsigned Font::base = 0;

//konstruktor

Font::Font (int _x, int _y, int _l, string t, string s) : Object (_x, _y, _l)
{
str = s;
text = t;
}

void Font::init ()
{
//pismo jeste neni vytvoreno

//natahnu texturu pisma

TextureLoader tx;
tx.load (texture, text);
tx.bind (texture, false, GL_LINEAR, GL_LINEAR);

//vytvorim display-list

const int LETTERS = 128;
base = glGenLists (LETTERS);

//a rozsekam texturu do pismen

const double ONE_SIXTEENTH = (1.0 / 16.0);
double x, y;

for (int i = 0; i < LETTERS; i++) {
//vygeneruju souradnice aktualniho znaku

x = static_cast <double>(i % 16) / 16.0;
y = static_cast <double>(i / 16) / 16.0;

//vytvorim display-list pro znak

glNewList (base + i, GL_COMPILE);
glBegin (GL_QUADS);
glTexCoord2f (x, 1 - y - ONE_SIXTEENTH);
glVertex2i (0, 16);
glTexCoord2f (x + ONE_SIXTEENTH, 1 - y - ONE_SIXTEENTH);
glVertex2f (16, 16);
glTexCoord2f (x + ONE_SIXTEENTH, 1 - y);
glVertex2f (16, 0);
glTexCoord2f (x, 1 - y);
glVertex2f (0, 0);
glEnd ();

//translatuju na dalsi pismo

glTranslated (16, 0, 0);
glEndList ();
}
}

//nakresli text

void Font::draw () const
{
//binduju texturu s pismem

glBindTexture (GL_TEXTURE_2D, texture.id);

glColor3f (1, 1, 1);
glPushAttrib (GL_LIST_BIT);
glListBase (base - 32);
glCallLists (str.length (), GL_UNSIGNED_BYTE, str.c_str ());
glPopAttrib ();
}


And test.cpp - textured quad - purely for testing purposes


/*
soubor: test.cpp

popis: implementace tridy test

programator: Oxyd <oxygenium@volny.cz>

datum: 11.8.2003
*/


#include "test.h"
#include "exceptions.h"
using namespace std;

//vytvori objekt test

Test::Test (int _x, int _y, int _l, float _r, float _g, float _b, string t)
: Object (_x, _y, _l)
{
r = _r;
g = _g;
b = _b;
texture = t;
}

//natahne textury pro objekt test

void Test::init ()
{
TextureLoader tx;
tx.load (text, texture);
tx.bind (text, true, GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR);
}

//vykresli objekt test

void Test::draw () const
{
glBindTexture (GL_TEXTURE_2D, text.id);

static const int SIZE = 100;

glColor3f (r, g, b);
glBegin (GL_QUADS);
glTexCoord2f (1, 1); glVertex2f (+SIZE, -SIZE);
glTexCoord2f (0, 1); glVertex2f (-SIZE, -SIZE);
glTexCoord2f (0, 0); glVertex2f (-SIZE, +SIZE);
glTexCoord2f (1, 0); glVertex2f (+SIZE, +SIZE);
glEnd ();
}


You asked for it .

It appears that if I call Font::load () function before all_other_objects::load () it works ok! I don''t know why...

Oxygenium

Share this post


Link to post
Share on other sites

  • Advertisement