Fullscreen in SDL OpenGL?

Started by
12 comments, last by KaiserJohan 13 years ago
Hello,

I've been doing a SDL openGL tutorial to show a simple triangle in fullscreen mode. The problem is, while the game is fullscreen and the background is black and all, when I try to draw triangles they 'clip' if they go otuside the 800*600 viewport I have defined (I have 1280*800 on my laptop). So the game should change the reoslution while playing to 800*600.

Here's what I've done - I know its alot :(

#include "cApp.h"

//
// constructor
//
cApp::cApp() {
running = true;
mainSurface = NULL;
}

//
// destructor
//
cApp::~cApp() {
}

//
// toggls fullscreen
//
void cApp::toggleFullscreen() {
if(!SDL_WM_ToggleFullScreen(mainSurface))
onExit();
}

//
// creates the main window
//
void cApp::createWindow(int height, int width, int flags) {
mainSurface = SDL_SetVideoMode(width,height,SCREEN_DEPTH,flags);
if (!mainSurface)
onExit();

}

//
// initialises opengl
//
void cApp::initOpenGL() {
glViewport(0,0,SCREEN_WIDTH,SCREEN_HEIGHT);

glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity();

// FOV // Ratio
gluPerspective(45.0f,(GLfloat)SCREEN_WIDTH/(GLfloat)SCREEN_HEIGHT, 1 ,150.0f);


glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity();
}

//
// renders a scene
//
void cApp::renderScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity();

// Position View Up Vector
gluLookAt(0, 0, 6, 0, 0, 0, 0, 1, 0);

glBegin (GL_TRIANGLES); // This is our BEGIN to draw
glVertex3f(0, 1, 0); // Here is the top point of the triangle
glVertex3f(-1, 0, 0); glVertex3f(1, 0, 0); // Here are the left and right points of the triangle
glEnd();

SDL_GL_SwapBuffers();
}

//
// handles key-down event
//
void cApp::onKeyDown(SDL_keysym* keysym) {
switch (keysym->sym) {
case SDLK_ESCAPE:
onExit();

default:
break;
}

}

//
// Cleans up resources
//
void cApp::onCleanup() {
SDL_Quit();
}

//
// Starts terminating game
//
void cApp::onExit() {
running = false;
}

//
// render graphics
//
void cApp::onRender() {
renderScene();
}

//
// game logics
//
void cApp::onLoop() {

}

//
// event handling
//
void cApp::onEvent(SDL_Event* Event) {
switch (Event->type) {
case SDL_KEYDOWN:
onKeyDown(&Event->key.keysym);
break;

default:
break;

}
}

//
// init
//
bool cApp::onInit() {

if(SDL_Init( SDL_INIT_VIDEO ) < 0)
return false;

// create main window
createWindow(SCREEN_HEIGHT,SCREEN_WIDTH,SDL_HWSURFACE | SDL_GL_DOUBLEBUFFER | SDL_OPENGL | SDL_FULLSCREEN);

// init opengl
initOpenGL();

return true;
}


//
// Entry point & main loop
//
int cApp::onExecute() {
bool res;
if (!( res = onInit())) {
onCleanup();
return -1;
}

SDL_Event m_event;

while (running) {
while(SDL_PollEvent(&m_event)) {
onEvent(&m_event);
}
onLoop();
onRender();
}

onCleanup();

return 0;
}
Advertisement
When the window becomes fullscreen, don't forget to call glViewport and tell it the new dimensions.
I am, in the initOpenGL function.
I SDL_SetVideomode for 800*600 fullscreen and then later glViewport on 800*600 (which should be the new resolution ingame?)
Isn't your problem that the fullscreen window is larger than 800x600? I certainly don't know of any monitors that are that size.

Your viewport's dimensions should be equal to the number of pixels the window occupies. So, i make a 600x600 window for my game. When i go fullscreen, i need to call glViewport( 0, 0, 1440, 900 ) to make it match my screen's resolution. When i go back, i need to call glViewport( 0, 0, 600, 600 ) again.

Isn't your problem that the fullscreen window is larger than 800x600? I certainly don't know of any monitors that are that size.

Your viewport's dimensions should be equal to the number of pixels the window occupies. So, i make a 600x600 window for my game. When i go fullscreen, i need to call glViewport( 0, 0, 1440, 900 ) to make it match my screen's resolution. When i go back, i need to call glViewport( 0, 0, 600, 600 ) again.


I'm not sure I follow :P

What is the point of specifying 800*600 for my game window if the fullscreen covers the whole dekstop resolution anyway..? I want the 800*600 to fill the entire fullscreen when I'm playing the game.
Like, when you run an old game in 800*600 the game changes your resolution while playing to match the game so you don't have any black edges.
What are SCREEN_WIDTH and SCREEN_HEIGHT defined as? Are they defined as 800 and 600?

If so, keep in mind that when switching to a fullscreen mode, you'll first want to check and make sure that mode is actually supported by the hardware (SDL provides functionality to facilitate this). Also keep in mind that even if the mode is supported, if the aspect ratio is different than that of the monitor, the window might end up being stretched or pillar/letter-boxed (depending).

What are SCREEN_WIDTH and SCREEN_HEIGHT defined as? Are they defined as 800 and 600?

If so, keep in mind that when switching to a fullscreen mode, you'll first want to check and make sure that mode is actually supported by the hardware (SDL provides functionality to facilitate this). Also keep in mind that even if the mode is supported, if the aspect ratio is different than that of the monitor, the window might end up being stretched or pillar/letter-boxed (depending).


Yeah, its 800 and 600. It's the lowest resolution my laptop supports. Dosn't that mean it's supported by the hardware?

Yeah, its 800 and 600. It's the lowest resolution my laptop supports. Dosn't that mean it's supported by the hardware?

If it shows up in your display control panel and if SDL reports it as available, then I'd say it's supported. However, IMX at least, different monitors handle resolutions that don't match the native aspect ratio in different ways. Some may stretch the resolution to fit, while others may pillar- or letter-box it.

The problem you're having is that it's pillar-boxing, but you don't want that, right? If so, the alternative would be a horizontally stretched image. Is that what you're wanting?

(Disclaimer: My comments about how various resolutions are handled is based on what I remember from trying out some different resolutions on widescreen monitors, but I may not have my information on that exactly right.)
My laptop supports, havn't checked if SDL do it (how do you do it?)

I'm not sure of the term pillar-boxing - basically what happens it switches to fullscreen and renders it all black, yet openGL can only draw on a 800*600 area.

Doing openGL without SDL, I know there's something like ChangeDisplaySettings(). I guess it's that effect I want.


(It has the same effect on my stationary too which has a 4:3 monitor) - something must be wrong with my code

My laptop supports, havn't checked if SDL do it (how do you do it?)

I'm not looking at the docs right now, but in both SDL 1.2.x and 1.3 there are functions that will return a list of supported video modes.

Doing openGL without SDL, I know there's something like ChangeDisplaySettings(). I guess it's that effect I want.[/quote]
The OpenGL API itself doesn't expose that sort of thing; ChangeDisplaySettings() is part of the Windows API and doesn't have anything in particular to do with OpenGL.

In any case, you *might* be able to get different results going through a different windowing API, but I wouldn't necessarily count on it. I'm not really sure what's causing the behavior you're seeing, but it sounds like it might just be how your monitor handles the requested resolution. (Or maybe not - I'm really not sure, to be honest.)

Here's something you could try. Get a hold of a game that uses a fixed 800x600 resolution (I think World of Goo does), run it on your computer, and see what happens. If it does the same thing that your application does, that would suggest that it's simply how the hardware handles the request. (You could also try a game made with the Unity engine, as that would likely have an 800x600 option as well.)

Sorry I don't have a better answer; maybe someone else will be able to help though.

This topic is closed to new replies.

Advertisement