mail[Wesnoth-commits] r13573 - in /trunk/src: Makefile.am events.cpp game.cpp gp2x.cpp gp2x.hpp


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by grzywacz on September 17, 2006 - 11:07:
Author: grzywacz
Date: Sun Sep 17 11:07:04 2006
New Revision: 13573

URL: http://svn.gna.org/viewcvs/wesnoth?rev=13573&view=rev
Log:
Add mouse simulation with joystick for gp2x, wire in changes.

Added:
    trunk/src/gp2x.cpp   (with props)
    trunk/src/gp2x.hpp   (with props)
Modified:
    trunk/src/Makefile.am
    trunk/src/events.cpp
    trunk/src/game.cpp

Modified: trunk/src/Makefile.am
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/Makefile.am?rev=13573&r1=13572&r2=13573&view=diff
==============================================================================
--- trunk/src/Makefile.am (original)
+++ trunk/src/Makefile.am Sun Sep 17 11:07:04 2006
@@ -62,6 +62,7 @@
        game_events.cpp \
        gamestatus.cpp \
        generic_event.cpp \
+       gp2x.cpp \
        halo.cpp \
        help.cpp \
        hotkeys.cpp \
@@ -433,6 +434,7 @@
        playsingle_controller.hpp \
        generic_event.hpp \
        sdl_ttf/SDL_ttf.h \
+       gp2x.hpp \
        wesconfig.h
 
 #############################################################################

Modified: trunk/src/events.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/events.cpp?rev=13573&r1=13572&r2=13573&view=diff
==============================================================================
--- trunk/src/events.cpp (original)
+++ trunk/src/events.cpp Sun Sep 17 11:07:04 2006
@@ -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"
@@ -285,6 +286,13 @@
 
                                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

Modified: trunk/src/game.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/game.cpp?rev=13573&r1=13572&r2=13573&view=diff
==============================================================================
--- trunk/src/game.cpp (original)
+++ trunk/src/game.cpp Sun Sep 17 11:07:04 2006
@@ -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 @@
        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 @@
 
        bool test_mode_, multiplayer_mode_, no_gui_;
        bool use_caching_;
+       bool force_valid_cache_;
        int force_bpp_;
 
        config game_config_;
@@ -154,8 +156,8 @@
 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 @@
                        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 @@
 }
 
 //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 @@
 
                                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 @@
                                        }
                                }
 
-                               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 @@
 
                        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 @@
                        << "                               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 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);

Added: trunk/src/gp2x.cpp
URL: http://svn.gna.org/viewcvs/wesnoth/trunk/src/gp2x.cpp?rev=13573&view=auto
==============================================================================
--- trunk/src/gp2x.cpp (added)
+++ trunk/src/gp2x.cpp Sun Sep 17 11:07:04 2006
@@ -1,0 +1,225 @@
+/* $Id$ */
+/*
+   Copyright (C) 2003-6 by David White <davidnwhite@xxxxxxxxxxx>
+   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: */

Propchange: trunk/src/gp2x.cpp
------------------------------------------------------------------------------
    svn:keywords = Id

Added: trunk/src/gp2x.hpp
URL: http://svn.gna.org/viewcvs/wesnoth/trunk/src/gp2x.hpp?rev=13573&view=auto
==============================================================================
--- trunk/src/gp2x.hpp (added)
+++ trunk/src/gp2x.hpp Sun Sep 17 11:07:04 2006
@@ -1,0 +1,32 @@
+/* $Id$ */
+/*
+   Copyright (C) 2003-6 by David White <davidnwhite@xxxxxxxxxxx>
+   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: */

Propchange: trunk/src/gp2x.hpp
------------------------------------------------------------------------------
    svn:keywords = Id




Related Messages


Powered by MHonArc, Updated Sun Sep 17 12:01:41 2006