Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Source/controls/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

namespace devilution {

inline int PollEvent(SDL_Event *event)
inline int PollEventCustom(SDL_Event *event, int (*poll)(SDL_Event *event))
{
int result = SDL_PollEvent(event);
int result = poll(event);

Check warning on line 12 in Source/controls/input.h

View workflow job for this annotation

GitHub Actions / tidy-check

Source/controls/input.h:12:2 [misc-const-correctness]

variable 'result' of type 'int' can be declared 'const'
if (result != 0) {
UnlockControllerState(*event);
ProcessControllerMotion(*event);
Expand All @@ -18,4 +18,9 @@
return result;
}

inline int PollEvent(SDL_Event *event)
{
return PollEventCustom(event, SDL_PollEvent);
}

} // namespace devilution
2 changes: 1 addition & 1 deletion Source/controls/touch/renderers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@

void LoadPotionArt(ButtonTexture *potionArt)
{
item_cursor_graphic potionGraphics[] {

Check warning on line 107 in Source/controls/touch/renderers.cpp

View workflow job for this annotation

GitHub Actions / tidy-check

Source/controls/touch/renderers.cpp:107:2 [misc-const-correctness]

variable 'potionGraphics' of type 'item_cursor_graphic[8]' can be declared 'const'
ICURS_POTION_OF_HEALING,
ICURS_POTION_OF_MANA,
ICURS_POTION_OF_REJUVENATION,
Expand All @@ -115,13 +115,13 @@
ICURS_SCROLL_OF
};

int potionFrame = static_cast<int>(CURSOR_FIRSTITEM) + static_cast<int>(ICURS_POTION_OF_HEALING);

Check warning on line 118 in Source/controls/touch/renderers.cpp

View workflow job for this annotation

GitHub Actions / tidy-check

Source/controls/touch/renderers.cpp:118:2 [misc-const-correctness]

variable 'potionFrame' of type 'int' can be declared 'const'
Size potionSize = GetInvItemSize(potionFrame);

Check warning on line 119 in Source/controls/touch/renderers.cpp

View workflow job for this annotation

GitHub Actions / tidy-check

Source/controls/touch/renderers.cpp:119:2 [misc-const-correctness]

variable 'potionSize' of type 'Size' (aka 'SizeOf<int>') can be declared 'const'

auto surface = SDLWrap::CreateRGBSurfaceWithFormat(
/*flags=*/0,
/*width=*/potionSize.width,
/*height=*/potionSize.height * sizeof(potionGraphics),

Check warning on line 124 in Source/controls/touch/renderers.cpp

View workflow job for this annotation

GitHub Actions / tidy-check

Source/controls/touch/renderers.cpp:124:17 [bugprone-narrowing-conversions]

narrowing conversion from 'unsigned long' to signed type 'int' is implementation-defined
/*depth=*/8,
SDL_PIXELFORMAT_INDEX8);

Expand All @@ -129,14 +129,14 @@
if (SDLC_SetSurfaceAndPaletteColors(surface.get(), palette.get(), orig_palette.data(), 0, 256) < 0)
ErrSdl();

Uint32 bgColor = SDL_MapRGB(surface->format, orig_palette[1].r, orig_palette[1].g, orig_palette[1].b);

Check warning on line 132 in Source/controls/touch/renderers.cpp

View workflow job for this annotation

GitHub Actions / tidy-check

Source/controls/touch/renderers.cpp:132:2 [misc-const-correctness]

variable 'bgColor' of type 'Uint32' (aka 'unsigned int') can be declared 'const'
if (SDL_FillRect(surface.get(), nullptr, bgColor) < 0)
ErrSdl();
if (SDL_SetColorKey(surface.get(), SDL_TRUE, bgColor) < 0)
ErrSdl();

Point position { 0, 0 };
for (item_cursor_graphic graphic : potionGraphics) {

Check warning on line 139 in Source/controls/touch/renderers.cpp

View workflow job for this annotation

GitHub Actions / tidy-check

Source/controls/touch/renderers.cpp:139:7 [misc-const-correctness]

variable 'graphic' of type 'item_cursor_graphic' can be declared 'const'
const int cursorID = static_cast<int>(CURSOR_FIRSTITEM) + graphic;
position.y += potionSize.height;
ClxDraw(Surface(surface.get()), position, GetInvItemSprite(cursorID));
Expand All @@ -149,7 +149,7 @@

bool InteractsWithCharButton(Point point)
{
Player &myPlayer = *MyPlayer;

Check warning on line 152 in Source/controls/touch/renderers.cpp

View workflow job for this annotation

GitHub Actions / tidy-check

Source/controls/touch/renderers.cpp:152:2 [misc-const-correctness]

variable 'myPlayer' of type 'Player &' can be declared 'const'
if (myPlayer._pStatPts == 0)
return false;
for (auto attribute : enum_values<CharacterAttribute>()) {
Expand All @@ -169,14 +169,14 @@

Size ButtonTexture::size() const
{
int w, h;

Check warning on line 172 in Source/controls/touch/renderers.cpp

View workflow job for this annotation

GitHub Actions / tidy-check

Source/controls/touch/renderers.cpp:172:2 [readability-isolate-declaration]

multiple declarations in a single statement reduces readability
if (surface != nullptr) {
w = surface->w;
h = surface->h;
} else {
SDL_QueryTexture(texture.get(), /*format=*/nullptr, /*access=*/nullptr, &w, &h);
}
w /= numSprites;

Check warning on line 179 in Source/controls/touch/renderers.cpp

View workflow job for this annotation

GitHub Actions / tidy-check

Source/controls/touch/renderers.cpp:179:7 [bugprone-narrowing-conversions]

narrowing conversion from 'unsigned int' to signed type 'int' is implementation-defined
h /= numFrames;
return Size { w, h };
}
Expand Down Expand Up @@ -235,7 +235,7 @@

void VirtualGamepadRenderer::Render(RenderFunction renderFunction)
{
if (CurrentEventHandler == DisableInputEventHandler)
if (CurrentEventHandler.handle == DisableInputEventHandler)
return;

primaryActionButtonRenderer.Render(renderFunction, buttonArt);
Expand Down
25 changes: 18 additions & 7 deletions Source/diablo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,8 @@ void RunGameLoop(interface_mode uMsg)
nthread_ignore_mutex(true);
StartGame(uMsg);
assert(HeadlessMode || ghMainWnd);
EventHandler previousHandler = SetEventHandler(GameEventHandler);
EventHandler newHandler = { GameEventHandler, SDL_PollEvent };
EventHandler previousHandler = SetEventHandler(newHandler);
run_delta_info();
gbRunGame = true;
gbProcessPlayers = IsDiabloAlive(true);
Expand Down Expand Up @@ -937,7 +938,7 @@ void RunGameLoop(interface_mode uMsg)
RedrawEverything();
scrollrt_draw_game_screen();
previousHandler = SetEventHandler(previousHandler);
assert(HeadlessMode || previousHandler == GameEventHandler);
assert(HeadlessMode || previousHandler.handle == GameEventHandler);
FreeGame();

if (cineflag) {
Expand Down Expand Up @@ -1179,8 +1180,17 @@ void ApplicationInit()
if (*GetOptions().Graphics.showFPS)
EnableFrameCount();

init_create_window();
was_window_init = true;
if (!HeadlessMode) {
init_create_window();
was_window_init = true;
} else {
#ifdef USE_SDL1
// Unfortunately no way to init only events queue for SDL1.2
SDL_Init(SDL_INIT_VIDEO);
#else
SDL_Init(SDL_INIT_EVENTS);
#endif
}

InitializeScreenReader();
LanguageInitialize();
Expand Down Expand Up @@ -1236,9 +1246,10 @@ void DiabloInit()

DiabloInitScreen();

snd_init();

ui_sound_init();
if (!HeadlessMode) {
snd_init();
ui_sound_init();
}

// Item graphics are loaded early, they already get touched during hero selection.
InitItemGFX();
Expand Down
4 changes: 4 additions & 0 deletions Source/effects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,10 @@ void effects_play_sound(SfxID id)

int GetSFXLength(SfxID nSFX)
{
if (!gbSndInited || !gbSoundOn) {
return 0;
}

TSFX &sfx = sgSFX[static_cast<int16_t>(nSFX)];
if (sfx.pSnd == nullptr)
sfx.pSnd = sound_file_load(sfx.pszName.c_str(),
Expand Down
8 changes: 4 additions & 4 deletions Source/engine/demomode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -683,13 +683,13 @@ bool GetRunGameLoop(bool &drawGame, bool &processInput)
return isGameTick;
}

bool FetchMessage(SDL_Event *event, uint16_t *modState)
bool FetchMessage(SDL_Event *event, uint16_t *modState, int (*poll)(SDL_Event *event))
{
if (CurrentEventHandler == DisableInputEventHandler)
if (CurrentEventHandler.handle == DisableInputEventHandler)
return false;

SDL_Event e;
if (SDL_PollEvent(&e) != 0) {
if (poll(&e) != 0) {
if (e.type == SDL_QUIT) {
*event = e;
return true;
Expand Down Expand Up @@ -741,7 +741,7 @@ void RecordMessage(const SDL_Event &event, uint16_t modState)
{
if (!gbRunGame || DemoRecording == nullptr)
return;
if (CurrentEventHandler == DisableInputEventHandler)
if (CurrentEventHandler.handle == DisableInputEventHandler)
return;
switch (event.type) {
case SDL_MOUSEMOTION:
Expand Down
4 changes: 2 additions & 2 deletions Source/engine/demomode.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ bool IsRunning();
bool IsRecording();

bool GetRunGameLoop(bool &drawGame, bool &processInput);
bool FetchMessage(SDL_Event *event, uint16_t *modState);
bool FetchMessage(SDL_Event *event, uint16_t *modState, int (*poll)(SDL_Event *event));
void RecordGameLoopResult(bool runGameLoop);
void RecordMessage(const SDL_Event &event, uint16_t modState);

Expand All @@ -46,7 +46,7 @@ inline bool GetRunGameLoop(bool &, bool &)
{
return false;
}
inline bool FetchMessage(SDL_Event *, uint16_t *)
inline bool FetchMessage(SDL_Event *, uint16_t *, int (*poll)(SDL_Event *event))
{
return false;
}
Expand Down
13 changes: 7 additions & 6 deletions Source/engine/events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ bool FalseAvail(const char *name, int value)
return true;
}

bool FetchMessage_Real(SDL_Event *event, uint16_t *modState)
static bool FetchMessage_Real(SDL_Event *event, uint16_t *modState,
int (*poll)(SDL_Event *event))
{
#ifdef __SWITCH__
HandleDocking();
#endif

SDL_Event e;
if (PollEvent(&e) == 0) {
if (PollEventCustom(&e, poll) == 0) {
return false;
}

Expand Down Expand Up @@ -151,9 +152,9 @@ EventHandler SetEventHandler(EventHandler eventHandler)
return previousHandler;
}

bool FetchMessage(SDL_Event *event, uint16_t *modState)
bool FetchMessage(SDL_Event *event, uint16_t *modState, int (*poll)(SDL_Event *event))
{
const bool available = demo::IsRunning() ? demo::FetchMessage(event, modState) : FetchMessage_Real(event, modState);
const bool available = demo::IsRunning() ? demo::FetchMessage(event, modState, poll) : FetchMessage_Real(event, modState, poll);

if (available && demo::IsRecording())
demo::RecordMessage(*event, *modState);
Expand All @@ -163,9 +164,9 @@ bool FetchMessage(SDL_Event *event, uint16_t *modState)

void HandleMessage(const SDL_Event &event, uint16_t modState)
{
assert(CurrentEventHandler != nullptr);
assert(CurrentEventHandler.handle != nullptr);

CurrentEventHandler(event, modState);
CurrentEventHandler.handle(event, modState);
}

} // namespace devilution
10 changes: 7 additions & 3 deletions Source/engine/events.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@

namespace devilution {

using EventHandler = void (*)(const SDL_Event &event, uint16_t modState);
struct EventHandler {
void (*handle)(const SDL_Event &event, uint16_t modState);
int (*poll)(SDL_Event *event);
};

/** @brief The current input handler function */
extern EventHandler CurrentEventHandler;

EventHandler SetEventHandler(EventHandler NewProc);
EventHandler SetEventHandler(EventHandler NewHandler);

bool FetchMessage(SDL_Event *event, uint16_t *modState);
bool FetchMessage(SDL_Event *event, uint16_t *modState,
int (*poll)(SDL_Event *event) = SDL_PollEvent);

void HandleMessage(const SDL_Event &event, uint16_t modState);

Expand Down
5 changes: 5 additions & 0 deletions Source/engine/sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ int CapVolume(int volume)

void OptionAudioChanged()
{
if (HeadlessMode)
return;
effects_cleanup_sfx();
music_stop();
snd_deinit();
Expand Down Expand Up @@ -289,6 +291,9 @@ void music_start(_music_id nTrack)
{
const char *trackPath;

if (HeadlessMode)
return;

assert(nTrack < NUM_MUSIC);
music_stop();
if (!gbMusicOn)
Expand Down
10 changes: 6 additions & 4 deletions Source/gamemenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,8 @@ void gamemenu_quit_game(bool bActivate)

void gamemenu_load_game(bool /*bActivate*/)
{
EventHandler saveProc = SetEventHandler(DisableInputEventHandler);
EventHandler newHandler = { DisableInputEventHandler, SDL_PollEvent };
EventHandler prevHandler = SetEventHandler(newHandler);
gamemenu_off();
ClearFloatingNumbers();
NewCursor(CURSOR_NONE);
Expand Down Expand Up @@ -321,7 +322,7 @@ void gamemenu_load_game(bool /*bActivate*/)
PaletteFadeIn(8);
NewCursor(CURSOR_HAND);
interface_msg_pump();
SetEventHandler(saveProc);
SetEventHandler(prevHandler);
}

void gamemenu_save_game(bool /*bActivate*/)
Expand All @@ -335,7 +336,8 @@ void gamemenu_save_game(bool /*bActivate*/)
return;
}

EventHandler saveProc = SetEventHandler(DisableInputEventHandler);
EventHandler newHandler = { DisableInputEventHandler, SDL_PollEvent };
EventHandler prevHandler = SetEventHandler(newHandler);
NewCursor(CURSOR_NONE);
gamemenu_off();
InitDiabloMsg(EMSG_SAVING);
Expand All @@ -352,7 +354,7 @@ void gamemenu_save_game(bool /*bActivate*/)
if (!demo::IsRunning()) SaveOptions();
}
interface_msg_pump();
SetEventHandler(saveProc);
SetEventHandler(prevHandler);
}

void gamemenu_on()
Expand Down
4 changes: 3 additions & 1 deletion Source/gmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,10 @@ bool gmenu_presskeys(SDL_Keycode vkey)
GmenuUpDown(true);
break;
default:
break;
// Key was not handled by the gmenu
return false;
}
// Key was handled
return true;
}

Expand Down
3 changes: 3 additions & 0 deletions Source/hwcursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "engine/clx_sprite.hpp"
#include "engine/render/clx_render.hpp"
#include "engine/surface.hpp"
#include "headless_mode.hpp"
#include "utils/display.h"
#include "utils/sdl_bilinear_scale.hpp"
#include "utils/sdl_wrap.h"
Expand Down Expand Up @@ -164,6 +165,8 @@ void SetHardwareCursor(CursorInfo cursorInfo)
#if SDL_VERSION_ATLEAST(2, 0, 0)
CurrentCursorInfo = cursorInfo;
CurrentCursorInfo.setNeedsReinitialization(false);
if (HeadlessMode)
return;
switch (cursorInfo.type()) {
case CursorType::Game:
#if LOG_HWCURSOR
Expand Down
35 changes: 29 additions & 6 deletions Source/interfac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,28 @@ void CheckShouldSkipRendering()
if (!HeadlessMode) InitRendering();
}

static int PeepEvents(SDL_Event *event, unsigned evType)
{
#ifdef USE_SDL1
return SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_EVENTMASK(evType));
#else
return SDL_PeepEvents(event, 1, SDL_GETEVENT, evType, evType + 1);
#endif
}

static int ProgressEventPoll(SDL_Event *event)
{
int ret;

// SDL_QUIT event has higher priority
ret = PeepEvents(event, SDL_QUIT);
if (ret)
return ret;

// Peek only custom events, leaving others in the queue
return PeepEvents(event, CustomEventType);
}

void ProgressEventHandler(const SDL_Event &event, uint16_t modState)
{
DisableInputEventHandler(event, modState);
Expand Down Expand Up @@ -498,8 +520,8 @@ void ProgressEventHandler(const SDL_Event &event, uint16_t modState)
}

[[maybe_unused]] EventHandler prevHandler = SetEventHandler(ProgressEventHandlerState.prevHandler);
assert(prevHandler == ProgressEventHandler);
ProgressEventHandlerState.prevHandler = nullptr;
assert(prevHandler.handle == ProgressEventHandler);
ProgressEventHandlerState.prevHandler.handle = nullptr;
IsProgress = false;

Player &myPlayer = *MyPlayer;
Expand Down Expand Up @@ -554,7 +576,7 @@ void interface_msg_pump()
{
SDL_Event event;
uint16_t modState;
while (FetchMessage(&event, &modState)) {
while (FetchMessage(&event, &modState, CurrentEventHandler.poll)) {
if (event.type != SDL_QUIT) {
HandleMessage(event, modState);
}
Expand Down Expand Up @@ -596,7 +618,8 @@ void ShowProgress(interface_mode uMsg)
gbSomebodyWonGameKludge = false;

ProgressEventHandlerState.loadStartedAt = SDL_GetTicks();
ProgressEventHandlerState.prevHandler = SetEventHandler(ProgressEventHandler);
EventHandler newHandler = { ProgressEventHandler, ProgressEventPoll };
ProgressEventHandlerState.prevHandler = SetEventHandler(newHandler);
ProgressEventHandlerState.skipRendering = true;
ProgressEventHandlerState.done = false;
ProgressEventHandlerState.drawnProgress = 0;
Expand Down Expand Up @@ -650,9 +673,9 @@ void ShowProgress(interface_mode uMsg)
while (true) {
CheckShouldSkipRendering();
SDL_Event event;
// We use the real `PollEvent` here instead of `FetchMessage`
// We use the real `PollEventCustom` here instead of `FetchMessage`
// to process real events rather than the recorded ones in demo mode.
while (PollEvent(&event)) {
while (PollEventCustom(&event, ProgressEventPoll)) {
if (!processEvent(event)) return;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Source/menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ void mainmenu_loop()

do {
_mainmenu_selections menu = MAINMENU_NONE;
if (demo::IsRunning())
if (demo::IsRunning() || HeadlessMode)
menu = MAINMENU_SINGLE_PLAYER;
else if (!UiMainMenuDialog(gszProductName, &menu, 30))
app_fatal(_("Unable to display mainmenu"));
Expand Down
3 changes: 2 additions & 1 deletion Source/minitext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "engine/render/clx_render.hpp"
#include "engine/render/primitive_render.hpp"
#include "engine/render/text_render.hpp"
#include "headless_mode.hpp"
#include "playerdat.hpp"
#include "textdat.h"
#include "utils/language.h"
Expand Down Expand Up @@ -162,7 +163,7 @@ void InitQTextMsg(_speech_id m)
default:
break;
}
if (Speeches[m].scrlltxt) {
if (!HeadlessMode && Speeches[m].scrlltxt) {
QuestLogIsOpen = false;
LoadText(_(Speeches[m].txtstr));
qtextflag = true;
Expand Down
1 change: 1 addition & 0 deletions Source/utils/log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ std::string format(std::string_view fmt, Args &&...args)
SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, "%s", error.c_str());
app_fatal(error);
#endif
return "";
}
}

Expand Down
Loading