IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

API graphiques Discussion :

[GDI]Création de bitmap


Sujet :

API graphiques

  1. #1
    Membre actif Avatar de babar63
    Homme Profil pro
    Développeur jeux vidéos/3d Temps réel
    Inscrit en
    Septembre 2005
    Messages
    241
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France

    Informations professionnelles :
    Activité : Développeur jeux vidéos/3d Temps réel

    Informations forums :
    Inscription : Septembre 2005
    Messages : 241
    Points : 207
    Points
    207
    Par défaut [GDI]Création de bitmap
    Bonjour à tous,
    je souhaite créer une bitmap voila en abrégé les structures de données que j'utilise :
    Couleur utilisé :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    template <typename T>
    class CColor3
    {
    public:
    	//.....
    	operator T * const ();     // return &r;
    	T r,g,b;
    }
    Buffer :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    template <class T>
    class CBuffer
    {
    public:
    	//.....
    	T * const GetArray();        //return &m_buffer[0];
    	std::vector<T> m_Buffer;
    };
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    // typedef :
    typedef CColor3<unsigned char> TPixel_uc;
    typedef CBuffer<TPixel>	TPixelBuffer;
    Cela étant fait, voila le code que j'utilise pour tenter tant bien que mal (pour pas dire très mal) de créer ma bitmap :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    case WM_PAINT:
    {
    	BITMAP bm;
    	PAINTSTRUCT ps;
    	HDC hdc = BeginPaint(hWnd, &ps);
     
    	// Bitmap handle :
    	HBITMAP HBitmap = CreateBitmap( PixelBuffer->SizeW(), PixelBuffer->SizeH(), 1, 8, (float *)(PixelBuffer->GetArray()) );
    	//HBITMAP HBitmap = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BITMAP1));   // La bitmap est bien affichée si je remplace la précédente ligne par celle là donc je suppose qu'il n'y a pas d'erreur par la suite
     
    	// Virtual context handle :
    	HDC hdcMem = CreateCompatibleDC(NULL);
     
    	// Load bitmap :
    	SelectObject(hdcMem, HBitmap);
    	GetObject(HBitmap, sizeof(bm), &bm);
     
    	// Transfer :
    	BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
     
    	DeleteDC(hdcMem);
     
    	EndPaint(hWnd, &ps);
     
    	break;
    }
    Mais la fonction CreateBitmap ne fonctionne pas , ça fait plusieurs heures maintenant que je cherche dans la doc et diverses articles mais je n'ai encore rien trouvé alors si vous voyez comment régler mon problème ( ou même un début de piste ) je suis preneur,
    merci d'avance et bonne fin de journée
    - hp pavillon dv7
    - intel(R) Core(TM)2 Duo CPU P8400 @ 2.26GHz 2.27GHz
    - nVidia GeForce 9600M GT
    - mémoire vive : 3.0Go

  2. #2
    Membre actif Avatar de babar63
    Homme Profil pro
    Développeur jeux vidéos/3d Temps réel
    Inscrit en
    Septembre 2005
    Messages
    241
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France

    Informations professionnelles :
    Activité : Développeur jeux vidéos/3d Temps réel

    Informations forums :
    Inscription : Septembre 2005
    Messages : 241
    Points : 207
    Points
    207
    Par défaut
    Oui en effet je m'y prenais vraiment mal j'ai finis par trouver une solution en fouillant sur la doc et quelques articles notamment : http://www.codeguru.com/cpp/g-m/bitmap/ Voilà la solution que j'ai trouvé
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    //Initialisation :
    BITMAP bm;
    PAINTSTRUCT ps;
    BITMAPINFOHEADER bmInfoHead;
    HDC hdc = BeginPaint(hWnd, &ps);
     
    // Bitmap creation :
    HBITMAP hBitmap = CreateCompatibleBitmap(hdc, PixelBuffer->SizeW(), PixelBuffer->SizeH());
    int result = GetObject(hBitmap, sizeof(bm), &bm);
    LPVOID lpvBits = new char[ bm.bmWidth * bm.bmHeight * bm.bmBitsPixel ];
    bmInfoHead.biSize = sizeof(BITMAPINFOHEADER);
    bmInfoHead.biWidth = bm.bmWidth;
    bmInfoHead.biHeight = bm.bmHeight;
    bmInfoHead.biPlanes = bm.bmPlanes;
    bmInfoHead.biBitCount = bm.bmBitsPixel;
    bmInfoHead.biCompression = BI_RGB;
    result = GetDIBits(hdc, hBitmap, 0, bm.bmHeight, lpvBits, (LPBITMAPINFO)&bmInfoHead, DIB_RGB_COLORS);
     
    // Bitmap modification :
    BYTE* dibits = static_cast<BYTE *>(lpvBits);
    for(unsigned int y=0; y<PixelBuffer->SizeH(); ++y)
    {
    	for(unsigned int x=0; x<PixelBuffer->SizeW(); ++x)
    	{
    		int yBuff = (PixelBuffer->SizeH()-1) - y;
    		TPixel_uc Pix = (*PixelBuffer)(x,yBuff);
    		*(dibits++) = Pix.b;
    		*(dibits++) = Pix.g;
    		*(dibits++) = Pix.r;
    		*(dibits++);
    	}
    }
    result = SetDIBits(hdc, hBitmap, 0, bm.bmHeight, lpvBits, (LPBITMAPINFO)&bmInfoHead, DIB_RGB_COLORS);
     
    // Bitmap drawing :
    HDC hdcMem = CreateCompatibleDC(NULL);
    SelectObject(hdcMem, hBitmap);
    BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
     
    // Release memory :
    DeleteDC(hdcMem);
    delete lpvBits;
    EndPaint(hWnd, &ps);
    Bon je suis assez déçu quand même du résultat, entre 1 et 2 secondes pour afficher ma bitmap (800*600) et uniquement ma bitmap ( ), je dois encore mal m'y prendre. Je clos le topic mais si vous avez de meilleurs solutions n'hésitait pas...
    - hp pavillon dv7
    - intel(R) Core(TM)2 Duo CPU P8400 @ 2.26GHz 2.27GHz
    - nVidia GeForce 9600M GT
    - mémoire vive : 3.0Go

  3. #3
    Expert éminent sénior
    Avatar de Mat.M
    Profil pro
    Développeur informatique
    Inscrit en
    Novembre 2006
    Messages
    8 361
    Détails du profil
    Informations personnelles :
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Novembre 2006
    Messages : 8 361
    Points : 20 381
    Points
    20 381
    Par défaut
    1- quel outil de développement ?
    2-SetDIbbits est assez rapide, la boucle serait à optimiser.
    Sinon au besoin prendre SDL ou DirectDraw...

  4. #4
    Membre actif Avatar de babar63
    Homme Profil pro
    Développeur jeux vidéos/3d Temps réel
    Inscrit en
    Septembre 2005
    Messages
    241
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France

    Informations professionnelles :
    Activité : Développeur jeux vidéos/3d Temps réel

    Informations forums :
    Inscription : Septembre 2005
    Messages : 241
    Points : 207
    Points
    207
    Par défaut
    En fait je suis en train de créer un moteur de rendu, je récupère toutes les données de ma scène dans un buffer. Maintenant le seul rôle de mon exécutable est de permettre l'affichage de ce buffer à travers une interface basique. J'ai donc pour l'instant simplement créé un projet WIN32 (c++, VS2005) vierge composé de ma fonction winmain ainsi que de la fonction pour les callbacks. Dans ces conditions, je ne suis pas intéressé par SDL, DirectDraw, OpenGL...

    Mais je pensais quand même pouvoir afficher une bitmap plus rapidement... pour améliorer le code j'ai tenter ça :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    //Global :
    HBITMAP				g_hBitmap;
    BITMAP				g_bm;
    BITMAPINFOHEADER		g_bmInfoHead;
    LPVOID				g_lpvImg;
    
    int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
    	//...
    	// Set the class with the properties of the window :
    	WNDCLASSEX wcex;
    	wcex.cbSize = sizeof(WNDCLASSEX);
    	//...
    
    	// Register the window :
    	RegisterClassEx(&wcex);
    
    	// Creating the window :
    	HWND MainWnd = CreateWindow("MyWindowClass", "Hello World", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,CW_USEDEFAULT, 808, 650, NULL, NULL, hInstance, NULL);
    
    	// Bitmap initialisation :
    	HDC hdc = GetDC(MainWnd);            // [ICI]
    	g_hBitmap = CreateCompatibleBitmap(hdc, g_PixelBuffer->SizeW(), g_PixelBuffer->SizeH());
    	int result = GetObject(g_hBitmap, sizeof(g_bm), &g_bm);
    	LPVOID g_lpvImg = new char[ g_bm.bmWidth * g_bm.bmHeight * g_bm.bmBitsPixel ];
    	g_bmInfoHead.biSize = sizeof(BITMAPINFOHEADER);
    	g_bmInfoHead.biWidth = g_bm.bmWidth;
    	g_bmInfoHead.biHeight = g_bm.bmHeight;
    	g_bmInfoHead.biPlanes = g_bm.bmPlanes;
    	g_bmInfoHead.biBitCount = g_bm.bmBitsPixel;
    	g_bmInfoHead.biCompression = BI_RGB;
    	result = GetDIBits(hdc, g_hBitmap, 0, g_bm.bmHeight, g_lpvImg, (LPBITMAPINFO)&g_bmInfoHead, DIB_RGB_COLORS);
    
    	// Show and update the window :
    	ShowWindow(MainWnd, nCmdShow);
    	UpdateWindow(MainWnd);
    
    	// Loop :
    	MSG msg;
    	ZeroMemory( &msg, sizeof(msg) );
    	while( msg.message != WM_QUIT )
    	{
    		if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
    		{
    			TranslateMessage(&msg);
    			DispatchMessage(&msg);
    		}
    	}
    
    	delete[] g_lpvImg;
    
    	//quit the program :
    	return (int)msg.wParam;
    }
    Et ma fonction callback :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	switch(message)
    	{
    		//...
    		case WM_PAINT:
    		{
    			PAINTSTRUCT ps;
    			HDC hdc = BeginPaint(hWnd, &ps);
    
    			// Bitmap modification :
    			BYTE* dibits = static_cast<BYTE *>(g_lpvImg);              // // [ICI] (g_lpvImg = 0x00000000)
    			for(unsigned int y=0; y<g_PixelBuffer->SizeH(); ++y)
    			{
    				for(unsigned int x=0; x<g_PixelBuffer->SizeW(); ++x)
    				{
    					TPixel_uc Pix = (*g_PixelBuffer)(x,y);
    					*(dibits++) = Pix.b;
    					*(dibits++) = Pix.g;
    					*(dibits++) = Pix.r;
    					*(dibits++);		//alpha value
    				}
    			}
    			int result = SetDIBits(hdc, g_hBitmap, 0, g_bm.bmHeight, g_lpvImg, (LPBITMAPINFO)&g_bmInfoHead, DIB_RGB_COLORS);
    
    			// Bitmap drawing :
    			HDC hdcMem = CreateCompatibleDC(NULL);
    			SelectObject(hdcMem, g_hBitmap);
    			BitBlt(hdc, 0, 0, g_bm.bmWidth, g_bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
    
    			// Release memory :
    			DeleteDC(hdcMem);
    
    			EndPaint(hWnd, &ps);
    			break;
    		}
    		//...	
    	}
    	return 0;
    }
    Je pense savoir d'où vient le problème (ligne commenté // [ICI]), mais pas de solution pour l'instant.
    Sinon pour ma boucle, comment puis-je l'optimiser?
    Merci pour ton aide
    - hp pavillon dv7
    - intel(R) Core(TM)2 Duo CPU P8400 @ 2.26GHz 2.27GHz
    - nVidia GeForce 9600M GT
    - mémoire vive : 3.0Go

  5. #5
    Expert éminent sénior
    Avatar de Mat.M
    Profil pro
    Développeur informatique
    Inscrit en
    Novembre 2006
    Messages
    8 361
    Détails du profil
    Informations personnelles :
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Novembre 2006
    Messages : 8 361
    Points : 20 381
    Points
    20 381
    Par défaut
    A tout hasard consulter ceci c'est pour VC6 mais cela peut demeurer encore utile.

    http://msdn2.microsoft.com/en-us/library/aa260969.aspx
    Sinon je pense que la solution ce serait de prendre un back et front buffer dessiner dans le back-buffer et copier le back buffer vers le front buffer c'est le grand classique du dessin de bitmap

  6. #6
    Membre actif Avatar de babar63
    Homme Profil pro
    Développeur jeux vidéos/3d Temps réel
    Inscrit en
    Septembre 2005
    Messages
    241
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France

    Informations professionnelles :
    Activité : Développeur jeux vidéos/3d Temps réel

    Informations forums :
    Inscription : Septembre 2005
    Messages : 241
    Points : 207
    Points
    207
    Par défaut
    Je ne penses pas m'embarrasser avec l'optimisation pour l'instant (manque de temps ), et puis ce n'est qu'une simple interface de test donc pour l'instant ça ira comme ça
    Mais merci beaucoup pour le lien ça à l'air intéressant ca vaut le coup d'y jeter un oeil je pense

    Pour la piste des backs et fronts buffers je vais faire quelques recherches pour voire si ce n'est pas trop compliqué à mettre en place... encore merci pour ton aide
    - hp pavillon dv7
    - intel(R) Core(TM)2 Duo CPU P8400 @ 2.26GHz 2.27GHz
    - nVidia GeForce 9600M GT
    - mémoire vive : 3.0Go

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Création de Bitmap
    Par malus56 dans le forum Débuter
    Réponses: 0
    Dernier message: 25/07/2011, 10h18
  2. Création de Bitmap et problème de mémoire!
    Par NejNej dans le forum Windows Forms
    Réponses: 7
    Dernier message: 24/09/2008, 17h00
  3. Création de bitmap à partir d'un tableau de byte
    Par Phenix26 dans le forum Windows
    Réponses: 2
    Dernier message: 06/12/2007, 20h30
  4. création de bitmap 8 bits avec GDI
    Par tamar_a_roulettes dans le forum Windows
    Réponses: 4
    Dernier message: 30/11/2006, 23h43
  5. [GDI] Générer un bitmap dynamiquement
    Par Copros dans le forum Windows
    Réponses: 2
    Dernier message: 12/06/2006, 11h10

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo