Day 2 of the Level Editor project

We need to poll the mouse, so we define a new class:

#ifndef _MOUSE_H_
#define _MOUSE_H_

class Mouse
{
public:
BOOL buttonPressed;
unsigned int coordX;
unsigned int coordY;
};

#endif

In the class GL_Window we create a new variable Mouse *mouse; that references to this class. In our Initialize method of class Game, we write g_mouse = window->mouse; so that we can reference to the mouse data by means of g_mouse in analogy to g_keys.

To the Window method, we add the following lines:

case WM_LBUTTONDOWN:
window->mouse->buttonPressed = true;
window->mouse->coordX = GET_X_LPARAM(lParam);
window->mouse->coordY = GET_Y_LPARAM(lParam);
break;

case WM_LBUTTONUP:
window->mouse->buttonPressed = false;
window->mouse->coordX = GET_X_LPARAM(lParam);
window->mouse->coordY = GET_Y_LPARAM(lParam);
break;

Then we also need a new data structure for keeping up with the level details:

class LevelDrawElement
{
public:
LevelEditorDrawModes type;
unsigned int coordX0;
unsigned int coordY0;
unsigned int coordX1;
unsigned int coordY1;
};

where LevelEditorDrawModes is an enum that currently consists only of:

enum LevelEditorDrawModes
{
Delete = 0,
Solid
};

Then we define new Update and Draw methods which look like this:

void Game::Update_LevelEditor(DWORD tickCount, DWORD lastTickCount)
{
static unsigned int newElement_coordX0;
static unsigned int newElement_coordX1;
static unsigned int newElement_coordY0;
static unsigned int newElement_coordY1;
GlobalStatics globalStatics;

if (g_mouse->buttonPressed != mouse->buttonPressed)
{
if (g_mouse->buttonPressed)
{
newElement_coordX0 = g_mouse->coordX;
newElement_coordY0 = globalStatics.worldY - g_mouse->coordY;
}
else
{
unsigned int newElement_coordX1 = g_mouse->coordX;
unsigned int newElement_coordY1 = globalStatics.worldY - g_mouse->coordY;
LevelDrawElement *newLevelDrawElement = new LevelDrawElement();
newLevelDrawElement->type = levelEditorDrawMode;
newLevelDrawElement->coordX0 = min(newElement_coordX0, newElement_coordX1);
newLevelDrawElement->coordX1 = max(newElement_coordX0, newElement_coordX1);
newLevelDrawElement->coordY0 = min(newElement_coordY0, newElement_coordY1);
newLevelDrawElement->coordY1 = max(newElement_coordY0, newElement_coordY1);
level.push_back(newLevelDrawElement);
}

mouse->buttonPressed = g_window->mouse->buttonPressed;
}
}

void Game::Draw_LevelEditor(DWORD tickCount)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

for (auto const& i : level) {
switch (i->type)
{
case Solid:
glColor3f(1.0f, 0.8f, 0.2f);
glBegin(GL_QUADS);
glVertex2f(i->coordX0, i->coordY0);
glVertex2f(i->coordX1, i->coordY0);
glVertex2f(i->coordX1, i->coordY1);
glVertex2f(i->coordX0, i->coordY1);
glEnd();
break;
}
}
}

And voilà! - We have a simple but working application that allows us to draw rectangles by means of drag-and-drop and stores them into a data structure we have defined ourselves.

The next steps will be to make the whole thing more user-friendly.

Kommentare

Beliebte Posts aus diesem Blog

The Demoscene

Digital Art Natives

Autobiographical Sketch