File: Os.cpp

package info (click to toggle)
numptyphysics 0.2+svn157-0.3
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 2,596 kB
  • ctags: 2,896
  • sloc: cpp: 18,280; sh: 810; makefile: 205
file content (174 lines) | stat: -rw-r--r-- 4,579 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * This file is part of NumptyPhysics
 * Copyright (C) 2008 Tim Edmonds
 * 
 * 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 3 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.
 *
 */

#include "Os.h"
#include <SDL/SDL.h>

#include <sys/stat.h>
#include <unistd.h>

OsObj OS;

static const BasicEventMap::KeyPair game_keymap[] = {
  { SDLK_SPACE,    Event::PAUSE },
  { SDLK_KP_ENTER, Event::PAUSE },
  { SDLK_RETURN,   Event::PAUSE },
  { SDLK_ESCAPE,   Event::UNDO  },
  { SDLK_BACKSPACE,Event::UNDO  },
  { SDLK_u,        Event::UNDO  },
  { SDLK_DOWN,     Event::UNDO  },
  { SDLK_F7,       Event::UNDO  },
  { SDLK_s,        Event::SAVE  },
  { SDLK_F4,       Event::OPTION},
  { SDLK_m,        Event::MENU},
  { SDLK_e,        Event::EDIT  },
  { SDLK_F6,       Event::EDIT  },
  { SDLK_r,        Event::RESET },
  { SDLK_UP,       Event::RESET },
  { SDLK_n,        Event::NEXT  },
  { SDLK_RIGHT,    Event::NEXT  },
  { SDLK_p,        Event::PREVIOUS },
  { SDLK_LEFT,     Event::PREVIOUS },
  { SDLK_v,        Event::REPLAY},
  {}
};

static const BasicEventMap::ButtonPair game_mousemap[] = {
  { SDL_BUTTON_LEFT, Event::DRAWBEGIN, Event::DRAWMORE, Event::DRAWEND },
  {}
};

static const BasicEventMap::ButtonPair game_move_mousemap[] = {
  { SDL_BUTTON_LEFT, Event::MOVEBEGIN, Event::MOVEMORE, Event::MOVEEND },
  {}
};

static const BasicEventMap::ButtonPair game_erase_mousemap[] = {
  { SDL_BUTTON_LEFT, Event::NOP, Event::NOP, Event::DELETE },
  {}
};

static const BasicEventMap::KeyPair app_keymap[] = {
  { SDLK_q, Event::QUIT },
  {}
};


static const BasicEventMap::ButtonPair edit_mousemap[] = {
  { SDL_BUTTON_LEFT, Event::DRAWBEGIN, Event::DRAWMORE, Event::DRAWEND },
  { SDL_BUTTON_RIGHT, Event::MOVEBEGIN, Event::MOVEMORE, Event::MOVEEND },
  { SDL_BUTTON_MIDDLE, Event::DELETE },
  {}
};

static const BasicEventMap::ButtonPair ui_button_mousemap[] = {
  { SDL_BUTTON_LEFT, Event::FOCUS, Event::FOCUS, Event::SELECT },
  {}
};

static const BasicEventMap::ButtonPair ui_draggable_mousemap[] = {
  { SDL_BUTTON_LEFT, Event::MOVEBEGIN, Event::MOVEMORE, Event::MOVEEND },
  {}
};

static const BasicEventMap::KeyPair ui_draggable_keymap[] = {
  { SDLK_UP,       Event::UP },
  { SDLK_DOWN,     Event::DOWN },
  { SDLK_RIGHT,    Event::RIGHT  },
  { SDLK_LEFT,     Event::LEFT },
  {}
};

static const BasicEventMap::KeyPair ui_dialog_keymap[] = {
  { SDLK_ESCAPE,   Event::CLOSE  },
  {}
};


class AppMap : public BasicEventMap
{
public:
  AppMap() : BasicEventMap( app_keymap, NULL ) {}
  Event process(const SDL_Event& ev)
  {
    if (ev.type==SDL_QUIT) {
      return Event(Event::QUIT);
    }
    return BasicEventMap::process(ev);
  }

};



EventMap* Os::getEventMap( EventMapType type )
{
  static BasicEventMap gameMap(game_keymap,game_mousemap);
  static BasicEventMap gameMoveMap(game_keymap,game_move_mousemap);
  static BasicEventMap gameEraseMap(game_keymap,game_erase_mousemap);
  static BasicEventMap editMap(NULL,edit_mousemap);
  static BasicEventMap uiButtonMap(NULL,ui_button_mousemap);
  static BasicEventMap uiDraggableMap(ui_draggable_keymap,ui_draggable_mousemap);
  static BasicEventMap uiDialogMap(ui_dialog_keymap,NULL);
  static AppMap appMap;

  switch (type) {
  case GAME_MAP:
    return &gameMap;
  case GAME_MOVE_MAP:
    return &gameMoveMap;
  case GAME_ERASE_MAP:
    return &gameEraseMap;
  case APP_MAP:
    return &appMap;
  case EDIT_MAP:
    return &editMap;
  case UI_BUTTON_MAP:
    return &uiButtonMap;
  case UI_DRAGGABLE_MAP:
    return &uiDraggableMap;
  case UI_DIALOG_MAP:
    return &uiDialogMap;
  }
  return NULL;
}


bool Os::ensurePath(const std::string& path)
{
  struct stat st;
  if ( stat(path.c_str(),&st)!=0 ) {
    size_t sep = path.rfind(Os::pathSep);
    if ( sep != std::string::npos && sep > 0 ) {
      ensurePath(path.substr(0,sep));
    }
    if ( mkdir( path.c_str(), 0755)!=0 ) {
      fprintf(stderr,"failed to create dir %s\n", path.c_str());
      return false;
    } else {
      fprintf(stderr,"created dir %s\n", path.c_str());
      return true;
    }
  } 
  return true;
}


bool Os::exists(const std::string& file)
{
  struct stat st;
  return stat(file.c_str(),&st)==0;
}