Jump to content
  • Advertisement
Sign in to follow this  
MylesEvans

OpenGL I'm having a problem using Opengl and SDL together. Help please.

This topic is 2153 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 able to draw to the screen and it shows up in the window. The problem is that Opengl will only draw to a window of about 1.0Wx1.0H. I also can't get it to change origins. I want the origin to be in the top left corner with a screen size that I define. However, no matter what do to the code the origin wont change and it wont draw outside a small 1.0x1.0 size area in the lower left part of screen.

Now when I full screen the window the drawing are fits the screen (instead of being just in the lower left corner), but the size is still 1.0x1.0 and not what I define. Also the origin is still in the middle despite what I want. Here is my code. Its just the Lazy Foo tutorials.

[source lang="cpp"]
#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.0;
const int SCREEN_HIEGHT =480.0;
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(640.0,480.0,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()
{
glViewport(0,0,640,480);
//set clear color
glClearColor(1.0,0.0,0.0,0.0);
//set projection
glMatrixMode(GL_PROJECTION);
glClear(GL_COLOR_BUFFER_BIT);
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()
{

glLoadIdentity();

glTranslatef(0.0f,0.0f,0.0f);
glBegin(GL_QUADS);
//Set color
glColor4f(1.0,0.0,0.0,1.0);



glVertex3f(-1.0f,1.0f,0.0f);
glVertex3f(1.0f,1.0f,0.0f);
glVertex3f(1.0f,1.0f,0.0f);
glVertex3f(-1.0f,-1.0f,0.0f);


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( "Game Window", 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( "Iconified", NULL );
}
else
{
SDL_WM_SetCaption( "Game Window", 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( "Keyboard focus lost", NULL );
}
else
{
SDL_WM_SetCaption( "Game Window", 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( "Mouse Focus Lost", NULL );
}
else
{
SDL_WM_SetCaption( "Game Window", 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;
}
}

else if( ( event.type == SDL_KEYDOWN ) && ( event.key.keysym.sym == SDLK_RETURN ) )
{
//Turn fullscreen on/off
toggle_fullscreen();
}
}

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
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
Advertisement
glOrtho(0,SCREEN_WIDTH,SCREEN_HIEGHT,0,-1,1);

that line sets up your projection:

the parameters are:

left
right
top
bottom
near
far

so you want to swap the top and bottom parameters to invert the y axis.

If you're only able to draw to a 1x1 unit area you might be calling glLoadIdentity() on the projection matrix by mistake somewhere. (doing it before glOrtho should be fine, but if you call it afterwards it will reset your projection matrix) Edited by SimonForsman

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!