SF | Создание кнопки обычным рендером

ANZR

Известный
Автор темы
168
104
Даров ребят, кто шарит
Крч, сижу на c++, решил создать интерфейс на чистом рендере. Все бы заебись, но вот вопрос)
в getRender такой функции нету, делал сам

У меня примерно вышло вот так
с++:
void CreateButton(int posX, int posY, int Width, int Height, D3DCOLOR StandartColor, D3DCOLOR PressedButton) // Создание кнопки и ее функции
{
    GetCursorPos(&cursorPos);
    if (cursorPos.x > posX && cursorPos.x < Width+posX && cursorPos.y > posY && cursorPos.y < Height+posY && GetAsyncKeyState(VK_LBUTTON)) {
        SF->getRender()->DrawBox(posX, posY, Width, Height, PressedButton);
    }
    else {
        SF->getRender()->DrawBox(posX, posY, Width, Height, StandartColor);
    }
}

Не бейте, если код хуйня, однако для этого я вашей помощи и прошу

Если есть другие варианты того, как реализовать код лучше - пишите, т.к это в будущем может вылиться в некоторые баги, а искать потом в чем причина мне не особо хочется
 
Решение
Накидал небольшой класс:
C++:
class Button {
public:
    float x, y, w, h;
    bool state;

    Button(float _x, float _y, float _w, float _h) : x(_x), y(_y), w(_w), h(_h), state(false) {}

    void Draw() {
        if (this->state) {
            state = false;
            return SF->getRender()->DrawBox(posX, posY, Width, Height, PressedButton);
        }
        return SF->getRender()->DrawBox(posX, posY, Width, Height, StandartColor);
    }

    bool HandleClick(float x, float y) noexcept {
        if ((this->x <= x && x - w <= this->x) && (this->y <= y && y - h <= this->y)) {
            return state = true;
        }
        return state = false;
    }
}

Можно еще сделать типа пула, только он урежет твою мобильность:
C++:
class...

kin4stat

mq-team
Всефорумный модератор
2,731
4,693
Накидал небольшой класс:
C++:
class Button {
public:
    float x, y, w, h;
    bool state;

    Button(float _x, float _y, float _w, float _h) : x(_x), y(_y), w(_w), h(_h), state(false) {}

    void Draw() {
        if (this->state) {
            state = false;
            return SF->getRender()->DrawBox(posX, posY, Width, Height, PressedButton);
        }
        return SF->getRender()->DrawBox(posX, posY, Width, Height, StandartColor);
    }

    bool HandleClick(float x, float y) noexcept {
        if ((this->x <= x && x - w <= this->x) && (this->y <= y && y - h <= this->y)) {
            return state = true;
        }
        return state = false;
    }
}

Можно еще сделать типа пула, только он урежет твою мобильность:
C++:
class ButtonPool {
    std::vector<Button*> Buttons;

    unsigned int CreateButton(float x, float y, float w, float h) {
        this->Buttons.push_back(new Button(x, y, w, h));
        return this->Buttons.size() - 1;
    }

    void HandleClick(LPARAM lParam) noexcept {
        float x = GET_X_LPARAM(lParam);
        float y = GET_Y_LPARAM(lParam);
        for (auto& i : this->Buttons) {
            i->HandleClick(x, y);
        }
    }

    void Draw() {
        for (auto& i : this->Buttons) {
            i->Draw();
        }
    }

    void DeleteButton(unsigned int id) {
        this->Buttons.erase(this->Buttons.begin() + id);
    }
}
Также при удалении кнопки, придется все иды смещать самому.

Касательно класса кнопки, чтобы обрабатывать нажатия, запихнуть в WndProc под такими условиями:
C++:
switch (uMsg) {
    case WM_LBUTTONDOWN: {
        float x = GET_X_LPARAM(lParam);
        float y = GET_Y_LPARAM(lParam);
        Button::HandleClick(x, y);
    }
}

Ща налетят всякие мемиры и человечики, и скажут что говно
 
  • Нравится
Реакции: ANZR

ANZR

Известный
Автор темы
168
104
Накидал небольшой класс:
C++:
class Button {
public:
    float x, y, w, h;
    bool state;

    Button(float _x, float _y, float _w, float _h) : x(_x), y(_y), w(_w), h(_h), state(false) {}

    void Draw() {
        if (this->state) {
            state = false;
            return SF->getRender()->DrawBox(posX, posY, Width, Height, PressedButton);
        }
        return SF->getRender()->DrawBox(posX, posY, Width, Height, StandartColor);
    }

    bool HandleClick(float x, float y) noexcept {
        if ((this->x <= x && x - w <= this->x) && (this->y <= y && y - h <= this->y)) {
            return state = true;
        }
        return state = false;
    }
}

Можно еще сделать типа пула, только он урежет твою мобильность:
C++:
class ButtonPool {
    std::vector<Button*> Buttons;

    unsigned int CreateButton(float x, float y, float w, float h) {
        this->Buttons.push_back(new Button(x, y, w, h));
        return this->Buttons.size() - 1;
    }

    void HandleClick(LPARAM lParam) noexcept {
        float x = GET_X_LPARAM(lParam);
        float y = GET_Y_LPARAM(lParam);
        for (auto& i : this->Buttons) {
            i->HandleClick(x, y);
        }
    }

    void Draw() {
        for (auto& i : this->Buttons) {
            i->Draw();
        }
    }

    void DeleteButton(unsigned int id) {
        this->Buttons.erase(this->Buttons.begin() + id);
    }
}
Также при удалении кнопки, придется все иды смещать самому.

Касательно класса кнопки, чтобы обрабатывать нажатия, запихнуть в WndProc под такими условиями:
C++:
switch (uMsg) {
    case WM_LBUTTONDOWN: {
        float x = GET_X_LPARAM(lParam);
        float y = GET_Y_LPARAM(lParam);
        Button::HandleClick(x, y);
    }
}

Ща налетят всякие мемиры и человечики, и скажут что говно
Кстати, по поводу WindowProc
Все написал, робит отменно. Однако есть проблема
Если я с ним отключу sf плагин через консоль, сначала ничего, но при повторном отключении игра крашит
Если не понял, я про команду pfree
Знаешь, с чем это связано?
 

barspinoff

Известный
126
33