WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: SDL makes screen go dim!  (Read 5468 times)

Offline jackarius86

  • Newbie
  • *
  • Posts: 31
SDL makes screen go dim!
« on: May 18, 2013, 03:20:39 AM »
Have been getting back into some opengl programming lately and first went down the route of setting up with glut and had no problems what so ever.

Ideally I would like to work with SDL but am having problems whenevr i call SDL_SetVideoMode() as it makes the entire screen go dim for some reason, i have to then exit to prompt and startx.

I have trawled the internet for this problem, have TRIED to ask in the SDL forum but I dont think i have permission to post there or something like that..

Can anyone please help me?

Thanks.

Online Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11623
Re: SDL makes screen go dim!
« Reply #1 on: May 18, 2013, 05:23:51 PM »
Hi jackarius86
What values are you passing to SDL_SetVideoMode()? Are using 0 for bpp or trying to force it to some other value?
Is it returning a valid value or NULL? Did you try single stepping with gdb to make sure the SDL_SetVideoMode()
instruction is causing it and not some later instruction?

Offline jackarius86

  • Newbie
  • *
  • Posts: 31
Re: SDL makes screen go dim!
« Reply #2 on: May 19, 2013, 11:14:40 AM »
Hi rich,

Im not at the computer now but off the top of my head

SDL_SetVideoMode(400, 400, 0 | SDL_Opengl)

other values such as 8, 24, 32 have been tried to the same effect.

i didnt use gdb i used getchar() between function calls so i could tell it was this particular call.

The program compiles and runs as expected with the added side effect of a dim screen on all workspaces which wont go away not even when i end the program. 

to get the screen back to full brightness i have to exit to prompt and then startx which is not ideal.

It also maybe worth me mentioning that this work is being done on a laptop.

Online Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11623
Re: SDL makes screen go dim!
« Reply #3 on: May 19, 2013, 01:57:49 PM »
Hi jackarius86
Quote
Im not at the computer now but off the top of my head
SDL_SetVideoMode(400, 400, 0 | SDL_Opengl)
Well that's obviously not right, SDL_SetVideoMode takes four parameters. Maybe you meant:
Code: [Select]
SDL_SetVideoMode(400, 400, 0, SDL_Opengl)You might want to take a closer look at the events leading up to that instruction. This page:
http://www.libsdl.org/docs/html/sdlsetvideomode.html
states:
Quote
SDL_OPENGL   Create an OpenGL rendering context. You should have previously set OpenGL video attributes with SDL_GL_SetAttribute.
If you haven't already read it, this might be worth taking a look at:
http://www.libsdl.org/docs/html/guidevideoopengl.html

Offline jackarius86

  • Newbie
  • *
  • Posts: 31
Re: SDL makes screen go dim!
« Reply #4 on: May 19, 2013, 02:53:02 PM »
this is the code i am using, mostly cut from an example:

Code: [Select]
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
#include <iostream>

using namespace std;

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

//The frame rate
const int FRAMES_PER_SECOND = 1;

//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;
    }

//Fault finding
cout<<"1";
getchar();
cout<<"\n";

SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
    SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
    SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
    SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

//Fault finding
cout<<"2";
getchar();
cout<<"\n";


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

//Fault finding
cout<<"3";
getchar();
cout<<"\n";


    //Enable unicode
    SDL_EnableUNICODE( SDL_TRUE );

//Fault finding
cout<<"4";
getchar();
cout<<"\n";


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

//Fault finding
cout<<"5";
getchar();
cout<<"\n";


    //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;
}

Online Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11623
Re: SDL makes screen go dim!
« Reply #5 on: May 20, 2013, 01:59:00 AM »
Hi jackarius86
Does it work any better using:
Code: [Select]
    SDL_GL_SetAttribute(SDL_GL_RED_SIZE,            8);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,          8);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,           8);
    SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE,          8);
 
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,          16);
    SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE,            32);
 
    SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE,        8);
    SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE,    8);
    SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE,        8);
    SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE,    8);
 
    if((Surf_Display = SDL_SetVideoMode(640, 480, 32, SDL_OPENGL)) == NULL)
        return false;

Offline jackarius86

  • Newbie
  • *
  • Posts: 31
Re: SDL makes screen go dim!
« Reply #6 on: May 25, 2013, 04:37:03 PM »
Hi Rich,

I tried that but got the same results :(

I also posted in libsdl.org about this problem but still havent had any replies :(   -  http://forums.libsdl.org/viewtopic.php?t=9085

Looks like i will have to stick to glut for the meantime :(

Online Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11623
Re: SDL makes screen go dim!
« Reply #7 on: May 25, 2013, 05:13:36 PM »
Hi jackarius86
Some laptops dim the screen when they think they are running off of the batteries. If you disconnect and reconnect
the supply, is the brightness restored?

Offline jackarius86

  • Newbie
  • *
  • Posts: 31
Re: SDL makes screen go dim!
« Reply #8 on: May 27, 2013, 06:02:29 PM »
Hi rich i have heard similar theories to explain this behaviour.. unfortunately i havent got a battery for my laptop so i couldnt possibly test it!

I think im at a dead end with it and will use sfml instead as it seems to work fine!

thanks for your input though :-)

Offline jackarius86

  • Newbie
  • *
  • Posts: 31
Re: SDL makes screen go dim!
« Reply #9 on: June 07, 2013, 05:28:40 PM »
Just for reference:

I have just discovered that after using the Ctrl-Alt F1 shortcut which takes you back to the original prompt (or alternatively Ctrl-Alt F3) ; and then returning back to the window manager screen, the screen turns dim and seems to be identical to the earlier reported fault.

Does anyone have a good idea about what might be causing this? Thankyou :)

Offline curaga

  • Administrator
  • Hero Member
  • *****
  • Posts: 11044
Re: SDL makes screen go dim!
« Reply #10 on: June 08, 2013, 07:07:07 AM »
Buggy video driver then. What X server are you using?
The only barriers that can stop you are the ones you create yourself.

Offline jackarius86

  • Newbie
  • *
  • Posts: 31
Re: SDL makes screen go dim!
« Reply #11 on: June 19, 2013, 06:08:34 PM »
I'm using Xorg-7.6