Commits

spencercw committed 5462085

Display an open ROM dialog when Ctrl+O is pressed.

Comments (0)

Files changed (8)

gb_emulator/include/gb_emulator/gb_input.hpp

 	//! Translates the current input and modifies the appropriate registers.
 	uint8_t translate();
 
+	//! Displays a dialog to allow the user to open a ROM file.
+	void openRomDialog();
+
 private:
 	enum Key
 	{

gb_emulator/include/gb_emulator/gb_video.hpp

 
 #include <boost/scoped_array.hpp>
 
+#ifdef _WIN32
 #include <Windows.h>
+#endif
 
 #include <gb_emulator/constants.hpp>
 
 	void dumpGraphics();
 #endif
 
+#ifdef _WIN32
+	//! Returns the window handle.
+	virtual HWND window() const = 0;
+#else
+	// Handle some other native window handle
+#endif
+
 	//! Saves the current state of the video emulator into the given message.
 	void save(GbVideoData &data) const;
 

gb_emulator/include/gb_emulator/gb_video_d3d11.h

 	//! Initialises Direct3D and opens the display.
 	void initialise();
 
+	//! Returns the window handle.
+	HWND window() const
+	{
+		return window_;
+	}
+
 private:
 	// Direct3D stuff
 	boost::shared_ptr<HINSTANCE__> d3d11_;
+	HWND window_;
 
 	ATL::CComPtr<ID3D11Device> device_;
 	ATL::CComPtr<ID3D11DeviceContext> deviceContext_;

gb_emulator/include/gb_emulator/gb_video_sdl.h

 	{
 	}
 
+#ifdef _WIN32
+	//! Returns the window handle.
+	HWND window() const;
+#endif
+
 private:
 	SDL_Surface *surface_;
 

gb_emulator/src/gb_input.cpp

 #include <gb_emulator/gb_input.hpp>
 
 #include <Windows.h>
+#include <CommDlg.h>
 #include <XInput.h>
 
 #include <gb_emulator/gb.hpp>
 							gb_.load();
 							break;
 
-						case 0x51:  // Q
+						case 'O':
+							// Open ROM
+							openRomDialog();
+							break;
+
+						case 'Q':
 							// Quit
 							gb_.saveRam();
 							exit(0);
 							break;
 
-						case 0x52:  // R
+						case 'R':
 							// Toggle audio recording
 							if (!recording_)
 							{
 	gb_.mem_->ioPorts()[P1] = p1;
 	return p1;
 }
+
+void GbInput::openRomDialog()
+{
+#ifdef _WIN32
+	wchar_t fileName[32768];
+	fileName[0] = L'\0';
+
+	OPENFILENAMEW ofn;
+	memset(&ofn, 0, sizeof(ofn));
+	ofn.lStructSize = sizeof(ofn);
+	ofn.hwndOwner = gb_.video_->window();
+	ofn.lpstrFilter = L"Game Boy ROMs (*.gb, *.gbc)\0*.gb;*.gbc\0";
+	ofn.lpstrFile = fileName;
+	ofn.nMaxFile = sizeof(fileName)/sizeof(fileName[0]);
+	ofn.lpstrTitle = L"Open ROM";
+	ofn.Flags = OFN_DONTADDTORECENT | OFN_FILEMUSTEXIST | OFN_NONETWORKBUTTON | OFN_PATHMUSTEXIST;
+
+	if (GetOpenFileNameW(&ofn))
+	{
+		gb_.reset(ofn.lpstrFile);
+	}
+#else
+#error Not implemented.
+#endif
+}

gb_emulator/src/gb_video_d3d11.cpp

 
 #include <Cg/cgD3D11.h>
 
-#include <CommDlg.h>
 #include <D3D11.h>
 
 #include <gb_emulator/gb.hpp>
+#include <gb_emulator/gb_input.hpp>
 
 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
 
 	}
 
 	// Create the main window
-	HWND window = CreateWindowEx(NULL, L"WindowClass", L"Game Boy Emulator",
+	window_ = CreateWindowEx(NULL, L"WindowClass", L"Game Boy Emulator",
 		WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT,
 		windowRect.right - windowRect.left, windowRect.bottom - windowRect.top, NULL, NULL,
 		windowClass.hInstance, NULL);
-	if (!window)
+	if (!window_)
 	{
 		throw runtime_error("failed to create the main window: " +
 			lexical_cast<string>(GetLastError()));
 	swapChainDesc.SampleDesc.Quality = 0;
 	swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
 	swapChainDesc.BufferCount = 2;
-	swapChainDesc.OutputWindow = window;
+	swapChainDesc.OutputWindow = window_;
 	swapChainDesc.Windowed = TRUE;
 	swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
 	swapChainDesc.Flags = 0;
 		switch (wParam & 0xffff)
 		{
 		case 57601:  // Open
-			{
-				wchar_t fileName[32768];
-				fileName[0] = L'\0';
-
-				OPENFILENAMEW ofn;
-				memset(&ofn, 0, sizeof(ofn));
-				ofn.lStructSize = sizeof(ofn);
-				ofn.hwndOwner = hwnd;
-				ofn.lpstrFilter = L"Game Boy ROMs (*.gb, *.gbc)\0*.gb;*.gbc\0";
-				ofn.lpstrFile = fileName;
-				ofn.nMaxFile = sizeof(fileName)/sizeof(fileName[0]);
-				ofn.lpstrTitle = L"Open ROM";
-				ofn.Flags = OFN_DONTADDTORECENT | OFN_FILEMUSTEXIST | OFN_NONETWORKBUTTON |
-					OFN_PATHMUSTEXIST;
-
-				if (GetOpenFileNameW(&ofn))
-				{
-					gD3D11->gb_.reset(ofn.lpstrFile);
-				}
-			}
+			gD3D11->gb_.input_->openRomDialog();
 			break;
 
 		case 57603:  // Save

gb_emulator/src/gb_video_sdl.cpp

 	}
 }
 
+#ifdef _WIN32
+HWND GbVideoSdl::window() const
+{
+	SDL_SysWMinfo info;
+	SDL_VERSION(&info.version);
+	if (SDL_GetWMInfo(&info) != 1)
+	{
+		throw runtime_error("failed to get window information");
+	}
+	return info.window;
+}
+#endif
+
 void GbVideoSdl::draw(uint16_t *pixelBuffer, bool gbc)
 {
 	// Perform colour correction and image scaling in software

gb_emulator_main/gb_emulator.rc

Binary file modified.