Jump to content
  • Advertisement
Sign in to follow this  
philipptr

2 problems about 2d game/sdl/opengl

This topic is 4877 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

First thing is: I started coding an 2d engine with SDL, but the more stuff i added the slower it got and since it had only 76 fps on 2.8ghz I think I have to start learning Opengl. I havent found anything related to 2d with opengl on the net and so i would like to know if theres something different or do i only draw GL_QUADS on top of each other like i did with sdl. So i started reading the Nehe tutorials and especially looked into lesson 6 (textures) and tried to port the functions into the dev-c++ example programm for SDL&OpenGL. it compiles but it doesnt work so it would be very nice if somebody could look through the code: Initialisation
	SDL_Event event;
	float angle = 0.0f;
	
	SDL_Init(SDL_INIT_VIDEO);
	SDL_SetVideoMode(640, 480, 0, SDL_OPENGL | SDL_HWSURFACE /*| SDL_FULLSCREEN*/);
	
	glViewport(0, 0, 640, 480);
	if (!LoadGLTextures())							// Jump To Texture Loading Routine ( NEW )
	{
		return FALSE;							// If Texture Didn't Load Return FALSE ( NEW )
	}
	glEnable(GL_TEXTURE_2D);
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	glClearDepth(1.0);
	glDepthFunc(GL_LESS);
	glEnable(GL_DEPTH_TEST);

	glShadeModel(GL_SMOOTH);
	glMatrixMode(GL_PROJECTION);
	glMatrixMode(GL_MODELVIEW);

Texture loading:
AUX_RGBImageRec *LoadBMP(char *Filename)					// Loads A Bitmap Image
{
	FILE *File=NULL;
	
	if (!Filename)											// Make Sure A Filename Was Given
	{
		return NULL;										// If Not Return NULL
	}
	
	File=fopen(Filename,"r")
	;
	if (File)												// Does The File Exist?
	{
		fclose(File);										// Close The Handle
		return auxDIBImageLoad(Filename);					// Load The Bitmap And Return A Pointer
	}	
	return NULL;											// If Load Failed Return NULL
}

int LoadGLTextures()										// Load Bitmaps And Convert To Textures
{
	int Status=FALSE;										// Status Indicator

	AUX_RGBImageRec *TextureImage[1];						// Create Storage Space For The Texture

	memset(TextureImage,0,sizeof(void *)*1);  	         	// Set The Pointer To NULL

															// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
	if (TextureImage[0]=LoadBMP("sprites/NeHe.bmp"))
	{
		Status=TRUE;										// Set The Status To TRUE

		glGenTextures(1, &texture[0]);						// Create The Texture

															// Typical Texture Generation Using Data From The Bitmap
		glBindTexture(GL_TEXTURE_2D, texture[0]);
		glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
	}

	if (TextureImage[0])									// If Texture Exists
	{
		if (TextureImage[0]->data)							// If Texture Image Exists
		{
///////////////////////////////////////////////////////////////////////////////////////////	
   	free(TextureImage[0]->data);                            // Free The Texture Image Memory 
		}

		free(TextureImage[0]);								// Free The Image Structure
	}

	return Status;										// Return The Status
}

drawing:
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    	glLoadIdentity();
    	glTranslatef(0.0f,0.0f,-5.0f);
		//glRotatef(angle,0.0f,0.0f,1.0f);
		glBindTexture(GL_TEXTURE_2D, texture[0]);	
		angle+=0.3f;        
		glBegin(GL_QUADS);              //specify that we want to draw quadrilaterals
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);		
		glEnd();
		
		SDL_GL_SwapBuffers();       //Copy back buffer to front buffer (screen)

		glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);


Share this post


Link to post
Share on other sites
Advertisement
How exactly do you mean "doesn't work"? Do you get error messages, or do your images just not appear on screen?

I can't really take a good look unless I have a rough idea of what I'm looking for.

Share this post


Link to post
Share on other sites
no errors, it starts but it stays black so i guess theres either something worng with the loading of the texture or with the drawing..

Share this post


Link to post
Share on other sites
Your viewport is probably wrong. You need something like this...


void 2DMode()
{
// a
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// b
glOrtho(0.0f, wWidth2D, wHeight2D, 0.0,-1.0f,1.0f);

// c
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}



That you call before drawing 2D graphics.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Wow... dejá vu!
I had exactly the same problem!!

One advice, just to initialize more properly, do a glLoadIdentity() after doing glMatrixMode(...) in glInit.
What I think it's your problem is that you should call (again) glEnable(GL_TEXTURE_2D) before calling glBindTexture....
I know that sounds stupid, but I had the same problem, I tried that, and I think that was what solved my problem.... Give it a try and see if that works for you ;)

Share this post


Link to post
Share on other sites
both methods didnt really work, but i finally found a tutorial ( http://gpwiki.org/index.php/C:Using_SDL_with_OpenGL ) so i will try that and if that doesnt work ill maybe come back ;)

Share this post


Link to post
Share on other sites
Does it show a white quad if you comment out all the texture stuff?

Maybe there is something small ... for example:
Nehe uses glDepthFunc(...); after glEnable(GL_DEPTH_TEST);
!?

Share this post


Link to post
Share on other sites
so reading the tutorial i got a bit weird about GL_TEXTURE_2D the tutorial says:

//Create the texture
glGenTextures(1, &image);
//Load the texture
glBindTexture(GL_TEXTURE_2D, image);

//Generate the texture
glTexImage2D(GL_TEXTURE_2D, 0, 3, temp->w, temp->h, 0, GL_RGB, GL_UNSIGNED_BYTE, temp->pixels);

//Use linear filtering, very good
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);


so do i always have to call glBindTexture, glTexImage2D and glTexParameteri when drawing a different sprite (different from the one i drew before)? i guess i only have to call glTexParameteri once but it seems like i have to call glTexImage2D every time, right?

Share this post


Link to post
Share on other sites
I think you only need it once.
What you do once:

//Create the texture
glGenTextures(1, &image);
//Load the texture
glBindTexture(GL_TEXTURE_2D, image);

//Generate the texture
glTexImage2D(GL_TEXTURE_2D, 0, 3, temp->w, temp->h, 0, GL_RGB, GL_UNSIGNED_BYTE, temp->pixels);

//Use linear filtering, very good
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);



image holds an id.
glBindTexture uses that Id to set the texture:
glTexImage2D, glTexParameteri affect that texture.
Then you only need to use glBindTexture and the Id image to set a different sprite later.
Pretty much what the code already does.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!