Thread: Pong...from scratch

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    100

    Pong...from scratch

    Hey I'm pretty comfortable with C++ now, and I was wondering about making pong in c++ from scratch without any APIs is very difficult. Where would I learn the necessary skills to make it from the ground up? I know the basic loop of games and the framework of operations for games.

  2. #2
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    What platform do you want to make it for? DOS, Windows, Linux? There's considerations for each, take your pick and we'll get back to you after the beep.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    100
    Windows.

    Which brings me to my question too:

    I know how to program things. Just I don't understand ANY of the functions of making and controlling windows. I've just been programming DOS so far, but that's just because it's the default. I obviously want to make my own window. I have some idea as to some of the classes and such. Here's my globals planning for my code. But when I start a program in Dev-C++ it provides me with the window thing. Will that do? I still need to manipulate it do I not? When I fire the window up it's just grey. How do I actually put stuff in it?

    Code:
    /////////////GLOBALS/////////////
    
    #include <iostream>
    
    class ball {
         public:
         float Ball
         //color (white)
         //speed (rate, velocity (can be implemented by vectors))
         //collision data
         //model space ratio to world space 
    }
    
    class player {
         //float paddle ????, (size)
         //color (white for first implementation)
         //collision
         //model space ratio to world space
         //restrict to Y-Axis
    }
    Of course they're just comments, but hey they're a start.

    My first implementation i've planned is to make a correct window and not some stupid DOS command prompt window. Next, I'll draw the ping pong ball in it. Then I'll implement the physics (no idea as to the code, but I heard vectors might work well). Next, one paddle bouncing the ball (from the left side of the screen).

    I have ideas, but ideas are nothing without properly executed code.

    P.S. I have Bloodshed Dev-C++ 4.9.9.2.
    Last edited by blankstare77; 07-24-2005 at 07:50 PM.

  4. #4
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    Controlling windows is almost as simple as DOS. You can use DirectInput, SDL or WindowsAPI for catching the input and same for rendering the objects to your screen. I'd recomment SDL for a pong game, I use it myself, fast, simple and cross-platform. Oh and I finally found a way of catching arrow keys (and other special keys) in dev-c++. You should getch() and check if it's a 224, if it is, you getch() the second time and get the value of your arrow key
    what does signature stand for?

  5. #5
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    Except why use getch() when SDL already has the ability to handle input?

  6. #6
    Registered User
    Join Date
    Jul 2005
    Posts
    100
    Okay, I don't want to use APIs. I really want to write it myself. Where can I learn the commands? If I learn the commands, I know what's going on in the API and learning any API would be a cinch. (Oh and my video card is DirectX 7...so it can't use all the latest APIs so learning them right now with this PC would be a waste of time.

    Basically I want to build up to making 3D games and my goal is to make a 3D game looking like the original Quake. A lot of work, and if I'm gonna do it, I need to understand everything about it.
    Last edited by blankstare77; 07-25-2005 at 02:53 PM.

  7. #7
    Registered User
    Join Date
    Jul 2005
    Posts
    100
    Code:
    #include <windows.h>
    
    /*  Declare Windows procedure  */
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    
    /*  Make the class name into a global variable  */
    char szClassName[ ] = "WindowsApp";
    
    int WINAPI WinMain (HINSTANCE hThisInstance,
                        HINSTANCE hPrevInstance,
                        LPSTR lpszArgument,
                        int nFunsterStil)
    
    {
        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 color 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 */
               "GnipponG",       /* 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, nFunsterStil);
    
        /* 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;
    }
    
    
    /*  This function is called by the Windows function DispatchMessage()  */
    
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)                  /* handle the messages */
        {
            case WM_DESTROY:
                PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
                break;
            default:                      /* for messages that we don't deal with */
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
    
        return 0;
    }
    This is what comes up for Dev-C++ window. Uhh...do I just use that? (of course I renamed the window). How do i make a black playing field and a ball drawn on the screen?!

  8. #8
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    what devcpp is giving you is a standard code for beginning a win32 application.. almost every windows program starts out by initializing and registering a window class... so devcpp provides it for you... although it's starting to become somewhat outdated (does not initially make provisions for unicode for example)
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  9. #9
    Registered User
    Join Date
    Jul 2005
    Posts
    100
    Quote Originally Posted by The Brain
    what devcpp is giving you is a standard code for beginning a win32 application.. almost every windows program starts out by initializing and registering a window class... so devcpp provides it for you... although it's starting to become somewhat outdated (does not initially make provisions for unicode for example)
    http://www.winprog.org/tutorial/

    I'm learning about Windows Programming from this site. Seems to be doing well. Is anything outdated from here?

  10. #10
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    Quote Originally Posted by blankstare77
    Okay, I don't want to use APIs. I really want to write it myself.
    You mean you don't want to use APIs that simplify the work for you like SDL. You'll have to use an API to some degree though (aka the Win32 API).

  11. #11
    Registered User
    Join Date
    Jul 2005
    Posts
    100
    For Pong, how do you think I would do a demo of first drawing the ball (.bmp), then using code to make that ball bounce like a pong ball would, except it would be like those screen savers that just bounce (as in there wouldn't be a paddle...yet). First...the ball class:

    Code:
    class ball {
         public:
         float Ball
         //color (white)
         //speed (rate, velocity (can be implemented by vectors))
         //collision data
         //model space ratio to world space 
    }
    Although I've posted this already...would I just present the compiler with the data types inside the class, and since they're public, affect them later? Where inside the code should I?

    EDIT: Maybe something like this...?
    Code:
    class Ball {
    int state;
    int attr; //attributes of the ball
    int color; //white
    float position;
    //collision data, physics, and model space I have no clue of
    }
    Does that seem okay? And how do I implement the collision and physics and the space the model takes? the space would be affected by how big the .bmp is right? Hmm.

    Also I learned from a tutorial (link given above by me) how to make a bitmap transparent and only make the actual ball visible. How do I change that collision (i don't know how to implememt the initial collision, i'm just saying) so that it doesn't include the old extra "flab" if you will.
    Last edited by blankstare77; 07-26-2005 at 03:02 PM.

  12. #12
    Registered User
    Join Date
    Jul 2005
    Posts
    100
    Uhh another thing...about the window size...I found this code to make a window on the tutorial that I'm doing:

    Code:
    #include <windows.h>
    
    const char g_szClassName[] = "myWindowClass";
    
    // Step 4: the Window Procedure
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        switch(msg)
        {
            case WM_CLOSE:
                DestroyWindow(hwnd);
            break;
            case WM_DESTROY:
                PostQuitMessage(0);
            break;
            default:
                return DefWindowProc(hwnd, msg, wParam, lParam);
        }
        return 0;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
        LPSTR lpCmdLine, int nCmdShow)
    {
        WNDCLASSEX wc;
        HWND hwnd;
        MSG Msg;
    
        //Step 1: Registering the Window Class
        wc.cbSize        = sizeof(WNDCLASSEX);
        wc.style         = 0;
        wc.lpfnWndProc   = WndProc;
        wc.cbClsExtra    = 0;
        wc.cbWndExtra    = 0;
        wc.hInstance     = hInstance;
        wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
        wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
        wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
        wc.lpszMenuName  = NULL;
        wc.lpszClassName = g_szClassName;
        wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    
        if(!RegisterClassEx(&wc))
        {
            MessageBox(NULL, "Window Registration Failed!", "Error!",
                MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        // Step 2: Creating the Window
        hwnd = CreateWindowEx(
            WS_EX_CLIENTEDGE,
            g_szClassName,
            "The title of my window",
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
            NULL, NULL, hInstance, NULL);
    
        if(hwnd == NULL)
        {
            MessageBox(NULL, "Window Creation Failed!", "Error!",
                MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        ShowWindow(hwnd, nCmdShow);
        UpdateWindow(hwnd);
    
        // Step 3: The Message Loop
        while(GetMessage(&Msg, NULL, 0, 0) > 0)
        {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
        return Msg.wParam;
    }
    How bout that for a window? Is that okay? Do I need to memorize all this stuff?
    Is there any way to code all this stuff in C++ so I don't confuse the heck outta myself?
    Last edited by blankstare77; 07-26-2005 at 03:34 PM.

  13. #13
    ---
    Join Date
    May 2004
    Posts
    1,379
    Yeah it takes a bit to make a window but sooner or later you will remember it but don't try too hard.

  14. #14
    Registered User
    Join Date
    Jul 2005
    Posts
    100
    I need help...okay now that I have the window and I've made the background black, I've named and sized the window, I want to get a bitmap into that window. People say that you have to import a bitmap to get it to work in the compilation process, but I just don't see how to in Dev-C++ and I know there HAS to be a way.

  15. #15
    ---
    Join Date
    May 2004
    Posts
    1,379
    There are lots of different ways. You keep saying that you don't want to use any API's but you already are using the WindowsAPI. Windows has its own set of graphics functions know has the GDI. Go read a book or a tutorial about it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cant get a PONG
    By x77 in forum Networking/Device Communication
    Replies: 7
    Last Post: 07-25-2007, 09:48 AM
  2. The Story Of Pong (my game cookie)
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 05-16-2005, 11:14 PM
  3. pong
    By lambs4 in forum Game Programming
    Replies: 2
    Last Post: 06-02-2003, 04:00 PM
  4. Battle Pong
    By DOlson in forum Game Programming
    Replies: 13
    Last Post: 01-08-2003, 11:32 PM
  5. God pong -update
    By gamegod3001 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 09-03-2001, 09:16 PM