还是想写点记录,免得以后忘了,前几天一直遇到一个问题:
我把SDL 生成的 screen 嵌到MFC中,我的第一想法,就是覆盖:把生成的SDL窗口直接放到指定的位置。
可以,我把SDL 的API翻了个遍,就是没有对应的函数,一个偶尔的机会,我发现了 DIB_CreateWindow (在文件SDL_dibevents.c中)中有这样一段代码:
int DIB_CreateWindow(_THIS) { char *windowid; SDL_RegisterApp(NULL, 0, 0); windowid = SDL_getenv("SDL_WINDOWID"); SDL_windowid = (windowid != NULL); if ( SDL_windowid ) { #if defined(_WIN32_WCE) && (_WIN32_WCE < 300) /* wince 2.1 does not have strtol */ wchar_t *windowid_t = SDL_malloc((SDL_strlen(windowid) + 1) * sizeof(wchar_t)); MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, windowid, -1, windowid_t, SDL_strlen(windowid) + 1); SDL_Window = (HWND)wcstol(windowid_t, NULL, 0); SDL_free(windowid_t); #else SDL_Window = (HWND)((size_t)SDL_strtoull(windowid, NULL, 0)); #endif if ( SDL_Window == NULL ) { SDL_SetError("Couldn't get user specified window"); return(-1); } /* DJM: we want all event's for the user specified window to be handled by SDL. */ userWindowProc = (WNDPROCTYPE)GetWindowLongPtr(SDL_Window, GWLP_WNDPROC); SetWindowLongPtr(SDL_Window, GWLP_WNDPROC, (LONG_PTR)WinMessage); } else { SDL_Window = CreateWindow(SDL_Appname, SDL_Appname, (WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX), CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, NULL, NULL, SDL_Instance, NULL); if ( SDL_Window == NULL ) { SDL_SetError("Couldn't create window"); return(-1); } ShowWindow(SDL_Window, SW_HIDE); } /* JC 14 Mar 2006 Flush the message loop or this can cause big problems later Especially if the user decides to use dialog boxes or assert()! */ WIN_FlushMessageQueue(); return(0); }
SDL_Init(flags); atexit(SDL_Quit);// 注册SDL_Quit,当退出时调用,使得退出时程序自动清理 if(hWnd){ this->sdlHWnd = hWnd ; WCHAR wch[20],iwch[20]; _ltow((long)hWnd,iwch,10); SetEnvironmentVariable(AnsiToUnicode("SDL_WINDOWID",wch,20), iwch); //设置环境变量 } flags = SDL_HWSURFACE | SDL_HWPALETTE | SDL_DOUBLEBUF |SDL_RESIZABLE |SDL_NOFRAME; Surf_Display = SDL_SetVideoMode(surWidth,surHeight,0,flags); SDL_Flip(Surf_Display);