[c][résolu] SDL et API Windows

SDL et API Windows [c][résolu] - C - Programmation

Marsh Posté le 31-07-2006 à 03:06:56    

Je programme en C avec entre autres la SDL et l'API Windows, je maîtrise en gros les deux, et j'aimerai bien savoir comme faire pour intégrer une zone SDL dans une fenetre Windows de ce type (là il n'y a pas le menu):

Code :
  1. #include <windows.h>
  2. LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
  3. int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevHinstance, LPSTR lpCmdLine, int nCmdShow)
  4. {
  5.     HWND hwnd;
  6.     WNDCLASS wc;
  7.     MSG msg;
  8.     wc.style = 0;
  9.     wc.lpfnWndProc = MainWndProc;
  10.     wc.cbClsExtra = 0;
  11.     wc.cbWndExtra = 0;
  12.     wc.hInstance = hinstance;
  13.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  14.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  15.     wc.hbrBackground = (HBRUSH) (1 + COLOR_BTNFACE);
  16.     wc.lpszMenuName = NULL;
  17.     wc.lpszClassName = "MaWinClass";
  18.     if(!RegisterClass(&wc)) return FALSE;
  19.     hwnd = CreateWindow("MaWinClass", "The titre",
  20.                         WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 400, 300,
  21.                         NULL, NULL, hinstance, NULL);
  22.     if(!hwnd) return FALSE;
  23.     ShowWindow(hwnd, nCmdShow);
  24.     UpdateWindow(hwnd);
  25.     while(GetMessage(&msg, NULL, 0, 0))
  26.     {
  27.         TranslateMessage(&msg);
  28.         DispatchMessage(&msg);
  29.     }
  30.     return msg.wParam;
  31. }
  32. /******************************************************************************/
  33. LRESULT CALLBACK MainWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  34. {
  35.     switch(uMsg)
  36.     {
  37.         case WM_CREATE:
  38.             return 0;
  39.             break;
  40.         case WM_DESTROY:
  41.             PostQuitMessage(0);
  42.             return 0;
  43.             break;
  44.         default:
  45.             return DefWindowProc(hwnd, uMsg, wParam, lParam);
  46.     }
  47. }


 
http://img124.imageshack.us/img124/2893/fenetredz7.png
 
Merci d'avance.
 
Amicalement,  
Pcsnake


Message édité par Pcsnake le 02-08-2006 à 14:07:36
Reply

Marsh Posté le 31-07-2006 à 03:06:56   

Reply

Marsh Posté le 31-07-2006 à 12:08:39    

Ca fait un moment que je n'ai pas utiliser la SDL mais à l'époque, j'utilisais :  
 

Code :
  1. char windowid[100];
  2.    [...]
  3.    strcpy(windowid, "SDL_WINDOWID=" );
  4.    _ltoa((long)hwnd, windowid+13, 10);
  5.    _putenv(windowid);


 
Juste apres CreateWindow().

Reply

Marsh Posté le 31-07-2006 à 13:38:59    

Euh le [...] je suppose que c'est mon contenu SDL n'est-ce pas?

Reply

Marsh Posté le 31-07-2006 à 14:08:50    

Non, il faut le faire avant la création de ta fenêtre SDL. Sinon, ça sert à quoi de spécifier un id de fenêtre après avoir initialisé SDL ;)
 
Les [...] c'est la récupération de ton id de fenêtre.

Reply

Marsh Posté le 31-07-2006 à 14:17:19    

Bin a vrai dire j'ai du mal a comprendre ce code... Je ne connais aucune des fonctions utilisées donc voilou lol, maintenant je connais en gros grâce a google (GEMA) mais c'est assez limité, en tout cas merci beaucoup.
 
EDIT: Si jai bien compris jutilise hwnd comme une SDL_Surface?


Message édité par Pcsnake le 31-07-2006 à 14:24:16
Reply

Marsh Posté le 31-07-2006 à 16:12:31    

Je pensais que c'était clair pourtant ... le code, tu le copie colle apres CreateWindow(), apres tu init la SDL et tu créés ta surface.

Reply

Marsh Posté le 31-07-2006 à 18:36:44    

Ouais mais ça me donne ces erreurs de compilation:
Compiling: main.cpp
main.cpp: In function `int WinMain(HINSTANCE__*, HINSTANCE__*, CHAR*, int)':
main.cpp:32: error: expected primary-expression before '[' token
main.cpp:32: error: expected primary-expression before '...' token
main.cpp:32: error: expected `]' before '...' token
main.cpp:32: error: expected `;' before '...' token
Process terminated with status 1 (0 minutes, 2 seconds)

Reply

Marsh Posté le 31-07-2006 à 19:21:06    

t'as quand même pas copier/coller '[...]' dans ton code?! :ouch:  
 
Comme je te l'ai dit:

Citation :

Les [...] c'est la récupération de ton id de fenêtre.


Ca veut pas dire qu'en tapant ça, tu vas récupérer ton id de fenêtre par magie  [:pingouino]  
 
Si c'est pas ça, désolé, mais ça y ressemble vraiment méchamment :D
 
Sinon, sans code, on pourra rien te dire...
 
Edit: l'id de ta fenêtre c'est le HWND; faudra peut-être que tu le cast en int. Je l'avais sous linux, et la question se posait pas :p


Message édité par IrmatDen le 31-07-2006 à 19:37:10
Reply

Marsh Posté le 31-07-2006 à 19:56:43    

Code :
  1. #include <windows.h>
  2. #include <SDL/SDL.h>
  3. #include <SDL/SDL_Image.h>
  4. LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
  5. int WINAPI WinMain (HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
  6.                     int nCmdShow)
  7. {
  8.     HWND hwnd;
  9.     WNDCLASS wc;
  10.     MSG msg;
  11.     wc.style = 0;
  12.     wc.lpfnWndProc = MainWndProc;
  13.     wc.cbClsExtra = 0;
  14.     wc.cbWndExtra = 0;
  15.     wc.hInstance = hinstance;
  16.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  17.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  18.     wc.hbrBackground = (HBRUSH) (1 + COLOR_BTNFACE);
  19.     wc.lpszMenuName = NULL;
  20.     wc.lpszClassName = "MaWinClass";
  21.     if(!RegisterClass(&wc)) return FALSE;
  22.     hwnd = CreateWindow("MaWinClass", "Essai SDL + API Win32", WS_OVERLAPPEDWINDOW,
  23.                         CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL,
  24.                         hinstance, NULL);
  25.     if(!hwnd) return FALSE;
  26.     char windowid[100];
  27.     hwnd;
  28.     strcpy(windowid, "SDL_WINDOWID=" );
  29.     _ltoa((long)hwnd, windowid+13, 10);
  30.     _putenv(windowid);
  31.     SDL_Init(SDL_INIT_VIDEO);
  32.     SDL_Surface *ecran = NULL, *photo = NULL;
  33.     ecran = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE);
  34.     photo = IMG_Load("gmemecookies.jpg" );
  35.     SDL_BlitSurface(photo, NULL, ecran, 0);
  36.     SDL_Quit();
  37.     ShowWindow(hwnd, nCmdShow);
  38.     while(GetMessage(&msg, hwnd, 0, 0))
  39.     {
  40.         TranslateMessage(&msg);
  41.         DispatchMessage(&msg);
  42.     }
  43.     msg.wParam;
  44. }
  45. LRESULT CALLBACK MainWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  46. {
  47.     switch(uMsg)
  48.     {
  49.         case WM_CREATE:
  50.             return 0;
  51.         case WM_DESTROY:
  52.             PostQuitMessage(0);
  53.             return 0;
  54.         default:
  55.             return DefWindowProc(hwnd, uMsg, wParam, lParam);
  56.     }
  57. }


 
Voila mon code j'ai tout fait comme bon me semblait, mais j'ai toujours une fenetre grise, moche, voir même pas belle ou pire: laide, mais surtout vide...

Reply

Marsh Posté le 31-07-2006 à 20:10:28    

pourquoi t'appelles SDL_Quit avant la fin de ton programme?

Reply

Marsh Posté le 31-07-2006 à 20:10:28   

Reply

Marsh Posté le 31-07-2006 à 20:56:51    

J'ai retrouvé un de mes vieux codes :
 

Code :
  1. BOOL CALLBACK  WndProc(
  2.   HWND hwnd,
  3.   UINT uMsg,
  4.   WPARAM wParam,
  5.   LPARAM lParam) {
  6.    SDL_Event event;
  7.    switch(uMsg) {
  8.  case WM_CLOSE:
  9.   {
  10.    bye = 1;
  11.    SDL_Quit();
  12.       SDL_FreeSurface(image);
  13.    return TRUE;
  14.   }
  15.  case WM_COMMAND:
  16.   {
  17.         if (bye) {
  18.    event.type = SDL_QUIT;
  19.    SDL_PushEvent(&event);
  20.     }
  21.   }
  22.  default:
  23.          return DefWindowProc(hwnd, uMsg, wParam, lParam);
  24.         break;
  25.    }
  26.    return TRUE;
  27. }
  28. void init_win32(HINSTANCE hInstance, int width, int height)
  29. {
  30.  char windowid[100];
  31.    WNDCLASSEX wcex;
  32.    RECT rect;
  33.    ZeroMemory(&wcex, sizeof(wcex));
  34.    wcex.cbSize = sizeof(wcex);
  35.    wcex.style = CS_BYTEALIGNCLIENT;
  36.    wcex.lpfnWndProc = WndProc;
  37.    wcex.hInstance = hInstance;
  38.    wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  39.    wcex.hIcon = NULL;
  40.    wcex.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
  41.    wcex.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
  42.    wcex.lpszClassName = "Mainframe";
  43.    if (!RegisterClassEx(&wcex)) {
  44.       MessageBox(NULL, "could not register class", "Error", MB_ICONERROR);
  45.       return 0;
  46.    }
  47.    ZeroMemory(&rect, sizeof(RECT));
  48.    rect.right = width;
  49.    rect.bottom = height;
  50.    AdjustWindowRectEx(&rect, WS_OVERLAPPEDWINDOW, TRUE, 0);
  51.    mainHwnd = CreateWindowEx(
  52.       0,
  53.       "Mainframe",
  54.       "SDL+Win32",
  55.       WS_OVERLAPPEDWINDOW,
  56.       CW_USEDEFAULT,
  57.       CW_USEDEFAULT,
  58.       abs(rect.left) + rect.right,
  59.       abs(rect.top) + rect.bottom,
  60.       NULL,
  61.       NULL,
  62.       hInstance,
  63.       NULL);
  64.    if (!mainHwnd) {
  65.       MessageBox(NULL, "could not create window", "Error", MB_ICONERROR);
  66.       return 0;
  67.    }
  68.    strcpy(windowid, "SDL_WINDOWID=" );
  69.    _ltoa((long)mainHwnd, windowid+13, 10);
  70.    _putenv(windowid);
  71. }
  72. int APIENTRY WinMain(HINSTANCE hInstance,
  73.                      HINSTANCE hPrevInstance,
  74.                      LPSTR     lpCmdLine,
  75.                      int       nCmdShow)
  76. {
  77.   MSG msg;
  78.   char windowid[100];
  79.   SDL_Surface *screen;
  80.   char *imagefile;
  81.   SDL_Palette *palette;
  82. /* Load SDL dynamicly */
  83.   LoadSDL();
  84.   init_win32(GetModuleHandle(NULL), WATERWID, WATERHGT);
  85.   ShowWindow(mainHwnd, SW_NORMAL);
  86.   UpdateWindow(mainHwnd);
  87.   if (SDL_Init(SDL_INIT_VIDEO)<0)
  88.   {
  89.     printf("Couldn't init SDL: %s\n", SDL_GetError());
  90.     exit(1);
  91.   }
  92. /* Set a video mode */
  93. screen = CreateScreen(WATERWID, WATERHGT, 8, (SDL_HWSURFACE|SDL_HWPALETTE));
  94. if ( screen == NULL ) {
  95.  exit(2);
  96. }
  97.   if (screen == NULL)
  98.   {
  99.     printf("Couldn't set video mode: %s\n", SDL_GetError());
  100.     exit(1);
  101.   }
  102.   /* start the random number generator */
  103.   randomize();
  104.   /* load a user-specified picture, or load the default picture */
  105.   imagefile = "BackGround.bmp";
  106.   image = SDL_LoadBMP(imagefile);
  107.   if (image == NULL)
  108.   {
  109.     printf("Couldn't load %s: %s\n", imagefile, SDL_GetError());
  110.     exit(1);
  111.   }
  112.   palette = image->format->palette;
  113.   if ((image->format->BitsPerPixel != 8) || (palette == NULL))
  114.   {
  115.     printf("Image must be 8-bit and have a palette\n" );
  116.     exit(1);
  117.   }
  118.   if ((image->w != WATERWID) || (image->h != WATERHGT))
  119.   {
  120.     printf("Image must be %dx%d in size\n", WATERWID, WATERHGT);
  121.     exit(1);
  122.   }
  123. /* ici ton code principale */
  124.    
  125. /* appli terminée */
  126.   SDL_Quit();
  127.   SDL_FreeSurface(image);
  128.   FreeSDL();
  129. return(0);
  130. }

Reply

Marsh Posté le 31-07-2006 à 22:30:23    

Oula c'est compliqué tout celà, il y'a des fonctions que je ne connais pas, celà a dû être fait avec une version plus ancienne de la SDL... Et certaines constantes aussi.
 
EDIT: Merci a tous j'ai fini par trouver ce qui allait pas, j'avais oublier le "UpdateWindow()"
 
Voici mon code pour les intéressés

Code :
  1. #include <windows.h>
  2. #include <SDL/SDL.h>
  3. #include <SDL/SDL_Image.h>
  4. LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
  5. int WINAPI WinMain (HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
  6.                     int nCmdShow)
  7. {
  8.     HWND hwnd;
  9.     WNDCLASS wc;
  10.     MSG msg;
  11.     wc.style = 0;
  12.     wc.lpfnWndProc = MainWndProc;
  13.     wc.cbClsExtra = 0;
  14.     wc.cbWndExtra = 0;
  15.     wc.hInstance = hinstance;
  16.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  17.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  18.     wc.hbrBackground = (HBRUSH) (1 + COLOR_BTNFACE);
  19.     wc.lpszMenuName = NULL;
  20.     wc.lpszClassName = "MaWinClass";
  21.     if(!RegisterClass(&wc)) return FALSE;
  22.     hwnd = CreateWindow("MaWinClass", "Essai SDL + API Win32", WS_OVERLAPPEDWINDOW,
  23.                         CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL,
  24.                         hinstance, NULL);
  25.     if(!hwnd) return FALSE;
  26.     char windowid[100];
  27.     hwnd;
  28.     strcpy(windowid, "SDL_WINDOWID=" );
  29.     _ltoa((long)hwnd, windowid+13, 10);
  30.     _putenv(windowid);
  31.     SDL_Init(SDL_INIT_VIDEO);
  32.     SDL_Surface *ecran = NULL, *photo = NULL;
  33.     ecran = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE);
  34.     photo = IMG_Load("memecookies.jpg" );
  35.     SDL_BlitSurface(photo, NULL, ecran, 0);
  36.     SDL_Flip(ecran);
  37.     ShowWindow(hwnd, nCmdShow);
  38.     UpdateWindow(hwnd);
  39.     SDL_Quit();
  40.     while(GetMessage(&msg, hwnd, 0, 0))
  41.     {
  42.         TranslateMessage(&msg);
  43.         DispatchMessage(&msg);
  44.     }
  45.     msg.wParam;
  46. }
  47. LRESULT CALLBACK MainWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  48. {
  49.     switch(uMsg)
  50.     {
  51.         case WM_CREATE:
  52.             return 0;
  53.         case WM_DESTROY:
  54.             PostQuitMessage(0);
  55.             return 0;
  56.         default:
  57.             return DefWindowProc(hwnd, uMsg, wParam, lParam);
  58.     }
  59. }


Message édité par Pcsnake le 31-07-2006 à 22:32:42
Reply

Marsh Posté le 01-08-2006 à 10:07:01    

Merci de mettre [résolu] dans le titre du topic :jap:

Reply

Marsh Posté le 02-08-2006 à 14:07:02    

Ok c'est fait

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed