Sign in to follow this  
manjunathssv

SDL window in MFC

Recommended Posts

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 <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #ifdef WIN32 # include <windows.h> # 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<NUM_COLORS; ++i ) { palette[i].b = (NUM_COLORS-1)-i * (256 / NUM_COLORS); palette[i].g = (NUM_COLORS-1)-i * (256 / NUM_COLORS); palette[i].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; i<screen->h; ++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(&rect, sizeof(RECT)); rect.right = width ; rect.bottom = height +height; // AdjustWindowRectEx(&rect, 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); }

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this