stk-code_catmod/src/sdldrv.cpp
thebohemian 059d9037a7 - separate input map implementation
- make mouse axes work like analog axes
 - enable display menu for non-Win32 platforms


git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/trunk/supertuxkart@1272 178a84e3-b1eb-0310-8ba1-8eac791a3b58
2007-10-03 11:23:07 +00:00

256 lines
8.0 KiB
C++
Executable File

// $Id: plibdrv.cpp 757 2006-09-11 22:27:39Z hiker $
//
// SuperTuxKart - a fun racing game with go-kart
// Copyright (C) 2004 Steve Baker <sjbaker1@airmail.net>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include <SDL/SDL.h>
#include <plib/ssg.h>
#include "user_config.hpp"
#include "sdldrv.hpp"
#include "material_manager.hpp"
#include "kart_properties_manager.hpp"
#include "game_manager.hpp"
#include "herring_manager.hpp"
#include "collectable_manager.hpp"
#include "attachment_manager.hpp"
#include "projectile_manager.hpp"
#include "loader.hpp"
#include "gui/menu_manager.hpp"
#include "player.hpp"
#include "gui/font.hpp"
SDL_Surface *mainSurface;
long flags;
SDL_Joystick **sticks;
bool pointerVisible = true;
#define DEADZONE_MOUSE 1
#define DEADZONE_JOYSTICK 1000
#define MULTIPLIER_MOUSE 750
int mouseValX = 0;
int mouseValY = 0;
//-----------------------------------------------------------------------------
void drv_init()
{
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_TIMER);
flags = SDL_OPENGL | SDL_HWSURFACE;
if(user_config->m_fullscreen)
flags |= SDL_FULLSCREEN;
mainSurface = SDL_SetVideoMode(user_config->m_width, user_config->m_height, 0,
flags);
SDL_JoystickEventState(SDL_ENABLE);
const int NUM_STICKS = SDL_NumJoysticks();
sticks = (SDL_Joystick **) malloc(sizeof(SDL_Joystick *) * NUM_STICKS);
for (int i=0;i<NUM_STICKS;i++)
sticks[i] = SDL_JoystickOpen(i);
SDL_WM_SetCaption("SuperTuxKart", NULL);
ssgInit () ;
}
//-----------------------------------------------------------------------------
void
drv_showPointer()
{
pointerVisible = true;
SDL_ShowCursor(SDL_ENABLE);
}
//-----------------------------------------------------------------------------
void
drv_hidePointer()
{
pointerVisible = false;
SDL_ShowCursor(SDL_DISABLE);
}
//-----------------------------------------------------------------------------
void drv_toggleFullscreen(int resetTextures)
{
user_config->m_fullscreen = !user_config->m_fullscreen;
flags = SDL_OPENGL | SDL_HWSURFACE;
if(user_config->m_fullscreen)
{
flags |= SDL_FULLSCREEN;
if(menu_manager->isSomewhereOnStack(MENUID_RACE))
drv_showPointer();
}
else if(menu_manager->isSomewhereOnStack(MENUID_RACE))
drv_hidePointer();
SDL_FreeSurface(mainSurface);
mainSurface = SDL_SetVideoMode(user_config->m_width, user_config->m_height, 0, flags);
#if defined(WIN32) || defined(__APPLE__)
if(resetTextures)
{
// Clear plib internal texture cache
loader->endLoad();
// Windows needs to reload all textures, display lists, ... which means
// that all models have to be reloaded. So first, free all textures,
// models, then reload the textures from materials.dat, then reload
// all models, textures etc.
// startScreen -> removeTextures();
attachment_manager -> removeTextures();
projectile_manager -> removeTextures();
herring_manager -> removeTextures();
kart_properties_manager -> removeTextures();
collectable_manager -> removeTextures();
material_manager->reInit();
collectable_manager -> loadCollectables();
kart_properties_manager -> loadKartData();
herring_manager -> loadDefaultHerrings();
projectile_manager -> loadData();
attachment_manager -> loadModels();
// startScreen -> installMaterial();
//FIXME: the font reinit funcs should be inside the font class
//Reinit fonts
delete_fonts();
init_fonts();
}
#endif
}
//-----------------------------------------------------------------------------
void drv_deinit()
{
const int NUM_STICKS = SDL_NumJoysticks();
for (int i=0;i<NUM_STICKS;i++)
SDL_JoystickClose(sticks[i]);
free(sticks);
SDL_FreeSurface(mainSurface);
SDL_Quit();
}
//-----------------------------------------------------------------------------
void input(InputType type, int id0, int id1, int id2, int value)
{
BaseGUI* menu= menu_manager->getCurrentMenu();
if (menu != NULL)
menu->input(type, id0, id1, id2, value);
}
//-----------------------------------------------------------------------------
void drv_loop()
{
SDL_Event ev;
while(SDL_PollEvent(&ev))
{
switch(ev.type)
{
case SDL_QUIT:
game_manager->abort();
break;
case SDL_KEYUP:
input(IT_KEYBOARD, ev.key.keysym.sym, ev.key.keysym.unicode, 0, 0);
break;
case SDL_KEYDOWN:
input(IT_KEYBOARD, ev.key.keysym.sym, ev.key.keysym.unicode, 0, 32768);
break;
case SDL_MOUSEMOTION:
if (pointerVisible)
{
BaseGUI* menu= menu_manager->getCurrentMenu();
if (menu != NULL)
menu->inputPointer(ev.motion.x, mainSurface->h - ev.motion.y);
// Reset parameters for relative mouse handling to make sure we are
// in a valid state when returning to relative mouse mode
mouseValX = mouseValY = 0;
}
else
{
mouseValX = std::max(-32768,
std::min(32768, mouseValX
+ ev.motion.xrel
* MULTIPLIER_MOUSE));
mouseValY = std::max(-32768,
std::min(32768, mouseValY
+ ev.motion.yrel
* MULTIPLIER_MOUSE));
}
break;
case SDL_MOUSEBUTTONUP:
input(IT_MOUSEBUTTON, ev.button.button, 0, 0, 0);
break;
case SDL_MOUSEBUTTONDOWN:
input(IT_MOUSEBUTTON, ev.button.button, 0, 0, 32768);
break;
case SDL_JOYAXISMOTION:
if(ev.jaxis.value <= -DEADZONE_JOYSTICK)
input(IT_STICKMOTION, ev.jaxis.which, ev.jaxis.axis, AD_NEGATIVE, -ev.jaxis.value);
else if(ev.jaxis.value <= 0)
input(IT_STICKMOTION, ev.jaxis.which, ev.jaxis.axis, AD_NEGATIVE, 0);
if(ev.jaxis.value >= DEADZONE_JOYSTICK)
input(IT_STICKMOTION, ev.jaxis.which, ev.jaxis.axis, AD_POSITIVE, ev.jaxis.value);
else if(ev.jaxis.value >= 0)
input(IT_STICKMOTION, ev.jaxis.which, ev.jaxis.axis, AD_POSITIVE, 0);
break;
case SDL_JOYBUTTONUP:
input(IT_STICKBUTTON, ev.jbutton.which, ev.jbutton.button, 0,
0);
break;
case SDL_JOYBUTTONDOWN:
input(IT_STICKBUTTON, ev.jbutton.which, ev.jbutton.button, 0,
32768);
break;
} // switch
} // while (SDL_PollEvent())
// Makes mouse behave like an analog axis.
if (mouseValX <= DEADZONE_MOUSE)
input(IT_MOUSEMOTION, 0, AD_NEGATIVE, 0, -mouseValX);
else if (mouseValX >= DEADZONE_MOUSE)
input(IT_MOUSEMOTION, 0, AD_POSITIVE, 0, mouseValX);
if (mouseValY <= DEADZONE_MOUSE)
input(IT_MOUSEMOTION, 1, AD_NEGATIVE, 0, -mouseValY);
else if (mouseValY >= DEADZONE_MOUSE)
input(IT_MOUSEMOTION, 1, AD_POSITIVE, 0, mouseValY);
}