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

Windows Discussion :

[API Win32]Problème déplacement horizontal de sprites


Sujet :

Windows

  1. #1
    Membre extrêmement actif Avatar de nikau6
    Homme Profil pro
    Inscrit en
    Février 2008
    Messages
    406
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Etats-Unis

    Informations forums :
    Inscription : Février 2008
    Messages : 406
    Points : 737
    Points
    737
    Par défaut [API Win32]Problème déplacement horizontal de sprites
    Bonjours,

    Je programme actuellement un moteur de jeux 2D avec l'API win32, et simplement avec l'API win32.

    Lors du déplacement de sprites verticalement, tous va bien, mais lorsque je les déplaces horizontalement, c'est pas terrible.C'est légèrement saccadé.Par moments, c'est comme ci certaines partis du sprite étaient décalée par rapport à d'autres.
    Comment y remédier?

    Merci

  2. #2
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 369
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 369
    Points : 41 519
    Points
    41 519
    Par défaut
    Passes-tu par un "double" buffer?
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  3. #3
    Membre extrêmement actif Avatar de nikau6
    Homme Profil pro
    Inscrit en
    Février 2008
    Messages
    406
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Etats-Unis

    Informations forums :
    Inscription : Février 2008
    Messages : 406
    Points : 737
    Points
    737
    Par défaut
    Oui, j'utilise une sorte de double buffer.Je creer un HDC mémoire, un bitmap vierge, je copie la scéne dans le bitmap vierge, et je recopie le tous dans le DC de ma fenêtre avec la fonction BitBlt();

    Mon problème n'est pas un problème de scintillement que l'on peux avoir lorsque l'on n'utilise pas de double buffer.

    Il y a comme un décalage entre certaines partis du sprite, mais seulement en déplacement horizontale

  4. #4
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 369
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 369
    Points : 41 519
    Points
    41 519
    Par défaut
    Oui, ça ressemble à un problème dû à une lenteur de la copie d'image.
    Ton bitmap intermédiaire, l'as-tu créé avec CreateDIBSection() ou CreateCompatibleBitmap()? Je pense que ça sera plus rapide à copier avec le second...
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  5. #5
    Membre extrêmement actif Avatar de nikau6
    Homme Profil pro
    Inscrit en
    Février 2008
    Messages
    406
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Etats-Unis

    Informations forums :
    Inscription : Février 2008
    Messages : 406
    Points : 737
    Points
    737
    Par défaut
    J'utilise CreateCompatibleBitmap()

    Voila à quoi ca ressemble:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     mHdc = GetDC(mHwnd);
     mBufferDC = CreateCompatibleDC(mHdc); 
     mHbitmapBuffer = CreateCompatibleBitmap(mHdc, mWidth, mHeight);
    ...
    BitBlt(mHdc, 0, 0, mWidth, mHeight, mBufferDC, 0, 0 ,SRCCOPY);

  6. #6
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 369
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 369
    Points : 41 519
    Points
    41 519
    Par défaut
    Dans ce cas, je ne vois pas trop comment corriger le problème.
    Par contre, au lieu du GetDC(), ne devrais-tu pas avoir un BeginPaint() ?
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  7. #7
    Membre extrêmement actif Avatar de nikau6
    Homme Profil pro
    Inscrit en
    Février 2008
    Messages
    406
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Etats-Unis

    Informations forums :
    Inscription : Février 2008
    Messages : 406
    Points : 737
    Points
    737
    Par défaut
    Je n'utilise pas WM_PAINT, et je crois que BeginPaint() ne fonctionne que pour cet événement, non?J'utilise une fonctoin que je place dans la boucle d'événements.
    Je vais voir ou je pourrais optimiser mon code

  8. #8
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 369
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 369
    Points : 41 519
    Points
    41 519
    Par défaut
    Pour moi, tu devrais utiliser WM_PAINT.
    J'ai déjà fait un moteur de test qui "utilise autant de proc que possible pour avoir la fluidité maximale".
    Ça donnait un truc de ce genre:
    Code C : 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
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    #include "stdafx.h"
    #define INLINE_SSaveGdiObj
    #include "TestTime.h"
    #include "GetWindowLongPtr.h"
    #include "Sprite.h"
    #include "resource.h"
     
    #pragma warning(disable:4204)
    #define WMSG_UPDATENOW (WM_USER+100)
     
    struct st_testTimeWndData
    {
    	//TIMER action
    	UINT_PTR timerId;
    	UINT timerDelay;	
    	//Performance counter
    	double periodPerf; //Period, in seconds (inverse of the frequency)
    	LARGE_INTEGER lastPerf;
    	//Double-buffer
    	HBITMAP hDbBmp;
    	struct SSaveGdiObj * pDbDC;
     
    	//Animated object
    	double speed;
    	double x;
    	struct st_sprite sprite;
    	BOOL bSpriteOK;
    	//Mouse
    	int mouseX, mouseY;
    	HPEN hMousePen;
    	//Mouse chaser
    	double mouseChaserX, mouseChaserY;
    	HPEN hMouseChaserPen;
    	double mouseChaserSpeed;
    };
     
    /* Update action. For the ChangeUpdateAction function, 
       the higher the value, the more "active" it's considered. */
    enum e_updateAction
    {
    	LOWEST,
    	TIMER = LOWEST,
    	REPOST,
    	PAINT,
    	ERASE_PAINT
    };
     
    static LRESULT CALLBACK TestTimeWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
    static LONGLONG GetNewCounter(LARGE_INTEGER * pt_counter);
    static void ChangeUpdateAction(enum e_updateAction * pt_eAction, enum e_updateAction eNew);
    static void MoveX(struct st_testTimeWndData *pt_data, HWND hWnd, double fTimeDiff, enum e_updateAction * pt_eAction);
    static void MoveMouseChaser(struct st_testTimeWndData *pt_data, HWND hWnd, double fTimeDiff, enum e_updateAction * pt_eAction);
     
     
    BOOL CreateWndClass(HINSTANCE hInst, LPCTSTR * pt_sczClassName)
    {
    	static const LPCTSTR sczClassName = _T("TestTimeWndClass");
    	BOOL bRet = TRUE;
     
    	if(pt_sczClassName)
    		*pt_sczClassName = sczClassName;
     
    	if(hInst)
    	{
    		WNDCLASSEX const wcx = {
    		 sizeof wcx, 0|CS_VREDRAW|CS_HREDRAW/**/, TestTimeWndProc,
                     0, sizeof(struct st_testTimeWndData *), hInst,
                     NULL, LoadCursor(NULL, IDC_ARROW), (HBRUSH)(COLOR_WINDOW+1),
                     NULL, sczClassName,
                     NULL
                     };
                    bRet = (RegisterClassEx(&wcx) != 0);
            }
            return bRet;
    }
     
     
    static LRESULT CALLBACK TestTimeWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
            LRESULT ret = 0;
            struct st_testTimeWndData * pt_data = (struct st_testTimeWndData *)GetWindowLongPtr(hWnd, 0);
            
            switch(uMsg)
            {
            case WM_CREATE:
                    {
                            LARGE_INTEGER liFreq = {0};
     
                            //Performance counter frequency
                            if(ret >= 0)
                            {
                                    if(!QueryPerformanceFrequency(&liFreq))
                                            ret = -1;
                            }
     
                            //Data
                            if(ret >= 0)
                            {
                                    pt_data = malloc(sizeof *pt_data);
                                    SetWindowLongPtr(hWnd, 0, (LONG_PTR)pt_data);
                                    
                                    if(pt_data==NULL)
                                            ret = -1;
                            }
                            //Double buffer
                            if(ret >= 0)
                            {
                                    HDC hdc = GetDC(NULL);
                                    HDC hCompatDC = CreateCompatibleDC(hdc);
                                    RECT r;
                                    GetClientRect(hWnd, &r);
                                    if(hCompatDC)
                                    {
                                            HBITMAP hBitmap = CreateCompatibleBitmap(hdc, r.right, r.bottom);
                                            if(hBitmap != NULL)
                                            {
                                                    pt_data->pDbDC = SSaveGdiObj_NewFromObj(hCompatDC, hBitmap, TRUE);
                                                    if(pt_data->pDbDC != NULL)
                                                    {
                                                            //OK.
                                                            pt_data->hDbBmp = hBitmap;
                                                    }
                                                    else
                                                    {
                                                            DeleteObject(hBitmap), hBitmap=NULL;
                                                            ret = -1;
                                                    }
                                            }
                                            else
                                            {
                                                    DeleteDC(hCompatDC), hCompatDC=NULL;
                                                    ret = -1;
                                            }
                                    }
                                    else
                                            ret = -1;
                                    ReleaseDC(NULL, hdc), hdc=NULL;
                            }
                            //Moving things
                            if(ret >= 0)
                            {
                                    HINSTANCE hInst = ((LPCREATESTRUCT)lParam)->hInstance;
                                    double freq = (double)liFreq.QuadPart;
                                    assert(freq > 0.0);
                                    assert(pt_data != NULL);
                                    pt_data->periodPerf = 1.0/freq;
                                    assert(pt_data->periodPerf > 0.0);
     
                                    pt_data->x = 0;
                                    pt_data->speed = 96;
                                    {
                                            HBITMAP hBmp = LoadBitmap(hInst,MAKEINTRESOURCE(IDB_SPRITE32));
                                            //HBITMAP hMask = LoadBitmap(hInst,MAKEINTRESOURCE(IDB_MASK32_1));
                                            pt_data->sprite.cb = sizeof(pt_data->sprite);
                                            //pt_data->bSpriteOK = InitializeSpriteFromMask(&pt_data->sprite, TRUE, hBmp, hMask);
                                            pt_data->bSpriteOK = InitializeSpriteFromColor(&pt_data->sprite, TRUE, hBmp, RGB(0, 255, 0));
                                            if(pt_data->bSpriteOK)
                                            {
                                                    //SetSpriteFormat(&pt_data->sprite, SF_BITMAPONWHITE|SF_MASKONBLACK);
                                            }
                                            else
                                            {
                                                    DeleteObject(hBmp);
                                                    //DeleteObject(hMask);
                                            }
                                    }
                                    
                                    pt_data->mouseX = -1;
                                    pt_data->mouseY = -1;
                                    pt_data->hMousePen = CreatePen(PS_SOLID, 0, RGB(192, 192, 192));
     
                                    pt_data->mouseChaserX = -1;
                                    pt_data->mouseChaserY = -1;
                                    pt_data->hMouseChaserPen = CreatePen(PS_SOLID, 0, RGB(0, 0, 255));
                                    pt_data->mouseChaserSpeed = 144;
     
                                    QueryPerformanceCounter(&pt_data->lastPerf);
                            }
                    }
                    
                    if(ret >= 0)
                            PostMessage(hWnd, WMSG_UPDATENOW, 0, 0);
                    break;
     
            case WM_DESTROY:
                    if(pt_data != NULL)
                    {
                            DeleteObject(pt_data->hMousePen), pt_data->hMousePen=NULL;
                            DeleteObject(pt_data->hMouseChaserPen), pt_data->hMouseChaserPen=NULL;
                            if(pt_data->bSpriteOK)
                                    ClearSprite(&pt_data->sprite), pt_data->bSpriteOK=FALSE;
                            if(pt_data->pDbDC != NULL)
                                    SSaveGdiObj_Delete(pt_data->pDbDC), pt_data->pDbDC=NULL, pt_data->hDbBmp=NULL;
                    }
                    free(pt_data), pt_data = NULL;
                    SetWindowLongPtr(hWnd, 0, (LONG_PTR)pt_data);
                    {
                            MSG msg;
                            while(PeekMessage(&msg, hWnd, WMSG_UPDATENOW, WMSG_UPDATENOW, PM_REMOVE)) {}
                    }
                    //Exit the message loop when the window is closed.
                    PostQuitMessage(0);
                    break;
            
            case WM_TIMER:
                    if(pt_data!=NULL && pt_data->timerId != 0 && pt_data->timerId != wParam)
                    {
                            KillTimer(hWnd, pt_data->timerId);
                            PostMessage(hWnd, WMSG_UPDATENOW, 0, 0);
                    }
                    break;
                    
            case WMSG_UPDATENOW:
                    //Allow only one WMSG_UPDATENOW per window
                    {
                            MSG msg;
                            while(PeekMessage(&msg, hWnd, WMSG_UPDATENOW, WMSG_UPDATENOW, PM_REMOVE)) {}
                    }
                    
                    if(pt_data!=NULL)
                    {
                            enum e_updateAction eAction = REPOST; //Always repost at least (100% CPU)
     
                            LONGLONG const diff = GetNewCounter(&pt_data->lastPerf);
                            if(diff != 0)
                            {
                                    double const fDiff = (double)diff;
                                    double const fTimeDiff = fDiff * pt_data->periodPerf;
                                    assert(fDiff > 0.0);
                                    assert(fTimeDiff > 0.0);
                            
                                    //Move X.
                                    MoveX(pt_data, hWnd, fTimeDiff, &eAction);
                                    //Move mouse chaser.
                                    MoveMouseChaser(pt_data, hWnd, fTimeDiff, &eAction);
                            }//If diff
                            
                            //Repost the message if not painting.
                            switch(eAction)
                            {
                            case TIMER:
                                    if(pt_data->timerId != 0)
                                    {
                                            SetTimer(hWnd, pt_data->timerId, pt_data->timerDelay, NULL);
                                    }
                                    break;
                            case REPOST:
                                    PostMessage(hWnd, WMSG_UPDATENOW, 0, 0);
                                    break;
                            case PAINT:
                                    InvalidateRect(hWnd, NULL, FALSE);
                                    break;
                            case ERASE_PAINT:
                                    InvalidateRect(hWnd, NULL, TRUE);
                                    break;
                            }
                    }//if pt_data!=NULL
                    break;
            
            case WM_LBUTTONDOWN:
                    SetCapture(hWnd);
                    //no break
            case WM_MOUSEMOVE:
                    if(pt_data != NULL)
                    {
                            pt_data->mouseX = GET_X_LPARAM(lParam);
                            pt_data->mouseY = GET_Y_LPARAM(lParam);
                            InvalidateRect(hWnd, NULL, TRUE);
                    }
                    break;
            case WM_LBUTTONUP:
                    ReleaseCapture();
                    break;
            
            case WM_ERASEBKGND:
                    //TODO: I think I do not handle this message correctly.
                    //I should:
                    //*     Repaint the bitmap, independently from size 
                    //      (if the bitmap is too small, maybe erase the rest)
                    //* Set a flag asking to clear the bitmap even if not just re-created.
                    if(pt_data!=NULL && SSaveGdiObj_GetSafeHdc(pt_data->pDbDC)!=NULL)
                    {
                            ULONG_PTR upHBrush = GetClassLongPtr(hWnd, GCL_HBRBACKGROUND);
                            RECT r;
                            GetClientRect(hWnd, &r);
                            FillRect(pt_data->pDbDC->m_hdc, &r, (HBRUSH)upHBrush);
                    }
                    else //Removing the ELSE Cleans the bitmap IN ADDITION to erasing. But the flicker comes back.
                            ret = DefWindowProc(hWnd, uMsg, wParam, lParam);
                    break;
            
            case WM_PAINT:
                    {
                            PAINTSTRUCT ps;
                            HDC hdcWin = BeginPaint(hWnd, &ps);
                            HDC hdc = hdcWin;
                            
     
                            if(pt_data != NULL)
                            {
                                    RECT r;
                                    GetClientRect(hWnd, &r);
                                    
                                    //Use the double buffer if possible
                                    if(SSaveGdiObj_GetSafeHdc(pt_data->pDbDC)!=NULL)
                                    {
                                            BITMAP bmp = {0};
                                            GetObject(pt_data->hDbBmp, sizeof(bmp), &bmp);
                                            if(bmp.bmWidth!=r.right || bmp.bmHeight!=r.bottom)
                                            {
                                                    pt_data->hDbBmp = CreateCompatibleBitmap(hdcWin, r.right, r.bottom);
                                                    SSaveGdiObj_Select(pt_data->pDbDC, pt_data->hDbBmp, TRUE);
                                                    //Always erase the bitmap contents when re-creating : seems that it is created black.
                                                    {
                                                            ULONG_PTR upHBrush = GetClassLongPtr(hWnd, GCL_HBRBACKGROUND);
                                                            FillRect(pt_data->pDbDC->m_hdc, &r, (HBRUSH)upHBrush);
                                                    }
                                            }
                                            hdc = pt_data->pDbDC->m_hdc;                                    
                                    }
                                    
                                    //Mouse
                                    assert(r.left==0 && r.top==0);
                                    if((pt_data->mouseX >= 0 && pt_data->mouseX < r.right)
                                     || (pt_data->mouseY >= 0 && pt_data->mouseY < r.bottom))
                                    {
                                            HPEN hPen = ((pt_data->hMousePen != NULL) ? pt_data->hMousePen : GetStockObject(BLACK_PEN));
                                            HGDIOBJ hOldPen = SelectObject(hdc, hPen);
                                            
                                            if(pt_data->mouseX >= 0 && pt_data->mouseX < r.right)
                                            {
                                                    MoveToEx(hdc, pt_data->mouseX, 0, NULL);
                                                    LineTo(hdc, pt_data->mouseX, r.bottom);
                                            }
                                            if(pt_data->mouseY >= 0 && pt_data->mouseY < r.bottom)
                                            {
                                                    MoveToEx(hdc, 0, pt_data->mouseY, NULL);
                                                    LineTo(hdc, r.right, pt_data->mouseY);
                                            }
                                            
                                            SelectObject(hdc, hOldPen);
                                    }
     
                                    //Mouse chaser.
                                    {
                                            HPEN hPen = ((pt_data->hMouseChaserPen != NULL) ? pt_data->hMouseChaserPen : GetStockObject(BLACK_PEN));
                                            HGDIOBJ hOldPen = SelectObject(hdc, hPen);
                                            
                                            MoveToEx(hdc, (int)pt_data->mouseChaserX-10, (int)pt_data->mouseChaserY-10, NULL);
                                            LineTo(hdc, (int)pt_data->mouseChaserX+11, (int)pt_data->mouseChaserY+11);
                                            MoveToEx(hdc, (int)pt_data->mouseChaserX+10, (int)pt_data->mouseChaserY-10, NULL);
                                            LineTo(hdc, (int)pt_data->mouseChaserX-11, (int)pt_data->mouseChaserY+11);
                                            
                                            SelectObject(hdc, hOldPen);
                                    }
     
                                    //Moving object
                                    {
                                            const int SPRITE_WIDTH = 32;
                                            const int SPRITE_HEIGHT = 32;
                                            if(pt_data->bSpriteOK)
                                                    DrawSprite(&pt_data->sprite, hdc, (int)pt_data->x-SPRITE_WIDTH/2, r.bottom/2-SPRITE_HEIGHT/2);
                                            else
                                                    SetPixelV(hdc, (int)pt_data->x, r.bottom/2, RGB(255, 0, 0));
                                            
                                    }
                                    if(pt_data->pDbDC!=NULL && pt_data->pDbDC->m_hdc!=NULL)
                                            BitBlt(hdcWin, 0, 0, r.right, r.bottom, pt_data->pDbDC->m_hdc, 0, 0, SRCCOPY);
                            }
                            
                            EndPaint(hWnd, &ps);
                            PostMessage(hWnd, WMSG_UPDATENOW, 0, 0);
                    }
                    break;
                    
            default:
                    ret = DefWindowProc(hWnd, uMsg, wParam, lParam);
                    break;
            }
            return ret;
    }
     
     
    /* Query the performance counter, update the value and return the difference with the old one.
       ------------------------------------------------------------------------------------------- */
    static LONGLONG GetNewCounter(LARGE_INTEGER *pt_counter)
    {
    	LARGE_INTEGER newPerf;
     
    	assert(pt_counter != NULL);
    	QueryPerformanceCounter(&newPerf);
    	if(newPerf.QuadPart != pt_counter->QuadPart)
    	{
    		LONGLONG lastComp = pt_counter->QuadPart;
    		LONGLONG liComp = newPerf.QuadPart;
    		if(liComp < lastComp)
    		{
    			//Counter wrapped. Offset both values.
    			static const ULONGLONG inc = 0x8000000000000000ULL;
    			//---- ----- --------- --- =   FEDCBA9876543210
    			liComp += inc;
    			lastComp += inc;
    		}
    		pt_counter->QuadPart = newPerf.QuadPart;
     
    		//Local variables
    		{
    			LONGLONG const diff = liComp - lastComp;
    			assert(diff > 0); //because of enclosing if
    			return diff;
    		}//Local variables
    	}
    	else
    		return 0;
    }
     
     
    /* Change the update action for something more "active"
       ---------------------------------------------------- */
    static void ChangeUpdateAction(enum e_updateAction * pt_eAction, enum e_updateAction eNew)
    {
    	if(*pt_eAction < eNew)
    		*pt_eAction = eNew;
    }
     
     
    /* Modify X according to the time difference
       ----------------------------------------- */
    static void MoveX(struct st_testTimeWndData *pt_data, HWND hWnd, double fTimeDiff, enum e_updateAction * pt_eAction)
    {
    	double const oldX = floor(pt_data->x);
    	double newX = pt_data->x;
     
    	//Local variables : Increment X.
    	{
    		double const incX = (fTimeDiff * pt_data->speed);
    		assert(fTimeDiff > 0.0);
    		if(pt_data->speed > 0)
    			assert(incX > 0);
    		else
    			assert(incX < 0);
    		newX += incX;
    	}
    	//Ensure X doesn't leave the window.
    	{
    		RECT r;
    		GetClientRect(hWnd, &r);
    		while(r.right != 0 && (newX < 0 || newX > r.right))
    		{
    			if(newX > r.right)
    			{
    				newX = r.right - (newX - r.right);
    				pt_data->speed = -pt_data->speed; 
    				//Erase the window
    				ChangeUpdateAction(pt_eAction, ERASE_PAINT);
    			}
    			if(newX < 0)
    			{
    				newX = -newX;
    				pt_data->speed = -pt_data->speed;
    				//Erase the window
    				ChangeUpdateAction(pt_eAction, ERASE_PAINT);
    			}
    		}
    	}
    	pt_data->x = newX;
     
    	if(floor(newX) != oldX)
    	{
    		//Redraw or erase the window.
    		if(SSaveGdiObj_GetSafeHdc(pt_data->pDbDC)!=NULL)
    			ChangeUpdateAction(pt_eAction, ERASE_PAINT);
    		else
    			ChangeUpdateAction(pt_eAction, PAINT);
    	}
    }
     
    /* Modify mouse chaser coordinates according to the time difference
       ---------------------------------------------------------------- */
    static void MoveMouseChaser(struct st_testTimeWndData *pt_data, HWND hWnd, double fTimeDiff, enum e_updateAction * pt_eAction)
    {
    	double const oldX = floor(pt_data->mouseChaserX);
    	double newX = pt_data->mouseChaserX;
    	double const oldY = floor(pt_data->mouseChaserY);
    	double newY = pt_data->mouseChaserY;
    	(void)hWnd;
     
    	if(pt_data->mouseChaserX == pt_data->mouseX && pt_data->mouseChaserY == pt_data->mouseY)
    		return;
     
    	//Local variables : Calculate Increments.
    	{
    		double incX = 0.0, incY = 0.0;
    		double const inc = (fTimeDiff * pt_data->mouseChaserSpeed);
    		assert(fTimeDiff > 0.0);
    		assert(pt_data->mouseChaserSpeed > 0.0);
    		assert(inc > 0);		
     
    		{
    			double const diffX = pt_data->mouseX - pt_data->mouseChaserX;
    			double const diffY = pt_data->mouseY - pt_data->mouseChaserY;
    			double const angle = atan2(diffY, diffX);
    			if(diffX != 0.0)
    				incX = inc*cos(angle);
    			if(diffY != 0.0)
    				incY = inc*sin(angle);
    		}
    		newX += incX;
    		newY += incY;
     
    		//Ensure the chaser doesn't pass the mouse
    		{
    			if(newX > pt_data->mouseX && incX > 0.0)
    				newX = pt_data->mouseX;
    			if(newX < pt_data->mouseX && incX < 0.0)
    				newX = pt_data->mouseX;	
    			if(newY > pt_data->mouseY && incY > 0.0)
    				newY = pt_data->mouseY;
    			if(newY < pt_data->mouseY && incY < 0.0)
    				newY = pt_data->mouseY;	
    		}
    	}
    	pt_data->mouseChaserX = newX;
    	pt_data->mouseChaserY = newY;
     
    	if(floor(newX) != oldX || floor(newY) != oldY)
    	{
    		//Redraw the window.
    		ChangeUpdateAction(pt_eAction, ERASE_PAINT);
    	}
    }
    Ici, c'est surtout la WndProc qui est intéressante: Le programme alterne principalement entre les messages WMSG_UPDATENOW (posté par le traitement de WM_PAINT) et WM_PAINT (posté par le InvalidateRect() dans le traitement de WMSG_UPDATENOW).
    Le déplacement des différents objets est déduit du temps écoulé entre deux rafraichissements, donc on fait autant de rafraichissements que possible par seconde.

    EDIT: Ne prête pas attention au Timer, il ne sert à rien ici; C'était juste une expérience.
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  9. #9
    Membre extrêmement actif Avatar de nikau6
    Homme Profil pro
    Inscrit en
    Février 2008
    Messages
    406
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Etats-Unis

    Informations forums :
    Inscription : Février 2008
    Messages : 406
    Points : 737
    Points
    737
    Par défaut
    Merci, c'est très intéressant.
    Si j'ai bien compris, tu place la boucle de traitement(PeekMessage) dans l'événement WMSG_UPDATENOW, tu appel WM_PAINT avec InvalidateRect() qui rappel WMSG_UPDATENOW avec PostMessage();

    Vraiment très intéressant, merci encors.

    Et pour la maitrise du temps(fps), Je n'ai pas trop bien saisis comment tu fais?

  10. #10
    Membre extrêmement actif Avatar de nikau6
    Homme Profil pro
    Inscrit en
    Février 2008
    Messages
    406
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Etats-Unis

    Informations forums :
    Inscription : Février 2008
    Messages : 406
    Points : 737
    Points
    737
    Par défaut
    Excuse moi, jen'avais pas vu la dernière ligne ou tu dis que le TIMER ne saire à rien.
    Je suppose que je dois simplement placer un compteur dans PeekMessage.

  11. #11
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 369
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 369
    Points : 41 519
    Points
    41 519
    Par défaut
    Non, je ne place aucune boucle de traitement dans WMSG_UPDATENOW!
    Comme l'indique le commentaire, la boucle avec PeekMessage() sert juste à s'assurer qu'il n'y a pas d'autre message WMSG_UPDATENOW dans la file d'attente.

    Par contre, cette boucle n'est pas sécurisée, car elle ne vérifie pas qu'elle n'a pas chopé un WM_QUIT par mégarde. Normalement, il faudrait détecter cela, re-poster le WM_QUIT et sortir de la boucle. Je corrigerai un jour si j'ai le temps.
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  12. #12
    Membre extrêmement actif Avatar de nikau6
    Homme Profil pro
    Inscrit en
    Février 2008
    Messages
    406
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Etats-Unis

    Informations forums :
    Inscription : Février 2008
    Messages : 406
    Points : 737
    Points
    737
    Par défaut
    Ouais, ce que j'ai dis étais vraiment stupide, pas besoin de boucle de traitement, la boucle s'éffectue entre WMSG_UPDATENOW, et WM_PAINT.

    Merci, je vais essayer

  13. #13
    Membre extrêmement actif Avatar de nikau6
    Homme Profil pro
    Inscrit en
    Février 2008
    Messages
    406
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Etats-Unis

    Informations forums :
    Inscription : Février 2008
    Messages : 406
    Points : 737
    Points
    737
    Par défaut
    J'ai verifié mon "frame core" et il tourne à 6500fps si je n'affiche qu'un seul sprite que je déplace, et pourtant celui ci à toujours les mêmes problèmes en déplacements horizontaux.Quelqu'un saurait-il ce que cela pourrait être.Un problème de synchronisation avec le rafraîchissement de l'écran peut-être?
    Merci

  14. #14
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 369
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 369
    Points : 41 519
    Points
    41 519
    Par défaut
    Si tu as autant d'images par secondes, je dirais oui: Tu changes d'image au cours d'un même rafraichissement.

    Pas contre, je n'ai aucune idée de comment faire de la synchronisation en GDI...
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  15. #15
    Membre extrêmement actif Avatar de nikau6
    Homme Profil pro
    Inscrit en
    Février 2008
    Messages
    406
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Etats-Unis

    Informations forums :
    Inscription : Février 2008
    Messages : 406
    Points : 737
    Points
    737
    Par défaut
    Maintenant, je suis sur que c'est un problème de rafraîchissement.Lorsque le frame rate est de 60fps(ce qui est la vitesse de rafraîchissement de l'écran), je n'ai plus de problème, mais le frame rate ne reste jamais complètement stable et passe à 61fps, 59 fps, régulièrement et alors le problème revient.
    Quelqu'un connaîtrait-il la solution au problème?Je ne sais vraiment pas quoi faire.

  16. #16
    Membre actif Avatar de Twindruff
    Inscrit en
    Janvier 2005
    Messages
    216
    Détails du profil
    Informations forums :
    Inscription : Janvier 2005
    Messages : 216
    Points : 237
    Points
    237
    Par défaut
    Tant qu'on ne fait pas de plein-écran il me semble qu'on ne peut pas faire de synchro verticale (ça s'appelle vsync aussi, ça peut aider pour googler).
    Ya qu'à voir, en DirectX en fenêtré je crois bien qu'on ne peut pas faire de vsync.

  17. #17
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 369
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 369
    Points : 41 519
    Points
    41 519
    Par défaut
    C'est bête.
    Ils devraient au moins donner un "poor man's substitute" pour éviter d'avoir plus de FPS que de rafraichissements, du genre "si le derner WM_PAINT n'est pas encore affiché, attendre avant d'en envoyer un autre"...
    Même si ça diminue légèrement la fluidité des calculs derrière.

    PS: Comment fais-tu pour avoir 6500 FPS ? Mon code dont j'ai posté un extrait tourne sur une petite fenêtre simple, et il a "seulement" 170 FPS...
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  18. #18
    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 379
    Points
    20 379
    Par défaut
    Citation Envoyé par Twindruff Voir le message
    Tant qu'on ne fait pas de plein-écran il me semble qu'on ne peut pas faire de synchro verticale (ça s'appelle vsync aussi, ça peut aider pour googler).
    Ya qu'à voir, en DirectX en fenêtré je crois bien qu'on ne peut pas faire de vsync.
    Vraisemblablement oui quand on crée un IDirect3D9Ex avec IDirect3D9Ex::CreateDeviceEx() ( ou la même interface avec DX10).
    il faut mettre le D3DPRESENT_PARAMETERS à 0 pour FullScreen_RefreshRateInHz
    FullScreen_RefreshRateInHz
    The rate at which the display adapter refreshes the screen. The value depends on the mode in which the application is running:
    For windowed mode, the refresh rate must be 0.
    For full-screen mode, the refresh rate is one of the refresh rates returned by IDirect3D9::EnumAdapterModes.
    Mais il y a une méthode IDirect3DDevice9Ex::WaitForVBlank() qui permet peut-être de gérer cela.

    Sinon Nikau6 si tu ne résoud pas le problème c'est que tu as atteint les limites du GDI et que tu devrais passer à Direct X carrèment ou autres ( SFML...)

  19. #19
    Membre extrêmement actif Avatar de nikau6
    Homme Profil pro
    Inscrit en
    Février 2008
    Messages
    406
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Etats-Unis

    Informations forums :
    Inscription : Février 2008
    Messages : 406
    Points : 737
    Points
    737
    Par défaut
    Medinoc,Je ne saurais pas t'éxpliquer pourquoi j'ai un frame core de 6500fps.Tous ce que je peux dire, c'est que je sur de l'avoir.Et même des fois plus.Mon frame core arrive autour de 170fps lorsque le prog affiche 1000 sprites à l'écran et qu'il calcul pour chacun d'entre si ils sont en collision avec un autre sprite unique.
    Donc, avec GDI se probléme de synchro ne semble pas avoir de solutions.Je vais passer sous directX alors.
    Merci

  20. #20
    Membre extrêmement actif Avatar de nikau6
    Homme Profil pro
    Inscrit en
    Février 2008
    Messages
    406
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Etats-Unis

    Informations forums :
    Inscription : Février 2008
    Messages : 406
    Points : 737
    Points
    737
    Par défaut
    En fait, pour être plus précis, j'utilise "PeekMessage()" et je place à l'intérieur ma fonction de dessins, Bitblt()(pour le double buffer), ainsi que ma fonction de traitement des évenements clavier et de calcul.La façon classique de faire quoi

Discussions similaires

  1. Réponses: 10
    Dernier message: 03/03/2007, 13h46
  2. API Win32 problème de fermeture de fenêtre
    Par Ano dans le forum Windows
    Réponses: 3
    Dernier message: 13/12/2006, 21h37
  3. [API WIN32] Double problème: DialogBox && Statusbar
    Par Ackboo dans le forum Windows
    Réponses: 9
    Dernier message: 20/08/2006, 13h15
  4. Réponses: 2
    Dernier message: 26/07/2006, 00h33
  5. Problème de declaration de variable (API Win32)
    Par barbarello dans le forum Windows
    Réponses: 2
    Dernier message: 30/01/2006, 09h57

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