acc8fcdc997a6e3c971f565e45e0697415cea6ba
[picodrive.git] / platform / win32 / GenaDrive / Main.cpp
1 #include "app.h"\r
2 #include "version.h"\r
3 #include <crtdbg.h>\r
4 #include <commdlg.h>\r
5 \r
6 char *romname;\r
7 HWND FrameWnd=NULL;\r
8 \r
9 int MainWidth=720,MainHeight=480;\r
10 \r
11 // Window proc for the frame window:\r
12 static LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)\r
13 {\r
14   if (msg==WM_CLOSE) { PostQuitMessage(0); return 0; }\r
15   if (msg==WM_DESTROY) FrameWnd=NULL; // Blank handle\r
16 \r
17   return DefWindowProc(hwnd,msg,wparam,lparam);\r
18 }\r
19 \r
20 static int FrameInit()\r
21 {\r
22   WNDCLASS wc;\r
23   RECT rect={0,0,0,0};\r
24   int style=0;\r
25   int left=0,top=0,width=0,height=0;\r
26 \r
27   memset(&wc,0,sizeof(wc));\r
28 \r
29   // Register the window class:\r
30   wc.lpfnWndProc=WndProc;\r
31   wc.hInstance=GetModuleHandle(NULL);\r
32   wc.hCursor=LoadCursor(NULL,IDC_ARROW);\r
33   wc.hbrBackground=CreateSolidBrush(0);\r
34   wc.lpszClassName="MainFrame";\r
35   RegisterClass(&wc);\r
36 \r
37   rect.right =320;//MainWidth;\r
38   rect.bottom=224;//MainHeight;\r
39 \r
40   // Adjust size of windows based on borders:\r
41   style=WS_OVERLAPPEDWINDOW;\r
42   AdjustWindowRect(&rect,style,0);\r
43   width =rect.right-rect.left;\r
44   height=rect.bottom-rect.top;\r
45 \r
46   // Place window in the centre of the screen:\r
47   SystemParametersInfo(SPI_GETWORKAREA,0,&rect,0);\r
48   left=rect.left+rect.right;\r
49   top=rect.top+rect.bottom;\r
50 \r
51   left-=width; left>>=1;\r
52   top-=height; top>>=1;\r
53 \r
54   // Create the window:\r
55   FrameWnd=CreateWindow(wc.lpszClassName,"PicoDrive " VERSION,style|WS_VISIBLE,\r
56     left,top,width,height,NULL,NULL,NULL,NULL);\r
57 \r
58   return 0;\r
59 }\r
60 \r
61 // --------------------\r
62 \r
63 static DWORD WINAPI ThreadCode(void *)\r
64 {\r
65   LoopCode();\r
66   return 0;\r
67 }\r
68 \r
69 int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR cmdline,int)\r
70 {\r
71   MSG msg;\r
72   int ret=0;\r
73   DWORD tid=0;\r
74   HANDLE thread=NULL;\r
75   unsigned char *rom_data = 0;\r
76   unsigned int rom_size = 0;\r
77 \r
78   FrameInit();\r
79   ret=LoopInit(); if (ret) goto end0;\r
80 \r
81   // notaz: load rom\r
82   static char rompath[MAX_PATH]; rompath[0] = 0;\r
83   strcpy(rompath, cmdline + (cmdline[0] == '\"' ? 1 : 0));\r
84   if(rompath[strlen(rompath)-1] == '\"') rompath[strlen(rompath)-1] = 0;\r
85 \r
86   pm_file *rom = 0;\r
87   if(strlen(rompath) > 4) rom = pm_open(rompath);\r
88   if(!rom) {\r
89     OPENFILENAME of; ZeroMemory(&of, sizeof(OPENFILENAME));\r
90     of.lStructSize = sizeof(OPENFILENAME);\r
91     of.lpstrFilter = "ROMs\0*.smd;*.bin;*.gen;*.zip\0";\r
92     of.lpstrFile = rompath; rompath[0] = 0;\r
93     of.nMaxFile = MAX_PATH;\r
94     of.Flags = OFN_FILEMUSTEXIST|OFN_HIDEREADONLY;\r
95     if(!GetOpenFileName(&of)) goto end0;\r
96     rom = pm_open(rompath);\r
97     if(!rom) goto end0;\r
98   }\r
99   romname = rompath;\r
100 \r
101   ret=PicoCartLoad(rom, &rom_data, &rom_size);\r
102   pm_close(rom);\r
103   if (ret) {\r
104     error("failed to load ROM");\r
105     goto end0;\r
106   }\r
107 \r
108   PicoCartInsert(rom_data, rom_size);\r
109 \r
110   // only now we got the mode (pal/ntsc), so init sound now\r
111   ret=DSoundInit();\r
112   if (ret) error("Failed to init DirectSound"); // warning\r
113 \r
114   // Make another thread to run LoopCode():\r
115   LoopQuit=0;\r
116   thread=CreateThread(NULL,0,ThreadCode,NULL,0,&tid);\r
117 \r
118   // Main window loop:\r
119   for (;;)\r
120   {\r
121     GetMessage(&msg,NULL,0,0);\r
122     if (msg.message==WM_QUIT) break;\r
123 \r
124     TranslateMessage(&msg);\r
125     DispatchMessage(&msg);\r
126   }\r
127 \r
128   // Signal thread to quit and wait for it to exit:\r
129   LoopQuit=1; WaitForSingleObject(thread,5000);\r
130   CloseHandle(thread); thread=NULL;\r
131 \r
132 end0:\r
133   LoopExit();\r
134   DestroyWindow(FrameWnd);\r
135 \r
136   free(rom_data);\r
137 \r
138   _CrtDumpMemoryLeaks();\r
139   return 0;\r
140 }\r
141 \r
142 extern void error(char *text)\r
143 {\r
144   MessageBox(FrameWnd, text, "Error", 0);\r
145 }\r
146 \r