1
1
Fork 0
Browse Source

janky text box for level switching

master
alistair 3 years ago
parent
commit
3a7bc25889
  1. 1
      src/controlscheme.c
  2. 1
      src/controlscheme.h
  3. 21
      src/draw.c
  4. 93
      src/game.c
  5. 25
      src/game.h
  6. 7
      src/main.c

1
src/controlscheme.c

@ -32,6 +32,7 @@ void get_input_map(void) { @@ -32,6 +32,7 @@ void get_input_map(void) {
input_map.mute = SDL_SCANCODE_M;
input_map.pause = SDL_SCANCODE_ESCAPE;
input_map.quit = SDL_SCANCODE_Q;
input_map.goto_level = SDL_SCANCODE_G;
input_map.mouse_attach_rope_pull = SDL_BUTTON_LEFT;
input_map.mouse_attach_rope = SDL_BUTTON_RIGHT;
}

1
src/controlscheme.h

@ -21,6 +21,7 @@ struct InputMap { @@ -21,6 +21,7 @@ struct InputMap {
SDL_Scancode mute;
SDL_Scancode quit;
SDL_Scancode pause;
SDL_Scancode goto_level;
Uint8 mouse_attach_rope_pull;
Uint8 mouse_attach_rope;
};

21
src/draw.c

@ -571,6 +571,19 @@ void draw_text(SDL_Renderer *rend, Vect pos, struct colour colour, char *text) { @@ -571,6 +571,19 @@ void draw_text(SDL_Renderer *rend, Vect pos, struct colour colour, char *text) {
}
void draw_level_chooser_tbox(SDL_Renderer *ren) {
char string[250];
snprintf(string, 250,"GOTO LEVEL: %s", gameui.currently_bound_textbox->text_input);
struct environment scene = get_scene_at(viewport_pos, level);
Vect pos = {.x= width - (width / 4), .y = height - (height / 10)};
draw_text(ren, pos, scene.colours.fg1, string);
}
int draw_end_screen(SDL_Renderer *ren) {
struct environment scene = get_scene_at(viewport_pos, level);
@ -698,8 +711,8 @@ void draw_pause_screen(SDL_Renderer *ren, struct environment *e) { @@ -698,8 +711,8 @@ void draw_pause_screen(SDL_Renderer *ren, struct environment *e) {
"controls",
"________",
"pause/unpause: esc",
"grapple: left click",
"grapple and pull: right click",
"grapple: right click",
"grapple and pull: left click",
"go to level: g",
"mute/unmute: m",
"quit: q"
@ -824,6 +837,10 @@ void redraw_buffer(SDL_Renderer * ren) { @@ -824,6 +837,10 @@ void redraw_buffer(SDL_Renderer * ren) {
draw_collision_poly(ren, &lplayer);
draw_forces(ren, &lplayer);
if (gameui.currently_bound_textbox) {
draw_level_chooser_tbox(ren);
}
if (game_paused) {
SDL_Rect r = {.x = 0, .y = 0, .w = width, .h = height};
SDL_SetRenderDrawColor(ren, 0, 0, 0, 90);

93
src/game.c

@ -21,6 +21,7 @@ GlobWorld world; @@ -21,6 +21,7 @@ GlobWorld world;
player_st *glob_player;
SDL_Renderer *debug_ren;
bool in_game;
bool game_paused = false;
extern bool mute;
@ -31,6 +32,15 @@ long level_time; @@ -31,6 +32,15 @@ long level_time;
int get_floor_ceiling();
struct sg_times_list save_times_lst;
/* object that draw.c watches */
struct draw_watcher draw_watch = {};
/* object that handles ui interactions in game.c and which draw watches */
struct textbox_info textboxes[1];
struct ui_state gameui = {};
struct timespec last_tick;
double time_remaining = 0;
@ -56,9 +66,25 @@ void set_motor_max_velocity(Body *thing, int motorID, double max); @@ -56,9 +66,25 @@ void set_motor_max_velocity(Body *thing, int motorID, double max);
void get_new_physics(Body **phys);
void ratcheted_winch_motor_update(Motor* motor);
void winch_motor_update (struct motorstruct *motor);
void load_level();
/* object that draw.c watches */
struct draw_watcher draw_watch;
void reset_textbox(struct textbox_info *tb) {
tb->text_input_bufpos = 0;
memset(tb->text_input, 0, sizeof(tb->text_input));
}
void write_to_textbox(struct textbox_info *tb, char c) {
if (tb->text_input_bufpos >= sizeof(tb->text_input)) {
return;
}
tb->text_input[tb->text_input_bufpos++] = c;
}
void delete_from_textbox(struct textbox_info *tb) {
if (tb->text_input_bufpos > 0)
tb->text_input[--tb->text_input_bufpos] = 0;
}
// move the collision poly to the position of the player
void default_update_collision_poly(Body *body) {
@ -72,6 +98,17 @@ void default_update_collision_poly(Body *body) { @@ -72,6 +98,17 @@ void default_update_collision_poly(Body *body) {
}
}
void new_level_tb_close_callback(struct textbox_info*textbox, void*callback) {
for (int i = 0; i < textbox->text_input_bufpos; i++) {
if (textbox->text_input[i] < '0' || textbox->text_input[i] > '9')
return;
}
int lid = atoi(textbox->text_input);
gameui.goto_new_level = lid;
}
void level_timer_update(bool reset) {
static int start_point = 0;
@ -698,7 +735,13 @@ void destroy_physics_collection(struct physics_collection *s) { @@ -698,7 +735,13 @@ void destroy_physics_collection(struct physics_collection *s) {
}
void next_level() {
void next_level(int lvl) {
level = lvl;
load_level();
}
void load_level() {
SDL_LockMutex(player.physics->lock);
Vect v;
v.x = 0;
@ -724,7 +767,6 @@ void next_level() { @@ -724,7 +767,6 @@ void next_level() {
destroy_physics_collection(&world.uniques_index[ROOM_W]->room->floor);
destroy_environment(&world.uniques_index[ROOM_W]->room->env);
level += 1;;
get_floor_ceiling();
draw_watch.best_time = get_best_time();
load_score_times("saves/times");
@ -737,6 +779,7 @@ void next_level() { @@ -737,6 +779,7 @@ void next_level() {
quality = 100;
level_timer_update(true);
draw_watch.finish_level = false;
viewport_pos.x = player.physics->position.x - width / 2;
viewport_pos.y = player.physics->position.y - height / 2;
@ -1604,6 +1647,8 @@ GlobWorld create_world() { @@ -1604,6 +1647,8 @@ GlobWorld create_world() {
void startgame(SDL_Renderer * ren) {
get_input_map();
textboxes[TB_LEVEL_CHOOSER].close_callback = new_level_tb_close_callback;
debug_ren = ren;
level = 0;
@ -1685,7 +1730,6 @@ void handle_input_event(SDL_Event event) { @@ -1685,7 +1730,6 @@ void handle_input_event(SDL_Event event) {
if (event.key.keysym.scancode == input_map.player_rope) {
delete_rope();
}
#ifdef DEBUGMODE
if (sc == input_map.player_up || sc == input_map.player_down
|| sc == input_map.player_left
@ -1693,15 +1737,38 @@ void handle_input_event(SDL_Event event) { @@ -1693,15 +1737,38 @@ void handle_input_event(SDL_Event event) {
set_motor_newtons(player.physics, M_PLAYER_WALK, 0, 0);
set_motor_status(player.physics, M_PLAYER_WALK, false);
}
if (event.key.keysym.scancode == SDL_SCANCODE_F10) {
next_level();
}
#endif
if (sc == input_map.player_pull_rope) {
stop_pull_rope();
}
// DEBUGINFO
if (event.key.keysym.scancode == SDL_SCANCODE_F10) {
next_level();
if (gameui.currently_bound_textbox) {
if (event.key.keysym.scancode == SDL_SCANCODE_BACKSPACE
|| event.key.keysym.scancode == SDL_SCANCODE_DELETE) {
delete_from_textbox(gameui.currently_bound_textbox);
} else if (event.key.keysym.scancode == SDL_SCANCODE_RETURN) {
if (gameui.currently_bound_textbox->close_callback) {
gameui.currently_bound_textbox->close_callback(
gameui.currently_bound_textbox, NULL);
}
gameui.currently_bound_textbox = NULL;
}
else {
write_to_textbox(gameui.currently_bound_textbox,
(char)event.key.keysym.sym);
}
}
if (sc == input_map.goto_level) {
gameui.level_chooser = !gameui.level_chooser;
if (gameui.level_chooser) {
gameui.currently_bound_textbox = textboxes + TB_LEVEL_CHOOSER;
}
};
break;
case SDL_MOUSEBUTTONDOWN:
add_rope(event.button.x, event.button.y);
@ -1746,10 +1813,16 @@ int step(void) { @@ -1746,10 +1813,16 @@ int step(void) {
game_paused = 1;
}
if (gameui.goto_new_level) {
int nevl = gameui.goto_new_level;
gameui.goto_new_level = 0;
return nevl;
}
if (player.physics->position.x > world.uniques_index[ROOM_W]->room
->floor.items[world.uniques_index[ROOM_W]->room->floor.numItems - 1]->position.x) {
return 1;
draw_watch.finish_level = true;
return level + 1;
}
if (!game_paused) {

25
src/game.h

@ -16,9 +16,32 @@ extern int quality; @@ -16,9 +16,32 @@ extern int quality;
struct draw_watcher {
long best_time;
bool finish_level;
};
struct ui_state {
struct textbox_info *currently_bound_textbox;
bool level_chooser;
int goto_new_level;
};
struct textbox_info {
bool capture_input_text_field;
char text_input[32];
int text_input_bufpos;
void (*close_callback)(struct textbox_info *textbox, void*callback);
};
enum TextBoxId {
TB_LEVEL_CHOOSER = 0
};
extern struct draw_watcher draw_watch;
extern struct textbox_info* texboxes;
extern struct ui_state gameui;
void handle_input_event(SDL_Event event);
@ -30,7 +53,7 @@ bool get_motor_active(Body *thing, int i); @@ -30,7 +53,7 @@ bool get_motor_active(Body *thing, int i);
/* temporary storage variable *n should be initialised to the seed value */
int get_rand(int *n);
void next_level();
void next_level(int lvl);
int step(void);

7
src/main.c

@ -48,7 +48,8 @@ void redraw(struct SDL_Renderer * ren) { @@ -48,7 +48,8 @@ void redraw(struct SDL_Renderer * ren) {
void godophysics(void) {
if (step()) {
int lvl;
if ((lvl = step())) {
// display end level screen
in_game = false;
game_paused = true;
@ -57,7 +58,7 @@ void godophysics(void) { @@ -57,7 +58,7 @@ void godophysics(void) {
add_time(level_time);
SDL_LockMutex(player.physics->lock);
next_level();
next_level(lvl);
in_game = true;
SDL_UnlockMutex(player.physics->lock);
SDL_Delay(1000);
@ -128,7 +129,7 @@ int game(void) { @@ -128,7 +129,7 @@ int game(void) {
}
}
if (!in_game) {
if (!in_game && draw_watch.finish_level) {
draw_end_screen(ren);
} else {
redraw(ren);

Loading…
Cancel
Save