简体   繁体   English

使用Windows API和C ++进行2D(或3D)图形编程?

[英]2D (or 3D) Graphics Programming using Windows API with C++?

I am about to start my project for a university course unit. 我即将开始我的大学课程单元项目。 I am looking to make something a bit like asteroids, the retro 2D game, in c++. 我正在寻找用c ++制作类似于复古2D游戏的小行星的东西。

I could do this using something like SFML, which would certainly seem to be the logical thing to do. 我可以使用SFML之类的方法来做到这一点,这似乎是合乎逻辑的事情。 However I cannot use SFML, as the libraries are not available by default on Windows. 但是,我无法使用SFML,因为这些库在Windows默认情况下不可用。

The requirement is that my c++ code must be copied and pasted onto any Windows PC (with Visual Studio already installed) and then compiled/run from Visual Studio. 要求是必须将我的c ++代码复制并粘贴到任何Windows PC(已安装Visual Studio)上,然后从Visual Studio编译/运行。

I have never used the Windows API before, however I get the feeling I might be able to use it to open a window and draw some 2d graphics within the window. 我以前从未使用过Windows API,但是我感到可以使用它来打开一个窗口并在该窗口中绘制一些2d图形。 Am I correct? 我对么? Essentially this is all I need to be able to do: Open a window then draw some lines on it. 本质上,这就是我所需要做的:打开一个窗口,然后在其上画一些线。

This is not a "can you do my homework for me" type of question. 这不是“您能帮我做作业”吗? Please could someone point me in the right direction for getting started with WinAPI if this is the tool I need, and if not is there something else I can use instead? 如果这是我需要的工具,请有人为我指出WinAPI入门的正确方向,如果没有,我可以使用其他方法代替吗? What about some sort of OpenGL libraries? 那么某种OpenGL库呢? Are the OpenGL 2.0 or similar libraries bundled with Visual Studio? OpenGL 2.0或类似的库是否与Visual Studio捆绑在一起?

(In case it is significant, I think we are using VS 2010.) (如果意义重大,我认为我们正在使用VS2010。)

Cheers everyone, 欢呼大家

You can open a window, create some sort of canvas and draw on it, but I don't know if that is rendered by software or hardware. 您可以打开一个窗口,创建某种画布并在其上绘制,但是我不知道它是由软件还是由硬件渲染的。 In your case, if it is a very small game you might not need hardware rendering. 就您而言,如果它是一个很小的游戏,则可能不需要硬件渲染。

However, if you want hardware rendering through DirectX , you can try Mircrosoft XNA Game Studios 4.0 , it comes free for universities with DreamSpark subscription. 但是,如果您希望通过DirectX硬件渲染,则可以尝试Mircrosoft XNA Game Studios 4.0 ,该软件对于订阅DreamSpark的大学免费提供。

This tutorial covers just avout everything you need for creating a graphical Win32 app: 本教程仅介绍创建图形化Win32应用所需的一切:

http://www.winprog.org/tutorial/ http://www.winprog.org/tutorial/

Please note that some older versions of visual studio express couldn't compile windows application unless windows sdk is installed. 请注意,除非安装了Windows sdk,否则某些较早版本的Visual Studio Express无法编译Windows应用程序。 If that's the case with your visual studio installations, then you won't be able to do any graphics at all with requirements you listed. 如果安装Visual Studio就是这种情况,那么您将完全无法按照列出的要求来制作任何图形。

Please could someone point me in the right direction for getting started with WinAPI 请有人指出我正确的方向开始使用WinAPI

Sure. 当然。

The requirement is that my c++ code must be copied and pasted onto any Windows PC (with Visual Studio already installed) and then compiled/run from Visual Studio. 要求是必须将我的c ++代码复制并粘贴到任何Windows PC(已安装Visual Studio)上,然后从Visual Studio编译/运行。

You know, you can simply include code of whatever library you use into your project. 您知道,您可以简单地在项目中包括使用的任何库的代码。 Because projects like libsdl use permissive licenses, you could do that. 因为像libsdl这样的项目使用许可许可证,所以您可以这样做。 Not sure if that would break your "rules", though. 不过,不确定是否会破坏您的“规则”。

With default visual studio installation you don't get much. 使用默认的Visual Studio安装,您获得的收益不多。 You definitely can create a window and draw something on it with pure winapi. 您绝对可以使用纯winapi创建一个窗口并在其上绘制一些东西。 Using something like SetDIBitsToDevice you could easily do full software rendering yourself. 使用SetDIBitsToDevice之类的东西,您可以轻松地自己制作完整的软件。

Advanced OpenGL features require wrapper library (like GLEW ), but you can initialize extensions and advanced features yourself. OpenGL的高级功能需要包装器库(例如GLEW ),但是您可以自己初始化扩展名和高级功能。 Quake3 engine did that, so you could do that to. Quake3引擎做到了这一点,因此您可以这样做。

Using DirectX most likely is out of question, because it normally requires installation of SDK. 使用DirectX最有可能成为问题,因为它通常需要安装SDK。 You could copy/paste required libraries/headers into subdirectory of your project, but then again, it is unclear if this would break the "rules". 您可以将所需的库/标头复制/粘贴到项目的子目录中,但是同样,还不清楚这是否会破坏“规则”。

Are the OpenGL 2.0 or similar libraries bundled with Visual Studio? OpenGL 2.0或类似的库是否与Visual Studio捆绑在一起?

Default OpenGL header supports version 1.1 or something like that. 默认的OpenGL标头支持1.1版或类似版本。 However, you can load missing functions yourself using wglGetProcAddress . 但是,您可以使用wglGetProcAddress自己加载缺少的函数。 You can also declare missing constants yourself in your code. 您也可以自己在代码中声明缺少的常量。 Whether it is worth it or not, is another story. 是否值得,是另一回事。

Bottom line: 底线:

If you want full software rendering, create window using winapi, allocate 2d buffer for a "screen" (RGB format) and blit it onto window every frame using SetDIBitsToDevice or StretchDIBitsToDevice. 如果要进行完整的软件渲染,请使用winapi创建窗口,为“屏幕”(RGB格式)分配2d缓冲区,然后使用SetDIBitsToDevice或StretchDIBitsToDevice将其blit到窗口上。 That'll work and compile pretty much anywhere, if windows headers are available. 如果Windows标头可用,那么它将在几乎任何地方工作并编译。

You could also try using GDI routines for drawing lines, etc, but I'm not sure how well that would work for a game. 您也可以尝试使用GDI例程绘制线条等,但是我不确定这对游戏的效果如何。 I haven't used GDI in quite a while, but I think you can get noticeable flickering if you aren't careful. 我已经有一段时间没有使用GDI了,但是如果您不小心,我认为您会出现明显的闪烁。

If you need advanced features (for example, for some people it'll be difficult to write software rendering routines that can draw textured triangles or can rotate 2d sprites) consider using OpenGL and loading missing functions using wglGetProcAddress. 如果需要高级功能(例如,对于某些人来说,编写可以绘制带纹理的三角形或可以旋转2d精灵的软件渲染例程将很困难),请考虑使用OpenGL并使用wglGetProcAddress加载缺少的函数。 However, if your university (for some reason) has ancient(or faulty) hardware, you might want to limit yourself to an old version of OpenGL (OpenGL 1.1). 但是,如果您的大学(出于某种原因)使用的是较旧的(或有故障的)硬件,则您可能希望将自己限于旧版本的OpenGL(OpenGL 1.1)。 The reason is that OpenGL 1.1 is supported by software OpenGL renderer which will be enabled in case there's some kind of trouble using hardware acceleration. 原因是软件OpenGL渲染器支持OpenGL 1.1,以防在使用硬件加速时遇到某种麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM