Day 3 of the Level Editor project

Yesterday I wrote that today's objective would be to make the input more user-friendly, so that's exactly what I did.

Instead of dragging and dropping the rectangle, the user now has to click once, then when he/she moves the mouse a rectangular shape will be shown, and as soon as he/she clicks again the rectangle will be drawn and stored in the level data.

In order to achieve this I defined two new variables in game.h:

LevelDrawElement* newElement;
bool newElementActive;

I set newElementActive = false; in Initialize and I modified Update and Draw as follows:

void Game::Update_LevelEditor(DWORD tickCount, DWORD lastTickCount)
{
GlobalStatics globalStatics;

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

mouse->buttonPressed = g_mouse->buttonPressed;
}
}

void Game::Draw_LevelEditor(DWORD tickCount)
{
GlobalStatics globalStatics;

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;
}
}

if (newElementActive)
{
switch (newElement->type)
{
case Solid:
glColor3f(1.0f, 0.8f, 0.2f);
glBegin(GL_LINE_LOOP);
glVertex2f(newElement->coordX0, newElement->coordY0);
glVertex2f(g_mouse->coordX, newElement->coordY0);
glVertex2f(g_mouse->coordX, globalStatics.worldY - g_mouse->coordY);
glVertex2f(newElement->coordX0, globalStatics.worldY - g_mouse->coordY);
glEnd();
break;
}
}
}

To make this work, an additional code is also needed in WinMain.cpp:

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

That's it! A user-friendly rudimentary level editor. Probably the next thing to do is to implement the saving function so that the designed level can actually be stored on disk.

As a side-note, I've been made aware of the level editor Tiled, which is available at https://www.mapeditor.org/. Still I won't abandon my project as in the end I want to extend it to make the levels playable, as well.

Kommentare

Beliebte Posts aus diesem Blog

Digital Art Natives

Three Branches of Creative Activity: Engineering - Art - Science

Warum ich Mensa verlassen habe