Skip to content

Commit

Permalink
movable cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
goblinhack committed May 11, 2024
1 parent 60017e0 commit 9f11625
Show file tree
Hide file tree
Showing 16 changed files with 249 additions and 253 deletions.
45 changes: 31 additions & 14 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,12 @@ class Game
//
// These are the onscreen map pixel co-ords.
//
int onscreen_map_tl_x;
int onscreen_map_tl_y;
int onscreen_map_br_x;
int onscreen_map_br_y;
int visible_map_tl_x;
int visible_map_tl_y;
int visible_map_br_x;
int visible_map_br_y;
int visible_map_mouse_x;
int visible_map_mouse_y;

/////////////////////////////////////////////////////////////////////////
// not worth saving
Expand Down Expand Up @@ -361,6 +363,7 @@ void Game::display(void)
if (level) {
level_tick(level);
level_anim(level);
level_cursor_reset(level);
level_display(level);
}
}
Expand Down Expand Up @@ -450,20 +453,34 @@ void game_load_config(class Game *game) { game->load_config(); }

class HiScores *game_hiscores_get(class Game *game) { return &game->config.hiscores; }

void game_onscreen_map_get(class Game *game, int *onscreen_map_tl_x, int *onscreen_map_tl_y, int *onscreen_map_br_x,
int *onscreen_map_br_y)
void game_visible_map_get(class Game *game, int *visible_map_tl_x, int *visible_map_tl_y, int *visible_map_br_x,
int *visible_map_br_y)
{
*onscreen_map_tl_x = game->onscreen_map_tl_x, *onscreen_map_tl_y = game->onscreen_map_tl_y,
*onscreen_map_br_x = game->onscreen_map_br_x, *onscreen_map_br_y = game->onscreen_map_br_y;
*visible_map_tl_x = game->visible_map_tl_x;
*visible_map_tl_y = game->visible_map_tl_y;
*visible_map_br_x = game->visible_map_br_x;
*visible_map_br_y = game->visible_map_br_y;
}

void game_onscreen_map_set(class Game *game, int onscreen_map_tl_x, int onscreen_map_tl_y, int onscreen_map_br_x,
int onscreen_map_br_y)
void game_visible_map_set(class Game *game, int visible_map_tl_x, int visible_map_tl_y, int visible_map_br_x,
int visible_map_br_y)
{
game->onscreen_map_tl_x = onscreen_map_tl_x;
game->onscreen_map_tl_y = onscreen_map_tl_y;
game->onscreen_map_br_x = onscreen_map_br_x;
game->onscreen_map_br_y = onscreen_map_br_y;
game->visible_map_tl_x = visible_map_tl_x;
game->visible_map_tl_y = visible_map_tl_y;
game->visible_map_br_x = visible_map_br_x;
game->visible_map_br_y = visible_map_br_y;
}

void game_visible_map_mouse_get(class Game *game, int *visible_map_mouse_x, int *visible_map_mouse_y)
{
*visible_map_mouse_x = game->visible_map_mouse_x;
*visible_map_mouse_y = game->visible_map_mouse_y;
}

void game_visible_map_mouse_set(class Game *game, int visible_map_mouse_x, int visible_map_mouse_y)
{
game->visible_map_mouse_x = visible_map_mouse_x;
game->visible_map_mouse_y = visible_map_mouse_y;
}

uint32_t game_last_mouse_down_get(class Game *game) { return game->last_mouse_down; }
Expand Down
39 changes: 39 additions & 0 deletions src/level_cursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
//

#include "my_callstack.hpp"
#include "my_game.hpp"
#include "my_level.hpp"
#include "my_sdl_event.hpp"
#include "my_tp.hpp"

#include <string.h>
Expand All @@ -28,3 +30,40 @@ int level_cursor_get(Level *l, int x, int y)

return l->cursor[ x ][ y ];
}

//
// Clear all cursors and get the mouse position, so when displaying tiles
// we can work out where the cursor is now.
//
void level_cursor_reset(Levelp l)
{
TRACE_NO_INDENT();

memset(l->cursor, 0, sizeof(l->cursor));

//
// Get the visible map bounds
//
int visible_map_tl_x;
int visible_map_tl_y;
int visible_map_br_x;
int visible_map_br_y;
int visible_map_mouse_x;
int visible_map_mouse_y;
game_visible_map_get(game, &visible_map_tl_x, &visible_map_tl_y, &visible_map_br_x, &visible_map_br_y);

//
// Find out what pixel on the map the mouse is over
//
visible_map_mouse_x = sdl.mouse_x - visible_map_tl_x;
visible_map_mouse_y = sdl.mouse_y;
float scale_x = (float) game_pix_width_get(game) / (float) game_window_pix_width_get(game);
float scale_y = (float) game_pix_height_get(game) / (float) game_window_pix_height_get(game);
visible_map_mouse_x = (int) ((float) visible_map_mouse_x * scale_x);
visible_map_mouse_y = (int) ((float) visible_map_mouse_y * scale_y);

//
// Now we wait for level_display to find the cursor
//
game_visible_map_mouse_set(game, visible_map_mouse_x, visible_map_mouse_y);
}
132 changes: 87 additions & 45 deletions src/level_display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,14 @@
#include "my_gl.hpp"
#include "my_level.hpp"
#include "my_main.hpp"
#include "my_sdl_event.hpp"
#include "my_tex.hpp"
#include "my_tile.hpp"
#include "my_tp.hpp"

static int onscreen_map_tl_x;
static int onscreen_map_tl_y;
static int onscreen_map_br_x;
static int onscreen_map_br_y;
static int onscreen_map_mouse_x;
static int onscreen_map_mouse_y;
static int visible_map_mouse_x;
static int visible_map_mouse_y;

static void level_display_tile(Levelp l, Tpp tp, uint16_t tile_index, point tl, point br, point offset)
static void level_display_tile_index(Levelp l, Tpp tp, uint16_t tile_index, point tl, point br, point offset)
{
auto tile = tile_index_to_tile(tile_index);
if (! tile) {
Expand All @@ -39,26 +34,23 @@ static void level_display_tile(Levelp l, Tpp tp, uint16_t tile_index, point tl,
}
}

static void level_display_z_layer(Levelp l, int x, int y, int slot, int z, bool deco)
static void level_display_obj(Levelp l, int x, int y, Tpp tp, Thingp t, ThingOrTp *obj, bool deco)
{
int dw = TILE_WIDTH;
int dh = TILE_HEIGHT;

point tl;
point br;

Tpp tp;
auto t = level_thing_or_tp_get(l, x, y, slot, &tp);
if (! tp) {
return;
}
int tile_index;

if (tp_z_depth_get(tp) != z) {
return;
if (obj) {
tile_index = obj->tile;
} else {
Tilep tile = tp_tiles_get(tp, 0);
tile_index = tile_global_index(tile);
}

auto obj = &l->obj[ x ][ y ][ slot ];
auto tile_index = obj->tile;
if (! tile_index) {
return;
}
Expand Down Expand Up @@ -108,12 +100,10 @@ static void level_display_z_layer(Levelp l, int x, int y, int slot, int z, bool
//
// Is the cursor here?
//
if (z == MAP_DEPTH_FLOOR) {
if ((onscreen_map_mouse_x >= tl.x) && (onscreen_map_mouse_x < br.x) && (onscreen_map_mouse_y >= tl.y)
&& (onscreen_map_mouse_y < br.y)) {
CON("CURSOR %d %d %d,%d -> %d,%d", onscreen_map_mouse_x, onscreen_map_mouse_y, tl.x, tl.y, br.x, br.y);
if (tp_is_floor(tp)) {
if ((visible_map_mouse_x >= tl.x) && (visible_map_mouse_x < br.x) && (visible_map_mouse_y >= tl.y)
&& (visible_map_mouse_y < br.y)) {
level_cursor_set(l, x, y, CURSOR_AT);
return;
}
}

Expand All @@ -130,7 +120,69 @@ static void level_display_z_layer(Levelp l, int x, int y, int slot, int z, bool
tile_index += 47;
}

level_display_tile(l, tp, tile_index, tl, br, point(0, 0));
level_display_tile_index(l, tp, tile_index, tl, br, point(0, 0));
}

static void level_display_cursor(Levelp l, int x, int y)
{
Tpp tp = nullptr;

switch (l->cursor[ x ][ y ]) {
case CURSOR_NONE :
//
// Normal case. No cursor or anything else here.
//
return;
case CURSOR_PATH :
{
//
// Cursors do not use up slots on the map, to avoid them interacting with anything
//
static Tpp tp_once;
if (! tp_once) {
tp_once = tp_find("cursor_at");
}
tp = tp_once;
break;
}
case CURSOR_AT :
{
//
// Cursors do not use up slots on the map, to avoid them interacting with anything
//
static Tpp tp_once;
if (! tp_once) {
tp_once = tp_find("cursor_at");
}
tp = tp_once;
break;
}
}

if (tp) {
level_display_obj(l, x, y, tp, NULL_THING, NULL_OBJ, false);
}
}

static void level_display_slot(Levelp l, int x, int y, int slot, int z, bool deco)
{
Tpp tp;
auto t = level_thing_or_tp_get(l, x, y, slot, &tp);
if (! tp) {
return;
}

if (tp_z_depth_get(tp) != z) {
return;
}

ThingOrTp *obj = &l->obj[ x ][ y ][ slot ];
auto tile_index = obj->tile;
if (! tile_index) {
return;
}

level_display_obj(l, x, y, tp, t, obj, deco);
}

void level_display(Levelp l)
Expand All @@ -151,25 +203,15 @@ void level_display(Levelp l)
const bool no_deco = false;

//
// Get the visible map bounds
//
game_onscreen_map_get(game, &onscreen_map_tl_x, &onscreen_map_tl_y, &onscreen_map_br_x, &onscreen_map_br_y);

//
// Find out what pixel on the map the mouse is over
// We need to find out what pixel on the map the mouse is over
//
onscreen_map_mouse_x = sdl.mouse_x - onscreen_map_tl_x;
onscreen_map_mouse_y = sdl.mouse_y;
float scale_x = (float) game_pix_width_get(game) / (float) game_window_pix_width_get(game);
float scale_y = (float) game_pix_height_get(game) / (float) game_window_pix_height_get(game);
onscreen_map_mouse_x = (int) ((float) onscreen_map_mouse_x * scale_x);
onscreen_map_mouse_y = (int) ((float) onscreen_map_mouse_y * scale_y);
game_visible_map_mouse_get(game, &visible_map_mouse_x, &visible_map_mouse_y);

for (auto y = l->miny; y < l->maxy; y++) {
for (auto x = l->maxx - 1; x >= l->minx; x--) {
for (auto slot = 0; slot < MAP_SLOTS; slot++) {
level_display_z_layer(l, x, y, slot, MAP_DEPTH_FLOOR, no_deco);
level_display_z_layer(l, x, y, slot, MAP_DEPTH_WALL, no_deco);
level_display_slot(l, x, y, slot, MAP_DEPTH_FLOOR, no_deco);
level_display_slot(l, x, y, slot, MAP_DEPTH_WALL, no_deco);
}
}
}
Expand All @@ -180,10 +222,10 @@ void level_display(Levelp l)
for (auto y = l->miny; y < l->maxy; y++) {
for (auto x = l->maxx - 1; x >= l->minx; x--) {
for (auto slot = 0; slot < MAP_SLOTS; slot++) {
level_display_z_layer(l, x, y, slot, MAP_DEPTH_DOOR, no_deco);
level_display_z_layer(l, x, y, slot, MAP_DEPTH_OBJ1, no_deco);
level_display_z_layer(l, x, y, slot, MAP_DEPTH_OBJ2, no_deco);
level_display_z_layer(l, x, y, slot, MAP_DEPTH_PLAYER, no_deco);
level_display_slot(l, x, y, slot, MAP_DEPTH_DOOR, no_deco);
level_display_slot(l, x, y, slot, MAP_DEPTH_OBJ1, no_deco);
level_display_slot(l, x, y, slot, MAP_DEPTH_OBJ2, no_deco);
level_display_slot(l, x, y, slot, MAP_DEPTH_PLAYER, no_deco);
}
}
}
Expand All @@ -194,8 +236,8 @@ void level_display(Levelp l)
for (auto y = l->miny; y < l->maxy; y++) {
for (auto x = l->maxx - 1; x >= l->minx; x--) {
for (auto slot = 0; slot < MAP_SLOTS; slot++) {
level_display_z_layer(l, x, y, slot, MAP_DEPTH_WALL, deco);
level_display_z_layer(l, x, y, slot, MAP_DEPTH_DOOR, deco);
level_display_slot(l, x, y, slot, MAP_DEPTH_WALL, deco);
level_display_slot(l, x, y, slot, MAP_DEPTH_DOOR, deco);
}
}
}
Expand All @@ -205,7 +247,7 @@ void level_display(Levelp l)
//
for (auto y = l->miny; y < l->maxy; y++) {
for (auto x = l->maxx - 1; x >= l->minx; x--) {
level_display_z_layer(l, x, y, 0, MAP_DEPTH_CURSOR, no_deco);
level_display_cursor(l, x, y);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/level_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void level_thing_player_create_and_place(Levelp l)
continue;
}

if (! tp_is_player_get(tp)) {
if (! tp_is_player(tp)) {
continue;
}

Expand Down
33 changes: 0 additions & 33 deletions src/level_thing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,39 +34,6 @@ Thingp level_thing_or_tp_get(Levelp l, int x, int y, uint8_t slot, Tpp *out)
}

if (! id) {
if (out) {
switch (l->cursor[ x ][ y ]) {
case CURSOR_NONE :
//
// Normal case. No cursor or anything else here.
//
return nullptr;
case CURSOR_PATH :
{
//
// Cursors do not use up slots on the map, to avoid them interacting with anything
//
static Tpp tp;
if (! tp) {
tp = tp_find("cursor_at");
}
*out = tp;
return nullptr;
}
case CURSOR_AT :
{
//
// Cursors do not use up slots on the map, to avoid them interacting with anything
//
static Tpp tp;
if (! tp) {
tp = tp_find("cursor_at");
}
*out = tp;
return nullptr;
}
}
}
return nullptr;
}

Expand Down
2 changes: 1 addition & 1 deletion src/level_tiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ void level_assign_tiles(Levelp l)
//
// Switch the door direction if next to walls
//
if (tp_is_door_get(tp)) {
if (tp_is_door(tp)) {
if (level_tp_get(l, x, y - 1, MAP_DEPTH_WALL) && level_tp_get(l, x, y + 1, MAP_DEPTH_WALL)) {
block_type = IS_JOIN_TOP;
}
Expand Down
Loading

0 comments on commit 9f11625

Please sign in to comment.