Click here to Skip to main content
15,886,634 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: register window message Pin
softwaremonkey19-Jun-11 19:41
softwaremonkey19-Jun-11 19:41 
QuestionMoving Objects in small increments Pin
Cyclone_S19-Jun-11 14:07
Cyclone_S19-Jun-11 14:07 
QuestionRe: Moving Objects in small increments Pin
enhzflep19-Jun-11 15:03
enhzflep19-Jun-11 15:03 
AnswerRe: Moving Objects in small increments Pin
Cyclone_S19-Jun-11 16:03
Cyclone_S19-Jun-11 16:03 
GeneralRe: Moving Objects in small increments Pin
enhzflep19-Jun-11 16:23
enhzflep19-Jun-11 16:23 
GeneralRe: Moving Objects in small increments Pin
Cyclone_S19-Jun-11 16:49
Cyclone_S19-Jun-11 16:49 
GeneralRe: Moving Objects in small increments Pin
enhzflep19-Jun-11 18:24
enhzflep19-Jun-11 18:24 
GeneralRe: Moving Objects in small increments Pin
enhzflep19-Jun-11 21:35
enhzflep19-Jun-11 21:35 
I got bored. Here try if this if you like - it's two balls, a red one and a white one. If you hit the red one with the white one it will move in a direction away from the white one before being slowed to a halt by friction. Of course your collision handling stuff will be different(hopefully pixel-accurate, unlike mine) for the paddle and the ball, but it's the same concepts at work.

Rose | [Rose]
#include <math.h>
#include <windows.h>
#include <windowsx.h>
#include <wingdi.h>

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

void handleCollisions();

/*  Make the class name into a global variable  */
char szClassName[ ] = "CodeBlocksWindowsApp";

int WINAPI WinMain (HINSTANCE hThisInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpszArgument,
                     int nCmdShow)
{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor =LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default colour as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Click to release a ball",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nCmdShow);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}

////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
float ballPosX, ballPosY, ballVelocityX, ballVelocityY;
float curMouseX, lastMouseX, curMouseY, lastMouseY;
int clientWidth, clientHeight;

const int myTimerId = 1;
const int intervalMs = 50;
const int ballRadius = 10; // pixels
const int cursorRadius = 15; // pixels


void handleCollisions()
{
    float dx, dy, dist;

    // part 1 - check for collisions with the mouse cursor
    dx = ballPosX - curMouseX;
    dy = ballPosY - curMouseY;

    dist = sqrt( dx*dx + dy*dy);
    if (dist<ballRadius+cursorRadius)
    {
        ballVelocityX = 0.5 * dx;
        ballVelocityY = 0.5 * dy;
    }

    // part 2 - check for collisions with the walls
    if (ballPosX < ballRadius)
        ballVelocityX = abs(ballVelocityX);

    if (ballPosY < ballRadius)
        ballVelocityY = abs(ballVelocityY);

    if (ballPosX > clientWidth-ballRadius)
        ballVelocityX = -1 * abs(ballVelocityX);

    if (ballPosY > clientHeight-ballRadius)
        ballVelocityY = -1 * abs(ballVelocityY);
}

VOID CALLBACK myTimerProc(HWND hwnd,        // handle to window for timer messages
    UINT message,     // WM_TIMER message
    UINT idTimer,     // timer identifier
    DWORD dwTime)     // current system time
{
	ballPosX += ballVelocityX;
	ballPosY += ballVelocityY;
	ballVelocityX *= 0.9;
	ballVelocityY *= 0.9;

    handleCollisions();

	InvalidateRect(hwnd, NULL, true);
}

/*  This function is called by the Windows function DispatchMessage()  */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT ps;
	HDC hdc;
    RECT clientRect;

    HBRUSH newBr, oldBr;


    switch (message)                  /* handle the messages */
    {
        case WM_CREATE:
            ballPosX = 100;
            ballPosY = 100;
            SetTimer(hwnd, myTimerId, intervalMs, myTimerProc);
            GetClientRect(hwnd, &clientRect);
            clientWidth = clientRect.right;
            clientHeight = clientRect.bottom;
            return true;

        case WM_DESTROY:
            KillTimer(hwnd, myTimerId);
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;

        case WM_PAINT:
            hdc = BeginPaint(hwnd, &ps);

            //draw the red ball
            newBr = CreateSolidBrush(RGB(250,0,0));
            oldBr = (HBRUSH)SelectObject(hdc, newBr);
            Ellipse(hdc, ballPosX-ballRadius,ballPosY-ballRadius,ballPosX+ballRadius,ballPosY+ballRadius);
            SelectObject(hdc, oldBr);
            DeleteObject(newBr);

            // draw the white ball
            Ellipse(hdc, curMouseX-cursorRadius, curMouseY-cursorRadius, curMouseX+cursorRadius, curMouseY+cursorRadius);

            EndPaint(hwnd, &ps);
            break;

        case WM_MOUSEMOVE:
            lastMouseX = curMouseX;
            lastMouseY = curMouseY;
            curMouseX = GET_X_LPARAM(lParam);
            curMouseY = GET_Y_LPARAM(lParam);
            InvalidateRect(hwnd,NULL,true);
            break;

        case WM_LBUTTONUP:
            // shoot the red ball from the top left corner
            ballPosX=11;
            ballPosY=11;
            ballVelocityX=6.2;
            ballVelocityY=18.1;
            break;

        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}
}

GeneralRe: Moving Objects in small increments Pin
Cyclone_S20-Jun-11 14:33
Cyclone_S20-Jun-11 14:33 
GeneralRe: Moving Objects in small increments Pin
enhzflep20-Jun-11 18:45
enhzflep20-Jun-11 18:45 
QuestionWSA Events Pin
csrss19-Jun-11 3:32
csrss19-Jun-11 3:32 
AnswerRe: WSA Events Pin
enhzflep19-Jun-11 4:13
enhzflep19-Jun-11 4:13 
GeneralRe: WSA Events Pin
csrss19-Jun-11 5:43
csrss19-Jun-11 5:43 
QuestionRe: WSA Events Pin
Mark Salsbery19-Jun-11 9:38
Mark Salsbery19-Jun-11 9:38 
GeneralRe: WSA Events Pin
csrss19-Jun-11 13:13
csrss19-Jun-11 13:13 
GeneralRe: WSA Events Pin
Mark Salsbery19-Jun-11 14:10
Mark Salsbery19-Jun-11 14:10 
GeneralRe: WSA Events Pin
csrss20-Jun-11 5:46
csrss20-Jun-11 5:46 
GeneralRe: WSA Events Pin
Mark Salsbery20-Jun-11 9:29
Mark Salsbery20-Jun-11 9:29 
GeneralRe: WSA Events Pin
csrss20-Jun-11 10:17
csrss20-Jun-11 10:17 
QuestionHow can I read all files from 'Recent File List' ? Pin
_Flaviu18-Jun-11 8:56
_Flaviu18-Jun-11 8:56 
AnswerRe: How can I read all files from 'Recent File List' ? Pin
Richard MacCutchan18-Jun-11 21:42
mveRichard MacCutchan18-Jun-11 21:42 
GeneralRe: How can I read all files from 'Recent File List' ? [modified] Pin
_Flaviu18-Jun-11 21:45
_Flaviu18-Jun-11 21:45 
GeneralRe: How can I read all files from 'Recent File List' ? Pin
Richard MacCutchan19-Jun-11 21:45
mveRichard MacCutchan19-Jun-11 21:45 
GeneralRe: How can I read all files from 'Recent File List' ? Pin
_Flaviu20-Jun-11 21:28
_Flaviu20-Jun-11 21:28 
GeneralRe: How can I read all files from 'Recent File List' ? Pin
_Flaviu22-Jun-11 20:24
_Flaviu22-Jun-11 20:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.