Advertisement

[SDL] SDL_SetVideoMode with OpenGL and fullscreen

Started by June 28, 2011 03:37 PM
10 comments, last by EvilTesla-RG 13 years, 4 months ago

I don't think that's the problem.

As my larger program does completely reload OpenGL and all the textures, and it has the same problems.


Furthermore, it works if I run it in windowed mode. It only doesn't work if it is in fullscreen.


AFAIK SDL_SetVideoMode doesn't create a new rendering context if you're in windowed mode in the latest SDL 1.2 build (it shouldn't be necessary as the actual resolution doesn't change) but it still does it when switching to or from a fullscreen mode.

I'm fairly sure you are forgetting to re-initialize something in your larger program aswell.
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
Actully, you're probably right.

I just re-wrote my previous program to put all the OpenGL stuff in a differnt function, and now it works.



Great, now I have to find what isn't reloading in my big program. Will be a pain, as it isn't a small program.



Thanks

Re-written code:


//#include <iostream>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_opengl.h>
#include <gl/gl.h>
#include <iostream>

#include "OGL_setup.h"

using namespace std;

#if SDL_BYTEORDER == SDL_BIG_ENDIAN
Uint32 rmask = 0xff000000;
Uint32 gmask = 0x00ff0000;
Uint32 bmask = 0x0000ff00;
Uint32 amask = 0x000000ff;
#else
Uint32 rmask = 0x000000ff;
Uint32 gmask = 0x0000ff00;
Uint32 bmask = 0x00ff0000;
Uint32 amask = 0xff000000;
#endif

GLuint setOpenGL()
{
//enable openGL
setupExtensions();

glViewport(0, 0, 640, 480);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, 640, 480, 0.0f, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();


//load white square texture
SDL_Surface* surf=SDL_CreateRGBSurface(SDL_SRCALPHA, 15, 18, 32, rmask, gmask, bmask, amask);
Uint32 color=SDL_MapRGBA(surf->format,255,255,255,255);
SDL_FillRect(surf, NULL, color);

GLuint texture;
glGenTextures( 1, &texture );
glBindTexture( GL_TEXTURE_2D, texture );

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surf->w, surf->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, surf->pixels);

GLuint GLcalllist=glGenLists(1);
glNewList(GLcalllist, GL_COMPILE);

glBegin( GL_QUADS );
//Bottom-left vertex (corner)
glMultiTexCoord2fARB(GL_TEXTURE0, 0, 0);
glVertex2f( 0.f, 0.f);

//Bottom-right vertex (corner)
glMultiTexCoord2fARB(GL_TEXTURE0, 1, 0);
glVertex2f( 15, 0.f );

//Top-right vertex (corner)
glMultiTexCoord2fARB(GL_TEXTURE0, 1, 1);
glVertex2f( 15, 18 );

//Top-left vertex (corner)
glMultiTexCoord2fARB(GL_TEXTURE0, 0, 1);
glVertex2f( 0.f, 18 );
glEnd();

glEndList();

return GLcalllist;
}

int main(int argc, char *argv[])
{

//enable SDL
SDL_Init(SDL_INIT_VIDEO);
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 0 );
SDL_SetVideoMode(640, 480, 0, SDL_OPENGL | SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_RESIZABLE | SDL_FULLSCREEN);
//note that if SDL_FULLSCREEN is removed (both here and in event handeling), then this works

//OpenGL
GLuint GLcalllist=setOpenGL();

//Down to buisness
while(true)
{
//handel events
SDL_Event event;
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_KEYDOWN:
if(event.key.keysym.sym==SDLK_ESCAPE)
return(0);
else if(event.key.keysym.sym==SDLK_a) //resize screen
SDL_SetVideoMode(760, 480, 0, SDL_OPENGL | SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_RESIZABLE | SDL_FULLSCREEN);
GLcalllist=setOpenGL();
cout<<"reset"<<endl;
break;
case SDL_QUIT:
return(0);
break;
}
}

//render square at mouse position
int pos[2]={0,0};
SDL_GetMouseState(&pos[0], &pos[1]);
glLoadIdentity();
glTranslatef(pos[0], pos[1], 0);
glCallList(GLcalllist);
SDL_GL_SwapBuffers();
}

return(0);
}




This topic is closed to new replies.

Advertisement