Sign in to follow this  

Help with using SDL/Opengl together.

This topic is 1899 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'm going through the lazy foo tutorial (Lesson 36 using Opengl with SDL). Code below.

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

//The headers
#include "SDL.h"
#include "SDL_opengl.h"

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

//The frame rate
const int FRAMES_PER_SECOND = 60;

//Event handler
SDL_Event event;

//Rendering flag
bool renderQuad = true;

//The timer
class Timer
{
private:
//The clock time when the timer started
int startTicks;

//The ticks stored when the timer was paused
int pausedTicks;

//The timer status
bool paused;
bool started;

public:
//Initializes variables
Timer();

//The various clock actions
void start();
void stop();
void pause();
void unpause();

//Gets the timer's time
int get_ticks();

//Checks the status of the timer
bool is_started();
bool is_paused();
};

bool initGL()
{
//Initialize Projection Matrix
glMatrixMode( GL_PROJECTION );
glLoadIdentity();

//Initialize Modelview Matrix
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();

//Initialize clear color
glClearColor( 0.f, 0.f, 0.f, 1.f );

//Check for error
GLenum error = glGetError();
if( error != GL_NO_ERROR )
{
printf( "Error initializing OpenGL! %s\n", gluErrorString( error ) );
return false;
}

return true;
}

bool init()
{
//Initialize SDL
if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
{
return false;
}

//Create Window
if( SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL ) == NULL )
{
return false;
}

//Enable unicode
SDL_EnableUNICODE( SDL_TRUE );

//Initialize OpenGL
if( initGL() == false )
{
return false;
}

//Set caption
SDL_WM_SetCaption( "OpenGL Test", NULL );

return true;
}

void handleKeys( unsigned char key, int x, int y )
{
//Toggle quad
if( key == 'q' )
{
renderQuad = !renderQuad;
}
}

void update()
{

}

void render()
{
//Clear color buffer
glClear( GL_COLOR_BUFFER_BIT );

//Render quad
if( renderQuad == true )
{
glBegin( GL_QUADS );
glVertex2f( -0.5f, -0.5f );
glVertex2f( 0.5f, -0.5f );
glVertex2f( 0.5f, 0.5f );
glVertex2f( -0.5f, 0.5f );
glEnd();
}

//Update screen
SDL_GL_SwapBuffers();
}

void clean_up()
{
//Quit SDL
SDL_Quit();
}

Timer::Timer()
{
//Initialize the variables
startTicks = 0;
pausedTicks = 0;
paused = false;
started = false;
}

void Timer::start()
{
//Start the timer
started = true;

//Unpause the timer
paused = false;

//Get the current clock time
startTicks = SDL_GetTicks();
}

void Timer::stop()
{
//Stop the timer
started = false;

//Unpause the timer
paused = false;
}

void Timer::pause()
{
//If the timer is running and isn't already paused
if( ( started == true ) && ( paused == false ) )
{
//Pause the timer
paused = true;

//Calculate the paused ticks
pausedTicks = SDL_GetTicks() - startTicks;
}
}

void Timer::unpause()
{
//If the timer is paused
if( paused == true )
{
//Unpause the timer
paused = false;

//Reset the starting ticks
startTicks = SDL_GetTicks() - pausedTicks;

//Reset the paused ticks
pausedTicks = 0;
}
}

int Timer::get_ticks()
{
//If the timer is running
if( started == true )
{
//If the timer is paused
if( paused == true )
{
//Return the number of ticks when the timer was paused
return pausedTicks;
}
else
{
//Return the current time minus the start time
return SDL_GetTicks() - startTicks;
}
}

//If the timer isn't running
return 0;
}

bool Timer::is_started()
{
return started;
}

bool Timer::is_paused()
{
return paused;
}

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

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

//The frame rate regulator
Timer fps;

//Wait for user exit
while( quit == false )
{
//Start the frame timer
fps.start();

//While there are events to handle
while( SDL_PollEvent( &event ) )
{
if( event.type == SDL_QUIT )
{
quit = true;
}
else if( event.type == SDL_KEYDOWN )
{
//Handle keypress with current mouse position
int x = 0, y = 0;
SDL_GetMouseState( &x, &y );
handleKeys( event.key.keysym.unicode, x, y );
}
}

//Run frame update
update();

//Render frame
render();

//Cap the frame rate
if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
{
SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
}
}

//Clean up
clean_up();

return 0;
}
[/source]

Produces the following errors when I build.

warning C4244: 'argument' : conversion from 'Uint16' to 'unsigned char', possible loss of data
Draw1.obj : error LNK2019: unresolved external symbol _gluErrorString@4 referenced in function "bool __cdecl initGL(void)" (?initGL@@YA_NXZ)
Draw1.obj : error LNK2019: unresolved external symbol __imp__glGetError@0 referenced in function "bool __cdecl initGL(void)" (?initGL@@YA_NXZ)
Draw1.obj : error LNK2019: unresolved external symbol __imp__glClearColor@16 referenced in function "bool __cdecl initGL(void)" (?initGL@@YA_NXZ)
Draw1.obj : error LNK2019: unresolved external symbol __imp__glLoadIdentity@0 referenced in function "bool __cdecl initGL(void)" (?initGL@@YA_NXZ)
Draw1.obj : error LNK2019: unresolved external symbol __imp__glMatrixMode@4 referenced in function "bool __cdecl initGL(void)" (?initGL@@YA_NXZ)
Draw1.obj : error LNK2019: unresolved external symbol __imp__glEnd@0 referenced in function "void __cdecl render(void)" (?render@@YAXXZ)
Draw1.obj : error LNK2019: unresolved external symbol __imp__glVertex2f@8 referenced in function "void __cdecl render(void)" (?render@@YAXXZ)
Draw1.obj : error LNK2019: unresolved external symbol __imp__glBegin@4 referenced in function "void __cdecl render(void)" (?render@@YAXXZ)
Draw1.obj : error LNK2019: unresolved external symbol __imp__glClear@4 referenced in function "void __cdecl render(void)" (?render@@YAXXZ)
C:\Users\Myles\documents\visual studio 2010\Projects\ProjectIVBirth\Debug\ProjectIVBirth.exe : fatal error LNK1120: 9 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Any help would be greatly appreciated. I just want to get the square to appear.

Share this post


Link to post
Share on other sites

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