Archived

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

Problems with SDL

This topic is 5172 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't seem to get SDL initialized properly to use OpenGL. The worst thing is that I don't get any errors back from either OpenGL or SDL, and i've been sitting here staring at my code for a few hours, and trying to find the problems. heres the code, not very big I hope, just split into 3 files for simplicity's sake. most of the SDL initialization is in the CSDL.cpp file in the Init method. there are some parts which are commented out and values are hardcoded in their place, I did that because when I used the debugger to step through my code it gave very wierd values for those variables, so i replaced them with numbers.
//main.cpp

#pragma comment(lib,"SDL.lib")
#pragma comment(lib,"SDLmain.lib")
#pragma comment(lib,"opengl32.lib")
#pragma comment(lib,"glu32.lib")

#include "CSDL.h"

void handle_key_down(SDL_keysym *keysym);
void process_events( void );
void Quit( int code );

int main(int argc, char **argv)
{
	CSDL SDL;

	SDL.SetVideoProperties(640,480,32,16);
	SDL.Init();

	while(1)
	{
		process_events();
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glLoadIdentity();

		glBegin(GL_QUADS);
		glVertex3f(-2.0f,2.0f,-2.0f);
		glVertex3f(2.0f,2.0f,-2.0f);
		glVertex3f(2.0f,-2.0f,-2.0f);
		glVertex3f(-2.0f,-2.0f,-2.0f);
		glEnd();

		SDL_GL_SwapBuffers();
	
	}

	
	
	return 0;
}

void handle_key_down( SDL_keysym* keysym )
{

    switch( keysym->sym ) {
    case SDLK_ESCAPE:
		{
        Quit( 0 );
        break;
		}
    default:
        break;
    }

}

void process_events( void )
{

    SDL_Event event;


    while( SDL_PollEvent( &event ) ) {

        switch( event.type ) {
        case SDL_KEYDOWN:
          
            handle_key_down( &event.key.keysym );
            break;
        case SDL_QUIT:
      
            Quit(0);
            break;
        }

    }

}

void Quit( int code )
{
  
    SDL_Quit( );
    exit( code );
}


   
//CSDL.cpp

#include "CSDL.h"



int CSDL::Init()
{
	static SDL_Surface *mainWindow;
	if (SDL_Init(SDL_INIT_VIDEO)<0)
	{ LogErrorSDL();}
	
	
	int flags;

	if (SDLInfo.bitsPerPixel == 16)
	{
		SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
		SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
		SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
		
	}
	else 
	{
		SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
		SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,8);
		SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,8);
		LogErrorSDL();
	}
	
	LogErrorSDL();
	SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE , 16/*SDLInfo.zBufferSize*/);
	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
	SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 0);         
    SDL_GL_SetAttribute( SDL_GL_ACCUM_RED_SIZE, 0);       
	SDL_GL_SetAttribute( SDL_GL_ACCUM_GREEN_SIZE, 0);      
    SDL_GL_SetAttribute( SDL_GL_ACCUM_BLUE_SIZE, 0);
    SDL_GL_SetAttribute( SDL_GL_ACCUM_ALPHA_SIZE, 0);

	flags = SDL_OPENGL| SDL_HWSURFACE | SDL_HWPALETTE;
	mainWindow=SDL_SetVideoMode(640/*SDLInfo.width*/, 480/*SDLInfo.height*/, 32/*SDLInfo.bitsPerPixel*/, flags);
	if( mainWindow== 0)
	{LogErrorSDL();}
	
	InitGL(640/*SDLInfo.width*/, 480/*SDLInfo.height*/);
	LogErrorSDL();
	return 0;
}

void CSDL::LogErrorSDL()
{
	std::ofstream fout;
	fout.open("ErrorLog.txt");
	char * error = SDL_GetError();
	fout.write(error, (int)strlen(error));
	
	fout.flush();
	fout.close();
}

int CSDL::InitGL(int width, int height)
{
	
	glShadeModel(GL_SMOOTH);
	glClearColor(0.0f,0.0f,0.0f,0.0f);
	glViewport(0,0,width,height);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	gluPerspective(45.0f,(float) width/ (float)height,1.0f,150.0f);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	return 0;
}

void CSDL::SetVideoProperties(int windowWidth, int windowHeight, int bpp, int zBuffer) 
{
	this->SDLInfo.width = 640;//windowWidth; 

	this->SDLInfo.height= 480;//windowHeight; 

	this->SDLInfo.bitsPerPixel = 16;//bpp;

	this->SDLInfo.zBufferSize = 16;//zBuffer;

}


   
#include <SDL/SDL.h>
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <iostream>
#include <fstream>
#include <cstring>


class CSDL_INFO
{
public:
	int width;
	int height;
	int bitsPerPixel;
	int zBufferSize;
};

class CSDL
{
public:
	
	int Init();

	void SetVideoProperties(int windowWidth, int windowHeight, int bpp, int zBuffer);
private:
	int InitGL(int width, int height);
	void LogErrorSDL();
	CSDL_INFO SDLInfo;

	

};
[edited by - deathtrap on October 14, 2003 12:04:29 PM]

Share this post


Link to post
Share on other sites
Hmm forgot to say that ..
Console window comes up, normal win32 window comes up, but is just filled with a white color, it is not cleared by OpenGL (glClearColor is set to black). and no vertices/polygons are rendered.

Share this post


Link to post
Share on other sites
oh for fcks sake. If I give you my home address, can you bring a baseball bat and start hitting me with it for a few hours??
I have never felt more stupid than I do now.

Thanks for the help

[edited by - deathtrap on October 14, 2003 12:04:50 PM]

Share this post


Link to post
Share on other sites
he he..

don''t worry, that''s totally normal.. when you stare at the monitor for hours, you can miss some obvious things.. that happens all the time (to me at least).. best thing to do is to take a break for a few minutes.. and think everything in some reverse order.. because, usually answers are simpler than we think..

Share this post


Link to post
Share on other sites