aboutsummaryrefslogtreecommitdiffstats
path: root/imlib2.c
blob: 4b4c72a1018df071aba8d7122bd0b6cf7ae48ed0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Imlib2 stuff which I think I copied from somewhere.

#include <Imlib2.h>
#include <X11/Xlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

Display *disp;
Window win;
Visual *vis;
Colormap cm;
int depth;

int main(int argc, char **argv)
{
        XEvent ev;

        Imlib_Updates updates, current_update;
        Imlib_Image buffer;
        Imlib_Font font;
        Imlib_Color_Range range;
        int mouse_x = 0, mouse_y = 0;

        disp = XOpenDisplay(NULL);

        vis = DefaultVisual(disp, DefaultScreen(disp));
        depth = DefaultDepth(disp, DefaultScreen(disp));
        cm = DefaultColormap(disp, DefaultScreen(disp));

        win = XCreateSimpleWindow(disp, DefaultRootWindow(disp),
                        0, 0, 640, 480, 0, 0, 0);

        XSelectInput(disp, win, ButtonPressMask | ButtonReleaseMask |
                        PointerMotionMask | ExposureMask);

        XMapWindow(disp, win);

        imlib_set_cache_size(2048 * 1024);

        imlib_set_font_cache_size(512 * 1024);

        imlib_add_path_to_font_path("./ttfonts");

        imlib_set_color_usage(128);

        imlib_context_set_dither(1);

        imlib_context_set_display(disp);
        imlib_context_set_visual(vis);
        imlib_context_set_colormap(cm);
        imlib_context_set_drawable(win);

        while(true) {
                Imlib_Image image;
                int w, h, text_w, text_h;

                updates = imlib_updates_init();

                do {
                        XNextEvent(disp, &ev);
                        switch (ev.type) {
                                case Expose:
                                        updates = imlib_update_append_rect(updates,
                                                        ev.xexpose.x, ev.xexpose.y,
                                                        ev.xexpose.width, ev.xexpose.height);
                                        break;
                                case ButtonPress:
                                        exit(0);
                                        break;
                                case MotionNotify:
                                        image = imlib_load_image("./test_images/mush.png");
                                        imlib_context_set_image(image);
                                        w = imlib_image_get_width();
                                        h = imlib_image_get_height();

                                        imlib_context_set_image(image);
                                        imlib_free_image();

                                        updates = imlib_update_append_rect(updates,
                                                        mouse_x - (w / 2), mouse_y - (h / 2),
                                                        w, h);
                                        font = imlib_load_font("notepad/30");
                                        if (font) {
                                                char text[4096];
                                                imlib_context_set_font(font);
                                                sprintf(text, "Mouse is at %i, %i", mouse_x, mouse_y);
                                                imlib_get_text_size(text, &text_w, &text_h);
                                                imlib_free_font();

                                                updates = imlib_update_append_rect(updates,
                                                                320 - text_w / 2, 240 - text_h / 2,
                                                                text_w, text_h);
                                        }

                                        mouse_x = ev.xmotion.x;
                                        mouse_y = ev.xmotion.y;

                                        updates = imlib_update_append_rect(updates,
                                                        mouse_x - w / 2, mouse_y - h / 2,
                                                        w, h);
                                        font = imlib_load_font("notepad/30");
                                        if (font) {
                                                char text[4096];

                                                imlib_context_set_font(font);
                                                sprintf(text, "Mouse is at %i, %i", mouse_x, mouse_y);
                                                imlib_get_text_size(text, &text_w, &text_h);
                                                imlib_free_font();
                                                updates = imlib_update_append_rect(updates,
                                                                320 - text_w / 2, 240 - text_h / 2,
                                                                text_w, text_h);
                                        }
                                default:
                                        break;
                        }
                } while (XPending(disp));


                updates = imlib_updates_merge_for_rendering(updates, 640, 480);
                for (current_update = updates;
                                current_update;
                                current_update = imlib_updates_get_next(current_update)) {
                        int up_x, up_y, up_w, up_h;

                        imlib_updates_get_coordinates(current_update, &up_x, &up_y, &up_w, &up_h);

                        buffer = imlib_create_image(up_w, up_h);

                        imlib_context_set_blend(1);

                        image = imlib_load_image("./test_images/bg.png");

                        imlib_context_set_image(image);

                        w = imlib_image_get_width();
                        h = imlib_image_get_height();

                        imlib_context_set_image(buffer);

                        if (image) {
                                imlib_blend_image_onto_image(image, 0, 0, 0,
                                                w, h, -up_x, -up_y, 640, 480);

                                imlib_context_set_image(image);

                                imlib_free_image();
                        }

                        image = imlib_load_image("./test_images/mush.png");
                        imlib_context_set_image(image);
                        w = imlib_image_get_width();
                        h = imlib_image_get_height();
                        imlib_context_set_image(image);
                        if (image) {
                                imlib_blend_image_onto_image(image, 0, 0, 0, w, h,
                                                mouse_x - w / 2 - up_x,
                                                mouse_y - h / 2 - up_y,
                                                w, h);
                                imlib_context_set_image(image);
                                imlib_free_image();
                        }

                        range = imlib_create_color_range();
                        imlib_context_set_color_range(range);

                        imlib_context_set_color(255, 255, 255, 255);
                        imlib_add_color_to_color_range(0);

                        imlib_context_set_color(255, 200, 10, 100);
                        imlib_add_color_to_color_range(10);

                        imlib_context_set_color(0, 0, 0, 0);
                        imlib_add_color_to_color_range(20);

                        imlib_context_set_image(buffer);
                        imlib_image_fill_color_range_rectangle(-up_x, -up_y, 128, 128, -45);

                        imlib_free_color_range();

                        font = imlib_load_font("notepad/30");
                        if (font) {
                                char text[4096];

                                imlib_context_set_font(font);
                                imlib_context_set_image(buffer);
                                imlib_context_set_color(0, 0, 0, 255);
                                sprintf(text, "Mouse is at %i, %i", mouse_x, mouse_y);
                                imlib_get_text_size(text, &text_w, &text_h);
                                imlib_text_draw(320 - text_w / 2 - up_x,
                                                240 - text_h / 2 - up_y,
                                                text);
                                imlib_free_font();
                        }

                        imlib_context_set_blend(0);
                        imlib_context_set_image(buffer);
                        imlib_render_image_on_drawable(up_x, up_y);
                        imlib_free_image();
                }
                if (updates)
                        imlib_updates_free(updates);
        }
        return 0;
}

        /*Imlib_Image image = imlib_create_image(640, 480);*/

        /*imlib_context_set_image(image);*/

        /*imlib_context_set_color(255, 255, 255, 255);*/

        /*imlib_image_fill_rectangle(0, 0, 640, 480);*/

        /*imlib_context_set_color(255, 128, 0, 255);*/

        /*ImlibPolygon poly = imlib_polygon_new();*/

        /*imlib_polygon_add_point(poly, 0, 0);*/
        /*imlib_polygon_add_point(poly, 10, 440);*/
        /*imlib_polygon_add_point(poly, 600, 10);*/

        /*imlib_image_fill_polygon(poly);*/

        /*imlib_polygon_free(poly);*/

        /*imlib_save_image("imlibtest.png");*/

        /*imlib_free_image();*/

        /*return 0;*/
/*}*/