Click here to Skip to main content
11,790,816 members (50,011 online)
Click here to Skip to main content


, 3 Sep 2001 133K 2.9K 29
Rate this:
Please Sign up or sign in to vote.
The Space Donuts 3D for MFC.
<!-- Link to source file download -->
  • Download source files - 496 Kb
  • <!-- Link to demo file download -->
  • Download demo project - 583 Kb
  • <!-- Article image -->

    Sample Image - KillTux1.gif

    <!-- Add the rest of your HTML here -->


    Most of the samples that come with DirectX 8 SDK don't use MFC. What if we want to take a sample and add some code that requires MFC?
    This article describes the steps to convert the 'Space Donuts 3D' into an MFC application.

    Create the Framework

    figure 1

    Start VC, Select File/New from the menu and select MFC Appwizard(exe). Give a name (ex.'BattleMachine') and press OK to start the wizard. Select Single document as the type of application and uncheck the Document/View architecture support (Fig.1).
    Click Finish.

    Adding Donuts

    The Sample from the SDK have a lot of global functions & variables. I created a new class named C3DEngine and copy most of the global functions/variables as members of C3DEngine. Then i added a global instance of C3DEngine after the decleration of theApp.

    CBattleMachineApp theApp;
    C3DEngine g_3DEngine;

    I also added the following two lines in BattleMachine.h after the definition of CBattleMachineApp

    extern CBattleMachineApp theApp;
    extern C3DEngine g_3DEngine;

    You can use the Space Donuts 3D engine in you application by adding the files 3DEngine.cpp and 3DEngine.h. Those files contain any other classes, structs and definitions used by space donuts.

    There are 2 functions from space donuts 3D that we can't make them members of C3DEngine. One is ConfigureInputDevicesCB which is a Callback function. We have to change it's code a little, to reference g_3DEngine member functions.

    BOOL CALLBACK ConfigureInputDevicesCB( IUnknown* pUnknown, VOID* pUserData )
    	//if( g_dwAppState != APPSTATE_ACTIVE )
    	if( g_3DEngine.g_dwAppState != APPSTATE_ACTIVE )
    		return TRUE;

    The other function is WindowProc. I moved all of it's code to the CMainFrame::WindowProc. Once again, references to previously globals has to change into references to members of g_3DEngine.

    Initialisation and Changes to the framework

    To initialise the 3D Engine you have to set g_hWndMain to a valid HWND and then call CreateGameObjects by passing that same HWND.
    Also a good idea is to set the window size to 640x480 as the space donuts 3D. We do that in CMainFrame::OnCreate by using the WHND of the MainFrame.

    int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
    	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
    		return -1;
    	// create a view to occupy the client area of the frame
    	if (!m_wndView.Create(NULL, NULL, AFX_WS_DEFAULT_VIEW,
    		CRect(0, 0, 0, 0), this, AFX_IDW_PANE_FIRST, NULL))
    		TRACE0("Failed to create view window\n");
    		return -1;
    	SetWindowPos( NULL , 50,50, 640,480, SWP_NOOWNERZORDER   | SWP_NOREPOSITION   |SWP_NOZORDER    );
    	g_3DEngine.g_hWndMain = GetSafeHwnd();
        // Create the game objects (display objects, sounds, input devices,
        // menus, etc.)
        if( FAILED( g_3DEngine.CreateGameObjects( g_3DEngine.g_hWndMain ) ) )
            return FALSE;
    	return 0;

    Then we have to change the function CMainFrame::OnSetFocus. The function generated by appwizard gives focus to the CChildView window. The CMainFrame should keep the focus, because we need to handle keypress from the CMainFrame::WindowProc.

    // CMainFrame message handlers
    void CMainFrame::OnSetFocus(CWnd* pOldWnd)
    	// forward focus to the view window

    Finally we add the virtual function OnIdle to the CBattleMachineApp class. The following code will render the next frame.

    BOOL CBattleMachineApp::OnIdle(LONG lCount) 
    	// Render a frame during idle time (no messages are waiting)
    	if( g_3DEngine.g_bDisplayReady )
    	return true;

    Other changes

    I made some changes to the original space donuts engine.

    First, i replaced the sprites of donuts, pyramid, cube and sphere with Tux. There's no fun killing donuts after all!

    Second, I made the landscape more flat by changing the function C3DEngine::HeightField. The height of every object (ship,bullets,donuts ) depence on that function.

    // Simple function to define "hilliness" for terrain
    inline FLOAT C3DEngine::HeightField( FLOAT x, FLOAT z )
    	return (cosf(x/2.0f+0.2f)*cosf(z/1.5f-0.2f)+1.0f)*0.1f - 2.0f;

    Third, i added the ability to the ship to change altitude. By using the mouse wheel, instead of changing ships, you can lift or drop the ship's nose. Try to fly above all those Tuxs and kill them with no fear of dropping on them.


    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    About the Author

    Kastellanos Nikos
    Founder tainicom
    Greece Greece
    No Biography provided

    You may also be interested in...

    Comments and Discussions

    Generalcan't compile Pin
    jokokoko8-Aug-03 18:52
    memberjokokoko8-Aug-03 18:52 
    GeneralRe: can't compile Pin
    Kastellanos Nikos10-Aug-03 22:04
    memberKastellanos Nikos10-Aug-03 22:04 
    QuestionHow to compile Pin
    ap132526-Jul-03 4:58
    sussap132526-Jul-03 4:58 
    AnswerRe: How to compile Pin
    Kastellanos Nikos3-Aug-03 21:21
    memberKastellanos Nikos3-Aug-03 21:21 
    GeneralAnimationSets Pin
    William Barry16-Jul-02 16:42
    sussWilliam Barry16-Jul-02 16:42 
    GeneralRe: AnimationSets Pin
    Kastellanos Nikos16-Jul-02 20:15
    memberKastellanos Nikos16-Jul-02 20:15 
    GeneralIntersing Pin
    Anonymous5-Mar-02 8:17
    memberAnonymous5-Mar-02 8:17 
    GeneralNeed som tips Pin
    Carlos Mechoso Sandin27-Sep-01 3:42
    memberCarlos Mechoso Sandin27-Sep-01 3:42 
    GeneralRe: Need som tips Pin
    Kastellanos Nikos16-Jul-02 20:12
    memberKastellanos Nikos16-Jul-02 20:12 
    QuestionWho's the sadder? The Sad bloke or the guy who mocks him? Pin
    Xelous9-Sep-01 22:20
    memberXelous9-Sep-01 22:20 
    GeneralSad bloke Pin
    Greenpeace13-Mar-01 3:43
    memberGreenpeace13-Mar-01 3:43 
    GeneralRe: Sad bloke Pin
    Anonymous Pete14-Mar-01 5:20
    memberAnonymous Pete14-Mar-01 5:20 
    GeneralEven sadder Pin
    Greenpeace19-Mar-01 6:24
    memberGreenpeace19-Mar-01 6:24 
    GeneralRe: Even sadder Pin
    AlexMarbus19-Mar-01 7:06
    memberAlexMarbus19-Mar-01 7:06 
    GeneralThis is funny Pin
    Farhan Noor Qureshi27-Jun-01 11:54
    memberFarhan Noor Qureshi27-Jun-01 11:54 
    GeneralRe: Even sadder Pin
    Anonymous17-Jun-04 14:35
    sussAnonymous17-Jun-04 14:35 
    GeneralRe: Even sadder Pin
    VickyC#11-Oct-06 4:32
    memberVickyC#11-Oct-06 4:32 
    GeneralThat is soooo wrong! :) Pin
    James R. Twine12-Mar-01 2:54
    memberJames R. Twine12-Mar-01 2:54 
       How can you attack Tux like that?  You should change the bullets so that they have "FUD" in them! Smile | :)


    -=- James.
    GeneralRe: From Dazimran - Linux users are hypocritical Pin
    Dazimran8-Oct-01 8:21
    memberDazimran8-Oct-01 8:21 
    GeneralRe: From Dazimran - Linux users are hypocritical Pin
    Dazimran8-Oct-01 8:22
    memberDazimran8-Oct-01 8:22 
    GeneralTee hee, its great! Pin
    Norm Almond11-Mar-01 4:11
    memberNorm Almond11-Mar-01 4:11 
    GeneralRe: Tee hee, its great! Pin
    Anonymous5-Sep-01 4:54
    memberAnonymous5-Sep-01 4:54 
    GeneralRe: Tee hee, its great! Pin
    Anonymous10-Sep-01 13:05
    memberAnonymous10-Sep-01 13:05 
    GeneralRe: Tee hee, its great! Pin
    Anonymous31-May-02 16:42
    memberAnonymous31-May-02 16:42 
    GeneralRe: Tee hee, its great! Pin
    Anonymous31-Oct-02 11:53
    sussAnonymous31-Oct-02 11:53 

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

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

    | Advertise | Privacy | Terms of Use | Mobile
    Web01 | 2.8.1509028.1 | Last Updated 4 Sep 2001
    Article Copyright 2001 by Kastellanos Nikos
    Everything else Copyright © CodeProject, 1999-2015
    Layout: fixed | fluid