Skip to content

Commit b521da4

Browse files
author
beryll1um
committed
impl(icons, set_title): window icons implemented & set_title window method implemented
1 parent 348d7da commit b521da4

File tree

6 files changed

+106
-2
lines changed

6 files changed

+106
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Example : public Application
1313
{
1414
public:
1515

16-
virtual bool OnUpdate(Time&) override {
16+
virtual bool OnUpdate(Time) override {
1717
return poll_events();
1818
}
1919

examples/interfaces/interfaces.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ class ExampleInterfaces : public Application
6666
push_layer<TriangleLayer>(this, triangle);
6767
push_layer<ImGuiLayer>(triangle);
6868

69+
Icons icons("../texture/image.png");
70+
6971
set_frame_limit(60);
7072
}
7173

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#ifndef _UNIFIED_APPLICATION_WINDOW_ICONS_ICONS_HPP
2+
#define _UNIFIED_APPLICATION_WINDOW_ICONS_ICONS_HPP
3+
4+
# include <unified/core/int_types.hpp>
5+
# include <unified/core/string.hpp>
6+
7+
# include <initializer_list>
8+
9+
UNIFIED_BEGIN_NAMESPACE
10+
11+
class Icons
12+
{
13+
protected:
14+
15+
struct glfw_images_wrapper;
16+
17+
public:
18+
19+
Icons();
20+
Icons(const string &icon);
21+
Icons(std::initializer_list<string> icons);
22+
23+
virtual ~Icons();
24+
25+
void add_icon(const string &icon);
26+
27+
u32 count() const;
28+
29+
void *data();
30+
31+
protected:
32+
33+
glfw_images_wrapper *_images;
34+
35+
};
36+
37+
UNIFIED_END_NAMESPACE
38+
39+
#endif

include/unified/application/window/window.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
# include <unified/application/event/cursor_move.hpp>
1111
# include <unified/application/event/key_press.hpp>
1212

13+
# include <unified/application/window/icons/icons.hpp>
14+
1315
# include <functional>
1416

1517
UNIFIED_BEGIN_NAMESPACE
@@ -41,10 +43,13 @@ class Window
4143

4244
Window(string title, VideoMode mode, u32 style);
4345

44-
virtual ~Window() { }
46+
virtual ~Window();
4547

4648
bool poll_events() const;
4749

50+
void set_title(string title);
51+
void set_icons(Icons icons);
52+
4853
UNIFIED_NODISCARD Point2i get_size() const;
4954
void set_size(Point2i size);
5055

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <unified/application/window/icons/icons.hpp>
2+
3+
# include <GLFW/glfw3.h>
4+
# include <stb_image.h>
5+
# include <vector>
6+
7+
UNIFIED_BEGIN_NAMESPACE
8+
9+
struct Icons::glfw_images_wrapper
10+
{
11+
std::vector<GLFWimage> handle;
12+
};
13+
14+
Icons::Icons() : _images(new glfw_images_wrapper) { }
15+
16+
Icons::Icons(const string &icon) : _images(new glfw_images_wrapper) {
17+
add_icon(icon);
18+
}
19+
20+
Icons::Icons(std::initializer_list<string> icons) : _images(new glfw_images_wrapper) {
21+
for (const auto &icon : icons) {
22+
add_icon(icon);
23+
}
24+
}
25+
26+
Icons::~Icons() {
27+
for (const auto &icon : _images->handle) {
28+
stbi_image_free(icon.pixels);
29+
}
30+
delete _images;
31+
}
32+
33+
void Icons::add_icon(const string &icon) {
34+
GLFWimage image = { .pixels = stbi_load(icon.c_str(), &image.width, &image.height, 0, 4) };
35+
_images->handle.push_back(image);
36+
}
37+
38+
u32 Icons::count() const {
39+
return _images->handle.size();
40+
}
41+
42+
void *Icons::data() {
43+
return _images->handle.data();
44+
}
45+
46+
UNIFIED_END_NAMESPACE

src/application/window/window.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,23 @@ Window::Window(string title, VideoMode video_mode, u32 style) : _title(title), _
8080
});
8181
}
8282

83+
Window::~Window() {
84+
delete _window;
85+
}
86+
8387
bool Window::poll_events() const {
8488
glfwPollEvents();
8589
return !glfwWindowShouldClose(_window->glfw_handle);
8690
}
8791

92+
void Window::set_title(string title) {
93+
glfwSetWindowTitle(_window->glfw_handle, (_title = title).c_str());
94+
}
95+
96+
void Window::set_icons(Icons icons) {
97+
glfwSetWindowIcon(_window->glfw_handle, icons.count(), static_cast<GLFWimage*>(icons.data()));
98+
}
99+
88100
UNIFIED_NODISCARD Point2i Window::get_size() const {
89101
glfwGetWindowSize(_window->glfw_handle, (int*)&_video_mode.width, (int*)&_video_mode.height);
90102
return Point2i(_video_mode.width, _video_mode.height);

0 commit comments

Comments
 (0)