Started from buttom go to the sky
This commit is contained in:
parent
96215449bd
commit
f811e55dea
4781 changed files with 10121 additions and 1743 deletions
74
stdme/src/blx/blx.c
Normal file
74
stdme/src/blx/blx.c
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* blx.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/14 18:01:06 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/31 20:14:31 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/blx/blx.h"
|
||||
#include "me/blx/blx_handlers.h"
|
||||
#include "me/blx/sprite.h"
|
||||
#include "me/blx/inputs.h"
|
||||
#include "me/blx/xdata.h"
|
||||
#include "me/types.h"
|
||||
#include "me/vec/vec_u8.h"
|
||||
#include "me/printf/printf.h"
|
||||
#include <mlx.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void blx_create_fontsheet(t_blx *app);
|
||||
|
||||
t_blx blx_initialize(t_run_function func, t_free_function free_fn,
|
||||
t_blx_app data)
|
||||
{
|
||||
t_blx ctx;
|
||||
|
||||
ctx = (t_blx){.func = func, .app = data, .free = free_fn};
|
||||
ctx.mlx = mlx_init();
|
||||
if (ctx.mlx == NULL)
|
||||
(me_eprintf("Error:\nfailed to inialize blx (mlx) !\n"), exit(1));
|
||||
ctx.inputs = create_inputs_manager(&ctx);
|
||||
ctx.win = mlx_new_window(ctx.mlx, data.size_x * data.pixel_size, data.size_y
|
||||
* data.pixel_size, data.title);
|
||||
if (!blx_sprite_new(&ctx, data.size_x * data.pixel_size, data.size_y
|
||||
* data.pixel_size, &ctx._data.screen))
|
||||
(me_eprintf("Error:\nfailed to inialize blx (screen) !\n"), exit(1));
|
||||
blx_create_fontsheet(&ctx);
|
||||
return (ctx);
|
||||
}
|
||||
|
||||
//mlx_do_key_autorepeatoff(app.mlx);
|
||||
void blx_run(t_blx app)
|
||||
{
|
||||
mlx_hook(app.win, KEYPRESS, KEYPRESSMASK, &blx_key_pressed_handler, &app);
|
||||
mlx_hook(app.win, KEYRELEASE, KEYRELEASEMASK, &blx_key_released_handler,
|
||||
&app);
|
||||
mlx_hook(app.win, DESTROYNOTIFY, NOEVENTMASK, &blx_key_exit_handler, &app);
|
||||
mlx_loop_hook(app.mlx, &blx_loop_func, &app);
|
||||
mlx_loop(app.mlx);
|
||||
}
|
||||
|
||||
void blx_free(t_blx app)
|
||||
{
|
||||
blx_sprite_free(app._data.screen);
|
||||
blx_sprite_free(app._data.font);
|
||||
mlx_do_key_autorepeaton(app.mlx);
|
||||
if (app.free)
|
||||
app.free(app.app);
|
||||
if (app.win)
|
||||
mlx_destroy_window(app.mlx, app.win);
|
||||
if (app.mlx)
|
||||
{
|
||||
mlx_destroy_display(app.mlx);
|
||||
free(app.mlx);
|
||||
}
|
||||
vec_u8_free(app.inputs.keysyms_held);
|
||||
vec_u8_free(app.inputs.keysyms_pressed);
|
||||
vec_u8_free(app.inputs.keysyms_released);
|
||||
}
|
||||
29
stdme/src/blx/blx_create_fontsheet.c
Normal file
29
stdme/src/blx/blx_create_fontsheet.c
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* blx_create_fontsheet.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/27 19:18:44 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/31 20:10:02 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/blx/blx.h"
|
||||
#include "me/blx/sprite.h"
|
||||
#include "me/types.h"
|
||||
#include "me/printf/printf.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define FONT_SHEET_PATH "textures/font.xpm"
|
||||
|
||||
void blx_create_fontsheet(t_blx *app)
|
||||
{
|
||||
if (!blx_sprite_from_xpm(app, FONT_SHEET_PATH, &app->_data.font))
|
||||
{
|
||||
me_eprintf("Error:\nCouldn't open sprite sheet !\n");
|
||||
exit(5);
|
||||
}
|
||||
}
|
||||
53
stdme/src/blx/blx_handlers.c
Normal file
53
stdme/src/blx/blx_handlers.c
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* blx_handlers.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/21 17:44:55 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/21 19:59:47 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/blx/blx.h"
|
||||
#include "me/blx/blx_handlers.h"
|
||||
#include "me/blx/blx_key.h"
|
||||
#include <mlx.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int blx_key_pressed_handler(t_keysym keysym, t_blx *ctx)
|
||||
{
|
||||
t_usize bit_index;
|
||||
|
||||
bit_index = keysym_to_bit_index(keysym);
|
||||
if (bit_index / 8 >= ctx->inputs.keysyms_pressed.len)
|
||||
return (0);
|
||||
if (is_key_held(ctx, bit_index))
|
||||
return (0);
|
||||
ctx->inputs.keysyms_pressed.buffer[bit_index / 8] |= ((t_u8)1 << (bit_index
|
||||
% 8));
|
||||
return (1);
|
||||
}
|
||||
|
||||
int blx_key_released_handler(t_keysym keysym, t_blx *ctx)
|
||||
{
|
||||
t_usize bit_index;
|
||||
|
||||
bit_index = keysym_to_bit_index(keysym);
|
||||
if (bit_index / 8 >= ctx->inputs.keysyms_released.len)
|
||||
return (0);
|
||||
ctx->inputs.keysyms_held.buffer[bit_index / 8] &= ~((t_u8)1 << (bit_index
|
||||
% 8));
|
||||
ctx->inputs.keysyms_pressed.buffer[bit_index / 8] &= ~((t_u8)1 << (bit_index
|
||||
% 8));
|
||||
ctx->inputs.keysyms_released.buffer[bit_index / 8] |= ((t_u8)1 << (bit_index
|
||||
% 8));
|
||||
return (1);
|
||||
}
|
||||
|
||||
int blx_key_exit_handler(t_blx *app)
|
||||
{
|
||||
app->_data.exit = true;
|
||||
return (1);
|
||||
}
|
||||
50
stdme/src/blx/blx_keycode.c
Normal file
50
stdme/src/blx/blx_keycode.c
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* blx_keysym.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/14 15:07:52 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/14 15:58:17 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/blx/blx.h"
|
||||
#include "me/blx/blx_key.h"
|
||||
#include "me/types.h"
|
||||
#include "me/vec/vec_u8.h"
|
||||
|
||||
t_usize keysym_to_bit_index(t_keysym key)
|
||||
{
|
||||
if (key < 0xff00)
|
||||
return ((((t_usize)key) & 0xff) + 0x0000);
|
||||
if (key >= 0xff00)
|
||||
return ((((t_usize)key) & 0xff) + 0x0100);
|
||||
return (0);
|
||||
}
|
||||
|
||||
bool get_key(t_vec_u8 *key_storage, t_keysym keysym)
|
||||
{
|
||||
t_usize index;
|
||||
|
||||
index = keysym_to_bit_index(keysym);
|
||||
if (index == 0 || index / 8 > key_storage->len)
|
||||
return (false);
|
||||
return (!!(key_storage->buffer[index / 8] & (1 << (index % 8))));
|
||||
}
|
||||
|
||||
bool is_key_pressed(t_blx *ctx, t_keysym key)
|
||||
{
|
||||
return (get_key(&ctx->inputs.keysyms_pressed, key));
|
||||
}
|
||||
|
||||
bool is_key_held(t_blx *ctx, t_keysym key)
|
||||
{
|
||||
return (get_key(&ctx->inputs.keysyms_held, key));
|
||||
}
|
||||
|
||||
bool is_key_released(t_blx *ctx, t_keysym key)
|
||||
{
|
||||
return (get_key(&ctx->inputs.keysyms_released, key));
|
||||
}
|
||||
24
stdme/src/blx/colors.c
Normal file
24
stdme/src/blx/colors.c
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* colors.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/14 16:01:18 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/27 16:26:30 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/blx/colors.h"
|
||||
#include "me/types.h"
|
||||
|
||||
t_color new_color_with_alpha(t_u8 r, t_u8 g, t_u8 b, t_u8 alpha)
|
||||
{
|
||||
return ((t_color){.r = r, .g = g, .b = b, .a = alpha});
|
||||
}
|
||||
|
||||
t_color new_color(t_u8 r, t_u8 g, t_u8 b)
|
||||
{
|
||||
return (new_color_with_alpha(r, g, b, 0x00));
|
||||
}
|
||||
42
stdme/src/blx/draw/draw.c
Normal file
42
stdme/src/blx/draw/draw.c
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* draw.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/21 20:19:59 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/31 15:05:17 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/blx/blx.h"
|
||||
#include "me/blx/sprite.h"
|
||||
#include <mlx.h>
|
||||
#include <stdio.h>
|
||||
|
||||
void blx_clear(t_blx *app, t_color col)
|
||||
{
|
||||
sprite_clear(&app->_data.screen, col);
|
||||
}
|
||||
|
||||
void blx_draw(t_blx *app, t_vi2d pos, t_color col)
|
||||
{
|
||||
t_usize i;
|
||||
t_usize j;
|
||||
|
||||
if (get_draw_mode(app) == MASK && col.a != 0)
|
||||
return ;
|
||||
i = 0;
|
||||
while (i < app->app.pixel_size)
|
||||
{
|
||||
j = 0;
|
||||
while (j < app->app.pixel_size)
|
||||
{
|
||||
sprite_draw(&app->_data.screen, vi2d(pos.x * app->app.pixel_size
|
||||
+ j, pos.y * app->app.pixel_size + i), col);
|
||||
j++;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
34
stdme/src/blx/draw/draw_sprite.c
Normal file
34
stdme/src/blx/draw/draw_sprite.c
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* draw_sprite.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/26 22:12:31 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/31 15:06:55 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/blx/blx.h"
|
||||
#include "me/blx/sprite.h"
|
||||
#include "me/types.h"
|
||||
|
||||
void draw_sprite(t_blx *app, t_vi2d pos, t_sprite *spr)
|
||||
{
|
||||
t_vi2d p;
|
||||
t_color col;
|
||||
|
||||
p.y = 0;
|
||||
while (p.y < spr->height)
|
||||
{
|
||||
p.x = 0;
|
||||
while (p.x < spr->width)
|
||||
{
|
||||
sprite_get_pixel(spr, p, &col);
|
||||
blx_draw(app, vi2d_add(pos, p), col);
|
||||
p.x++;
|
||||
}
|
||||
p.y++;
|
||||
}
|
||||
}
|
||||
82
stdme/src/blx/draw/draw_string.c
Normal file
82
stdme/src/blx/draw/draw_string.c
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* draw_string.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/27 19:02:59 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/31 15:31:57 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/blx/blx.h"
|
||||
#include "me/blx/colors.h"
|
||||
#include "me/blx/sprite.h"
|
||||
#include "me/types.h"
|
||||
#include "me/vec2/vec2.h"
|
||||
#include <stdio.h>
|
||||
|
||||
static inline void inner(t_blx *app, char c, t_vi2d pixel_pos, t_color col)
|
||||
{
|
||||
t_vi2d o;
|
||||
t_color texel;
|
||||
t_vi2d offset;
|
||||
|
||||
o.x = (c - 32) % 16;
|
||||
o.y = (c - 32) / 16;
|
||||
texel = new_color(0, 0, 0);
|
||||
offset.y = 0;
|
||||
while (offset.y < 8)
|
||||
{
|
||||
offset.x = 0;
|
||||
while (offset.x < 8)
|
||||
{
|
||||
if (!sprite_get_pixel(&app->_data.font, vi2d(offset.x + o.x * 8,
|
||||
offset.y + o.y * 8), &texel) && texel.a == 0)
|
||||
blx_draw(app, vi2d_add(pixel_pos, offset), col);
|
||||
offset.x++;
|
||||
}
|
||||
offset.y++;
|
||||
}
|
||||
}
|
||||
|
||||
static inline t_draw_mode handle_draw_mode(t_blx *app, t_color col)
|
||||
{
|
||||
t_draw_mode m;
|
||||
|
||||
m = get_draw_mode(app);
|
||||
if (col.a != 0x00)
|
||||
set_draw_mode(app, ALPHA);
|
||||
else
|
||||
set_draw_mode(app, MASK);
|
||||
return (m);
|
||||
}
|
||||
|
||||
void blx_draw_string(t_blx *app, t_vi2d pos, t_const_str sText, t_color col)
|
||||
{
|
||||
t_vi2d s;
|
||||
t_draw_mode m;
|
||||
char c;
|
||||
|
||||
s.x = 0;
|
||||
s.y = 0;
|
||||
m = handle_draw_mode(app, col);
|
||||
while (*sText)
|
||||
{
|
||||
c = *sText++;
|
||||
if (c == '\n')
|
||||
{
|
||||
s.x = 0;
|
||||
s.y += 8;
|
||||
}
|
||||
else if (c == '\t')
|
||||
s.x += 8 * 4;
|
||||
else
|
||||
{
|
||||
inner(app, c, vi2d(pos.x + s.x, pos.y + s.y), col);
|
||||
s.x += 8;
|
||||
}
|
||||
}
|
||||
set_draw_mode(app, m);
|
||||
}
|
||||
40
stdme/src/blx/inputs.c
Normal file
40
stdme/src/blx/inputs.c
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* inputs.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/15 16:38:08 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/30 18:15:42 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/blx/blx.h"
|
||||
#include "me/blx/inputs.h"
|
||||
#include "me/vec/vec_u8.h"
|
||||
#include <stdio.h>
|
||||
|
||||
static t_vec_u8 alloc_input_vector(void)
|
||||
{
|
||||
t_vec_u8 out;
|
||||
|
||||
out = vec_u8_new(64, NULL);
|
||||
while (out.len < 64)
|
||||
vec_u8_push(&out, 0);
|
||||
return (out);
|
||||
}
|
||||
|
||||
t_blx_input create_inputs_manager(t_blx *ctx)
|
||||
{
|
||||
t_blx_input out;
|
||||
|
||||
(void)(ctx);
|
||||
out = (t_blx_input){
|
||||
.mouse = 0,
|
||||
.keysyms_held = alloc_input_vector(),
|
||||
.keysyms_pressed = alloc_input_vector(),
|
||||
.keysyms_released = alloc_input_vector(),
|
||||
};
|
||||
return (out);
|
||||
}
|
||||
60
stdme/src/blx/logic.c
Normal file
60
stdme/src/blx/logic.c
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* logic.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/15 18:06:06 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/25 18:33:12 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/blx/blx.h"
|
||||
#include "me/blx/blx_key.h"
|
||||
#include "me/blx/sprite.h"
|
||||
#include "me/blx/inputs.h"
|
||||
#include "mlx.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static void blx_stop(t_blx *app)
|
||||
{
|
||||
mlx_loop_end(app->mlx);
|
||||
blx_free(*app);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
// TODO: implement mouse buttons when needed
|
||||
void blx_post_func(t_blx *ctx)
|
||||
{
|
||||
t_usize idx;
|
||||
|
||||
idx = 0;
|
||||
while (idx < ctx->inputs.keysyms_pressed.len
|
||||
&& idx < ctx->inputs.keysyms_held.len)
|
||||
{
|
||||
ctx->inputs.keysyms_held.buffer[idx] |= \
|
||||
ctx->inputs.keysyms_pressed.buffer[idx];
|
||||
ctx->inputs.keysyms_pressed.buffer[idx] = 0;
|
||||
idx++;
|
||||
}
|
||||
idx = 0;
|
||||
while (idx < ctx->inputs.keysyms_pressed.len)
|
||||
ctx->inputs.keysyms_pressed.buffer[idx++] = 0;
|
||||
idx = 0;
|
||||
while (idx < ctx->inputs.keysyms_released.len)
|
||||
ctx->inputs.keysyms_released.buffer[idx++] = 0;
|
||||
}
|
||||
|
||||
int blx_loop_func(t_blx *ctx)
|
||||
{
|
||||
mlx_put_image_to_window(ctx->mlx, ctx->win, ctx->_data.screen.img, 0, 0);
|
||||
if (ctx->_data.exit || ctx->func(ctx))
|
||||
{
|
||||
blx_stop(ctx);
|
||||
return (0);
|
||||
}
|
||||
blx_post_func(ctx);
|
||||
return (0);
|
||||
}
|
||||
21
stdme/src/blx/sprite/draw_image.c
Normal file
21
stdme/src/blx/sprite/draw_image.c
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* draw_image.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/21 22:43:04 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/31 15:20:24 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/blx/blx.h"
|
||||
#include "me/blx/colors.h"
|
||||
#include "me/blx/sprite.h"
|
||||
#include <mlx.h>
|
||||
|
||||
void blx_draw_sprite_raw(t_blx *ctx, t_vi2d pos, t_sprite *img)
|
||||
{
|
||||
mlx_put_image_to_window(ctx->mlx, ctx->win, img->img, pos.x, pos.y);
|
||||
}
|
||||
78
stdme/src/blx/sprite/draw_pixel_onto.c
Normal file
78
stdme/src/blx/sprite/draw_pixel_onto.c
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* draw_pixel_onto.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/23 16:32:19 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/31 15:21:33 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/blx/blx.h"
|
||||
#include "me/blx/sprite.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void sprite_draw_other_bpp(t_sprite *spr, t_vi2d pos, t_color col)
|
||||
{
|
||||
(void)(spr);
|
||||
(void)(pos);
|
||||
(void)(col);
|
||||
return ;
|
||||
}
|
||||
|
||||
void sprite_draw(t_sprite *spr, t_vi2d pos, t_color col)
|
||||
{
|
||||
t_u8 *addr;
|
||||
|
||||
if (!(pos.x >= 0 && pos.x < spr->width && pos.y >= 0
|
||||
&& pos.y < spr->height))
|
||||
return ;
|
||||
if (spr->bpp != 32)
|
||||
return (sprite_draw_other_bpp(spr, pos, col));
|
||||
addr = &(spr->data)[pos.y * spr->line_size + pos.x * 4];
|
||||
if (!spr->big_endian)
|
||||
{
|
||||
addr[0] = col.b;
|
||||
addr[1] = col.g;
|
||||
addr[2] = col.r;
|
||||
addr[3] = col.a;
|
||||
}
|
||||
else
|
||||
{
|
||||
addr[0] = col.a;
|
||||
addr[1] = col.r;
|
||||
addr[2] = col.g;
|
||||
addr[3] = col.b;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
addr = &(img->data)[y * img->line_size + (x << 2)];
|
||||
if (!img->big_endian)
|
||||
out = ((unsigned long)addr[0] << 24 | (unsigned long)addr[1] << 16 |
|
||||
(unsigned long)addr[2] << 8 | addr[3]);
|
||||
else
|
||||
out = ((unsigned long)addr[3] << 24 | (unsigned long)addr[2] << 16 |
|
||||
(unsigned long)addr[1] << 8 | addr[0]);
|
||||
return (out);
|
||||
*/
|
||||
|
||||
void sprite_clear(t_sprite *img, t_color col)
|
||||
{
|
||||
t_vi2d pos;
|
||||
|
||||
pos.y = 0;
|
||||
while (pos.y < img->height)
|
||||
{
|
||||
pos.x = 0;
|
||||
while (pos.x < img->width)
|
||||
{
|
||||
sprite_draw(img, pos, col);
|
||||
pos.x++;
|
||||
}
|
||||
pos.y++;
|
||||
}
|
||||
}
|
||||
82
stdme/src/blx/sprite/draw_string.c
Normal file
82
stdme/src/blx/sprite/draw_string.c
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* draw_string.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboxy[1]er <maiboxy[1]er@student.42.fr> +#+ +:+ +#+
|
||||
*/
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/27 18:29:56 by maiboxy[1]er #+# #+# */
|
||||
/* Updated: 2023/12/27 18:43:57 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/blx/blx.h"
|
||||
#include "me/blx/colors.h"
|
||||
#include "me/blx/sprite.h"
|
||||
#include "me/types.h"
|
||||
|
||||
static inline void inner(t_sprite *spr, char c, t_vi2d pixel_pos, t_color col)
|
||||
{
|
||||
t_vi2d o;
|
||||
t_color texel;
|
||||
t_vi2d offset;
|
||||
|
||||
o.x = (c - 32) % 16;
|
||||
o.y = (c - 32) / 16;
|
||||
texel = new_color(0, 0, 0);
|
||||
offset.y = 0;
|
||||
while (offset.y < 8)
|
||||
{
|
||||
offset.x = 0;
|
||||
while (offset.x < 8)
|
||||
{
|
||||
if (!sprite_get_pixel(&spr->ctx->_data.font, vi2d(offset.x + o.x
|
||||
* 8, offset.y + o.y * 8), &texel) && texel.a == 0)
|
||||
sprite_draw(spr, vi2d_add(pixel_pos, offset), col);
|
||||
offset.x++;
|
||||
}
|
||||
offset.y++;
|
||||
}
|
||||
}
|
||||
|
||||
static inline t_draw_mode handle_draw_mode(t_blx *app, t_color col)
|
||||
{
|
||||
t_draw_mode m;
|
||||
|
||||
m = get_draw_mode(app);
|
||||
if (col.a != 0x00)
|
||||
set_draw_mode(app, ALPHA);
|
||||
else
|
||||
set_draw_mode(app, MASK);
|
||||
return (m);
|
||||
}
|
||||
|
||||
void sprite_draw_string(t_sprite *spr, t_vi2d pos, t_const_str sText,
|
||||
t_color col)
|
||||
{
|
||||
t_vi2d s;
|
||||
t_draw_mode m;
|
||||
char c;
|
||||
|
||||
s.x = 0;
|
||||
s.y = 0;
|
||||
m = handle_draw_mode(spr->ctx, col);
|
||||
while (*sText)
|
||||
{
|
||||
c = *sText++;
|
||||
if (c == '\n')
|
||||
{
|
||||
s.x = 0;
|
||||
s.y += 8;
|
||||
}
|
||||
else if (c == '\t')
|
||||
s.x += 8 * 4;
|
||||
else
|
||||
{
|
||||
inner(spr, c, vi2d(pos.x + s.x, pos.y + s.y), col);
|
||||
s.x += 8;
|
||||
}
|
||||
}
|
||||
set_draw_mode(spr->ctx, m);
|
||||
}
|
||||
20
stdme/src/blx/sprite/free_image.c
Normal file
20
stdme/src/blx/sprite/free_image.c
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* free_image.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/21 22:40:12 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/26 15:39:04 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/blx/blx.h"
|
||||
#include "me/blx/sprite.h"
|
||||
#include <mlx.h>
|
||||
|
||||
void blx_sprite_free(t_sprite img)
|
||||
{
|
||||
mlx_destroy_image(img.ctx->mlx, img.img);
|
||||
}
|
||||
51
stdme/src/blx/sprite/get_pixel.c
Normal file
51
stdme/src/blx/sprite/get_pixel.c
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_pixel.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/24 00:48:23 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/31 15:27:55 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/blx/colors.h"
|
||||
#include "me/blx/sprite.h"
|
||||
|
||||
static bool sprite_get_pixel_other_bpp(t_sprite *spr, t_vi2d pos, t_color *out)
|
||||
{
|
||||
(void)(spr);
|
||||
(void)(pos);
|
||||
(void)(out);
|
||||
return (true);
|
||||
}
|
||||
|
||||
bool sprite_get_pixel(t_sprite *spr, t_vi2d pos, t_color *out)
|
||||
{
|
||||
t_u8 *addr;
|
||||
t_color col;
|
||||
|
||||
if (!(pos.x >= 0 && pos.x < spr->width && pos.y >= 0
|
||||
&& pos.y < spr->height))
|
||||
return (true);
|
||||
if (spr->bpp != 32)
|
||||
return (sprite_get_pixel_other_bpp(spr, pos, out));
|
||||
addr = &(spr->data)[pos.y * spr->line_size + pos.x * 4];
|
||||
if (spr->big_endian)
|
||||
{
|
||||
col.a = addr[0];
|
||||
col.r = addr[1];
|
||||
col.g = addr[2];
|
||||
col.b = addr[3];
|
||||
}
|
||||
else
|
||||
{
|
||||
col.a = addr[3];
|
||||
col.r = addr[2];
|
||||
col.g = addr[1];
|
||||
col.b = addr[0];
|
||||
}
|
||||
*out = col;
|
||||
return (false);
|
||||
}
|
||||
56
stdme/src/blx/sprite/new_image.c
Normal file
56
stdme/src/blx/sprite/new_image.c
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* new_image.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/21 21:59:04 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/26 15:40:08 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/blx/blx.h"
|
||||
#include "me/blx/colors.h"
|
||||
#include "me/blx/sprite.h"
|
||||
#include <mlx.h>
|
||||
|
||||
bool blx_sprite_from_xpm(t_blx *ctx, t_str path, t_sprite *out)
|
||||
{
|
||||
t_sprite spr;
|
||||
t_i32 bpp;
|
||||
t_i32 size_line;
|
||||
t_i32 endian;
|
||||
|
||||
spr.ctx = ctx;
|
||||
spr.img = mlx_xpm_file_to_image(ctx->mlx, path, &spr.width, &spr.height);
|
||||
if (spr.img == NULL)
|
||||
return (false);
|
||||
spr.data = (t_u8 *)mlx_get_data_addr(spr.img, &bpp, &size_line, &endian);
|
||||
spr.bpp = bpp;
|
||||
spr.line_size = (t_usize)size_line;
|
||||
spr.big_endian = endian == 1;
|
||||
*out = spr;
|
||||
return (true);
|
||||
}
|
||||
|
||||
bool blx_sprite_new(t_blx *ctx, t_i32 width, t_i32 height, t_sprite *out)
|
||||
{
|
||||
t_sprite spr;
|
||||
t_i32 bpp;
|
||||
t_i32 size_line;
|
||||
t_i32 endian;
|
||||
|
||||
spr.ctx = ctx;
|
||||
spr.img = mlx_new_image(ctx->mlx, width, height);
|
||||
if (spr.img == NULL)
|
||||
return (false);
|
||||
spr.data = (t_u8 *)mlx_get_data_addr(spr.img, &bpp, &size_line, &endian);
|
||||
spr.bpp = bpp;
|
||||
spr.line_size = (t_usize)size_line;
|
||||
spr.big_endian = endian == 1;
|
||||
spr.width = width;
|
||||
spr.height = height;
|
||||
*out = spr;
|
||||
return (true);
|
||||
}
|
||||
36
stdme/src/blx/sprite/sprite_draw_onto_sprite.c
Normal file
36
stdme/src/blx/sprite/sprite_draw_onto_sprite.c
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* sprite_draw_onto_sprite.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/26 19:33:22 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/31 15:26:41 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/blx/blx.h"
|
||||
#include "me/blx/colors.h"
|
||||
#include "me/blx/sprite.h"
|
||||
#include "me/types.h"
|
||||
#include "me/vec2/vec2.h"
|
||||
|
||||
void sprite_draw_onto(t_sprite *dest, t_vi2d pos, t_sprite *source)
|
||||
{
|
||||
t_vi2d p;
|
||||
t_color col;
|
||||
|
||||
p.y = 0;
|
||||
while (p.y < source->height)
|
||||
{
|
||||
p.x = 0;
|
||||
while (p.x < source->width)
|
||||
{
|
||||
sprite_get_pixel(source, p, &col);
|
||||
sprite_draw(dest, vi2d_add(pos, p), col);
|
||||
p.x++;
|
||||
}
|
||||
p.y++;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue