Skip to content

Commit

Permalink
mess. Changed majority of code to use dynamic length of array. Broke …
Browse files Browse the repository at this point in the history
…other parts of code(marked with TODO: unbreak). Program does not compile with 'expected unqualified id' error.
  • Loading branch information
al0fdf committed Oct 26, 2024
1 parent a658f39 commit 6af7feb
Show file tree
Hide file tree
Showing 16 changed files with 185 additions and 108 deletions.
90 changes: 59 additions & 31 deletions src/controls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
distribution.
*/

#include <SDL2/SDL_stdinc.h>
#ifdef _MSC_VER
#include <winsock2.h>
#else
Expand All @@ -37,6 +38,7 @@

#include "asserts.hpp"
#include "controls.hpp"
#include "module.hpp"
#include "joystick.hpp"
#include "multiplayer.hpp"
#include "preferences.hpp"
Expand All @@ -46,11 +48,44 @@ PREF_INT(max_control_history, 1024, "Maximum number of frames to keep control hi

namespace controls
{
const char** control_names()
void resize_controls_list(){
// Change num_controls variable to accurately reflect
// the number of current actions
const char** names = control_names();

for(int n=0;names[n] != NULL;n++){
num_controls=n;
}

// List of SDL keycodes
sdlk = (key_type*) malloc(num_controls*sizeof(key_type));
if(sdlk == NULL){
LOG_ERROR("Memory not allocated to sdlk");
exit(0);
}
sdlk = module::control_keys();


LOG_INFO("NUMBER OF CONTROLS:");
LOG_INFO(num_controls);
}

const static char** control_names()
{
// Null-terminated list of action names
static const char* names[] = { "up", "down", "left", "right", "attack", "jump", "tongue", "sprint", nullptr };
return names;
//static const char* names[] = { "up", "down", "left", "right", "attack", "jump", "tongue", "sprint", nullptr };
return module::module_control_names();
}

int get_action_index(std::string string){
const char** names = control_names();
for(int i=0;names[i] != NULL;i++){
// Does '==' work for std::string and char* comparison?
if (string == names[i]){
return i;
}
}
return -1;
}

namespace {
Expand Down Expand Up @@ -95,21 +130,10 @@ namespace controls

int first_invalid_cycle_var = -1;

// List of SDL keycodes
key_type sdlk[NUM_CONTROLS] = {
SDLK_UP,
SDLK_DOWN,
SDLK_LEFT,
SDLK_RIGHT,
SDLK_d,
SDLK_a,
SDLK_s
};

CONTROL_ITEM g_mouse_controls[3] = {
NUM_CONTROLS,
NUM_CONTROLS,
NUM_CONTROLS,
num_controls,
num_controls,
num_controls,
};

//If any of these keys are held, we ignore other keyboard input.
Expand Down Expand Up @@ -240,18 +264,22 @@ namespace controls
}
}

//array of keys which we are ignoring. We ignore keys on the end of a dialog.
//keys will be unignored as soon as they are no longer depressed.

namespace
{
//array of keys which we are ignoring. We ignore keys on the end of a dialog.
//keys will be unignored as soon as they are no longer depressed.
bool key_ignore[NUM_CONTROLS];
bool* key_ignore = (bool*) malloc(num_controls*sizeof(bool));
if(key_ignore == NULL){
LOG_ERROR("Memory not allocated to key_ignore");
exit(0);
}
for(int i=0;i<num_controls;i++){
key_ignore[i] = false;
}

void ignore_current_keypresses()
{
const Uint8 *state = SDL_GetKeyboardState(nullptr);
for(int n = 0; n < NUM_CONTROLS; ++n) {
for(int n = 0; n < num_controls; ++n) {
key_ignore[n] = state[SDL_GetScancodeFromKey(sdlk[n])] != 0;
}
}
Expand Down Expand Up @@ -305,14 +333,14 @@ namespace controls

Uint32 mouse_buttons = SDL_GetMouseState(nullptr, nullptr);
for(int n = 0; n < 3; ++n) {
if(g_mouse_controls[n] != NUM_CONTROLS && (mouse_buttons&SDL_BUTTON(n+1))) {
if(g_mouse_controls[n] != num_controls && (mouse_buttons&SDL_BUTTON(n+1))) {
if(!key_ignore[g_mouse_controls[n]]) {
state.keys |= (1 << g_mouse_controls[n]);
}
}
}

for(int n = 0; n < NUM_CONTROLS; ++n) {
for(int n = 0; n < num_controls; ++n) {
if(key_state[SDL_GetScancodeFromKey(sdlk[n])] && !ignore_keypresses) {
if(!key_ignore[n]) {
state.keys |= (1 << n);
Expand Down Expand Up @@ -404,7 +432,7 @@ namespace controls

cycle -= delay;
if(cycle < 0) {
for(int n = 0; n != NUM_CONTROLS; ++n) {
for(int n = 0; n != num_controls; ++n) {
output[n] = false;
}
return;
Expand All @@ -414,7 +442,7 @@ namespace controls

unsigned char state = controls[player][cycle].keys;

for(int n = 0; n != NUM_CONTROLS; ++n) {
for(int n = 0; n != num_controls; ++n) {
output[n] = (state&(1 << n)) ? true : false;
}

Expand Down Expand Up @@ -698,7 +726,7 @@ namespace controls
for(unsigned m = 0; m < controls[n].size() && m < static_cast<unsigned>(highest_confirmed[n]); ++m) {
ss.clear();
ss << "CTRL PLAYER " << n << " CYCLE " << m << ": ";
for(int j = 0; j != NUM_CONTROLS; ++j) {
for(int j = 0; j != num_controls; ++j) {
ss << (((1 << j)&controls[n][m].keys) ? "1" : "0");
}
LOG_INFO(ss.str());
Expand All @@ -721,19 +749,19 @@ namespace controls
return g_mouse_controls[mouse_button];
}

return NUM_CONTROLS;
return num_controls;
}

void set_keycode(CONTROL_ITEM item, key_type key)
{
if (item < NUM_CONTROLS) {
if (item < num_controls) {
sdlk[item] = key;
}
}

key_type get_keycode(CONTROL_ITEM item)
{
if (item < NUM_CONTROLS) {
if (item < num_controls) {
return sdlk[item];
}
return SDLK_UNKNOWN;
Expand Down
14 changes: 10 additions & 4 deletions src/controls.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,24 @@ class variant;

namespace controls
{
enum CONTROL_ITEM {
void resize_controls_list();
/* enum CONTROL_ITEM {
CONTROL_UP,
CONTROL_DOWN,
CONTROL_LEFT,
CONTROL_RIGHT,
CONTROL_ATTACK,
CONTROL_JUMP,
CONTROL_TONGUE,
NUM_CONTROLS,
};
num_controls,
};*/

static key_type * sdlk;
static int num_controls;
typedef int CONTROL_ITEM;

const char** control_names();
const static char** control_names();
int get_action_index(std::string string);

void set_mouse_to_keycode(CONTROL_ITEM item, int mouse_button);
CONTROL_ITEM get_mouse_keycode(int mouse_button);
Expand Down
29 changes: 15 additions & 14 deletions src/controls_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@
#include "i18n.hpp"
#include "key_button.hpp"
#include "screen_handling.hpp"
#include <vector>

namespace
{
gui::KeyButtonPtr KeyButtons[controls::NUM_CONTROLS];
std::vector<gui::KeyButtonPtr> KeyButtons(controls::num_controls);

void end_dialog(gui::Dialog* d)
{
using namespace controls;
for(int n = 0; n < NUM_CONTROLS; ++n) {
for(int n = 0; n < num_controls; ++n) {
const CONTROL_ITEM item = static_cast<CONTROL_ITEM>(n);
set_keycode(item, KeyButtons[item]->get_key());
}
Expand All @@ -52,7 +53,7 @@ void show_controls_dialog()
using namespace controls;
const int vw = graphics::GameScreen::get().getVirtualWidth();
const int vh = graphics::GameScreen::get().getVirtualHeight();

//HACK: 10 and 4 are the default button padding. Padding should be taken from buttons in KeyButtons list
int butt_padx = 10;
int butt_pady = 4;
Expand All @@ -62,7 +63,7 @@ void show_controls_dialog()

int butt_width_wp = butt_width + butt_padx;
int butt_height_wp = butt_height + butt_pady;

int sep_y = 50;

int height = vh- 20;
Expand All @@ -74,32 +75,32 @@ void show_controls_dialog()
d.setDrawBackgroundFn(draw_last_scene);


for(int n = 0; n < NUM_CONTROLS; ++n) {
for(int n = 0; n < num_controls; ++n) {
const CONTROL_ITEM item = static_cast<CONTROL_ITEM>(n);
KeyButtons[item] = KeyButtonPtr(new KeyButton(get_keycode(item), BUTTON_SIZE_DOUBLE_RESOLUTION));
KeyButtons[item]->setDim(butt_width, butt_height);
}

WidgetPtr t_dirs(new GraphicalFontLabel(_("Directions"), "door_label", 2));
WidgetPtr b_up(KeyButtons[CONTROL_UP]);
WidgetPtr b_down(KeyButtons[CONTROL_DOWN]);
WidgetPtr b_left(KeyButtons[CONTROL_LEFT]);
WidgetPtr b_right(KeyButtons[CONTROL_RIGHT]);
WidgetPtr b_up(KeyButtons[controls::get_action_index("up")]);
WidgetPtr b_down(KeyButtons[controls::get_action_index("down")]);
WidgetPtr b_left(KeyButtons[controls::get_action_index("left")]);
WidgetPtr b_right(KeyButtons[controls::get_action_index("right")]);

WidgetPtr t_jump(new GraphicalFontLabel(_("Jump"), "door_label", 2));
WidgetPtr b_jump(KeyButtons[CONTROL_JUMP]);
WidgetPtr b_jump(KeyButtons[controls::get_action_index("jump")]);
WidgetPtr t_tongue(new GraphicalFontLabel(_("Tongue"), "door_label", 2));
WidgetPtr b_tongue(KeyButtons[CONTROL_TONGUE]);
WidgetPtr b_tongue(KeyButtons[controls::get_action_index("tongue")]);
WidgetPtr t_item(new GraphicalFontLabel(_("Item"), "door_label", 2));
WidgetPtr b_item(KeyButtons[CONTROL_ATTACK]);
WidgetPtr b_item(KeyButtons[controls::get_action_index("attack")]);

//WidgetPtr b_sprint(KeyButtons[CONTROL_SPRINT]);
//WidgetPtr t_sprint(new GraphicalFontLabel(_("Sprint"), "door_label", 2));

WidgetPtr back_button(new Button(WidgetPtr(new GraphicalFontLabel(_("Back"), "door_label", 2)), std::bind(end_dialog, &d), BUTTON_STYLE_DEFAULT, BUTTON_SIZE_DOUBLE_RESOLUTION));
back_button->setDim(230, 60);


int top_label_height = d.padding();
int top_label_botm_edge = top_label_height+t_dirs->height();

Expand Down
4 changes: 2 additions & 2 deletions src/dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,8 +566,8 @@ namespace gui

if(!claimed && opened_) {
if(ev.type == SDL_KEYDOWN) {
if(ev.key.keysym.sym == controls::get_keycode(controls::CONTROL_ATTACK)
|| ev.key.keysym.sym == controls::get_keycode(controls::CONTROL_JUMP)) {
if(ev.key.keysym.sym == controls::get_keycode(controls::get_action_index("attack"))
|| ev.key.keysym.sym == controls::get_keycode(controls::get_action_index("jump"))) {
doSelectEvent();
}
if(ev.key.keysym.sym == SDLK_TAB) {
Expand Down
5 changes: 3 additions & 2 deletions src/dropdown_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,9 @@ namespace gui

if(hasFocus() && dropdown_menu_) {
if(event.type == SDL_KEYDOWN
&& (event.key.keysym.sym == controls::get_keycode(controls::CONTROL_ATTACK)
|| event.key.keysym.sym == controls::get_keycode(controls::CONTROL_JUMP))) {

&& (event.key.keysym.sym == controls::get_keycode(controls::get_action_index("attack"))
|| event.key.keysym.sym == controls::get_keycode(controls::get_action_index("jump")))) {
claimed = true;
dropdown_menu_->setVisible(!dropdown_menu_->visible());
}
Expand Down
14 changes: 8 additions & 6 deletions src/entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include "Canvas.hpp"

#include "controls.hpp"
#include "custom_object.hpp"
#include "debug_console.hpp"
#include "entity.hpp"
Expand Down Expand Up @@ -65,9 +66,10 @@ Entity::Entity(variant node)
setAnchorY(node["anchory"].as_decimal());
}

for(bool& b : controls_) {
b = false;
}

for (int i=0;i<controls::num_controls;i++) {
controls_[i] = false;
}
}

Entity::Entity(int x, int y, bool face_right)
Expand All @@ -81,8 +83,8 @@ Entity::Entity(int x, int y, bool face_right)
mouseover_delay_(0), mouseover_trigger_cycle_(std::numeric_limits<int>::max()),
true_z_(false), tx_(double(x)), ty_(double(y)), tz_(0.0f)
{
for(bool& b : controls_) {
b = false;
for(int i=0;i<controls::num_controls;i++) {
controls_[i] = false;
}
}

Expand Down Expand Up @@ -571,7 +573,7 @@ const rect& Entity::getMouseOverArea() const

void Entity::beingRemoved()
{
scheduled_commands_.clear();
scheduled_commands_.clear();
}

bool zorder_compare(const EntityPtr& a, const EntityPtr& b)
Expand Down
4 changes: 2 additions & 2 deletions src/entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ class Entity : public game_logic::WmlSerializableFormulaCallable
void setControlStatusUser(const variant& v) { controls_user_ = v; }
void setControlStatus(const std::string& key, bool value);
void setControlStatus(controls::CONTROL_ITEM ctrl, bool value) { controls_[ctrl] = value; }
void clearControlStatus() { for(int n = 0; n != controls::NUM_CONTROLS; ++n) { controls_[n] = false; } }
void clearControlStatus() { for(int n = 0; n != controls::num_controls; ++n) { controls_[n] = false; } }

virtual bool enter() const { return false; }

Expand Down Expand Up @@ -418,7 +418,7 @@ class Entity : public game_logic::WmlSerializableFormulaCallable

std::vector<ScheduledCommand> scheduled_commands_;

bool controls_[controls::NUM_CONTROLS];
std::vector<bool> controls_;
variant controls_user_;

//attached objects are objects which are also drawn with this object.
Expand Down
Loading

0 comments on commit 6af7feb

Please sign in to comment.