Skip to main content
GameDev.net gamedev.net
🔒 Locked

SDL window in MFC

Started by manjunathssv Jun 6, 2006 at 8:57 AM 0 replies 2.8k views
Original Post
manjunathssv
manjunathssv
Hi , Can I create SDL(simple Directmedia layer) With somw other window control on the single window .... But below is the sample code which creates a window and just place a SDL screen on it. but what i need is to place some button(like play,stop,pause) beside the sdl screen in the same window... /* Simple program: Fill a colormap with gray and stripe it down the screen */ #include #include #include #include #ifdef WIN32 # include # include "Resource.h" static HWND g_hwnd; #endif /* WIN32 */ #include "SDL.h" #ifdef TEST_VGA16 /* Define this if you want to test VGA 16-color video modes */ #define NUM_COLORS 16 #else #define NUM_COLORS 256 #endif HWND g_hToolbar = NULL,hWndToolbox; /******************************************************************************/ LRESULT CALLBACK ToolboxProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam) { switch(Msg) { case WM_INITDIALOG: return TRUE; } return FALSE; } /**********************************************************************************/ BOOL CALLBACK ToolDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) { switch(Message) { case WM_COMMAND: switch(LOWORD(wParam)) { case IDC_PRESS: MessageBox(hwnd, "Hi!", "This is a message", MB_OK | MB_ICONEXCLAMATION); break; case IDC_OTHER: MessageBox(hwnd, "Bye!", "This is also a message", MB_OK | MB_ICONEXCLAMATION); break; } break; default: return FALSE; } return TRUE; } /* Draw a randomly sized and colored box centered about (X,Y) */ void DrawBox(SDL_Surface *screen, int X, int Y) { static unsigned int seeded = 0; SDL_Rect area; Uint32 color; /* Seed the random number generator */ if ( seeded == 0 ) { srand(time(NULL)); seeded = 1; } /* Get the bounds of the rectangle */ area.w = (rand()%640); area.h = (rand()%480); area.x = X-(area.w/2); area.y = Y-(area.h/2); color = (rand()%NUM_COLORS); /* Do it! */ SDL_FillRect(screen, &area, color); SDL_UpdateRects(screen, 1, &area); } SDL_Surface *CreateScreen(Uint16 w, Uint16 h, Uint8 bpp, Uint32 flags) { SDL_Surface *screen; int i; SDL_Color palette[NUM_COLORS]; Uint8 *buffer; /* Set the video mode */ screen = SDL_SetVideoMode(320, 240, bpp, flags); if ( screen == NULL ) { fprintf(stderr, "Couldn't set display mode: %s\n", SDL_GetError()); return(NULL); } fprintf(stderr, "Screen is in %s mode\n", (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed"); /* Set a gray colormap, reverse order from white to black */ for ( i=0; i.b = (NUM_COLORS-1)-i * (256 / NUM_COLORS); palette.g = (NUM_COLORS-1)-i * (256 / NUM_COLORS); palette.r = (NUM_COLORS-1)-i * (256 / NUM_COLORS); } SDL_SetColors(screen, palette, 0, NUM_COLORS); /* Set the surface pixels and refresh! */ if ( SDL_LockSurface(screen) < 0 ) { fprintf(stderr, "Couldn't lock display surface: %s\n", SDL_GetError()); return(NULL); } buffer = (Uint8 *)screen->pixels; for ( i=0; ih; ++i ) { memset(buffer,(i*(NUM_COLORS-1))/screen->h, screen->w); buffer += screen->pitch; } SDL_UnlockSurface(screen); SDL_UpdateRect(screen, 0, 0, 0, 0); return(screen); } #ifdef WIN32 LRESULT CALLBACK graywinWindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { SDL_Event event; switch(uMsg) { case WM_COMMAND: /* LOWORD find exact event */ switch(LOWORD(wParam)) { case ID_QUIT : event.type = SDL_QUIT; SDL_PushEvent(&event); break; case ID_DIALOG_SHOW: ShowWindow(g_hToolbar, SW_SHOW); break; case ID_DIALOG_HIDE: ShowWindow(g_hToolbar, SW_HIDE); break; } break; case WM_CREATE: g_hToolbar = CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_TOOLBAR), hwnd, ToolDlgProc); if(g_hToolbar != NULL) { ShowWindow(g_hToolbar, SW_SHOW); } else { MessageBox(hwnd, "CreateDialog returned NULL", "Warning!", MB_OK | MB_ICONINFORMATION); } break; default: return DefWindowProc(hwnd, uMsg, wParam, lParam); break; } return 1; } int init_win32_window(HINSTANCE hInst, int width, int height) { MSG msg; char windowid[100]; WNDCLASSEX wcex; RECT rect; ZeroMemory(&wcex, sizeof(wcex)); wcex.cbSize = sizeof(wcex); wcex.style = CS_HREDRAW; wcex.lpfnWndProc = graywinWindowProc; wcex.hInstance = hInst; wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_ICON)); wcex.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH); //wcex.lpszMenuName = MAKEINTRESOURCE(IDR_MENU); wcex.lpszClassName = "Mainframe"; if (!RegisterClassEx(&wcex)) { MessageBox(NULL, "could not register class", "Error", MB_ICONERROR); return 0; } ZeroMemory(▭, sizeof(RECT)); rect.right = width ; rect.bottom = height +height; // AdjustWindowRectEx(▭, WS_OVERLAPPEDWINDOW, TRUE, 0); g_hwnd = CreateWindowEx( WS_EX_STATICEDGE , "Mainframe", "SDL Window", 0 , CW_USEDEFAULT, CW_USEDEFAULT, abs(rect.left) + rect.right, abs(rect.top) + rect.bottom , NULL, LoadMenu(hInst, MAKEINTRESOURCE(IDR_MENU)), hInst, NULL); if (!g_hwnd) { MessageBox(NULL, "could not create window", "Error", MB_ICONERROR); return 0; } // Create a child window based on the available dialog box hWndToolbox = CreateDialog(hInst, MAKEINTRESOURCE(IDD_TOOLBOX_DLG), g_hwnd, (DLGPROC)ToolboxProc); ShowWindow (hWndToolbox, SW_SHOW); ShowWindow(g_hwnd ,SW_SHOWNORMAL ); while (GetMessage(&msg,NULL, 0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } ShowWindow(g_hwnd, SW_SHOWDEFAULT ); UpdateWindow(g_hwnd); strcpy(windowid, "SDL_WINDOWID="); _ltoa((long)g_hwnd, windowid + 13, 10); _putenv(windowid); return 1; } #endif /* WIN32 */ int main(int argc, char *argv[]) { SDL_Surface *screen = NULL; Uint32 videoflags; int done; SDL_Event event; Uint16 width, height; Uint8 bpp; /* See if we try to get a hardware colormap */ width = 320; height = 240; bpp = 8; videoflags = SDL_SWSURFACE; while ( argc > 1 ) { --argc; if ( argv[argc-1] && (strcmp(argv[argc-1], "-width") == 0) ) { width = atoi(argv[argc]); --argc; } else if ( argv[argc-1] && (strcmp(argv[argc-1], "-height") == 0) ) { height = atoi(argv[argc]); --argc; } else if ( argv[argc-1] && (strcmp(argv[argc-1], "-bpp") == 0) ) { bpp = atoi(argv[argc]); --argc; } else if ( argv[argc] && (strcmp(argv[argc], "-hw") == 0) ) { videoflags |= SDL_HWSURFACE; } else if ( argv[argc] && (strcmp(argv[argc], "-hwpalette") == 0) ) { videoflags |= SDL_HWPALETTE; } else if ( argv[argc] && (strcmp(argv[argc], "-noframe") == 0) ) { videoflags |= SDL_NOFRAME; } else if ( argv[argc] && (strcmp(argv[argc], "-fullscreen") == 0) ) { videoflags |= SDL_FULLSCREEN; } else { fprintf(stderr, "Usage: %s [-warp] [-fullscreen]\n", argv[0]); exit(1); } } # ifdef WIN32 init_win32_window(GetModuleHandle(NULL), width, height); # endif //#ifdef XX /* Initialize SDL */ if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); exit(1); } // Set a video mode screen = CreateScreen(width, height, bpp, videoflags); if ( screen == NULL ) { exit(2); } // Wait for a keystroke/ done = 0; while ( !done && SDL_WaitEvent(&event) ) { switch (event.type) { case SDL_QUIT: done = 1; break; default: break; } } SDL_Quit(); //#endif return(0); }
xEricx
xEricx
I've never done it myself, but google for SDL_WINDOWID, I think there's a way to force SDL to be initialized within a specific window. It includes messing with environment variables but you should find out easily how to do it.

Hope this helps

Eric

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.