1 /* Copyright (C) 2001 by Alex Kompel <shurikk@pacbell.net> */
2 // winhack.cpp : Defines the entry point for the application.
14 #define SHARED_DCL extern
17 SHARED_DCL char orgdir[PATHLEN]; /* also used in pcsys.c, amidos.c */
19 extern void FDECL(nethack_exit,(int));
20 static TCHAR* _get_cmd_arg(TCHAR* pCmdLine);
23 NHWinApp _nethack_app;
25 // Foward declarations of functions included in this code module:
26 BOOL InitInstance(HINSTANCE, int);
28 static void win_hack_init(int, char **);
29 static void __cdecl mswin_moveloop(void *);
30 static BOOL setMapTiles(const char* fname);
32 extern void FDECL(pcmain, (int,char **));
34 #define MAX_CMDLINE_PARAM 255
36 int APIENTRY WinMain(HINSTANCE hInstance,
37 HINSTANCE hPrevInstance,
41 INITCOMMONCONTROLSEX InitCtrls;
44 char* argv[MAX_CMDLINE_PARAM];
47 TCHAR wbuf[NHSTR_BUFSIZE];
48 char buf[NHSTR_BUFSIZE];
50 /* ensure that we don't access violate on a panic() */
51 windowprocs.win_raw_print = mswin_raw_print;
52 windowprocs.win_raw_print_bold = mswin_raw_print_bold;
54 /* init applicatio structure */
55 _nethack_app.hApp = hInstance;
56 _nethack_app.nCmdShow = nCmdShow;
57 _nethack_app.hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_WINHACK);
58 _nethack_app.hMainWnd = NULL;
59 _nethack_app.hPopupWnd = NULL;
60 _nethack_app.hMenuBar = NULL;
61 _nethack_app.bmpTiles = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_TILES));
62 if( _nethack_app.bmpTiles==NULL ) panic("cannot load tiles bitmap");
63 _nethack_app.bmpPetMark = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_PETMARK));
64 if( _nethack_app.bmpPetMark==NULL ) panic("cannot load pet mark bitmap");
65 _nethack_app.bmpMapTiles = _nethack_app.bmpTiles;
66 _nethack_app.mapTile_X = TILE_X;
67 _nethack_app.mapTile_Y = TILE_Y;
68 _nethack_app.mapTilesPerLine = TILES_PER_LINE;
69 _nethack_app.bNoHScroll = FALSE;
70 _nethack_app.bNoVScroll = FALSE;
72 #if defined(WIN_CE_PS2xx) || defined(WIN_CE_POCKETPC) || defined(WIN_CE_SMARTPHONE)
73 _nethack_app.bCmdPad = TRUE;
75 _nethack_app.bCmdPad = FALSE;
78 _nethack_app.bWrapText = TRUE;
79 _nethack_app.bFullScreen = TRUE;
81 #if defined(WIN_CE_SMARTPHONE)
82 _nethack_app.bHideScrollBars = TRUE;
84 _nethack_app.bHideScrollBars = FALSE;
87 _nethack_app.bUseSIP = TRUE;
89 // check for running nethack programs
90 nethackWnd = FindWindow(szMainWindowClass, NULL);
93 SetForegroundWindow(nethackWnd);
98 ZeroMemory(&InitCtrls, sizeof(InitCtrls));
99 InitCtrls.dwSize = sizeof(InitCtrls);
100 InitCtrls.dwICC = ICC_LISTVIEW_CLASSES;
101 if( !InitCommonControlsEx(&InitCtrls) ) {
102 MessageBox(NULL, TEXT("Cannot init common controls"), TEXT("ERROR"), MB_OK | MB_ICONSTOP);
106 // Perform application initialization:
107 if (!InitInstance (hInstance, nCmdShow))
112 /* get command line parameters */
114 #if defined(WIN_CE_PS2xx) || defined(WIN32_PLATFORM_HPCPRO)
120 for( argc = 1; p && argc<MAX_CMDLINE_PARAM; argc++ ) {
123 argv[argc] = _strdup( NH_W2A(p, buf, BUFSZ) );
127 p = _get_cmd_arg(NULL);
129 GetModuleFileName(NULL, wbuf, BUFSZ);
130 argv[0] = _strdup(NH_W2A(wbuf, buf, BUFSZ));
140 // FUNCTION: InitInstance(HANDLE, int)
142 // PURPOSE: Saves instance handle and creates main window
146 // In this function, we save the instance handle in a global variable and
147 // create and display the main program window.
149 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
156 return &_nethack_app;
164 /* cannot use maxledgerno() here, because we need to find a lock name
165 * before starting everything (including the dungeon initialization
166 * that sets astral_level, needed for maxledgerno()) up
168 for(i = 1; i <= MAXDUNGEON*MAXLEVEL + 1; i++) {
169 /* try to remove all */
170 set_levelfile_name(lock, i);
171 (void) unlink(fqname(lock, LEVELPREFIX, 0));
173 set_levelfile_name(lock, 0);
174 if(unlink(fqname(lock, LEVELPREFIX, 0)))
175 return 0; /* cannot remove it */
176 return(1); /* success! */
189 /* regularize(lock); */ /* already done in pcmain */
190 Sprintf(tbuf, "%s", fqname(lock, LEVELPREFIX, 0));
191 set_levelfile_name(lock, 0);
192 fq_lock = fqname(lock, LEVELPREFIX, 1);
195 NH_A2W(fq_lock, wbuf, BUFSZ),
200 FILE_ATTRIBUTE_NORMAL,
202 if( f==INVALID_HANDLE_VALUE ) {
203 if(GetLastError()==ERROR_FILE_NOT_FOUND) goto gotlock; /* no such file */
204 error("Cannot open %s", fq_lock);
209 /* prompt user that the game alredy exist */
211 GetNHApp()->hMainWnd,
212 TEXT("There are files from a game in progress under your name. Recover?"),
214 MB_YESNO | MB_DEFBUTTON1
218 if(recover_savefile()) {
221 error("Couldn't recover old game.");
227 error("%s", "Cannot start a new game.");
232 fd = creat(fq_lock, FCMASK);
234 error("cannot creat lock file (%s.)", fq_lock);
236 if(write(fd, (char *) &hackpid, sizeof(hackpid))
238 error("cannot write lock (%s)", fq_lock);
240 if(close(fd) == -1) {
241 error("cannot close lock (%s)", fq_lock);
248 error VA_DECL(const char *,s)
251 DWORD last_error = GetLastError();
254 VA_INIT(s, const char *);
255 /* error() may get called before tty is initialized */
256 if (iflags.window_inited) end_screen();
258 vsprintf(buf, s, VA_ARGS);
259 NH_A2W(buf, wbuf, sizeof(wbuf)/sizeof(wbuf[0]));
263 FORMAT_MESSAGE_ALLOCATE_BUFFER |
264 FORMAT_MESSAGE_FROM_SYSTEM |
265 FORMAT_MESSAGE_IGNORE_INSERTS,
268 0, // Default language
276 _tcsncat(wbuf, TEXT("\nSystem Error: "), sizeof(wbuf)/sizeof(wbuf[0]) - _tcslen(wbuf) );
277 _tcsncat(wbuf, lpMsgBuf, sizeof(wbuf)/sizeof(wbuf[0]) - _tcslen(wbuf) );
280 LocalFree( lpMsgBuf );
285 MessageBox( NULL, wbuf, TEXT("Error"), MB_OK | MB_ICONERROR );
290 TCHAR* _get_cmd_arg(TCHAR* pCmdLine)
292 static TCHAR* pArgs = NULL;
296 if( !pCmdLine && !pArgs ) return NULL;
297 if( !pArgs ) pArgs = pCmdLine;
299 /* skip whitespace */
300 for(pRetArg = pArgs; *pRetArg && _istspace(*pRetArg); pRetArg = CharNext(pRetArg));
306 /* check for quote */
307 if( *pRetArg==TEXT('"') ) {
309 pRetArg = CharNext(pRetArg);
310 pArgs = _tcschr(pRetArg, TEXT('"'));
312 /* skip to whitespace */
313 for(pArgs = pRetArg; *pArgs && !_istspace(*pArgs); pArgs = CharNext(pArgs));
316 if( pArgs && *pArgs ) {
319 pArgs = CharNext(pArgs);
329 * Strip out troublesome file system characters.
333 nt_regularize(s) /* normalize file name */
336 register unsigned char *lp;
338 for (lp = s; *lp; lp++)
339 if ( *lp == '?' || *lp == '"' || *lp == '\\' ||
340 *lp == '/' || *lp == '>' || *lp == '<' ||
341 *lp == '*' || *lp == '|' || *lp == ':' || (*lp > 127))
359 char *portstr = PORT_CE_PLATFORM " " PORT_CE_CPU;
360 Sprintf(eos(buf), " %s", portstr);