lots of win32 port work
[picodrive.git] / platform / win32 / GenaDrive / Main.cpp
index f5f41cc..4fbbf02 100644 (file)
 #include <crtdbg.h>\r
 #include <commdlg.h>\r
 \r
-char *romname;\r
+char *romname=NULL;\r
 HWND FrameWnd=NULL;\r
 RECT FrameRectMy;\r
+int lock_to_1_1 = 1;\r
 \r
 int MainWidth=720,MainHeight=480;\r
 \r
+static HMENU mdisplay = 0;\r
+\r
+static void UpdateRect()\r
+{\r
+  WINDOWINFO wi;\r
+  memset(&wi, 0, sizeof(wi));\r
+  wi.cbSize = sizeof(wi);\r
+  GetWindowInfo(FrameWnd, &wi);\r
+  FrameRectMy = wi.rcClient;\r
+}\r
+\r
+static void LoadROM(const char *cmdpath)\r
+{\r
+  static char rompath[MAX_PATH] = { 0, };\r
+  static unsigned char *rom_data = NULL;\r
+  unsigned char *rom_data_new = NULL;\r
+  unsigned int rom_size = 0;\r
+  pm_file *rom = NULL;\r
+  int oldwait=LoopWait;\r
+  int i, ret;\r
+\r
+  if (cmdpath) {\r
+    strcpy(rompath, cmdpath + (cmdpath[0] == '\"' ? 1 : 0));\r
+    if (rompath[strlen(rompath)-1] == '\"') rompath[strlen(rompath)-1] = 0;\r
+    if (strlen(rompath) > 4) rom = pm_open(rompath);\r
+  }\r
+\r
+  if (!rom) {\r
+    OPENFILENAME of; ZeroMemory(&of, sizeof(OPENFILENAME));\r
+    of.lStructSize = sizeof(OPENFILENAME);\r
+    of.lpstrFilter = "ROMs\0*.smd;*.bin;*.gen;*.zip\0";\r
+    of.lpstrFile = rompath; rompath[0] = 0;\r
+    of.nMaxFile = MAX_PATH;\r
+    of.Flags = OFN_FILEMUSTEXIST|OFN_HIDEREADONLY;\r
+    of.hwndOwner = FrameWnd;\r
+    if (!GetOpenFileName(&of)) return;\r
+    rom = pm_open(rompath);\r
+    if (!rom) { error("failed to open ROM"); return; }\r
+  }\r
+\r
+  ret=PicoCartLoad(rom, &rom_data_new, &rom_size);\r
+  pm_close(rom);\r
+  if (ret) {\r
+    error("failed to load ROM");\r
+    return;\r
+  }\r
+\r
+  // halt the work thread..\r
+  // just a hack, should've used proper sync. primitives here, but who will use this emu anyway..\r
+  LoopWaiting=0;\r
+  LoopWait=1;\r
+  for (i = 0; LoopWaiting == 0 && i < 10; i++) Sleep(100);\r
+\r
+  PicoCartInsert(rom_data_new, rom_size);\r
+\r
+  if (rom_data) free(rom_data);\r
+  rom_data = rom_data_new;\r
+  romname = rompath;\r
+  LoopWait=0;\r
+}\r
+\r
+static int rect_widths[4]  = { 320, 256, 640, 512 };\r
+static int rect_heights[4] = { 224, 224, 448, 448 };\r
+\r
 // Window proc for the frame window:\r
 static LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)\r
 {\r
+  int i;\r
   switch (msg)\r
   {\r
     case WM_CLOSE:   PostQuitMessage(0); return 0;\r
     case WM_DESTROY: FrameWnd=NULL; break; // Blank handle\r
     case WM_SIZE:\r
     case WM_MOVE:\r
-    case WM_SIZING:  GetWindowRect(hwnd, &FrameRectMy); break;\r
+    case WM_SIZING:  UpdateRect(); break;\r
+    case WM_COMMAND:\r
+      switch (LOWORD(wparam))\r
+      {\r
+        case 1000: LoadROM(NULL); break;\r
+        case 1001: PostQuitMessage(0); return 0;\r
+        case 1100:\r
+        case 1101:\r
+        case 1102:\r
+        case 1103:\r
+          LoopWait=1; // another sync hack\r
+          for (i = 0; !LoopWaiting && i < 10; i++) Sleep(10);\r
+          FrameRectMy.right  = FrameRectMy.left + rect_widths[wparam&3];\r
+          FrameRectMy.bottom = FrameRectMy.top  + rect_heights[wparam&3];\r
+          AdjustWindowRect(&FrameRectMy, WS_OVERLAPPEDWINDOW, 1);\r
+          MoveWindow(hwnd, FrameRectMy.left, FrameRectMy.top,\r
+            FrameRectMy.right-FrameRectMy.left, FrameRectMy.bottom-FrameRectMy.top, 1);\r
+          UpdateRect();\r
+          if (HIWORD(wparam) == 0) { // locally sent\r
+            lock_to_1_1=0;\r
+            CheckMenuItem(mdisplay, 1104, MF_UNCHECKED);\r
+          }\r
+          LoopWait=0;\r
+          return 0;\r
+        case 1104:\r
+          lock_to_1_1=!lock_to_1_1;\r
+          CheckMenuItem(mdisplay, 1104, lock_to_1_1 ? MF_CHECKED : MF_UNCHECKED);\r
+          return 0;\r
+        case 1200: break;\r
+        case 1300:\r
+          MessageBox(FrameWnd, "PicoDrive v" VERSION " (c) notaz, 2006-2008\n"\r
+              "SVP demo edition\n\n"\r
+              "Credits:\n"\r
+              "fDave: base code of PicoDrive, GenaDrive (the frontend)\n"\r
+              "Chui: Fame/C\n"\r
+              "NJ: CZ80\n"\r
+              "MAME devs: YM2612 and SN76496 cores\n"\r
+              "Stéphane Dallongeville: Gens code, base of Fame/C (C68K), CZ80\n"\r
+              "Tasco Deluxe: SVP RE work\n",\r
+              "About", 0);\r
+          return 0;\r
+      }\r
+      break;\r
   }\r
 \r
   return DefWindowProc(hwnd,msg,wparam,lparam);\r
@@ -28,6 +136,7 @@ static int FrameInit()
 {\r
   WNDCLASS wc;\r
   RECT rect={0,0,0,0};\r
+  HMENU mmain, mfile;\r
   int style=0;\r
   int left=0,top=0,width=0,height=0;\r
 \r
@@ -46,7 +155,7 @@ static int FrameInit()
 \r
   // Adjust size of windows based on borders:\r
   style=WS_OVERLAPPEDWINDOW;\r
-  AdjustWindowRect(&rect,style,0);\r
+  AdjustWindowRect(&rect,style,1);\r
   width =rect.right-rect.left;\r
   height=rect.bottom-rect.top;\r
 \r
@@ -58,13 +167,30 @@ static int FrameInit()
   left-=width; left>>=1;\r
   top-=height; top>>=1;\r
 \r
+  // Create menu:\r
+  mfile = CreateMenu();\r
+  InsertMenu(mfile, -1, MF_BYPOSITION|MF_STRING, 1000, "&Load ROM");\r
+  InsertMenu(mfile, -1, MF_BYPOSITION|MF_STRING, 1001, "E&xit");\r
+  mdisplay = CreateMenu();\r
+  InsertMenu(mdisplay, -1, MF_BYPOSITION|MF_STRING, 1100, "320x224");\r
+  InsertMenu(mdisplay, -1, MF_BYPOSITION|MF_STRING, 1101, "256x224");\r
+  InsertMenu(mdisplay, -1, MF_BYPOSITION|MF_STRING, 1102, "640x448");\r
+  InsertMenu(mdisplay, -1, MF_BYPOSITION|MF_STRING, 1103, "512x448");\r
+  InsertMenu(mdisplay, -1, MF_BYPOSITION|MF_STRING, 1104, "Lock to 1:1");\r
+  mmain = CreateMenu();\r
+  InsertMenu(mmain, -1, MF_BYPOSITION|MF_STRING|MF_POPUP, (UINT_PTR) mfile, "&File");\r
+  InsertMenu(mmain, -1, MF_BYPOSITION|MF_STRING|MF_POPUP, (UINT_PTR) mdisplay, "&Display");\r
+//  InsertMenu(mmain, -1, MF_BYPOSITION|MF_STRING|MF_POPUP, 1200, "&Config");\r
+  InsertMenu(mmain, -1, MF_BYPOSITION|MF_STRING|MF_POPUP, 1300, "&About");\r
+\r
   // Create the window:\r
   FrameWnd=CreateWindow(wc.lpszClassName,"PicoDrive " VERSION,style|WS_VISIBLE,\r
-    left,top,width,height,NULL,NULL,NULL,NULL);\r
+    left,top,width,height,NULL,mmain,NULL,NULL);\r
 \r
+  CheckMenuItem(mdisplay, 1104, lock_to_1_1 ? MF_CHECKED : MF_UNCHECKED);\r
   ShowWindow(FrameWnd, SW_NORMAL);\r
   UpdateWindow(FrameWnd);\r
-  GetWindowRect(FrameWnd, &FrameRectMy);\r
+  UpdateRect();\r
 \r
   return 0;\r
 }\r
@@ -83,50 +209,17 @@ int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR cmdline,int)
   int ret=0;\r
   DWORD tid=0;\r
   HANDLE thread=NULL;\r
-  unsigned char *rom_data = 0;\r
-  unsigned int rom_size = 0;\r
-\r
-  static char rompath[MAX_PATH] = { 0, };\r
-  pm_file *rom = NULL;\r
 \r
   FrameInit();\r
   ret=LoopInit(); if (ret) goto end0;\r
 \r
-  // notaz: load rom\r
-  strcpy(rompath, cmdline + (cmdline[0] == '\"' ? 1 : 0));\r
-  if(rompath[strlen(rompath)-1] == '\"') rompath[strlen(rompath)-1] = 0;\r
-\r
-  if(strlen(rompath) > 4) rom = pm_open(rompath);\r
-  if(!rom) {\r
-    OPENFILENAME of; ZeroMemory(&of, sizeof(OPENFILENAME));\r
-    of.lStructSize = sizeof(OPENFILENAME);\r
-    of.lpstrFilter = "ROMs\0*.smd;*.bin;*.gen;*.zip\0";\r
-    of.lpstrFile = rompath; rompath[0] = 0;\r
-    of.nMaxFile = MAX_PATH;\r
-    of.Flags = OFN_FILEMUSTEXIST|OFN_HIDEREADONLY;\r
-    if(!GetOpenFileName(&of)) goto end0;\r
-    rom = pm_open(rompath);\r
-    if(!rom) goto end0;\r
-  }\r
-  romname = rompath;\r
-\r
-  ret=PicoCartLoad(rom, &rom_data, &rom_size);\r
-  pm_close(rom);\r
-  if (ret) {\r
-    error("failed to load ROM");\r
-    goto end0;\r
-  }\r
-\r
-  PicoCartInsert(rom_data, rom_size);\r
-\r
-  // only now we got the mode (pal/ntsc), so init sound now\r
-  ret=DSoundInit();\r
-  if (ret) error("Failed to init DirectSound"); // warning\r
-\r
   // Make another thread to run LoopCode():\r
   LoopQuit=0;\r
+  LoopWait=1; // wait for ROM to be loaded\r
   thread=CreateThread(NULL,0,ThreadCode,NULL,0,&tid);\r
 \r
+  LoadROM(cmdline);\r
+\r
   // Main window loop:\r
   for (;;)\r
   {\r
@@ -145,8 +238,6 @@ end0:
   LoopExit();\r
   DestroyWindow(FrameWnd);\r
 \r
-  free(rom_data);\r
-\r
   _CrtDumpMemoryLeaks();\r
   return 0;\r
 }\r