Skip to content

Commit

Permalink
wip on cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
goblinhack committed May 11, 2024
1 parent aa51ff1 commit 60017e0
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 69 deletions.
24 changes: 24 additions & 0 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,14 @@ class Game
uint32_t last_mouse_down {};
uint32_t last_pause {};

//
// 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;

/////////////////////////////////////////////////////////////////////////
// not worth saving
// ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
Expand Down Expand Up @@ -442,6 +450,22 @@ 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)
{
*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;
}

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)
{
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;
}

uint32_t game_last_mouse_down_get(class Game *game) { return game->last_mouse_down; }
void game_last_mouse_down_set(class Game *game, uint32_t val) { game->last_mouse_down = val; }

Expand Down
3 changes: 0 additions & 3 deletions src/game_display.cpp

This file was deleted.

30 changes: 30 additions & 0 deletions src/level_cursor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// Copyright Neil McGill, [email protected]
//

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

#include <string.h>

void level_cursor_set(Level *l, int x, int y, int cursor)
{
TRACE_NO_INDENT();

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

void level_cursor_unset(Level *l, int x, int y, int cursor)
{
TRACE_NO_INDENT();

l->cursor[ x ][ y ] = CURSOR_NONE;
}

int level_cursor_get(Level *l, int x, int y)
{
TRACE_NO_INDENT();

return l->cursor[ x ][ y ];
}
116 changes: 81 additions & 35 deletions src/level_display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,19 @@
#include "my_game.hpp"
#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 void level_display_tile(Levelp l, Tpp tp, uint16_t tile_index, point tl, point br, point offset)
{
auto tile = tile_index_to_tile(tile_index);
Expand Down Expand Up @@ -90,9 +99,24 @@ static void level_display_z_layer(Levelp l, int x, int y, int slot, int z, bool
tl.y -= (pix_height - dh) / 2;
}

//
// Update the br coords if we changed the position
//
br.x = tl.x + pix_width;
br.y = tl.y + pix_height;

//
// 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);
level_cursor_set(l, x, y, CURSOR_AT);
return;
}
}

//
// Flippable?
//
Expand All @@ -118,50 +142,72 @@ void level_display(Levelp l)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glcolor(WHITE);

{
int fbo = FBO_MAP;
blit_fbo_bind(fbo);
glClear(GL_COLOR_BUFFER_BIT);
blit_init();

const bool deco = true;
const bool no_deco = false;

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);
}
int fbo = FBO_MAP;
blit_fbo_bind(fbo);
glClear(GL_COLOR_BUFFER_BIT);
blit_init();

const bool deco = true;
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
//
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);

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);
}
}
}

//
// Doors only
//
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);
}
//
// Doors only
//
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);
}
}
}

//
// Shadows
//
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);
}
//
// Shadows
//
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);
}
}
}

//
// Top level cursor
//
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);
}
}

blit_flush();
}
2 changes: 1 addition & 1 deletion src/level_thing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Thingp level_thing_or_tp_get(Levelp l, int x, int y, uint8_t slot, Tpp *out)

if (! id) {
if (out) {
switch (l->cursor_at[ x ][ y ]) {
switch (l->cursor[ x ][ y ]) {
case CURSOR_NONE :
//
// Normal case. No cursor or anything else here.
Expand Down
6 changes: 6 additions & 0 deletions src/my_game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ extern class Game *game;

class HiScores *game_hiscores_get(class Game *);

void game_onscreen_map_get(class Game *, int *onscreen_map_tl_x, int *onscreen_map_tl_y, int *onscreen_map_br_x,
int *onscreen_map_br_y);

void game_onscreen_map_set(class Game *, int onscreen_map_tl_x, int onscreen_map_tl_y, int onscreen_map_br_x,
int onscreen_map_br_y);

uint32_t game_last_mouse_down_get(class Game *);
void game_last_mouse_down_set(class Game *, uint32_t);

Expand Down
17 changes: 12 additions & 5 deletions src/my_level.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ typedef struct Level_ {
//
// What the player is currently highlighting.
//
uint8_t cursor_at[ MAP_WIDTH ][ MAP_HEIGHT ];
uint8_t cursor[ MAP_WIDTH ][ MAP_HEIGHT ];

//
// The current player.
Expand All @@ -146,6 +146,7 @@ typedef struct Level_ {
//////////////////////////////////////////////////////////////
} Level;

// begin sort marker1 {
bool level_is_oob(Levelp, int x, int y);
bool level_is_same_type(Levelp, int x, int y, Tpp);
bool level_set_id(Levelp, int x, int y, uint8_t z, Id);
Expand All @@ -165,11 +166,10 @@ Thingp level_thing_player(Levelp);
Tilep level_get_tile(Levelp, int x, int y, uint8_t z);
Tilep level_get_tile_no_check(Levelp, int x, int y, uint8_t z);
Tpp level_thing_tp(Levelp, Thingp);
Tpp level_tp_get(Levelp, int x, int y, uint8_t slot);
void level_anim(Levelp);
void level_assign_tiles(Levelp);
void level_bounds_set(Levelp);
void level_destructor(Levelp l);
void level_destructor(Levelp);
void level_display(Levelp);
void level_display(Levelp);
void level_dungeon_create_and_place(Levelp);
Expand All @@ -196,8 +196,15 @@ void level_tick_body(Levelp, float dt);
void level_tick_end_requested(Levelp);
void level_tick(Levelp);
void level_tick_time_step(Levelp);
void level_tp_set(Levelp, int x, int y, Tpp);
void level_tp_unset(Levelp, int x, int y, Tpp);
// end sort marker1 }

Tpp level_tp_get(Levelp, int x, int y, uint8_t slot);
void level_tp_set(Levelp, int x, int y, Tpp);
void level_tp_unset(Levelp, int x, int y, Tpp);

int level_cursor_get(Levelp, int x, int y);
void level_cursor_set(Levelp, int x, int y, int);
void level_cursor_unset(Levelp, int x, int y, int);

//
// Works on a copy of the level data, so things can move cells and we never
Expand Down
2 changes: 0 additions & 2 deletions src/my_tp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,6 @@ Tpp tp_random_unused195(void);
Tpp tp_random_unused196(void);
Tpp tp_random_unused197(void);
Tpp tp_random_unused198(void);
Tpp tp_random_is_cursor_at(void);
Tpp tp_random_is_cursor_path(void);

void con(Tpp, const char *fmt, ...) CHECK_FORMAT_STR(printf, 2, 3);
void con_(Tpp, const char *fmt, va_list args); // compile error without
Expand Down
14 changes: 11 additions & 3 deletions src/sdl_display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,26 @@ void sdl_display(void)
// Blit the game map in the middle of the screen as a square
//
glBlendFunc(GL_ONE, GL_ZERO);
const bool blit_centered_map = true;

const bool blit_centered_map = true;
if (blit_centered_map) {
float x_offset
= (float) game_pix_width_get(game) - (UI_RIGHTBAR_WIDTH * UI_FONT_WIDTH) - game_pix_height_get(game);
x_offset /= (float) game_pix_width_get(game);
x_offset *= (float) game_window_pix_width_get(game);
x_offset = floor(x_offset);

int onscreen_map_tl_x = x_offset;
int onscreen_map_tl_y = 0;
int onscreen_map_br_x = game_window_pix_height_get(game) + x_offset;
int onscreen_map_br_y = game_window_pix_height_get(game);

game_onscreen_map_set(game, onscreen_map_tl_x, onscreen_map_tl_y, onscreen_map_br_x, onscreen_map_br_y);

blit_init();
blit(fbo_tex_id[ FBO_MAP ], 0.0, 1.0, 1.0, 0.0, x_offset, 0, game_window_pix_height_get(game) + x_offset,
game_window_pix_height_get(game));
blit(fbo_tex_id[ FBO_MAP ], 0.0, 1.0, 1.0, 0.0, onscreen_map_tl_x, onscreen_map_tl_y, onscreen_map_br_x,
onscreen_map_br_y);

blit_flush();
} else {
blit_fbo_window_pix(FBO_MAP);
Expand Down
20 changes: 0 additions & 20 deletions src/tp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3556,26 +3556,6 @@ Tpp tp_random_unused198(void)
return tp_get_with_no_rarity_filter(tp_unused198);
}

Tpp tp_random_is_cursor_at(void)
{
TRACE_NO_INDENT();
if (unlikely(! tp_is_cursor_at.size())) {
DIE("No is_cursor_at found");
return nullptr;
}
return tp_get_with_no_rarity_filter(tp_is_cursor_at);
}

Tpp tp_random_is_cursor_path(void)
{
TRACE_NO_INDENT();
if (unlikely(! tp_is_cursor_path.size())) {
DIE("No is_cursor_path found");
return nullptr;
}
return tp_get_with_no_rarity_filter(tp_is_cursor_path);
}

Tpp tp_random_key(void)
{
TRACE_NO_INDENT();
Expand Down

0 comments on commit 60017e0

Please sign in to comment.