view data/model/SDLWidget.cpp @ 139:2d883a374b79

Zoom with the mouse wheel, for the SDLWidget.
author benoitrigolleau
date Fri, 09 Nov 2007 13:21:34 +0000
parents 3b2b790e47e5
children e63e8272dcb0
line wrap: on
line source
#include <vector>
#include <cstdlib>
#include <ctime>
#include "SDLWidget.h"
extern int Videow;
extern int Videoh;

#define MIN_WIDTH 320
#define MIN_HEIGHT 240

SDLWidget::SDLWidget()
	:windowInitialized(false), screen(0)
	{
		setAttribute(Qt::WA_PaintOnScreen);
		setAttribute(Qt::WA_NoSystemBackground);
		zoomFactor = 1;
		setMinimumSize( MIN_WIDTH*zoomFactor,MIN_HEIGHT*zoomFactor );

	}

SDLWidget::~SDLWidget()
	{
		SDL_Quit();
	}

void SDLWidget::setZoomFactor( float f )
{
	int w, h;
	
	if( f == zoomFactor )
		return;

	zoomFactor = f;

	w = MIN_WIDTH*zoomFactor;
	h = MIN_HEIGHT*zoomFactor;
	setMinimumSize( w, h );
	emit(sizeChanged(w,h));
	
	/*QWidget *p = dynamic_cast<QWidget*>( parent() );
	if( p ){
		resize( p->width(), p->height() );
	}*/
}

void SDLWidget::wheelEvent( QWheelEvent *event )
{
	float f;

	f = zoomFactor + 0.001*event->delta();
	if( f < 32.0/MIN_WIDTH ){
		f = 32.0/MIN_WIDTH;
	}

	setZoomFactor( f );
}


void SDLWidget::showEvent(QShowEvent *e)
	{
		(void)e;
		
		if(!windowInitialized)
		{
			// it's here the link between SDL and QT
			char windowid[64];
#ifdef Q_WS_WIN
			sprintf(windowid, "SDL_WINDOWID=0x%lx", reinterpret_cast<qlonglong>(winId()));
#elif defined Q_WS_X11
			sprintf(windowid, "SDL_WINDOWID=0x%lx", winId());
#else
			qFatal("uncorrect window ID");
#endif
			SDL_putenv(windowid);

			//SDL_Init(SDL_INIT_VIDEO);
			//screen = SDL_SetVideoMode(width(), height(), 32, SDL_SWSURFACE);
			windowInitialized = true;
		}
	}