; screensaver sample for hsp3dish. ; fork arusave.hsp ; ; hsp3dish を使ってスクリーンセーバが作れるか実験。その2 ; ; hsp3dish を使ってスクリーンセーバを作っても、 ; キー押し、マウスカーソル移動で終了してくれないので ; スクリーンセーバにはならなかったが、 ; 自前でキー押しやマウスカーソル移動を判定して ; 自分で勝手に終了すれば上手くいくのではと思いついたので試してみた。 ; また、多重起動禁止も盛り込んでおく。 ; Windowsのスクリーンセーバは何重にも起動される場面があるらしいので…。 ; ; しかし、プレビュー表示が正常に表示できなかった。実験失敗。 ; ; 2023/11/30, Windows10 x64 22H2 + HSP 3.7 beta 7 #include "hsp3dish.as" #include "d3m.hsp" ; 以下の行のコメントアウトを外すとスクリーンセーバとして実行ファイルを作成する ; #define SCREENSAVER_ENABLE ; 自前でキー押しやマウスカーソル移動を判定して終了する (0 or 1) #define MY_EXIT_JOB 1 ; FPS表示 (0 or 1) #define FPS_DISP_ENABLE 1 #define TITLE_STR "Screensaver sample for hsp3dish" #define VER_STR "0.0.2" ; 多重起動チェック用 Mutex Object 名を定義。アプリ固有の名前であること。 #define MUTEX_NAME "SSStar_HSP3DISH_Mutex" #packopt name "ssstarh3d" ; filename #ifdef SCREENSAVER_ENABLE #packopt type 2 ; generate ".scr" #else #packopt type 0 ; generate ".exe" #endif #packopt xsize 640 ; 設定画面横幅 #packopt ysize 360 ; 設定画面縦幅 ; 同梱する画像ファイル #define TEX_FILE "star.png" #define PREVIEWFILE "preview.png" #pack TEX_FILE #pack PREVIEWFILE #define KB_ESC $000080 #define FRAMERATE 60 ; ---------------------------------------- ; アプリの起動チェックを行うモジュール #module #uselib "kernel32.dll" #cfunc CreateMutex "CreateMutexA" int, int, sptr #cfunc GetLastError "GetLastError" #func CloseHandle "CloseHandle" int #define ERROR_ALREADY_EXISTS 183 ; アプリがすでに起動されているか取得する関数 #defcfunc AlreadyAppRunning str name if (hMutex == 0) { hMutex = CreateMutex(0, 0, name) ; 名前付き Mutex Object 作成 ; オブジェクトがすでに作成されていたかどうかの判別 if (GetLastError() == ERROR_ALREADY_EXISTS) { ; すでに同じ名前のオブジェクトが存在する alreadyRunning = 1 } else { ; オブジェクトが新しく作成された alreadyRunning = 0 } } return alreadyRunning ; クリーンアップ処理(終了時に自動実行) #deffunc CleanupAppRunChecker onexit if (hMutex != 0) { ; ミューテックスオブジェクトハンドルのクローズ CloseHandle hMutex hMutex = 0 } return ; ---------------------------------------- #global ; 多重起動チェック if (AlreadyAppRunning(MUTEX_NAME)) { ; 既に起動しているので終了 end } fps_display_enable = FPS_DISP_ENABLE ; ---------------------------------------- ; parse command line option s = dir_cmdline ; get command line option i = instr(s, 0, "/") ; "/" の出現位置を検索 if i = -1 { ; not option #ifdef SCREENSAVER_ENABLE goto *config_mode #else goto *fullscreen_mode #endif ; goto *preview_mode } a = strmid(s, i, 2) ; 2文字だけ取り出し if a == "/p" or a == "/P" : goto *preview_mode ; プレビュー if a == "/s" or a == "/S" : goto *fullscreen_mode ; フルスクリーン if a == "/c" or a == "/C" : goto *config_mode ; コンフィグ ; コマンドラインがどれでもない時はエラー表示 dialog "Unknown switch [" + s + "]" end *config_mode ; 設定画面 screen 0, 640, 360, 0 gsel 0 cls tt = "Config - " + TITLE_STR + " " + VER_STR title tt font "Tahoma", 24, 1 redraw 0 color 0, 0, 0 pos 20, 20 mes "Config - " + TITLE_STR pos 20, 50 mes "Version : " + VER_STR pos 520, 300 button "OK", *ssend redraw 1 stop *ssend end *preview_mode ; プレビュー画面 ; スクリーンセーバー選択時の、小さな画面内でのプレビューの処理。 ; HSPのWindow ID0がプレビュー画面に合わせて小さくなっているので、 ; ginfo_winx, ginfo_winy で画面サイズを取得後、その中に描画する。 ; 経験上、単に画像を表示してstopしたほうが不具合が起きにくい。 wdw_w = ginfo_winx wdw_h = ginfo_winy ; load preview image imgid = 3 w = 152 h = 112 buffer imgid, w, h picload PREVIEWFILE iw = ginfo_winx ih = ginfo_winy ; draw gsel 0 redraw 0 cls 4 gmode gmode_gdi, iw, ih gzoom wdw_w, wdw_h, imgid, 0, 0, iw, ih, 1 redraw 1 stop ; ---------------------------------------- *fullscreen_mode ; フルスクリーン描画処理 onexit *jobend if MY_EXIT_JOB { ; キーを押したりマウスクリックしたら終了するようにしてみる onkey *jobend onclick *jobend } ; get desktop size dispw = ginfo_dispx disph = ginfo_dispy ; Windows ID=2 を新規作成しようとしたが hsp3dish 利用時はエラーになった ; bgscr 2, 640, 480, screen_hide, 0, 0, 640, 480 ; initialize window without frame ; bgscr WindowID, width, height, mode, x, y, width, height bgscr 0, dispw, disph, 0, 0, 0, dispw, disph ; load image imgid = 3 celload TEX_FILE, imgid ; split image. set orign celdiv imgid, 512, 512, 256, 256 ; Set the buffer to draw from. Windows ID 0 gsel 0, 1 ; Set screen clear color to be performed when redraw 0 is called. ; 0 or 1, color ($000000 - $ffffff) ; setcls CLSMODE_SOLID, $4080c0 setcls CLSMODE_SOLID, $000000 font "Tahoma", 24, 1 randomize star_size = 0.75 dist = 150.0 ; screen distabce area_z = 5000 zmax = double(area_z) + dist ; rnd() only accepts 1 to 32768. ; adjust the value to stay within that range. area_m = 4.0 area_w = int((double(dispw) * zmax) / dist / area_m) area_h = int((double(disph) * zmax) / dist / area_m) objmax = 512 ; initialize stars work ddim posx, objmax ; position x ddim posy, objmax ; position y ddim posz, objmax ; position z dim px, objmax ; display position x dim py, objmax ; display position y ddim scalev, objmax ; scale dim alpha, obj_max ; alpha value dim patidx, obj_max ; pattern index repeat objmax gosub *get_newpos_xy posx(cnt) = nx posy(cnt) = ny patidx(cnt) = idx posz(cnt) = double(rnd(area_z)) + dist px(cnt) = 0 py(cnt) = 0 scale(cnt) = 0.0 alpha(cnt) = 0 loop time_start = d3timer() msx = ginfo_mx msy = ginfo_my ; ---------------------------------------- *mainloop tm = d3timer() - time_start fps = d3getfps() ; get FPS stick key, 0 ; check keyboard if key & KB_ESC : goto *jobend ; ESC key to exit if MY_EXIT_JOB { ; マウスカーソルを動かしたら終了するようにしてみる d = 32 mx = ginfo_mx - msx my = ginfo_my - msy if ((mx * mx) + (my * my)) >= (d * d) : goto *jobend msx = ginfo_mx msy = ginfo_my } ; move stars spd = 40.0 repeat objmax posz(cnt) -= spd if posz(cnt) < (dist / 2.0) { posz(cnt) += zmax gosub *get_newpos_xy posx(cnt) = nx posy(cnt) = ny patidx(cnt) = idx } ; get display x, y position px(cnt) = int(posx(cnt) * dist / posz(cnt)) + (dispw / 2) py(cnt) = int(posy(cnt) * dist / posz(cnt)) + (disph / 2) scalev(cnt) = star_size * dist / posz(cnt) size = int(512.0 * scalev(cnt)) x0 = px(cnt) - (size / 2) y0 = py(cnt) - (size / 2) x1 = px(cnt) + (size / 2) y1 = py(cnt) + (size / 2) if x1 < 0 or x0 > dispw or y1 < 0 or y0 > disph { ; out of display area. position reset posz(cnt) += zmax gosub *get_newpos_xy posx(cnt) = nx posy(cnt) = ny patidx(cnt) = idx px(cnt) = int(posx(cnt) * dist / posz(cnt)) + (dispw / 2) py(cnt) = int(posy(cnt) * dist / posz(cnt)) + (disph / 2) scalev(cnt) = star_size * dist / posz(cnt) } ; get alpha value az = double(area_z) * 0.7 if posz(cnt) < az { alpha(cnt) = 255 } else { ; fade in z = posz(cnt) if z > zmax : z = zmax v0 = z - az v1 = zmax - az alpha(cnt) = int(255.0 - (255.0 * v0 / v1)) } loop ; ---------------------------------------- ; draw start redraw 0 ; clear screen ; color 64, 128, 192 ; boxf ; draw stars repeat objmax rot = 0.0 idx = 0 ; draw image by celput gmode gmode_add, 512, 512, alpha(cnt) pos px(cnt), py(cnt) rot = 0.0 idx = patidx(cnt) celput imgid, idx, scalev(cnt), scalev(cnt), rot loop ; draw text if fps_display_enable { gmode gmode_rgb0 color 255, 255, 255 pos 10, 10 mes strf("[%dx%d] %d/%dFPS", dispw, disph, fps, FRAMERATE) } ; draw end. screen refersh redraw 1 await (1000 / FRAMERATE) ; await 12 goto *mainloop *jobend: end *get_newpos_xy nx = double(rnd(area_w) - (area_w / 2)) * area_m ny = double(rnd(area_h) - (area_h / 2)) * area_m idx = rnd(4) return