Skip to content

Commit

Permalink
Removed dependency on MFC (afxres.h and afxwin.h) (#106)
Browse files Browse the repository at this point in the history
* Removed dependency on MFC (afxres.h and afxwin.h)

* updated tape counter and operation mode in the tape dialog to use the RichEdit20A (MBCS) class for the rich edit control.
  • Loading branch information
ChetSimpson authored Oct 15, 2022
1 parent a2a0866 commit 8d3d5f5
Show file tree
Hide file tree
Showing 20 changed files with 217 additions and 174 deletions.
103 changes: 52 additions & 51 deletions Breakpoints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ This file is part of VCC (Virtual Color Computer).
Author: Mike Rojas
*/

#include <afx.h>
#include "defines.h"
#include "DebuggerUtils.h"
#include "resource.h"
#include <commdlg.h>
#include <map>
Expand All @@ -29,8 +29,9 @@ This file is part of VCC (Virtual Color Computer).
#include <vector>
#include <regex>
#include <Richedit.h>
#include <codecvt>
#include <locale>

using namespace std;

extern SystemState EmuState;

Expand All @@ -42,22 +43,23 @@ namespace Breakpoints
bool BrowseDialogOpen = false;
unsigned short PriorCPUHaltAddress = 0;

typedef struct {
struct Breakpoint
{
int id;
int addr;
int line;
bool enabled;
} Breakpoint;
};

map<int, Breakpoint> mapBreakpoints;
map<int, int> mapLineAddr;
map<int, int> mapAddrLine;
std::map<int, Breakpoint> mapBreakpoints;
std::map<int, int> mapLineAddr;
std::map<int, int> mapAddrLine;

vector<string> split(const string& input, const string& rgx)
std::vector<std::string> split(const std::string& input, const std::string& rgx)
{
// passing -1 as the submatch index parameter performs splitting
regex re(rgx);
sregex_token_iterator
std::regex re(rgx);
std::sregex_token_iterator
first{ input.begin(), input.end(), re, -1 },
last;
return { first, last };
Expand All @@ -81,31 +83,30 @@ namespace Breakpoints
SendDlgItemMessage(hWndBreakpoints, IDC_EDIT_SOURCE, WM_SETTEXT, strlen(source), (LPARAM)(LPCSTR)source);

int nLines = 0;
CString loaded;
loaded.Format("%d lines loaded", nLines);
SendDlgItemMessage(hWndBreakpoints, IDC_LINES_LOADED, WM_SETTEXT, loaded.GetLength(), (LPARAM)(LPCSTR)loaded);
std::string loaded(std::to_string(nLines) + " lines loaded");
SendDlgItemMessage(hWndBreakpoints, IDC_LINES_LOADED, WM_SETTEXT, loaded.size(), (LPARAM)ToLPCSTR(loaded));

ifstream file(source);
std::ifstream file(source);
if (!file.is_open())
{
return false;
}

CString lines;
std::string lines;

for (string line; getline(file, line); )
for (std::string line; getline(file, line); )
{
string addr = line.length() >= 4 ? line.substr(0, 4): string(4, ' ');
string opcodes = line.length() >= 20 ? line.substr(5, 16) : string(16, ' ');
string sourceline = line.length() >= 45 ? line.substr(22, 25) : string(25, ' ');
string code = line.length() >= 60 ? line.substr(56) : "";
auto addr = line.length() >= 4 ? line.substr(0, 4): std::string(4, ' ');
auto opcodes = line.length() >= 20 ? line.substr(5, 16) : std::string(16, ' ');
auto sourceline = line.length() >= 45 ? line.substr(22, 25) : std::string(25, ' ');
auto code = line.length() >= 60 ? line.substr(56) : "";

if (addr != string(4, ' '))
if (addr != std::string(4, ' '))
{
unsigned int addrInt;
std::stringstream ss;
ss << addr;
ss >> hex >> addrInt;
ss >> std::hex >> addrInt;
mapLineAddr[nLines] = addrInt; // Map address to listing line number.
mapAddrLine[addrInt] = nLines; // Map listing line number to address.
}
Expand All @@ -119,11 +120,11 @@ namespace Breakpoints
nLines++;
}

loaded.Format("%d lines loaded", nLines);
loaded = std::to_string(nLines) + " lines loaded";
SendDlgItemMessage(hWndBreakpoints, IDC_SOURCE_LISTING, EM_SETSEL, WPARAM(0), LPARAM(-1));
SendDlgItemMessage(hWndBreakpoints, IDC_SOURCE_LISTING, EM_REPLACESEL, WPARAM(TRUE), (LPARAM)(LPCSTR)(lines));
SendDlgItemMessage(hWndBreakpoints, IDC_SOURCE_LISTING, EM_REPLACESEL, WPARAM(TRUE), (LPARAM)ToLPCSTR(lines));
SendDlgItemMessage(hWndBreakpoints, IDC_SOURCE_LISTING, EM_LINESCROLL, 0, (LPARAM)-nLines);
SendDlgItemMessage(hWndBreakpoints, IDC_LINES_LOADED, WM_SETTEXT, loaded.GetLength(), (LPARAM)(LPCSTR)loaded);
SendDlgItemMessage(hWndBreakpoints, IDC_LINES_LOADED, WM_SETTEXT, loaded.size(), (LPARAM)ToLPCSTR(loaded));
return true;
}

Expand Down Expand Up @@ -208,13 +209,15 @@ namespace Breakpoints
SendMessage(hCtl, EM_SETSEL, currentCharStart + pos, currentCharEnd);
}

CString GetSelectedSourceText()
std::string GetSelectedSourceText()
{
WCHAR buf[1000];
memset(buf, 0, sizeof(buf));
SendDlgItemMessage(hWndBreakpoints, IDC_SOURCE_LISTING, EM_GETSELTEXT, 0, (LPARAM)&buf);
CString start = buf;
return start;

//setup converter
using convert_type = std::codecvt_utf8<wchar_t>;
return std::wstring_convert<convert_type, wchar_t>().to_bytes( buf );
}

void SetLineColor(int line, int rgb)
Expand All @@ -231,10 +234,9 @@ namespace Breakpoints
void UpdateBreakpointUI(Breakpoint bp)
{
HWND hListCtl = GetDlgItem(hWndBreakpoints, IDC_LIST_BREAKPOINTS);
CString s;
CString enabled = bp.enabled ? "ON" : "off";
s.Format("Breakpoint #%d\t%s\taddr = %X", bp.id, (LPCSTR)enabled, bp.addr);
int pos = SendMessage(hListCtl, LB_ADDSTRING, 0, (LPARAM)(LPCSTR)s);
const std::string enabled = bp.enabled ? "On" : "Off";
std::string s = "Breakpoint #" + std::to_string(bp.id) + "\t" + enabled + "\taddr = " + ToHexString(bp.addr, 0, false);
int pos = SendMessage(hListCtl, LB_ADDSTRING, 0, (LPARAM)ToLPCSTR(s));
SendMessage(hListCtl, LB_SETITEMDATA, pos, (LPARAM)bp.id);
SendMessage(hListCtl, LB_SETCURSEL, pos, 0);

Expand All @@ -243,8 +245,8 @@ namespace Breakpoints
EnableWindow(GetDlgItem(hWndBreakpoints, IDC_BTN_BREAKPOINT_OFF), true);

SelectSourceText(bp.line, 4);
s.Format("%s%2d", bp.enabled ? "BK" : "--", bp.id);
SendDlgItemMessage(hWndBreakpoints, IDC_SOURCE_LISTING, EM_REPLACESEL, WPARAM(FALSE), (LPARAM)(LPCSTR)s);
s = (bp.enabled ? "BK" : "--") + ToDecimalString(bp.id, 2, false);
SendDlgItemMessage(hWndBreakpoints, IDC_SOURCE_LISTING, EM_REPLACESEL, WPARAM(FALSE), (LPARAM)ToLPCSTR(s));
SetLineColor(bp.line, RGB(255, 100, 100));
}

Expand All @@ -260,7 +262,7 @@ namespace Breakpoints
EmuState.CPUBreakpoints = (unsigned short*)malloc(sizeof(unsigned short) * EmuState.CPUNumBreakpoints);

int n = 0;
map<int, Breakpoint>::iterator it = mapBreakpoints.begin();
std::map<int, Breakpoint>::iterator it = mapBreakpoints.begin();
while (it != mapBreakpoints.end())
{
Breakpoint bp = it->second;
Expand Down Expand Up @@ -340,9 +342,8 @@ namespace Breakpoints
EnableWindow(GetDlgItem(hWndBreakpoints, IDC_BTN_BREAKPOINT_OFF), false);

SelectSourceText(bp.line, 4);
CString s;
s.Format(" ");
SendDlgItemMessage(hWndBreakpoints, IDC_SOURCE_LISTING, EM_REPLACESEL, WPARAM(FALSE), (LPARAM)(LPCSTR)s);
const std::string s(" ");
SendDlgItemMessage(hWndBreakpoints, IDC_SOURCE_LISTING, EM_REPLACESEL, WPARAM(FALSE), (LPARAM)ToLPCSTR(s));
SetLineColor(bp.line, RGB(255, 255, 255));
break;
}
Expand All @@ -359,8 +360,8 @@ namespace Breakpoints
return;
}
SelectSourceText(currentLine, 4);
CString start = GetSelectedSourceText();
if (start.Left(4) == " ")
const std::string start = GetSelectedSourceText();
if (start.substr(0, 4) == " ")
{
AddBreakpoint(currentLine);
}
Expand Down Expand Up @@ -426,11 +427,11 @@ namespace Breakpoints
}

SelectSourceText(line, 4);
CString start = GetSelectedSourceText();
if (start.Left(2) == "BK" || start.Left(2) == "--")
const std::string start(GetSelectedSourceText());
if (start.substr(0, 2) == "BK" || start.substr(0, 2) == "--")
{
CString nn = start.Mid(2, 2);
int id = atoi(nn);
const std::string nn = start.substr(2, 2);
int id = atoi(nn.c_str());
RemoveBreakpoint(id);
}
else
Expand Down Expand Up @@ -487,17 +488,17 @@ namespace Breakpoints
if (key == 'E' || key == 'e')
{
SelectSourceText(currentLine, 4);
CString start = GetSelectedSourceText();
if (start.Left(2) == "BK")
const std::string start = GetSelectedSourceText();
if (start.substr(0, 2) == "BK")
{
CString nn = start.Mid(2, 2);
int id = atoi(nn);
const std::string nn = start.substr(2, 2);
int id = atoi(nn.c_str());
EnableBreakpoint(id, false);
}
if (start.Left(2) == "--")
if (start.substr(0, 2) == "--")
{
CString nn = start.Mid(2, 2);
int id = atoi(nn);
const std::string nn = start.substr(2, 2);
int id = atoi(nn.c_str());
EnableBreakpoint(id, true);
}
}
Expand Down
22 changes: 22 additions & 0 deletions DebuggerUtils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "DebuggerUtils.h"
#include <sstream>
#include <iomanip>

std::string ToHexString(long value, int length, bool leadingZeros)
{
std::ostringstream fmt;

fmt << std::hex << std::setw(length) << std::setfill(leadingZeros ? '0' : ' ') << value;

return fmt.str();
}


std::string ToDecimalString(long value, int length, bool leadingZeros)
{
std::ostringstream fmt;

fmt << std::dec << std::setw(length) << std::setfill(leadingZeros ? '0' : ' ') << value;

return fmt.str();
}
24 changes: 24 additions & 0 deletions DebuggerUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once
#include <Windows.h>
#include <string>

template<class Type_>
inline LPCSTR ToLPCSTR(std::basic_string<Type_>& str)
{
return str.c_str();
}

template<class Type_>
inline LPCSTR ToLPCSTR(const std::basic_string<Type_>& str)
{
return str.c_str();
}

template<class Type_>
LPCSTR ToLPCSTR(const std::basic_filebuf<Type_>&& str)
{
static_assert("RValue not supported for ToLPCSTR");
}

std::string ToHexString(long value, int length, bool leadingZeros = true);
std::string ToDecimalString(long value, int length, bool leadingZeros);
4 changes: 2 additions & 2 deletions DirectDrawInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ This file is part of VCC (Virtual Color Computer).
#define NO_WARN_MBCS_MFC_DEPRECATION
#define _WIN32_WINNT 0x05010000 // I want to support XP

#include <afxwin.h>
#include <windows.h>
#include <commctrl.h> // Windows common controls
#include "defines.h"
#include "ddraw.h"
Expand Down Expand Up @@ -63,7 +63,7 @@ BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
g_hInstance=hInstance;
g_nCmdShow=nCmdShow;
AfxInitRichEdit();
LoadLibrary("RICHED20.DLL");
LoadString(g_hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(g_hInstance, IDS_APP_TITLE, szWindowClass, MAX_LOADSTRING);
return TRUE;
Expand Down
4 changes: 2 additions & 2 deletions FD502/fd502.rc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "afxres.h"
#include "winres.h"

/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS
Expand All @@ -32,7 +32,7 @@ END

2 TEXTINCLUDE
BEGIN
"#include ""afxres.h""\r\n"
"#include ""winres.h""\r\n"
"\0"
END

Expand Down
4 changes: 2 additions & 2 deletions GMC/GMC.rc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "afxres.h"
#include "winres.h"

/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS
Expand All @@ -32,7 +32,7 @@ END

2 TEXTINCLUDE
BEGIN
"#include ""afxres.h""\r\n"
"#include ""winres.h""\r\n"
"\0"
END

Expand Down
17 changes: 4 additions & 13 deletions GMC/ROM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,19 @@ using namespace std;

bool ROM::Load(std::string filename)
{

// std::ifstream input(filename, std::ifstream::binary);
ifstream input(filename,ios::binary);
if (!input.is_open())
{
return false;
}

int begin = input.tellg();
std::streamoff begin = input.tellg();
input.seekg(0,ios::end);
int end = input.tellg();
int fileLength=end-begin;
std::streamoff end = input.tellg();
std::streamoff fileLength = end - begin;
input.seekg(0,ios::beg);

// input.seekg(0, std::ifstream::ios_base::end);
// const auto fileLength(input.tellg().seekpos());
// input.seekg(0, std::ifstream::ios_base::beg);

std::vector<unsigned char> fileData(fileLength);
std::vector<unsigned char> fileData(static_cast<size_t>(fileLength));
input.read(reinterpret_cast<char*>(&fileData[0]), fileData.size());

if (fileLength != fileData.size())
Expand All @@ -36,9 +30,6 @@ bool ROM::Load(std::string filename)
m_BankOffset = 0;
m_Bank = 0;

// char msg[256];
// sprintf(msg,"%s loaded %d",&m_Filename[0],m_Data.size());
// MessageBoxA(nullptr, msg, "Rom loaded", MB_OK);
return true;
}

Expand Down
Loading

0 comments on commit 8d3d5f5

Please sign in to comment.