Add mouse simulation with joystick for gp2x, wire in changes.

This commit is contained in:
Karol Nowak 2006-09-17 09:07:04 +00:00
parent 8a97634c73
commit 625f1bf82a
5 changed files with 292 additions and 9 deletions

View file

@ -62,6 +62,7 @@ wesnoth_SOURCES = \
game_events.cpp \
gamestatus.cpp \
generic_event.cpp \
gp2x.cpp \
halo.cpp \
help.cpp \
hotkeys.cpp \
@ -433,6 +434,7 @@ noinst_HEADERS = \
playsingle_controller.hpp \
generic_event.hpp \
sdl_ttf/SDL_ttf.h \
gp2x.hpp \
wesconfig.h
#############################################################################

View file

@ -16,6 +16,7 @@
#include "clipboard.hpp"
#include "cursor.hpp"
#include "events.hpp"
#include "gp2x.hpp"
#include "mouse.hpp"
#include "preferences_display.hpp"
#include "sound.hpp"
@ -286,6 +287,13 @@ void pump()
break;
}
#ifdef GP2X
case SDL_JOYBUTTONDOWN:
case SDL_JOYBUTTONUP:
gp2x::handle_joystick(reinterpret_cast<SDL_JoyButtonEvent *>(&event));
break;
#endif
case SDL_MOUSEMOTION: {
//always make sure a cursor is displayed if the
//mouse moves or if the user clicks

View file

@ -27,6 +27,7 @@
#include "game_errors.hpp"
#include "gamestatus.hpp"
#include "gettext.hpp"
#include "gp2x.hpp"
#include "help.hpp"
#include "hotkeys.hpp"
#include "intro.hpp"
@ -108,7 +109,7 @@ private:
game_controller(const game_controller&);
void operator=(const game_controller&);
void read_game_cfg(const preproc_map& defines, config& cfg, bool use_cache);
void read_game_cfg(const preproc_map& defines, config& cfg, bool use_cache, bool force_valid_cache_);
void refresh_game_cfg(bool reset_translations=false);
void upload_campaign(const std::string& campaign, network::connection sock);
@ -136,6 +137,7 @@ private:
bool test_mode_, multiplayer_mode_, no_gui_;
bool use_caching_;
bool force_valid_cache_;
int force_bpp_;
config game_config_;
@ -154,8 +156,8 @@ private:
game_controller::game_controller(int argc, char** argv)
: argc_(argc), arg_(1), argv_(argv), thread_manager(),
test_mode_(false), multiplayer_mode_(false),
no_gui_(false), use_caching_(true), force_bpp_(-1), disp_(NULL),
loaded_game_show_replay_(false)
no_gui_(false), use_caching_(true), force_valid_cache_(false),
force_bpp_(-1), disp_(NULL), loaded_game_show_replay_(false)
{
for(arg_ = 1; arg_ != argc_; ++arg_) {
const std::string val(argv_[arg_]);
@ -167,6 +169,8 @@ game_controller::game_controller(int argc, char** argv)
preferences::set_show_fps(true);
} else if(val == "--nocache") {
use_caching_ = false;
} else if(val == "--validcache") {
force_valid_cache_ = true;
} else if(val == "--resolution" || val == "-r") {
if(arg_+1 != argc_) {
++arg_;
@ -1282,7 +1286,7 @@ void game_controller::show_upload_begging()
}
//this function reads the game configuration, searching for valid cached copies first
void game_controller::read_game_cfg(const preproc_map& defines, config& cfg, bool use_cache)
void game_controller::read_game_cfg(const preproc_map& defines, config& cfg, bool use_cache, bool force_valid_cache_)
{
log_scope("read_game_cfg");
@ -1309,7 +1313,7 @@ void game_controller::read_game_cfg(const preproc_map& defines, config& cfg, boo
file_tree_checksum dir_checksum;
if(use_cache) {
if(use_cache && !force_valid_cache_) {
try {
if(file_exists(fname_checksum)) {
config checksum_cfg;
@ -1324,7 +1328,10 @@ void game_controller::read_game_cfg(const preproc_map& defines, config& cfg, boo
}
}
if(use_cache && file_exists(fname) && file_create_time(fname) > data_tree_checksum().modified && dir_checksum == data_tree_checksum()) {
if(force_valid_cache_)
std::cerr << "skipping cache validation (forced)\n";
if(use_cache && file_exists(fname) && (force_valid_cache_ || file_create_time(fname) > data_tree_checksum().modified && dir_checksum == data_tree_checksum())) {
std::cerr << "found valid cache at '" << fname << "' using it\n";
log_scope("read cache");
try {
@ -1447,7 +1454,7 @@ void game_controller::refresh_game_cfg(bool reset_translations)
if(!reset_translations) {
game_config_.clear();
read_game_cfg(defines_map_, game_config_, use_caching_);
read_game_cfg(defines_map_, game_config_, use_caching_, force_valid_cache_);
} else {
game_config_.reset_translation();
}
@ -1572,6 +1579,7 @@ int play_game(int argc, char** argv)
<< " sets the severity level of the debug domains.\n"
<< " \"all\" can be used to match any debug domain.\n"
<< " --nocache disables caching of game data.\n"
<< " --validcache assume that cache is valid (dangerous)\n"
<< " --nosound runs the game without sounds and music.\n"
<< " --path prints the name of the game data directory and exits.\n"
<< " -r, --resolution XxY sets the screen resolution. Example: -r 800x600\n"
@ -1834,11 +1842,19 @@ int play_game(int argc, char** argv)
int main(int argc, char** argv)
{
#ifdef OS2 /* required for SDL_GetTicks to work on OS/2 */
if ( SDL_Init(SDL_INIT_TIMER) < 0 ) {
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
if(SDL_Init(SDL_INIT_TIMER) < 0) {
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
return(1);
}
#endif
#ifdef GP2X
if(gp2x::init_joystick() < 0) {
fprintf(stderr, "Couldn't initialize joystick: %s\n", SDL_GetError());
return 1;
}
#endif
try {
std::cerr << "Battle for Wesnoth v" << VERSION << "\n";
time_t t = time(NULL);

225
src/gp2x.cpp Normal file
View file

@ -0,0 +1,225 @@
/* $Id$ */
/*
Copyright (C) 2003-6 by David White <davidnwhite@verizon.net>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#ifdef GP2X
#include <iostream>
#include "SDL.h"
#include "gp2x.hpp"
#include "preferences.hpp"
namespace gp2x {
namespace {
/*
* GP2X joystick mapping from gp2x wiki
*/
#define GP2X_BUTTON_UP (0)
#define GP2X_BUTTON_DOWN (4)
#define GP2X_BUTTON_LEFT (2)
#define GP2X_BUTTON_RIGHT (6)
#define GP2X_BUTTON_UPLEFT (1)
#define GP2X_BUTTON_UPRIGHT (7)
#define GP2X_BUTTON_DOWNLEFT (3)
#define GP2X_BUTTON_DOWNRIGHT (5)
#define GP2X_BUTTON_CLICK (18)
#define GP2X_BUTTON_A (12)
#define GP2X_BUTTON_B (13)
#define GP2X_BUTTON_X (14)
#define GP2X_BUTTON_Y (15)
#define GP2X_BUTTON_L (10)
#define GP2X_BUTTON_R (11)
#define GP2X_BUTTON_START (8)
#define GP2X_BUTTON_SELECT (9)
#define GP2X_BUTTON_VOLUP (16)
#define GP2X_BUTTON_VOLDOWN (17)
enum MouseDirection {
UP = 1 << 0,
DOWN = 1 << 1,
LEFT = 1 << 2,
RIGHT = 1 << 3,
DOWNLEFT = DOWN | LEFT,
DOWNRIGHT = DOWN | RIGHT,
UPLEFT = UP | LEFT,
UPRIGHT = UP | RIGHT
};
/**
* Current mouse position, valid only with emulated mouse
*/
struct mousepos {
unsigned int x;
unsigned int y;
} mouse_position_;
void movemouse(MouseDirection dir)
{
#define MOTION_SPEED (4)
if(dir & UP)
mouse_position_.y -= MOTION_SPEED;
if(dir & DOWN)
mouse_position_.y += MOTION_SPEED;
if(dir & LEFT)
mouse_position_.x -= MOTION_SPEED;
if(dir & RIGHT)
mouse_position_.x += MOTION_SPEED;
/*
* Move mouse cursor and generate MOUSEMOTION event
*/
SDL_WarpMouse(mouse_position_.x, mouse_position_.y);
}
void handle_joybutton(int button, bool down)
{
static SDL_KeyboardEvent keyevent;
static SDL_MouseButtonEvent mouseevent;
if(button == GP2X_BUTTON_VOLDOWN && down) {
preferences::set_music_volume( maximum<int>(preferences::music_volume() - 10, 0) );
preferences::set_sound_volume( maximum<int>(preferences::sound_volume() - 10, 0) );
preferences::set_bell_volume( maximum<int>(preferences::bell_volume() - 10, 0) );
}
else if(button == GP2X_BUTTON_VOLUP && down) {
preferences::set_music_volume(minimum<int>(preferences::music_volume() + 10, 100));
preferences::set_sound_volume(minimum<int>(preferences::sound_volume() + 10, 100));
preferences::set_bell_volume(minimum<int>(preferences::bell_volume() + 10, 100));
}
else if(button == GP2X_BUTTON_A || button == GP2X_BUTTON_B) {
if(down) {
mouseevent.type = SDL_MOUSEBUTTONDOWN;
mouseevent.state = SDL_PRESSED;
} else {
mouseevent.type = SDL_MOUSEBUTTONUP;
mouseevent.state = SDL_RELEASED;
}
mouseevent.button = button == GP2X_BUTTON_A ? SDL_BUTTON_LEFT : SDL_BUTTON_RIGHT;
mouseevent.x = mouse_position_.x;
mouseevent.y = mouse_position_.y;
SDL_PushEvent( (SDL_Event *) &mouseevent);
}
else {
keyevent.which = 0;
if(down) {
keyevent.type = SDL_KEYDOWN;
keyevent.state = SDL_PRESSED;
} else {
keyevent.type = SDL_KEYUP;
keyevent.state = SDL_RELEASED;
}
switch(button) {
case GP2X_BUTTON_R:
keyevent.keysym.sym = SDLK_n; // this is a default hotkey for "next unit"
keyevent.keysym.unicode = 'n'; // ???
keyevent.keysym.mod = KMOD_NONE;
break;
case GP2X_BUTTON_L:
keyevent.keysym.sym = SDLK_n; // this is a default hotkey for "previous unit"
keyevent.keysym.unicode = 'N'; // ???
keyevent.keysym.mod = KMOD_LSHIFT;
break;
case GP2X_BUTTON_X:
keyevent.keysym.sym = SDLK_l; // this is a default hotkey for "go to leader"
keyevent.keysym.unicode = 'l'; // ???
keyevent.keysym.mod = KMOD_NONE;
break;
case GP2X_BUTTON_Y:
keyevent.keysym.sym = SDLK_v; // this is a default hotkey for "show enemy moves"
keyevent.keysym.unicode = 'v'; // ???
keyevent.keysym.mod = KMOD_LCTRL;
break;
case GP2X_BUTTON_CLICK:
keyevent.keysym.sym = SDLK_u; // this is a default hotkey for "undo"
keyevent.keysym.unicode = 'u'; // ???
keyevent.keysym.mod = KMOD_NONE;
break;
case GP2X_BUTTON_START:
keyevent.keysym.sym = SDLK_a; // this is a default hotkey for "accelerated"
keyevent.keysym.unicode = 'a'; // ???
keyevent.keysym.mod = KMOD_LCTRL;
break;
case GP2X_BUTTON_SELECT:
keyevent.keysym.sym = SDLK_r; // this is a default hotkey for "recruit"
keyevent.keysym.unicode = 'r'; // ???
keyevent.keysym.mod = KMOD_LCTRL;
break;
default:
return; // huh?
}
SDL_PushEvent((SDL_Event *) &keyevent);
}
}
} // namespace $$
int init_joystick()
{
std::cerr << "Initializing joystick...\n";
if(SDL_InitSubSystem(SDL_INIT_JOYSTICK) >= 0) {
if(SDL_JoystickOpen(0) == NULL)
return 0;
}
return -1;
}
void handle_joystick(SDL_JoyButtonEvent *ev)
{
SDL_JoyButtonEvent e = *ev;
bool down = (e.type == SDL_JOYBUTTONDOWN ? true : false);
if(e.button == GP2X_BUTTON_UP && down)
movemouse(UP);
else if(e.button == GP2X_BUTTON_DOWN && down)
movemouse(DOWN);
else if(e.button == GP2X_BUTTON_LEFT && down)
movemouse(LEFT);
else if(e.button == GP2X_BUTTON_RIGHT && down)
movemouse(RIGHT);
else if(e.button == GP2X_BUTTON_UPLEFT && down)
movemouse(UPLEFT);
else if(e.button == GP2X_BUTTON_DOWNLEFT && down)
movemouse(DOWNLEFT);
else if(e.button == GP2X_BUTTON_UPRIGHT && down)
movemouse(UPRIGHT);
else if(e.button == GP2X_BUTTON_DOWNRIGHT && down)
movemouse(DOWNRIGHT);
else
handle_joybutton(e.button, down);
}
} // namespace gp2x
#endif
/* vim: set ts=4 sw=4: */

32
src/gp2x.hpp Normal file
View file

@ -0,0 +1,32 @@
/* $Id$ */
/*
Copyright (C) 2003-6 by David White <davidnwhite@verizon.net>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#ifdef GP2X
#ifndef JOYMOUSE_HPP_INCLUDED
#define JOYMOUSE_HPP_INCLUDED
#include "SDL.h"
namespace gp2x {
int init_joystick();
void handle_joystick(SDL_JoyButtonEvent *);
}
#endif
#endif
/* vim: set ts=4 sw=4: */