Testing Keyboard and Mouse Input

That’s a lot of code, and we’ve burned through the keyboard and mouse input subject quickly in this chapter! A demo is called for to test the functionality of the new input routines in the engine, as well as the new event functions added to the exports. I’ve got a great idea: We’ll create a demo that uses the mouse to draw particles! I’ve wanted to work more with the ParticleEmitter class again, and this is a good way to play with it while simultaneously testing input. The new DirectInput keyboard events will replace the old KEY_DOWN macro for the purpose of exiting the program via the Escape key. The mouse is used to draw particles with random velocities. See Figure 5.1.

Figure 5.1. The InputDemo program demonstrates keyboard and mouse input.


Although the InputDemo program runs fine in a window, I recommend running it in full-screen and windowed mode to see for yourself how the exclusive or nonexclusive mode affects the mouse and keyboard. If you’re using exclusive mode, the mouse will be completely trapped by the program unless you Alt+Tab to another window. Because the Input class intelligently handles the loss of devices, it will re-acquire the keyboard and mouse when you return to the demo.

#include "..EngineAdvanced2D.h"
using namespace Advanced2D;

ParticleEmitter *p;
Sprite *cursor;

bool game_preload()
{
    g_engine->setAppTitle("INPUT DEMO");
    g_engine->setFullscreen(false);
    g_engine->setScreenWidth(1024);
    g_engine->setScreenHeight(768);
    g_engine->setColorDepth(32);
    return 1;
}
bool game_init(HWND)
{
    p = new ParticleEmitter();
    p->loadImage("particle16.tga");
    p->setMax(0);
    p->setAlphaRange(50,200);
    p->setDirection(0);
    p->setSpread(270);
    p->setScale(1.5f);
    p->setLength(2000);

    //load cursor
    cursor = new Sprite();
    cursor->loadImage("particle16.tga");

    return true;
}

void game_update()
{
    p->update();
}

void game_keyPress(int key) { }
void game_keyRelease(int key)
{
    if (key == DIK_ESCAPE) g_engine->Close();
}

void game_mouseButton(int button)
{
    switch(button) {
        case 0: //button 1
            p->setVelocity( (rand() % 10 - 5) / 500.0f );
            p->add();
            break;
    }
}

void game_mouseMotion(int x,int y) { }
void game_mouseMove(int x,int y)
{
    float fx = (float)x;
    float fy = (float)y;
    cursor->setPosition(fx,fy);
    p->setPosition(fx,fy);
}
void game_mouseWheel(int wheel) { }

void game_render3d()
{
    g_engine->ClearScene(D3DCOLOR_XRGB(0,0,0));
}

void game_render2d()
{
    p->draw();
    cursor->draw();
}

void game_end()
{
    delete p;
    delete cursor;
}

That’s it for input. You can load up the current implementation of the Advanced2D engine and build a game with the tools developed so far. But we still have much ground to cover! For instance, we have no way to support sound effects or music yet. Good thing that is coming up in the next chapter.

 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset