Changing Screen Resolution

Hi

I’m wondering whether there is a way in which i can resize the screen without using XF86, just using xlib. I was trying to get the root window, and resizing that, but it doesnt seem to work all that well. This is what I effectively done:

XResizeWindow(_display, DefaultRootWindow(_display),1024, 768);

But thats so doesnt work. Any suggestions?

What are your goals ? Having a fullscreen window of the choosen size ? Tell more please.

In effect I just want to switch the game between fullscreen and windowed mode. The resolution of the desktop might be 1280x960, and the game in fullscreen mode should have resolution of 800x600. Hence I just want to know how to switch the desktop resolution from within the engine to 800x600. I cant be to hard because I know there is a couple of little apps which come with Gnome to change the resolution, without using XF86. I was just wondering how I can switch the resolution using Xorg, because the targeted systems uses Xorg instead of XFree86 as theyr xserver. (As well as I heard rumours that Xorg is replacing XF86.)

Note: I just have to change the screen resolution, thats all, my engine can do everything but that …

I don’t use the function you stippled. With some Internet searches, you’ll be able to find out good sources examples about how to switch between fullscreen and window mode, as for creating directly a window/fullscreen window.
The code is more complex at first glances, but it’s not so hard to understand.

Here is some code I was able to find on my old working codes. There might not have all things needed, but there’s almost all.

class WinAttr
{
   public:
	
   int x, y;
   uint width, height;
   uint depth;
   bool	fullscreen;
   char	*name;

   XSetWindowAttributes attributes;
	
   int *glx_attr;

   WinAttr()
   {
      glx_attr= new int[11];
		
      glx_attr[0]= GLX_RGBA;
      glx_attr[1]= GLX_DOUBLEBUFFER;
      glx_attr[2]= GLX_RED_SIZE;
      glx_attr[3]= 4;
      glx_attr[4]= GLX_GREEN_SIZE;
      glx_attr[5]= 4;
      glx_attr[6]= GLX_BLUE_SIZE;
      glx_attr[7]= 4;
      glx_attr[8]= GLX_DEPTH_SIZE;
      glx_attr[9]= 16;
      glx_attr[10]= None;
		
      name= new char[14];
      strcpy( name, "Window on GLX");
		
      width= 640;
      height= 480;
      x=0; y=0;
      depth= 24;
      fullscreen= false;
   }
	
   void SetXWAttr( XSetWindowAttributes& a)
   { attributes= a;}
};

class Window
{
   protected:

   Display *display;
   Window window;
   GLXContext context;
   XVisualInfo	*visual;
   int screen;
   XF86VidModeModeInfo	mode_info;
   WinAttr	attributes;

   public:
	
   Window(){}

   bool CreateWindow();
};

bool Window::CreateWindow()
{
   Colormap cmap;
   int dw, dh;
   int i;
   int glx_rel_maj, glx_rel_min;
   int vid_rel_maj, vid_rel_min;
   XF86VidModeModeInfo **modes;
   int mode_num;
   int best_mode= 0;
   Atom wm_delete;
   uint border_dummy;
   Window win_dummy;
		
   display= XOpenDisplay( 0);
   screen= DefaultScreen( display);
		
   XF86VidModeQueryVersion( display, &vid_rel_maj, &vid_rel_min);
   XF86VidModeGetAllModeLines(display, this->screen, &mode_num, &modes);
		
   this->mode_info= *modes[0];
		
   for( i=0; i< mode_num; i++)
      if( ( modes[i]->hdisplay==attributes.width) && ( modes[i]->vdisplay== attributes.height))
         best_mode= i;
				
   this->visual= glXChooseVisual( display, screen, attributes.glx_attr);
   if( !visual)
      return false;

   glXQueryVersion( display, &glx_rel_maj, &glx_rel_min);
		
   this->context= glXCreateContext( display, visual, 0, GL_TRUE);
		
   cmap= XCreateColormap( display, 
   RootWindow( this->display, 
   this->visual->screen), this->visual->visual, None);

   this->attributes.attributes.colormap= cmap;
   this->attributes.attributes.border_pixel= 0;
		
   if( attributes.fullscreen){
      XF86VidModeSwitchToMode( this->display, this->screen, modes[best_mode]);
      XF86VidModeSetViewPort( this->display, this->screen, 0, 0);
			
      dw= modes[best_mode]->hdisplay;
      dh= modes[best_mode]->vdisplay;
      XFree( modes);
      
      attributes.attributes.override_redirect= true;
      attributes.attributes.event_mask= 
      ExposureMask | KeyPressMask | 
      ButtonPressMask | StructureNotifyMask;
			
      this->window= XCreateWindow( this->display, 
         RootWindow( this->display, 
         this->visual->screen), 0, 0, dw, dh, 0, 
         this->visual->depth, InputOutput, 
         this->visual->visual, CWBorderPixel | 
         CWColormap | CWEventMask | 
         CWOverrideRedirect, &attributes.attributes);
				
      XWarpPointer( this->display, None, this->window, 0,0,0,0,0,0);
      XMapRaised( this->display, this->window);
      XGrabKeyboard( this->display, this->window, 
         true, GrabModeAsync, GrabModeAsync, 
         CurrentTime);
      XGrabPointer( this->display, this->window, 
         true, ButtonPressMask, GrabModeAsync, 
         GrabModeAsync,this->window, None, 
         CurrentTime);
   }
   else{
      attributes.attributes.event_mask= 
      ExposureMask | KeyPressMask | 
      ButtonPressMask | StructureNotifyMask;
			
      this->window= XCreateWindow( this->display, 
         RootWindow( this->display, 
         this->visual->screen), 0, 
         0,attributes.width, attributes.height, 0, 
         this->visual->depth, InputOutput,  
         this->visual->visual,CWBorderPixel | 
         CWColormap | CWEventMask | 
         CWOverrideRedirect, &attributes.attributes);

      wm_delete= XInternAtom( this->display, "WM_DELETE_WINDOW", true);
      XSetWMProtocols( this->display, this->window, &wm_delete, 1);
      XSetStandardProperties( this->display, 
         this->window, attributes.name, 
         attributes.name, None, NULL, 0, NULL);
				
      XMapRaised( this->display, this->window);
   }
		
   glXMakeCurrent( this->display, this->window, this->context);
   XGetGeometry( this->display, this->window, 
      &win_dummy, &attributes.x, &attributes.y, 
      &attributes.width, &attributes.height, 
      &border_dummy, &attributes.depth);

   return true;
} 

Again, it’s somewhat an older code, but that should work on recent libs too. Xorg or XFree doesn’t matter (at least for the moment).

That must help you.

Yeh, I know what that code does, and its using XF86 extensions. However the systems I want to use it on doesnt have XF86 as its Xserver, or for that matter doesnt have the XF86 extensions/dev libiraries installed. You would also notice that the code would most likely produce some errors when you switch modes several times during gameplay.

I did however solve the problem earlier today without using any XF86 libiraries or functions. (It should also work on both the newest Xorg and XF86 Xservers)

cheers for trying to help tho.

Not that I currently have any need for that sort of stuff, but:
would you care to share your solution?

Sure, well ya see the bit that say:

XF86VidModeSwitchToMode( this->display, this->screen, modes[best_mode]); XF86VidModeSetViewPort( this->display, this->screen, 0, 0);

That bit is used to switch the video to the current resolution that you want. Then you create a window which is just the same size, witout the border and then you move it to (0,0) where (0,0) is the top,left of the screen, not GL (0,0) which is the bottom,left of the screen.

So all I had to do was to find a function wich can switch the video mode, hence replace:

XF86VidModeSwitchToMode( this->display, this->screen, modes[best_mode]); XF86VidModeSetViewPort( this->display, this->screen, 0, 0);

And then I rember there is a function which was in a bit of an infancy when I was just but a rookie coder, which you could call thought the concole called “xrandr” which Rotate & Resize the screen.

So all have to do was replace the code which say:

XF86VidModeSwitchToMode( this->display, this->screen, modes[best_mode]); XF86VidModeSetViewPort( this->display, this->screen, 0, 0);

with a system call:

system(“xrandr -s 1024x768”);

This might seem dogey, but it works oh so well, and not a single crash or error or problem what so ever. So now what I do is I first search for an Xorg.conf file, if that not found I search for and XF86Config-4 file, to get the supported screen resolutions, and match what window size you want to a close enought resolution. Hence all you do is use sprintf(); to make the resolution string and thats it :> Really very simple when you think about it…

(Oh yeah, rember to save the screen res first before ya change it tho:P)

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.