Skip to content

Commit

Permalink
Merge branch 'sdl-gamecontroller' (pull request GH-17)
Browse files Browse the repository at this point in the history
  • Loading branch information
lluchs committed Mar 21, 2016
2 parents 21fd080 + 8f31ae2 commit 19caa65
Show file tree
Hide file tree
Showing 45 changed files with 955 additions and 802 deletions.
5 changes: 2 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ endif()
# SDL
if(USE_SDL_MAINLOOP)
find_package(SDL2 REQUIRED)
elseif(NOT WIN32)
else()
# for gamepads
find_package(SDL2)
endif()
Expand Down Expand Up @@ -908,8 +908,6 @@ elseif(USE_WIN32_WINDOWS)
list(APPEND OC_GUI_SOURCES
src/editor/C4ConsoleWin32.cpp
src/platform/C4WindowWin32.cpp
src/platform/StdJoystick.cpp
src/platform/StdJoystick.h
)
elseif(USE_COCOA)
list(APPEND OC_GUI_SOURCES
Expand Down Expand Up @@ -1131,6 +1129,7 @@ target_link_libraries(openclonk-server
${PNG_LIBRARIES}
${JPEG_LIBRARIES}
${EXECINFO_LIBRARY}
${SDL2_LIBRARIES}
${READLINE_LIBRARIES}
${Audio_LIBRARIES}
${GETOPT_LIBRARIES}
Expand Down
14 changes: 12 additions & 2 deletions cmake/FindSDL2.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,19 @@
# SDL2_LIBRARIES - a list of libraries to link against to use SDL2
# SDL2_FOUND - if false, SDL2 cannot be used

find_path(SDL2_INCLUDE_DIR SDL.h PATH_SUFFIXES SDL2 HINTS ENV SDL2DIR)
find_path(SDL2_INCLUDE_DIR SDL.h
HINTS
$ENV{SDL2DIR}
PATH_SUFFIXES SDL2 include
)
mark_as_advanced(SDL2_INCLUDE_DIR)
find_library(SDL2_LIBRARY SDL2 HINTS ENV SDL2DIR)

find_library(SDL2_LIBRARY
SDL2
HINTS
$ENV{SDL2DIR}
PATH_SUFFIXES lib
)
mark_as_advanced(SDL2_LIBRARY)

include(FindPackageHandleStandardArgs)
Expand Down
15 changes: 8 additions & 7 deletions docs/sdk/playercontrols.xml
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@
return true;
}</code>
<text>most commands (except for asynchronous commands in the player menu) call a global script function:</text>
<code>global func PlayerControl(int player, int control, C4ID control_extra, int x, int y, int strength, bool repeated, bool release)</code>
<code>global func PlayerControl(int player, int control, C4ID control_extra, int x, int y, int strength, bool repeated, int state)</code>
<text>For an explanation of the parameters see <funclink>PlayerControl</funclink>. Amongst others, the function receives the calling player in player as well as the command to be executed in control.</text>
<text>As a simple example let's assume that in the global <em>PlayerControls.txt</em> the following command has been defined:</text>
<code>[ControlDefs]
Expand All @@ -293,11 +293,11 @@
Control=Jump
Priority=50</code>
<text>This defines a Jump key and the corresponding standard mapping on the keyboard for the first player. The following script is used to handle the control:</text>
<code>global func PlayerControl(int player, int control, C4ID control_extra, int x, int y, int strength, bool repeated, bool release)
<code>global func PlayerControl(int player, int control, C4ID control_extra, int x, int y, int strength, bool repeated, int state)
{
// Which command has been issued?
// The constant CON_Jump has been declared automatically through the definition in PlayerControls.txt
if (control == CON_Jump &amp;&amp; !release)
if (control == CON_Jump &amp;&amp; state == CONS_Down)
{
// pressed the jump button. The clonk selected by the player shall jump
var player_clonk = GetCursor(player);
Expand All @@ -319,17 +319,17 @@
GUIDesc=Going underground
ExtraData=Shovel</code>
<text>Let shovel be the ID of a shovel object. In the global script there could be the following, generic handling for unknown commands, for example:</text>
<code>global func PlayerControl(int player, int control, C4ID control_extra, int x, int y, int strength, bool repeated, bool release)
<code>global func PlayerControl(int player, int control, C4ID control_extra, int x, int y, int strength, bool repeated, int state)
{
// Handling of known controls
// [...]
// control with own handling
if (control_extra) return control_extra-&gt;PlayerControl(player, control, x, y, strength, repeat, release);
if (control_extra) return control_extra-&gt;PlayerControl(player, control, x, y, strength, repeat, state);
// unkown control
return false;
}</code>
<text>And in the script of the shovel:</text>
<code>func PlayerControl(int player, int control, int x, int y, int strength, bool repeated, bool release)
<code>func PlayerControl(int player, int control, int x, int y, int strength, bool repeated, int state)
{
// Handling of known controls
// Control dig directly in the shovel
Expand All @@ -353,6 +353,7 @@
<li>Mappings can emulate permanent key presses using the <em>Hold</em>/<em>Release</em> flags.</li>
<li><emlink href="playercontrols.xml#Repeat">Key repeats</emlink> are generated.</li>
<li>The held state of the key can be queried in the script via <funclink>GetPlayerControlState</funclink>.</li>
<li>If the command is bound to an analog stick or trigger on a controller, every change in position causes in a call to PlayerControl() with state = CONS_Moved.</li>
</ul>
</text>
<text>A good example for this functionality is a directional command:</text>
Expand All @@ -362,7 +363,7 @@
GUIDesc=Walk left
Hold=1</code>
<text>In the script the direction is transferred to the Clonk:</text>
<code>global func PlayerControl(int player, int control, C4ID control_extra, int x, int y, int strength, bool repeated, bool release)
<code>global func PlayerControl(int player, int control, C4ID control_extra, int x, int y, int strength, bool repeated, int state)
{
if (control == CON_Left) return UpdateControlDir(player);
// ...
Expand Down
11 changes: 9 additions & 2 deletions docs/sdk/script/fn/GetPlayerControlState.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<func>
<title>GetPlayerControlState</title>
<category>Player</category>
<version>5.1 OC</version>
<version>5.1 OC (extended in 8.0 OC)</version>
<syntax>
<rtype>int</rtype>
<params>
Expand All @@ -20,9 +20,15 @@
<name>control</name>
<desc>Control to query. A CON_* constant should be used here.</desc>
</param>
<param>
<type>bool</type>
<name>analog_strength</name>
<desc>If true: Query current state of an analog control on a gamepad instead of the emulated button state.</desc>
<optional />
</param>
</params>
</syntax>
<desc>Returns the current state of a control for a certain player. The return value is the strength of the control (e.g. for gamepad joysticks). If the control is assigned to a key, a value not equal to 0 means that the key is currently held down by the player.</desc>
<desc>Returns the current state of a control for a certain player. If the control is assigned to a key, a value not equal to 0 means that the key is currently held down by the player. For analog controls on gamepads, the function either queries the current emulated button state (analog_strength = false), or the current position of the stick or trigger (analog_strength = true).</desc>
<examples>
<example>
<code>
Expand All @@ -36,4 +42,5 @@ if (GetPlayerControlState(GetOwner(), CON_Left) != 0)
</related>
</func>
<author>Zapper</author><date>2015-10</date>
<author>Luchs</author><date>2016-02</date>
</funcs>
45 changes: 45 additions & 0 deletions docs/sdk/script/fn/PlayRumble.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE funcs
SYSTEM '../../../clonk.dtd'>
<?xml-stylesheet type="text/xsl" href="../../../clonk.xsl"?>
<funcs>
<func>
<title>PlayRumble</title>
<category>Player</category>
<version>8.0 OC</version>
<syntax>
<rtype>bool</rtype>
<params>
<param>
<type>int</type>
<name>player</name>
<desc>Number of the player whose controller should rumble. Can be NO_OWNER to make all controllers rumble.</desc>
</param>
<param>
<type>int</type>
<name>strength</name>
<desc>Strength of the rumble, between 0 and 1000.</desc>
</param>
<param>
<type>int</type>
<name>length</name>
<desc>Duration of the rumble in milliseconds.</desc>
</param>
</params>
</syntax>
<desc>Plays a haptic effect on the given player's gamepad. Returns true if all parameters are valid; there is no way to know whether the rumble was actually played.</desc>
<examples>
<example>
<code>
<funclink>ShakeObjects</funclink>(<funclink>LandscapeWidth</funclink>()/2, <funclink>LandscapeHeight</funclink>()/2, <funclink>Distance</funclink>(<funclink>LandscapeWidth</funclink>(), <funclink>LandscapeHeight</funclink>())/2);
PlayRumble(NO_OWNER, 1000, 2000);
</code>
<text>Earthquake: Shakes all Clonks and rumbles all controllers at full strength for two seconds.</text>
</example>
</examples>
<related>
<funclink>StopRumble</funclink>
</related>
</func>
<author>Luchs</author><date>2016-02</date>
</funcs>
80 changes: 80 additions & 0 deletions docs/sdk/script/fn/PlayerControl.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE funcs
SYSTEM '../../../clonk.dtd'>
<?xml-stylesheet type="text/xsl" href="../../../clonk.xsl"?>
<funcs>
<func>
<title>PlayerControl</title>
<category>Callbacks</category>
<version>5.1 OC</version>
<syntax>
<rtype>bool</rtype>
<params>
<param>
<type>int</type>
<name>player</name>
<desc>Number of the player who pressed the control.</desc>
</param>
<param>
<type>int</type>
<name>control</name>
<desc>Number of the pressed control, defined as a CON_ constant via PlayerControls.txt.</desc>
</param>
<param>
<type>id</type>
<name>control_extra</name>
<desc>Optional id defined with ExtraData in PlayerControls.txt.</desc>
</param>
<param>
<type>int</type>
<name>x</name>
<desc>X coordinate for mouse controls.</desc>
</param>
<param>
<type>int</type>
<name>y</name>
<desc>Y coordinate for mouse controls.</desc>
</param>
<param>
<type>int</type>
<name>strength</name>
<desc>Current strength of the control. For key presses: 0 or 100. For analog stick or trigger movement (state = CONS_Moved): 0 to <code>PLRCON_MaxStrength</code>.</desc>
</param>
<param>
<type>bool</type>
<name>repeated</name>
<desc>Whether the call is generated because of a held button.</desc>
</param>
<param>
<type>int</type>
<name>state</name>
<desc>
State of the key press. Possible values:
<table>
<rowh>
<col>Constant</col>
<col>Description</col>
</rowh>
<row>
<literal_col>CONS_Down</literal_col>
<col>Key has been pressed down.</col>
</row>
<row>
<literal_col>CONS_Up</literal_col>
<col>Key has been released. Only generated for held keys.</col>
</row>
<row>
<literal_col>CONS_Moved</literal_col>
<col>An analog control on a gamepad has been moved. Only generated for held keys.</col>
</row>
</table>
</desc>
</param>
</params>
</syntax>
<desc>Called globally for each control command by players. See <emlink href="script/playercontrols.html">Player Controls</emlink>.</desc>
<related><funclink>GetPlayerControlState</funclink></related>
<related><emlink href="playercontrols.html">Player Controls</emlink></related>
</func>
<author>Luchs</author><date>2016-02</date>
</funcs>
26 changes: 26 additions & 0 deletions docs/sdk/script/fn/StopRumble.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE funcs
SYSTEM '../../../clonk.dtd'>
<?xml-stylesheet type="text/xsl" href="../../../clonk.xsl"?>
<funcs>
<func>
<title>StopRumble</title>
<category>Player</category>
<version>8.0 OC</version>
<syntax>
<rtype>bool</rtype>
<params>
<param>
<type>int</type>
<name>player</name>
<desc>Number of the player whose controller should stop rumbling. Can be NO_OWNER to make all controllers stop.</desc>
</param>
</params>
</syntax>
<desc>Stops a rumble effect that was started with <funclink>PlayRumble</funclink>. Returns true if the given player is valid; there is no way to know whether there was actually a playing rumble effect.</desc>
<related>
<funclink>PlayRumble</funclink>
</related>
</func>
<author>Luchs</author><date>2016-02</date>
</funcs>
3 changes: 3 additions & 0 deletions planet/Graphics.ocg/Authors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ StartupPlrSelBG.jpg
StartupScenSelBG.jpg)

Loader1.jpg - Nachtfalter

ControllerIcons.png - Nicolae Berbece (http://opengameart.org/content/free-keyboard-and-controllers-prompts-pack)
License: CC0
Binary file added planet/Graphics.ocg/ControllerIcons.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 5 additions & 3 deletions planet/System.ocg/PlayerControl.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ static g_player_cursor_pos; // array of [x,y] pos arrays; indexed by player. las
// Called by engine whenever a control is issued
// Forwards control to special handler or cursor
// Return whether handled
global func PlayerControl(int plr, int ctrl, id spec_id, int x, int y, int strength, bool repeat, bool release)
global func PlayerControl(int plr, int ctrl, id spec_id, int x, int y, int strength, bool repeat, int status)
{
//Log("%d, %s, %i, %d, %d, %d, %v, %v", plr, GetPlayerControlName(ctrl), spec_id, x,y,strength, repeat, release);
var release = status == CONS_Up;
//Log("%d, %s, %i, %d, %d, %d, %v, %v", plr, GetPlayerControlName(ctrl), spec_id, x,y,strength, repeat, status);
if (status == CONS_Moved) return false;
// Control handled by definition? Forward
if (spec_id) return spec_id->PlayerControl(plr, ctrl, x, y, strength, repeat, release);

Expand Down Expand Up @@ -550,4 +552,4 @@ global func Library_ClonkInventoryControl_Sort_Priority(int x_position)
var priority_x = GetX() - x_position;
if (priority_x < 0) priority_x += 1000;
return priority_x;
}
}

0 comments on commit 19caa65

Please sign in to comment.