[SDL] SDL window with fixed aspect ratio

MortenH mortenholmgaardmh at hotmail.com
Sun Dec 5 10:14:17 PST 2010


I’ve now found the solution myself.
It only suits window but that’s my primarily need.
I’ve used the VM_RESIZING message.

It still needs full screen handling but that will come..

Code:

#include <Windows.h>

const int SCREEN_W_RATIO=5;
const int SCREEN_H_RATIO=4;

int eventFilter(const SDL_Event *e)
{
	if( e->type == SDL_SYSWMEVENT)
	{
		switch(e->syswm.msg->msg)
		{ 
			case WM_SIZING:
			{
				RECT windowRect;
				RECT * lParamRectPtr = ((RECT*)e->syswm.msg->lParam);
				GetWindowRect(e->syswm.msg->hwnd,&windowRect);
				int diff_w=(lParamRectPtr->right-lParamRectPtr->left)-(windowRect.right-windowRect.left), diff_h=(lParamRectPtr->bottom-lParamRectPtr->top)-(windowRect.bottom-windowRect.top);

				if ((e->syswm.msg->wParam==WMSZ_BOTTOMLEFT) || (e->syswm.msg->wParam==WMSZ_BOTTOMRIGHT))			
					e->syswm.msg->wParam=WMSZ_BOTTOM;		
				else if ((e->syswm.msg->wParam==WMSZ_TOPLEFT) || (e->syswm.msg->wParam==WMSZ_TOPRIGHT))			
					e->syswm.msg->wParam=WMSZ_TOP;

				if(e->syswm.msg->wParam==WMSZ_RIGHT || e->syswm.msg->wParam==WMSZ_LEFT)
				{
					if(diff_w > SCREEN_W_RATIO/2)
					{
						int resizeFactor = diff_w/SCREEN_W_RATIO + ((diff_w%SCREEN_W_RATIO)>SCREEN_W_RATIO/2 ? 1 : 0);
						screen_w += SCREEN_W_RATIO*resizeFactor;
						screen_h += SCREEN_H_RATIO*resizeFactor;
						calcZoom(screen_w);

						(*lParamRectPtr) = windowRect;
						if(e->syswm.msg->wParam==WMSZ_RIGHT)
							lParamRectPtr->right += SCREEN_W_RATIO*resizeFactor;
						else
							lParamRectPtr->left += SCREEN_W_RATIO*resizeFactor;
						lParamRectPtr->bottom += SCREEN_H_RATIO*resizeFactor;

					}
					else if(diff_w < -SCREEN_W_RATIO/2)
					{
						int resizeFactor = (-diff_w)/SCREEN_W_RATIO + (((-diff_w)%SCREEN_W_RATIO)>SCREEN_W_RATIO/2 ? 1 : 0);
						screen_w -= SCREEN_W_RATIO*resizeFactor;
						screen_h -= SCREEN_H_RATIO*resizeFactor;
						calcZoom(screen_w);

						(*lParamRectPtr) = windowRect;
						if(e->syswm.msg->wParam==WMSZ_RIGHT)
							lParamRectPtr->right -= SCREEN_W_RATIO*resizeFactor;
						else
							lParamRectPtr->left -= SCREEN_W_RATIO*resizeFactor;
						lParamRectPtr->bottom -= SCREEN_H_RATIO*resizeFactor;
					}
					else
						(*lParamRectPtr) = windowRect;
				}
				else if(e->syswm.msg->wParam==WMSZ_BOTTOM || e->syswm.msg->wParam==WMSZ_TOP)
				{
					if(diff_h > SCREEN_H_RATIO/2)
					{
						int resizeFactor = diff_h/SCREEN_H_RATIO + ((diff_h%SCREEN_H_RATIO)>SCREEN_H_RATIO/2 ? 1 : 0);
						screen_w += SCREEN_W_RATIO*resizeFactor;
						screen_h += SCREEN_H_RATIO*resizeFactor;
						calcZoom(screen_w);

						(*lParamRectPtr) = windowRect;
						if(e->syswm.msg->wParam==WMSZ_BOTTOM)
							lParamRectPtr->bottom += SCREEN_W_RATIO*resizeFactor;
						else
							lParamRectPtr->top += SCREEN_W_RATIO*resizeFactor;
						lParamRectPtr->right += SCREEN_H_RATIO*resizeFactor;
					}
					else if(diff_h < -SCREEN_H_RATIO/2)
					{
						int resizeFactor = (-diff_h)/SCREEN_H_RATIO + (((-diff_h)%SCREEN_H_RATIO)>SCREEN_H_RATIO/2 ? 1 : 0);
						screen_w -= SCREEN_W_RATIO*resizeFactor;
						screen_h -= SCREEN_H_RATIO*resizeFactor;
						calcZoom(screen_w);

						(*lParamRectPtr) = windowRect;
						if(e->syswm.msg->wParam==WMSZ_BOTTOM)
							lParamRectPtr->bottom -= SCREEN_W_RATIO*resizeFactor;
						else
							lParamRectPtr->top -= SCREEN_W_RATIO*resizeFactor;
						lParamRectPtr->right -= SCREEN_H_RATIO*resizeFactor;
					}
					else
						(*lParamRectPtr) = windowRect;
				}
				
			break;
			}
		}
	}

	if( e->type == SDL_VIDEORESIZE )
    {
		SDL_SetVideoMode(screen_w,screen_h,SCREEN_BPP, SDL_SWSURFACE | SDL_RESIZABLE);
		staticShow();
	}
	return 1;
}






-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20101205/f24b0264/attachment-0008.htm>


More information about the SDL mailing list