commit 7da5b967f0f215fe5183027861ae0f099d65d312
parent d412dad2a013993ae5a714107c4db327ec79d27c
Author: amin <dev@aminmesbah.com>
Date: Wed, 25 Oct 2017 04:46:38 +0000
Interpret left thumb stick as a thrust vector
FossilOrigin-Name: f65f0396cbfb85fcfa854361765f3777f9fef4b6413ac744f08958e2415c63fe
Diffstat:
4 files changed, 29 insertions(+), 11 deletions(-)
diff --git a/src/game.c b/src/game.c
@@ -1,4 +1,6 @@
#include "game.h"
+// TODO: remove this
+#include <math.h>
#include <stdbool.h>
#include <stdio.h>
@@ -34,6 +36,14 @@ void game_update(struct GameState *game_state, struct GameControllerInput game_i
// TODO: handle invalid pointer error
return;
}
+
+ game_state->thrust_vector.angle = atan2f(game_input.left_stick_y, game_input.left_stick_x);
+ game_state->thrust_vector.length = 100 * hypotf(game_input.left_stick_x, game_input.left_stick_y);
+ printf("(lx: %f, ly: %f, thrust_a: %f, thrust_l: %f)\n",
+ game_input.left_stick_x,
+ game_input.left_stick_y,
+ game_state->thrust_vector.angle,
+ game_state->thrust_vector.length);
}
@@ -46,6 +56,11 @@ void game_render(struct OffscreenBuffer *buffer, float dt, struct GameState *gam
}
struct Entity player = game_state->player;
game_render_circle(buffer, player.x, player.y, player.size, player.color);
+ game_render_circle(buffer,
+ player.x + (game_state->thrust_vector.length * cos(game_state->thrust_vector.angle)),
+ player.y + (game_state->thrust_vector.length * sin(game_state->thrust_vector.angle)),
+ 10,
+ 0x0000FF);
}
@@ -63,11 +78,6 @@ void game_render_circle(struct OffscreenBuffer *buffer, float center_x, float ce
}
}
-float game_calc_render_offset(float zoom, float delta, float pos, float center)
-{
- return ((1 - zoom) * center) + (pos + delta) * zoom;
-}
-
void game_set_pixel(struct OffscreenBuffer *buffer, uint32_t x, uint32_t y, uint32_t color)
{
@@ -91,6 +101,12 @@ void game_set_pixel(struct OffscreenBuffer *buffer, uint32_t x, uint32_t y, uint
}
+float game_calc_render_offset(float zoom, float delta, float pos, float center)
+{
+ return ((1 - zoom) * center) + (pos + delta) * zoom;
+}
+
+
void game_cleanup(struct GameState *game_state)
{
if (!game_state)
diff --git a/src/game.h b/src/game.h
@@ -48,6 +48,7 @@ struct GameView
struct GameState
{
struct Entity player;
+ struct Vec2d thrust_vector;
struct GameView view;
};
diff --git a/src/platform_sdl.c b/src/platform_sdl.c
@@ -69,6 +69,7 @@ void clear_screen(struct SDLOffscreenBuffer *buffer, uint32_t pixel_value)
}
+// TODO: make the deadzone circular
float sdl_process_controller_axis_value(int16_t value, int16_t deadzone_threshold)
{
float result = 0.0;
@@ -120,11 +121,11 @@ bool sdl_handle_event(SDL_Event *event, struct GameControllerInput *controller_i
// down and right are positive
float normalized_axis_value = sdl_process_controller_axis_value(event->caxis.value, DEADZONE_THRESHOLD);
- printf("SDL_CONTROLLERAXISMOTION (%d, %s, %d, %f)\n",
- event->caxis.which,
- SDL_GameControllerGetStringForAxis(event->caxis.axis),
- event->caxis.value,
- normalized_axis_value);
+ //printf("SDL_CONTROLLERAXISMOTION (%d, %s, %d, %f)\n",
+ // event->caxis.which,
+ // SDL_GameControllerGetStringForAxis(event->caxis.axis),
+ // event->caxis.value,
+ // normalized_axis_value);
switch(event->caxis.axis)
{
diff --git a/src/platform_sdl.h b/src/platform_sdl.h
@@ -3,7 +3,7 @@
#include "SDL.h"
#define MAX_CONTROLLERS 4
-#define DEADZONE_THRESHOLD 1000
+#define DEADZONE_THRESHOLD 9000
struct SDLOffscreenBuffer
{