幻想森林

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 5047|回复: 24

[通用编程] [很麻烦]opengl,step 0

[复制链接]

136

主题

1751

帖子

548

积分

版主

Rank: 7Rank: 7Rank: 7

积分
548
发表于 2007-11-9 22:01:15 | 显示全部楼层 |阅读模式
  1. //test.d
  2. //save as utf 16 LE
  3. //dmd test.d -L/exet:nt/su:windows:4.0
  4. import win32.windows;
  5. import std.c.string;
  6. import opengl.gl;
  7. pragma(lib,"win32.lib");
  8. pragma(lib,"gdi32.lib");
  9. pragma(lib,"opengl32.lib");
  10. pragma(lib,"glu32.lib");
  11. /*  Make the class name into a global variable  */
  12. const wchar* szClassName = "OpenGL";
  13. const wchar* szAppName = "OpenGL测试";
  14. extern (C) void gc_init();
  15. extern (C) void gc_term();
  16. extern (C) void _minit();
  17. extern (C) void _moduleCtor();
  18. extern (C) void _moduleDtor();
  19. extern (C) void _moduleUnitTests();
  20. extern (Windows) int WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
  21. {
  22.     int result;
  23.     gc_init();          // initialize garbage collector
  24.     _minit();           // initialize module constructor table
  25.     try
  26.     {
  27.         _moduleCtor();      // call module constructors
  28.         _moduleUnitTests(); // run unit tests (optional)
  29.         result = myWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
  30.         _moduleDtor();      // call module destructors
  31.     }
  32.     catch (Object o)        // catch any uncaught exceptions
  33.     {
  34.         MessageBoxA(null, cast(char *)o.toString(), "Error",
  35.                     MB_OK | MB_ICONEXCLAMATION);
  36.         result = 0;     // failed
  37.     }
  38.     gc_term();          // run finalizers; terminate garbage collector
  39.     return result;
  40. }
  41. int myWinMain (HINSTANCE hThisInstance,HINSTANCE hPrevInstance,LPSTR lpszArgument,int nFunsterStil)
  42. {
  43.     HWND hWnd;               /* This is the handle for our window */
  44.     MSG messages;            /* Here messages to the application are saved */
  45.     WNDCLASSEXW wincl;        /* Data structure for the windowclass */
  46.     HDC hDC;
  47.     HGLRC hRC;
  48.     bool bQuit = false;
  49.     /* The Window structure */
  50.     wincl.hInstance = hThisInstance;
  51.     wincl.lpszClassName = szClassName;
  52.     wincl.lpfnWndProc = &WindowProcedure;      /* This function is called by windows */
  53.     wincl.style = CS_OWNDC;                 /* Catch double-clicks */
  54.     wincl.cbSize = WNDCLASSEXW.sizeof;
  55.     /* Use default icon and mouse-pointer */
  56.     wincl.hIcon = null;
  57.     wincl.hIconSm = null;
  58.     wincl.hCursor = null;
  59.     wincl.lpszMenuName = null;                 /* No menu */
  60.     wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
  61.     wincl.cbWndExtra = 0;                      /* structure or the window instance */
  62.     /* Use Windows's default color as the background of the window */
  63.     wincl.hbrBackground = cast(HBRUSH) COLOR_BACKGROUND;
  64.     /* Register the window class, and if it fails quit the program */
  65.     if (!RegisterClassExW (&wincl))
  66.         return 0;
  67.     /* The class is registered, let's create the program*/
  68.     hWnd = CreateWindowExW (
  69.                0,                   /* Extended possibilites for variation */
  70.                szClassName,         /* Classname */
  71.                szAppName,           /* Title Text */
  72.                WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE, /* default window */
  73.                CW_USEDEFAULT,       /* Windows decides the position */
  74.                CW_USEDEFAULT,       /* where the window ends up on the screen */
  75.                250,                 /* The programs width */
  76.                250,                 /* and height in pixels */
  77.                HWND_DESKTOP,        /* The window is a child-window to desktop */
  78.                null,                /* No menu */
  79.                hThisInstance,       /* Program Instance handler */
  80.                null                 /* No Window Creation data */
  81.            );
  82.     /* enable OpenGL for the window */
  83.     EnableOpenGL (hWnd, &hDC, &hRC);
  84.     /* Make the window visible on the screen */
  85.     ShowWindow (hWnd, nFunsterStil);
  86.     while (!bQuit)
  87.     {
  88.         if (PeekMessage(&messages, null, 0, 0, PM_REMOVE))
  89.         {
  90.             if (messages.message == WM_QUIT)
  91.             {
  92.                 bQuit = true;
  93.             }
  94.             else
  95.             {
  96.                 /* Translate virtual-key messages into character messages */
  97.                 TranslateMessage(&messages);
  98.                 /* Send message to WindowProcedure */
  99.                 DispatchMessageW(&messages);
  100.             }
  101.         }
  102.         else
  103.         {
  104.             draw(hDC);
  105.         }
  106.     }
  107.     /* shutdown OpenGL */
  108.     DisableOpenGL (hWnd, hDC, hRC);
  109.     /* destroy the window explicitly */
  110.     DestroyWindow (hWnd);
  111.     /* The program return-value is 0 - The value that PostQuitMessage() gave */
  112.     return messages.wParam;
  113. }
  114. /*  This function is called by the Windows function DispatchMessage()  */
  115. extern(Windows) LRESULT WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  116. {
  117.     switch (message)                  /* handle the messages */
  118.     {
  119.     case WM_DESTROY:
  120.         PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
  121.         break;
  122.     default:                      /* for messages that we don't deal with */
  123.         return DefWindowProcW (hwnd, message, wParam, lParam);
  124.     }
  125.     return 0;
  126. }
  127. void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
  128. {
  129.     PIXELFORMATDESCRIPTOR pfd;
  130.     int iFormat;
  131.     /* get the device context (DC) */
  132.     *hDC = GetDC (hWnd);
  133.     /* set the pixel format for the DC */
  134.     memset(&pfd, pfd.sizeof,1);
  135.     pfd.nSize = pfd.sizeof;
  136.     pfd.nVersion = 1;
  137.     pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  138.     pfd.iPixelType = PFD_TYPE_RGBA;
  139.     pfd.cColorBits = 24;
  140.     pfd.cDepthBits = 16;
  141.     pfd.iLayerType = 0;
  142.     //pfd.iLayerType = PFD_MAIN_PLANE;
  143.     iFormat = ChoosePixelFormat (*hDC, &pfd);
  144.     SetPixelFormat (*hDC, iFormat, &pfd);
  145.     /* create and enable the render context (RC) */
  146.     *hRC = wglCreateContext( *hDC );
  147.     wglMakeCurrent( *hDC, *hRC );
  148. }
  149. void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
  150. {
  151.     wglMakeCurrent (null,null);
  152.     wglDeleteContext (hRC);
  153.     ReleaseDC (hWnd, hDC);
  154. }
  155. void draw(HDC hDC)
  156. {
  157.     glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
  158.     glClear (GL_COLOR_BUFFER_BIT);
  159.     glPushMatrix ();
  160.     glRotatef (0.0f, 0.0f, 0.0f, 1.0f);
  161.     glBegin (GL_TRIANGLES);
  162.     glColor3f (1.0f, 0.0f, 0.0f);
  163.     glVertex2f (0.0f, 1.0f);
  164.     glColor3f (0.0f, 1.0f, 0.0f);
  165.     glVertex2f (0.87f, -0.5f);
  166.     glColor3f (0.0f, 0.0f, 1.0f);
  167.     glVertex2f (-0.87f, -0.5f);
  168.     glEnd ();
  169.     glPopMatrix ();
  170.     SwapBuffers (hDC);
  171.     Sleep (1);
  172. }
复制代码
え~え~お!!!
回复

使用道具 举报

50

主题

742

帖子

402

积分

版主

自定义头衔

Rank: 7Rank: 7Rank: 7

积分
402
发表于 2007-11-12 20:50:36 | 显示全部楼层
=0= 用D的……上次看到这个去下载了GDC来……
BUT就忘了编译……为什么不能够用C写……这样可以省力啊……
Style-C
回复 支持 反对

使用道具 举报

136

主题

1751

帖子

548

积分

版主

Rank: 7Rank: 7Rank: 7

积分
548
 楼主| 发表于 2007-11-12 22:01:03 | 显示全部楼层
c的话,对于我个人来说有三个理由吧。
1.c的基本数据类看着别扭,unsign char ...不如直接ubyte多好。还有c不是强类型。
2.c里面没有委托可用。
3.c多线程的时候不是一般的麻烦。
而现在D的麻烦只是在库上,一旦越过了,后面就没有太麻烦的问题了。必竟D是号称能和ruby比开发效率,和c++比运行效率的新语言嘛。虽然我对此没有任何体会。[s:5]

ps.如果是用c,我就直接去用irrlicht了,绝对不会多看dx&opengl一眼。  [s:4]
え~え~お!!!
回复 支持 反对

使用道具 举报

50

主题

742

帖子

402

积分

版主

自定义头衔

Rank: 7Rank: 7Rank: 7

积分
402
发表于 2007-11-13 19:25:49 | 显示全部楼层
不解ING……
主要是用C可能比较适合偶看而已……,D今天看FX的那段闭包完全看不懂
如果光是函数里面嵌套函数也就罢了,什么delegate(int)……

之后又一堆东东,所以结果容易变成语法课了啊……
Style-C
回复 支持 反对

使用道具 举报

8

主题

215

帖子

2223

积分

⑥精研

积分
2223
发表于 2007-11-13 20:14:37 | 显示全部楼层
int delegate(int) ptr就是int (*ptr)(int)啊...这样容易明白些么?
在D有完全闭包前,它的delegate/function pointer很难说比C/C++的有多少改良...本来就是一回事。
不过在有完全闭包后,D里面的delegate就比C的要有更强的能力,在作为参数传递的时候也会传递更多信息。
……不过我觉得shawind应该没用到什么需要这种能力的地方……C里的函数指针估计是完全够用
回复 支持 反对

使用道具 举报

136

主题

1751

帖子

548

积分

版主

Rank: 7Rank: 7Rank: 7

积分
548
 楼主| 发表于 2007-11-13 20:21:42 | 显示全部楼层
是的,每个人的习惯不一样,喜爱的语言也不一样。

delegate(int)就是申明了一个委托类型。在c#,d这类语言中,用委托比在c中只能使用函数指针要安全很多。
居说c++的boost库里也实现了多线程和(委托?)之类的东西。但是上次尝试编译krkr2的时候,boost在我的心目里留下了很不友好的印像,所以一直都没有怎么接触。
え~え~お!!!
回复 支持 反对

使用道具 举报

136

主题

1751

帖子

548

积分

版主

Rank: 7Rank: 7Rank: 7

积分
548
 楼主| 发表于 2007-11-13 20:30:17 | 显示全部楼层
因为我是用完全面向对像的思路来考虑实现引擎的。委托带有它周围的环境信息,在有很多类的时候,比较不容易指错地方。当然高手用c也能完成这种任务,但我只是菜鸟来着,完全不会Debug......
え~え~お!!!
回复 支持 反对

使用道具 举报

8

主题

215

帖子

2223

积分

⑥精研

积分
2223
发表于 2007-11-13 20:33:35 | 显示全部楼层
尝试编译krkr2么?(苦笑
那里面boost应该不是带来什么麻烦,如果你用对了版本的话。麻烦是麻烦在另外的一些依赖上吧。我极其厌恶的一些东西,例如VCL什么的……也有一些库被稍微改过?这个得问问米粒看他知不知道。

C不是强类型……==,什么是强类型?
另外D里的delegate比C的函数指针安全么?hmm...没看出来。
Delegates are an aggregate of two pieces of data: an object reference and a function pointer.
===========================我是分隔线============================
委托带有它周围的环境信息,在有很多类的时候,比较不容易指错地方。
这个值得商榷。能给出一个例子,来说明一下怎样会指错,而D里的delegate如何协助不指错吗?
回复 支持 反对

使用道具 举报

136

主题

1751

帖子

548

积分

版主

Rank: 7Rank: 7Rank: 7

积分
548
 楼主| 发表于 2007-11-13 21:01:28 | 显示全部楼层
我是卡在了boost上,后面就没有耐心去做了。

c或许也算强类型的一种,可是在D中声明了struct,class后,就可以把它当作新的数据类型来使用,还有D中用typedef int i; 把i当作一个全新的类型,c中的类型显然是没有这么“强”的。

委托的例子的话,就比如说,有两个类,一个是人物A,一个是人物B,都有一个类似的方法,move(int x, int y); 如果我要区别使用这两方法,用委托应该比用函数指针好吧。我是这么想的,还没有做过这样的实验。
え~え~お!!!
回复 支持 反对

使用道具 举报

8

主题

215

帖子

2223

积分

⑥精研

积分
2223
发表于 2007-11-13 21:23:38 | 显示全部楼层
根据米粒的指导,用Boost 1.30基本上没出什么问题……不过始终我还是编译不出krkr2的内核的。也罢。
Alright,那来看看:
  1. class A {
  2.     bool move(int x, int y) {
  3.         return true;
  4.     }
  5. }
  6. class B {
  7.     bool move(int x, int y) {
  8.         return false;
  9.     }
  10. }
  11. bool delegate(int, int) dg;
  12. void main(char[][] args) {
  13.     A a = new A;
  14.     B b = new B;
  15.     dg = &a.move;
  16.     printf("%d\\n", dg(1, 2));
  17.     dg = &b.move;
  18.     printf("%d\\n", dg(3, 4));
  19. }
复制代码
并没有区别A与B。事实上函数指针就该这么用。对一个函数指针或者委托,重要的只是参数个数和类型,以及返回类型;至于所指向的成员方法属于什么类型的类,不是函数指针需要关心的。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|幻想森林

GMT+8, 2024-5-3 04:55 , Processed in 0.031065 second(s), 20 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表