From 56cac262069d341f428b0ddec61c1c6fdca6479f Mon Sep 17 00:00:00 2001 From: Ondrej Novak Date: Sun, 23 Feb 2025 14:00:55 +0100 Subject: [PATCH] fix manabatery, fix timer clock skew, check queue when mouse move, wasd control --- CMakeLists.txt | 2 +- game/automap.c | 14 ++++++++++---- game/builder.c | 2 -- game/enemy.c | 4 ++-- game/engine1.c | 4 +--- game/gamesave.c | 26 ++++++++++++++++++++------ game/kouzla.c | 10 ++++------ game/realgame.c | 7 +++++++ game/skeldal.c | 8 ++++---- game/souboje.c | 12 +++++++++--- libs/bmouse.c | 14 ++++++++++++++ libs/bmouse.h | 1 + libs/logfile.h | 4 ++++ libs/memman.c | 2 +- platform/error.cpp | 17 +++++++++++++---- platform/sdl/sdl_context.cpp | 33 ++++++++++----------------------- platform/sdl/sdl_context.h | 2 +- 17 files changed, 102 insertions(+), 60 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f937be8..e270ac8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ project(skeldal) find_package(SDL2 REQUIRED) if (MSVC) - add_compile_options(/W4 /EHsc /DNOMINMAX /J) + add_compile_options(/W4 /EHsc /DNOMINMAX /J /DLOGFILE) set(STANDARD_LIBRARIES "") else() add_compile_options(-Wall -Wextra -Werror -Wno-unused-result -Wno-unused-parameter -Wno-unused-value -Wno-extern-c-compat -funsigned-char) diff --git a/game/automap.c b/game/automap.c index 93c4045..1dbb9c0 100644 --- a/game/automap.c +++ b/game/automap.c @@ -653,12 +653,18 @@ void map_keyboard(EVENT_MSG *msg,void **usr) c=d>>8; switch (c) { - case 'H':yr++;break; - case 'P':yr--;break; - case 'M':xr--;break; - case 'K':xr++;break; + case 17: + case 'H':yr--;break; + case 31: + case 'P':yr++;break; + case 32: + case 'M':xr++;break; + case 30: + case 'K':xr--;break; + case 16: case 'Q': case 's':if (check_for_layer(cur_depth-1)) cur_depth--;break; + case 18: case 'I': case 't':if (check_for_layer(cur_depth+1)) cur_depth++;break; case 15: diff --git a/game/builder.c b/game/builder.c index 81feb16..aa8d359 100644 --- a/game/builder.c +++ b/game/builder.c @@ -1261,8 +1261,6 @@ void render_scene(int sector, int smer) draw_spectxtrs(s,-j,i); } } - do_events(); - if (cancel_render) return; } calc_spectxtrs(); if (lodka) { diff --git a/game/enemy.c b/game/enemy.c index 70cc346..5a48723 100644 --- a/game/enemy.c +++ b/game/enemy.c @@ -2185,10 +2185,10 @@ static void knock_mob_back(TMOB *mm,int dir) if (itnum == 0) return; const TITEM *it = glob_items + itnum-1; if (it->druh != TYP_UTOC) return; - int vls[] = {VLS_MGSIL_H,VLS_MGSIL_L,VLS_UTOK_H,VLS_UTOK_L,VLS_DAMAGE}; + int vls[] = {VLS_UTOK_H,VLS_UTOK_L,VLS_DAMAGE}; for (size_t i = 0; i < countof(vls); ++i) { vlastnosti[vls[i]] -= it->zmeny[vls[i]]; - } + } } diff --git a/game/engine1.c b/game/engine1.c index 0755a15..b20b9d8 100644 --- a/game/engine1.c +++ b/game/engine1.c @@ -473,7 +473,6 @@ static void zooming_forward_backward(const word *background,char back) //phase=(float)sin(3.14159265*0.5f*phase); if (back) phase=-phase; DxZoomWalk(buffer, SCREEN_OFFLINE, tpoints,phase, NULL); - do_events(); } while (curtime= item_count) { + free(new_item_map[i]); + new_item_map[i] = map_items[i]; + map_items[i] = NULL; + break; + } + ++v; } } + for(i=0;imana-=k->mge; p->exp+=k->mge; check_player_new_level(p); - if (p->mana>p->mana_battery) - { - if (p->mana_battery>=0)p->mana=p->mana_battery; - else - p->mana_battery=32767; - } + if (p->mana>p->mana_battery) { + p->mana=p->mana_battery; + } + p->mana_battery=32767; end: GlobEvent(MAGLOB_AFTERMAGIC,p->sektor,p->direction); } diff --git a/game/realgame.c b/game/realgame.c index b110259..63c2fa7 100644 --- a/game/realgame.c +++ b/game/realgame.c @@ -1920,8 +1920,14 @@ void game_keyboard(EVENT_MSG *msg,void **usr) while (_bios_keybrd(_KEYBRD_READY) ) _bios_keybrd(_KEYBRD_READ); switch (c >> 8) { + case 17: case 'H':step_zoom(0);break; + case 31: case 'P':step_zoom(2);break; + case 30: step_zoom(3);break; + case 32: step_zoom(1);break; + case 16: turn_zoom(-1);break; + case 18: turn_zoom(1);break; case 'M':if (get_control_key_state() & 0x80) step_zoom(1); else @@ -1950,6 +1956,7 @@ void game_keyboard(EVENT_MSG *msg,void **usr) case 1:konec(0,0,0,0,0);break; // case 25:GamePause();break; case 28:enforce_start_battle();break; + case 45: case 82:group_all();break; case '<':if (!battle && GlobEvent(MAGLOB_CLICKSAVE,viewsector,viewdir)) {unwire_proc();cancel_render=1;do_save_dialog();wire_proc();}break; diff --git a/game/skeldal.c b/game/skeldal.c index aab3c21..8708f7f 100644 --- a/game/skeldal.c +++ b/game/skeldal.c @@ -440,9 +440,8 @@ void mouse_set_cursor(int cursor) { alock(cursor); schovej_mysku(); - register_ms_cursor(ablock(cursor)); + nastav_mysku_kurzor(ablock(cursor),0,0); last_ms_cursor=cursor; - set_ms_finger(0,0); ukaz_mysku(); } else @@ -454,8 +453,8 @@ void mouse_set_cursor(int cursor) p=(char *)ablock(cursor/18+ikon_libs); memcpy(ms_item,&p[(cursor%18)*IT_ICONE_SIZE],IT_ICONE_SIZE); schovej_mysku(); - register_ms_cursor(ms_item); - set_ms_finger(45/2,55/2); + nastav_mysku_kurzor(ms_item,45/2,55/2); + last_ms_cursor=-cursor; ukaz_mysku(); } @@ -692,6 +691,7 @@ void user_timer(EVENT_MSG *msg,void **usr) x=get_timer_value(); x-=lastvalue; lastvalue+=x; + x = MIN(TIMERSPEED, x); //prevent clock skew if (x) send_message(E_TIMER,x); } } diff --git a/game/souboje.c b/game/souboje.c index 5c3c57b..fce2447 100644 --- a/game/souboje.c +++ b/game/souboje.c @@ -1183,10 +1183,11 @@ void pouzij_zbran(THUMAN *p,int ruka) } vybrana_zbran=itm; bonus=vypocti_bonus(p,itm); - if (itm>0) + if (itm>0) { memcpy(&p->vlastnosti[VLS_MGSIL_L],&glob_items[itm-1].zmeny[VLS_MGSIL_L],3*sizeof(short)); - else + } else { memset(&p->vlastnosti[VLS_MGSIL_L],0,3*sizeof(short)); + } if (!itm || (it=&glob_items[itm-1])->druh==TYP_UTOC) { TMOB *m;int mm,chaos; @@ -1304,7 +1305,7 @@ void jadro_souboje(EVENT_MSG *msg,void **unused) //!!!! Jadro souboje if(nxt==-255) { int i; - SEND_LOG("(BATTLE) Ending round...(nxt=%d,mob=%s)",nxt,mobs[nxt].name); +// SEND_LOG("(BATTLE) Ending round...(nxt=%d,mob=%s)",nxt,mobs[nxt].name); delete_from_timer(TM_SCENE); add_to_timer(TM_SCENE,gamespeed,-1,refresh_scene); for(i=0;i':game_setup(0,0,0,0,0);break; case 57:souboje_dalsi();bott_draw(1);break; case 15: + case 28:zahajit_kolo(0); + souboje_stisknout(AC_START); + break; case 50: if (GlobEvent(MAGLOB_BEFOREMAPOPEN,viewsector,viewdir)) show_automap(1); diff --git a/libs/bmouse.c b/libs/bmouse.c index 4caab07..5cc2c72 100644 --- a/libs/bmouse.c +++ b/libs/bmouse.c @@ -33,6 +33,17 @@ void ukaz_mysku() } } +void nastav_mysku_kurzor(const void *mscursor, int finger_x, int finger_y) { + register_ms_cursor(mscursor); + h_x = finger_x; + h_y = finger_y; +#ifndef FORCE_SOFTWARE_CURSOR + cur_hw_mouse = mscursor; + game_display_show_mouse(mscursor,h_x, h_y); +#endif + +} + void schovej_mysku() { #ifdef FORCE_SOFTWARE_CURSOR @@ -107,6 +118,9 @@ void set_ms_finger(int x,int y) { h_x=x; h_y=y; + #ifndef FORCE_SOFTWARE_CURSOR + game_display_show_mouse(cur_hw_mouse,h_x, h_y); + #endif } diff --git a/libs/bmouse.h b/libs/bmouse.h index 9798ece..76eb47b 100644 --- a/libs/bmouse.h +++ b/libs/bmouse.h @@ -12,6 +12,7 @@ extern MS_EVENT ms_last_event; short init_mysky(void); short done_mysky(void); void ukaz_mysku(void); +void nastav_mysku_kurzor(const void *mscursor, int finger_x, int finger_y); void schovej_mysku(void); void zobraz_mysku(void); void set_ms_finger(int x,int y); diff --git a/libs/logfile.h b/libs/logfile.h index dafcb05..f2fa1e4 100644 --- a/libs/logfile.h +++ b/libs/logfile.h @@ -5,6 +5,10 @@ extern "C" { #endif +#ifdef _MSC_VER +#define __attribute__(...) +#endif + #ifdef LOGFILE void send_log_impl(const char *format, ...) __attribute__((format(printf, 1, 2))); #define SEND_LOG(...) send_log_impl( __VA_ARGS__) diff --git a/libs/memman.c b/libs/memman.c index 4edb283..e064379 100644 --- a/libs/memman.c +++ b/libs/memman.c @@ -705,7 +705,7 @@ void *grealloc(void *p,int32_t size) q=realloc(p,size); if (q!=NULL) { - SEND_LOG("(ALLOC) **** Realloc: New %p size %d\n",q,*((int32_t *)q-1)); +// SEND_LOG("(ALLOC) **** Realloc: New %p size %d\n",q,*((int32_t *)q-1)); return q; } q=getmem(size); diff --git a/platform/error.cpp b/platform/error.cpp index c3aeecb..f13803f 100644 --- a/platform/error.cpp +++ b/platform/error.cpp @@ -7,7 +7,10 @@ #include "error.h" #include "platform.h" - +#ifdef _WIN32 +#define WIN32_LEAN_AND_MEAN +#include +#endif static std::uint32_t gtick = get_game_tick_count(); @@ -15,13 +18,19 @@ void send_log_impl(const char *format, ...) { va_list args; int task = q_current_task(); char buff2[1000]; - va_start(args, format); + char buff[1000]; + va_start(args, format); auto reltik = get_game_tick_count() - gtick; double sec = reltik * 0.001; - std::cerr << sec << "[" << task << "]"; vsnprintf(buff2,1000,format, args); - std::cerr << buff2 << std::endl; va_end(args); + snprintf(buff, sizeof(buff), "%f [%d] %s\r\n", sec, task, buff2); + #ifdef _WIN32 + OutputDebugStringA(buff); + #else + std::cerr << buff; + #endif + } diff --git a/platform/sdl/sdl_context.cpp b/platform/sdl/sdl_context.cpp index 25e6ce0..16d3926 100644 --- a/platform/sdl/sdl_context.cpp +++ b/platform/sdl/sdl_context.cpp @@ -298,14 +298,8 @@ void SDLContext::event_loop(std::stop_token stp) { if (_mouse) { _mouse_rect.x = e.motion.x; _mouse_rect.y = e.motion.y; - bool do_update = false; - { - std::lock_guard _(_mx); - do_update = _display_update_queue.empty(); - } - if (do_update) { - refresh_screen(); - } + update_screen(true); + } } else if (e.type == SDL_MOUSEBUTTONDOWN || e.type == SDL_MOUSEBUTTONUP) { int button = e.button.button; @@ -357,7 +351,6 @@ void SDLContext::show_slide_transition(const SDL_Rect &visible_from, const SDL_Rect &visible_where, const SDL_Rect &hidden_from, const SDL_Rect &hidden_where) { std::lock_guard _(_mx); - signal_push(); push_item(DisplayRequest::slide_transition); push_item(SlideTransitionReq{visible_from, visible_where,hidden_from, hidden_where}); } @@ -435,10 +428,13 @@ void SDLContext::refresh_screen() { } -void SDLContext::update_screen() { +void SDLContext::update_screen(bool force_refresh) { { std::lock_guard _(_mx); - if (_display_update_queue.empty()) return; + if (_display_update_queue.empty()) { + if (force_refresh) refresh_screen(); + return; + } QueueIter iter = _display_update_queue.data(); QueueIter end = iter + _display_update_queue.size(); while (iter != end) { @@ -577,16 +573,18 @@ void SDLContext::update_zindex() { } template requires(std::is_trivially_copy_constructible_v) -void SDLContext::push_item(const T &item) { +void SDLContext::push_item(const T &item) { auto b = reinterpret_cast(&item); auto e = b + sizeof(T); auto sz = _display_update_queue.size(); + if (sz == 0) signal_push(); _display_update_queue.resize(sz + sizeof(T)); std::copy(b, e, _display_update_queue.begin()+sz); } void SDLContext::push_item(const std::string_view &item) { auto sz = _display_update_queue.size(); + if (sz == 0) signal_push(); _display_update_queue.resize(sz + item.size()); std::copy(item.begin(), item.end(), _display_update_queue.begin()+sz); } @@ -620,20 +618,17 @@ std::string_view SDLContext::pop_data(QueueIter &iter, std::size_t size) { void SDLContext::swap_render_buffers() { std::lock_guard _(_mx); - signal_push(); push_item(DisplayRequest::swap_render_buffers); } void SDLContext::swap_display_buffers() { std::lock_guard _(_mx); - signal_push(); push_item(DisplayRequest::swap_visible_buffers); } void SDLContext::show_blend_transition(const SDL_Rect &wrkarea, const SDL_Rect &prev, const SDL_Rect &next, float phase) { std::lock_guard _(_mx); - signal_push(); push_item(DisplayRequest::blend_transition); push_item(BlendTransitionReq{wrkarea, prev, next, phase}); } @@ -780,7 +775,6 @@ void SDLContext::push_hi_image(const unsigned short *image) { void SDLContext::show_mouse_cursor(const unsigned short *ms_hi_format, SDL_Point finger) { std::lock_guard _(_mx); - signal_push(); push_item(DisplayRequest::show_mouse_cursor); push_hi_image(ms_hi_format); _mouse_finger = finger; @@ -788,14 +782,12 @@ void SDLContext::show_mouse_cursor(const unsigned short *ms_hi_format, SDL_Point void SDLContext::hide_mouse_cursor() { std::lock_guard _(_mx); - signal_push(); push_item(DisplayRequest::hide_mouse_cursor); } void SDLContext::load_sprite(int sprite_id, const unsigned short *hi_image) { std::lock_guard _(_mx); - signal_push(); push_item(DisplayRequest::sprite_load); push_item(sprite_id); push_hi_image(hi_image); @@ -804,7 +796,6 @@ void SDLContext::load_sprite(int sprite_id, const unsigned short *hi_image) { void SDLContext::place_sprite(int sprite_id, int x, int y) { std::lock_guard _(_mx); - signal_push(); push_item(DisplayRequest::sprite_place); push_item(sprite_id); SDL_Point pt{x,y}; @@ -814,7 +805,6 @@ void SDLContext::place_sprite(int sprite_id, int x, int y) { void SDLContext::scale_sprite(int sprite_id, int x, int y, int w, int h) { std::lock_guard _(_mx); - signal_push(); push_item(DisplayRequest::sprite_scale); push_item(sprite_id); SDL_Rect pt{x,y,w,h}; @@ -823,14 +813,12 @@ void SDLContext::scale_sprite(int sprite_id, int x, int y, int w, int h) { void SDLContext::hide_sprite(int sprite_id) { std::lock_guard _(_mx); - signal_push(); push_item(DisplayRequest::sprite_hide); push_item(sprite_id); } void SDLContext::sprite_set_zindex(int sprite_id, int zindex) { std::lock_guard _(_mx); - signal_push(); push_item(DisplayRequest::sprite_hide); push_item(sprite_id); push_item(zindex); @@ -838,7 +826,6 @@ void SDLContext::sprite_set_zindex(int sprite_id, int zindex) { void SDLContext::unload_sprite(int sprite) { std::lock_guard _(_mx); - signal_push(); push_item(DisplayRequest::sprite_unload); push_item(sprite); } diff --git a/platform/sdl/sdl_context.h b/platform/sdl/sdl_context.h index cdfbb7e..996037b 100644 --- a/platform/sdl/sdl_context.h +++ b/platform/sdl/sdl_context.h @@ -201,7 +201,7 @@ protected: void event_loop(std::stop_token stp); - void update_screen(); + void update_screen(bool force_refresh = false); template