Sign in to follow this  

SDL & OpenGL

This topic is 3378 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 can get the window to come up and all that magic but I can't seem to get anything to draw, I'd also like to know a couple of things. Do I have to reset the viewport and perspective when using SDL with OGL, if so, how do I know when the window's resized? How do you set the window caption? I tried SDL_WM_SetCaption() but that didn't work. Here's my code:
#pragma comment(lib, "glut32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "SDLmain.lib")
#pragma comment(lib, "SDL.lib")

#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
#include <iostream>

using std::cout;
using std::endl;

bool InitOGL();
SDL_Event event;

unsigned int Screen[] = { 800, 600 };

bool Init() {
	if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
		return false;

	if(SDL_SetVideoMode(800, 600, 32, SDL_OPENGL) == NULL)
		return false;

	if(InitOGL() == false)
		return false;

	SDL_WM_SetCaption("Gurr", NULL);

	return true;
}

bool InitOGL() {
	glEnable(GL_DEPTH);
	glClearColor(1.0f, 1.0f, 1.0f, 1.0f);

	glViewport(0, 0, Screen[0], Screen[1]);
	glMatrixMode(GL_PROJECTION);

	glLoadIdentity();
	gluPerspective(75, Screen[0]/Screen[1], 1, 30);
	glMatrixMode(GL_MODELVIEW);

	if(glGetError() != GL_NO_ERROR)
		return false;

	return true;
}

void Exit() {
	SDL_Quit();
}

int main(int argc, char** argv) {
	if(Init() == false)
		cout << "Unable to initialize SDL or OGL" << endl;

	bool Quit = false;
	while(Quit == false) {
		while(SDL_PollEvent(&event)) {
			if(event.type == SDL_QUIT)
				Quit = true;
		}

		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		glColor3f(1.0f, 0.0f, 0.0f);
		glBegin(GL_LINE);
			glVertex3f(1.0f, 0.0f, 0.0f);
			glVertex3f(2.0f, 0.0f, 0.0f);
		glEnd();
		SDL_GL_SwapBuffers();
	}

	Exit();
	return 0;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by MrPickle
Do I have to reset the viewport and perspective when using SDL with OGL, if so, how do I know when the window's resized?

Yes, you also have to (annoyingly) reload your textures when the screen resizes.

SDL only allows the window to be resized by the user if you pass SDL_SetVideoMode a flag of SDL_RESIZABLE. If you resize the window yourself, you should know when to reset the perspective, but if the user is resizing it, you'll receive a SDL_ResizeEvent.
if(event.type == SDL_VIDEORESIZE)
createMyNewWindow(event.resize.w, event.resize.h);


When you get SDL_VIDEORESIZE, you need to recreate the window with SDL_SetVideoMode, and you also need to change the OpenGL viewport, and the perspective, as well as reload your textures.

Quote:
How do you set the window caption? I tried SDL_WM_SetCaption() but that didn't work.

SDL_WM_SetCaption() should work.

Share this post


Link to post
Share on other sites
The first problem I found is that you try to enable "GL_DEPTH" which gives an invalid enum error. You should instead enable GL_DEPTH_TEST.

You should also set up double buffering by calling SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); before calling SDL_SetVideoMode.

After that, the initialization passes and the caption is created, but nothing is drawn. It looks like you are drawing well outside of the view frustrum.

Share this post


Link to post
Share on other sites

This topic is 3378 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this