Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Started to add some support for touch controls.

  • Loading branch information...
commit b9945d13c62e6bda067397ea781d3bcd56add56a 1 parent c0c8854
authored November 16, 2011
62  jni/kasui/in_game.cpp
@@ -76,6 +76,9 @@ class falling_block {
76 76
 	void initialize();
77 77
 	void draw(gl_vertex_array_texuv_color& gv, float lerp_dt) const;
78 78
 	void update(unsigned dpad_state);
  79
+	void on_touch_down(int x, int y);
  80
+	void on_touch_up();
  81
+	void on_touch_move(int x, int y);
79 82
 
80 83
 	bool is_active() const
81 84
 	{ return state != INACTIVE; }
@@ -89,6 +92,7 @@ class falling_block {
89 92
 		MOVING_RIGHT,
90 93
 		DROPPING,
91 94
 		SWAPPING,
  95
+		TOUCH_DRAG,
92 96
 		INACTIVE,
93 97
 	};
94 98
 
@@ -104,6 +108,9 @@ class falling_block {
104 108
 	int block_types[2];
105 109
 	int tics_to_drop;
106 110
 	int state_tics;
  111
+
  112
+	int last_touch_x;
  113
+	int drag_x_offset;
107 114
 };
108 115
 
109 116
 static falling_block falling_block_queue[2];
@@ -211,6 +218,10 @@ falling_block::draw(gl_vertex_array_texuv_color& gv, float lerp_dt) const
211 218
 				y -= (state_tics + lerp_dt)*CELL_SIZE/MOVE_TICS;
212 219
 				break;
213 220
 
  221
+			case TOUCH_DRAG:
  222
+				x += drag_x_offset;
  223
+				break;
  224
+
214 225
 			default:
215 226
 				break;
216 227
 		}
@@ -306,6 +317,33 @@ falling_block::update(unsigned dpad_state)
306 317
 }
307 318
 
308 319
 void
  320
+falling_block::on_touch_down(int x, int y)
  321
+{
  322
+	if (state == IDLE) {
  323
+		set_state(TOUCH_DRAG);
  324
+		drag_x_offset = 0;
  325
+		last_touch_x = x;
  326
+	}
  327
+}
  328
+
  329
+void
  330
+falling_block::on_touch_up()
  331
+{
  332
+	if (state == TOUCH_DRAG) {
  333
+		set_state(IDLE);
  334
+	}
  335
+}
  336
+
  337
+void
  338
+falling_block::on_touch_move(int x, int y)
  339
+{
  340
+	if (state == TOUCH_DRAG) {
  341
+		drag_x_offset += x - last_touch_x;
  342
+		last_touch_x = x;
  343
+	}
  344
+}
  345
+
  346
+void
309 347
 block_sprite::initialize(int type_, int r, int c)
310 348
 {
311 349
 	assert(!is_active);
@@ -897,8 +935,32 @@ reset()
897 935
 // score_text::create(&jukugo_list[0]);
898 936
 }
899 937
 
  938
+static void
  939
+on_touch_down(int x, int y)
  940
+{
  941
+	if (cur_state == STATE_FALLING_BLOCK)
  942
+		cur_falling_block->on_touch_down(x, y);
  943
+}
  944
+
  945
+static void
  946
+on_touch_up()
  947
+{
  948
+	if (cur_state == STATE_FALLING_BLOCK)
  949
+		cur_falling_block->on_touch_up();
  950
+}
  951
+
  952
+static void
  953
+on_touch_move(int x, int y)
  954
+{
  955
+	if (cur_state == STATE_FALLING_BLOCK)
  956
+		cur_falling_block->on_touch_move(x, y);
  957
+}
  958
+
900 959
 state in_game_state = {
901 960
 	reset,
902 961
 	redraw,
903 962
 	update,
  963
+	on_touch_down,
  964
+	on_touch_up,
  965
+	on_touch_move,
904 966
 };
18  jni/kasui/kasui.cpp
@@ -97,3 +97,21 @@ kasui_redraw(unsigned next_dpad_state)
97 97
 
98 98
 	redraw((float)(get_cur_tics() - last_update)/update_interval);
99 99
 }
  100
+
  101
+void
  102
+kasui_on_touch_down(int x, int y)
  103
+{
  104
+	cur_state->on_touch_down(x, y);
  105
+}
  106
+
  107
+void
  108
+kasui_on_touch_up()
  109
+{
  110
+	cur_state->on_touch_up();
  111
+}
  112
+
  113
+void
  114
+kasui_on_touch_move(int x, int y)
  115
+{
  116
+	cur_state->on_touch_move(x, y);
  117
+}
11  jni/kasui/kasui.h
@@ -2,7 +2,7 @@
2 2
 #define KASUI_H_
3 3
 
4 4
 void
5  
-kasui_init(void);
  5
+kasui_init();
6 6
 
7 7
 void
8 8
 kasui_resize(int width, int height);
@@ -10,4 +10,13 @@ kasui_resize(int width, int height);
10 10
 void
11 11
 kasui_redraw(unsigned dpad_state);
12 12
 
  13
+void
  14
+kasui_on_touch_down(int x, int y);
  15
+
  16
+void
  17
+kasui_on_touch_up();
  18
+
  19
+void
  20
+kasui_on_touch_move(int x, int y);
  21
+
13 22
 #endif // KASUI_H_
21  jni/kasui/main.cpp
@@ -24,6 +24,7 @@ enum {
24 24
 
25 25
 static const char *WINDOW_CAPTION = "K-RAD";
26 26
 static unsigned dpad_state;
  27
+static bool mouse_button_down = false;
27 28
 
28 29
 class lunix_asset_reader : public asset_reader {
29 30
   public:
@@ -181,6 +182,26 @@ handle_events()
181 182
 						break;
182 183
 				}
183 184
 
  185
+			case SDL_MOUSEBUTTONDOWN:
  186
+				if (event.button.button == SDL_BUTTON_LEFT) {
  187
+					mouse_button_down = true;
  188
+					kasui_on_touch_down(event.button.x, WINDOW_HEIGHT - 1 - event.button.y);
  189
+				}
  190
+				break;
  191
+
  192
+			case SDL_MOUSEBUTTONUP:
  193
+				if (event.button.button == SDL_BUTTON_LEFT) {
  194
+					mouse_button_down = false;
  195
+					kasui_on_touch_up();
  196
+				}
  197
+				break;
  198
+
  199
+			case SDL_MOUSEMOTION:
  200
+				if (mouse_button_down) {
  201
+					kasui_on_touch_move(event.motion.x, WINDOW_HEIGHT - 1 - event.motion.y);
  202
+				}
  203
+				break;
  204
+
184 205
 			default:
185 206
 				break;
186 207
 		}
3  jni/kasui/state.h
@@ -5,6 +5,9 @@ struct state {
5 5
 	void (*reset)();
6 6
 	void (*redraw)(float lerp_dt);
7 7
 	void (*update)(unsigned dpad_state);
  8
+	void (*on_touch_down)(int x, int y);
  9
+	void (*on_touch_up)();
  10
+	void (*on_touch_move)(int x, int y);
8 11
 };
9 12
 
10 13
 #endif // STATE_H_

0 notes on commit b9945d1

Please sign in to comment.
Something went wrong with that request. Please try again.