Archived

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

enigmamwu03

SDL slow start up

Recommended Posts

I have noticed that when I have SDL loading more than one texture it is slow about starting the program. It displays the outline of my window and then a few seconds later after the textures and stuff have been intialized it starts my game loop. Is there anyway to speed my loading of tgas up? Here is my loading tga code: tImageTGA *etextures::LoadTGA(const char *texturename) { tImageTGA *pImageData = NULL; int width = 0, height = 0; byte length = 0; byte imageType = 0; byte bits = 0; FILE *pFile = NULL; int channels = 0; int stride = 0; int i = 0; if((pFile = fopen(texturename, "rb")) == NULL) { cout << "Could not open the tga file : " << texturename << endl; return NULL; } pImageData = (tImageTGA*)malloc(sizeof(tImageTGA)); fread(&length, sizeof(byte), 1, pFile); fseek(pFile,1,SEEK_CUR); fread(&imageType, sizeof(byte), 1, pFile); fseek(pFile, 9, SEEK_CUR); fread(&width, sizeof(WORD), 1, pFile); fread(&height, sizeof(WORD), 1, pFile); fread(&bits, sizeof(byte), 1, pFile); fseek(pFile, length + 1, SEEK_CUR); if(imageType != TGA_RLE) { if(bits == 24 || bits == 32) { channels = bits / 8; stride = channels * width; pImageData->data = new unsigned char[stride * height]; for(int y = 0; y < height; y++) { unsigned char *pLine = &(pImageData->data[stride * y]); fread(pLine, stride, 1, pFile); for(i = 0; i < stride; i += channels) { int temp = pLine; pLine[i] = pLine[i + 2]; pLine[i + 2] = temp; } } } else if(bits == 16) { unsigned short pixels = 0; int r=0, g=0, b=0; channels = 3; stride = channels * width; pImageData->data = new unsigned char[stride * height]; for(int i = 0; i < width*height; i++) { fread(&pixels, sizeof(unsigned short), 1, pFile); b = (pixels & 0x1f) << 3; g = ((pixels >> 5) & 0x1f) << 3; r = ((pixels >> 10) & 0x1f) << 3; pImageData->data[i * 3 + 0] = r; pImageData->data[i * 3 + 1] = g; pImageData->data[i * 3 + 2] = b; } } else return NULL; } else { byte rleID = 0; int colorsRead = 0; channels = bits / 8; stride = channels * width; pImageData->data = new unsigned char[stride * height]; byte *pColors = new byte [channels]; while(i < width*height) { fread(&rleID, sizeof(byte), 1, pFile); if(rleID < 128) { rleID++; while(rleID) { fread(pColors, sizeof(byte) * channels, 1, pFile); pImageData->data[colorsRead + 0] = pColors[2]; pImageData->data[colorsRead + 1] = pColors[1]; pImageData->data[colorsRead + 2] = pColors[0]; if(bits == 32) pImageData->data[colorsRead + 3] = pColors[3]; i++; rleID--; colorsRead += channels; } } else { rleID -= 127; fread(pColors, sizeof(byte) * channels, 1, pFile); while(rleID) { pImageData->data[colorsRead + 0] = pColors[2]; pImageData->data[colorsRead + 1] = pColors[1]; pImageData->data[colorsRead + 2] = pColors[0]; if(bits == 32) pImageData->data[colorsRead + 3] = pColors[3]; i++; rleID--; colorsRead += channels; } } } } fclose(pFile); pImageData->channels = channels; pImageData->sizeX = width; pImageData->sizeY = height; return pImageData; } void etextures::CreateTexture(UINT textureArray[],char * strFileName, int textureID) { if(!strFileName) return; tImageTGA *pImage = LoadTGA(strFileName); if(pImage == NULL) exit(0); glGenTextures(1, &textureArray[textureID]); glBindTexture(GL_TEXTURE_2D, textureArray[textureID]); int textureType = GL_RGB; if(pImage->channels == 4) textureType = GL_RGBA; gluBuild2DMipmaps(GL_TEXTURE_2D, pImage->channels, pImage->sizeX, pImage->sizeY, textureType, GL_UNSIGNED_BYTE, pImage->data); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); if (pImage) { if (pImage->data) { free(pImage->data); } free(pImage); } }

Share this post


Link to post
Share on other sites
yes ive had this problem.
does it freeze for about 1 second when its initializing the window under windows?
i believe this is due to the methods they use to query various screen things, they use directdraw under windows which takes a while to get going (i dont know why).

btw i noticed with the new nvidia drivers 40x startup time is now quick (theyve improved directdraw querys somehow im guessing)

http://uk.geocities.com/sloppyturds/kea/kea.html
http://uk.geocities.com/sloppyturds/gotterdammerung.html

Share this post


Link to post
Share on other sites
Yeah, It freezes for about 5 seconds. One thing though I am using opengl not directx, I refuse to use any directx in anything i make.
I am now loading about 87 textures but it takes the same amount of pause to show anything but the window outline. I went and got the nvidia 40x drivers for windows xp but it still has the delay.
All solutions i can get i am greatful for in advance.

Share this post


Link to post
Share on other sites
draw onto the screen before you start with your loading process.. does it get drawn early? i don''t have problems here but i remember i had one time..

if it works to draw early, then load, then you could add a loadingbar..

"take a look around" - limp bizkit
www.google.com

Share this post


Link to post
Share on other sites
Okay, I will try that out in a minute here, but I just found another small glitch. When I press the full screen button in the window I lose all my textures until i changed the code and then i get the pause again while in the middle of the game.
Here is my resize cose and the main() that sets up the main window:


case SDL_VIDEORESIZE :

MainWindow = SDL_SetVideoMode( event.resize.w, event.resize.h, SDEPTH, VFlags );
InitializeOpenGL(event.resize.w, event.resize.h);reinits opengl
Init();//reload textures

if(MainWindow == NULL)
{
cerr << " Failed resizing SDL window : " << SDL_GetError() << endl;
Exit(0);
}
break;

int main( int argc, char* argv[] )
{
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
cerr << "Failed initializing SDL Video : " << SDL_GetError() << endl;
return 1;
}

SetupPixelFormat();

MakeWindow("Enigma:Mysteries Withen", SWIDTH, SHEIGHT, VFlags);//createsthe window

InitializeOpenGL(SWIDTH, SHEIGHT);//sets up opengl
Init();//loads in the textures
MainLoop();

return 0;
}

Share this post


Link to post
Share on other sites