Sign in to follow this  
MylesEvans

I cant get opengl to draw a simple square on he screen. IDK what the problem is

Recommended Posts

I'm using the tutorials on lazy foo. Specifically the ones on creating resizable windows and the one on using opengl with sdl (drawing a simple square). The windows and everything show up fine, but the square thats supposed to be drawn with opengl doesnt show up.

Some other info that may be of importance.
- when I take all the code for the window stuff out and just leave the opengl stuff it still doesnt work. I get this "unhandled exception error"
- If I take out the #include "gl/GL.h" "gl/GLU.h" and pragma stuff and just leave the "SDL_Opengl" header like the tutorial says to do there are a ton of errors. So those are added by me.

I can get it to compile, but like I said. The square doesnt show and I have no idea why. So heres my code:

[source lang="java"]/*This source code copyrighted by Lazy Foo' Productions (2004-2012)
and may not be redestributed without written permission.*/

//The headers
#include "SDL.h"
#include <SDL_opengl.h>
#include <string>
#include <gl/GL.h>
#include <gl/GLU.h>

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

//Screen attributes
const int SCREEN_WIDTH = 640;
const int SCREEN_HIEGHT = 480;
const int SCREEN_BPP = 32;

bool init();
bool init_GL();
void clean_up();
void show();



//The surfaces
SDL_Surface *screen = NULL;


//The event structure
SDL_Event event;

//Our window
class Window
{
private:
//Whether the window is windowed or not
bool windowed;

//Whether the window is fine
bool windowOK;

public:
//Constructor
Window();

//Handle window events
void handle_events();

//Turn fullscreen on/off
void toggle_fullscreen();

//Check if anything's wrong with the window
bool error();
};






bool init()
{
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return false;
}
//create window
if(SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HIEGHT,SCREEN_BPP,SDL_OPENGL)==NULL)
{
return false;
}
//Initialize Opengl
if(init_GL()==false)
{
return false;
}
//Set caption
SDL_WM_SetCaption("Game Window",NULL);

//If eveything loads fine
return true;
}

bool init_GL()
{
//set clear color
glClearColor(0,0,0,0);
//set projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,SCREEN_WIDTH,SCREEN_HIEGHT,0,-1,1);
//Initialize modelview matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

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

return true;
}



void clean_up()
{


//Quit SDL
SDL_Quit();
}



void show()
{

int x=100;
int y=100;

//move to offset
glTranslatef(x,y,0);
//start quads
glBegin(GL_QUADS);
//Set color
glColor4f(1,0,1,0);

//Draw
glVertex3f(0,0,0);
glVertex3f(4,0,0);
glVertex3f(4,3,0);
glVertex3f(0,3,0);

glEnd();
}

Window::Window()
{
//Set up the screen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HIEGHT, SCREEN_BPP, SDL_OPENGL | SDL_RESIZABLE );

//If there's an error
if( screen == NULL )
{
windowOK = false;
return;
}
else
{
windowOK = true;
}

//Set the window caption
SDL_WM_SetCaption( "Window Event Test", NULL );

//Set window flag
windowed = true;
}

void Window::handle_events()
{
//If there's something wrong with the window
if( windowOK == false )
{
return;
}

//If the window resized
if( event.type == SDL_VIDEORESIZE )
{
//Resize the screen
screen = SDL_SetVideoMode( event.resize.w, event.resize.h, SCREEN_BPP, SDL_OPENGL | SDL_RESIZABLE );

//If there's an error
if( screen == NULL )
{
windowOK = false;
return;
}
}
//If enter was pressed
else if( ( event.type == SDL_KEYDOWN ) && ( event.key.keysym.sym == SDLK_RETURN ) )
{
//Turn fullscreen on/off
toggle_fullscreen();
}
//If the window focus changed
else if( event.type == SDL_ACTIVEEVENT )
{
//If the window was iconified or restored
if( event.active.state & SDL_APPACTIVE )
{
//If the application is no longer active
if( event.active.gain == 0 )
{
SDL_WM_SetCaption( "Window Event Test: Iconified", NULL );
}
else
{
SDL_WM_SetCaption( "Window Event Test", NULL );
}
}
//If something happened to the keyboard focus
else if( event.active.state & SDL_APPINPUTFOCUS )
{
//If the application lost keyboard focus
if( event.active.gain == 0 )
{
SDL_WM_SetCaption( "Window Event Test: Keyboard focus lost", NULL );
}
else
{
SDL_WM_SetCaption( "Window Event Test", NULL );
}
}
//If something happened to the mouse focus
else if( event.active.state & SDL_APPMOUSEFOCUS )
{
//If the application lost mouse focus
if( event.active.gain == 0 )
{
SDL_WM_SetCaption( "Window Event Test: Mouse Focus Lost", NULL );
}
else
{
SDL_WM_SetCaption( "Window Event Test", NULL );
}
}
}
//If the window's screen has been altered
else if( event.type == SDL_VIDEOEXPOSE )
{
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
//If there's an error
windowOK = false;
return;
}
}
}

void Window::toggle_fullscreen()
{
//If the screen is windowed
if( windowed == true )
{
//Set the screen to fullscreen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HIEGHT, SCREEN_BPP, SDL_OPENGL| SDL_RESIZABLE | SDL_FULLSCREEN );

//If there's an error
if( screen == NULL )
{
windowOK = false;
return;
}

//Set the window state flag
windowed = false;
}
//If the screen is fullscreen
else if( windowed == false )
{
//Window the screen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HIEGHT, SCREEN_BPP, SDL_OPENGL | SDL_RESIZABLE );

//If there's an error
if( screen == NULL )
{
windowOK = false;
return;
}

//Set the window state flag
windowed = true;
}
}

bool Window::error()
{
return !windowOK;
}

int main( int argc, char* args[] )
{
//Quit flag
bool quit = false;

//Initialize
if( init() == false )
{
return 1;
}

//Create a window
Window myWindow;

//If the window failed
if( myWindow.error() == true )
{
return 1;
}


//While the user hasn't quit
while( quit == false )
{
//While there's events to handle
while( SDL_PollEvent( &event ) )
{
//Handle window events
myWindow.handle_events();
glClear(GL_COLOR_BUFFER_BIT);
show();
SDL_GL_SwapBuffers();

//If escape was pressed
if( ( event.type == SDL_KEYDOWN ) && ( event.key.keysym.sym == SDLK_ESCAPE ) )
{
//Quit the program
quit = true;
}

//If the user has Xed out the window
if( event.type == SDL_QUIT )
{
//Quit the program
quit = true;
}
}

//If the window failed
if( myWindow.error() == true )
{
return 1;
}

//Fill the screen white
SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );


//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
}

//Clean up
clean_up();

return 0;
}
[/source]

Share this post


Link to post
Share on other sites

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