如何在64位环境中编译和运行Physics2D.NET演示

发布于 2024-11-26 11:17:29 字数 456 浏览 3 评论 0原文

在提出问题的过程中,我已经找到了我需要的答案,但也有兴趣分享我所学到的知识,并查看人们在我尝试进入二维物理时可能提出的任何其他评论。

我想用 C# 玩一下 2D 物理引擎。我已经下载了Box2dx和Physics2D.Net。 Box2dx 的演示运行良好,但我担心性能和编码复杂性。所以我决定尝试一下Physics2D.Net(我找不到太多信息来比较Box2dx和Physics2d.Net,但我有理由相信Physics2D.Net根据我所读到的内容更容易编码,并且我怀疑它可能表现更好)。不幸的是,该演示将无法运行。当尝试在 Windows 7 64 位上运行Physics2DDemo 示例程序时,我遇到 BadImageFormat 异常。

就像我说的,我已经弄清楚如何让演示运行,所以我希望你不介意我发布并接受我自己的答案,但与这个主题相关的任何其他有用的评论我仍然会投票,如果有人有更好的答案(例如“尝试其他效果更好的下载”)我仍然可以选择我自己的答案。

In the process of asking my question I have figured out the answers I needed, but am also interested in sharing what I've learned and seeing any other comments people may have as I try to get into 2D physics.

I wanted to play with a 2D physics engine in C#. I have downloaded Box2dx and Physics2D.Net. The demos for Box2dx ran nicely, but I'm concerned about performance and coding complexity. So I decided to give Physics2D.Net a try (I can't find much info to compare Box2dx and Physics2d.Net, but I'm reasonably sure Physics2D.Net is simpler to code with based on what I've read, and I have suspicions that it might perform better). Unfortunately, the demo will not run. I get BadImageFormat exceptions when it try to run the Physics2DDemo sample program on Windows 7 64-bit.

Like I said, I have figured out how to get the demo to run, so I hope you don't mind that I post and accept my own answer, but any other helpful comments related to this topic I will still vote up, and if someone has a better answer (like "try this other download that works better") I could still choose over my own.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅听莫相离 2024-12-03 11:17:29

下载 SdlDotNet 的源代码以调试加载字体时发生的 BadImageformatException 的源代码后,我发现它是在尝试初始化字体系统时发生的。我猜测为什么会发生这种情况,因为那里仍然有一些 32 位 DLL 正在运行,而所有 .NET 程序集都在 64 位进程中运行。因此,我强制最高级别的 .NET 程序集以 x86 而不是任何 CPU 为目标。为了从 Visual C# 2010 Express 中执行此操作,我打开“生成”菜单,选择“配置管理器”,选择“x86”作为活动解决方案平台,并对 3 个项目Physics2DDemoPhysics2DDotNetPhysics2DDotNet.Demo 进行了一些更改

  • 可能

不需要更改所有这些,但仅更改Physics2DDemo似乎不起作用,这可能是因为我需要重新打开解决方案和/或更强有力地重建,但它最终起作用了。)以下是我对每个项目所做的更改:

  1. 在平台列中,选择“<新建...>”
  2. 在弹出的对话框中,选择 x86 作为新平台,然后选择“任何 CPU”作为复制源。
  3. 选中“构建”框。
  4. 关闭“配置管理器”窗口并打开该单个项目的“项目设置”。
  5. 在“Build”选项卡上,从输出路径中删除“x86”,仅保留 bin\Debug。

回想起来,我认为我应该在每个项目上选中“构建”框,这可能会减少我在使项目 DLL 版本保持彼此同步方面遇到的一些问题。

这确实让我解决了字体问题,但只需要几行代码。接下来,在执行 SurfaceGl 的类型初始值设定项时失败。它在这一行失败:

static glLoadIndentityDelegate glLoadIdentity = 
(glLoadIndentityDelegate)Marshal.GetDelegateForFunctionPointer(
Sdl.SDL_GL_GetProcAddress("glLoadIdentity"), typeof(glLoadIndentityDelegate));

显然 SDL_GL_GetProcAddress 返回 0。事实证明,我为 SdlDotNet 下载的代码比Physics2D.Net 提供的代码稍新,但无法与Physics2D.Net 一起使用。我用原始 DLL 替换了更新的 DLL,现在我可以编译并运行Physics2D.NET 演示了!

当然,任何想要将Physics2D.NET合并到纯.NET引擎中的人,或者可以容纳64位进程的人可能会希望构建“AnyCPU”解决方案目标而不是x86目标。将 x86 解决方案目标中的所有项目切换为 x86 可能是值得的。

我注意到我现在可以切换项目目标,以便Physics2DDemo 是唯一构建为x86 的项目。其余的程序集(即 DLL)将按照该进程选择的方式加载到该进程中。因此,仅强制该程序集启动 32 位进程就足够了。

可能有一些步骤我在这里忽略提及。我确实重新加载了解决方案并强制重建了多次,以便 Visual Studio 与新的目标位置和版本等重新同步。但我认为这一切都是因为我切换了 SdlDotNet 版本。所以我希望这一切并不是真正必要的。

After downloading the source code for SdlDotNet to debug into the source of the BadImageformatException that occurred while loading the font, I discovered it was happening while trying to initialize the font system. My guess as to why this was happening was that there was still some 32-bit DLL running under there somewhere, and all the .NET assemblies were running in a 64-bit process. So I forced the highest level .NET assemblies to target x86 instead of Any CPU. To do this from Visual C# 2010 Express, I opened the Build menu, selected "Configuration Manager", selected "x86" as the Active Solution Platform, and made some changes to 3 projects

  • Physics2DDemo
  • Physics2DDotNet
  • Physics2DDotNet.Demo

(I probably didn't need to change all of these, but changing just Physics2DDemo didn't seem to work. It may have been because I needed to re-open the solution and/or rebuild more forcefully, but it eventually worked.) These are the changes I made to each project:

  1. In the platform column, select "<New...>"
  2. In the pop-up dialog, select x86 as the New Platform, and select "Any CPU" as the source to copy from.
  3. Check the "Build" box.
  4. Close the Configuration Manager window and open the Project Settings of this individual project.
  5. On the "Build" tab, delete "x86" from the output path, leaving only bin\Debug.

In retrospect, I think I should have checked the "Build" box on every project, which could have reduced some of my problems in getting the project DLL versions to stay in sync with each other.

That did get me past the font problem, but only by a couple lines of code. Next it was failing when executing the type initializer for SurfaceGl. It failed on this line:

static glLoadIndentityDelegate glLoadIdentity = 
(glLoadIndentityDelegate)Marshal.GetDelegateForFunctionPointer(
Sdl.SDL_GL_GetProcAddress("glLoadIdentity"), typeof(glLoadIndentityDelegate));

Apparently SDL_GL_GetProcAddress was returning 0. As it turns out, the code that I had downloaded for SdlDotNet, being somewhat newer than that delivered with Physics2D.Net, was not working with Physics2D.Net. I replaced the updated DLLs with the original DLL's, and now I am able to compile and run the Physics2D.NET demo!

Of course anyone wanting to incorporate Physics2D.NET into a pure .NET engine, or one that can accommodate a 64-bit process will probably want to Build the "AnyCPU" solution target instead of the x86 target. And it may be worthwhile switching all projects in the x86 solution target to build as x86.

I have noticed that I can now switch the project targets so that Physics2DDemo is the only project building as x86. The rest of the assemblies, being DLLs, will load into that process as whatever that process chooses to be. So forcing just that assembly to start a 32-bit process is sufficient.

There may be some steps I neglected to mention here. I did re-load the solution and force a rebuild a number of times in order for Visual Studio to re-sync with new target locations and versions and such. But I think all that was because of my switching the SdlDotNet version. So I hope all that isn't really necessary.

迷迭香的记忆 2024-12-03 11:17:29

如果您在进行上述更改后收到“找不到文件”,

您可能还需要添加 ..|到 dataDir 的开头,所以就像

static readonly string dataDir =
  @"..|..|..|..|data".Replace('|',Path.DirectorySeparatorChar);

在 VS2010 中一样,当您摆弄平台和目标时,您会获得额外的目录层......

if you get a "file not found" after the changes above,

you may also need to add a ..| to the start of the dataDir, so it becomes

static readonly string dataDir =
  @"..|..|..|..|data".Replace('|',Path.DirectorySeparatorChar);

as in VS2010 you gain an extra layer of directories when you fiddle with platforms and targets...

饮惑 2024-12-03 11:17:29

我知道它很旧,但我在谷歌上搜索解决方案时遇到了同样的问题,我来到这里,但意识到它比你的解决方案更简单。
我通过以下方式实现了这一点:

        string dataDir = "C:\\Windows\\Fonts\\arial.ttf";
        font = new Font(dataDir, 40);
        font.Bold = true;
        font2 = new Font(dataDir, 15);

在“Demo.cs”的第 97 - 100 行。为我工作。

I know it's old, but I had the same Problem searching for Solutions on Google and I came up here, but realised it simplier than your Solution.
I Realised it by writing:

        string dataDir = "C:\\Windows\\Fonts\\arial.ttf";
        font = new Font(dataDir, 40);
        font.Bold = true;
        font2 = new Font(dataDir, 15);

on Lines 97 - 100 on "Demo.cs". Worked for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文