feat: base + display functions!!!!!
This commit is contained in:
parent
ebb5931632
commit
664b18eb2e
187 changed files with 29451 additions and 0 deletions
61
src/assets.c
Normal file
61
src/assets.c
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* assets.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/26 16:27:06 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/02/29 18:05:41 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "display.h"
|
||||
|
||||
/**
|
||||
* Path of each asset relative to assets/, without extension.
|
||||
*/
|
||||
const t_asset g_assetsmap[NB_ASSETS] = {
|
||||
{"floor/1", 50, 100, 50, 100, true},
|
||||
{"floor/2", 50, 100, 50, 100, true},
|
||||
// WALL
|
||||
{"wall/border", 50, 100, 50, 100, true},
|
||||
{"wall/inner", 50, 100, 30, 100, false},
|
||||
// OBJECTS
|
||||
// snas
|
||||
{"object/snas/down", 50, 100, 30, 100, false},
|
||||
{"object/snas/up", 50, 100, 30, 100, false},
|
||||
{"object/snas/left", 50, 100, 30, 100, false},
|
||||
{"object/snas/right", 50, 100, 30, 100, false},
|
||||
// firsk
|
||||
{"object/firsk/down", 50, 110, 30, 100, false},
|
||||
{"object/firsk/up", 40, 100, 30, 100, false},
|
||||
{"object/firsk/left", 50, 100, 30, 100, false},
|
||||
{"object/firsk/right", 50, 110, 30, 100, false},
|
||||
// ketchup
|
||||
{"object/ketchup/still/1", 50, 100, 40, 110, false},
|
||||
{"object/ketchup/still/2", 50, 100, 40, 110, false},
|
||||
{"object/ketchup/still/3", 50, 100, 40, 110, false},
|
||||
{"object/ketchup/still/4", 50, 100, 40, 110, false},
|
||||
{"object/ketchup/fade/1", 50, 100, 40, 110, false},
|
||||
{"object/ketchup/fade/2", 50, 100, 30, 120, false},
|
||||
{"object/ketchup/fade/3", 50, 100, 20, 130, false},
|
||||
// exit
|
||||
{"object/exit/inactive/1", 50, 100, 50, 100, false},
|
||||
{"object/exit/inactive/2", 50, 100, 50, 100, false},
|
||||
{"object/exit/active/1", 50, 100, 50, 100, false},
|
||||
{"object/exit/active/2", 50, 100, 50, 100, false},
|
||||
{"object/exit/active/3", 50, 100, 50, 100, false},
|
||||
{"object/exit/active/4", 50, 100, 50, 100, false},
|
||||
{"object/exit/using1/1", 50, 100, 50, 100, false},
|
||||
{"object/exit/using1/2", 50, 100, 50, 100, false},
|
||||
{"object/exit/using1/3", 50, 100, 50, 100, false},
|
||||
{"object/exit/using1/4", 50, 100, 50, 100, false},
|
||||
{"object/exit/using2/1", 40, 110, 40, 110, false},
|
||||
{"object/exit/using2/2", 40, 110, 40, 110, false},
|
||||
{"object/exit/using2/3", 30, 120, 30, 120, false},
|
||||
{"object/exit/using2/4", 50, 100, 50, 100, false},
|
||||
{"object/exit/using2/5", 50, 100, 50, 100, false},
|
||||
{"object/exit/using2/6", 50, 100, 50, 100, false},
|
||||
{"object/exit/using2/7", 50, 100, 50, 100, false}
|
||||
};
|
||||
75
src/display1.c
Normal file
75
src/display1.c
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* display1.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/26 15:05:30 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/03/06 13:08:51 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "display.h"
|
||||
#include "display_utils.h"
|
||||
#include "env.h"
|
||||
#include "error.h"
|
||||
#include "mlx.h"
|
||||
#include "libtf.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
void display_init(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (i < NB_ASSETS)
|
||||
{
|
||||
load_xpm_file_with_alpha(i);
|
||||
i++;
|
||||
}
|
||||
g_screenbuf.img = mlx_new_image(g_env.mlx, WINDOW_WIDTH, WINDOW_HEIGHT);
|
||||
g_screenbuf.width = WINDOW_WIDTH;
|
||||
g_screenbuf.height = WINDOW_HEIGHT;
|
||||
g_screenbuf.data = get_data_addr(g_screenbuf.img);
|
||||
display_erase();
|
||||
}
|
||||
|
||||
void display_erase(void)
|
||||
{
|
||||
const unsigned int black = 0x00000000;
|
||||
int i;
|
||||
|
||||
i = -1;
|
||||
while (++i < WINDOW_WIDTH * WINDOW_HEIGHT)
|
||||
g_screenbuf.data[i] = black;
|
||||
}
|
||||
|
||||
void display_flush(void)
|
||||
{
|
||||
mlx_put_image_to_window(g_env.mlx, g_env.win, g_screenbuf.img, 0, 0);
|
||||
}
|
||||
|
||||
void display_destroyall(void)
|
||||
{
|
||||
int i;
|
||||
void *img;
|
||||
|
||||
i = 0;
|
||||
while (i < NB_ASSETS)
|
||||
{
|
||||
img = g_allassets[i].img;
|
||||
if (img)
|
||||
mlx_destroy_image(g_env.mlx, img);
|
||||
i++;
|
||||
}
|
||||
if (g_screenbuf.img)
|
||||
mlx_destroy_image(g_env.mlx, g_screenbuf.img);
|
||||
if (g_env.win)
|
||||
mlx_destroy_window(g_env.mlx, g_env.win);
|
||||
if (g_env.mlx)
|
||||
{
|
||||
mlx_destroy_display(g_env.mlx);
|
||||
free(g_env.mlx);
|
||||
}
|
||||
}
|
||||
109
src/display2.c
Normal file
109
src/display2.c
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* display2.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/26 15:05:30 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/03/06 13:40:18 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "display.h"
|
||||
#include "display_utils.h"
|
||||
#include "env.h"
|
||||
#include "error.h"
|
||||
#include "mlx.h"
|
||||
#include "libtf.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
static inline void add_color(t_pixel *dest, t_pixel src)
|
||||
{
|
||||
const uint16_t transparency = (uint8_t)(src >> 24);
|
||||
const uint16_t opacity = (uint8_t) ~ transparency;
|
||||
const t_pixel dest_value = *dest;
|
||||
|
||||
*dest = (
|
||||
(uint8_t)(dest_value >> 16) * transparency
|
||||
+ (uint8_t)(src >> 16) * opacity)
|
||||
>> 8 << 16
|
||||
| (
|
||||
(uint8_t)(dest_value >> 8) * transparency
|
||||
+ (uint8_t)(src >> 8) * opacity)
|
||||
>> 8 << 8
|
||||
| (
|
||||
(uint8_t)(dest_value >> 0) * transparency
|
||||
+ (uint8_t)(src >> 0) * opacity)
|
||||
>> 8;
|
||||
}
|
||||
|
||||
static inline t_imglimits get_limits(int x, int y, t_image img)
|
||||
{
|
||||
t_imglimits r;
|
||||
|
||||
r.xmin = -x;
|
||||
if (img.asset.limit_left > r.xmin)
|
||||
r.xmin = img.asset.limit_left;
|
||||
r.xmax = WINDOW_WIDTH - x;
|
||||
if (img.asset.limit_right < r.xmax)
|
||||
r.xmax = img.asset.limit_right;
|
||||
r.ymin = -y;
|
||||
if (img.asset.limit_up > r.ymin)
|
||||
r.ymin = img.asset.limit_up;
|
||||
r.ymax = WINDOW_HEIGHT - y;
|
||||
if (img.asset.limit_down < r.ymax)
|
||||
r.ymax = img.asset.limit_down;
|
||||
return (r);
|
||||
}
|
||||
|
||||
static inline void display_draw_alpha(int x, int y, t_image img)
|
||||
{
|
||||
const t_imglimits limits = get_limits(x, y, img);
|
||||
int img_x;
|
||||
int img_y;
|
||||
|
||||
img_y = limits.ymin;
|
||||
while (img_y < limits.ymax)
|
||||
{
|
||||
img_x = limits.xmin;
|
||||
while (img_x < limits.xmax)
|
||||
{
|
||||
add_color(
|
||||
&g_screenbuf.data[(y + img_y) * WINDOW_WIDTH + (x + img_x)],
|
||||
img.data[img_y * img.width + img_x]);
|
||||
img_x++;
|
||||
}
|
||||
img_y++;
|
||||
}
|
||||
}
|
||||
|
||||
static inline void display_draw_opaque(int x, int y, t_image img)
|
||||
{
|
||||
const t_imglimits limits = get_limits(x, y, img);
|
||||
int img_x;
|
||||
int img_y;
|
||||
|
||||
img_y = limits.ymin;
|
||||
while (img_y < limits.ymax)
|
||||
{
|
||||
img_x = limits.xmin;
|
||||
while (img_x < limits.xmax)
|
||||
{
|
||||
g_screenbuf.data[(y + img_y) * WINDOW_WIDTH + (x + img_x)]
|
||||
= img.data[img_y * img.width + img_x];
|
||||
img_x++;
|
||||
}
|
||||
img_y++;
|
||||
}
|
||||
}
|
||||
|
||||
void display_draw(int x, int y, t_assetid asset)
|
||||
{
|
||||
const t_image img = g_allassets[asset];
|
||||
|
||||
if (img.asset.opaque)
|
||||
display_draw_opaque(x, y, img);
|
||||
else
|
||||
display_draw_alpha(x, y, img);
|
||||
}
|
||||
72
src/display_utils.c
Normal file
72
src/display_utils.c
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* display_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/06 13:03:30 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/03/06 13:48:02 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "display_utils.h"
|
||||
#include "mlx.h"
|
||||
#include "error.h"
|
||||
#include "env.h"
|
||||
|
||||
t_image g_allassets[NB_ASSETS];
|
||||
t_image g_screenbuf;
|
||||
|
||||
t_pixel *get_data_addr(void *img)
|
||||
{
|
||||
int bpp;
|
||||
int size_line;
|
||||
int endian;
|
||||
t_pixel *r;
|
||||
|
||||
r = (t_pixel *)mlx_get_data_addr(img, &bpp, &size_line, &endian);
|
||||
if (!r || bpp != 32 || endian != 0)
|
||||
err("wrong image format");
|
||||
return (r);
|
||||
}
|
||||
|
||||
void apply_mask_to_image(void *img, void *mask, int nb_px)
|
||||
{
|
||||
const t_pixel *addr_img = get_data_addr(img);
|
||||
const t_pixel *addr_mask = get_data_addr(mask);
|
||||
int i;
|
||||
|
||||
i = -1;
|
||||
while (++i < nb_px)
|
||||
((t_component *)(addr_img + i))[3]
|
||||
= 255 - ((t_component *)(addr_mask + i))[1];
|
||||
}
|
||||
|
||||
void load_xpm_file_with_alpha(int i)
|
||||
{
|
||||
t_string tmp;
|
||||
t_string src;
|
||||
t_string src_mask;
|
||||
void *mask;
|
||||
|
||||
tmp = str_join(err, g_env.mc, "assets/", g_assetsmap[i].src);
|
||||
src = str_join(err, g_env.mc, tmp, ".xpm");
|
||||
src_mask = str_join(err, g_env.mc, tmp, ".alpha.xpm");
|
||||
g_allassets[i].img = mlx_xpm_file_to_image(g_env.mlx, src,
|
||||
&(g_allassets[i].width), &g_allassets[i].height);
|
||||
if (!g_allassets[i].img)
|
||||
err("image loading failed");
|
||||
mask = mlx_xpm_file_to_image(g_env.mlx, src_mask,
|
||||
&(g_allassets[i].width), &g_allassets[i].height);
|
||||
if (!mask)
|
||||
err("image loading failed");
|
||||
apply_mask_to_image(g_allassets[i].img, mask,
|
||||
g_allassets[i].width * g_allassets[i].height);
|
||||
mlx_destroy_image(g_env.mlx, mask);
|
||||
g_allassets[i].data = get_data_addr(g_allassets[i].img);
|
||||
g_allassets[i].asset = g_assetsmap[i];
|
||||
mem_free(tmp);
|
||||
mem_free(src);
|
||||
mem_free(src_mask);
|
||||
}
|
||||
15
src/env.c
Normal file
15
src/env.c
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* env.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/26 16:32:36 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/02/27 16:42:22 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "env.h"
|
||||
|
||||
t_env g_env;
|
||||
36
src/error.c
Normal file
36
src/error.c
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* error.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/26 16:05:21 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/02/29 18:04:54 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "error.h"
|
||||
#include "libtf.h"
|
||||
#include "display.h"
|
||||
#include "env.h"
|
||||
|
||||
static void end(int no)
|
||||
{
|
||||
display_destroyall();
|
||||
mem_freeall(g_env.mc);
|
||||
exit(no);
|
||||
}
|
||||
|
||||
void err(t_const_string msg)
|
||||
{
|
||||
print_str(err_remember, 2, "error: ");
|
||||
print_line(err_remember, 2, msg);
|
||||
end(1);
|
||||
}
|
||||
|
||||
void finish(void)
|
||||
{
|
||||
end(0);
|
||||
}
|
||||
52
src/main.c
Normal file
52
src/main.c
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/26 15:28:34 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/03/06 16:18:09 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "mlx.h"
|
||||
#include "env.h"
|
||||
#include "error.h"
|
||||
#include "display.h"
|
||||
#include "assets.h"
|
||||
|
||||
int expose_hook(void)
|
||||
{
|
||||
display_flush();
|
||||
return (0);
|
||||
}
|
||||
|
||||
int close_hook(void)
|
||||
{
|
||||
finish();
|
||||
return (0);
|
||||
}
|
||||
|
||||
int loop_hook(void)
|
||||
{
|
||||
return (0);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
g_env.mc = NULL;
|
||||
g_env.mc = mem_newclass(err);
|
||||
g_env.mlx = mlx_init();
|
||||
if (!g_env.mlx)
|
||||
err("mlx_init() failed");
|
||||
display_init();
|
||||
g_env.win = mlx_new_window(g_env.mlx, WINDOW_WIDTH, WINDOW_HEIGHT,
|
||||
WINDOW_TITLE);
|
||||
if (!g_env.win)
|
||||
err("mlx_new_window() failed");
|
||||
mlx_expose_hook(g_env.win, expose_hook, NULL);
|
||||
mlx_hook(g_env.win, 17, 1L << 0, close_hook, NULL);
|
||||
mlx_loop_hook(g_env.mlx, loop_hook, NULL);
|
||||
mlx_loop(g_env.mlx);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue