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

## Recommended Posts

##### Share on other sites
1) That code is horrible. time to refactor (or re-write preferably!)
2) do not read data from a file every frame, you'd be better off loading the data into memory.
3) use glPushMatrix / glPopMatrix
4) you may get rounding errors if you are pumping int's into glTranslatef, try using glTranslated instead.
5) use the source tags when posting code, it makes it much easier to read.
7) if you are just trying to draw pixel areas, you could use glDrawPixels maybe?

Quote:
 Look, icetrash is a data type I made up and it has an overloaded =Char * operator so ignore that it isn't important. The important thing is I glTranslate on x-axis 16 pixels 64 times. Then after that ix loop I glTranslate 1024 pixels opposite so I SHOULD be back where I started.

use glPushMatrix() *before* the loop, use glPopMatrix after the loop instead of traslating back 1024.

##### Share on other sites
Just ignore this I'm practicing source tags, never heard of them before.

static void drawblocks(void){   glPushMatrix();   glTranslatef(-(fx%16),-(fy%16),0.0);   for(ij=0;ij<48;ij++)   {      infile.seekg((fy/16+ij)*64*icol*2+2+(fx/16)*2);  //simplify the math later, left it like this for understanding      infile.read(rgcgrabber,128);      for(ix=0;ix<64;ix++)      {         icetrash=&rgcgrabber[ix*2];         if(icetrash!=0)         {            if(icetrash==1)               glColor3f(1.0,0.0,0.0);            else               glColor3f(0.0,1.0,0.0);            glBegin(GL_POLYGON);               glTexCoord2f(0.0,0.0);	glVertex3f(0,0,0.0);               glTexCoord2f(1.0,0.0);	glVertex3f(16,0,0.0);               glTexCoord2f(1.0,1.0);	glVertex3f(16,16,0.0);               glTexCoord2f(0.0,1.0);	glVertex3f(0,16,0.0);            glEnd();         }         glTranslatef(16.0,0.0,0.0);      }      glTranslatef(-1024.0,16.0,0.0);   }   glTranslatef(fx%16,(fy%16)-768.0,0.0);   glPopMatrix();  //I give up.  Someone get rid of the Push/Pop matrix.  Then load a map with some                   //rats and windmills and watch the screen float around.  I cannot figure out what in holy hell is going on.}

##### Share on other sites
Okay,
1) I'm not a guru, cut me some slack

2) The file I'm reading is 58K and most of it represents blocks of 16x16 on a 1024x768 map. Since this map is 3x3 screens wide then technically I only use about 1/9 of the file for this drawblocks() function. I don't know how to load it into memory except by copying it into an array and whenever I make an array that massive it crashes my computer. I can't remember the biggest array I can make I think it was like 3800 bytes or something. I ended up using that 128 byte array and grabbing one row at a time. I know memory would be faster I just don't know how to do it. Maybe just malloc a space the size of the file and dump all the bytes into it? I have no idea.

3) Yeah I just read about glPop/Push matrix and I guess they are faster than reverse translating. All the same I still want to know why reverse translating wasn't working.

4) I tried pumping floats into glTranslatef instead of ints and it made no difference. Using glTranslated with float, ints, or doubles made no difference.

5) Check.

6) I don't know much about projection matrices. I just don't see how if I comment out the translating in drawblocks() then my screen quits floating around. Every translation in there should cancel itself out perfectly there's no reason you shouldn't end up back at the exact same spot. It's not like there's other threads messing with those variable. But I'm absolutely certain there's a very small amount of error in that function that the rest of the program compounds. I guess it doesn't matter anymore because I'm using glPop/PushMatrix() but still I would like to figure out why translating backwards wasn't working.

7) Yeah there's a lot of stuff I don't know. I might leave it as a polygon though because I might slap a texture on it later. It doesn't matter. I only use drawblocks() for debugging it's not part of the final program.

If I loaded the part of the file I need into memory and cut down my glTranslates and use glPopMatrix instead I don't know that there's much I can do to improve this code. Thanks for the advice.

static void drawblocks(void){   //glPushMatrix();   glTranslatef(-(fx%16),-(fy%16),0.0);   for(ij=0;ij<48;ij++)   {      infile.seekg((fy/16+ij)*64*icol*2+2+(fx/16)*2);  //simplify the math later, left it like this for understanding      infile.read(rgcgrabber,128);      for(ix=0;ix<64;ix++)      {         icetrash=&rgcgrabber[ix*2];         if(icetrash!=0)         {            if(icetrash==1)               glColor3f(1.0f,0.0f,0.0f);            else               glColor3f(0.0f,1.0f,0.0f);            glBegin(GL_POLYGON);               glTexCoord2f(0.0f,0.0f);	glVertex3f(0.0f,0.0f,0.0f);               glTexCoord2f(1.0f,0.0f);	glVertex3f(16.0f,0.0f,0.0f);               glTexCoord2f(1.0f,1.0f);	glVertex3f(16.0f,16.0f,0.0f);               glTexCoord2f(0.0f,1.0f);	glVertex3f(0.0f,16.0f,0.0f);            glEnd();         }         glTranslated(16,0,0);      }      glTranslatef(-1024.0f,16.0f,0.0f);   }   glTranslatef(fx%16,(fy%16)-768.0f,0.0f);   //glPopMatrix();  //I give up.  Someone get rid of the Push/Pop matrix.  Then load a map with some                   //rats and windmills and watch the screen float around.  I cannot figure out what in holy hell is going on.}

##### Share on other sites

if you are having trouble allocating memory, you are probably trying to put in on the stack.... use new and delete instead. This should solve your problem of not being able to allocate the image in memory. If not, you got serious problems indeed.... time to get a new compiler.

second, you are absolutely correct on what is causing your rounding errors. It is very important that you don't have possibility for cumulative rounding errors. In your case, I woule be tempted to set the raster position to the corner and write pixels that way for your overlay. glDrawPixels is a good function to use since it would appear you are using screen coordinates anyway.

I hope this helps...

##### Share on other sites
Hey I just emailed you. I'm in STL.

Man, there's nothing wrong with my compiler I'm on Slackware Linux with the GNU g++ compiler. Now that I think about it the time I couldn't run a program with a static 54 Kilobyte character array I was using MS VC++. Like this:

char rgcval[55296];

That simply wouldn't work. Or maybe it was like this:

char rgcval[256][256];

I can't remember. But arrays like that simply didn't work. It was like the program would start and try to statically declare all that memory and then it would die.

So if I used new and delete I suppose I would have to create a data type that holds 55296 chars or bytes and then declare one and then copy my file into it?

[code]
struct fun
{
char rccfun[55296];
};
[\code]

And then do a

new fun; and a

delete fun;

I remember doing a

char *rgcfun;
rgcfun=new char[55296];

and a

delete[] rgcfun;

but that didn't work either, it still crashed.

I'm not sure that it matters. Right now the program works awesome. I can have almost 1000 monsters on the screen, the only time the file is used is if you or a monster moves to the next block then the program infile.seekg's to that specific two bytes and reads it in and checks it. It works very fast.

I suppose memory would be faster though. I'm still not convinced it's worth it to read that whole file into memory when only like 5% of those blocks will ever be read by the program. That drawblocks() function I was stressed about isn't really part of the program it's for debugging. The normal game just grabs a couple bytes from the file when something moves.

Still I'd like to know how to read it all into memory it seems good to know.

##### Share on other sites
#include <iostream>#include <fstream>// open filestd::ifstream file;file.open('yourmap.fun', std::ios::binary);// get lengthfile.seekg (0, std::ios::end);unsigned int length = file.tellg();file.seekg (0, std::ios::beg);// create bufferchar *buffer = new char [length];// fill bufferfile.read(buffer,length);// close filefile.close();

##### Share on other sites

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

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628688
• Total Posts
2984243

• 16
• 13
• 13
• 10
• 10