Implement emoji detection and green frame
authorLukas Jiriste <ljiriste@student.42prague.com>
Wed, 3 Apr 2024 22:25:07 +0000 (00:25 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Wed, 3 Apr 2024 22:25:07 +0000 (00:25 +0200)
This implementation is so very slow because of the transparency issues,
but it is functional and without leaks.
I would like to refactor some of the functions as they git quite verbose
with the layered structures.

solution/emoji.xpm [new file with mode: 0644]
solution/green_frame.xpm [new file with mode: 0644]
solution/main.c

diff --git a/solution/emoji.xpm b/solution/emoji.xpm
new file mode 100644 (file)
index 0000000..6f1301e
--- /dev/null
@@ -0,0 +1,233 @@
+/* XPM */
+static char * emoji_xpm[] = {
+"50 50 180 2",
+"      c #FFFFFF",
+".     c #FEFEFE",
+"+     c #FDFDFD",
+"@     c #FCFCFC",
+"#     c #EDEDED",
+"$     c #C4C4C4",
+"%     c #9D9D9D",
+"&     c #8A8A8A",
+"*     c #838383",
+"=     c #868686",
+"-     c #969696",
+";     c #ACACAC",
+">     c #D0D0D0",
+",     c #F8F8F8",
+"'     c #FAFAFA",
+")     c #F6F6F6",
+"!     c #A0A0A0",
+"~     c #393939",
+"{     c #020202",
+"]     c #000000",
+"^     c #030303",
+"/     c #010101",
+"(     c #040404",
+"_     c #161616",
+":     c #5D5D5D",
+"<     c #BFBFBF",
+"[     c #FBFBFB",
+"}     c #DDDDDD",
+"|     c #444444",
+"1     c #141414",
+"2     c #2D2D2D",
+"3     c #383838",
+"4     c #373737",
+"5     c #232323",
+"6     c #080808",
+"7     c #050505",
+"8     c #E3E3E3",
+"9     c #3A3A3A",
+"0     c #484848",
+"a     c #E9E9E9",
+"b     c #D2D2D2",
+"c     c #878787",
+"d     c #292929",
+"e     c #7C7C7C",
+"f     c #777777",
+"g     c #191919",
+"h     c #F9F9F9",
+"i     c #E8E8E8",
+"j     c #757575",
+"k     c #C9C9C9",
+"l     c #EFEFEF",
+"m     c #252525",
+"n     c #070707",
+"o     c #BBBBBB",
+"p     c #727272",
+"q     c #767676",
+"r     c #E0E0E0",
+"s     c #6A6A6A",
+"t     c #272727",
+"u     c #424242",
+"v     c #CDCDCD",
+"w     c #6E6E6E",
+"x     c #2F2F2F",
+"y     c #989898",
+"z     c #333333",
+"A     c #F4F4F4",
+"B     c #9E9E9E",
+"C     c #5C5C5C",
+"D     c #E6E6E6",
+"E     c #848484",
+"F     c #8E8E8E",
+"G     c #B6B6B6",
+"H     c #F2F2F2",
+"I     c #F3F3F3",
+"J     c #5E5E5E",
+"K     c #555555",
+"L     c #B2B2B2",
+"M     c #090909",
+"N     c #C3C3C3",
+"O     c #EBEBEB",
+"P     c #999999",
+"Q     c #2E2E2E",
+"R     c #696969",
+"S     c #121212",
+"T     c #C5C5C5",
+"U     c #D8D8D8",
+"V     c #898989",
+"W     c #0E0E0E",
+"X     c #131313",
+"Y     c #7E7E7E",
+"Z     c #F1F1F1",
+"`     c #464646",
+" .    c #1A1A1A",
+"..    c #A1A1A1",
+"+.    c #5F5F5F",
+"@.    c #BEBEBE",
+"#.    c #5B5B5B",
+"$.    c #979797",
+"%.    c #939393",
+"&.    c #363636",
+"*.    c #B8B8B8",
+"=.    c #8D8D8D",
+"-.    c #F0F0F0",
+";.    c #D1D1D1",
+">.    c #D5D5D5",
+",.    c #171717",
+"'.    c #CECECE",
+").    c #CBCBCB",
+"!.    c #9A9A9A",
+"~.    c #616161",
+"{.    c #DADADA",
+"].    c #F7F7F7",
+"^.    c #404040",
+"/.    c #858585",
+"(.    c #060606",
+"_.    c #A9A9A9",
+":.    c #212121",
+"<.    c #343434",
+"[.    c #B1B1B1",
+"}.    c #737373",
+"|.    c #1D1D1D",
+"1.    c #C2C2C2",
+"2.    c #A2A2A2",
+"3.    c #1B1B1B",
+"4.    c #D6D6D6",
+"5.    c #282828",
+"6.    c #E7E7E7",
+"7.    c #A6A6A6",
+"8.    c #181818",
+"9.    c #959595",
+"0.    c #D3D3D3",
+"a.    c #5A5A5A",
+"b.    c #414141",
+"c.    c #2A2A2A",
+"d.    c #818181",
+"e.    c #3D3D3D",
+"f.    c #B7B7B7",
+"g.    c #E2E2E2",
+"h.    c #8F8F8F",
+"i.    c #929292",
+"j.    c #2B2B2B",
+"k.    c #111111",
+"l.    c #909090",
+"m.    c #6F6F6F",
+"n.    c #151515",
+"o.    c #E5E5E5",
+"p.    c #A3A3A3",
+"q.    c #6C6C6C",
+"r.    c #D7D7D7",
+"s.    c #303030",
+"t.    c #4F4F4F",
+"u.    c #747474",
+"v.    c #BDBDBD",
+"w.    c #222222",
+"x.    c #323232",
+"y.    c #8C8C8C",
+"z.    c #454545",
+"A.    c #535353",
+"B.    c #101010",
+"C.    c #494949",
+"D.    c #CCCCCC",
+"E.    c #808080",
+"F.    c #F5F5F5",
+"G.    c #CACACA",
+"H.    c #4C4C4C",
+"I.    c #9F9F9F",
+"J.    c #595959",
+"K.    c #9C9C9C",
+"L.    c #C7C7C7",
+"M.    c #6D6D6D",
+"N.    c #7D7D7D",
+"O.    c #BCBCBC",
+"P.    c #6B6B6B",
+"Q.    c #B4B4B4",
+"R.    c #787878",
+"S.    c #E1E1E1",
+"T.    c #1C1C1C",
+"U.    c #353535",
+"V.    c #545454",
+"W.    c #7A7A7A",
+"      . . . . . . . . + @   + +     .   # $ % & * = - ; > +   + @     +   @       @ @ . + + + . . . ",
+"    . . . . . . . . . + + , '   ) ! ~ { ] ] ^ / ] ] ] ( ] ] _ : <   [ [ @ + +   . [   . . . . . .   ",
+"  . . . . . . . + + + @     } | ] ] ^ { { ] 1 2 3 4 5 6 7 ] { / 7 7 * @ @   ) + + @   . . . . .     ",
+"  . .       . + @ @ [   8 9 ] { ( / 0 % a   [   + [     [ b c d ] ] ] ] e @     +   . + + + + + .   ",
+"+     ' . . [   ,     f / ] ] g ; .   + + . . . . . . [   h . @ i j ] ^ ^ 6 k ' + + . .   + +     + ",
+". . .       h @   l m ] ^ n o + @   ' + + . . . . . . + .     [ . . . p { ] { q   @   +   + @   @   ",
+"  + [   h @   + r / { 6 s   + [ @     + . . . . . . .   @ + @ . +   +   # t ] { u '     h @ . +     ",
+"  + [   + @ @ v ] ( ] <   @ +     + ' . . .         . + +   .   +   @ . [ + w ] / x @ +   .     .   ",
+"  . . '     } ] ^ 7 a   +   . . +                           +   +     @ @   + y ] { z     [   . . . ",
+". .         ] ^ { A ' +   [ .     +                   +     . .   . . +   ' @ @ B ] ] C     ,   . . ",
+". . . . h d ] ] D + . [ @ +   [     +                   . .     + [       + @ . + E ^ / y   @ +   + ",
+". + + . F 7 { G + + . '   .     .                           [   . .     @     ,   . 0 ^ ] H + ' [   ",
+". [   I ] ^ J   @   @   . + . @   .                       .       '     + +     h     ( ] 3 .       ",
+"+   ' K n { + . @   [ @     . .   . +                     . [   [   + ' . @   '   . @ L ^ M N   .   ",
+"[ . O ] ] P . + + h       @ + [ + .                   @ .   . + . ,       + [ + +   . @ Q ] 2       ",
+".   R ] S           @ @ . @   @   . .                     +   '   . .   + +     + + .   T ] ( U .   ",
+"  [ ] ] V   [   ' +     @ C W X Y   @ . . + + . . . . @   +     Z ` 6  ...'   . .   @   [  .{ +.  @ ",
+". @.^ ] , .         [ + Q ] ] ( ^ #.. . + + . .   . .   .     [ n ] { ] ] $.[ + + .   h   %.{ / @   ",
+"  C ] &.@ @ [     ,   *.^ ( ( ] ] 7 [ . . + .     . + . +   @ =.] ^ ( ( ] /   @ [ + [ . . -.] / ;.  ",
+".  .] & @   ' @   .   =.] { ] ( { ] >.+ + + .     . +     @   J / { ] ] ( ] [ '   . . .   + ,.( E [ ",
+"  ] { '.@   + +     . ).7 ^ / / { / [         . . . . +   + [ !./ { ^ { ^ M .   h ,   @   . ~.] 0 . ",
+"{.{ ] @   ]..   [ [ @ [ ^.] ^ ] ] /.  + + . . . . . .   '     . S (.] ] / _.  '   . .   . ' $.( ,.+ ",
+"G (.n [       . .   '   [ /.:.<.[.@ [ @ @ + .     . .   @   [ +   }.|.9 1.  @ [ [   @ + .   < ] ^   ",
+"2.( 3.. [   + . .   @ . [     , .   @ . . .         . @   . ].    h     + @     . +   . [   4.] ( ' ",
+"$.] 5.  @ . @   . +   +   '       +                     .   +     @ @ + + +   '   .     . + 6.{ ] ].",
+"- ] t , .   .     .         @ .   +         . .       +   @   . ' @     [           +   .   6.] ] [ ",
+"7.{ 8.  + ' +   . +   + .       + +                       +         + +   [     @   .   h   U ] / ' ",
+"*.] 6 .   @   [     .       + .     +                   @       . +     .     +       [   + $ ] ]   ",
+"{.^ ] ) . .             [     .   . .                     . .       .   +   [ .       . .   9./ 3.+ ",
+"h ^ ] 0.+ + [   .   ' '   .     @ .                   h   +       .   .   +     .   .     @ a.] b.  ",
+"   ./ F .   +   . .     . .   ,     .                   '     . .   +   @ + +   +   '   + , c.{ d., ",
+"' a.^ e.h         [ + @ +   +     +                 . .   .     . [   .     [   . +       , ] ] v   ",
+"  f.] ] [ + @ h .   '   g.p     '                 . + +   [   @ '   @ >.h.[   '     @ @ . %.( ]     ",
+"  , ( ( i.'     + +   } ] 7 : +   @ . + + + .     . +   @   @ .   . h ( ] j.+ . + + [ .   :.7 C . + ",
+")   #.] k..   , [ @ + @ l.] { m.+ + . . + @ + . . . +   @ @ + ) h   n.{ ^ o..   @   .   v / ^ ;.@   ",
+"  @ 8 { { p.+   . @   h   q./ { `   @   . + @ + . . . + +   h   r.n ] / v   . + .   ' [ &.] s.  . . ",
+"+ + . t./ 6   .   +   [ [   R ] / M @.. . . + + . [     ,     u./ ] ( k   [ +   h . + v.] 6 v..   . ",
+"+ + . Z / ] ~.  + + + @   H   7.] ] ] w.7.@     '   , ' O }.] ( / |.O   . . ' .   '   7 ] x.  @ [   ",
+". . . . y.7 ] <   . @   h   ' . -.^.] ] { ] 8.z.a.A.~ B.] ( ] ( c .     [ . +   '   C.] / l +       ",
+"  . + + ' 5.{ ] O @     [ .     ].  D.t./ { ] { ^ { { / ] M E.Z + + +       @ + + c ] ] !.[ @ [   [ ",
+". + @   . ' { / ] F.[ ].  F.    @   + + + G.=.: H.A.s I.}   . ' [   + + .   @ @ ; ] ^ J.,     @     ",
+"  . + +   @ 4.7 ] k.I   ' @   @ @ [         [   [ [       h @   . @ [ . @ [   K.] / Q   . [ @   . @ ",
+"    + @   '   $ { / ] L.    @ .   +   [   . . +     , @     [ . + '   + . + M.{ { 5.+ , @     +     ",
+"  + [   @   + + r.] 7 ] N.. [   + . '   @   +   ].    +     . ' . +   . # d ] / <.[   [ . . @ . +   ",
+"  . . h + .   @ . # g ] ] g O.+   @ . . + + . @       +   + . '   @   N.] / (.P.  . +   . + .       ",
+". . . .   . h     +   }.] ] { :.Q.  @ .   [ + + @ h .   .     + Z R.] ^ ] M v.+ . + ' . + + .       ",
+". . .     F.    . ,   [ S.x.^ { ] n K _.O   +     +     @ {./.<.] 7 { ] m.  h     +   . + + .       ",
+"  . . .       [ .   [   [   D.b.] ] ] ] ] ] T.U.u e.c.1 / { / ] 7 ] }.h @       + . . . . . .       ",
+"        .   @   . .   @   @ +   l y x.n / ] n ] ] 7 ] ^ ] ] g V.*.  @   + ,   @ .                   ",
+"      .   .     '   '   +   + + + + . + D *.!.= W.Y & I.D.A + + +   )   .   '     .                 "};
diff --git a/solution/green_frame.xpm b/solution/green_frame.xpm
new file mode 100644 (file)
index 0000000..9f0a47e
--- /dev/null
@@ -0,0 +1,55 @@
+/* XPM */
+static char * red_frame_xpm[] = {
+"50 50 2 1",
+"      c None",
+".     c #00FF00",
+"..................................................",
+"..................................................",
+"..................................................",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"...                                            ...",
+"..................................................",
+"..................................................",
+".................................................."};
index 7695c4e376dd7b1d6ca846a8cce57d7ea2964a0c..8629b281259533f6e38e372bb517cbe28c05d661 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/04/02 08:44:53 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/04/03 22:20:56 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/04/04 00:24:08 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -21,6 +21,8 @@
 #include <stdio.h>
 #include <jpeglib.h>
 
+#define EMOJI_TRESHOLD 255
+
 typedef struct s_mlx_session
 {
        void    *mlx;
@@ -43,8 +45,23 @@ typedef struct s_graphics
        t_mlx_session   mlx_ses;
        t_mlx_data              background;
        t_mlx_data              red_frame;
+       t_mlx_data              green_frame;
+       t_mlx_data              emoji;
 }                                      t_graphics;
 
+typedef struct s_position
+{
+       int     x;
+       int     y;
+}              t_position;
+
+typedef struct s_state
+{
+       t_graphics      graph;
+       t_position      pos;
+       t_vec           found;
+}                              t_state;
+
 void   decompress_to_image(struct jpeg_decompress_struct *cinfo,
                        t_mlx_data *img, void *mlx_ptr)
 {
@@ -109,7 +126,8 @@ void        flip_alpha(t_mlx_data *img)
                y = 0;
                while (y < img->height)
                {
-                       pixel = (char *)img->addr + y * img->line_length + x * img->bits_per_pixel / CHAR_BIT;
+                       pixel = (char *)img->addr
+                               + y * img->line_length + x * img->bits_per_pixel / CHAR_BIT;
                        color = uint_to_argb(*(unsigned int *)pixel);
                        color.a = 255 - color.a;
                        *(unsigned int *)pixel = argb_to_uint(color);
@@ -163,9 +181,12 @@ unsigned int       mix_colors(unsigned int back_uint, unsigned int front_uint)
        back = uint_to_argb(back_uint);
        front = uint_to_argb(front_uint);
        res.a = front.a - (255 - back.a) * front.a / 255;
-       res.r = (front.r * (255 - front.a) + back.r * (255 - back.a) * front.a / 255) / (255 - res.a);
-       res.g = (front.g * (255 - front.a) + back.g * (255 - back.a) * front.a / 255) / (255 - res.a);
-       res.b = (front.b * (255 - front.a) + back.b * (255 - back.a) * front.a / 255) / (255 - res.a);
+       res.r = (front.r * (255 - front.a)
+                       + back.r * (255 - back.a) * front.a / 255) / (255 - res.a);
+       res.g = (front.g * (255 - front.a)
+                       + back.g * (255 - back.a) * front.a / 255) / (255 - res.a);
+       res.b = (front.b * (255 - front.a)
+                       + back.b * (255 - back.a) * front.a / 255) / (255 - res.a);
        return (argb_to_uint(res));
 }
 
@@ -182,9 +203,12 @@ void       copy_image(t_mlx_data *dest, t_mlx_data *src, int x, int y)
                j = 0;
                while (j + y < dest->height && j < src->height)
                {
-                       dest_pix = (char *)dest->addr + ((j + y) * dest->line_length + (i + x) * dest->bits_per_pixel / CHAR_BIT);
-                       src_pix = (char *)src->addr + (j * src->line_length + i * src->bits_per_pixel / CHAR_BIT);
-                       *(unsigned int *)dest_pix = mix_colors(*(unsigned int *)dest_pix, *(unsigned int *)src_pix);
+                       dest_pix = (char *)dest->addr + ((j + y) * dest->line_length
+                                       + (i + x) * dest->bits_per_pixel / CHAR_BIT);
+                       src_pix = (char *)src->addr + (j * src->line_length
+                                       + i * src->bits_per_pixel / CHAR_BIT);
+                       *(unsigned int *)dest_pix = mix_colors(*(unsigned int *)dest_pix,
+                                       *(unsigned int *)src_pix);
                        ++j;
                }
                ++i;
@@ -193,7 +217,8 @@ void        copy_image(t_mlx_data *dest, t_mlx_data *src, int x, int y)
 }
 
 //     First use has to be with the largest picture, as that initializes the image
-int    mlx_put_image_to_window_transparency(t_mlx_session *s, t_mlx_data *img, int x, int y)
+int    mlx_put_image_to_window_transparency(t_mlx_session *s, t_mlx_data *img,
+               int x, int y)
 {
        static t_mlx_data       to_print;
 
@@ -207,7 +232,9 @@ int mlx_put_image_to_window_transparency(t_mlx_session *s, t_mlx_data *img, int
                to_print.img = mlx_new_image(s->mlx, img->width, img->height);
                if (!to_print.img)
                        return (1);
-               to_print.addr = mlx_get_data_addr(to_print.img, &to_print.bits_per_pixel, &to_print.line_length, &to_print.endian);
+               to_print.addr = mlx_get_data_addr(to_print.img,
+                               &to_print.bits_per_pixel, &to_print.line_length,
+                               &to_print.endian);
                to_print.width = img->width;
                to_print.height = img->height;
        }
@@ -230,13 +257,22 @@ void      free_session(t_mlx_session *s)
        return ;
 }
 
-void   cleanup(t_graphics *graphics)
+void   cleanup(t_state *state)
 {
-       if (graphics->background.img)
-               mlx_destroy_image(graphics->mlx_ses.mlx, graphics->background.img);
-       if (graphics->red_frame.img)
-               mlx_destroy_image(graphics->mlx_ses.mlx, graphics->red_frame.img);
-       free_session(&graphics->mlx_ses);
+       if (state->graph.background.img)
+               mlx_destroy_image(state->graph.mlx_ses.mlx,
+                       state->graph.background.img);
+       if (state->graph.red_frame.img)
+               mlx_destroy_image(state->graph.mlx_ses.mlx,
+                       state->graph.red_frame.img);
+       if (state->graph.green_frame.img)
+               mlx_destroy_image(state->graph.mlx_ses.mlx,
+                       state->graph.green_frame.img);
+       if (state->graph.emoji.img)
+               mlx_destroy_image(state->graph.mlx_ses.mlx, state->graph.emoji.img);
+       mlx_put_image_to_window_transparency(&state->graph.mlx_ses, NULL, 0, 0);
+       ft_vec_free(&state->found, NULL);
+       free_session(&state->graph.mlx_ses);
        return ;
 }
 
@@ -247,64 +283,174 @@ int      mlx_handle_key_press(int keycode, t_mlx_session *s)
        return (0);
 }
 
-int    no_event_handle(t_graphics *g)
+unsigned int   sqr_diff(unsigned char a, unsigned char b)
+{
+       if (a > b)
+               return ((a - b) * (a - b));
+       else
+               return ((b - a) * (b - a));
+}
+
+unsigned int   uint_color_sqr_diff(unsigned int uint_color1,
+               unsigned int uint_color2)
+{
+       t_argb                  color1;
+       t_argb                  color2;
+       unsigned int    res;
+
+       color1 = uint_to_argb(uint_color1);
+       color2 = uint_to_argb(uint_color2);
+       res = 0;
+       res += sqr_diff(color1.r, color2.r) / 255
+               * (255 - color1.a) * (255 - color2.a) / 255;
+       res += sqr_diff(color1.g, color2.g) / 255
+               * (255 - color1.a) * (255 - color2.a) / 255;
+       res += sqr_diff(color1.b, color2.b) / 255
+               * (255 - color1.a) * (255 - color2.a) / 255;
+       return (res);
+}
+
+int    emoji_encountered(t_mlx_data *background, t_mlx_data *emoji, int x, int y)
 {
-       static int      x = 0;
-       static int      y = 0;
+       int                             i;
+       int                             j;
+       char                    *pix_emoji;
+       char                    *pix_back;
+       unsigned int    sqr_diff;
+
+       sqr_diff = 0;
+       i = 0;
+       while (i < emoji->width)
+       {
+               j = 0;
+               while (j < emoji->height)
+               {
+                       pix_emoji = (char *)emoji->addr
+                               + j * emoji->line_length
+                               + i * emoji->bits_per_pixel / CHAR_BIT;
+                       pix_back = (char *)background->addr
+                               + (j + y) * background->line_length
+                               + (i + x) * background->bits_per_pixel / CHAR_BIT;
+                       sqr_diff += uint_color_sqr_diff(*(unsigned int *)pix_emoji,
+                                       *(unsigned int *)pix_back);
+                       ++j;
+               }
+               ++i;
+       }
+       sqr_diff /= emoji->width * emoji->height;
+       return (sqr_diff < EMOJI_TRESHOLD);
+}
 
-       if (!g->mlx_ses.mlx_win)
+void   print_green_frames(t_state *state)
+{
+       t_position      pos;
+       size_t          i;
+
+       i = 0;
+       while (i < state->found.size)
+       {
+               pos = *(t_position *)ft_vec_access(&state->found, i);
+               mlx_put_image_to_window_transparency(&state->graph.mlx_ses,
+                       &state->graph.green_frame, pos.x, pos.y);
+               ++i;
+       }
+       return ;
+}
+
+int    no_event_handle(t_state *state)
+{
+       if (!state->graph.mlx_ses.mlx_win)
                return (0);
-       if (x + g->red_frame.width + 1 == g->background.width)
+       if (state->pos.x + state->graph.emoji.width > state->graph.background.width)
        {
-               x = 0;
-               ++y;
+               state->pos.x = 0;
+               ++state->pos.y;
        }
-       if (y + g->red_frame.height < g->background.height)
+       if (state->pos.y + state->graph.emoji.height
+               <= state->graph.background.height)
        {
-               mlx_put_image_to_window_transparency(&g->mlx_ses, &g->background, 0, 0);
-               mlx_put_image_to_window_transparency(&g->mlx_ses, &g->red_frame, x, y);
-               /*
-               if (emoji_encountered(image, emoji, x, y))
-               {
-                       mlx_put_image_to_window_transparency(s->mlx, s->mlx_win, green_frame, x, y);
-                       record_pos(state, x, y);
-               }
-               */
-               ++x;
+               mlx_put_image_to_window_transparency(&state->graph.mlx_ses,
+                       &state->graph.background, 0, 0);
+               print_green_frames(state);
+               mlx_put_image_to_window_transparency(&state->graph.mlx_ses,
+                       &state->graph.red_frame, state->pos.x, state->pos.y);
+               if (emoji_encountered(&state->graph.background,
+                               &state->graph.emoji, state->pos.x, state->pos.y))
+                       ft_vec_append(&state->found, &state->pos);
+               ++state->pos.x;
        }
        else
-               mlx_close_win(&g->mlx_ses);
+               mlx_close_win(&state->graph.mlx_ses);
        return (0);
 }
 
+void   set_addresses(t_graphics *graphics)
+{
+       graphics->background.addr = mlx_get_data_addr(graphics->background.img,
+                       &graphics->background.bits_per_pixel,
+                       &graphics->background.line_length, &graphics->background.endian);
+       graphics->red_frame.addr = mlx_get_data_addr(graphics->red_frame.img,
+                       &graphics->red_frame.bits_per_pixel,
+                       &graphics->red_frame.line_length, &graphics->red_frame.endian);
+       graphics->green_frame.addr = mlx_get_data_addr(graphics->green_frame.img,
+                       &graphics->green_frame.bits_per_pixel,
+                       &graphics->green_frame.line_length, &graphics->green_frame.endian);
+       graphics->emoji.addr = mlx_get_data_addr(graphics->emoji.img,
+                       &graphics->emoji.bits_per_pixel,
+                       &graphics->emoji.line_length, &graphics->emoji.endian);
+       return ;
+}
+
+int    open_images(t_graphics *graphics, char **argv)
+{
+       int     res;
+
+       graphics->background.img = mlx_jpeg_file_to_image(graphics->mlx_ses.mlx,
+                       argv[1], &graphics->background.width,
+                       &graphics->background.height);
+       graphics->red_frame.img = mlx_xpm_file_to_image(graphics->mlx_ses.mlx,
+                       "red_frame.xpm", &graphics->red_frame.width,
+                       &graphics->red_frame.height);
+       graphics->green_frame.img = mlx_xpm_file_to_image(graphics->mlx_ses.mlx,
+                       "green_frame.xpm", &graphics->green_frame.width,
+                       &graphics->green_frame.height);
+       graphics->emoji.img = mlx_xpm_file_to_image(graphics->mlx_ses.mlx,
+                       "emoji.xpm", &graphics->emoji.width, &graphics->emoji.height);
+       res = graphics->background.img && graphics->red_frame.img
+               && graphics->green_frame.img && graphics->emoji.img;
+       if (res)
+               set_addresses(graphics);
+       return (!res);
+}
+
+int    init_state(t_state *state)
+{
+       state->graph.mlx_ses.mlx = mlx_init();
+       state->pos.x = 0;
+       state->pos.y = 0;
+       return (ft_vec_init(&state->found, sizeof(t_position)) != success
+               || !state->graph.mlx_ses.mlx);
+}
+
 void   display(char **argv)
 {
-       t_graphics              graphics;
-
-       graphics.mlx_ses.mlx = mlx_init();
-       graphics.background.img = mlx_jpeg_file_to_image(graphics.mlx_ses.mlx,
-                       argv[1], &graphics.background.width, &graphics.background.height);
-       graphics.red_frame.img = mlx_xpm_file_to_image(graphics.mlx_ses.mlx,
-                       "red_frame.xpm", &graphics.red_frame.width, &graphics.red_frame.height);
-       graphics.background.addr = mlx_get_data_addr(graphics.background.img,
-                       &graphics.background.bits_per_pixel,
-                       &graphics.background.line_length, &graphics.background.endian);
-       if (graphics.background.img && graphics.red_frame.img)
+       t_state state;
+
+       if (!init_state(&state) && !open_images(&state.graph, argv))
        {
-               graphics.red_frame.addr = mlx_get_data_addr(graphics.red_frame.img,
-                               &graphics.red_frame.bits_per_pixel,
-                               &graphics.red_frame.line_length, &graphics.red_frame.endian);
-               graphics.mlx_ses.mlx_win = mlx_new_window(graphics.mlx_ses.mlx,
-                               graphics.background.width, graphics.background.height, argv[1]);
-               mlx_hook(graphics.mlx_ses.mlx_win, KeyPress, KeyPressMask,
-                               mlx_handle_key_press, &graphics.mlx_ses);
-               mlx_hook(graphics.mlx_ses.mlx_win, DestroyNotify, NoEventMask,
-                               mlx_close_win, &graphics.mlx_ses);
-               mlx_loop_hook(graphics.mlx_ses.mlx, no_event_handle, &graphics);
-               mlx_put_image_to_window_transparency(&graphics.mlx_ses, &graphics.background, 0, 0);
-               mlx_loop(graphics.mlx_ses.mlx);
+               state.graph.mlx_ses.mlx_win = mlx_new_window(state.graph.mlx_ses.mlx,
+                               state.graph.background.width,
+                               state.graph.background.height, argv[1]);
+               mlx_hook(state.graph.mlx_ses.mlx_win, KeyPress, KeyPressMask,
+                       mlx_handle_key_press, &state.graph.mlx_ses);
+               mlx_hook(state.graph.mlx_ses.mlx_win, DestroyNotify, NoEventMask,
+                       mlx_close_win, &state.graph.mlx_ses);
+               mlx_loop_hook(state.graph.mlx_ses.mlx, no_event_handle, &state);
+               mlx_put_image_to_window_transparency(&state.graph.mlx_ses,
+                       &state.graph.background, 0, 0);
+               mlx_loop(state.graph.mlx_ses.mlx);
        }
-       cleanup(&graphics);
+       cleanup(&state);
        return ;
 }