├── mlx ├── font.xcf ├── mlx_png.h ├── mlx_opengl.h ├── mlx_mouse.m ├── mlx_opengl.m ├── mlx_new_window.h ├── Makefile ├── mlx_int_str_to_wordtab.c ├── mlx_int.h ├── mlx.h ├── mlx_init_loop.m ├── mlx_new_image.m ├── mlx_shaders.c ├── mlx_xpm.c ├── mlx_png.c ├── mlx_rgb.c └── mlx_new_window.m ├── images ├── example.png ├── terminal_win.png └── terminal_give_up.png ├── textures ├── exit.png ├── wall.png ├── ship_up.png ├── background.png ├── ship_down.png ├── ship_left.png ├── ship_right.png ├── collectable.png ├── wall.xpm ├── ship_up.xpm ├── ship_down.xpm ├── ship_left.xpm ├── ship_right.xpm ├── collectable.xpm ├── exit.xpm └── background.xpm ├── maps ├── map1.ber ├── invalid.be ├── invalid.ber ├── map2.ber ├── give_up.ber ├── map3.ber └── map4.ber ├── .gitignore ├── win.c ├── libft ├── ft_strlen.c ├── ft_count_c.c ├── ft_calloc.c ├── ft_line_length.c ├── ft_strchr.c ├── ft_strnstr.c ├── ft_strjoin.c ├── ft_atoi.c ├── ft_count_lines.c ├── Makefile ├── libft.h ├── get_next_line_utils.c └── get_next_line.c ├── key_hook.c ├── put_imgs.c ├── so_long.c ├── map.c ├── init.c ├── Makefile ├── README.md ├── so_long.h ├── move.c └── parse_input.c /mlx/font.xcf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tblaase/so_long/HEAD/mlx/font.xcf -------------------------------------------------------------------------------- /images/example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tblaase/so_long/HEAD/images/example.png -------------------------------------------------------------------------------- /textures/exit.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tblaase/so_long/HEAD/textures/exit.png -------------------------------------------------------------------------------- /textures/wall.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tblaase/so_long/HEAD/textures/wall.png -------------------------------------------------------------------------------- /textures/ship_up.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tblaase/so_long/HEAD/textures/ship_up.png -------------------------------------------------------------------------------- /images/terminal_win.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tblaase/so_long/HEAD/images/terminal_win.png -------------------------------------------------------------------------------- /maps/map1.ber: -------------------------------------------------------------------------------- 1 | 1111111111111 2 | 10010000000C1 3 | 1000011111001 4 | 1P0011E000011 5 | 1111111111111 -------------------------------------------------------------------------------- /textures/background.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tblaase/so_long/HEAD/textures/background.png -------------------------------------------------------------------------------- /textures/ship_down.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tblaase/so_long/HEAD/textures/ship_down.png -------------------------------------------------------------------------------- /textures/ship_left.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tblaase/so_long/HEAD/textures/ship_left.png -------------------------------------------------------------------------------- /textures/ship_right.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tblaase/so_long/HEAD/textures/ship_right.png -------------------------------------------------------------------------------- /textures/collectable.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tblaase/so_long/HEAD/textures/collectable.png -------------------------------------------------------------------------------- /images/terminal_give_up.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tblaase/so_long/HEAD/images/terminal_give_up.png -------------------------------------------------------------------------------- /mlx/mlx_png.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | void *mlx_png_file_to_image(void *xvar, char *file, int *width, int *height); 5 | -------------------------------------------------------------------------------- /maps/invalid.be: -------------------------------------------------------------------------------- 1 | 1111111111 2 | 1P00000001 3 | 1000000001 4 | 10000000C1 5 | 1000000001 6 | 1000000001 7 | 1000000001 8 | 1000000011 9 | 100000E000 10 | 1111111111 11 | -------------------------------------------------------------------------------- /maps/invalid.ber: -------------------------------------------------------------------------------- 1 | 1111111111 2 | 1P00000001 3 | 1000000001 4 | 10000000C1 5 | 1000000001 6 | 1000000001 7 | 1000000001 8 | 1000000011 9 | 1000000E00 10 | 1111111111 11 | -------------------------------------------------------------------------------- /maps/map2.ber: -------------------------------------------------------------------------------- 1 | 1111111111111111111111111111111111 2 | 1E0000000000000C00000C000000000001 3 | 1010010100100000101001000000010101 4 | 1010010010101010001001000000010101 5 | 1P0000000C00C0000000000000000000C1 6 | 1111111111111111111111111111111111 7 | -------------------------------------------------------------------------------- /maps/give_up.ber: -------------------------------------------------------------------------------- 1 | 11111111111111111111111111111111111111111 2 | 1P0000E0000000000000000000000000000000001 3 | 10000000000000000C000000000000000C0000001 4 | 10000000000000000000000000000000000000001 5 | 10101010101010101010101010000C00001111111 6 | 10000000000000000000001011111111111111111 7 | 100000000000C0000000001011111111111111111 8 | 100000100000C0000000001011111111111111111 9 | 100000000000C0000000001011111111111111111 10 | 100000000000C0000000001011111111111111111 11 | 100000000000C0000000001011111111C11111111 12 | 100000000000C0000000001011111111111111111 13 | 10000000000000000000001E11111111111111111 14 | 11111111111111111111111111111111111111111 15 | -------------------------------------------------------------------------------- /mlx/mlx_opengl.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** 3 | ** mlx_opengl.h 4 | ** 5 | ** public include, use it after mlx.h 6 | ** designed only for minilibx_macos 7 | ** 8 | */ 9 | 10 | void *mlx_new_opengl_window(void *mlx_ptr, int size_x, int size_y, char *title); 11 | 12 | /* create an opengl window. put_image & pixel_put & string_put do not work there. */ 13 | 14 | int mlx_opengl_swap_buffers(void *win_ptr); 15 | 16 | /* the created window is double buffered. Use this funct to swap buffers */ 17 | /* this funct will call glFlush(). Don't call it. */ 18 | 19 | int mlx_opengl_window_set_context(void *win_ptr); 20 | 21 | /* in case multiple opengl windows are present, change opengl active context */ 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore all 2 | * 3 | 4 | # Unignore all with extensions 5 | !*.* 6 | 7 | # Unignore all dirs 8 | !*/ 9 | 10 | # Unignore Makefile 11 | !Makefile 12 | 13 | # Prerequisites 14 | *.d 15 | 16 | # Object files 17 | *.o 18 | *.ko 19 | *.obj 20 | *.elf 21 | 22 | # Linker output 23 | *.ilk 24 | *.map 25 | *.exp 26 | 27 | # Precompiled Headers 28 | *.gch 29 | *.pch 30 | 31 | # Libraries 32 | *.lib 33 | *.a 34 | *.la 35 | *.lo 36 | 37 | # Shared objects (inc. Windows DLLs) 38 | *.dll 39 | *.so 40 | *.so.* 41 | *.dylib 42 | 43 | # Executables 44 | *.exe 45 | *.out 46 | *.app 47 | *.i*86 48 | *.x86_64 49 | *.hex 50 | 51 | # Debug files 52 | *.dSYM/ 53 | *.su 54 | *.idb 55 | *.pdb 56 | 57 | # Kernel Module Compile Results 58 | *.mod* 59 | *.cmd 60 | .tmp_versions/ 61 | modules.order 62 | Module.symvers 63 | Mkfile.old 64 | dkms.conf 65 | 66 | # Visual Studio Code Debug Files 67 | .vscode 68 | -------------------------------------------------------------------------------- /win.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* win.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tblaase +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/10/07 16:12:06 by tblaase #+# #+# */ 9 | /* Updated: 2021/10/08 15:34:07 by tblaase ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "so_long.h" 14 | 15 | void ft_win(t_data *data) 16 | { 17 | printf("Congratulations, you found all the Diamonds and the exit.\n"); 18 | printf("You won!\n"); 19 | printf("Is %d moves really the best you can do?\n", data->counter); 20 | exit(EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /libft/ft_strlen.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlen.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tblaase +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/10/06 13:50:29 by tblaase #+# #+# */ 9 | /* Updated: 2021/10/06 13:52:09 by tblaase ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | 15 | size_t ft_strlen(const char *s) 16 | /* will return the length of a string 17 | ** will return 0 if the string is empty */ 18 | { 19 | int i; 20 | 21 | i = 0; 22 | while (s && s[i]) 23 | { 24 | i++; 25 | } 26 | return (i); 27 | } 28 | -------------------------------------------------------------------------------- /libft/ft_count_c.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_count_c.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tblaase +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/10/06 14:01:45 by tblaase #+# #+# */ 9 | /* Updated: 2021/10/06 14:05:47 by tblaase ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_count_c(char *s, char c) 16 | /* will count appearances of c inside s and return them as integer */ 17 | { 18 | int i; 19 | int x; 20 | 21 | i = 0; 22 | x = 0; 23 | if (!s) 24 | return (-1); 25 | while (s && s[i]) 26 | { 27 | if (s[i++] == c) 28 | x++; 29 | } 30 | return (x); 31 | } 32 | -------------------------------------------------------------------------------- /mlx/mlx_mouse.m: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #import 4 | #import 5 | 6 | #include "mlx_int.h" 7 | #include "mlx_new_window.h" 8 | 9 | int mlx_mouse_hide() 10 | { 11 | // CGDisplayHideCursor(kCGDirectMainDisplay); 12 | [NSCursor hide]; 13 | return (0); 14 | } 15 | 16 | int mlx_mouse_show() 17 | { 18 | // CGDisplayShowCursor(kCGDirectMainDisplay); 19 | [NSCursor unhide]; 20 | return (0); 21 | } 22 | 23 | int mlx_mouse_move(mlx_win_list_t *win, int x, int y) 24 | { 25 | CGPoint point; 26 | NSRect pos; 27 | id thewin; 28 | 29 | thewin = [(id)(win->winid) win]; 30 | pos = [thewin frame]; 31 | // printf("got win pos %f %f\n", pos.origin.x, pos.origin.y); 32 | point.x = pos.origin.x + x; 33 | point.y = NSHeight([[thewin screen] frame]) - NSHeight([(id)(win->winid) frame]) - pos.origin.y + 1 + y; 34 | CGWarpMouseCursorPosition(point); 35 | CGAssociateMouseAndMouseCursorPosition(true); 36 | return (0); 37 | } 38 | 39 | 40 | int mlx_mouse_get_pos(mlx_win_list_t *win, int *x, int *y) 41 | { 42 | CGPoint point; 43 | id thewin; 44 | NSRect pos; 45 | 46 | thewin = [(id)(win->winid) win]; 47 | pos = [(id)(win->winid) frame]; 48 | point = [thewin mouseLocationOutsideOfEventStream]; 49 | *x = point.x; 50 | *y = NSHeight(pos) - 1 - point.y; 51 | return (0); 52 | } 53 | -------------------------------------------------------------------------------- /libft/ft_calloc.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_calloc.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tblaase +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/10/06 18:35:23 by tblaase #+# #+# */ 9 | /* Updated: 2021/10/06 18:35:27 by tblaase ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_calloc(size_t nelem, size_t elsize) 16 | /* mallocs for the size of nelem * elsize and sets everything to NULL*/ 17 | { 18 | char *ptr; 19 | unsigned int i; 20 | 21 | i = 0; 22 | ptr = (char *)malloc(nelem * elsize); 23 | if (!ptr) 24 | return (NULL); 25 | while (i < nelem * elsize) 26 | { 27 | ptr[i] = 0; 28 | i++; 29 | } 30 | return (ptr); 31 | } 32 | -------------------------------------------------------------------------------- /libft/ft_line_length.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_line_length.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tblaase +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/10/06 15:42:17 by tblaase #+# #+# */ 9 | /* Updated: 2021/10/08 15:09:49 by tblaase ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_line_length(int fd) 16 | /* this function will find the length of the first line in a file */ 17 | { 18 | char buffer[1]; 19 | int length; 20 | int bytes; 21 | 22 | buffer[0] = '\0'; 23 | bytes = 1; 24 | length = 0; 25 | while (bytes == 1) 26 | { 27 | bytes = read(fd, buffer, 1); 28 | if (buffer[0] != '\n') 29 | length++; 30 | else 31 | break ; 32 | } 33 | return (length); 34 | } 35 | -------------------------------------------------------------------------------- /libft/ft_strchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tblaase +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/10/06 13:37:46 by tblaase #+# #+# */ 9 | /* Updated: 2021/10/06 13:53:02 by tblaase ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strchr(const char *s, int c) 16 | /* will return a pointer to the position of first appearance of c in s 17 | ** will return a pointer to NULL if not found */ 18 | { 19 | char a; 20 | int i; 21 | 22 | if (!s) 23 | return (NULL); 24 | a = c; 25 | i = 0; 26 | while (s && s[i] != '\0' && s[i] != a) 27 | i++; 28 | if (s[i] == a) 29 | return ((char *)(s + i)); 30 | else 31 | return (NULL); 32 | } 33 | -------------------------------------------------------------------------------- /mlx/mlx_opengl.m: -------------------------------------------------------------------------------- 1 | // mlx_opengl.m 2 | 3 | #import 4 | #import 5 | #import 6 | 7 | #include 8 | 9 | #include "mlx_int.h" 10 | #include "mlx_new_window.h" 11 | 12 | 13 | 14 | 15 | 16 | NSOpenGLPixelFormatAttribute pfa_attrs_opengl[] = 17 | { 18 | NSOpenGLPFADepthSize, 32, 19 | NSOpenGLPFADoubleBuffer, 20 | NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion4_1Core, 21 | 0 22 | }; 23 | 24 | 25 | 26 | void *mlx_new_opengl_window(mlx_ptr_t *mlx_ptr, int size_x, int size_y, char *title) 27 | { 28 | mlx_win_list_t *newwin; 29 | NSString *str; 30 | 31 | if ((newwin = malloc(sizeof(*newwin))) == NULL) 32 | return ((void *)0); 33 | newwin->img_list = NULL; 34 | newwin->next = mlx_ptr->win_list; 35 | newwin->nb_flush = 0; 36 | newwin->pixmgt = 0; 37 | mlx_ptr->win_list = newwin; 38 | 39 | NSRect windowRect = NSMakeRect(100, 100, size_x, size_y); 40 | str = [NSString stringWithCString:title encoding:NSASCIIStringEncoding]; 41 | newwin->winid = [[MlxWin alloc] initWithRect:windowRect andTitle:str pfaAttrs:pfa_attrs_opengl]; 42 | 43 | return ((void *)newwin); 44 | } 45 | 46 | 47 | int mlx_opengl_swap_buffers(mlx_win_list_t *win_ptr) 48 | { 49 | [(id)(win_ptr->winid) flushGLContext]; 50 | return (0); 51 | } 52 | 53 | int mlx_opengl_window_set_context(mlx_win_list_t *win_ptr) 54 | { 55 | [(id)(win_ptr->winid) selectGLContext]; 56 | return (0); 57 | } 58 | -------------------------------------------------------------------------------- /libft/ft_strnstr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strnstr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tblaase +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/10/07 17:07:07 by tblaase #+# #+# */ 9 | /* Updated: 2021/10/07 17:07:40 by tblaase ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strnstr(const char *haystack, const char *needle, size_t len) 16 | { 17 | size_t i; 18 | size_t j; 19 | 20 | i = 0; 21 | if (ft_strlen(needle) == 0) 22 | return ((char *)haystack); 23 | if (len == 0) 24 | return (NULL); 25 | while (haystack[i] != '\0' && i < len) 26 | { 27 | j = 0; 28 | while (needle[j] == haystack[i + j] && i + j < len) 29 | { 30 | if (needle[j + 1] == '\0') 31 | return ((char *)haystack + i); 32 | j++; 33 | } 34 | i++; 35 | } 36 | return (NULL); 37 | } 38 | -------------------------------------------------------------------------------- /textures/wall.xpm: -------------------------------------------------------------------------------- 1 | /* XPM */ 2 | static char *result[] = { 3 | /* columns rows colors chars-per-pixel */ 4 | "32 32 4 1 ", 5 | " c #5F5F5F", 6 | ". c #717171", 7 | "X c gray49", 8 | "o c None", 9 | /* pixels */ 10 | "oooooooooooooooooooooooooooooooo", 11 | "oooooooooooooooooooooooooooooooo", 12 | "oooooooooooooooooooooooooooooooo", 13 | "oooooooooooooooooooooooooooooooo", 14 | "oooooooooooooooooooooo ooooooooo", 15 | "oooooooooooooooooooo . oooooooo", 16 | "oooooooooooooooooo ... oooooooo", 17 | "oooooooooooooooo ...... ooooooo", 18 | "ooooooooooooo ....X... ooooooo", 19 | "oooooooooo ..XX..XXX... oooooo", 20 | "oooooooo ....XX...XX.... oooooo", 21 | "oooooooo ...XXXX.XXXX..... ooooo", 22 | "ooooooo ...XXXX.XXXX...... ooooo", 23 | "ooooooo ..XXXXXXXXXX...... ooooo", 24 | "oooooo ...XXXXXXXXX...X.. ooooo", 25 | "oooooo ..XXXXXXXXX...XX.. ooooo", 26 | "ooooo ...XXXX..XXX...X... ooooo", 27 | "ooooo ...XXX........X... X ooooo", 28 | "ooooo .................. X ooooo", 29 | "oooooo ...XX........... XX ooooo", 30 | "oooooo ..XX...X..... XX ooooo", 31 | "oooooo ..XX..XX... X ooooo", 32 | "ooooooo .....XX... X ooooo", 33 | "ooooooo ......... ooooo", 34 | "ooooooo ........ ooooo", 35 | "oooooooo ..... ooooo", 36 | "oooooooo .. ooooooo", 37 | "ooooooooooo ooooooooo", 38 | "oooooooooooooooo oooooooooooo", 39 | "oooooooooooooooooooooooooooooooo", 40 | "oooooooooooooooooooooooooooooooo", 41 | "oooooooooooooooooooooooooooooooo" 42 | }; 43 | -------------------------------------------------------------------------------- /libft/ft_strjoin.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strjoin.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tblaase +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/10/06 15:29:26 by tblaase #+# #+# */ 9 | /* Updated: 2021/10/07 19:00:13 by tblaase ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strjoin(char *s1, const char *s2) 16 | /* will append s2 to s1 17 | ** will return NULL if s2 is NULL */ 18 | { 19 | char *c; 20 | int i; 21 | int i2; 22 | 23 | if (!s2) 24 | return (NULL); 25 | i = 0; 26 | i2 = 0; 27 | c = malloc(ft_strlen(s1) + ft_strlen(s2) + 1); 28 | if (c == NULL) 29 | return (NULL); 30 | while (s1 && s1[i] != '\0') 31 | { 32 | c[i] = s1[i]; 33 | i++; 34 | } 35 | while (s2[i2] != '\0') 36 | c[i++] = s2[i2++]; 37 | c[i] = '\0'; 38 | free(s1); 39 | s1 = NULL; 40 | return (c); 41 | } 42 | -------------------------------------------------------------------------------- /key_hook.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* key_hook.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tblaase +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/10/07 16:51:04 by tblaase #+# #+# */ 9 | /* Updated: 2021/10/08 15:12:21 by tblaase ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "so_long.h" 14 | 15 | int ft_key_hook(int keycode, t_data *data) 16 | /* will cause the different actions of the valid key presses 17 | ** will cause you to win if you reached the exit */ 18 | { 19 | if (keycode == ESC) 20 | ft_exit(data); 21 | else if (keycode == W) 22 | ft_move(data, 'y', UP); 23 | else if (keycode == A) 24 | ft_move(data, 'x', LEFT); 25 | else if (keycode == S) 26 | ft_move(data, 'y', DOWN); 27 | else if (keycode == D) 28 | ft_move(data, 'x', RIGHT); 29 | if (data->map->map[data->p_y][data->p_x] == 'E') 30 | ft_win(data); 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /libft/ft_atoi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_atoi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tblaase +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/10/06 13:54:00 by tblaase #+# #+# */ 9 | /* Updated: 2021/10/07 18:59:51 by tblaase ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_atoi(const char *str) 16 | /* will find the first number inside a string and return this number as int */ 17 | { 18 | int c; 19 | int i; 20 | int x; 21 | 22 | i = 0; 23 | c = 1; 24 | x = 0; 25 | while (str[i] == ' ' || str[i] == '\t' || str[i] == '\r' || str[i] == '\n' 26 | || str[i] == '\v' || str[i] == '\f') 27 | i++; 28 | if (str[i] == '-' || str[i] == '+') 29 | { 30 | if (str[i] == '-') 31 | c = -1; 32 | i++; 33 | } 34 | while (str[i] >= 48 && str[i] <= 57) 35 | { 36 | x = x * 10 + (str[i] - 48); 37 | i++; 38 | } 39 | return (x * c); 40 | } 41 | -------------------------------------------------------------------------------- /textures/ship_up.xpm: -------------------------------------------------------------------------------- 1 | /* XPM */ 2 | static char *result[] = { 3 | /* columns rows colors chars-per-pixel */ 4 | "32 32 9 1 ", 5 | " c #29366F", 6 | ". c #257179", 7 | "X c #38B764", 8 | "o c #A7F070", 9 | "O c #3B5DC9", 10 | "+ c #41A6F6", 11 | "@ c #73EFF7", 12 | "# c #F4F4F4", 13 | "$ c None", 14 | /* pixels */ 15 | "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$", 16 | "$$$$$$$$$$$$###$$###$$$$$$$$$$$$", 17 | "$$$$$$$$$$$#X.#$$#Xo#$$$$$$$$$$$", 18 | "$$$$$$$$$$$#X.#$$#Xo#$$$$$$$$$$$", 19 | "$$$$$$$$$$#.X.#$$#XoX#$$$$$$$$$$", 20 | "$$$$$$$$$$#XX. ##.Xoo#$$$$$$$$$$", 21 | "$$$$$$$$$$#.X. ..XoX#$$$$$$$$$$", 22 | "$$$$$$$$$$#XX. O+.Xoo#$$$$$$$$$$", 23 | "$$$$$$$$$$#.X.O+@+XoX#$$$$$$$$$$", 24 | "$$$$$$$$$$#XX.++@@Xoo#$$$$$$$$$$", 25 | "$$$$$$$$$$#.#.++@@X#X#$$$$$$$$$$", 26 | "$$$$$$$$$$## .++@@X.##$$$$$$$$$$", 27 | "$$$$$$$$$$# .+O+@X..#$$$$$$$$$$", 28 | "$$$$$$$$$# ..O .+XX..#$$$$$$$$$", 29 | "$$$$$$$$$# .. ..XX..#$$$$$$$$$", 30 | "$$$$$$$$$# ... ..XXX.#$$$$$$$$$", 31 | "$$$$$$$$## ... ..XXX.##$$$$$$$$", 32 | "$$$$$$$#X#.. . ..X.XX#o#$$$$$$$", 33 | "$$$$$$#XX#.... ..XXXX#oo#$$$$$$", 34 | "$$$$$#XXX#.. . ..X.XX#ooo#$$$$$", 35 | "$$$$#XXXX#.. . ..X.XX#oooo#$$$$", 36 | "$$$#XXX.XX#... ..XXX#ooXooo#$$$", 37 | "$$#XXX.XXXX#... .XXX#ooooXooo#$$", 38 | "$$#XXXXX....#...XXX#XXXXooooo#$$", 39 | "$$#X.XX......#..XX#XXXXXXooXo#$$", 40 | "$$#.XX..XXXXXX####ooooooXXooX#$$", 41 | "$$#XX..########$$########XXoo#$$", 42 | "$$#X..#$# #$$$$$$#...#$#XXo#$$", 43 | "$$#..#$$$###$$$$$$$$###$$$#XX#$$", 44 | "$$#.#$$$$$$$$$$$$$$$$$$$$$$#X#$$", 45 | "$$##$$$$$$$$$$$$$$$$$$$$$$$$##$$", 46 | "$$#$$$$$$$$$$$$$$$$$$$$$$$$$$#$$" 47 | }; 48 | -------------------------------------------------------------------------------- /textures/ship_down.xpm: -------------------------------------------------------------------------------- 1 | /* XPM */ 2 | static char *result[] = { 3 | /* columns rows colors chars-per-pixel */ 4 | "32 32 9 1 ", 5 | " c #29366F", 6 | ". c #257179", 7 | "X c #38B764", 8 | "o c #A7F070", 9 | "O c #3B5DC9", 10 | "+ c #41A6F6", 11 | "@ c #73EFF7", 12 | "# c #F4F4F4", 13 | "$ c None", 14 | /* pixels */ 15 | "$$#$$$$$$$$$$$$$$$$$$$$$$$$$$#$$", 16 | "$$##$$$$$$$$$$$$$$$$$$$$$$$$##$$", 17 | "$$#X#$$$$$$$$$$$$$$$$$$$$$$#.#$$", 18 | "$$#XX#$$$###$$$$$$$$###$$$#..#$$", 19 | "$$#oXX#$#...#$$$$$$# #$#..X#$$", 20 | "$$#ooXX########$$########..XX#$$", 21 | "$$#XooXXoooooo####XXXXXX..XX.#$$", 22 | "$$#oXooXXXXXX#XX..#......XX.X#$$", 23 | "$$#oooooXXXX#XXX...#....XXXXX#$$", 24 | "$$#oooXoooo#XXX. ...#XXXX.XXX#$$", 25 | "$$$#oooXoo#XXX.. ...#XX.XXX#$$$", 26 | "$$$$#oooo#XX.X.. . ..#XXXX#$$$$", 27 | "$$$$$#ooo#XX.X.. . ..#XXX#$$$$$", 28 | "$$$$$$#oo#XXXX.. ....#XX#$$$$$$", 29 | "$$$$$$$#o#XX.X.. . ..#X#$$$$$$$", 30 | "$$$$$$$$##.XXX.. ... ##$$$$$$$$", 31 | "$$$$$$$$$#.XXX.. ... #$$$$$$$$$", 32 | "$$$$$$$$$#..XX.. .. #$$$$$$$$$", 33 | "$$$$$$$$$#..XX+. O.. #$$$$$$$$$", 34 | "$$$$$$$$$$#..X@+O+. #$$$$$$$$$$", 35 | "$$$$$$$$$$##.X@@++. ##$$$$$$$$$$", 36 | "$$$$$$$$$$#X#X@@++.#.#$$$$$$$$$$", 37 | "$$$$$$$$$$#ooX@@++.XX#$$$$$$$$$$", 38 | "$$$$$$$$$$#XoX+@+O.X.#$$$$$$$$$$", 39 | "$$$$$$$$$$#ooX.+O .XX#$$$$$$$$$$", 40 | "$$$$$$$$$$#XoX.. .X.#$$$$$$$$$$", 41 | "$$$$$$$$$$#ooX.## .XX#$$$$$$$$$$", 42 | "$$$$$$$$$$#XoX#$$#.X.#$$$$$$$$$$", 43 | "$$$$$$$$$$$#oX#$$#.X#$$$$$$$$$$$", 44 | "$$$$$$$$$$$#oX#$$#.X#$$$$$$$$$$$", 45 | "$$$$$$$$$$$$###$$###$$$$$$$$$$$$", 46 | "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" 47 | }; 48 | -------------------------------------------------------------------------------- /textures/ship_left.xpm: -------------------------------------------------------------------------------- 1 | /* XPM */ 2 | static char *result[] = { 3 | /* columns rows colors chars-per-pixel */ 4 | "32 32 9 1 ", 5 | " c #29366F", 6 | ". c #257179", 7 | "X c #38B764", 8 | "o c #A7F070", 9 | "O c #3B5DC9", 10 | "+ c #41A6F6", 11 | "@ c #73EFF7", 12 | "# c #F4F4F4", 13 | "$ c None", 14 | /* pixels */ 15 | "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$", 16 | "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$", 17 | "$$$$$$$$$$$$$$$$$$$$$$##########", 18 | "$$$$$$$$$$$$$$$$$$$$$#oooXooXX#$", 19 | "$$$$$$$$$$$$$$$$$$$$#oooXooXX#$$", 20 | "$$$$$$$$$$$$$$$$$$$#ooooooXX#$$$", 21 | "$$$$$$$$$$$$$$$$$$#oooXooXX#$$$$", 22 | "$$$$$$$$$$$$$$$$$#oooXooXX#$$$$$", 23 | "$$$$$$$$$$$$$$$$#ooooooXXo##$$$$", 24 | "$$$$$$$$$$$$$########ooXXo#.#$$$", 25 | "$$$$#########....XXXX#oXXo#.#$$$", 26 | "$$##XoXoXoX#...XXXXXXX#XXo#.#$$$", 27 | "$#oooooooo#..XXXX.X..XX#Xo##$$$$", 28 | "$#XXXXXXXXXXXXXXXXXXXXXX#o#$$$$$", 29 | "$####...+@@@@+........XXX##$$$$$", 30 | "$$$$$#.+@@@@+..........XX#$$$$$$", 31 | "$$$$$# O++++O ..#$$$$$$", 32 | "$#### O++++O ...##$$$$$", 33 | "$#......................#X#$$$$$", 34 | "$#XXXXXXXX# .... . ..#.X##$$$$", 35 | "$$##.X.X.X.# .......#..X# #$$$", 36 | "$$$$######### ....#X..X# #$$$", 37 | "$$$$$$$$$$$$$########XX..X# #$$$", 38 | "$$$$$$$$$$$$$$$$#XXXXXX..X##$$$$", 39 | "$$$$$$$$$$$$$$$$$#XXX.XX..#$$$$$", 40 | "$$$$$$$$$$$$$$$$$$#XXX.XX..#$$$$", 41 | "$$$$$$$$$$$$$$$$$$$#XXXXXX..#$$$", 42 | "$$$$$$$$$$$$$$$$$$$$#XXX.XX..#$$", 43 | "$$$$$$$$$$$$$$$$$$$$$#XXX.XX..#$", 44 | "$$$$$$$$$$$$$$$$$$$$$$##########", 45 | "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$", 46 | "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" 47 | }; 48 | -------------------------------------------------------------------------------- /textures/ship_right.xpm: -------------------------------------------------------------------------------- 1 | /* XPM */ 2 | static char *result[] = { 3 | /* columns rows colors chars-per-pixel */ 4 | "32 32 9 1 ", 5 | " c #29366F", 6 | ". c #257179", 7 | "X c #38B764", 8 | "o c #A7F070", 9 | "O c #3B5DC9", 10 | "+ c #41A6F6", 11 | "@ c #73EFF7", 12 | "# c #F4F4F4", 13 | "$ c None", 14 | /* pixels */ 15 | "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$", 16 | "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$", 17 | "##########$$$$$$$$$$$$$$$$$$$$$$", 18 | "$#..XX.XXX#$$$$$$$$$$$$$$$$$$$$$", 19 | "$$#..XX.XXX#$$$$$$$$$$$$$$$$$$$$", 20 | "$$$#..XXXXXX#$$$$$$$$$$$$$$$$$$$", 21 | "$$$$#..XX.XXX#$$$$$$$$$$$$$$$$$$", 22 | "$$$$$#..XX.XXX#$$$$$$$$$$$$$$$$$", 23 | "$$$$##X..XXXXXX#$$$$$$$$$$$$$$$$", 24 | "$$$# #X..XX########$$$$$$$$$$$$$", 25 | "$$$# #X..X#.... #########$$$$", 26 | "$$$# #X..#....... #.X.X.X.##$$", 27 | "$$$$##X.#.. . .... #XXXXXXXX#$", 28 | "$$$$$#X#......................#$", 29 | "$$$$$##... O++++O ####$", 30 | "$$$$$$#.. O++++O #$$$$$", 31 | "$$$$$$#XX..........+@@@@+.#$$$$$", 32 | "$$$$$##XXX........+@@@@+...####$", 33 | "$$$$$#o#XXXXXXXXXXXXXXXXXXXXXX#$", 34 | "$$$$##oX#XX..X.XXXX..#oooooooo#$", 35 | "$$$#.#oXX#XXXXXXX...#XoXoXoX##$$", 36 | "$$$#.#oXXo#XXXX....#########$$$$", 37 | "$$$#.#oXXoo########$$$$$$$$$$$$$", 38 | "$$$$##oXXoooooo#$$$$$$$$$$$$$$$$", 39 | "$$$$$#XXooXooo#$$$$$$$$$$$$$$$$$", 40 | "$$$$#XXooXooo#$$$$$$$$$$$$$$$$$$", 41 | "$$$#XXoooooo#$$$$$$$$$$$$$$$$$$$", 42 | "$$#XXooXooo#$$$$$$$$$$$$$$$$$$$$", 43 | "$#XXooXooo#$$$$$$$$$$$$$$$$$$$$$", 44 | "##########$$$$$$$$$$$$$$$$$$$$$$", 45 | "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$", 46 | "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" 47 | }; 48 | -------------------------------------------------------------------------------- /libft/ft_count_lines.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_count_lines.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tblaase +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/10/06 15:25:07 by tblaase #+# #+# */ 9 | /* Updated: 2022/02/08 17:19:29 by tblaase ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | #include 15 | 16 | void exit_error(void) 17 | { 18 | printf("Error\nwrong map dimensions\n"); 19 | exit(EXIT_FAILURE); 20 | } 21 | 22 | int ft_count_lines(int fd, int x, int img_w) 23 | /* returns how many lines the file of fd contains */ 24 | { 25 | char *line; 26 | int linecount; 27 | int i; 28 | 29 | i = 0; 30 | linecount = 1; 31 | while (1) 32 | { 33 | line = get_next_line(fd); 34 | if (line == NULL) 35 | break ; 36 | if ((int)ft_strlen(line) < x / img_w 37 | || (ft_strlen(line) == 1 && *line != '\n')) 38 | { 39 | free(line); 40 | exit_error(); 41 | } 42 | else 43 | { 44 | free(line); 45 | linecount++; 46 | } 47 | } 48 | return (linecount); 49 | } 50 | -------------------------------------------------------------------------------- /mlx/mlx_new_window.h: -------------------------------------------------------------------------------- 1 | // 2 | // mlx_int.h for minilibx 3 | // 4 | // ol@staff.42.fr 5 | // 6 | // include opengl needed before mlx_int.h 7 | // 8 | 9 | #import 10 | #import "mlx_int.h" 11 | 12 | @interface NSWindowEvent : NSWindow 13 | { 14 | func_t event_funct[MAX_EVENT]; 15 | void *(event_param[MAX_EVENT]); 16 | int keyrepeat; 17 | int keyflag; 18 | int size_x; 19 | int size_y; 20 | } 21 | - (NSWindowEvent *) initWithContentRect:(NSRect)rect styleMask:(NSUInteger)winstyle backing:(NSBackingStoreType)bck defer:(BOOL) dfr; 22 | - (void) setEvent:(int)event andFunc:(func_t)func andParam:(void *)param; 23 | - (void) setKeyRepeat:(int)mode; 24 | - (void) exposeNotification:(NSNotification *)note; 25 | - (void) closeNotification:(NSNotification *)note; 26 | @end 27 | 28 | 29 | @interface MlxWin : NSOpenGLView 30 | { 31 | NSWindowEvent *win; 32 | NSOpenGLContext *ctx; 33 | glsl_info_t glsl; 34 | int openglwin; 35 | 36 | int size_x; 37 | int size_y; 38 | 39 | int pixel_nb; 40 | GLuint pixel_vbuffer; 41 | GLuint pixel_texture; 42 | unsigned int *pixtexbuff; 43 | } 44 | 45 | - (id) initWithRect: (NSRect)rect andTitle: (NSString *)title pfaAttrs: (NSOpenGLPixelFormatAttribute *)attrs; 46 | - (void) selectGLContext; 47 | - (void) flushGLContext; 48 | - (void) pixelPutColor: (int)color X:(int)x Y:(int)y; 49 | - (void) mlx_gl_draw; 50 | - (void) mlx_gl_draw_img:(mlx_img_list_t *)img andCtx:(mlx_img_ctx_t *)imgctx andX:(int)x andY:(int)y; 51 | - (void) mlx_gl_draw_font:(mlx_img_list_t *)img andCtx:(mlx_img_ctx_t *)imgctx andX:(int)x andY:(int)y andColor:(int)color glyphX:(int)gx glyphY:(int)gy; 52 | - (NSOpenGLContext *) ctx; 53 | - (NSWindowEvent *) win; 54 | - (void) setEvent:(int)event andFunc:(func_t)func andParam:(void *)param; 55 | - (void) setKeyRepeat:(int)mode; 56 | - (void) ctxNeedsUpdate; 57 | @end 58 | -------------------------------------------------------------------------------- /libft/Makefile: -------------------------------------------------------------------------------- 1 | # **************************************************************************** # 2 | # # 3 | # ::: :::::::: # 4 | # Makefile :+: :+: :+: # 5 | # +:+ +:+ +:+ # 6 | # By: tblaase +#+ +:+ +#+ # 7 | # +#+#+#+#+#+ +#+ # 8 | # Created: 2021/10/06 12:53:31 by tblaase #+# #+# # 9 | # Updated: 2022/02/08 16:56:27 by tblaase ### ########.fr # 10 | # # 11 | # **************************************************************************** # 12 | 13 | NAME = libft.a 14 | 15 | CC = gcc 16 | 17 | CFLAGS = -Wall -Wextra -Werror -g 18 | 19 | Y = "\033[33m" 20 | R = "\033[31m" 21 | G = "\033[32m" 22 | B = "\033[34m" 23 | X = "\033[0m" 24 | UP = "\033[A" 25 | CUT = "\033[K" 26 | 27 | CFILES = \ 28 | ft_atoi.c\ 29 | ft_calloc.c\ 30 | ft_count_c.c\ 31 | ft_count_lines.c\ 32 | ft_line_length.c\ 33 | ft_strchr.c\ 34 | ft_strjoin.c\ 35 | ft_strlen.c\ 36 | ft_strnstr.c\ 37 | get_next_line_utils.c\ 38 | get_next_line.c 39 | 40 | OBJECTS = $(CFILES:.c=.o) 41 | 42 | %.o : %.c 43 | @$(CC) $(CFLAGS) -c -o $@ $< 44 | @printf $(UP)$(CUT) 45 | @echo $(Y)Compiling [$<]...$(X) 46 | 47 | all: $(NAME) 48 | 49 | $(NAME): $(OBJECTS) 50 | @printf $(UP)$(CUT) 51 | @echo $(Y)Compiling [$(NAME)]...$(X) 52 | @ar -rcs $(NAME) $(OBJECTS) 53 | @ranlib $(NAME) 54 | @echo $(G)Finished [$(NAME)]$(X) 55 | @echo 56 | 57 | clean: 58 | @rm -f $(OBJECTS) 59 | @echo $(R)Removed [$(OBJECTS)]$(X) 60 | 61 | fclean: clean 62 | @rm -f $(NAME) 63 | @echo $(R)Removed [$(NAME)]$(X) 64 | 65 | re: fclean all 66 | 67 | .PHONY: all clean fclean re 68 | -------------------------------------------------------------------------------- /textures/collectable.xpm: -------------------------------------------------------------------------------- 1 | /* XPM */ 2 | static char *result[] = { 3 | /* columns rows colors chars-per-pixel */ 4 | "32 32 32 1 ", 5 | " c #012A9D", 6 | ". c #117AFE", 7 | "X c #0083FF", 8 | "o c #0889FF", 9 | "O c #0092FF", 10 | "+ c #0F9BFF", 11 | "@ c #0FA4FF", 12 | "# c #21AFFE", 13 | "$ c #34B7FF", 14 | "% c #5BA5FF", 15 | "& c #5AB9FF", 16 | "* c #63B1FF", 17 | "= c #76BBFF", 18 | "- c #3DC0FF", 19 | "; c #57C8FF", 20 | ": c #62D2FF", 21 | "> c #75DAFE", 22 | ", c #81CDFF", 23 | "< c #91CCFD", 24 | "1 c #8FD8FF", 25 | "2 c #8FDEFF", 26 | "3 c #95DCF1", 27 | "4 c #ADD7FF", 28 | "5 c #84E0FF", 29 | "6 c #93E3FE", 30 | "7 c #A5E8FE", 31 | "8 c #B9E4FF", 32 | "9 c #B3EDFE", 33 | "0 c #BBEBFE", 34 | "q c #CAF2FE", 35 | "w c #DCF4FB", 36 | "e c None", 37 | /* pixels */ 38 | "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", 39 | "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", 40 | "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", 41 | "eeeeeeeeeee eeeeeeeeeee", 42 | "eeeeeeee wwwwwwwwww eeeeeeee", 43 | "eeeeeee wwwwwwqqqqqqqqqq eeeeeee", 44 | "eeeeee wqqqqqq99999909099 eeeeee", 45 | "eeeee 99999977777777779999 eeeee", 46 | "eeee 0776666666667777777622 eeee", 47 | "eee 906>>>55566666255>>:::,: eee", 48 | "ee 3779>>>55>>>:::::;;---,$:> ee", 49 | "e 366660::::;;;-------$$,@&::> e", 50 | "e 555556wq81,----$$&,,,==#$::: e", 51 | "e 6555>779qqqq084<<&@@+X=#-::: e", 52 | "e 965>>966799777=@@@@+oo+--::5 e", 53 | "e w95>6666666666=@@@@OOOO&-:>3 e", 54 | "ee w7576655>5666=@@@OOOOO@-:3 ee", 55 | "eee w955>:>>>566=@@+OOOO++&3 eee", 56 | "eeee q7>::>>:>56=@+++++++@& eeee", 57 | "eeeee ,=,126>>>5<@@+*=*%%% eeeee", 58 | "eeeeee #@@#&,,174*=%X...o eeeeee", 59 | "eeeeeee ##$-$#@#%......O eeeeeee", 60 | "eeeeeeee #----##%....X+ eeeeeeee", 61 | "eeeeeeeee -;:;-$%Xooo@ eeeeeeeee", 62 | "eeeeeeeeee :::;-%OOO@ eeeeeeeeee", 63 | "eeeeeeeeeee :::;%++@ eeeeeeeeeee", 64 | "eeeeeeeeeeee >>:*@@ eeeeeeeeeeee", 65 | "eeeeeeeeeeeee >:&@ eeeeeeeeeeeee", 66 | "eeeeeeeeeeeeee >& eeeeeeeeeeeeee", 67 | "eeeeeeeeeeeeeee eeeeeeeeeeeeeee", 68 | "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", 69 | "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" 70 | }; 71 | -------------------------------------------------------------------------------- /put_imgs.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* put_imgs.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tblaase +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/10/06 16:04:13 by tblaase #+# #+# */ 9 | /* Updated: 2021/10/08 15:22:07 by tblaase ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "so_long.h" 14 | 15 | void ft_put_background(t_data *data) 16 | /* will fill the whole screen with background */ 17 | { 18 | int y; 19 | int x; 20 | 21 | y = 0; 22 | while (y < data->size_y) 23 | { 24 | x = 0; 25 | while (x < data->size_x) 26 | { 27 | mlx_put_image_to_window(data->mlx, data->win, 28 | data->img->background, x, y); 29 | x += IMG_W; 30 | } 31 | y += IMG_H; 32 | } 33 | } 34 | 35 | void ft_put_object(t_data *data, char *relative_path) 36 | /* puts the given object to the current position */ 37 | { 38 | int img_width; 39 | int img_height; 40 | 41 | data->map->object = mlx_xpm_file_to_image(data->mlx, relative_path, 42 | &img_width, &img_height); 43 | mlx_put_image_to_window(data->mlx, data->win, data->map->object, 44 | (data->map->x * IMG_W), (data->map->y * IMG_H)); 45 | } 46 | 47 | void ft_put_player(t_data *data) 48 | /* puts the player in the starting position */ 49 | { 50 | data->p_x = data->map->x; 51 | data->p_y = data->map->y; 52 | mlx_put_image_to_window(data->mlx, data->win, data->img->player_up, 53 | (data->p_x * IMG_W), (data->p_y * IMG_H)); 54 | } 55 | -------------------------------------------------------------------------------- /mlx/Makefile: -------------------------------------------------------------------------------- 1 | # **************************************************************************** # 2 | # # 3 | # ::: :::::::: # 4 | # Makefile :+: :+: :+: # 5 | # +:+ +:+ +:+ # 6 | # By: tblaase +#+ +:+ +#+ # 7 | # +#+#+#+#+#+ +#+ # 8 | # Created: 2021/12/08 17:20:08 by tblaase #+# #+# # 9 | # Updated: 2021/12/08 17:33:46 by tblaase ### ########.fr # 10 | # # 11 | # **************************************************************************** # 12 | 13 | 14 | 15 | NAME=libmlx.a 16 | SRC= mlx_shaders.c mlx_new_window.m mlx_init_loop.m mlx_new_image.m mlx_xpm.c mlx_int_str_to_wordtab.c 17 | SRC+= mlx_png.c mlx_mouse.m 18 | OBJ1=$(SRC:.c=.o) 19 | OBJ=$(OBJ1:.m=.o) 20 | CFLAGS+=-O2 21 | 22 | # colors for printing 23 | Y = "\033[33m" 24 | R = "\033[31m" 25 | G = "\033[32m" 26 | B = "\033[34m" 27 | X = "\033[0m" 28 | UP = "\033[A" 29 | CUT = "\033[K" 30 | 31 | # changed the following 8 lines to silence all the warnings created 32 | %.o : %.c 33 | @cc -O2 -w -DSTRINGPUTX11 -c -o $@ $< 34 | @printf $(UP)$(CUT) 35 | @echo $(Y)Compiling [$<]...$(X) 36 | %.o : %.m 37 | @cc -O2 -w -DSTRINGPUTX11 -c -o $@ $< 38 | @printf $(UP)$(CUT) 39 | @echo $(Y)Compiling [$<]...$(X) 40 | 41 | # add to match string put with X11 in size and position 42 | CFLAGS+= -DSTRINGPUTX11 43 | 44 | all: $(NAME) 45 | 46 | $(NAME): $(OBJ) 47 | @printf $(UP)$(CUT) 48 | @echo $(Y)Compiling [$(NAME)]...$(X) 49 | @ar -r $(NAME) $(OBJ) 50 | @ranlib $(NAME) 51 | @echo $(G)Finished [$(NAME)]$(X) 52 | 53 | clean: 54 | @rm -f $(OBJ) 55 | @echo $(R)Removed [$(OBJ)]$(X) 56 | 57 | fclean: clean 58 | @rm -f $(NAME) *~ 59 | @rm -f mlx_app 60 | @echo $(R)Removed [$(NAME)]$(X) 61 | 62 | re: clean all 63 | -------------------------------------------------------------------------------- /libft/libft.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* libft.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tblaase +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/10/06 12:53:29 by tblaase #+# #+# */ 9 | /* Updated: 2022/02/08 17:34:43 by tblaase ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef LIBFT_H 14 | # define LIBFT_H 15 | 16 | # include 17 | # include 18 | # include 19 | 20 | int ft_atoi(const char *str); 21 | void *ft_calloc(size_t nelem, size_t elsize); 22 | int ft_count_c(char *s, char c); 23 | int ft_count_lines(int fd, int line_length, int img_w); 24 | int ft_line_length(int fd); 25 | char *ft_strchr(const char *s, int c); 26 | char *ft_strjoin(char *s1, const char *s2); 27 | size_t ft_strlen(const char *s); 28 | char *ft_strnstr(const char *haystack, const char *needle, size_t len); 29 | 30 | // get_next_line 31 | //this part lets you controll the BUFFER_SIZE without adding it when compiling 32 | //this is really usefull when used inside libft 33 | # ifndef BUFFER_SIZE 34 | # define BUFFER_SIZE 1 35 | # endif 36 | 37 | void ft_free_gnl(char **p); 38 | int ft_strlen_gnl(char *s); 39 | int ft_strchr_gnl(char *s, int c, int flag); 40 | char *ft_strnjoin_gnl(char *s1, char *s2, int n); 41 | char *ft_strndup_gnl(char *str, int n); 42 | void *ft_calloc_gnl(size_t nelem, size_t elsize); 43 | char *ft_update_nl_gnl(char **next_line, int position); 44 | char *ft_output_gnl(char **next_line, int position, int bytes); 45 | char *get_next_line(int fd); 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /maps/map3.ber: -------------------------------------------------------------------------------- 1 | 111111111111111111111111111111111111111111111111111111111111 2 | 1E00000000000000000000000000000000000000000000000000000000E1 3 | 100000000000000000000000000000000000000000000000000000000001 4 | 100000000000000000000000000000000000000000000000000000000001 5 | 100000000C00000000000000000000000000000000000000000000000001 6 | 100000000000000000000000000000000000000000000000000000000001 7 | 100000000000000000000000000000000000000000000000000000000001 8 | 100000000000000000000000000000000000000000000000000000000001 9 | 100000000000000000000000000000000000000000000000000000000001 10 | 100000000000000000000000000000000000000000000000000000000001 11 | 100000000000000000000000000000000000000000000000000000000001 12 | 10000000000000000000000000000000000C000000000000000000000001 13 | 100000000000000000000000000000000000000000000000000000000001 14 | 100000000000000000000000000000000000000010000000000000000001 15 | 10000000000000000000C000000000000000000010000000000000000001 16 | 100000000000000000000000000000000000000010000000000000000001 17 | 100000000000000000000000000000000000000010000000000000000001 18 | 100000000000011111000000000000000000000010000000000000000001 19 | 100000000000000000000000000000000000000010000000000000000001 20 | 1000000000000000000000000000000000000000C0000000000000000001 21 | 100000000000000000000000000000000000000000000000000000000001 22 | 100000000000000000000000000000000000000000000000000000000001 23 | 100000000000000C00000000000000000000000000000000000000000001 24 | 100000000000000000000000000000000000000000000000000000000001 25 | 100000000000000000000000000000000000000000000000000000000001 26 | 1C0000000000000000000000000000000000000000000000000000000001 27 | 1C0000000000000000000000000000000000000000000000000000000001 28 | 1000000000000000000000000000000C0000000000000000000000000001 29 | 1000000000000000000000000000000C0000000000000000000000000001 30 | 1000000000000000000000000000000C0000000000000000000000000001 31 | 100000000000000000000000000000000000000000000000000000000001 32 | 100000000000000000000000000000000000000000000000000000000001 33 | 1P00000000000000000000000000000000000000000000000000000C00E1 34 | 111111111111111111111111111111111111111111111111111111111111 35 | -------------------------------------------------------------------------------- /maps/map4.ber: -------------------------------------------------------------------------------- 1 | 111111111111111111111111111111111111111111111111111111111111 2 | 1ECCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCE1 3 | 1CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC1 4 | 1CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC1 5 | 1CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC1 6 | 1CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC1 7 | 1CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC1 8 | 1CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC1 9 | 1CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC1 10 | 1CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC1 11 | 1CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC1 12 | 1CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC1 13 | 1CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC1 14 | 1CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC1CCCCCCCCCCCCCCCCCC1 15 | 1CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC1CCCCCCCCCCCCCCCCCC1 16 | 1CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC1CCCCCCCCCCCCCCCCCC1 17 | 1CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC1CCCCCCCCCCCCCCCCCC1 18 | 1CCCCCCCCCCCC11111CCCCCCCCCCCCCCCCCCCCCC1CCCCCCCCCCCCCCCCCC1 19 | 1CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC1CCCCCCCCCCCCCCCCCC1 20 | 1CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC1 21 | 1CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC1 22 | 1CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC1 23 | 1CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC1 24 | 1CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC1 25 | 1CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC1 26 | 1CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC1 27 | 1CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC1 28 | 1CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC1 29 | 1CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC1 30 | 1CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC1 31 | 1CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC1 32 | 1CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC1 33 | 1PCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCE1 34 | 111111111111111111111111111111111111111111111111111111111111 35 | -------------------------------------------------------------------------------- /mlx/mlx_int_str_to_wordtab.c: -------------------------------------------------------------------------------- 1 | // 2 | // str 2 wordtab & co 3 | // by ol 4 | 5 | 6 | #include 7 | #include 8 | 9 | int mlx_int_str_str(char *str,char *find,int len) 10 | { 11 | int len_f; 12 | int pos; 13 | char *s; 14 | char *f; 15 | 16 | len_f = strlen(find); 17 | if (len_f>len) 18 | return (-1); 19 | pos = 0; 20 | while (*(str+len_f-1)) 21 | { 22 | s = str; 23 | f = find; 24 | while (*(f++) == *(s++)) 25 | if (!*f) 26 | return (pos); 27 | str ++; 28 | pos ++; 29 | } 30 | return (-1); 31 | } 32 | 33 | 34 | 35 | int mlx_int_str_str_cote(char *str,char *find,int len) 36 | { 37 | int len_f; 38 | int pos; 39 | char *s; 40 | char *f; 41 | int cote; 42 | 43 | len_f = strlen(find); 44 | if (len_f>len) 45 | return (-1); 46 | cote = 0; 47 | pos = 0; 48 | while (*(str+len_f-1)) 49 | { 50 | if (*str=='"') 51 | cote = 1-cote; 52 | if (!cote) 53 | { 54 | s = str; 55 | f = find; 56 | while (*(f++) == *(s++)) 57 | if (!*f) 58 | return (pos); 59 | } 60 | str ++; 61 | pos ++; 62 | } 63 | return (-1); 64 | } 65 | 66 | 67 | char **mlx_int_str_to_wordtab(char *str) 68 | { 69 | char **tab; 70 | int pos; 71 | int nb_word; 72 | int len; 73 | 74 | len = strlen(str); 75 | nb_word = 0; 76 | pos = 0; 77 | while (pos +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/10/06 11:17:34 by tblaase #+# #+# */ 9 | /* Updated: 2021/10/08 15:28:12 by tblaase ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "so_long.h" 14 | 15 | int ft_exit(t_data *data) 16 | /* will show a message in the terminal and exit the process */ 17 | { 18 | mlx_destroy_window(data->mlx, data->win); 19 | printf("You gave up :(\n"); 20 | printf("Is the map to hard for you?\n"); 21 | exit(EXIT_SUCCESS); 22 | } 23 | 24 | static int ft_render_next_frame(t_data *data) 25 | /* checks for keyboard or mouse input */ 26 | { 27 | ft_put_background(data); 28 | ft_create_map(data); 29 | mlx_hook(data->win, 17, 1L << 2, ft_exit, data); 30 | mlx_key_hook(data->win, ft_key_hook, data); 31 | return (0); 32 | } 33 | 34 | int main(int argc, char **argv) 35 | /* runs the mlx loop */ 36 | { 37 | t_data data; 38 | t_map map; 39 | 40 | ft_window_size(&data, argv); 41 | map.map = ft_calloc(data.size_y + 1, sizeof(char *)); 42 | if (!map.map) 43 | { 44 | perror("Error\ncalloc failed\n"); 45 | exit(EXIT_FAILURE); 46 | } 47 | ft_init(&data, &map); 48 | ft_parse_input(&data, argv, argc); 49 | data.mlx = mlx_init(); 50 | if (!data.mlx) 51 | { 52 | perror("Error\nprogramm init failed\n"); 53 | exit(EXIT_FAILURE); 54 | } 55 | data.win = mlx_new_window(data.mlx, data.size_x, 56 | data.size_y, "./so_long"); 57 | ft_render_next_frame(&data); 58 | mlx_loop(data.mlx); 59 | perror("Error\nloop failed\n"); 60 | exit(EXIT_FAILURE); 61 | } 62 | -------------------------------------------------------------------------------- /map.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* map.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tblaase +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/10/06 12:32:53 by tblaase #+# #+# */ 9 | /* Updated: 2021/12/08 16:18:08 by tblaase ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "so_long.h" 14 | 15 | void ft_window_size(t_data *data, char **argv) 16 | /* will find the window size and check for some errors */ 17 | { 18 | int fd; 19 | 20 | fd = open(argv[1], O_RDONLY); 21 | if (fd < 0) 22 | { 23 | perror("Error\nInvalid map_path/map\n"); 24 | exit(EXIT_FAILURE); 25 | } 26 | if (ft_strnstr(argv[1], ".ber", ft_strlen(argv[1])) == NULL) 27 | { 28 | printf("Error\nmap has to be .ber\n"); 29 | exit(EXIT_FAILURE); 30 | } 31 | data->size_x = (ft_line_length(fd) * IMG_W); 32 | data->size_y = (ft_count_lines(fd, data->size_x, IMG_W) * IMG_H); 33 | } 34 | 35 | void ft_create_map(t_data *data) 36 | /* will fill the map with the corresponding images */ 37 | { 38 | data->map->x = 0; 39 | data->map->y = 0; 40 | while (data->map->y < (data->size_y / IMG_H)) 41 | { 42 | if (data->map->map[data->map->y][data->map->x] == 'P') 43 | ft_put_player(data); 44 | else if (data->map->map[data->map->y][data->map->x] == '1') 45 | ft_put_object(data, "./textures/wall.xpm"); 46 | else if (data->map->map[data->map->y][data->map->x] == 'C') 47 | ft_put_object(data, "./textures/collectable.xpm"); 48 | else if (data->map->map[data->map->y][data->map->x] == 'E') 49 | ft_put_object(data, "./textures/exit.xpm"); 50 | if (data->map->x < (data->size_x / IMG_W)) 51 | data->map->x++; 52 | else 53 | { 54 | data->map->y++; 55 | data->map->x = 0; 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /init.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* init.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tblaase +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/10/07 19:07:33 by tblaase #+# #+# */ 9 | /* Updated: 2021/10/08 15:11:03 by tblaase ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "so_long.h" 14 | 15 | static void ft_init_help(t_data *data) 16 | /* only exists because of the 25 line limit */ 17 | { 18 | char *relative_path; 19 | int img_width; 20 | int img_height; 21 | 22 | relative_path = "./textures/ship_right.xpm"; 23 | data->img->player_right = mlx_xpm_file_to_image(data->mlx, relative_path, 24 | &img_width, &img_height); 25 | relative_path = "./textures/background.xpm"; 26 | data->img->background = mlx_xpm_file_to_image(data->mlx, relative_path, 27 | &img_width, &img_height); 28 | } 29 | 30 | void ft_init(t_data *data, t_map *map) 31 | /* initialises most of my used data inside the structs */ 32 | { 33 | char *relative_path; 34 | int img_width; 35 | int img_height; 36 | t_img *img; 37 | 38 | data->map = map; 39 | img = malloc(sizeof(t_img)); 40 | if (!img) 41 | { 42 | perror("Error\nmalloc failed\n"); 43 | exit(EXIT_FAILURE); 44 | } 45 | data->img = img; 46 | relative_path = "textures/ship_up.xpm"; 47 | data->img->player_up = mlx_xpm_file_to_image(data->mlx, relative_path, 48 | &img_width, &img_height); 49 | relative_path = "./textures/ship_left.xpm"; 50 | data->img->player_left = mlx_xpm_file_to_image(data->mlx, relative_path, 51 | &img_width, &img_height); 52 | relative_path = "./textures/ship_down.xpm"; 53 | data->img->player_down = mlx_xpm_file_to_image(data->mlx, relative_path, 54 | &img_width, &img_height); 55 | ft_init_help(data); 56 | data->counter = 0; 57 | data->collected = 0; 58 | } 59 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # **************************************************************************** # 2 | # # 3 | # ::: :::::::: # 4 | # Makefile :+: :+: :+: # 5 | # +:+ +:+ +:+ # 6 | # By: tblaase +#+ +:+ +#+ # 7 | # +#+#+#+#+#+ +#+ # 8 | # Created: 2021/10/06 10:57:58 by tblaase #+# #+# # 9 | # Updated: 2022/02/08 17:34:11 by tblaase ### ########.fr # 10 | # # 11 | # **************************************************************************** # 12 | 13 | NAME = so_long 14 | 15 | CC = gcc 16 | 17 | CFLAGS = -Wall -Wextra -Werror -g 18 | 19 | MLX_PATH = mlx/ 20 | 21 | MLX_LIB = $(MLX_PATH)libmlx.a 22 | 23 | MLX_FLAGS = -Lmlx -lmlx -framework OpenGL -framework AppKit 24 | 25 | LIBFT_PATH = libft/ 26 | 27 | LIBFT_LIB = $(LIBFT_PATH)libft.a 28 | 29 | Y = "\033[33m" 30 | R = "\033[31m" 31 | G = "\033[32m" 32 | B = "\033[34m" 33 | X = "\033[0m" 34 | UP = "\033[A" 35 | CUT = "\033[K" 36 | 37 | CFILES = \ 38 | init.c\ 39 | key_hook.c\ 40 | map.c\ 41 | move.c\ 42 | parse_input.c\ 43 | put_imgs.c\ 44 | so_long.c\ 45 | win.c\ 46 | 47 | OBJECTS = $(CFILES:.c=.o) 48 | 49 | all: subsystems $(NAME) 50 | 51 | %.o : %.c 52 | @echo $(Y)Compiling [$<]...$(X) 53 | @$(CC) $(CFLAGS) -Imlx -c -o $@ $< 54 | @printf $(UP)$(CUT) 55 | 56 | subsystems: 57 | @echo $(B) 58 | make -C $(MLX_PATH) all 59 | @echo $(B) 60 | make -C $(LIBFT_PATH) all 61 | 62 | $(NAME): $(OBJECTS) 63 | @echo $(Y)Compiling [$(CFILES)]...$(X) 64 | @echo $(G)Finished [$(CFILES)]$(X) 65 | @echo 66 | @echo $(Y)Compiling [$(NAME)]...$(X) 67 | @$(CC) $(CFLAGS) $(MLX_FLAGS) $(OBJECTS) $(MLX_LIB) $(LIBFT_LIB) -o $(NAME) 68 | @echo $(G)Finished [$(NAME)]$(X) 69 | 70 | clean: 71 | @make -C $(MLX_PATH) clean 72 | @make -C $(LIBFT_PATH) clean 73 | @rm -f $(OBJECTS) 74 | @echo $(R)Removed [$(OBJECTS)]$(X) 75 | 76 | fclean: clean 77 | @make -C $(MLX_PATH) fclean 78 | @make -C $(LIBFT_PATH) fclean 79 | @rm -f $(NAME) 80 | @echo $(R)Removed [$(NAME)]$(X) 81 | 82 | re: fclean all 83 | 84 | norm: 85 | norminette libft init.c key_hook.c map.c move.c parse_input.c put_imgs.c so_long.c so_long.h win.c 86 | 87 | .PHONY: all clean fclean re norm 88 | -------------------------------------------------------------------------------- /mlx/mlx_int.h: -------------------------------------------------------------------------------- 1 | // 2 | // mlx_int.h for minilibx 3 | // 4 | // ol@staff.42.fr 5 | // 6 | // include opengl needed before mlx_int.h 7 | // 8 | 9 | 10 | #define MAX_EVENT 32 11 | #define MAX_PIXEL_NB 200000 12 | #define UNIQ_BPP 4 13 | 14 | #define FONT_WIDTH 10 15 | #define FONT_HEIGHT 20 16 | 17 | 18 | typedef int (*func_t)(); 19 | 20 | /* structs */ 21 | 22 | typedef struct glsl_info_s 23 | { 24 | GLuint pixel_vshader; 25 | GLuint pixel_fshader; 26 | GLuint pixel_program; 27 | GLint loc_pixel_position; 28 | GLint loc_pixel_texture; 29 | GLint loc_pixel_winhalfsize; 30 | 31 | GLuint image_vshader; 32 | GLuint image_fshader; 33 | GLuint image_program; 34 | GLint loc_image_position; 35 | GLint loc_image_winhalfsize; 36 | GLint loc_image_texture; 37 | GLint loc_image_pos; 38 | GLint loc_image_size; 39 | 40 | GLuint font_vshader; 41 | GLuint font_fshader; 42 | GLuint font_program; 43 | GLint loc_font_position; 44 | GLint loc_font_winhalfsize; 45 | GLint loc_font_texture; 46 | GLint loc_font_color; 47 | GLint loc_font_posinwin; 48 | GLint loc_font_posinatlas; 49 | GLint loc_font_atlassize; 50 | } glsl_info_t; 51 | 52 | 53 | typedef struct mlx_img_list_s 54 | { 55 | int width; 56 | int height; 57 | char *buffer; 58 | GLfloat vertexes[8]; 59 | struct mlx_img_list_s *next; 60 | } mlx_img_list_t; 61 | 62 | 63 | typedef struct mlx_img_ctx_s 64 | { 65 | GLuint texture; 66 | GLuint vbuffer; 67 | mlx_img_list_t *img; 68 | struct mlx_img_ctx_s *next; 69 | } mlx_img_ctx_t; 70 | 71 | typedef struct mlx_win_list_s 72 | { 73 | void *winid; 74 | mlx_img_ctx_t *img_list; 75 | int nb_flush; 76 | int pixmgt; 77 | struct mlx_win_list_s *next; 78 | } mlx_win_list_t; 79 | 80 | 81 | typedef struct mlx_ptr_s 82 | { 83 | void *appid; 84 | mlx_win_list_t *win_list; 85 | mlx_img_list_t *img_list; 86 | void (*loop_hook)(void *); 87 | void *loop_hook_data; 88 | void *loop_timer; 89 | mlx_img_list_t *font; 90 | int main_loop_active; 91 | } mlx_ptr_t; 92 | 93 | // proto 94 | 95 | int mlx_shaders(glsl_info_t *glsl); 96 | char **mlx_int_str_to_wordtab(char *str); 97 | int mlx_int_str_str(char *str,char *find,int len); 98 | int mlx_int_str_str_cote(char *str,char *find,int len); 99 | int mlx_destroy_image(mlx_ptr_t *mlx_ptr, mlx_img_list_t *img_ptr); 100 | void *mlx_new_image(); 101 | void *mlx_xpm_to_image(mlx_ptr_t *xvar,char **xpm_data,int *width,int *height); 102 | int mlx_do_sync(mlx_ptr_t *mlx_ptr); 103 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # so_long 102/100 (October 2021) 2 | This is my solution for the so_long project of the core curriculum of 42 school.
3 | 4 | ## Explanation of the project 5 | 6 | This projects goal is to create your own 2D game where you have a map that can have different dimensions and contents.
7 | 8 | ### Requirements to run the game 9 | Only tested on **macOS Catalina (Version 10.15.7)**.
10 | For all other OS it is not guaranteed to work.
11 | OpenGL and AppKit is required to run it.
12 | If you want to run it on Linux [here](https://harm-smits.github.io/42docs/libs/minilibx/getting_started.html#compilation-on-linux) is a tutorial that might help and [here](https://github.com/42Paris/minilibx-linux) is the required version of miniLibX, this is **not tested** with my so_long.
13 | 14 | ### Map 15 | A valid map is structured as followed:
16 | - 1 player (`P`) 17 | - at least 1 exit (`E`) 18 | - at least 1 collectible (`C`)` 19 | - is recatangular 20 | - has a solid outside border out of walls (`1`) 21 | - all the empty spaces are filled with `0` 22 | - map has to be `.ber` 23 | - no other characters than: 24 | * `P` 25 | * `E` 26 | * `C` 27 | * `1` 28 | * `0` 29 | - i.e. `map1.ber`: 30 | - one new line after the map is valid, if there is anything else, it is not valid 31 | 32 | ``` 33 | 1111111111111 34 | 10010000000C1 35 | 1000011111001 36 | 1P0011E000011 37 | 1111111111111 38 | ``` 39 | 40 |
41 | 42 | ### During the game 43 | The player can only be moved by `WASD`.
44 | The player can not move into walls.
45 | The player can only exit if all collectibles are collected.
46 | The amount of movements is counted and displayed in the terminal.
47 | Every try to move even if it is invalid will be counted as one move.
48 | 49 | All the visualization is done with the help of the [MiniLibX library](https://github.com/tblaase/so_long/tree/master/mlx "https://github.com/tblaase/so_long/tree/master/mlx").
50 | 51 | ## How to run the game 52 | 53 | All of the commands should be run in the root of the directory.
54 | - compile with `make all` 55 | - run the game with `./so_long maps/map1.ber` 56 | 57 | This is how the game looks for the map1.ber.
58 |
59 | 60 | This is what will be written in the terminal during the game when you successfully win the game.
61 |
62 | 63 | This is what will be written in the terminal if you give up by closing the game by pressing `ESC` or clicking the red cross.
64 |
65 | -------------------------------------------------------------------------------- /so_long.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* so_long.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tblaase +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/10/06 12:42:26 by tblaase #+# #+# */ 9 | /* Updated: 2021/10/08 15:26:10 by tblaase ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef SO_LONG_H 14 | # define SO_LONG_H 15 | 16 | # include 17 | # include 18 | # include 19 | # include "libft/libft.h" 20 | 21 | /* ********** defines to make work easier ********** */ 22 | 23 | # define IMG_W 32 24 | # define IMG_H 32 25 | # define ESC 53 26 | # define W 13 27 | # define A 0 28 | # define S 1 29 | # define D 2 30 | # define UP -1 31 | # define DOWN 1 32 | # define LEFT -1 33 | # define RIGHT 1 34 | 35 | /* ***** struct to store the different images ***** */ 36 | 37 | typedef struct s_img 38 | { 39 | void *player_up; 40 | void *player_left; 41 | void *player_right; 42 | void *player_down; 43 | void *background; 44 | } t_img; 45 | 46 | /* ***** struct to create the map and keep track of the contents ***** */ 47 | 48 | typedef struct s_map 49 | { 50 | char **map; 51 | void *object; 52 | int x; 53 | int y; 54 | int diamonds; 55 | } t_map; 56 | 57 | /* ***** struct to hand all of my programms data between functions ***** */ 58 | 59 | typedef struct s_data 60 | { 61 | void *mlx; 62 | void *win; 63 | int size_x; 64 | int size_y; 65 | int p_x; 66 | int p_y; 67 | int counter; 68 | int collected; 69 | t_map *map; 70 | t_img *img; 71 | } t_data; 72 | 73 | /* ********** all of my functions ********** */ 74 | 75 | void ft_window_size(t_data *data, char **argv); 76 | int ft_key_hook(int keycode, t_data *data); 77 | void ft_create_map(t_data *data); 78 | void ft_put_object(t_data *data, char *relative_path); 79 | void ft_put_player(t_data *data); 80 | void ft_parse_input(t_data *data, char **argv, int argc); 81 | void ft_put_background(t_data *data); 82 | void ft_win(t_data *data); 83 | int ft_mouse_hook(int mousecode, t_data *data); 84 | void ft_move(t_data *data, char position, int direction); 85 | int ft_exit(t_data *data); 86 | void ft_init(t_data *data, t_map *map); 87 | 88 | #endif 89 | -------------------------------------------------------------------------------- /libft/get_next_line_utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_next_line_utils.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tblaase +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/08/03 17:52:53 by tblaase #+# #+# */ 9 | /* Updated: 2022/02/08 16:57:49 by tblaase ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_free_gnl(char **p) 16 | /* 17 | ** free's a given pointer and set's it to NULL 18 | */ 19 | { 20 | free(*p); 21 | *p = NULL; 22 | } 23 | 24 | int ft_strlen_gnl(char *s) 25 | /* 26 | ** returns length of a string until '\0' 27 | ** if the string is NULL return is 0 28 | */ 29 | { 30 | int i; 31 | 32 | i = 0; 33 | if (s == NULL) 34 | return (0); 35 | while (s && s[i]) 36 | { 37 | i++; 38 | } 39 | return (i); 40 | } 41 | 42 | int ft_strchr_gnl(char *s, int c, int flag) 43 | /* 44 | ** finds char c in string s and returns its position as an integer 45 | ** if flag == 1 return is -5 when input string is NULL 46 | ** if flag == 0 return is -1 when input string is NULL 47 | */ 48 | { 49 | int i; 50 | 51 | if (flag == 1 && s == NULL) 52 | return (-5); 53 | i = 0; 54 | while (s && s[i]) 55 | { 56 | if (s[i] == c) 57 | return (i); 58 | i++; 59 | } 60 | return (-1); 61 | } 62 | 63 | char *ft_strnjoin_gnl(char *s1, char *s2, int n) 64 | /* 65 | ** joins two strings 66 | ** first string does not have o exist 67 | ** only n characters of the second string get copied 68 | ** output is a allocated string 69 | */ 70 | { 71 | char *output; 72 | int i; 73 | int i2; 74 | 75 | i = 0; 76 | i2 = 0; 77 | if (s2[i2] == '\0') 78 | return (NULL); 79 | output = malloc(ft_strlen_gnl(s1) + n + 1); 80 | if (output == NULL) 81 | return (NULL); 82 | if (s1 != NULL) 83 | { 84 | while (s1[i] != '\0') 85 | { 86 | output[i] = s1[i]; 87 | i++; 88 | } 89 | } 90 | while (s2 && s2[i2] != '\0' && i2 < n) 91 | output[i++] = s2[i2++]; 92 | output[i] = '\0'; 93 | if (s1) 94 | ft_free_gnl(&s1); 95 | return (output); 96 | } 97 | 98 | char *ft_strndup_gnl(char *input, int n) 99 | /* 100 | ** duplicates n characters from input into output 101 | ** output is a allocated string 102 | */ 103 | { 104 | char *output; 105 | int len; 106 | int i; 107 | 108 | i = 0; 109 | len = n; 110 | output = malloc(len + 1); 111 | if (output == NULL) 112 | return (NULL); 113 | while (input && input[i] != '\0' && i < n) 114 | { 115 | output[i] = input[i]; 116 | i++; 117 | } 118 | output[i] = '\0'; 119 | return (output); 120 | } 121 | -------------------------------------------------------------------------------- /move.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* move.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tblaase +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/10/06 12:39:11 by tblaase #+# #+# */ 9 | /* Updated: 2021/10/08 15:38:37 by tblaase ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "so_long.h" 14 | 15 | static void ft_player_move(t_data *data, char position, int direction) 16 | /* will put the correct orientation of the spaceship on the screen */ 17 | { 18 | if (position == 'y' && direction == UP) 19 | { 20 | mlx_put_image_to_window(data->mlx, data->win, data->img->player_up, 21 | (data->p_x * IMG_W), (data->p_y * IMG_H)); 22 | } 23 | if (position == 'x' && direction == LEFT) 24 | { 25 | mlx_put_image_to_window(data->mlx, data->win, data->img->player_left, 26 | (data->p_x * IMG_W), (data->p_y * IMG_H)); 27 | } 28 | if (position == 'y' && direction == DOWN) 29 | { 30 | mlx_put_image_to_window(data->mlx, data->win, data->img->player_down, 31 | (data->p_x * IMG_W), (data->p_y * IMG_H)); 32 | } 33 | if (position == 'x' && direction == RIGHT) 34 | { 35 | mlx_put_image_to_window(data->mlx, data->win, data->img->player_right, 36 | (data->p_x * IMG_W), (data->p_y * IMG_H)); 37 | } 38 | } 39 | 40 | static void ft_collect(t_data *data, char pos, int dir) 41 | /* will collect the collectables 42 | ** will remove them from the map-struct as well as the screen */ 43 | { 44 | data->collected++; 45 | data->map->map[data->p_y][data->p_x] = '0'; 46 | mlx_put_image_to_window(data->mlx, data->win, data->img->background, 47 | (data->p_x * IMG_W), (data->p_y * IMG_H)); 48 | ft_player_move(data, pos, dir); 49 | } 50 | 51 | void ft_move(t_data *data, char pos, int dir) 52 | /* will check if a move is valid and move the player if valid */ 53 | { 54 | mlx_put_image_to_window(data->mlx, data->win, data->img->background, 55 | (data->p_x * IMG_W), (data->p_y * IMG_H)); 56 | if (pos == 'y' && data->map->map[data->p_y + 1 * dir][data->p_x] != '1' 57 | && (data->map->map[data->p_y + 1 * dir][data->p_x] != 'E' 58 | || data->collected == data->map->diamonds)) 59 | data->p_y = data->p_y + 1 * dir; 60 | else if (pos == 'x' && data->map->map[data->p_y][data->p_x + 1 * dir] != '1' 61 | && (data->map->map[data->p_y][data->p_x + 1 * dir] != 'E' 62 | || data->collected == data->map->diamonds)) 63 | data->p_x = data->p_x + 1 * dir; 64 | else if (pos == 'y' && data->map->map[data->p_y + 1 * dir][data->p_x] == 'E' 65 | && data->collected != data->map->diamonds) 66 | printf("Collect all diamonds before leaving\n"); 67 | else if (pos == 'x' && data->map->map[data->p_y][data->p_x + 1 * dir] == 'E' 68 | && data->collected != data->map->diamonds) 69 | printf("Collect all diamonds before leaving\n"); 70 | ft_player_move(data, pos, dir); 71 | if (data->map->map[data->p_y][data->p_x] == 'C') 72 | ft_collect(data, pos, dir); 73 | mlx_do_sync(data->mlx); 74 | printf("You moved %d times.\n", ++data->counter); 75 | } 76 | -------------------------------------------------------------------------------- /libft/get_next_line.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_next_line.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tblaase +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/08/03 17:52:55 by tblaase #+# #+# */ 9 | /* Updated: 2022/02/08 16:57:55 by tblaase ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_calloc_gnl(size_t nelem, size_t elsize) 16 | /* 17 | ** standard libft function 18 | */ 19 | { 20 | char *ptr; 21 | unsigned int i; 22 | size_t x; 23 | 24 | i = 0; 25 | x = nelem * elsize; 26 | ptr = (char *)malloc(x); 27 | if (ptr == NULL) 28 | return (0); 29 | while (i < x) 30 | { 31 | ptr[i] = 0; 32 | i++; 33 | } 34 | return (ptr); 35 | } 36 | 37 | char *ft_update_nl_gnl(char **next_line, int position) 38 | /* 39 | ** updates *next_line to the not returned remainder of *next_line 40 | */ 41 | { 42 | char *tmp; 43 | int len; 44 | 45 | len = ft_strlen_gnl(*next_line) - position; 46 | tmp = ft_strndup_gnl(*next_line + position, len); 47 | ft_free_gnl(next_line); 48 | *next_line = tmp; 49 | return (*next_line); 50 | } 51 | 52 | char *ft_output_gnl(char **next_line, int position, int bytes) 53 | /* 54 | ** error managment in first if statement 55 | ** stores every character of *next_line until '/n' or '/0' into line 56 | ** calls ft_update_nl if '/n' is found 57 | ** line is a allocated string 58 | */ 59 | { 60 | char *line; 61 | 62 | if (((bytes == 0 || bytes == -1) && *next_line == NULL) || position == -5) 63 | { 64 | if (*next_line) 65 | return (*next_line); 66 | return (NULL); 67 | } 68 | line = NULL; 69 | if (position == -1) 70 | position = ft_strlen_gnl(*next_line); 71 | else 72 | position++; 73 | line = ft_strndup_gnl(*next_line, position); 74 | if (position == ft_strlen_gnl(*next_line)) 75 | ft_free_gnl(next_line); 76 | else 77 | *next_line = ft_update_nl_gnl(next_line, position); 78 | return (line); 79 | } 80 | 81 | char *get_next_line(int fd) 82 | /* 83 | ** reads into buff and hands the whole buff to next_line 84 | ** until '/n' is found, end of file is reached or error in read 85 | ** next_line: stores everything from the past reads that was never returned 86 | ** buff: where read read's to 87 | ** position: stores the position of '/n', will be -1 if no '/n' found and -5 if next_line == NULL 88 | ** bytes: stores the output of read 89 | */ 90 | { 91 | static char *next_line; 92 | char *buff; 93 | int position; 94 | int bytes; 95 | 96 | if (BUFFER_SIZE <= 0 || fd < 0 || fd > 10240) 97 | return (NULL); 98 | buff = NULL; 99 | position = ft_strchr_gnl(next_line, '\n', 0); 100 | while (position == -1 && position != -5) 101 | { 102 | buff = ft_calloc_gnl(BUFFER_SIZE + 1, 1); 103 | if (buff == NULL) 104 | return (NULL); 105 | bytes = read(fd, buff, BUFFER_SIZE); 106 | if (bytes == 0 || bytes == -1) 107 | break ; 108 | next_line = ft_strnjoin_gnl(next_line, buff, bytes); 109 | position = ft_strchr_gnl(next_line, '\n', 1); 110 | ft_free_gnl(&buff); 111 | } 112 | ft_free_gnl(&buff); 113 | return (ft_output_gnl(&next_line, position, bytes)); 114 | } 115 | -------------------------------------------------------------------------------- /parse_input.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* parse_input.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tblaase +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/10/07 12:14:33 by tblaase #+# #+# */ 9 | /* Updated: 2021/12/08 14:36:05 by tblaase ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "so_long.h" 14 | 15 | static void ft_map_error(char *error_msg) 16 | /* will print the error message end exit the process */ 17 | { 18 | printf("%s", error_msg); 19 | exit(EXIT_FAILURE); 20 | } 21 | 22 | static void ft_check_borders(t_data *data) 23 | /* will check if the whole map is surrounded by walls */ 24 | { 25 | int x; 26 | int y; 27 | 28 | y = 0; 29 | while (y < (data->size_y / IMG_H)) 30 | { 31 | if ((int)ft_strlen(data->map->map[y]) != data->size_x / IMG_W) 32 | ft_map_error("Error\nmap has to be rectangular\n"); 33 | x = 0; 34 | while (x < (data->size_x / IMG_W)) 35 | { 36 | if (data->map->map[y][x] != '0' && data->map->map[y][x] != '1' && 37 | data->map->map[y][x] != 'C' && data->map->map[y][x] != 'P' && 38 | data->map->map[y][x] != 'E') 39 | ft_map_error("Error\nfor map '0','1','C','P','E' are valid\n"); 40 | else if ((y == 0 || x == 0) && data->map->map[y][x] != '1') 41 | ft_map_error("Error\nmap has to be surrounded by walls\n"); 42 | else if ((y == (data->size_y / IMG_H - 1) 43 | || x == (data->size_x / IMG_W - 1)) 44 | && data->map->map[y][x] != '1') 45 | ft_map_error("Error\nmap has to be surrounded by walls\n"); 46 | x++; 47 | } 48 | y++; 49 | } 50 | } 51 | 52 | static void ft_check_content(t_data *data) 53 | /* will check the contents of the map if valid */ 54 | { 55 | int y; 56 | int exit; 57 | int player; 58 | 59 | exit = 0; 60 | player = 0; 61 | data->map->diamonds = 0; 62 | y = 0; 63 | while (data->map->map && data->map->map[y]) 64 | { 65 | exit += ft_count_c(data->map->map[y], 'E'); 66 | player += ft_count_c(data->map->map[y], 'P'); 67 | data->map->diamonds += ft_count_c(data->map->map[y], 'C'); 68 | y++; 69 | } 70 | if (player != 1) 71 | ft_map_error("Error\nonly valid map if one player is contained\n"); 72 | if (exit == 0) 73 | ft_map_error("Error\nonly valid map if at least one exit is contained\n"); 74 | if (data->map->diamonds == 0) 75 | ft_map_error("Error\nonly valid map if at least one coin is contained\n"); 76 | } 77 | 78 | static void ft_input_error(int argc) 79 | /* only exists because of the 25 line limit */ 80 | { 81 | if (argc != 2) 82 | { 83 | ft_map_error("Error\nUsage: './so_long mappath/mapname.ber'\n"); 84 | exit(EXIT_FAILURE); 85 | } 86 | } 87 | 88 | void ft_parse_input(t_data *data, char **argv, int argc) 89 | /* will translate the .ber file into a 2-dimensional array of char * */ 90 | { 91 | int fd; 92 | int i; 93 | int bytes; 94 | char buffer[2]; 95 | 96 | ft_input_error(argc); 97 | i = 0; 98 | bytes = 1; 99 | buffer[1] = '\0'; 100 | fd = open(argv[1], O_RDONLY); 101 | while (bytes == 1) 102 | { 103 | bytes = read(fd, buffer, 1); 104 | if (bytes != 1) 105 | break ; 106 | if (buffer[0] != '\n' && buffer[0] != '\0') 107 | { 108 | data->map->map[i] = ft_strjoin(data->map->map[i], buffer); 109 | } 110 | else 111 | i++; 112 | } 113 | ft_check_content(data); 114 | ft_check_borders(data); 115 | } 116 | -------------------------------------------------------------------------------- /mlx/mlx.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** mlx.h for MinilibX in 3 | ** 4 | ** Made by Charlie Root 5 | ** Login 6 | ** 7 | ** Started on Mon Jul 31 16:37:50 2000 Charlie Root 8 | ** Last update Tue Oct 01 16:23:28 2014 Olivier Crouzet 9 | */ 10 | 11 | /* 12 | ** MinilibX - Please report bugs 13 | */ 14 | 15 | 16 | /* 17 | ** FR msg - FR msg - FR msg 18 | ** 19 | ** MacOSX 20 | ** La MinilibX utilise 2 frameworks Mac : OpenGL et AppKit 21 | ** qu'il faut ajouter a la compilation : 22 | ** -framework OpenGL -framework AppKit 23 | ** 24 | ** UNIX / Linux 25 | ** La MinilibX utilise 2 librairies supplementaires qu'il 26 | ** est necessaire de rajouter a la compilation : 27 | ** -lmlx -lXext -lX11 28 | ** 29 | ** La MinilibX permet le chargement des images de type Xpm. 30 | ** Notez que cette implementation est incomplete. 31 | ** 32 | ** Il y a des differences entre X11 et MacOS. 33 | ** les numeros des touches ne sont pas les memes, 34 | ** les numeros des boutons souris ne sont pas les memes. 35 | ** Egalement l'expose est gere differemment, et sous MacOS 36 | ** il est preferable d'entrer le plus tot possible dans mlx_loop, 37 | ** il est normal que les fenetres n'apparaissent pas avant mlx_loop 38 | ** (ou bien forcez avec mlx_do_sync mais c'est pas genial). 39 | ** Sous MacOS, l'octet Alpha est pris en compte dans toutes les 40 | ** images, et represente la transparence et non l'opacite comme 41 | ** c'est normalement le cas. 42 | */ 43 | 44 | 45 | #ifndef MLX_H 46 | 47 | #define MLX_H 48 | 49 | 50 | void *mlx_init(); 51 | /* 52 | ** needed before everything else. 53 | ** return (void *)0 if failed 54 | */ 55 | 56 | 57 | /* 58 | ** Basic actions 59 | */ 60 | 61 | void *mlx_new_window(void *mlx_ptr, int size_x, int size_y, char *title); 62 | /* 63 | ** return void *0 if failed 64 | */ 65 | int mlx_clear_window(void *mlx_ptr, void *win_ptr); 66 | int mlx_pixel_put(void *mlx_ptr, void *win_ptr, int x, int y, int color); 67 | /* 68 | ** origin for x & y is top left corner of the window 69 | ** y down is positive 70 | ** color is 0x00RRGGBB 71 | */ 72 | 73 | 74 | /* 75 | ** Image stuff 76 | */ 77 | 78 | void *mlx_new_image(void *mlx_ptr,int width,int height); 79 | /* 80 | ** return void *0 if failed 81 | */ 82 | char *mlx_get_data_addr(void *img_ptr, int *bits_per_pixel, 83 | int *size_line, int *endian); 84 | /* 85 | ** endian : 0 = sever X is little endian, 1 = big endian 86 | ** endian : useless on macos, client and graphical framework have the same endian 87 | */ 88 | int mlx_put_image_to_window(void *mlx_ptr, void *win_ptr, void *img_ptr, 89 | int x, int y); 90 | unsigned int mlx_get_color_value(void *mlx_ptr, int color); 91 | 92 | 93 | /* 94 | ** dealing with Events 95 | */ 96 | 97 | int mlx_mouse_hook (void *win_ptr, int (*funct_ptr)(), void *param); 98 | int mlx_key_hook (void *win_ptr, int (*funct_ptr)(), void *param); 99 | int mlx_expose_hook (void *win_ptr, int (*funct_ptr)(), void *param); 100 | 101 | int mlx_loop_hook (void *mlx_ptr, int (*funct_ptr)(), void *param); 102 | int mlx_loop (void *mlx_ptr); 103 | 104 | 105 | /* 106 | ** hook funct are called as follow : 107 | ** 108 | ** expose_hook(void *param); 109 | ** key_hook(int keycode, void *param); 110 | ** mouse_hook(int button, int x,int y, void *param); 111 | ** loop_hook(void *param); 112 | ** 113 | */ 114 | 115 | 116 | /* 117 | ** Usually asked... 118 | */ 119 | 120 | int mlx_string_put(void *mlx_ptr, void *win_ptr, int x, int y, int color, 121 | char *string); 122 | void *mlx_xpm_to_image(void *mlx_ptr, char **xpm_data, 123 | int *width, int *height); 124 | void *mlx_xpm_file_to_image(void *mlx_ptr, char *filename, 125 | int *width, int *height); 126 | void *mlx_png_file_to_image(void *mlx_ptr, char *file, int *width, int *height); 127 | 128 | int mlx_destroy_window(void *mlx_ptr, void *win_ptr); 129 | 130 | int mlx_destroy_image(void *mlx_ptr, void *img_ptr); 131 | 132 | /* 133 | ** generic hook system for all events, and minilibX functions that 134 | ** can be hooked. Some macro and defines from X11/X.h are needed here. 135 | */ 136 | 137 | int mlx_hook(void *win_ptr, int x_event, int x_mask, 138 | int (*funct)(), void *param); 139 | 140 | int mlx_mouse_hide(); 141 | int mlx_mouse_show(); 142 | int mlx_mouse_move(void *win_ptr, int x, int y); 143 | int mlx_mouse_get_pos(void *win_ptr, int *x, int *y); 144 | 145 | int mlx_do_key_autorepeatoff(void *mlx_ptr); 146 | int mlx_do_key_autorepeaton(void *mlx_ptr); 147 | int mlx_do_sync(void *mlx_ptr); 148 | 149 | #endif /* MLX_H */ 150 | -------------------------------------------------------------------------------- /mlx/mlx_init_loop.m: -------------------------------------------------------------------------------- 1 | // mlx_init_loop.m 2 | // By Ol 3 | 4 | #import 5 | #import 6 | #import 7 | 8 | #include "mlx_int.h" 9 | #include "mlx_new_window.h" 10 | 11 | #include "font.c" 12 | 13 | 14 | void do_loop_hook2(CFRunLoopTimerRef observer, void * info) 15 | { 16 | ((mlx_ptr_t *)info)->loop_hook(((mlx_ptr_t *)info)->loop_hook_data); 17 | } 18 | 19 | 20 | void do_loop_flush(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void * info) 21 | { 22 | mlx_ptr_t *mlx_ptr; 23 | mlx_win_list_t *win; 24 | 25 | mlx_ptr = (mlx_ptr_t *)info; 26 | win = mlx_ptr->win_list; 27 | while (win) 28 | { 29 | if (win->nb_flush > 0 && win->pixmgt) 30 | { 31 | [(id)win->winid selectGLContext]; 32 | [(id)win->winid mlx_gl_draw]; 33 | glFlush(); 34 | win->nb_flush = 0; 35 | } 36 | win = win->next; 37 | } 38 | } 39 | 40 | 41 | 42 | 43 | void *mlx_init() 44 | { 45 | mlx_ptr_t *new_mlx; 46 | int bidon; 47 | int i; 48 | 49 | if ((new_mlx = malloc(sizeof(*new_mlx))) == NULL) 50 | return ((void *)0); 51 | new_mlx->win_list = NULL; 52 | new_mlx->img_list = NULL; 53 | new_mlx->loop_hook = NULL; 54 | new_mlx->loop_hook_data = NULL; 55 | new_mlx->main_loop_active = 0; 56 | 57 | new_mlx->appid = [NSApplication sharedApplication]; 58 | 59 | // super magic trick to detach app from terminal, get menubar & key input events 60 | for (NSRunningApplication * app in [NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.apple.finder"]) 61 | { 62 | [app activateWithOptions:NSApplicationActivateIgnoringOtherApps]; 63 | break; 64 | } 65 | usleep(100000); 66 | ProcessSerialNumber psn = { 0, kCurrentProcess }; 67 | (void) TransformProcessType(&psn, kProcessTransformToForegroundApplication); 68 | usleep(100000); 69 | [[NSRunningApplication currentApplication] activateWithOptions:NSApplicationActivateIgnoringOtherApps]; 70 | 71 | // load font 72 | new_mlx->font = mlx_new_image(new_mlx, (FONT_WIDTH+2)*95, FONT_HEIGHT); 73 | i = 0; 74 | while (i < 4*(FONT_WIDTH+2)*95*FONT_HEIGHT) 75 | { 76 | new_mlx->font->buffer[i+0] = font_atlas.pixel_data[i+2]; 77 | new_mlx->font->buffer[i+1] = font_atlas.pixel_data[i+1]; 78 | new_mlx->font->buffer[i+2] = font_atlas.pixel_data[i+0]; 79 | ((unsigned char *)new_mlx->font->buffer)[i+3] = 0xFF-font_atlas.pixel_data[i+3]; 80 | i += 4; 81 | } 82 | 83 | 84 | #ifdef STRINGPUTX11 85 | new_mlx->font->vertexes[2] = FONT_WIDTH/1.4; 86 | new_mlx->font->vertexes[4] = FONT_WIDTH/1.4; 87 | new_mlx->font->vertexes[5] = (-FONT_HEIGHT-1)/1.4; 88 | new_mlx->font->vertexes[7] = (-FONT_HEIGHT-1)/1.4; 89 | #else 90 | new_mlx->font->vertexes[2] = FONT_WIDTH; 91 | new_mlx->font->vertexes[4] = FONT_WIDTH; 92 | new_mlx->font->vertexes[5] = -FONT_HEIGHT-1; 93 | new_mlx->font->vertexes[7] = -FONT_HEIGHT-1; 94 | #endif 95 | 96 | return ((void *)new_mlx); 97 | } 98 | 99 | 100 | void mlx_loop(mlx_ptr_t *mlx_ptr) 101 | { 102 | CFRunLoopObserverRef observer; 103 | CFRunLoopObserverContext ocontext = {.version = 0, .info = mlx_ptr, .retain = NULL, .release = NULL, .copyDescription = NULL}; 104 | 105 | mlx_ptr->main_loop_active = 1; 106 | 107 | observer = CFRunLoopObserverCreate(NULL, kCFRunLoopBeforeTimers, true, 0, do_loop_flush, &ocontext); 108 | CFRunLoopAddObserver(CFRunLoopGetMain(), observer, kCFRunLoopCommonModes); 109 | 110 | // [[[MlxLoopHookObj alloc] initWithPtr:mlx_ptr] performSelector:@selector(do_loop_hook) withObject:nil afterDelay:0.0]; 111 | 112 | [NSApp run]; 113 | } 114 | 115 | 116 | void mlx_pixel_put(mlx_ptr_t *mlx_ptr, mlx_win_list_t *win_ptr, int x, int y, int color) 117 | { 118 | if (!win_ptr->pixmgt) 119 | return ; 120 | [(id)(win_ptr->winid) selectGLContext]; 121 | [(id)(win_ptr->winid) pixelPutColor:color X:x Y:y]; 122 | win_ptr->nb_flush ++; 123 | } 124 | 125 | 126 | void mlx_int_loop_once() 127 | { 128 | NSEvent *event; 129 | NSDate *thedate; 130 | 131 | thedate = [NSDate dateWithTimeIntervalSinceNow:0.1]; 132 | while (42) 133 | { 134 | event = [NSApp nextEventMatchingMask:NSEventMaskAny 135 | untilDate:thedate 136 | inMode:NSDefaultRunLoopMode 137 | dequeue:YES]; 138 | if (event == nil) 139 | { 140 | [thedate release]; 141 | return ; 142 | } 143 | [NSApp sendEvent:event]; 144 | [NSApp updateWindows]; 145 | } 146 | } 147 | 148 | 149 | int mlx_do_sync(mlx_ptr_t *mlx_ptr) 150 | { 151 | mlx_win_list_t *win; 152 | 153 | win = mlx_ptr->win_list; 154 | while (win) 155 | { 156 | if (win->pixmgt) 157 | { 158 | [(id)(win->winid) selectGLContext]; 159 | [(id)(win->winid) mlx_gl_draw]; 160 | glFlush(); 161 | if (!mlx_ptr->main_loop_active) 162 | mlx_int_loop_once(); 163 | } 164 | win = win->next; 165 | } 166 | return (0); 167 | } 168 | 169 | 170 | int mlx_loop_hook(mlx_ptr_t *mlx_ptr, void (*fct)(void *), void *param) 171 | { 172 | CFRunLoopTimerContext tcontext = {0, mlx_ptr, NULL, NULL, NULL}; 173 | CFRunLoopTimerRef timer; 174 | 175 | if (mlx_ptr->loop_hook != NULL) 176 | { 177 | CFRunLoopTimerInvalidate(mlx_ptr->loop_timer); 178 | [(id)(mlx_ptr->loop_timer) release]; 179 | } 180 | 181 | mlx_ptr->loop_hook = fct; 182 | mlx_ptr->loop_hook_data = param; 183 | 184 | if (fct) 185 | { 186 | timer = CFRunLoopTimerCreate(kCFAllocatorDefault, 0.0, 0.0001, 0, 0, &do_loop_hook2, &tcontext); 187 | mlx_ptr->loop_timer = timer; 188 | CFRunLoopAddTimer(CFRunLoopGetMain(), timer, kCFRunLoopCommonModes); 189 | } 190 | 191 | return (0); 192 | } 193 | -------------------------------------------------------------------------------- /mlx/mlx_new_image.m: -------------------------------------------------------------------------------- 1 | // mlx_new_image 2 | // 3 | // by Ol 4 | // 5 | 6 | 7 | #import 8 | #import 9 | 10 | #include "mlx_int.h" 11 | #include "mlx_new_window.h" 12 | 13 | 14 | 15 | void *mlx_new_image(mlx_ptr_t *mlx_ptr, int width, int height) 16 | { 17 | mlx_img_list_t *newimg; 18 | 19 | // if (mlx_ptr->win_list == NULL) 20 | // return (NULL); // need at leat one window created to have openGL context and create texture 21 | if ((newimg = malloc(sizeof(*newimg))) == NULL) 22 | return ((void *)0); 23 | newimg->next = mlx_ptr->img_list; 24 | mlx_ptr->img_list = newimg; 25 | newimg->width = width; 26 | newimg->height = height; 27 | newimg->vertexes[0] = 0.0; newimg->vertexes[1] = 0.0; 28 | newimg->vertexes[2] = width; newimg->vertexes[3] = 0.0; 29 | newimg->vertexes[4] = width; newimg->vertexes[5] = -height; 30 | newimg->vertexes[6] = 0.0; newimg->vertexes[7] = -height; 31 | newimg->buffer = malloc(UNIQ_BPP*width*height); 32 | bzero(newimg->buffer, UNIQ_BPP*width*height); 33 | 34 | return (newimg); 35 | } 36 | 37 | mlx_img_ctx_t *add_img_to_ctx(mlx_img_list_t *img, mlx_win_list_t *win) 38 | { 39 | mlx_img_ctx_t *imgctx; 40 | 41 | imgctx = win->img_list; 42 | while (imgctx) 43 | { 44 | if (imgctx->img == img) 45 | return (imgctx); 46 | imgctx = imgctx->next; 47 | } 48 | 49 | imgctx = malloc(sizeof(*imgctx)); 50 | imgctx->img = img; 51 | imgctx->next = win->img_list; 52 | win->img_list = imgctx; 53 | 54 | glGenTextures(1, &(imgctx->texture)); 55 | glBindTexture(GL_TEXTURE_2D, imgctx->texture); 56 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 57 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 58 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 59 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 60 | glTexImage2D( 61 | GL_TEXTURE_2D, 0, /* target, level of detail */ 62 | GL_RGBA8, /* internal format */ 63 | img->width, img->height, 0, /* width, height, border */ 64 | GL_BGRA, GL_UNSIGNED_BYTE, /* external format, type */ 65 | img->buffer /* pixels */ 66 | ); 67 | 68 | glGenBuffers(1, &(imgctx->vbuffer)); 69 | glBindBuffer(GL_ARRAY_BUFFER, imgctx->vbuffer); 70 | glBufferData(GL_ARRAY_BUFFER, sizeof(img->vertexes), img->vertexes, GL_DYNAMIC_DRAW); // 4 points buff 71 | 72 | return (imgctx); 73 | } 74 | 75 | 76 | void mlx_put_image_to_window(mlx_ptr_t *mlx_ptr, mlx_win_list_t *win_ptr, mlx_img_list_t *img_ptr, int x, int y) 77 | { 78 | mlx_img_ctx_t *imgctx; 79 | 80 | if (!win_ptr->pixmgt) 81 | return ; 82 | 83 | [(id)(win_ptr->winid) selectGLContext]; 84 | imgctx = add_img_to_ctx(img_ptr, win_ptr); 85 | 86 | // update texture 87 | glBindTexture(GL_TEXTURE_2D, imgctx->texture); 88 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, img_ptr->width, img_ptr->height, 0, 89 | GL_BGRA, GL_UNSIGNED_BYTE, img_ptr->buffer); 90 | 91 | [(id)(win_ptr->winid) mlx_gl_draw_img:img_ptr andCtx:imgctx andX:x andY:y]; 92 | 93 | win_ptr->nb_flush ++; 94 | } 95 | 96 | // assume here 32bpp little endian 97 | 98 | char *mlx_get_data_addr(mlx_img_list_t *img_ptr, int *bits_per_pixel, int *size_line, int *endian) 99 | { 100 | *bits_per_pixel = UNIQ_BPP*8; 101 | *size_line = img_ptr->width*UNIQ_BPP; 102 | *endian = 0; // little endian for now on mac-intel 103 | return (img_ptr->buffer); 104 | } 105 | 106 | unsigned int mlx_get_color_value(mlx_ptr_t *mlx_ptr, int color) 107 | { 108 | return (color); 109 | } 110 | 111 | int mlx_string_put(mlx_ptr_t *mlx_ptr, mlx_win_list_t *win_ptr, int x, int y, int color, unsigned char *string) 112 | { 113 | mlx_img_ctx_t *imgctx; 114 | int gX; 115 | int gY; 116 | 117 | if (!win_ptr->pixmgt) 118 | return(0); 119 | 120 | #ifdef STRINGPUTX11 121 | y -= (FONT_HEIGHT * 2)/3; 122 | #endif 123 | 124 | [(id)(win_ptr->winid) selectGLContext]; 125 | 126 | imgctx = add_img_to_ctx(mlx_ptr->font, win_ptr); 127 | 128 | while (*string) 129 | { 130 | if (*string >= 32 && *string <= 127) 131 | { 132 | gX = (FONT_WIDTH+2)*(*string-32); 133 | gY = 0; 134 | // printf("put char %c pos %d %d\n", *string, gX, gY); 135 | [(id)(win_ptr->winid) mlx_gl_draw_font:mlx_ptr->font andCtx:imgctx andX:x andY:y andColor:color glyphX:gX glyphY:gY]; 136 | #ifdef STRINGPUTX11 137 | x += FONT_WIDTH/1.4; 138 | #else 139 | x += FONT_WIDTH; 140 | #endif 141 | } 142 | string ++; 143 | } 144 | 145 | win_ptr->nb_flush ++; 146 | 147 | return (0); 148 | } 149 | 150 | int mlx_destroy_image(mlx_ptr_t *mlx_ptr, mlx_img_list_t *img_todel) 151 | { 152 | mlx_img_ctx_t ctx_first; 153 | mlx_img_ctx_t *ctx; 154 | mlx_img_ctx_t *ctx_to_del; 155 | mlx_img_list_t img_first; 156 | mlx_img_list_t *img; 157 | mlx_win_list_t *win; 158 | 159 | img_first.next = mlx_ptr->img_list; 160 | img = &img_first; 161 | while (img && img->next) 162 | { 163 | if (img->next == img_todel) 164 | img->next = img->next->next; 165 | img = img->next; 166 | } 167 | mlx_ptr->img_list = img_first.next; 168 | 169 | 170 | win = mlx_ptr->win_list; 171 | while (win) 172 | { 173 | ctx_first.next = win->img_list; 174 | ctx = &ctx_first; 175 | while (ctx && ctx->next) 176 | { 177 | if (ctx->next->img == img_todel) 178 | { 179 | [(id)(win->winid) selectGLContext]; 180 | glDeleteBuffers(1, &(ctx->next->vbuffer)); 181 | glDeleteTextures(1, &(ctx->next->texture)); 182 | ctx_to_del = ctx->next; 183 | ctx->next = ctx->next->next; 184 | free(ctx_to_del); 185 | } 186 | ctx = ctx->next; 187 | } 188 | win->img_list = ctx_first.next; 189 | win = win->next; 190 | } 191 | 192 | 193 | free(img_todel->buffer); 194 | free(img_todel); 195 | 196 | // printf("destroy image done.\n"); 197 | return (0); 198 | } 199 | -------------------------------------------------------------------------------- /textures/exit.xpm: -------------------------------------------------------------------------------- 1 | /* XPM */ 2 | static char *result[] = { 3 | /* columns rows colors chars-per-pixel */ 4 | "32 32 198 2 ", 5 | " c #000001", 6 | ". c #090504", 7 | "X c #03060A", 8 | "o c #06080D", 9 | "O c #090A0F", 10 | "+ c #020517", 11 | "@ c #060B12", 12 | "# c #090C13", 13 | "$ c #0B0E19", 14 | "% c #0C121C", 15 | "& c #10151D", 16 | "* c #0F1722", 17 | "= c #0F1927", 18 | "- c #07192B", 19 | "; c #111723", 20 | ": c #131C2A", 21 | "> c #0D1E35", 22 | ", c #131D32", 23 | "< c #0C253C", 24 | "1 c #152334", 25 | "2 c #1A2434", 26 | "3 c #14253C", 27 | "4 c #192539", 28 | "5 c #162839", 29 | "6 c #1C2B3C", 30 | "7 c #223639", 31 | "8 c #2F4238", 32 | "9 c #35463D", 33 | "0 c #0F2845", 34 | "q c #142C43", 35 | "w c #1A2B44", 36 | "e c #172F49", 37 | "r c #1D2F4B", 38 | "t c #0E3344", 39 | "y c #1B3246", 40 | "u c #163448", 41 | "i c #1C344B", 42 | "p c #1D3A4D", 43 | "a c #1F3855", 44 | "s c #202D46", 45 | "d c #212F4A", 46 | "f c #223345", 47 | "g c #23344D", 48 | "h c #233B4D", 49 | "j c #223653", 50 | "k c #263755", 51 | "l c #233B53", 52 | "z c #2A3B53", 53 | "x c #233D5A", 54 | "c c #2A3E5A", 55 | "v c #2E4645", 56 | "b c #354846", 57 | "n c #354D4C", 58 | "m c #3E544A", 59 | "M c #2B4355", 60 | "N c #284755", 61 | "B c #26415C", 62 | "V c #2A445B", 63 | "C c #2C4B5C", 64 | "Z c #32435E", 65 | "A c #344C5E", 66 | "S c #2E4363", 67 | "D c #2A4967", 68 | "F c #2B4D64", 69 | "G c #334461", 70 | "H c #354A65", 71 | "J c #354768", 72 | "K c #334C6B", 73 | "L c #3B4B69", 74 | "P c #2F5366", 75 | "I c #345363", 76 | "U c #3B5966", 77 | "Y c #34536D", 78 | "T c #3A516D", 79 | "R c #2D4E70", 80 | "E c #3B4D72", 81 | "W c #355371", 82 | "Q c #3B5372", 83 | "! c #395B75", 84 | "~ c #3C557A", 85 | "^ c #3C6274", 86 | "/ c #3A6079", 87 | "( c #455749", 88 | ") c #445959", 89 | "_ c #4C604E", 90 | "` c #566749", 91 | "' c #466759", 92 | "] c #5C735A", 93 | "[ c #5A795E", 94 | "{ c #647B5A", 95 | "} c #435C68", 96 | "| c #435A77", 97 | " . c #43557B", 98 | ".. c #425A79", 99 | "X. c #486667", 100 | "o. c #4C666C", 101 | "O. c #546C61", 102 | "+. c #567268", 103 | "@. c #4C6C77", 104 | "#. c #466A7A", 105 | "$. c #587773", 106 | "%. c #718959", 107 | "&. c #5E807A", 108 | "*. c #65836A", 109 | "=. c #788B60", 110 | "-. c #7A9268", 111 | ";. c #718F7C", 112 | ":. c #74927B", 113 | ">. c #A7B53D", 114 | ",. c #97A64C", 115 | "<. c #9BAC4B", 116 | "1. c #95A450", 117 | "2. c #9EA953", 118 | "3. c #859C69", 119 | "4. c #8DA86E", 120 | "5. c #93A562", 121 | "6. c #99AC63", 122 | "7. c #93A968", 123 | "8. c #94A86A", 124 | "9. c #8EA57B", 125 | "0. c #92AB78", 126 | "q. c #9EB176", 127 | "w. c #A1AE69", 128 | "e. c #A9BA6E", 129 | "r. c #AFBD78", 130 | "t. c #B1BF71", 131 | "y. c #B7C053", 132 | "u. c #CBD65F", 133 | "i. c #D1DB5F", 134 | "p. c #D3DC68", 135 | "a. c #C7CF73", 136 | "s. c #D5DE7B", 137 | "d. c #435E81", 138 | "f. c #485886", 139 | "g. c #416383", 140 | "h. c #496584", 141 | "j. c #4C648C", 142 | "k. c #476C89", 143 | "l. c #55688C", 144 | "z. c #4D7783", 145 | "x. c #4B718D", 146 | "c. c #537B83", 147 | "v. c #5A7D84", 148 | "b. c #56748D", 149 | "n. c #4D6790", 150 | "m. c #4D6C96", 151 | "M. c #546490", 152 | "N. c #506D92", 153 | "B. c #596992", 154 | "V. c #556E9B", 155 | "C. c #596B9C", 156 | "Z. c #4F7492", 157 | "A. c #4D759B", 158 | "S. c #537B95", 159 | "D. c #5B759A", 160 | "F. c #597A98", 161 | "G. c #5B7B9B", 162 | "H. c #627B86", 163 | "J. c #617A9A", 164 | "K. c #597AA4", 165 | "L. c #5E7DA9", 166 | "P. c #637CA2", 167 | "I. c #607DAE", 168 | "U. c #5D8583", 169 | "Y. c #5B869F", 170 | "T. c #688A87", 171 | "R. c #62858C", 172 | "E. c #748F87", 173 | "W. c #769586", 174 | "Q. c #638795", 175 | "!. c #678E94", 176 | "~. c #6A8D92", 177 | "^. c #669794", 178 | "/. c #6C9292", 179 | "(. c #709A97", 180 | "). c #5882A2", 181 | "_. c #5D82B0", 182 | "`. c #6486AB", 183 | "'. c #6886B2", 184 | "]. c #638AB4", 185 | "[. c #78A7A5", 186 | "{. c #97AC82", 187 | "}. c #94B395", 188 | "|. c #A3BB87", 189 | " X c #A3BA92", 190 | ".X c #87A6A5", 191 | "XX c #A1C19E", 192 | "oX c #B1CCAD", 193 | "OX c #D4DC87", 194 | "+X c #C9D6A3", 195 | "@X c #D2DCA1", 196 | "#X c #DCE3A3", 197 | "$X c #E4E9AF", 198 | "%X c #E5E9B2", 199 | "&X c #F3F5BA", 200 | "*X c #F5F7C8", 201 | "=X c #F7F9D1", 202 | "-X c #FBFBE6", 203 | /* pixels */ 204 | " X X X X X X @ @ ", 205 | " # X ", 206 | " X 1 z H | k.A.Z.h.T 6 O ", 207 | " O 6 z K ! T W Y K T ~ K.L.N.M O ", 208 | " o : f M V g q 0 u l l y q y V k.'.l. X ", 209 | " X 2 f p < e F / #.I N h h V N M 3 H P.l.- X ", 210 | " o : 6 5 p k.).Y.S.z.z./ ! u y h C Y y < J.F. # ", 211 | " % 1 1 Y G.).#.I P c.T.!././.@.C p C ! M q J.| @ ", 212 | " o : y n.`.k.l u &.^.^.&.+.$.;.^.(.X.6 P / i Q '.w X ", 213 | " X % 3 A._.I 5 P Y.U.' ( { %.%.{ _ U.[.@.t #.W t J.h. O ", 214 | " @ X d.].W i C !.&.8 ` -.4.7.7.0.{.` ^.[.< U x.< h.G.+ # ", 215 | " @ 3 `.m.i u c.c.7 [ |.r.<.<.<.,.6.{.*..XU N x.C H ).5 # ", 216 | " o Q I.R u @.R.v +.}.+X%X$Xs.p.y.,.q.4..X@.u S.W B ].B o ", 217 | " > P.m.i B Q.X.7 W. Xe.a.*X-X*Xp.>.6.}..Xo.6 x.! g _.K @ ", 218 | "X z '.~ q Q G.p m W.q.w.<.i.&X-X=XOXt.oXW.b z k.F S _.c $ @ ", 219 | "X H L.D i h.F.t ) W.:.q.1.y.u.p.#X%X@XXX' b I Z.a ! L.r = o ", 220 | " Q C.S a S.F.< } E.] 9.7.1.2.2.<.e.XX*.n n b...> k.A.> : o ", 221 | "X .n.a i x.S.6 V W.O.*.0.8.7.1.8.5.{ ( v } b.a g _.~ , = X ", 222 | "X E j.j a g.F.h u $.;.9 ] -.3.3.=.] m v X.H.L < N.L.w 4 * X ", 223 | " G j.k a Y S.M p N (.T.b 9 ( ( b 8 v @.~...3 K ].W w , # ", 224 | " g j.c d J F.A p p ^ !.T.+.X.X.X.+.v.U.| d G K.g.w g : X ", 225 | " : .c 6 k n.| f z y F z.T.^./.~.R.@.A j .N.~ g k s % X ", 226 | " X Z H 2 w T C.L i h p p D P H C p g Y h. .Z r r z 2 X ", 227 | " 2 G f 6 z n.G.V x B a p j p z H T T G j j c G d @ X ", 228 | " X # f z 2 6 z Z.D.T l l k k z c c c d r J f.E k = X ", 229 | " X % 6 7 6 4 y j.J.h.L g j w r k z G .C.f.j $ o ", 230 | " X # 6 6 : , 6 d.D.K.V.M.M.M.B.V.f. .J w O X ", 231 | " X % : : : : s S ~ V.C.C.V.j.~ z 3 $ @ X ", 232 | " X X O @ & ; * ; : , > q w : . O @ @ X ", 233 | " . X O O $ % $ # & $ @ o @ @ ", 234 | " X X X X X @ X X X ", 235 | " X X " 236 | }; 237 | -------------------------------------------------------------------------------- /textures/background.xpm: -------------------------------------------------------------------------------- 1 | /* XPM */ 2 | static char *result[] = { 3 | /* columns rows colors chars-per-pixel */ 4 | "32 32 256 2 ", 5 | " c black", 6 | ". c #010101", 7 | "X c #020202", 8 | "o c gray1", 9 | "O c #040404", 10 | "+ c gray2", 11 | "@ c #060606", 12 | "# c #070707", 13 | "$ c gray3", 14 | "% c #090909", 15 | "& c gray4", 16 | "* c #0B0B0B", 17 | "= c #0C0C0C", 18 | "- c gray5", 19 | "; c #0E0E0E", 20 | ": c gray6", 21 | "> c #101010", 22 | ", c #111111", 23 | "< c gray7", 24 | "1 c #131313", 25 | "2 c gray8", 26 | "3 c #151515", 27 | "4 c #161616", 28 | "5 c gray9", 29 | "6 c #181818", 30 | "7 c #191919", 31 | "8 c gray10", 32 | "9 c #1B1B1B", 33 | "0 c gray11", 34 | "q c #1D1D1D", 35 | "w c #1E1E1E", 36 | "e c gray12", 37 | "r c #202020", 38 | "t c gray13", 39 | "y c #222222", 40 | "u c #232323", 41 | "i c gray14", 42 | "p c #252525", 43 | "a c gray15", 44 | "s c #272727", 45 | "d c #282828", 46 | "f c gray16", 47 | "g c #2A2A2A", 48 | "h c gray17", 49 | "j c #2C2C2C", 50 | "k c #2D2D2D", 51 | "l c gray18", 52 | "z c #2F2F2F", 53 | "x c gray19", 54 | "c c #313131", 55 | "v c #323232", 56 | "b c gray20", 57 | "n c #343434", 58 | "m c #353535", 59 | "M c gray21", 60 | "N c #373737", 61 | "B c gray22", 62 | "V c #393939", 63 | "C c #3A3A3A", 64 | "Z c gray23", 65 | "A c #3C3C3C", 66 | "S c gray24", 67 | "D c #3E3E3E", 68 | "F c #3F3F3F", 69 | "G c gray25", 70 | "H c #414141", 71 | "J c gray26", 72 | "K c #434343", 73 | "L c #444444", 74 | "P c gray27", 75 | "I c #464646", 76 | "U c gray28", 77 | "Y c #484848", 78 | "T c #494949", 79 | "R c gray29", 80 | "E c #4B4B4B", 81 | "W c #4C4C4C", 82 | "Q c gray30", 83 | "! c #4E4E4E", 84 | "~ c gray31", 85 | "^ c #505050", 86 | "/ c #515151", 87 | "( c gray32", 88 | ") c #535353", 89 | "_ c gray33", 90 | "` c #555555", 91 | "' c #565656", 92 | "] c gray34", 93 | "[ c #585858", 94 | "{ c gray35", 95 | "} c #5A5A5A", 96 | "| c #5B5B5B", 97 | " . c gray36", 98 | ".. c #5D5D5D", 99 | "X. c gray37", 100 | "o. c #5F5F5F", 101 | "O. c #606060", 102 | "+. c gray38", 103 | "@. c #626262", 104 | "#. c gray39", 105 | "$. c #646464", 106 | "%. c #656565", 107 | "&. c gray40", 108 | "*. c #676767", 109 | "=. c #686868", 110 | "-. c DimGray", 111 | ";. c #6A6A6A", 112 | ":. c gray42", 113 | ">. c #6C6C6C", 114 | ",. c #6D6D6D", 115 | "<. c gray43", 116 | "1. c #6F6F6F", 117 | "2. c gray44", 118 | "3. c #717171", 119 | "4. c #727272", 120 | "5. c gray45", 121 | "6. c #747474", 122 | "7. c gray46", 123 | "8. c #767676", 124 | "9. c #777777", 125 | "0. c gray47", 126 | "q. c #797979", 127 | "w. c gray48", 128 | "e. c #7B7B7B", 129 | "r. c #7C7C7C", 130 | "t. c gray49", 131 | "y. c #7E7E7E", 132 | "u. c gray50", 133 | "i. c #808080", 134 | "p. c #818181", 135 | "a. c gray51", 136 | "s. c #838383", 137 | "d. c #848484", 138 | "f. c gray52", 139 | "g. c #868686", 140 | "h. c gray53", 141 | "j. c #888888", 142 | "k. c #898989", 143 | "l. c gray54", 144 | "z. c #8B8B8B", 145 | "x. c gray55", 146 | "c. c #8D8D8D", 147 | "v. c #8E8E8E", 148 | "b. c gray56", 149 | "n. c #909090", 150 | "m. c gray57", 151 | "M. c #929292", 152 | "N. c #939393", 153 | "B. c gray58", 154 | "V. c #959595", 155 | "C. c gray59", 156 | "Z. c #979797", 157 | "A. c #989898", 158 | "S. c gray60", 159 | "D. c #9A9A9A", 160 | "F. c #9B9B9B", 161 | "G. c gray61", 162 | "H. c #9D9D9D", 163 | "J. c gray62", 164 | "K. c #9F9F9F", 165 | "L. c #A0A0A0", 166 | "P. c gray63", 167 | "I. c #A2A2A2", 168 | "U. c gray64", 169 | "Y. c #A4A4A4", 170 | "T. c #A5A5A5", 171 | "R. c gray65", 172 | "E. c #A7A7A7", 173 | "W. c gray66", 174 | "Q. c #A9A9A9", 175 | "!. c #AAAAAA", 176 | "~. c gray67", 177 | "^. c #ACACAC", 178 | "/. c gray68", 179 | "(. c #AEAEAE", 180 | "). c #AFAFAF", 181 | "_. c gray69", 182 | "`. c #B1B1B1", 183 | "'. c #B2B2B2", 184 | "]. c gray70", 185 | "[. c #B4B4B4", 186 | "{. c gray71", 187 | "}. c #B6B6B6", 188 | "|. c #B7B7B7", 189 | " X c gray72", 190 | ".X c #B9B9B9", 191 | "XX c gray73", 192 | "oX c #BBBBBB", 193 | "OX c #BCBCBC", 194 | "+X c gray74", 195 | "@X c gray", 196 | "#X c gray75", 197 | "$X c #C0C0C0", 198 | "%X c #C1C1C1", 199 | "&X c gray76", 200 | "*X c #C3C3C3", 201 | "=X c gray77", 202 | "-X c #C5C5C5", 203 | ";X c #C6C6C6", 204 | ":X c gray78", 205 | ">X c #C8C8C8", 206 | ",X c gray79", 207 | " X @ o . . X X . X X X X X o o O ", 268 | ". . . X X . . . X @ : 6 > % X + o . . X X X . . X o o o X ", 269 | "o X X o + @ & + . O X X o o X X o O o . ", 270 | "+ + # # X . % + + O X X O . . . . . . . . ", 271 | " . # & @ o o O O X . o X . X o . . . . . . . . ", 272 | " O # O O . o O O X . X o . X . . o + . . . . . . . . ", 273 | "+ X O = X O @ @ o . . X O . X X . . . . . . . . ", 274 | "$ o . + = O @ # @ o . O O . . . . . . . . . . ", 275 | "o . o $ $ O X O + @ # @ o . . O O @ o . . . . . . . . ", 276 | "X . o $ % O o O + O X X O @ . O X + ; & . . . . . . . . ", 277 | "# X X X X . X o X X O $ * . o . % 3 > . . . . . . . . . ", 278 | "o X X X o + # % X . . O # @ @ X o O X . . . . X ", 279 | "O O o X X o + + X . . X o o X . . X o o X . . . . X ", 280 | "+ O o X . X X X O O o . X o @ + O o X X X X X . . . . X ", 281 | "X X X X . X X X . o + O X X o + # @ O o . X . . . . X ", 282 | " . X o o O O X X X . X + O o . X . . . . X ", 283 | " . o O + + + $ + O O + @ + o X X . . . X . . . . X ", 284 | ". X o + + + O O t 5 * @ # % # O . o O + X . . . . X ", 285 | "o O + @ @ O o X c t - X X + o . o + # $ X . . . . X ", 286 | "X X * 1 1 $ O o % . + + . X X o O O . . . . . . . . ", 287 | "- # X X # % # X + @ # . * = . . X o O O + + . . . . . . . . ", 288 | "= $ o . o O O . % & O t y . X X o o O + + @ . . . . . . . . ", 289 | " . O @ + o . . $ % + r y + X X X o o O O O . . . . . . . . ", 290 | " + # + X @ + # X @ $ . . . X X X . . . . . . . . ", 291 | "o o o . + O # . . . . . . . . ", 292 | "@ o . X . O $ . . % % . . . . . . . . . . . . . . . . ", 293 | " o + O o X + O o # X X X X X X X X . . . . . . . . " 294 | }; 295 | -------------------------------------------------------------------------------- /mlx/mlx_shaders.c: -------------------------------------------------------------------------------- 1 | // mlx_shaders.c 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "mlx_int.h" 8 | 9 | 10 | void display_log(GLuint object, void (*param_func)(), void (*getlog_func)()) 11 | { 12 | GLint log_length; 13 | char *log; 14 | 15 | param_func(object, GL_INFO_LOG_LENGTH, &log_length); 16 | log = malloc(log_length); 17 | getlog_func(object, log_length, NULL, log); 18 | fprintf(stderr, "%s", log); 19 | free(log); 20 | } 21 | 22 | 23 | int mlx_shaders_pixel(glsl_info_t *glsl) 24 | { 25 | char *source; 26 | int length; 27 | GLint action_ok; 28 | 29 | glsl->pixel_vshader = glCreateShader(GL_VERTEX_SHADER); 30 | source = strdup("#version 110 \n" 31 | "attribute vec2 position;" 32 | "varying vec2 texcoord;" 33 | "void main()" 34 | "{" 35 | " gl_Position = vec4( position, 0.0, 1.0);" 36 | " texcoord = vec2(position[0]+1.0, 1.0 - position[1]) / 2.0;" 37 | "}"); 38 | length = strlen(source); 39 | glShaderSource(glsl->pixel_vshader, 1, (const GLchar**)&source, &length); 40 | glCompileShader(glsl->pixel_vshader); 41 | free(source); 42 | 43 | glGetShaderiv(glsl->pixel_vshader, GL_COMPILE_STATUS, &action_ok); 44 | if (!action_ok) { 45 | fprintf(stderr, "Failed to compile pixel vshader :\n"); 46 | display_log(glsl->pixel_vshader, glGetShaderiv, glGetShaderInfoLog); 47 | return (1); 48 | } 49 | 50 | glsl->pixel_fshader = glCreateShader(GL_FRAGMENT_SHADER); 51 | source = strdup("#version 110 \n" 52 | "uniform sampler2D texture;" 53 | "varying vec2 texcoord;" 54 | "void main()" 55 | "{" 56 | " gl_FragColor = texture2D(texture, texcoord);" 57 | "}"); 58 | length = strlen(source); 59 | glShaderSource(glsl->pixel_fshader, 1, (const GLchar**)&source, &length); 60 | glCompileShader(glsl->pixel_fshader); 61 | free(source); 62 | 63 | glGetShaderiv(glsl->pixel_fshader, GL_COMPILE_STATUS, &action_ok); 64 | if (!action_ok) { 65 | fprintf(stderr, "Failed to compile pixel fshader :\n"); 66 | display_log(glsl->pixel_fshader, glGetShaderiv, glGetShaderInfoLog); 67 | return (1); 68 | } 69 | 70 | glsl->pixel_program = glCreateProgram(); 71 | glAttachShader(glsl->pixel_program, glsl->pixel_vshader); 72 | glAttachShader(glsl->pixel_program, glsl->pixel_fshader); 73 | glLinkProgram(glsl->pixel_program); 74 | 75 | glGetProgramiv(glsl->pixel_program, GL_LINK_STATUS, &action_ok); 76 | if (!action_ok) { 77 | fprintf(stderr, "Failed to link pixel shader program:\n"); 78 | display_log(glsl->pixel_program, glGetProgramiv, glGetProgramInfoLog); 79 | return (1); 80 | } 81 | 82 | glFlush(); 83 | 84 | return (0); 85 | } 86 | 87 | 88 | int mlx_shaders_image(glsl_info_t *glsl) 89 | { 90 | char *source; 91 | int length; 92 | GLint action_ok; 93 | 94 | glsl->image_vshader = glCreateShader(GL_VERTEX_SHADER); 95 | source = strdup("#version 110 \n" 96 | "attribute vec2 position;" 97 | "uniform vec2 winhalfsize;" 98 | "uniform vec2 imagepos;" 99 | "uniform vec2 imagesize;" 100 | "varying vec2 texcoord;" 101 | "void main()" 102 | "{" 103 | " texcoord = position / imagesize;" 104 | " vec2 pos = position - winhalfsize + imagepos;" 105 | " pos = pos / winhalfsize;" 106 | " gl_Position = vec4( pos, 0.0, 1.0);" 107 | "}"); 108 | length = strlen(source); 109 | glShaderSource(glsl->image_vshader, 1, (const GLchar**)&source, &length); 110 | glCompileShader(glsl->image_vshader); 111 | free(source); 112 | 113 | glGetShaderiv(glsl->image_vshader, GL_COMPILE_STATUS, &action_ok); 114 | if (!action_ok) { 115 | fprintf(stderr, "Failed to compile image vshader :\n"); 116 | display_log(glsl->image_vshader, glGetShaderiv, glGetShaderInfoLog); 117 | return (1); 118 | } 119 | 120 | glsl->image_fshader = glCreateShader(GL_FRAGMENT_SHADER); 121 | source = strdup("#version 110 \n" 122 | "uniform sampler2D texture;" 123 | "varying vec2 texcoord;" 124 | "void main()" 125 | "{" 126 | " gl_FragColor = texture2D(texture, texcoord);" 127 | "}"); 128 | length = strlen(source); 129 | glShaderSource(glsl->image_fshader, 1, (const GLchar**)&source, &length); 130 | glCompileShader(glsl->image_fshader); 131 | free(source); 132 | 133 | glGetShaderiv(glsl->image_fshader, GL_COMPILE_STATUS, &action_ok); 134 | if (!action_ok) { 135 | fprintf(stderr, "Failed to compile image fshader :\n"); 136 | display_log(glsl->image_fshader, glGetShaderiv, glGetShaderInfoLog); 137 | return (1); 138 | } 139 | 140 | glsl->image_program = glCreateProgram(); 141 | glAttachShader(glsl->image_program, glsl->image_vshader); 142 | glAttachShader(glsl->image_program, glsl->image_fshader); 143 | glLinkProgram(glsl->image_program); 144 | 145 | glGetProgramiv(glsl->image_program, GL_LINK_STATUS, &action_ok); 146 | if (!action_ok) { 147 | fprintf(stderr, "Failed to link image shader program:\n"); 148 | display_log(glsl->image_program, glGetProgramiv, glGetProgramInfoLog); 149 | return (1); 150 | } 151 | 152 | glFlush(); 153 | 154 | return (0); 155 | } 156 | 157 | 158 | 159 | 160 | int mlx_shaders_font(glsl_info_t *glsl) 161 | { 162 | char *source; 163 | int length; 164 | GLint action_ok; 165 | 166 | glsl->font_vshader = glCreateShader(GL_VERTEX_SHADER); 167 | source = strdup("#version 110 \n" 168 | "attribute vec2 position;" 169 | "uniform vec2 winhalfsize;" 170 | "uniform vec2 fontposinwin;" 171 | "uniform vec2 fontposinatlas;" 172 | "uniform vec2 fontatlassize;" 173 | "varying vec2 texcoord;" 174 | "void main()" 175 | "{" 176 | #ifdef STRINGPUTX11 177 | " texcoord = (position * vec2(1.4, -1.4) + fontposinatlas ) / fontatlassize;" 178 | #else 179 | " texcoord = (position * vec2(1.0, -1.0) + fontposinatlas ) / fontatlassize;" 180 | #endif 181 | " vec2 pos = position - winhalfsize + fontposinwin;" 182 | " pos = pos / winhalfsize;" 183 | " gl_Position = vec4( pos, 0.0, 1.0);" 184 | "}"); 185 | length = strlen(source); 186 | glShaderSource(glsl->font_vshader, 1, (const GLchar**)&source, &length); 187 | glCompileShader(glsl->font_vshader); 188 | free(source); 189 | 190 | glGetShaderiv(glsl->font_vshader, GL_COMPILE_STATUS, &action_ok); 191 | if (!action_ok) { 192 | fprintf(stderr, "Failed to compile font vshader :\n"); 193 | display_log(glsl->font_vshader, glGetShaderiv, glGetShaderInfoLog); 194 | return (1); 195 | } 196 | 197 | glsl->font_fshader = glCreateShader(GL_FRAGMENT_SHADER); 198 | source = strdup("#version 110 \n" 199 | "uniform sampler2D texture;" 200 | "uniform vec4 color;" 201 | "varying vec2 texcoord;" 202 | "void main()" 203 | "{" 204 | " gl_FragColor = color * texture2D(texture, texcoord);" 205 | "}"); 206 | length = strlen(source); 207 | glShaderSource(glsl->font_fshader, 1, (const GLchar**)&source, &length); 208 | glCompileShader(glsl->font_fshader); 209 | free(source); 210 | 211 | glGetShaderiv(glsl->font_fshader, GL_COMPILE_STATUS, &action_ok); 212 | if (!action_ok) { 213 | fprintf(stderr, "Failed to compile font fshader :\n"); 214 | display_log(glsl->font_fshader, glGetShaderiv, glGetShaderInfoLog); 215 | return (1); 216 | } 217 | 218 | glsl->font_program = glCreateProgram(); 219 | glAttachShader(glsl->font_program, glsl->font_vshader); 220 | glAttachShader(glsl->font_program, glsl->font_fshader); 221 | glLinkProgram(glsl->font_program); 222 | 223 | glGetProgramiv(glsl->font_program, GL_LINK_STATUS, &action_ok); 224 | if (!action_ok) { 225 | fprintf(stderr, "Failed to link font shader program:\n"); 226 | display_log(glsl->font_program, glGetProgramiv, glGetProgramInfoLog); 227 | return (1); 228 | } 229 | 230 | glFlush(); 231 | 232 | return (0); 233 | } 234 | 235 | 236 | 237 | int mlx_shaders(glsl_info_t *glsl) 238 | { 239 | return (mlx_shaders_pixel(glsl) + mlx_shaders_image(glsl) + mlx_shaders_font(glsl)); 240 | } 241 | -------------------------------------------------------------------------------- /mlx/mlx_xpm.c: -------------------------------------------------------------------------------- 1 | // mlx xpm 2 | // by ol 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include "mlx_int.h" 12 | 13 | typedef struct s_xpm_col 14 | { 15 | int name; 16 | int col; 17 | } t_xpm_col; 18 | 19 | 20 | struct s_col_name 21 | { 22 | char *name; 23 | int color; 24 | }; 25 | 26 | //extern struct s_col_name mlx_col_name[]; 27 | #include "mlx_rgb.c" 28 | 29 | 30 | #define RETURN { if (colors) free(colors); if (tab) free(tab); \ 31 | if (colors_direct) free(colors_direct); \ 32 | if (img) mlx_destroy_image(xvar, img); \ 33 | return ((void *)0); } 34 | 35 | 36 | 37 | 38 | char *mlx_int_get_line(char *ptr,int *pos,int size) 39 | { 40 | int pos2; 41 | int pos3; 42 | int pos4; 43 | 44 | if ((pos2 = mlx_int_str_str(ptr+*pos,"\"",size-*pos))==-1) 45 | return ((char *)0); 46 | if ((pos3 = mlx_int_str_str(ptr+*pos+pos2+1,"\"",size-*pos-pos2-1))==-1) 47 | return ((char *)0); 48 | *(ptr+*pos+pos2) = 0; 49 | *(ptr+*pos+pos2+1+pos3) = 0; 50 | pos4 = *pos+pos2+1; 51 | *pos += pos2+pos3+2; 52 | return (ptr+pos4); 53 | } 54 | 55 | 56 | 57 | char *mlx_int_static_line(char **xpm_data,int *pos,int size) 58 | { 59 | static char *copy = 0; 60 | static int len = 0; 61 | int len2; 62 | char *str; 63 | 64 | str = xpm_data[(*pos)++]; 65 | if ((len2 = strlen(str))>len) 66 | { 67 | if (copy) 68 | free(copy); 69 | if (!(copy = malloc(len2+1))) 70 | return ((char *)0); 71 | len = len2; 72 | } 73 | /* strcpy(copy,str); */ 74 | strlcpy(copy, str, len2+1); 75 | return (copy); 76 | } 77 | 78 | 79 | int mlx_int_get_col_name(char *str,int size) 80 | { 81 | int result; 82 | 83 | result = 0; 84 | while (size--) 85 | result = (result<<8)+*(str++); 86 | return (result); 87 | } 88 | 89 | int mlx_int_get_text_rgb(char *name, char *end) 90 | { 91 | int i; 92 | char buff[64]; 93 | 94 | if (*name == '#') 95 | return (strtol(name+1,0,16)); 96 | if (end) 97 | { 98 | snprintf(buff, 64, "%s %s", name, end); 99 | name = buff; 100 | } 101 | i = 0; 102 | while (mlx_col_name[i].name) 103 | { 104 | if (!strcasecmp(mlx_col_name[i].name, name)) 105 | return (mlx_col_name[i].color); 106 | i ++; 107 | } 108 | return (0); 109 | } 110 | 111 | 112 | void mlx_int_xpm_set_pixel(mlx_img_list_t *img, char *data, int opp, int col, int x) 113 | { 114 | /* 115 | int dec; 116 | 117 | dec = opp; 118 | while (dec--) 119 | { 120 | if (img->image->byte_order) 121 | *(data+x*opp+dec) = col&0xFF; 122 | else 123 | *(data+x*opp+opp-dec-1) = col&0xFF; 124 | col >>= 8; 125 | } 126 | */ 127 | // opp is 4, do it the simple way 128 | *((unsigned int *)(data+4*x)) = col; 129 | } 130 | 131 | 132 | void *mlx_int_parse_xpm(mlx_ptr_t *xvar,void *info,int info_size,char *(*f)()) 133 | { 134 | int pos; 135 | char *line; 136 | char **tab; 137 | char *data; 138 | char *clip_data; 139 | int nc; 140 | int opp; 141 | int cpp; 142 | int col; 143 | int rgb_col; 144 | int col_name; 145 | int method; 146 | int x; 147 | int i; 148 | int j; 149 | mlx_img_list_t *img; 150 | t_xpm_col *colors; 151 | int *colors_direct; 152 | int width; 153 | int height; 154 | 155 | colors = 0; 156 | colors_direct = 0; 157 | img = 0; 158 | tab = 0; 159 | pos = 0; 160 | if (!(line = f(info,&pos,info_size)) || 161 | !(tab = mlx_int_str_to_wordtab(line)) || !(width = atoi(tab[0])) || 162 | !(height = atoi(tab[1])) || !(nc = atoi(tab[2])) || 163 | !(cpp = atoi(tab[3])) ) 164 | RETURN; 165 | free(tab); 166 | tab = 0; 167 | 168 | method = 0; 169 | if (cpp<=2) 170 | { 171 | method = 1; 172 | if (!(colors_direct = malloc((cpp==2?65536:256)*sizeof(int)))) 173 | RETURN; 174 | } 175 | else 176 | if (!(colors = malloc(nc*sizeof(*colors)))) 177 | RETURN; 178 | 179 | clip_data = 0; 180 | 181 | i = nc; 182 | while (i--) 183 | { 184 | if (!(line = f(info,&pos,info_size)) || 185 | !(tab = mlx_int_str_to_wordtab(line+cpp)) ) 186 | RETURN; 187 | j = 0; 188 | while (tab[j] && strcmp(tab[j++],"c")); 189 | 190 | if (!tab[j]) 191 | RETURN; 192 | 193 | rgb_col = mlx_int_get_text_rgb(tab[j], tab[j+1]); 194 | /* 195 | if ((rgb_col = mlx_int_get_text_rgb(tab[j], tab[j+1]))==-1) 196 | { 197 | if (!(clip_data = malloc(4*width*height)) || // ok, nice size .. 198 | !(clip_img = XCreateImage(xvar->display, xvar->visual, 199 | 1, XYPixmap, 0, clip_data, 200 | width, height, 8, (width+7)/8)) ) 201 | RETURN; 202 | memset(clip_data, 0xFF, 4*width*height); 203 | } 204 | */ 205 | if (method) 206 | colors_direct[mlx_int_get_col_name(line,cpp)] = rgb_col; 207 | // rgb_col>=0?mlx_get_color_value(xvar, rgb_col):rgb_col; 208 | else 209 | { 210 | colors[i].name = mlx_int_get_col_name(line,cpp); 211 | colors[i].col = rgb_col; // rgb_col>=0?mlx_get_color_value(xvar,rgb_col):rgb_col; 212 | } 213 | free(tab); 214 | tab = 0; 215 | } 216 | 217 | if (!(img = mlx_new_image(xvar,width,height))) 218 | RETURN; 219 | //opp = img->bpp/8; 220 | opp = 4; 221 | 222 | 223 | i = height; 224 | data = img->buffer; 225 | while (i--) 226 | { 227 | if (!(line = f(info,&pos,info_size))) 228 | RETURN; 229 | x = 0; 230 | while (xsize_line; 255 | data += img->width*4; 256 | } 257 | /* 258 | if (clip_data) 259 | { 260 | if (!(clip_pix = XCreatePixmap(xvar->display, xvar->root, 261 | width, height, 1)) ) 262 | RETURN; 263 | img->gc = XCreateGC(xvar->display, clip_pix, 0, &xgcv); 264 | XPutImage(xvar->display, clip_pix, img->gc, clip_img, 265 | 0, 0, 0, 0, width, height); 266 | XFreeGC(xvar->display, img->gc); 267 | xgcv.clip_mask = clip_pix; 268 | xgcv.function = GXcopy; 269 | xgcv.plane_mask = AllPlanes; 270 | img->gc = XCreateGC(xvar->display, xvar->root, GCClipMask|GCFunction| 271 | GCPlaneMask, &xgcv); 272 | XSync(xvar->display, False); 273 | XDestroyImage(clip_img); 274 | } 275 | */ 276 | if (colors) 277 | free(colors); 278 | if (colors_direct) 279 | free(colors_direct); 280 | return (img); 281 | } 282 | 283 | 284 | void mlx_int_file_get_rid_comment(char *ptr, int size) 285 | { 286 | int com_begin; 287 | int com_end; 288 | 289 | while ((com_begin = mlx_int_str_str_cote(ptr,"/*",size))!=-1) 290 | { 291 | com_end = mlx_int_str_str(ptr+com_begin+2,"*/",size-com_begin-2); 292 | memset(ptr+com_begin,' ',com_end+4); 293 | } 294 | while ((com_begin = mlx_int_str_str_cote(ptr,"//",size))!=-1) 295 | { 296 | com_end = mlx_int_str_str(ptr+com_begin+2,"\n",size-com_begin-2); 297 | memset(ptr+com_begin,' ',com_end+3); 298 | } 299 | } 300 | 301 | 302 | void *mlx_xpm_file_to_image(mlx_ptr_t *xvar,char *file,int *width,int *height) 303 | { 304 | int fd; 305 | int size; 306 | char *ptr; 307 | mlx_img_list_t *img; 308 | 309 | if ((fd = open(file,O_RDONLY))==-1 || (size = lseek(fd,0,SEEK_END))==-1 || 310 | (ptr = mmap(0,size,PROT_WRITE|PROT_READ,MAP_PRIVATE,fd,0))== 311 | (void *)MAP_FAILED) 312 | { 313 | if (fd>=0) 314 | close(fd); 315 | return ((void *)0); 316 | } 317 | mlx_int_file_get_rid_comment(ptr, size); 318 | if ((img = mlx_int_parse_xpm(xvar,ptr,size,mlx_int_get_line))) 319 | { 320 | *width = img->width; 321 | *height = img->height; 322 | } 323 | munmap(ptr,size); 324 | close(fd); 325 | return (img); 326 | } 327 | 328 | void *mlx_xpm_to_image(mlx_ptr_t *xvar,char **xpm_data,int *width,int *height) 329 | { 330 | mlx_img_list_t *img; 331 | 332 | if ((img = mlx_int_parse_xpm(xvar,xpm_data,0,mlx_int_static_line))) 333 | { 334 | *width = img->width; 335 | *height = img->height; 336 | } 337 | return (img); 338 | } 339 | -------------------------------------------------------------------------------- /mlx/mlx_png.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #include "zlib.h" 13 | 14 | #include 15 | #include "mlx_int.h" 16 | 17 | 18 | #define PNG_MAGIC_SIZE 8 19 | unsigned char magic[PNG_MAGIC_SIZE] = {137, 80, 78, 71, 13, 10, 26, 10}; 20 | #define PNG_HDR_SIZE 13 21 | 22 | #define Z_CHUNK 32768 23 | 24 | #define ERR_MAGIC_SIZE 1 25 | #define ERR_MAGIC_WRONG 2 26 | #define ERR_STRUCT_INCOMPLETE 3 27 | #define ERR_STRUCT_HDR 4 28 | #define ERR_STRUCT_END 5 29 | #define ERR_STRUCT_CRC 6 30 | #define ERR_STRUCT_INCIMPL 7 31 | #define ERR_STRUCT_DAT 8 32 | #define ERR_STRUCT_MISSCHK 9 33 | #define ERR_ZLIB 10 34 | #define ERR_DATA_MISMATCH 11 35 | #define ERR_DATA_FILTER 12 36 | char *(mipng_err[]) = 37 | { 38 | "No error", 39 | "Not enough size for magic", 40 | "Wrong magic", 41 | "Incomplete chunk structure", 42 | "Duplicate or incorrect header", 43 | "Duplicate or incorrect end", 44 | "Invalid CRC in chunk", 45 | "Incorrect header or configuration not implemented", 46 | "Non consecutive dat chunks", 47 | "Missing header/dat/end chunk(s)", 48 | "Zlib inflate error", 49 | "Inflated data size mismatch", 50 | "Unknown scanline filter" 51 | }; 52 | 53 | typedef struct png_info_s 54 | { 55 | unsigned int width; 56 | unsigned int height; 57 | int depth; 58 | int color; 59 | int interlace; 60 | int bpp; 61 | } png_info_t; 62 | 63 | 64 | int mipng_is_type(unsigned char *ptr, char *type) 65 | { 66 | if (ptr[4] == type[0] && ptr[5] == type[1] && ptr[6] == type[2] && ptr[7] == type[3]) 67 | return (1); 68 | return (0); 69 | } 70 | 71 | 72 | unsigned char mipng_defilter_none(unsigned char *buff, int pos, int a, int b, int c) 73 | { return (buff[pos]); } 74 | unsigned char mipng_defilter_sub(unsigned char *buff, int pos, int a, int b, int c) 75 | { return (buff[pos]+(unsigned int)a); } 76 | unsigned char mipng_defilter_up(unsigned char *buff, int pos, int a, int b, int c) 77 | { return (buff[pos]+(unsigned int)b); } 78 | unsigned char mipng_defilter_average(unsigned char *buff, int pos, int a, int b, int c) 79 | { return (buff[pos]+((unsigned int)a+(unsigned int)b)/2); } 80 | unsigned char mipng_defilter_paeth(unsigned char *buff, int pos, int a, int b, int c) 81 | { 82 | int p; 83 | int result; 84 | 85 | p = a + b - c; 86 | if (abs(b - c) <= abs(a - c) && abs(b - c) <= abs(a + b - c - c)) 87 | result = a; 88 | else 89 | if (abs(a - c) <= abs(a + b - c - c)) 90 | result = b; 91 | else 92 | result = c; 93 | return (buff[pos]+result); 94 | } 95 | 96 | 97 | 98 | unsigned char (*(mipng_defilter[]))(unsigned char *buff, int pos, int a, int b, int c) = 99 | { 100 | mipng_defilter_none, 101 | mipng_defilter_sub, 102 | mipng_defilter_up, 103 | mipng_defilter_average, 104 | mipng_defilter_paeth 105 | }; 106 | 107 | // only work for mlx mac or img 32bpp 108 | int mipng_fill_img(mlx_img_list_t *img, unsigned char *buf, png_info_t *pi) 109 | { 110 | unsigned int current_filter; 111 | int ipos; 112 | int bpos; 113 | int ilen; 114 | int iline; 115 | int blen; 116 | unsigned char tmp; 117 | unsigned char *ibuf; 118 | 119 | ibuf = (unsigned char *)img->buffer; 120 | iline = img->width * UNIQ_BPP; 121 | ilen = img->width * img->height * UNIQ_BPP; 122 | blen = img->width * img->height * pi->bpp + img->height; 123 | ipos = 0; 124 | bpos = 0; 125 | while (ipos < ilen && bpos < blen) 126 | { 127 | if ((ipos % iline) == 0) 128 | { 129 | if ((current_filter = buf[bpos++]) > 4) 130 | return (ERR_DATA_FILTER); 131 | } 132 | ibuf[ipos] = mipng_defilter[current_filter](buf, bpos, 133 | ipos%iline>3?ibuf[ipos-UNIQ_BPP]:0, 134 | (ipos>=iline)?ibuf[ipos-iline]:0, 135 | (ipos>=iline && ipos%iline>3)?ibuf[ipos-iline-UNIQ_BPP]:0); 136 | ipos ++; 137 | bpos ++; 138 | if (pi->depth == 16) 139 | bpos ++; 140 | if (ipos % 4 == 3 && pi->color == 2) // no alpha 141 | img->buffer[ipos++] = 0xFF; 142 | } 143 | if (ipos != ilen || bpos != blen) 144 | { 145 | // printf("fill err ipos %d vs %d, bpos %d vs %d\n", ipos, ilen, bpos, blen); 146 | return (ERR_DATA_MISMATCH); 147 | } 148 | ipos = 0; 149 | while (ipos < ilen) 150 | { 151 | tmp = ibuf[ipos]; 152 | ibuf[ipos] = ibuf[ipos+2]; 153 | ibuf[ipos+2] = tmp; 154 | ibuf[ipos+3] = 0xFF - ibuf[ipos+3]; 155 | ipos += UNIQ_BPP; 156 | } 157 | return (0); 158 | } 159 | 160 | 161 | int mipng_data(mlx_img_list_t *img, unsigned char *dat, png_info_t *pi) 162 | { 163 | unsigned int len; 164 | int b_pos; 165 | unsigned char *buffer; 166 | int ret; 167 | int z_ret; 168 | unsigned z_have; 169 | z_stream z_strm; 170 | unsigned char z_out[Z_CHUNK]; 171 | 172 | b_pos = 0; 173 | if (!(buffer = malloc((long long)img->width*(long long)img->height*(long long)pi->bpp + img->height))) 174 | err(1, "Can't malloc"); 175 | z_strm.zalloc = Z_NULL; 176 | z_strm.zfree = Z_NULL; 177 | z_strm.opaque = Z_NULL; 178 | z_strm.avail_in = 0; 179 | z_strm.next_in = Z_NULL; 180 | z_ret = inflateInit(&z_strm); 181 | if (z_ret != Z_OK) 182 | return (ERR_ZLIB); 183 | 184 | while (mipng_is_type(dat, "IDAT")) 185 | { 186 | len = *((unsigned int *)dat); 187 | len = ntohl(len); 188 | z_strm.avail_in = len; 189 | z_strm.next_in = dat + 8; 190 | z_strm.avail_out = 0; 191 | while (z_strm.avail_out == 0) 192 | { 193 | z_strm.avail_out = Z_CHUNK; 194 | z_strm.next_out = z_out; 195 | z_ret = inflate(&z_strm, Z_NO_FLUSH); 196 | // printf("inflate ret %d avail_out %d\n", z_ret, z_strm.avail_out); 197 | if (z_ret != Z_OK && z_ret != Z_STREAM_END) 198 | { 199 | inflateEnd(&z_strm); 200 | return (ERR_ZLIB); 201 | } 202 | if (b_pos + Z_CHUNK - z_strm.avail_out > img->width*img->height*pi->bpp+img->height) 203 | { 204 | inflateEnd(&z_strm); 205 | return (ERR_DATA_MISMATCH); 206 | } 207 | bcopy(z_out, buffer+b_pos, Z_CHUNK - z_strm.avail_out); 208 | b_pos += Z_CHUNK - z_strm.avail_out; 209 | } 210 | dat += len + 4 + 4 + 4; 211 | } 212 | inflateEnd(&z_strm); 213 | if (b_pos != img->width*img->height*pi->bpp+img->height) 214 | { 215 | // printf("pb : bpos %d vs expected %d\n", b_pos, img->width*img->height*pi->bpp+img->height); 216 | return (ERR_DATA_MISMATCH); 217 | } 218 | if ((ret = mipng_fill_img(img, buffer, pi))) 219 | return (ret); 220 | return (0); 221 | } 222 | 223 | 224 | 225 | int mipng_magic(unsigned char *ptr, int size) 226 | { 227 | int i; 228 | 229 | if (size < PNG_MAGIC_SIZE) 230 | return (ERR_MAGIC_SIZE); 231 | i = 0; 232 | while (i < PNG_MAGIC_SIZE) 233 | if (*(ptr++) != magic[i++]) 234 | return (ERR_MAGIC_WRONG); 235 | return (0); 236 | } 237 | 238 | 239 | unsigned long crc_table[256] = { 0, 0x77073096, 0xee0e612c, 0x990951ba, 0x76dc419, 0x706af48f, 0xe963a535, 0x9e6495a3, 0xedb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x9b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x1db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x6b6b51f, 0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0xf00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x86d3d2d, 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x3b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x4db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, 0xd6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0xa00ae27, 0x7d079eb1, 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x26d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x5005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0xcb61b38, 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0xbdbdf21, 0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d }; 240 | 241 | // From http://www.w3.org/TR/PNG/#D-CRCAppendix 242 | int mipng_crc(unsigned char *ptr, int len) 243 | { 244 | unsigned int file_crc; 245 | unsigned long crc; 246 | int i; 247 | 248 | file_crc = *((unsigned int *)(ptr+4+4+len)); 249 | file_crc = ntohl(file_crc); 250 | 251 | crc = 0xffffffffL; 252 | i = 0; 253 | while (i < len+4) 254 | crc = crc_table[(crc ^ ptr[(i++)+4]) & 0xff] ^ (crc >> 8); 255 | crc ^= 0xffffffffL; 256 | 257 | if (file_crc != crc) 258 | return (1); 259 | return (0); 260 | } 261 | 262 | 263 | int mipng_structure(unsigned char *ptr, int size, unsigned char **hdr, unsigned char **dat) 264 | { 265 | unsigned int len; 266 | int dat_state; 267 | int end; 268 | 269 | dat_state = 0; 270 | *hdr = NULL; 271 | *dat = NULL; 272 | end = 0; 273 | while (size) 274 | { 275 | if (size >= 4) // length present 276 | { 277 | len = *((unsigned int *)ptr); 278 | len = ntohl(len); 279 | if (size < 4 + 4 + 4 + len) 280 | return (ERR_STRUCT_INCOMPLETE); 281 | if (mipng_crc(ptr, len)) 282 | return (ERR_STRUCT_CRC); 283 | // printf("found chunk len %d type %c%c%c%c\n", len, *(ptr+4), *(ptr+5), *(ptr+6), *(ptr+7)); 284 | if (mipng_is_type(ptr, "IHDR")) 285 | { 286 | if (*hdr || len != PNG_HDR_SIZE) 287 | return (ERR_STRUCT_HDR); 288 | *hdr = ptr; 289 | } 290 | if (mipng_is_type(ptr, "IEND")) 291 | { 292 | if (len != 0 || size != 4+4+4) 293 | return (ERR_STRUCT_END); 294 | end = 1; 295 | } 296 | if (mipng_is_type(ptr, "IDAT")) 297 | { 298 | if (dat_state == 0) 299 | { 300 | dat_state = 1; 301 | *dat = ptr; 302 | } 303 | if (dat_state == 2) 304 | return (ERR_STRUCT_DAT); 305 | } 306 | else 307 | if (dat_state == 1) 308 | dat_state = 2; 309 | size -= 4+4+4+len; 310 | ptr += 4+4+4+len; 311 | } 312 | else 313 | return (ERR_STRUCT_INCOMPLETE); 314 | } 315 | if (*hdr == 0 || *dat == 0 || end == 0) 316 | return (ERR_STRUCT_MISSCHK); 317 | return (0); 318 | } 319 | 320 | 321 | int mipng_verif_hdr(unsigned char *hdr, png_info_t *pi) 322 | { 323 | unsigned int compress; 324 | unsigned int filter; 325 | 326 | hdr += 8; 327 | pi->width = ntohl(*((unsigned long *)hdr)); 328 | pi->height = ntohl(*((unsigned long *)(hdr+4))); 329 | pi->depth = *(hdr+8); 330 | pi->color = *(hdr+9); 331 | compress = *(hdr+10); 332 | filter = *(hdr+11); 333 | pi->interlace = *(hdr+12); 334 | if (pi->width <= 0 || pi->height <= 0 || (pi->depth != 8 && pi->depth != 16) 335 | || (pi->color != 2 && pi->color != 6) || compress != 0 || filter != 0 || pi->interlace != 0) 336 | return (ERR_STRUCT_INCIMPL); 337 | pi->bpp = pi->depth / 8; 338 | if (pi->color == 2) 339 | pi->bpp *= 3; 340 | if (pi->color == 6) 341 | pi->bpp *= 4; 342 | // printf("hdr info : %d x %d, depth %d, col type %d, comp %d, filter %d, interlace %d\nbpp is %d\n", 343 | // pi->width, pi->height, pi->depth, pi->color, compress, filter, pi->interlace, pi->bpp); 344 | return (0); 345 | } 346 | 347 | 348 | mlx_img_list_t *mlx_int_parse_png(mlx_ptr_t *xvar, unsigned char *fptr, int size) 349 | { 350 | int err; 351 | unsigned char *hdr; 352 | unsigned char *dat; 353 | png_info_t pi; 354 | mlx_img_list_t *img; 355 | 356 | if ((err = mipng_magic(fptr, size))) 357 | { 358 | warnx("mlx PNG error : %s", mipng_err[err]); 359 | return ((mlx_img_list_t *)0); 360 | } 361 | fptr += PNG_MAGIC_SIZE; 362 | size -= PNG_MAGIC_SIZE; 363 | if ((err = mipng_structure(fptr, size, &hdr, &dat))) 364 | { 365 | warnx("mlx PNG error : %s", mipng_err[err]); 366 | return ((mlx_img_list_t *)0); 367 | } 368 | if ((err = mipng_verif_hdr(hdr, &pi))) 369 | { 370 | warnx("mlx PNG error : %s", mipng_err[err]); 371 | return ((mlx_img_list_t *)0); 372 | } 373 | if (!(img = mlx_new_image(xvar, pi.width, pi.height))) 374 | { 375 | warnx("mlx PNG error : Can't create mlx image"); 376 | return ((mlx_img_list_t *)0); 377 | } 378 | if ((err = mipng_data(img, dat, &pi))) 379 | { 380 | mlx_destroy_image(xvar, img); 381 | warnx("mlx PNG error : %s", mipng_err[err]); 382 | return ((mlx_img_list_t *)0); 383 | } 384 | return (img); 385 | } 386 | 387 | 388 | 389 | 390 | void *mlx_png_file_to_image(mlx_ptr_t *xvar, char *file, int *width, int *height) 391 | { 392 | int fd; 393 | int size; 394 | unsigned char *ptr; 395 | mlx_img_list_t *img; 396 | 397 | if ((fd = open(file, O_RDONLY)) == -1 || (size = lseek(fd, 0, SEEK_END)) == -1 || 398 | (ptr = mmap(0, size, PROT_READ, MAP_PRIVATE, fd, 0)) == (void *)MAP_FAILED) 399 | { 400 | if (fd >= 0) 401 | close(fd); 402 | warnx("Can't map png file '%s'", file); 403 | return ((void *)0); 404 | } 405 | if ((img = mlx_int_parse_png(xvar, ptr, size))) 406 | { 407 | *width = img->width; 408 | *height = img->height; 409 | } 410 | else 411 | { 412 | *width = 0; 413 | *height = 0; 414 | } 415 | munmap(ptr,size); 416 | close(fd); 417 | return (img); 418 | } 419 | -------------------------------------------------------------------------------- /mlx/mlx_rgb.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** This is a generated file with rgb2c.pl and rgb.txt from 3 | ** the XFree86 distribution. 4 | */ 5 | 6 | 7 | struct s_col_name mlx_col_name[] = 8 | { 9 | { "snow" , 0xfffafa }, 10 | { "ghost white" , 0xf8f8ff }, 11 | { "ghostwhite" , 0xf8f8ff }, 12 | { "white smoke" , 0xf5f5f5 }, 13 | { "whitesmoke" , 0xf5f5f5 }, 14 | { "gainsboro" , 0xdcdcdc }, 15 | { "floral white" , 0xfffaf0 }, 16 | { "floralwhite" , 0xfffaf0 }, 17 | { "old lace" , 0xfdf5e6 }, 18 | { "oldlace" , 0xfdf5e6 }, 19 | { "linen" , 0xfaf0e6 }, 20 | { "antique white" , 0xfaebd7 }, 21 | { "antiquewhite" , 0xfaebd7 }, 22 | { "papaya whip" , 0xffefd5 }, 23 | { "papayawhip" , 0xffefd5 }, 24 | { "blanched almond" , 0xffebcd }, 25 | { "blanchedalmond" , 0xffebcd }, 26 | { "bisque" , 0xffe4c4 }, 27 | { "peach puff" , 0xffdab9 }, 28 | { "peachpuff" , 0xffdab9 }, 29 | { "navajo white" , 0xffdead }, 30 | { "navajowhite" , 0xffdead }, 31 | { "moccasin" , 0xffe4b5 }, 32 | { "cornsilk" , 0xfff8dc }, 33 | { "ivory" , 0xfffff0 }, 34 | { "lemon chiffon" , 0xfffacd }, 35 | { "lemonchiffon" , 0xfffacd }, 36 | { "seashell" , 0xfff5ee }, 37 | { "honeydew" , 0xf0fff0 }, 38 | { "mint cream" , 0xf5fffa }, 39 | { "mintcream" , 0xf5fffa }, 40 | { "azure" , 0xf0ffff }, 41 | { "alice blue" , 0xf0f8ff }, 42 | { "aliceblue" , 0xf0f8ff }, 43 | { "lavender" , 0xe6e6fa }, 44 | { "lavender blush" , 0xfff0f5 }, 45 | { "lavenderblush" , 0xfff0f5 }, 46 | { "misty rose" , 0xffe4e1 }, 47 | { "mistyrose" , 0xffe4e1 }, 48 | { "white" , 0xffffff }, 49 | { "black" , 0x0 }, 50 | { "dark slate" , 0x2f4f4f }, 51 | { "darkslategray" , 0x2f4f4f }, 52 | { "dark slate" , 0x2f4f4f }, 53 | { "darkslategrey" , 0x2f4f4f }, 54 | { "dim gray" , 0x696969 }, 55 | { "dimgray" , 0x696969 }, 56 | { "dim grey" , 0x696969 }, 57 | { "dimgrey" , 0x696969 }, 58 | { "slate gray" , 0x708090 }, 59 | { "slategray" , 0x708090 }, 60 | { "slate grey" , 0x708090 }, 61 | { "slategrey" , 0x708090 }, 62 | { "light slate" , 0x778899 }, 63 | { "lightslategray" , 0x778899 }, 64 | { "light slate" , 0x778899 }, 65 | { "lightslategrey" , 0x778899 }, 66 | { "gray" , 0xbebebe }, 67 | { "grey" , 0xbebebe }, 68 | { "light grey" , 0xd3d3d3 }, 69 | { "lightgrey" , 0xd3d3d3 }, 70 | { "light gray" , 0xd3d3d3 }, 71 | { "lightgray" , 0xd3d3d3 }, 72 | { "midnight blue" , 0x191970 }, 73 | { "midnightblue" , 0x191970 }, 74 | { "navy" , 0x80 }, 75 | { "navy blue" , 0x80 }, 76 | { "navyblue" , 0x80 }, 77 | { "cornflower blue" , 0x6495ed }, 78 | { "cornflowerblue" , 0x6495ed }, 79 | { "dark slate" , 0x483d8b }, 80 | { "darkslateblue" , 0x483d8b }, 81 | { "slate blue" , 0x6a5acd }, 82 | { "slateblue" , 0x6a5acd }, 83 | { "medium slate" , 0x7b68ee }, 84 | { "mediumslateblue" , 0x7b68ee }, 85 | { "light slate" , 0x8470ff }, 86 | { "lightslateblue" , 0x8470ff }, 87 | { "medium blue" , 0xcd }, 88 | { "mediumblue" , 0xcd }, 89 | { "royal blue" , 0x4169e1 }, 90 | { "royalblue" , 0x4169e1 }, 91 | { "blue" , 0xff }, 92 | { "dodger blue" , 0x1e90ff }, 93 | { "dodgerblue" , 0x1e90ff }, 94 | { "deep sky" , 0xbfff }, 95 | { "deepskyblue" , 0xbfff }, 96 | { "sky blue" , 0x87ceeb }, 97 | { "skyblue" , 0x87ceeb }, 98 | { "light sky" , 0x87cefa }, 99 | { "lightskyblue" , 0x87cefa }, 100 | { "steel blue" , 0x4682b4 }, 101 | { "steelblue" , 0x4682b4 }, 102 | { "light steel" , 0xb0c4de }, 103 | { "lightsteelblue" , 0xb0c4de }, 104 | { "light blue" , 0xadd8e6 }, 105 | { "lightblue" , 0xadd8e6 }, 106 | { "powder blue" , 0xb0e0e6 }, 107 | { "powderblue" , 0xb0e0e6 }, 108 | { "pale turquoise" , 0xafeeee }, 109 | { "paleturquoise" , 0xafeeee }, 110 | { "dark turquoise" , 0xced1 }, 111 | { "darkturquoise" , 0xced1 }, 112 | { "medium turquoise" , 0x48d1cc }, 113 | { "mediumturquoise" , 0x48d1cc }, 114 | { "turquoise" , 0x40e0d0 }, 115 | { "cyan" , 0xffff }, 116 | { "light cyan" , 0xe0ffff }, 117 | { "lightcyan" , 0xe0ffff }, 118 | { "cadet blue" , 0x5f9ea0 }, 119 | { "cadetblue" , 0x5f9ea0 }, 120 | { "medium aquamarine" , 0x66cdaa }, 121 | { "mediumaquamarine" , 0x66cdaa }, 122 | { "aquamarine" , 0x7fffd4 }, 123 | { "dark green" , 0x6400 }, 124 | { "darkgreen" , 0x6400 }, 125 | { "dark olive" , 0x556b2f }, 126 | { "darkolivegreen" , 0x556b2f }, 127 | { "dark sea" , 0x8fbc8f }, 128 | { "darkseagreen" , 0x8fbc8f }, 129 | { "sea green" , 0x2e8b57 }, 130 | { "seagreen" , 0x2e8b57 }, 131 | { "medium sea" , 0x3cb371 }, 132 | { "mediumseagreen" , 0x3cb371 }, 133 | { "light sea" , 0x20b2aa }, 134 | { "lightseagreen" , 0x20b2aa }, 135 | { "pale green" , 0x98fb98 }, 136 | { "palegreen" , 0x98fb98 }, 137 | { "spring green" , 0xff7f }, 138 | { "springgreen" , 0xff7f }, 139 | { "lawn green" , 0x7cfc00 }, 140 | { "lawngreen" , 0x7cfc00 }, 141 | { "green" , 0xff00 }, 142 | { "chartreuse" , 0x7fff00 }, 143 | { "medium spring" , 0xfa9a }, 144 | { "mediumspringgreen" , 0xfa9a }, 145 | { "green yellow" , 0xadff2f }, 146 | { "greenyellow" , 0xadff2f }, 147 | { "lime green" , 0x32cd32 }, 148 | { "limegreen" , 0x32cd32 }, 149 | { "yellow green" , 0x9acd32 }, 150 | { "yellowgreen" , 0x9acd32 }, 151 | { "forest green" , 0x228b22 }, 152 | { "forestgreen" , 0x228b22 }, 153 | { "olive drab" , 0x6b8e23 }, 154 | { "olivedrab" , 0x6b8e23 }, 155 | { "dark khaki" , 0xbdb76b }, 156 | { "darkkhaki" , 0xbdb76b }, 157 | { "khaki" , 0xf0e68c }, 158 | { "pale goldenrod" , 0xeee8aa }, 159 | { "palegoldenrod" , 0xeee8aa }, 160 | { "light goldenrod" , 0xfafad2 }, 161 | { "lightgoldenrodyellow" , 0xfafad2 }, 162 | { "light yellow" , 0xffffe0 }, 163 | { "lightyellow" , 0xffffe0 }, 164 | { "yellow" , 0xffff00 }, 165 | { "gold" , 0xffd700 }, 166 | { "light goldenrod" , 0xeedd82 }, 167 | { "lightgoldenrod" , 0xeedd82 }, 168 | { "goldenrod" , 0xdaa520 }, 169 | { "dark goldenrod" , 0xb8860b }, 170 | { "darkgoldenrod" , 0xb8860b }, 171 | { "rosy brown" , 0xbc8f8f }, 172 | { "rosybrown" , 0xbc8f8f }, 173 | { "indian red" , 0xcd5c5c }, 174 | { "indianred" , 0xcd5c5c }, 175 | { "saddle brown" , 0x8b4513 }, 176 | { "saddlebrown" , 0x8b4513 }, 177 | { "sienna" , 0xa0522d }, 178 | { "peru" , 0xcd853f }, 179 | { "burlywood" , 0xdeb887 }, 180 | { "beige" , 0xf5f5dc }, 181 | { "wheat" , 0xf5deb3 }, 182 | { "sandy brown" , 0xf4a460 }, 183 | { "sandybrown" , 0xf4a460 }, 184 | { "tan" , 0xd2b48c }, 185 | { "chocolate" , 0xd2691e }, 186 | { "firebrick" , 0xb22222 }, 187 | { "brown" , 0xa52a2a }, 188 | { "dark salmon" , 0xe9967a }, 189 | { "darksalmon" , 0xe9967a }, 190 | { "salmon" , 0xfa8072 }, 191 | { "light salmon" , 0xffa07a }, 192 | { "lightsalmon" , 0xffa07a }, 193 | { "orange" , 0xffa500 }, 194 | { "dark orange" , 0xff8c00 }, 195 | { "darkorange" , 0xff8c00 }, 196 | { "coral" , 0xff7f50 }, 197 | { "light coral" , 0xf08080 }, 198 | { "lightcoral" , 0xf08080 }, 199 | { "tomato" , 0xff6347 }, 200 | { "orange red" , 0xff4500 }, 201 | { "orangered" , 0xff4500 }, 202 | { "red" , 0xff0000 }, 203 | { "hot pink" , 0xff69b4 }, 204 | { "hotpink" , 0xff69b4 }, 205 | { "deep pink" , 0xff1493 }, 206 | { "deeppink" , 0xff1493 }, 207 | { "pink" , 0xffc0cb }, 208 | { "light pink" , 0xffb6c1 }, 209 | { "lightpink" , 0xffb6c1 }, 210 | { "pale violet" , 0xdb7093 }, 211 | { "palevioletred" , 0xdb7093 }, 212 | { "maroon" , 0xb03060 }, 213 | { "medium violet" , 0xc71585 }, 214 | { "mediumvioletred" , 0xc71585 }, 215 | { "violet red" , 0xd02090 }, 216 | { "violetred" , 0xd02090 }, 217 | { "magenta" , 0xff00ff }, 218 | { "violet" , 0xee82ee }, 219 | { "plum" , 0xdda0dd }, 220 | { "orchid" , 0xda70d6 }, 221 | { "medium orchid" , 0xba55d3 }, 222 | { "mediumorchid" , 0xba55d3 }, 223 | { "dark orchid" , 0x9932cc }, 224 | { "darkorchid" , 0x9932cc }, 225 | { "dark violet" , 0x9400d3 }, 226 | { "darkviolet" , 0x9400d3 }, 227 | { "blue violet" , 0x8a2be2 }, 228 | { "blueviolet" , 0x8a2be2 }, 229 | { "purple" , 0xa020f0 }, 230 | { "medium purple" , 0x9370db }, 231 | { "mediumpurple" , 0x9370db }, 232 | { "thistle" , 0xd8bfd8 }, 233 | { "snow1" , 0xfffafa }, 234 | { "snow2" , 0xeee9e9 }, 235 | { "snow3" , 0xcdc9c9 }, 236 | { "snow4" , 0x8b8989 }, 237 | { "seashell1" , 0xfff5ee }, 238 | { "seashell2" , 0xeee5de }, 239 | { "seashell3" , 0xcdc5bf }, 240 | { "seashell4" , 0x8b8682 }, 241 | { "antiquewhite1" , 0xffefdb }, 242 | { "antiquewhite2" , 0xeedfcc }, 243 | { "antiquewhite3" , 0xcdc0b0 }, 244 | { "antiquewhite4" , 0x8b8378 }, 245 | { "bisque1" , 0xffe4c4 }, 246 | { "bisque2" , 0xeed5b7 }, 247 | { "bisque3" , 0xcdb79e }, 248 | { "bisque4" , 0x8b7d6b }, 249 | { "peachpuff1" , 0xffdab9 }, 250 | { "peachpuff2" , 0xeecbad }, 251 | { "peachpuff3" , 0xcdaf95 }, 252 | { "peachpuff4" , 0x8b7765 }, 253 | { "navajowhite1" , 0xffdead }, 254 | { "navajowhite2" , 0xeecfa1 }, 255 | { "navajowhite3" , 0xcdb38b }, 256 | { "navajowhite4" , 0x8b795e }, 257 | { "lemonchiffon1" , 0xfffacd }, 258 | { "lemonchiffon2" , 0xeee9bf }, 259 | { "lemonchiffon3" , 0xcdc9a5 }, 260 | { "lemonchiffon4" , 0x8b8970 }, 261 | { "cornsilk1" , 0xfff8dc }, 262 | { "cornsilk2" , 0xeee8cd }, 263 | { "cornsilk3" , 0xcdc8b1 }, 264 | { "cornsilk4" , 0x8b8878 }, 265 | { "ivory1" , 0xfffff0 }, 266 | { "ivory2" , 0xeeeee0 }, 267 | { "ivory3" , 0xcdcdc1 }, 268 | { "ivory4" , 0x8b8b83 }, 269 | { "honeydew1" , 0xf0fff0 }, 270 | { "honeydew2" , 0xe0eee0 }, 271 | { "honeydew3" , 0xc1cdc1 }, 272 | { "honeydew4" , 0x838b83 }, 273 | { "lavenderblush1" , 0xfff0f5 }, 274 | { "lavenderblush2" , 0xeee0e5 }, 275 | { "lavenderblush3" , 0xcdc1c5 }, 276 | { "lavenderblush4" , 0x8b8386 }, 277 | { "mistyrose1" , 0xffe4e1 }, 278 | { "mistyrose2" , 0xeed5d2 }, 279 | { "mistyrose3" , 0xcdb7b5 }, 280 | { "mistyrose4" , 0x8b7d7b }, 281 | { "azure1" , 0xf0ffff }, 282 | { "azure2" , 0xe0eeee }, 283 | { "azure3" , 0xc1cdcd }, 284 | { "azure4" , 0x838b8b }, 285 | { "slateblue1" , 0x836fff }, 286 | { "slateblue2" , 0x7a67ee }, 287 | { "slateblue3" , 0x6959cd }, 288 | { "slateblue4" , 0x473c8b }, 289 | { "royalblue1" , 0x4876ff }, 290 | { "royalblue2" , 0x436eee }, 291 | { "royalblue3" , 0x3a5fcd }, 292 | { "royalblue4" , 0x27408b }, 293 | { "blue1" , 0xff }, 294 | { "blue2" , 0xee }, 295 | { "blue3" , 0xcd }, 296 | { "blue4" , 0x8b }, 297 | { "dodgerblue1" , 0x1e90ff }, 298 | { "dodgerblue2" , 0x1c86ee }, 299 | { "dodgerblue3" , 0x1874cd }, 300 | { "dodgerblue4" , 0x104e8b }, 301 | { "steelblue1" , 0x63b8ff }, 302 | { "steelblue2" , 0x5cacee }, 303 | { "steelblue3" , 0x4f94cd }, 304 | { "steelblue4" , 0x36648b }, 305 | { "deepskyblue1" , 0xbfff }, 306 | { "deepskyblue2" , 0xb2ee }, 307 | { "deepskyblue3" , 0x9acd }, 308 | { "deepskyblue4" , 0x688b }, 309 | { "skyblue1" , 0x87ceff }, 310 | { "skyblue2" , 0x7ec0ee }, 311 | { "skyblue3" , 0x6ca6cd }, 312 | { "skyblue4" , 0x4a708b }, 313 | { "lightskyblue1" , 0xb0e2ff }, 314 | { "lightskyblue2" , 0xa4d3ee }, 315 | { "lightskyblue3" , 0x8db6cd }, 316 | { "lightskyblue4" , 0x607b8b }, 317 | { "slategray1" , 0xc6e2ff }, 318 | { "slategray2" , 0xb9d3ee }, 319 | { "slategray3" , 0x9fb6cd }, 320 | { "slategray4" , 0x6c7b8b }, 321 | { "lightsteelblue1" , 0xcae1ff }, 322 | { "lightsteelblue2" , 0xbcd2ee }, 323 | { "lightsteelblue3" , 0xa2b5cd }, 324 | { "lightsteelblue4" , 0x6e7b8b }, 325 | { "lightblue1" , 0xbfefff }, 326 | { "lightblue2" , 0xb2dfee }, 327 | { "lightblue3" , 0x9ac0cd }, 328 | { "lightblue4" , 0x68838b }, 329 | { "lightcyan1" , 0xe0ffff }, 330 | { "lightcyan2" , 0xd1eeee }, 331 | { "lightcyan3" , 0xb4cdcd }, 332 | { "lightcyan4" , 0x7a8b8b }, 333 | { "paleturquoise1" , 0xbbffff }, 334 | { "paleturquoise2" , 0xaeeeee }, 335 | { "paleturquoise3" , 0x96cdcd }, 336 | { "paleturquoise4" , 0x668b8b }, 337 | { "cadetblue1" , 0x98f5ff }, 338 | { "cadetblue2" , 0x8ee5ee }, 339 | { "cadetblue3" , 0x7ac5cd }, 340 | { "cadetblue4" , 0x53868b }, 341 | { "turquoise1" , 0xf5ff }, 342 | { "turquoise2" , 0xe5ee }, 343 | { "turquoise3" , 0xc5cd }, 344 | { "turquoise4" , 0x868b }, 345 | { "cyan1" , 0xffff }, 346 | { "cyan2" , 0xeeee }, 347 | { "cyan3" , 0xcdcd }, 348 | { "cyan4" , 0x8b8b }, 349 | { "darkslategray1" , 0x97ffff }, 350 | { "darkslategray2" , 0x8deeee }, 351 | { "darkslategray3" , 0x79cdcd }, 352 | { "darkslategray4" , 0x528b8b }, 353 | { "aquamarine1" , 0x7fffd4 }, 354 | { "aquamarine2" , 0x76eec6 }, 355 | { "aquamarine3" , 0x66cdaa }, 356 | { "aquamarine4" , 0x458b74 }, 357 | { "darkseagreen1" , 0xc1ffc1 }, 358 | { "darkseagreen2" , 0xb4eeb4 }, 359 | { "darkseagreen3" , 0x9bcd9b }, 360 | { "darkseagreen4" , 0x698b69 }, 361 | { "seagreen1" , 0x54ff9f }, 362 | { "seagreen2" , 0x4eee94 }, 363 | { "seagreen3" , 0x43cd80 }, 364 | { "seagreen4" , 0x2e8b57 }, 365 | { "palegreen1" , 0x9aff9a }, 366 | { "palegreen2" , 0x90ee90 }, 367 | { "palegreen3" , 0x7ccd7c }, 368 | { "palegreen4" , 0x548b54 }, 369 | { "springgreen1" , 0xff7f }, 370 | { "springgreen2" , 0xee76 }, 371 | { "springgreen3" , 0xcd66 }, 372 | { "springgreen4" , 0x8b45 }, 373 | { "green1" , 0xff00 }, 374 | { "green2" , 0xee00 }, 375 | { "green3" , 0xcd00 }, 376 | { "green4" , 0x8b00 }, 377 | { "chartreuse1" , 0x7fff00 }, 378 | { "chartreuse2" , 0x76ee00 }, 379 | { "chartreuse3" , 0x66cd00 }, 380 | { "chartreuse4" , 0x458b00 }, 381 | { "olivedrab1" , 0xc0ff3e }, 382 | { "olivedrab2" , 0xb3ee3a }, 383 | { "olivedrab3" , 0x9acd32 }, 384 | { "olivedrab4" , 0x698b22 }, 385 | { "darkolivegreen1" , 0xcaff70 }, 386 | { "darkolivegreen2" , 0xbcee68 }, 387 | { "darkolivegreen3" , 0xa2cd5a }, 388 | { "darkolivegreen4" , 0x6e8b3d }, 389 | { "khaki1" , 0xfff68f }, 390 | { "khaki2" , 0xeee685 }, 391 | { "khaki3" , 0xcdc673 }, 392 | { "khaki4" , 0x8b864e }, 393 | { "lightgoldenrod1" , 0xffec8b }, 394 | { "lightgoldenrod2" , 0xeedc82 }, 395 | { "lightgoldenrod3" , 0xcdbe70 }, 396 | { "lightgoldenrod4" , 0x8b814c }, 397 | { "lightyellow1" , 0xffffe0 }, 398 | { "lightyellow2" , 0xeeeed1 }, 399 | { "lightyellow3" , 0xcdcdb4 }, 400 | { "lightyellow4" , 0x8b8b7a }, 401 | { "yellow1" , 0xffff00 }, 402 | { "yellow2" , 0xeeee00 }, 403 | { "yellow3" , 0xcdcd00 }, 404 | { "yellow4" , 0x8b8b00 }, 405 | { "gold1" , 0xffd700 }, 406 | { "gold2" , 0xeec900 }, 407 | { "gold3" , 0xcdad00 }, 408 | { "gold4" , 0x8b7500 }, 409 | { "goldenrod1" , 0xffc125 }, 410 | { "goldenrod2" , 0xeeb422 }, 411 | { "goldenrod3" , 0xcd9b1d }, 412 | { "goldenrod4" , 0x8b6914 }, 413 | { "darkgoldenrod1" , 0xffb90f }, 414 | { "darkgoldenrod2" , 0xeead0e }, 415 | { "darkgoldenrod3" , 0xcd950c }, 416 | { "darkgoldenrod4" , 0x8b6508 }, 417 | { "rosybrown1" , 0xffc1c1 }, 418 | { "rosybrown2" , 0xeeb4b4 }, 419 | { "rosybrown3" , 0xcd9b9b }, 420 | { "rosybrown4" , 0x8b6969 }, 421 | { "indianred1" , 0xff6a6a }, 422 | { "indianred2" , 0xee6363 }, 423 | { "indianred3" , 0xcd5555 }, 424 | { "indianred4" , 0x8b3a3a }, 425 | { "sienna1" , 0xff8247 }, 426 | { "sienna2" , 0xee7942 }, 427 | { "sienna3" , 0xcd6839 }, 428 | { "sienna4" , 0x8b4726 }, 429 | { "burlywood1" , 0xffd39b }, 430 | { "burlywood2" , 0xeec591 }, 431 | { "burlywood3" , 0xcdaa7d }, 432 | { "burlywood4" , 0x8b7355 }, 433 | { "wheat1" , 0xffe7ba }, 434 | { "wheat2" , 0xeed8ae }, 435 | { "wheat3" , 0xcdba96 }, 436 | { "wheat4" , 0x8b7e66 }, 437 | { "tan1" , 0xffa54f }, 438 | { "tan2" , 0xee9a49 }, 439 | { "tan3" , 0xcd853f }, 440 | { "tan4" , 0x8b5a2b }, 441 | { "chocolate1" , 0xff7f24 }, 442 | { "chocolate2" , 0xee7621 }, 443 | { "chocolate3" , 0xcd661d }, 444 | { "chocolate4" , 0x8b4513 }, 445 | { "firebrick1" , 0xff3030 }, 446 | { "firebrick2" , 0xee2c2c }, 447 | { "firebrick3" , 0xcd2626 }, 448 | { "firebrick4" , 0x8b1a1a }, 449 | { "brown1" , 0xff4040 }, 450 | { "brown2" , 0xee3b3b }, 451 | { "brown3" , 0xcd3333 }, 452 | { "brown4" , 0x8b2323 }, 453 | { "salmon1" , 0xff8c69 }, 454 | { "salmon2" , 0xee8262 }, 455 | { "salmon3" , 0xcd7054 }, 456 | { "salmon4" , 0x8b4c39 }, 457 | { "lightsalmon1" , 0xffa07a }, 458 | { "lightsalmon2" , 0xee9572 }, 459 | { "lightsalmon3" , 0xcd8162 }, 460 | { "lightsalmon4" , 0x8b5742 }, 461 | { "orange1" , 0xffa500 }, 462 | { "orange2" , 0xee9a00 }, 463 | { "orange3" , 0xcd8500 }, 464 | { "orange4" , 0x8b5a00 }, 465 | { "darkorange1" , 0xff7f00 }, 466 | { "darkorange2" , 0xee7600 }, 467 | { "darkorange3" , 0xcd6600 }, 468 | { "darkorange4" , 0x8b4500 }, 469 | { "coral1" , 0xff7256 }, 470 | { "coral2" , 0xee6a50 }, 471 | { "coral3" , 0xcd5b45 }, 472 | { "coral4" , 0x8b3e2f }, 473 | { "tomato1" , 0xff6347 }, 474 | { "tomato2" , 0xee5c42 }, 475 | { "tomato3" , 0xcd4f39 }, 476 | { "tomato4" , 0x8b3626 }, 477 | { "orangered1" , 0xff4500 }, 478 | { "orangered2" , 0xee4000 }, 479 | { "orangered3" , 0xcd3700 }, 480 | { "orangered4" , 0x8b2500 }, 481 | { "red1" , 0xff0000 }, 482 | { "red2" , 0xee0000 }, 483 | { "red3" , 0xcd0000 }, 484 | { "red4" , 0x8b0000 }, 485 | { "deeppink1" , 0xff1493 }, 486 | { "deeppink2" , 0xee1289 }, 487 | { "deeppink3" , 0xcd1076 }, 488 | { "deeppink4" , 0x8b0a50 }, 489 | { "hotpink1" , 0xff6eb4 }, 490 | { "hotpink2" , 0xee6aa7 }, 491 | { "hotpink3" , 0xcd6090 }, 492 | { "hotpink4" , 0x8b3a62 }, 493 | { "pink1" , 0xffb5c5 }, 494 | { "pink2" , 0xeea9b8 }, 495 | { "pink3" , 0xcd919e }, 496 | { "pink4" , 0x8b636c }, 497 | { "lightpink1" , 0xffaeb9 }, 498 | { "lightpink2" , 0xeea2ad }, 499 | { "lightpink3" , 0xcd8c95 }, 500 | { "lightpink4" , 0x8b5f65 }, 501 | { "palevioletred1" , 0xff82ab }, 502 | { "palevioletred2" , 0xee799f }, 503 | { "palevioletred3" , 0xcd6889 }, 504 | { "palevioletred4" , 0x8b475d }, 505 | { "maroon1" , 0xff34b3 }, 506 | { "maroon2" , 0xee30a7 }, 507 | { "maroon3" , 0xcd2990 }, 508 | { "maroon4" , 0x8b1c62 }, 509 | { "violetred1" , 0xff3e96 }, 510 | { "violetred2" , 0xee3a8c }, 511 | { "violetred3" , 0xcd3278 }, 512 | { "violetred4" , 0x8b2252 }, 513 | { "magenta1" , 0xff00ff }, 514 | { "magenta2" , 0xee00ee }, 515 | { "magenta3" , 0xcd00cd }, 516 | { "magenta4" , 0x8b008b }, 517 | { "orchid1" , 0xff83fa }, 518 | { "orchid2" , 0xee7ae9 }, 519 | { "orchid3" , 0xcd69c9 }, 520 | { "orchid4" , 0x8b4789 }, 521 | { "plum1" , 0xffbbff }, 522 | { "plum2" , 0xeeaeee }, 523 | { "plum3" , 0xcd96cd }, 524 | { "plum4" , 0x8b668b }, 525 | { "mediumorchid1" , 0xe066ff }, 526 | { "mediumorchid2" , 0xd15fee }, 527 | { "mediumorchid3" , 0xb452cd }, 528 | { "mediumorchid4" , 0x7a378b }, 529 | { "darkorchid1" , 0xbf3eff }, 530 | { "darkorchid2" , 0xb23aee }, 531 | { "darkorchid3" , 0x9a32cd }, 532 | { "darkorchid4" , 0x68228b }, 533 | { "purple1" , 0x9b30ff }, 534 | { "purple2" , 0x912cee }, 535 | { "purple3" , 0x7d26cd }, 536 | { "purple4" , 0x551a8b }, 537 | { "mediumpurple1" , 0xab82ff }, 538 | { "mediumpurple2" , 0x9f79ee }, 539 | { "mediumpurple3" , 0x8968cd }, 540 | { "mediumpurple4" , 0x5d478b }, 541 | { "thistle1" , 0xffe1ff }, 542 | { "thistle2" , 0xeed2ee }, 543 | { "thistle3" , 0xcdb5cd }, 544 | { "thistle4" , 0x8b7b8b }, 545 | { "gray0" , 0x0 }, 546 | { "grey0" , 0x0 }, 547 | { "gray1" , 0x30303 }, 548 | { "grey1" , 0x30303 }, 549 | { "gray2" , 0x50505 }, 550 | { "grey2" , 0x50505 }, 551 | { "gray3" , 0x80808 }, 552 | { "grey3" , 0x80808 }, 553 | { "gray4" , 0xa0a0a }, 554 | { "grey4" , 0xa0a0a }, 555 | { "gray5" , 0xd0d0d }, 556 | { "grey5" , 0xd0d0d }, 557 | { "gray6" , 0xf0f0f }, 558 | { "grey6" , 0xf0f0f }, 559 | { "gray7" , 0x121212 }, 560 | { "grey7" , 0x121212 }, 561 | { "gray8" , 0x141414 }, 562 | { "grey8" , 0x141414 }, 563 | { "gray9" , 0x171717 }, 564 | { "grey9" , 0x171717 }, 565 | { "gray10" , 0x1a1a1a }, 566 | { "grey10" , 0x1a1a1a }, 567 | { "gray11" , 0x1c1c1c }, 568 | { "grey11" , 0x1c1c1c }, 569 | { "gray12" , 0x1f1f1f }, 570 | { "grey12" , 0x1f1f1f }, 571 | { "gray13" , 0x212121 }, 572 | { "grey13" , 0x212121 }, 573 | { "gray14" , 0x242424 }, 574 | { "grey14" , 0x242424 }, 575 | { "gray15" , 0x262626 }, 576 | { "grey15" , 0x262626 }, 577 | { "gray16" , 0x292929 }, 578 | { "grey16" , 0x292929 }, 579 | { "gray17" , 0x2b2b2b }, 580 | { "grey17" , 0x2b2b2b }, 581 | { "gray18" , 0x2e2e2e }, 582 | { "grey18" , 0x2e2e2e }, 583 | { "gray19" , 0x303030 }, 584 | { "grey19" , 0x303030 }, 585 | { "gray20" , 0x333333 }, 586 | { "grey20" , 0x333333 }, 587 | { "gray21" , 0x363636 }, 588 | { "grey21" , 0x363636 }, 589 | { "gray22" , 0x383838 }, 590 | { "grey22" , 0x383838 }, 591 | { "gray23" , 0x3b3b3b }, 592 | { "grey23" , 0x3b3b3b }, 593 | { "gray24" , 0x3d3d3d }, 594 | { "grey24" , 0x3d3d3d }, 595 | { "gray25" , 0x404040 }, 596 | { "grey25" , 0x404040 }, 597 | { "gray26" , 0x424242 }, 598 | { "grey26" , 0x424242 }, 599 | { "gray27" , 0x454545 }, 600 | { "grey27" , 0x454545 }, 601 | { "gray28" , 0x474747 }, 602 | { "grey28" , 0x474747 }, 603 | { "gray29" , 0x4a4a4a }, 604 | { "grey29" , 0x4a4a4a }, 605 | { "gray30" , 0x4d4d4d }, 606 | { "grey30" , 0x4d4d4d }, 607 | { "gray31" , 0x4f4f4f }, 608 | { "grey31" , 0x4f4f4f }, 609 | { "gray32" , 0x525252 }, 610 | { "grey32" , 0x525252 }, 611 | { "gray33" , 0x545454 }, 612 | { "grey33" , 0x545454 }, 613 | { "gray34" , 0x575757 }, 614 | { "grey34" , 0x575757 }, 615 | { "gray35" , 0x595959 }, 616 | { "grey35" , 0x595959 }, 617 | { "gray36" , 0x5c5c5c }, 618 | { "grey36" , 0x5c5c5c }, 619 | { "gray37" , 0x5e5e5e }, 620 | { "grey37" , 0x5e5e5e }, 621 | { "gray38" , 0x616161 }, 622 | { "grey38" , 0x616161 }, 623 | { "gray39" , 0x636363 }, 624 | { "grey39" , 0x636363 }, 625 | { "gray40" , 0x666666 }, 626 | { "grey40" , 0x666666 }, 627 | { "gray41" , 0x696969 }, 628 | { "grey41" , 0x696969 }, 629 | { "gray42" , 0x6b6b6b }, 630 | { "grey42" , 0x6b6b6b }, 631 | { "gray43" , 0x6e6e6e }, 632 | { "grey43" , 0x6e6e6e }, 633 | { "gray44" , 0x707070 }, 634 | { "grey44" , 0x707070 }, 635 | { "gray45" , 0x737373 }, 636 | { "grey45" , 0x737373 }, 637 | { "gray46" , 0x757575 }, 638 | { "grey46" , 0x757575 }, 639 | { "gray47" , 0x787878 }, 640 | { "grey47" , 0x787878 }, 641 | { "gray48" , 0x7a7a7a }, 642 | { "grey48" , 0x7a7a7a }, 643 | { "gray49" , 0x7d7d7d }, 644 | { "grey49" , 0x7d7d7d }, 645 | { "gray50" , 0x7f7f7f }, 646 | { "grey50" , 0x7f7f7f }, 647 | { "gray51" , 0x828282 }, 648 | { "grey51" , 0x828282 }, 649 | { "gray52" , 0x858585 }, 650 | { "grey52" , 0x858585 }, 651 | { "gray53" , 0x878787 }, 652 | { "grey53" , 0x878787 }, 653 | { "gray54" , 0x8a8a8a }, 654 | { "grey54" , 0x8a8a8a }, 655 | { "gray55" , 0x8c8c8c }, 656 | { "grey55" , 0x8c8c8c }, 657 | { "gray56" , 0x8f8f8f }, 658 | { "grey56" , 0x8f8f8f }, 659 | { "gray57" , 0x919191 }, 660 | { "grey57" , 0x919191 }, 661 | { "gray58" , 0x949494 }, 662 | { "grey58" , 0x949494 }, 663 | { "gray59" , 0x969696 }, 664 | { "grey59" , 0x969696 }, 665 | { "gray60" , 0x999999 }, 666 | { "grey60" , 0x999999 }, 667 | { "gray61" , 0x9c9c9c }, 668 | { "grey61" , 0x9c9c9c }, 669 | { "gray62" , 0x9e9e9e }, 670 | { "grey62" , 0x9e9e9e }, 671 | { "gray63" , 0xa1a1a1 }, 672 | { "grey63" , 0xa1a1a1 }, 673 | { "gray64" , 0xa3a3a3 }, 674 | { "grey64" , 0xa3a3a3 }, 675 | { "gray65" , 0xa6a6a6 }, 676 | { "grey65" , 0xa6a6a6 }, 677 | { "gray66" , 0xa8a8a8 }, 678 | { "grey66" , 0xa8a8a8 }, 679 | { "gray67" , 0xababab }, 680 | { "grey67" , 0xababab }, 681 | { "gray68" , 0xadadad }, 682 | { "grey68" , 0xadadad }, 683 | { "gray69" , 0xb0b0b0 }, 684 | { "grey69" , 0xb0b0b0 }, 685 | { "gray70" , 0xb3b3b3 }, 686 | { "grey70" , 0xb3b3b3 }, 687 | { "gray71" , 0xb5b5b5 }, 688 | { "grey71" , 0xb5b5b5 }, 689 | { "gray72" , 0xb8b8b8 }, 690 | { "grey72" , 0xb8b8b8 }, 691 | { "gray73" , 0xbababa }, 692 | { "grey73" , 0xbababa }, 693 | { "gray74" , 0xbdbdbd }, 694 | { "grey74" , 0xbdbdbd }, 695 | { "gray75" , 0xbfbfbf }, 696 | { "grey75" , 0xbfbfbf }, 697 | { "gray76" , 0xc2c2c2 }, 698 | { "grey76" , 0xc2c2c2 }, 699 | { "gray77" , 0xc4c4c4 }, 700 | { "grey77" , 0xc4c4c4 }, 701 | { "gray78" , 0xc7c7c7 }, 702 | { "grey78" , 0xc7c7c7 }, 703 | { "gray79" , 0xc9c9c9 }, 704 | { "grey79" , 0xc9c9c9 }, 705 | { "gray80" , 0xcccccc }, 706 | { "grey80" , 0xcccccc }, 707 | { "gray81" , 0xcfcfcf }, 708 | { "grey81" , 0xcfcfcf }, 709 | { "gray82" , 0xd1d1d1 }, 710 | { "grey82" , 0xd1d1d1 }, 711 | { "gray83" , 0xd4d4d4 }, 712 | { "grey83" , 0xd4d4d4 }, 713 | { "gray84" , 0xd6d6d6 }, 714 | { "grey84" , 0xd6d6d6 }, 715 | { "gray85" , 0xd9d9d9 }, 716 | { "grey85" , 0xd9d9d9 }, 717 | { "gray86" , 0xdbdbdb }, 718 | { "grey86" , 0xdbdbdb }, 719 | { "gray87" , 0xdedede }, 720 | { "grey87" , 0xdedede }, 721 | { "gray88" , 0xe0e0e0 }, 722 | { "grey88" , 0xe0e0e0 }, 723 | { "gray89" , 0xe3e3e3 }, 724 | { "grey89" , 0xe3e3e3 }, 725 | { "gray90" , 0xe5e5e5 }, 726 | { "grey90" , 0xe5e5e5 }, 727 | { "gray91" , 0xe8e8e8 }, 728 | { "grey91" , 0xe8e8e8 }, 729 | { "gray92" , 0xebebeb }, 730 | { "grey92" , 0xebebeb }, 731 | { "gray93" , 0xededed }, 732 | { "grey93" , 0xededed }, 733 | { "gray94" , 0xf0f0f0 }, 734 | { "grey94" , 0xf0f0f0 }, 735 | { "gray95" , 0xf2f2f2 }, 736 | { "grey95" , 0xf2f2f2 }, 737 | { "gray96" , 0xf5f5f5 }, 738 | { "grey96" , 0xf5f5f5 }, 739 | { "gray97" , 0xf7f7f7 }, 740 | { "grey97" , 0xf7f7f7 }, 741 | { "gray98" , 0xfafafa }, 742 | { "grey98" , 0xfafafa }, 743 | { "gray99" , 0xfcfcfc }, 744 | { "grey99" , 0xfcfcfc }, 745 | { "gray100" , 0xffffff }, 746 | { "grey100" , 0xffffff }, 747 | { "dark grey" , 0xa9a9a9 }, 748 | { "darkgrey" , 0xa9a9a9 }, 749 | { "dark gray" , 0xa9a9a9 }, 750 | { "darkgray" , 0xa9a9a9 }, 751 | { "dark blue" , 0x8b }, 752 | { "darkblue" , 0x8b }, 753 | { "dark cyan" , 0x8b8b }, 754 | { "darkcyan" , 0x8b8b }, 755 | { "dark magenta" , 0x8b008b }, 756 | { "darkmagenta" , 0x8b008b }, 757 | { "dark red" , 0x8b0000 }, 758 | { "darkred" , 0x8b0000 }, 759 | { "light green" , 0x90ee90 }, 760 | { "lightgreen" , 0x90ee90 }, 761 | { "none", -1 }, 762 | { 0, 0 } 763 | }; 764 | -------------------------------------------------------------------------------- /mlx/mlx_new_window.m: -------------------------------------------------------------------------------- 1 | // mlx_new_window.m 2 | 3 | #import 4 | #import 5 | #import 6 | 7 | #include 8 | #include 9 | 10 | #include "mlx_int.h" 11 | #include "mlx_new_window.h" 12 | 13 | 14 | NSOpenGLPixelFormatAttribute pfa_attrs[] = 15 | { 16 | NSOpenGLPFADepthSize, 32, 17 | NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersionLegacy, 18 | 0 19 | }; 20 | 21 | static const GLfloat pixel_vertexes[8] = 22 | { 23 | -1.0 , -1.0, 24 | 1.0, -1.0, 25 | 1.0, 1.0, 26 | -1.0, 1.0 27 | }; 28 | 29 | 30 | 31 | int get_mouse_button(NSEventType eventtype) 32 | { 33 | switch (eventtype) { 34 | case NSEventTypeLeftMouseDown: 35 | case NSEventTypeLeftMouseUp: 36 | case NSEventTypeLeftMouseDragged: 37 | return 1; 38 | case NSEventTypeRightMouseDown: 39 | case NSEventTypeRightMouseUp: 40 | case NSEventTypeRightMouseDragged: 41 | return 2; 42 | case NSEventTypeOtherMouseDown: 43 | case NSEventTypeOtherMouseUp: 44 | case NSEventTypeOtherMouseDragged: 45 | return 3; 46 | default: 47 | return 0; 48 | } 49 | } 50 | 51 | 52 | // classes for window & events 53 | 54 | 55 | 56 | 57 | @implementation NSWindowEvent 58 | 59 | - (NSWindowEvent *) initWithContentRect:(NSRect)rect styleMask:(NSUInteger)winstyle backing:(NSBackingStoreType)bck defer:(BOOL) dfr 60 | { 61 | int i; 62 | 63 | if ((self = [super initWithContentRect:rect 64 | styleMask:winstyle 65 | backing:bck 66 | defer:dfr])) 67 | { 68 | i = MAX_EVENT; 69 | while (i--) 70 | { 71 | event_funct[i] = NULL; 72 | event_param[i] = NULL; 73 | } 74 | keyrepeat = 0; 75 | keyflag = 0; 76 | size_x = rect.size.width; 77 | size_y = rect.size.height; 78 | } 79 | return (self); 80 | } 81 | 82 | 83 | - (void) setEvent:(int)event andFunc:(func_t)func andParam:(void *)param 84 | { 85 | event_funct[event] = func; 86 | event_param[event] = param; 87 | if (event == 6 || event == 32) // motion notify && high precision motion notify 88 | { 89 | if (func == NULL) 90 | [self setAcceptsMouseMovedEvents:NO]; 91 | else 92 | [self setAcceptsMouseMovedEvents:YES]; 93 | } 94 | } 95 | 96 | 97 | - (void) setKeyRepeat:(int)mode 98 | { 99 | keyrepeat = mode; 100 | } 101 | 102 | - (BOOL) acceptsFirstResponder 103 | { 104 | return (YES); 105 | } 106 | 107 | - (void) flagsChanged:(NSEvent *)theEvent 108 | { 109 | unsigned int flag; 110 | int the_key; 111 | unsigned int val; 112 | 113 | flag = [theEvent modifierFlags]; 114 | // printf("Key flag changed: %x => %x\n", keyflag, flag); 115 | // printf("**mlx flag low part : %d - %x\n", flag&0xFFFF, flag&0xFFFF); 116 | 117 | if (!(val = (keyflag|flag)&(~(keyflag&flag)))) 118 | return ; // no change - can happen when loosing focus on special key pressed, then re-pressed later 119 | the_key = 1; 120 | while (((val >> (the_key-1)) & 0x01)==0) 121 | the_key ++; 122 | if (flag > keyflag && event_funct[2] != NULL) 123 | event_funct[2](0xFF+the_key, event_param[2]); 124 | if (flag < keyflag && event_funct[3] != NULL) 125 | event_funct[3](0xFF+the_key, event_param[3]); 126 | /* 127 | if (event_funct[2] != NULL) 128 | { 129 | if (!(keyflag & NSAlphaShiftKeyMask) && (flag&NSAlphaShiftKeyMask)) event_funct[2](0xFF+1, event_param[2]); 130 | if (!(keyflag & NSShiftKeyMask) && (flag&NSShiftKeyMask)) event_funct[2](0xFF+2, event_param[2]); 131 | if (!(keyflag & NSControlKeyMask) && (flag&NSControlKeyMask)) event_funct[2](0xFF+3, event_param[2]); 132 | if (!(keyflag & NSAlternateKeyMask) && (flag&NSAlternateKeyMask)) event_funct[2](0xFF+4, event_param[2]); 133 | if (!(keyflag & NSCommandKeyMask) && (flag&NSCommandKeyMask)) event_funct[2](0xFF+5, event_param[2]); 134 | if (!(keyflag & NSNumericPadKeyMask) && (flag&NSNumericPadKeyMask)) event_funct[2](0xFF+6, event_param[2]); 135 | if (!(keyflag & NSHelpKeyMask) && (flag&NSHelpKeyMask)) event_funct[2](0xFF+7, event_param[2]); 136 | if (!(keyflag & NSFunctionKeyMask) && (flag&NSFunctionKeyMask)) event_funct[2](0xFF+8, event_param[2]); 137 | } 138 | if (event_funct[3] != NULL) 139 | { 140 | if ((keyflag & NSShiftKeyMask) && !(flag&NSShiftKeyMask)) event_funct[3](NSShiftKeyMask, event_param[3]); 141 | 142 | if ((keyflag & NSAlphaShiftKeyMask) && !(flag&NSAlphaShiftKeyMask)) event_funct[3](0xFF+1, event_param[3]); 143 | if ((keyflag & NSShiftKeyMask) && !(flag&NSShiftKeyMask)) event_funct[3](0xFF+2, event_param[3]); 144 | if ((keyflag & NSControlKeyMask) && !(flag&NSControlKeyMask)) event_funct[3](0xFF+3, event_param[3]); 145 | if ((keyflag & NSAlternateKeyMask) && !(flag&NSAlternateKeyMask)) event_funct[3](0xFF+4, event_param[3]); 146 | if ((keyflag & NSCommandKeyMask) && !(flag&NSCommandKeyMask)) event_funct[3](0xFF+5, event_param[3]); 147 | if ((keyflag & NSNumericPadKeyMask) && !(flag&NSNumericPadKeyMask)) event_funct[3](0xFF+6, event_param[3]); 148 | if ((keyflag & NSHelpKeyMask) && !(flag&NSHelpKeyMask)) event_funct[3](0xFF+7, event_param[3]); 149 | if ((keyflag & NSFunctionKeyMask) && !(flag&NSFunctionKeyMask)) event_funct[3](0xFF+8, event_param[3]); 150 | } 151 | */ 152 | keyflag = flag; 153 | } 154 | 155 | - (void) keyDown:(NSEvent *)theEvent 156 | { 157 | if (keyrepeat==0 && [theEvent isARepeat]) 158 | return ; 159 | // printf("Key Down: %d\n", [theEvent keyCode]); 160 | if (event_funct[2] != NULL) 161 | event_funct[2]([theEvent keyCode], event_param[2]); 162 | // else [super keyDown: theEvent]; 163 | } 164 | 165 | - (void) keyUp:(NSEvent *)theEvent 166 | { 167 | // printf("Key Up: %d\n", [theEvent keyCode]); 168 | if (event_funct[3] != NULL) 169 | event_funct[3]([theEvent keyCode], event_param[3]); 170 | // else [super keyUp: theEvent]; 171 | 172 | } 173 | 174 | - (void) mouseDown:(NSEvent *)theEvent 175 | { 176 | NSPoint thepoint; 177 | int button; 178 | 179 | thepoint = [theEvent locationInWindow]; 180 | button = get_mouse_button([theEvent type]); 181 | // printf("Mouse pressed bt %d pos: %f, %f\n", button, thepoint.x, thepoint.y); 182 | if (event_funct[4] != NULL) 183 | event_funct[4](button, (int)(thepoint.x), size_y - 1 - (int)(thepoint.y), event_param[4]); 184 | } 185 | 186 | - (void) rightMouseDown:(NSEvent *)theEvent 187 | { 188 | NSPoint thepoint; 189 | int button; 190 | 191 | thepoint = [theEvent locationInWindow]; 192 | button = get_mouse_button([theEvent type]); 193 | // printf("Mouse pressed bt %d pos: %f, %f\n", button, thepoint.x, thepoint.y); 194 | if (event_funct[4] != NULL) 195 | event_funct[4](button, (int)(thepoint.x), size_y - 1 - (int)(thepoint.y), event_param[4]); 196 | } 197 | 198 | - (void) otherMouseDown:(NSEvent *)theEvent 199 | { 200 | NSPoint thepoint; 201 | int button; 202 | 203 | thepoint = [theEvent locationInWindow]; 204 | button = get_mouse_button([theEvent type]); 205 | // printf("Mouse pressed bt %d pos: %f, %f\n", button, thepoint.x, thepoint.y); 206 | if (event_funct[4] != NULL) 207 | event_funct[4](button, (int)(thepoint.x), size_y - 1 - (int)(thepoint.y), event_param[4]); 208 | } 209 | 210 | - (void) mouseUp:(NSEvent *)theEvent 211 | { 212 | NSPoint thepoint; 213 | int button; 214 | 215 | thepoint = [theEvent locationInWindow]; 216 | button = get_mouse_button([theEvent type]); 217 | // printf("Mouse release bt %d pos: %f, %f\n", button, thepoint.x, thepoint.y); 218 | if (event_funct[5] != NULL) 219 | event_funct[5](button, (int)(thepoint.x), size_y - 1 - (int)(thepoint.y), event_param[5]); 220 | } 221 | 222 | - (void) rightMouseUp:(NSEvent *)theEvent 223 | { 224 | NSPoint thepoint; 225 | int button; 226 | 227 | thepoint = [theEvent locationInWindow]; 228 | button = get_mouse_button([theEvent type]); 229 | // printf("Mouse release bt %d pos: %f, %f\n", button, thepoint.x, thepoint.y); 230 | if (event_funct[5] != NULL) 231 | event_funct[5](button, (int)(thepoint.x), size_y - 1 - (int)(thepoint.y), event_param[5]); 232 | } 233 | 234 | - (void) otherMouseUp:(NSEvent *)theEvent 235 | { 236 | NSPoint thepoint; 237 | int button; 238 | 239 | thepoint = [theEvent locationInWindow]; 240 | button = get_mouse_button([theEvent type]); 241 | // printf("Mouse release bt %d pos: %f, %f\n", button, thepoint.x, thepoint.y); 242 | if (event_funct[5] != NULL) 243 | event_funct[5](button, (int)(thepoint.x), size_y - 1 - (int)(thepoint.y), event_param[5]); 244 | } 245 | 246 | - (void) mouseMoved:(NSEvent *)theEvent 247 | { 248 | NSPoint thepoint; 249 | 250 | thepoint = [theEvent locationInWindow]; 251 | // printf("Mouse moved pos: %f, %f\n", thepoint.x, thepoint.y); 252 | if (event_funct[6] != NULL) 253 | event_funct[6]((int)(thepoint.x), size_y - 1 - (int)(thepoint.y), event_param[6]); 254 | } 255 | 256 | 257 | - (void) mouseDragged:(NSEvent *)theEvent 258 | { 259 | NSPoint thepoint; 260 | 261 | thepoint = [theEvent locationInWindow]; 262 | // printf("Mouse moved pos: %f, %f\n", thepoint.x, thepoint.y); 263 | if (event_funct[6] != NULL) 264 | event_funct[6]((int)(thepoint.x), size_y - 1 - (int)(thepoint.y), event_param[6]); 265 | } 266 | 267 | 268 | - (void) rightMouseDragged:(NSEvent *)theEvent 269 | { 270 | NSPoint thepoint; 271 | 272 | thepoint = [theEvent locationInWindow]; 273 | // printf("Mouse moved pos: %f, %f\n", thepoint.x, thepoint.y); 274 | if (event_funct[6] != NULL) 275 | event_funct[6]((int)(thepoint.x), size_y - 1 - (int)(thepoint.y), event_param[6]); 276 | } 277 | 278 | 279 | - (void) otherMouseDragged:(NSEvent *)theEvent 280 | { 281 | NSPoint thepoint; 282 | 283 | thepoint = [theEvent locationInWindow]; 284 | // printf("Mouse moved pos: %f, %f\n", thepoint.x, thepoint.y); 285 | if (event_funct[6] != NULL) 286 | event_funct[6]((int)(thepoint.x), size_y - 1 - (int)(thepoint.y), event_param[6]); 287 | } 288 | 289 | 290 | - (void) scrollWheel:(NSEvent *)theEvent 291 | { 292 | NSPoint thepoint; 293 | int button; 294 | float sens; 295 | 296 | if (event_funct[4] == NULL) 297 | return ; 298 | 299 | button = 0; 300 | thepoint = [theEvent locationInWindow]; 301 | sens = [theEvent deltaY]; 302 | if (sens > 0.2) 303 | button = 4; 304 | if (sens < -0.2) 305 | button = 5; 306 | sens = [theEvent deltaX]; 307 | if (sens > 0.2) 308 | button = 6; 309 | if (sens < -0.2) 310 | button = 7; 311 | if (button != 0) 312 | event_funct[4](button, (int)(thepoint.x), size_y - 1 - (int)(thepoint.y), event_param[4]); 313 | } 314 | 315 | 316 | - (void) exposeNotification:(NSNotification *)note 317 | { 318 | // printf("Expose...\n"); 319 | if (event_funct[12] != NULL) 320 | event_funct[12](event_param[12]); 321 | // printf("Expose done.\n"); 322 | } 323 | 324 | - (void) closeNotification:(NSNotification *)note 325 | { 326 | if (event_funct[17] != NULL) 327 | event_funct[17](event_param[17]); 328 | } 329 | 330 | - (void) deminiaturizeNotification:(NSNotification *)note 331 | { 332 | // if (event_funct[??] != NULL) 333 | // event_funct[??](event_param[??]); 334 | [self exposeNotification:note]; 335 | } 336 | @end 337 | 338 | 339 | @implementation MlxWin 340 | 341 | - (id) initWithRect: (NSRect)rect andTitle: (NSString *)title pfaAttrs: (NSOpenGLPixelFormatAttribute *)attrs 342 | { 343 | NSOpenGLPixelFormat* pixFmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs]; 344 | 345 | if ((self = [super initWithFrame:rect pixelFormat:pixFmt]) != nil) 346 | { 347 | NSUInteger windowStyle = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable; 348 | 349 | win = [[NSWindowEvent alloc] initWithContentRect:rect 350 | styleMask:windowStyle 351 | backing:NSBackingStoreBuffered // NSBackingStoreNonretained 352 | defer:NO]; 353 | [win setContentView:self]; 354 | [win setTitle:title]; 355 | [win setKeyRepeat:1]; 356 | [win makeKeyAndOrderFront:self]; 357 | 358 | // printf("init ctx: current %p ", [NSOpenGLContext currentContext]); 359 | 360 | // ctx = [[NSOpenGLContext alloc] initWithFormat:pixFmt shareContext:[NSOpenGLContext currentContext]]; //other_context]; 361 | // [ctx setView:self]; 362 | // [ctx makeCurrentContext]; 363 | 364 | [[self openGLContext] makeCurrentContext]; 365 | [[self openGLContext] setView:self]; 366 | [self prepareOpenGL]; 367 | 368 | [self setNextKeyView:self]; 369 | 370 | // [[NSNotificationCenter defaultCenter] addObserver:win selector:@selector(exposeNotification:) name:@"NSWindowDidExposeNotification" object:nil]; 371 | [[NSNotificationCenter defaultCenter] addObserver:win selector:@selector(exposeNotification:) name:@"NSWindowDidBecomeKeyNotification" object:win]; 372 | [[NSNotificationCenter defaultCenter] addObserver:win selector:@selector(deminiaturizeNotification:) name:@"NSWindowDidDeminiaturizeNotification" object:win]; 373 | [[NSNotificationCenter defaultCenter] addObserver:win selector:@selector(closeNotification:) name:@"NSWindowWillCloseNotification" object:win]; 374 | // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ctxNeedsUpdate:) 375 | // name:NSViewGlobalFrameDidChangeNotification 376 | // object:nil]; 377 | 378 | size_x = rect.size.width; 379 | size_y = rect.size.height; 380 | 381 | glClearColor(0, 0, 0, 0); 382 | glClear(GL_COLOR_BUFFER_BIT); 383 | glFlush(); 384 | 385 | //[win makeKeyAndOrderFront:nil]; 386 | // BOOL r = [win isKeyWindow]; 387 | // if (r==YES) printf("keywindow ok\n"); else printf("keywindow KO\n"); 388 | 389 | // Window controller subclass to set title 390 | // NSWindowController* windowController = [[NSWindowController alloc] initWithWindow:win]; 391 | // [windowController windowTitleForDocumentDisplayName:title]; 392 | // [windowController showWindow:nil]; 393 | // MlxWinController *mlxWinCont = [[MlxWinController alloc] initWin:win andTitle:title]; 394 | 395 | // after nswindowcontroller who will retake first responder 396 | // BOOL r = [win makeFirstResponder:nil]; 397 | // if (r==YES) printf("responder ok\n"); else printf("responder KO\n"); 398 | 399 | [pixFmt release]; 400 | } 401 | return (self); 402 | } 403 | 404 | - (int) pixel_management 405 | { 406 | bzero(&glsl, sizeof(glsl)); // so gldelete[shader/program] go silent on error. 407 | 408 | glDisable(GL_DEPTH_TEST); 409 | glGenBuffers(1, &pixel_vbuffer); 410 | glBindBuffer(GL_ARRAY_BUFFER, pixel_vbuffer); 411 | glBufferData(GL_ARRAY_BUFFER, sizeof(pixel_vertexes), pixel_vertexes, GL_DYNAMIC_DRAW); // 4 points buff 412 | // pixel_ptr = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY); 413 | 414 | glGenTextures(1, &pixel_texture); 415 | glBindTexture(GL_TEXTURE_2D, pixel_texture); 416 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 417 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 418 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 419 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 420 | pixtexbuff = malloc(sizeof(unsigned int)*size_x*size_y); 421 | pixel_nb = size_x*size_y; 422 | while (pixel_nb--) pixtexbuff[pixel_nb] = 0xFF000000; 423 | pixel_nb = 0; 424 | glTexImage2D( 425 | GL_TEXTURE_2D, 0, /* target, level of detail */ 426 | GL_RGBA8, /* internal format */ 427 | size_x, size_y, 0, /* width, height, border */ 428 | GL_BGRA, GL_UNSIGNED_BYTE, /* external format, type */ 429 | pixtexbuff /* pixels */ 430 | ); 431 | // printf("pix tex err? 0x%x\n", glGetError()); 432 | 433 | if (mlx_shaders(&glsl)) 434 | return (0); 435 | 436 | glUseProgram(glsl.pixel_program); 437 | glsl.loc_pixel_texture = glGetUniformLocation(glsl.pixel_program, "texture"); 438 | //glsl.loc_pixel_winhalfsize = glGetUniformLocation(glsl.pixel_program, "winhalfsize"); 439 | glsl.loc_pixel_position = glGetAttribLocation(glsl.pixel_program, "position"); 440 | // printf("err? 0x%x\n", glGetError()); 441 | 442 | glUseProgram(glsl.image_program); 443 | glsl.loc_image_texture = glGetUniformLocation(glsl.image_program, "texture"); 444 | glsl.loc_image_pos = glGetUniformLocation(glsl.image_program, "imagepos"); 445 | glsl.loc_image_size = glGetUniformLocation(glsl.image_program, "imagesize"); 446 | glsl.loc_image_winhalfsize = glGetUniformLocation(glsl.image_program, "winhalfsize"); 447 | glsl.loc_image_position = glGetAttribLocation(glsl.image_program, "position"); 448 | // printf("err? 0x%x\n", glGetError()); 449 | 450 | glUseProgram(glsl.font_program); 451 | glsl.loc_font_texture = glGetUniformLocation(glsl.font_program, "texture"); 452 | glsl.loc_font_color = glGetUniformLocation(glsl.font_program, "color"); 453 | glsl.loc_font_posinwin = glGetUniformLocation(glsl.font_program, "fontposinwin"); 454 | glsl.loc_font_posinatlas = glGetUniformLocation(glsl.font_program, "fontposinatlas"); 455 | glsl.loc_font_atlassize = glGetUniformLocation(glsl.font_program, "fontatlassize"); 456 | glsl.loc_font_winhalfsize = glGetUniformLocation(glsl.font_program, "winhalfsize"); 457 | glsl.loc_font_position = glGetAttribLocation(glsl.font_program, "position"); 458 | // printf("err? 0x%x\n", glGetError()); 459 | 460 | glFlush(); 461 | return (1); 462 | } 463 | 464 | - (void) ctxNeedsUpdate 465 | { 466 | // printf("Context update\n"); 467 | [ctx update]; 468 | } 469 | 470 | - (void) selectGLContext 471 | { 472 | if ([NSOpenGLContext currentContext] != [self openGLContext]) 473 | { 474 | // printf("ctx: %p => %p\n", [NSOpenGLContext currentContext], [self openGLContext]); 475 | [[self openGLContext] makeCurrentContext]; 476 | } 477 | } 478 | 479 | - (void) flushGLContext 480 | { 481 | [[self openGLContext] flushBuffer]; 482 | } 483 | 484 | - (NSOpenGLContext *) ctx 485 | { 486 | return (ctx); 487 | } 488 | 489 | - (NSWindowEvent *) win 490 | { 491 | return (win); 492 | } 493 | 494 | 495 | - (void) pixelPutColor: (int)color X:(int)x Y:(int)y 496 | { 497 | pixel_nb ++; 498 | 499 | glBindTexture(GL_TEXTURE_2D, pixel_vbuffer); 500 | glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, 1, 1, GL_BGRA, GL_UNSIGNED_BYTE, (GLvoid *)(&color)); 501 | 502 | if (pixel_nb >= MAX_PIXEL_NB) 503 | [self mlx_gl_draw]; 504 | } 505 | 506 | - (void) destroyPixelManagement 507 | { 508 | free(pixtexbuff); 509 | [self selectGLContext]; 510 | glDeleteBuffers(1, &pixel_vbuffer); 511 | glDeleteTextures(1, &pixel_texture); 512 | glDeleteProgram(glsl.pixel_program); 513 | glDeleteProgram(glsl.image_program); 514 | glDeleteShader(glsl.pixel_vshader); 515 | glDeleteShader(glsl.pixel_fshader); 516 | glDeleteShader(glsl.image_vshader); 517 | glDeleteShader(glsl.image_fshader); 518 | } 519 | 520 | 521 | - (void) destroyMe 522 | { 523 | [[NSNotificationCenter defaultCenter] removeObserver:win]; 524 | [[NSNotificationCenter defaultCenter] removeObserver:self]; 525 | // [ctx release]; 526 | [win close]; 527 | [self release]; 528 | } 529 | 530 | - (void) setEvent:(int)event andFunc:(func_t)func andParam:(void *)param 531 | { 532 | [win setEvent:event andFunc:func andParam:param]; 533 | } 534 | 535 | - (void) setKeyRepeat:(int)mode 536 | { 537 | [win setKeyRepeat:mode]; 538 | } 539 | 540 | - (void) clearWin 541 | { 542 | glClearColor(0, 0, 0, 0); 543 | glClear(GL_COLOR_BUFFER_BIT); 544 | } 545 | 546 | - (void) mlx_gl_draw_img:(mlx_img_list_t *)img andCtx:(mlx_img_ctx_t *)imgctx andX:(int)x andY:(int)y 547 | { 548 | 549 | if (pixel_nb >0) 550 | [self mlx_gl_draw]; 551 | 552 | glUseProgram(glsl.image_program); 553 | 554 | glActiveTexture(GL_TEXTURE0); 555 | glBindTexture(GL_TEXTURE_2D, imgctx->texture); 556 | glUniform1i(glsl.loc_image_texture, 0); 557 | 558 | glUniform2f(glsl.loc_image_winhalfsize, size_x/2, size_y/2); 559 | glUniform2f(glsl.loc_image_pos, x, size_y - y); 560 | glUniform2f(glsl.loc_image_size, img->width, -img->height); 561 | 562 | glBindBuffer(GL_ARRAY_BUFFER, imgctx->vbuffer); 563 | glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2*sizeof(GLfloat), (void*)0); 564 | glEnableVertexAttribArray(0); 565 | 566 | glEnable(GL_BLEND); 567 | glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA); // src alpha 0xFF : keep dst 568 | glBlendEquation(GL_FUNC_ADD); 569 | 570 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 571 | glDisableVertexAttribArray(0); 572 | 573 | } 574 | 575 | 576 | - (void) mlx_gl_draw_font:(mlx_img_list_t *)img andCtx:(mlx_img_ctx_t *)imgctx andX:(int)x andY:(int)y andColor:(int)color glyphX:(int)gx glyphY:(int)gy 577 | { 578 | GLfloat color_tab[4]; 579 | 580 | if (pixel_nb >0) 581 | [self mlx_gl_draw]; 582 | 583 | color_tab[0] = ((float)((color&0xFF0000)>>16))/255.0; 584 | color_tab[1] = ((float)((color&0xFF00)>>8))/255.0; 585 | color_tab[2] = ((float)((color&0xFF)>>0))/255.0; 586 | color_tab[3] = 1.0; 587 | glUseProgram(glsl.font_program); 588 | 589 | glActiveTexture(GL_TEXTURE0); 590 | glBindTexture(GL_TEXTURE_2D, imgctx->texture); 591 | glUniform1i(glsl.loc_font_texture, 0); 592 | glUniform4fv(glsl.loc_font_color, 1, color_tab); 593 | 594 | glUniform2f(glsl.loc_font_winhalfsize, size_x/2, size_y/2); 595 | glUniform2f(glsl.loc_font_posinwin, x, size_y - 1 - y); 596 | glUniform2f(glsl.loc_font_posinatlas, gx, gy); 597 | glUniform2f(glsl.loc_font_atlassize, img->width, img->height); 598 | 599 | glBindBuffer(GL_ARRAY_BUFFER, imgctx->vbuffer); 600 | glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2*sizeof(GLfloat), (void*)0); 601 | glEnableVertexAttribArray(0); 602 | 603 | glEnable(GL_BLEND); 604 | glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA); // src alpha 0xFF : keep dst 605 | glBlendEquation(GL_FUNC_ADD); 606 | 607 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 608 | glDisableVertexAttribArray(0); 609 | 610 | } 611 | 612 | 613 | - (void) mlx_gl_draw 614 | { 615 | if (pixel_nb <= 0) 616 | return ; 617 | 618 | glUseProgram(glsl.pixel_program); 619 | 620 | glActiveTexture(GL_TEXTURE0); 621 | glBindTexture(GL_TEXTURE_2D, pixel_vbuffer); 622 | glUniform1i(glsl.loc_pixel_texture, 0); 623 | 624 | glBindBuffer(GL_ARRAY_BUFFER, pixel_vbuffer); 625 | glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2*sizeof(GLfloat), (void*)0); 626 | glEnableVertexAttribArray(0); 627 | 628 | glEnable(GL_BLEND); 629 | glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA); // src alpha 0xFF : keep dst 630 | glBlendEquation(GL_FUNC_ADD); 631 | 632 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 633 | glDisableVertexAttribArray(0); 634 | 635 | pixel_nb = size_x*size_y; 636 | while (pixel_nb--) pixtexbuff[pixel_nb] = 0xFF000000; 637 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, size_x, size_y, 0, GL_BGRA, GL_UNSIGNED_BYTE, pixtexbuff); 638 | pixel_nb = 0; 639 | 640 | } 641 | 642 | @end 643 | 644 | 645 | // mlx API 646 | 647 | 648 | void *mlx_new_window(mlx_ptr_t *mlx_ptr, int size_x, int size_y, char *title) 649 | { 650 | mlx_win_list_t *newwin; 651 | NSString *str; 652 | 653 | if ((newwin = malloc(sizeof(*newwin))) == NULL) 654 | return ((void *)0); 655 | newwin->img_list = NULL; 656 | newwin->next = mlx_ptr->win_list; 657 | newwin->nb_flush = 0; 658 | newwin->pixmgt = 1; 659 | mlx_ptr->win_list = newwin; 660 | 661 | NSRect windowRect = NSMakeRect(100, 100, size_x, size_y); 662 | str = [NSString stringWithCString:title encoding:NSASCIIStringEncoding]; 663 | newwin->winid = [[MlxWin alloc] initWithRect:windowRect andTitle:str pfaAttrs:pfa_attrs]; 664 | if (newwin->winid) 665 | if (![(id)(newwin->winid) pixel_management]) 666 | { 667 | [(id)(newwin->winid) destroyPixelManagement]; 668 | [(id)(newwin->winid) destroyMe]; 669 | } 670 | return ((void *)newwin); 671 | } 672 | 673 | 674 | void mlx_clear_window(mlx_ptr_t *mlx_ptr, mlx_win_list_t *win_ptr) 675 | { 676 | [(id)(win_ptr->winid) selectGLContext]; 677 | [(id)(win_ptr->winid) clearWin]; 678 | win_ptr->nb_flush ++; 679 | } 680 | 681 | 682 | void mlx_expose_hook(mlx_win_list_t *win_ptr, int (*funct_ptr)(), void *param) 683 | { 684 | [(id)(win_ptr->winid) setEvent:12 andFunc:funct_ptr andParam:param]; 685 | } 686 | 687 | void mlx_key_hook(mlx_win_list_t *win_ptr, int (*funct_ptr)(), void *param) 688 | { 689 | [(id)(win_ptr->winid) setEvent:3 andFunc:funct_ptr andParam:param]; 690 | } 691 | 692 | void mlx_mouse_hook(mlx_win_list_t *win_ptr, int (*funct_ptr)(), void *param) 693 | { 694 | [(id)(win_ptr->winid) setEvent:4 andFunc:funct_ptr andParam:param]; 695 | } 696 | 697 | void mlx_hook(mlx_win_list_t *win_ptr, int x_event, int x_mask, int (*funct_ptr)(), void *param) 698 | { 699 | [(id)(win_ptr->winid) setEvent:x_event andFunc:funct_ptr andParam:param]; 700 | } 701 | 702 | int mlx_do_key_autorepeatoff(mlx_ptr_t *mlx_ptr) 703 | { 704 | mlx_win_list_t *win; 705 | 706 | win = mlx_ptr->win_list; 707 | while (win) 708 | { 709 | [(id)(win->winid) setKeyRepeat:0]; 710 | win = win->next; 711 | } 712 | return (0); 713 | } 714 | 715 | int mlx_do_key_autorepeaton(mlx_ptr_t *mlx_ptr) 716 | { 717 | mlx_win_list_t *win; 718 | 719 | win = mlx_ptr->win_list; 720 | while (win) 721 | { 722 | [(id)(win->winid) setKeyRepeat:1]; 723 | win = win->next; 724 | } 725 | return (0); 726 | } 727 | 728 | 729 | int mlx_destroy_window(mlx_ptr_t *mlx_ptr, mlx_win_list_t *win_to_del) 730 | { 731 | mlx_win_list_t first; 732 | mlx_win_list_t *win; 733 | mlx_img_ctx_t *ctx; 734 | mlx_img_ctx_t *ctx2; 735 | 736 | first.next = mlx_ptr->win_list; 737 | win = &first; 738 | while (win && win->next) 739 | { 740 | if (win->next == win_to_del) 741 | win->next = win->next->next; 742 | win = win->next; 743 | } 744 | mlx_ptr->win_list = first.next; 745 | 746 | if (win_to_del->pixmgt) 747 | { 748 | [(id)(win_to_del->winid) selectGLContext]; 749 | ctx = win_to_del->img_list; // should be null anyway if no pixel management 750 | while (ctx) 751 | { 752 | glDeleteBuffers(1, &(ctx->vbuffer)); 753 | glDeleteTextures(1, &(ctx->texture)); 754 | ctx2 = ctx; 755 | ctx = ctx->next; 756 | free(ctx2); 757 | } 758 | [(id)(win_to_del->winid) destroyPixelManagement]; 759 | } 760 | [(id)(win_to_del->winid) destroyMe]; 761 | free(win_to_del); 762 | 763 | // printf("destroy window done.\n"); 764 | mlx_do_sync(mlx_ptr); 765 | return (0); 766 | } 767 | --------------------------------------------------------------------------------