diff --git a/src/kekengine/cpp/defaults.cpp b/src/kekengine/cpp/defaults.cpp index a7ba046..745aaae 100644 --- a/src/kekengine/cpp/defaults.cpp +++ b/src/kekengine/cpp/defaults.cpp @@ -108,7 +108,7 @@ void init() { Input::addMouseListener(MouseCallback(defaultMouseCallback, nullptr)); options = new ButtonElement(px(0), px(100), px(100), px(50)); - UI::addElement(options); + //UI::addElement(options); } } diff --git a/src/kekengine/cpp/engine.cpp b/src/kekengine/cpp/engine.cpp index 9637f45..31869c4 100644 --- a/src/kekengine/cpp/engine.cpp +++ b/src/kekengine/cpp/engine.cpp @@ -87,7 +87,7 @@ int init() { kekData.screenWidth = 800.0f; kekData.screenHeight = 600.0f; - kekData.window = glfwCreateWindow(kekData.screenWidth, kekData.screenHeight, "KekEngine", NULL, NULL); + kekData.window = glfwCreateWindow(kekData.screenWidth, kekData.screenHeight, "KekEngine", nullptr, nullptr); if(!kekData.window) { const char *errorMsg; int code = glfwGetError(&errorMsg); @@ -122,7 +122,7 @@ int init() { if (flags & GL_CONTEXT_FLAG_DEBUG_BIT) { glEnable(GL_DEBUG_OUTPUT); glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS); - glDebugMessageCallback(glDebugOutput, NULL); + glDebugMessageCallback(glDebugOutput, nullptr); glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, nullptr, GL_TRUE); } @@ -140,8 +140,19 @@ int init() { return KEK_ERROR; } + MemoryBuffer *buf = Resource::loadResource("image/icon.png"); + if(buf) { + int width, height, nrChannels; + unsigned char *data = stbi_load_from_memory((stbi_uc *) buf->buffer, buf->length, &width, &height, &nrChannels, 0); + GLFWimage image; + image.width = width; + image.height = height; + image.pixels = data; + glfwSetWindowIcon(kekData.window, 1, &image); + delete buf; + } + kekData.activeCamera = new Camera(); - std::cout << "Poop cam is " << kekData.activeCamera << std::endl; kekData.shader = new Shader("shader/mesh/vertex.glsl", "shader/mesh/fragment.glsl"); kekData.shader->initLighting(); @@ -166,7 +177,7 @@ int init() { Defaults::init(); fpsText = new TextElement(px(0), px(0)); - UI::addElement(fpsText); + //UI::addElement(fpsText); return KEK_SUCCESS; } @@ -280,7 +291,8 @@ int start() { prevTime = time; for(UIElement *uiEl : kekData.uiElements) { - uiEl->drawAll(UIPoint(0, 0), uiProjection); + UIPoint pos = uiEl->getPosition(); + uiEl->drawAll(pos, uiProjection); } glDisable(GL_BLEND); diff --git a/src/kekengine/cpp/fonts.cpp b/src/kekengine/cpp/fonts.cpp index 73f6da7..4bd2449 100644 --- a/src/kekengine/cpp/fonts.cpp +++ b/src/kekengine/cpp/fonts.cpp @@ -97,7 +97,7 @@ void TextObject::allocateBuffer(TextBlock *block, int numChars) { glBindVertexArray(block->vao); glBindBuffer(GL_ARRAY_BUFFER, block->vbo); - glBufferData(GL_ARRAY_BUFFER, targetSize * sizeof(RenderChar), NULL, GL_DYNAMIC_DRAW); // 6 verts/char, 4 floats/vertex + glBufferData(GL_ARRAY_BUFFER, targetSize * sizeof(RenderChar), nullptr, GL_DYNAMIC_DRAW); // 6 verts/char, 4 floats/vertex glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0); glEnableVertexAttribArray(0); diff --git a/src/kekengine/cpp/input.cpp b/src/kekengine/cpp/input.cpp index 9114881..5e76b2f 100644 --- a/src/kekengine/cpp/input.cpp +++ b/src/kekengine/cpp/input.cpp @@ -81,7 +81,6 @@ GLFWKeyState getKeyState(KeyBinding binding) { } void setCursorMode(GLFWCursorMode mode) { - //std::cout << "POOP is " << mode << std::endl; glfwSetInputMode(kekData.window, GLFW_CURSOR, (int) mode); kekData.uiCursorMode = mode; } diff --git a/src/kekengine/cpp/shader.cpp b/src/kekengine/cpp/shader.cpp index 6c81193..89c9e54 100644 --- a/src/kekengine/cpp/shader.cpp +++ b/src/kekengine/cpp/shader.cpp @@ -40,13 +40,13 @@ static GLuint compileShader(GLenum type, std::string path) { GLint success; GLuint shaderID = glCreateShader(type); - glShaderSource(shaderID, 1, &src, NULL); + glShaderSource(shaderID, 1, &src, nullptr); glCompileShader(shaderID); glGetShaderiv(shaderID, GL_COMPILE_STATUS, &success); if(!success) { char log[512]; - glGetShaderInfoLog(shaderID, 512, NULL, log); + glGetShaderInfoLog(shaderID, 512, nullptr, log); std::cout << "Failed to compile shader \"" << path << "\":\n" << log << std::endl; throw std::exception(); } @@ -75,7 +75,7 @@ static GLuint compileProgram(unsigned int n, GLenum *types, std::string *paths) if(!success) { char log[512]; - glGetProgramInfoLog(id, 512, NULL, log); + glGetProgramInfoLog(id, 512, nullptr, log); std::cout << "Failed to link program:\n" << log << std::endl; throw std::exception(); } diff --git a/src/kekengine/cpp/ui.cpp b/src/kekengine/cpp/ui.cpp index e92a097..569d80d 100644 --- a/src/kekengine/cpp/ui.cpp +++ b/src/kekengine/cpp/ui.cpp @@ -61,6 +61,7 @@ std::vector UIElement::getChildren() { } void UIElement::addChild(UIElement *child) { + child->parent = this; children.push_back(child); } @@ -97,7 +98,7 @@ bool UIElement::hoverAll(UIPoint pos, UIPoint screenPos) { return false; } - UIElement *hoveredChild = NULL; + UIElement *hoveredChild = nullptr; for(UIElement *child : children) { UIPoint childPos = child->getPosition(); int relX = pos.x - childPos.x; @@ -135,7 +136,7 @@ bool UIElement::clickAll(UIPoint pos, UIPoint screenPos, GLFWMouseButton button) UIBounds bounds = getBounds(); if(!bounds.contains(pos)) return false; // Only used by topmost parent UIElement - UIElement *clickedChild = NULL; + UIElement *clickedChild = nullptr; for(UIElement *child : children) { if(!child->clickable) continue; UIPoint childPos = child->getPosition(); @@ -146,7 +147,7 @@ bool UIElement::clickAll(UIPoint pos, UIPoint screenPos, GLFWMouseButton button) clickedChild = child; } - if(clickedChild != NULL) { + if(clickedChild != nullptr) { UIPoint childPos = clickedChild->getPosition(); clickedChild->clickAll(UIPoint(pos.x - childPos.x, pos.y - childPos.y), screenPos, button); }else { diff --git a/src/kekengine/cpp/uielements.cpp b/src/kekengine/cpp/uielements.cpp index 1337b80..eea474c 100644 --- a/src/kekengine/cpp/uielements.cpp +++ b/src/kekengine/cpp/uielements.cpp @@ -180,10 +180,6 @@ UIBounds RectangleElement::getBounds() { return offsetUIBounds(uiToScreen(w), uiToScreen(h), origin); } -void RectangleElement::setColor(Color color) { - this->color = color; -} - void RectangleElement::draw(UIPoint screenPos, glm::mat4 projection) { kekData.uiRectangleShader->use(); @@ -199,7 +195,9 @@ void RectangleElement::draw(UIPoint screenPos, glm::mat4 projection) { } ButtonElement::ButtonElement(UIValue x, UIValue y, UIValue w, UIValue h): RectangleElement(x, y, w, h) { - this->text = new TextElement(px(0), px(0)); + this->text = new TextElement(pw(0.5), ph(0.5)); + text->origin = Origin::CENTER; + addChild(text); } ButtonElement::~ButtonElement() { @@ -210,28 +208,16 @@ UIElementType ButtonElement::getType() { return UIElementType::BUTTON; } -TextElement *ButtonElement::getText() { - return text; -} - -void ButtonElement::setColor(Color color) { - -} - -void ButtonElement::setOnClickCallback(Callable onClick) { - this->onClick = onClick; -} - void ButtonElement::hover(UIPoint pos, UIPoint screenPos) { - RectangleElement::setColor(Colors::YELLOW); + } -void ButtonElement::hoverEnter() { - RectangleElement::setColor(Colors::RED); +void ButtonElement::hoverEnter(UIPoint pos, UIPoint screenPos) { + color = hoverColor; } void ButtonElement::hoverExit() { - + color = defaultColor; } void ButtonElement::click(UIPoint pos, UIPoint screenPos, GLFWMouseButton button) { diff --git a/src/kekengine/include/color.h b/src/kekengine/include/color.h index c28de98..dce7d39 100644 --- a/src/kekengine/include/color.h +++ b/src/kekengine/include/color.h @@ -20,19 +20,20 @@ struct Color { class Colors { public: - static constexpr Color RED = Color(1.0, 0.0, 0.0); - static constexpr Color ORANGE = Color(1.0, 0.5, 0.0); - static constexpr Color YELLOW = Color(1.0, 1.0, 0.0); - static constexpr Color GREEN = Color(0.0, 1.0, 0.0); - static constexpr Color CYAN = Color(0.0, 1.0, 1.0); - static constexpr Color BLUE = Color(0.0, 0.0, 1.0); - static constexpr Color PURPLE = Color(0.5, 0.0, 0.5); - static constexpr Color MAGENTA = Color(1.0, 0.0, 1.0); - static constexpr Color GRAY = Color(0.5, 0.5, 0.5); - static constexpr Color WHITE = Color(1.0, 1.0, 1.0); - static constexpr Color BLACK = Color(0.0, 0.0, 0.0); + static constexpr Color RED = Color(1.0, 0.0, 0.0); + static constexpr Color ORANGE = Color(1.0, 0.5, 0.0); + static constexpr Color YELLOW = Color(1.0, 1.0, 0.0); + static constexpr Color GREEN = Color(0.0, 1.0, 0.0); + static constexpr Color CYAN = Color(0.0, 1.0, 1.0); + static constexpr Color BLUE = Color(0.0, 0.0, 1.0); + static constexpr Color PURPLE = Color(0.5, 0.0, 0.5); + static constexpr Color MAGENTA = Color(1.0, 0.0, 1.0); + static constexpr Color GRAY = Color(0.5, 0.5, 0.5); + static constexpr Color WHITE = Color(1.0, 1.0, 1.0); + static constexpr Color BLACK = Color(0.0, 0.0, 0.0); + static constexpr Color TRANSPARENT = Color(0.0, 0.0, 0.0, 0.0); Colors() = delete; }; -} \ No newline at end of file +} diff --git a/src/kekengine/include/ui.h b/src/kekengine/include/ui.h index ac2bf63..a361d99 100644 --- a/src/kekengine/include/ui.h +++ b/src/kekengine/include/ui.h @@ -25,7 +25,7 @@ struct UIBounds { UIBounds(int x, int y, int w, int h): x(x), y(y), w(w), h(h) {} inline bool contains(UIPoint pos) { - return pos.x < x || pos.y < y || pos.x >= x + w || pos.y >= y + h; + return pos.x > x && pos.y > y && pos.x < x + w && pos.y < y + h; } }; @@ -152,7 +152,7 @@ protected: int uiToScreen(UIValue val); public: - // Returns the bounds of the element relative to its origin (as returned by getX() and getY()) + // Returns the bounds of the element relative to its origin virtual UIBounds getBounds() = 0; void drawAll(UIPoint screenPos, glm::mat4 projection); diff --git a/src/kekengine/include/uielements.h b/src/kekengine/include/uielements.h index 6a58332..a0f31d8 100644 --- a/src/kekengine/include/uielements.h +++ b/src/kekengine/include/uielements.h @@ -47,12 +47,13 @@ class RectangleElement: public UIElement { protected: UIValue w, h; - Color color; unsigned int vao; unsigned int vbo; public: + Color color; + RectangleElement(UIValue x, UIValue y, UIValue w, UIValue h); virtual ~RectangleElement(); @@ -61,35 +62,27 @@ public: virtual UIBounds getBounds(); - virtual void setColor(Color color); - virtual void draw(UIPoint screenPos, glm::mat4 projection); }; class ButtonElement: public RectangleElement { -protected: +public: TextElement *text; - Color color; + Color defaultColor; + Color hoverColor; Callable onClick; -public: ButtonElement(UIValue x, UIValue y, UIValue w, UIValue h); virtual ~ButtonElement(); virtual UIElementType getType(); - TextElement *getText(); - - virtual void setColor(Color color); - - void setOnClickCallback(Callable onClick); - virtual void hover(UIPoint pos, UIPoint screenPos); - virtual void hoverEnter(); + virtual void hoverEnter(UIPoint pos, UIPoint screenPos); virtual void hoverExit(); diff --git a/src/kekengine/res/image/icon.png b/src/kekengine/res/image/icon.png new file mode 100644 index 0000000..40aa335 Binary files /dev/null and b/src/kekengine/res/image/icon.png differ diff --git a/src/kekengine/res/shader/rectangle/fragment.glsl b/src/kekengine/res/shader/rectangle/fragment.glsl index bb08b1f..e9b1840 100644 --- a/src/kekengine/res/shader/rectangle/fragment.glsl +++ b/src/kekengine/res/shader/rectangle/fragment.glsl @@ -6,4 +6,4 @@ uniform vec4 rectColor; void main() { color = rectColor; -} \ No newline at end of file +} diff --git a/src/kekengine/res/shader/rectangle/vertex.glsl b/src/kekengine/res/shader/rectangle/vertex.glsl index 33e457a..facf88c 100644 --- a/src/kekengine/res/shader/rectangle/vertex.glsl +++ b/src/kekengine/res/shader/rectangle/vertex.glsl @@ -7,4 +7,4 @@ uniform vec4 bounds; void main() { gl_Position = projection * vec4(position * bounds.zw + bounds.xy, 0.0, 1.0); -} \ No newline at end of file +} diff --git a/src/kekgame/cpp/kekgame.cpp b/src/kekgame/cpp/kekgame.cpp index 3f37986..9a6c5a4 100644 --- a/src/kekgame/cpp/kekgame.cpp +++ b/src/kekgame/cpp/kekgame.cpp @@ -6,27 +6,16 @@ using namespace kek; -static KeyBinding keyWow; - void periodicCallback(GLFWwindow *window, void *data){ - if(Input::getKeyState(keyWow) == GLFW_PRESS) { - //std::cout << "WOW" << std::endl; - } + } void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods, void *data){ - if(key == Input::getKeyBinding(keyWow).key && action == GLFW_PRESS) { - std::cout << "WOW" << std::endl; - if(glfwGetInputMode(window, GLFW_CURSOR) == GLFW_CURSOR_DISABLED) { - glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); - }else { - glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); - } - } + } void mouseButtonCallback(GLFWwindow *window, int button, int action, int mods, void *data){ - std::cout << "HELLO" << std::endl; + } int main(int argc, char **argv) { @@ -61,13 +50,15 @@ int main(int argc, char **argv) { Engine::setActiveScene(scene); - keyWow = Input::createKeyBinding("wow", GLFW_KEY_O); Input::addPeriodicCallback(PeriodicCallback(periodicCallback, nullptr)); Input::addKeyListener(KeyCallback(keyCallback, nullptr)); Input::addMouseButtonListener(MouseButtonCallback(mouseButtonCallback, nullptr)); ButtonElement *btn = new ButtonElement(px(0), px(100), px(200), px(50)); - btn->getText()->setText("Hello There!"); + btn->text->color = Colors::BLACK; + btn->defaultColor = btn->color = Colors::WHITE; + btn->hoverColor = Colors::GRAY; + btn->text->setText("Hello There!"); UI::addElement(btn); Engine::start();