misc: many things

This commit is contained in:
mcolonna 2024-03-07 23:37:34 +01:00
parent 664b18eb2e
commit 8076487071
25 changed files with 554 additions and 73 deletions

View file

@ -1,21 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* assets.c :+: :+: :+: */
/* data_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 */
/* Updated: 2024/03/11 15:54:15 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "display.h"
#include "data_assets.h"
/**
* Path of each asset relative to assets/, without extension.
*/
const t_asset g_assetsmap[NB_ASSETS] = {
const t_assetinfo g_assetsmap[NB_ASSETS] = {
{"floor/1", 50, 100, 50, 100, true},
{"floor/2", 50, 100, 50, 100, true},
// WALL

18
src/data_sprites.c Normal file
View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* data_sprites.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/11 16:02:29 by mcolonna #+# #+# */
/* Updated: 2024/03/11 16:37:05 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "data_sprites.h"
const t_spriteinfo g_spritesmap[NB_SPRITES] = {
{WALL_BORDER, 1, 50, 50},
{FLOOR_1, 1, 50, 50}
};

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/26 15:05:30 by mcolonna #+# #+# */
/* Updated: 2024/03/06 13:08:51 by mcolonna ### ########.fr */
/* Updated: 2024/03/11 13:03:37 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
@ -29,6 +29,8 @@ void display_init(void)
i++;
}
g_screenbuf.img = mlx_new_image(g_env.mlx, WINDOW_WIDTH, WINDOW_HEIGHT);
if (!g_screenbuf.img)
err("on mlx_new_image()");
g_screenbuf.width = WINDOW_WIDTH;
g_screenbuf.height = WINDOW_HEIGHT;
g_screenbuf.data = get_data_addr(g_screenbuf.img);

View file

@ -6,11 +6,13 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/26 16:05:21 by mcolonna #+# #+# */
/* Updated: 2024/02/29 18:04:54 by mcolonna ### ########.fr */
/* Updated: 2024/03/11 13:37:12 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "error.h"
#include "libtf.h"
#include "display.h"
@ -25,7 +27,28 @@ static void end(int no)
void err(t_const_string msg)
{
print_str(err_remember, 2, "error: ");
print_line(err_remember, 2, "Error");
if (str_eq(msg, "errno"))
print_line(err_remember, 2, strerror(errno));
else
print_line(err_remember, 2, msg);
end(1);
}
void err_perror(t_const_string s)
{
print_line(err_remember, 2, "Error");
print_str(err_remember, 2, s);
print_str(err_remember, 2, ": ");
print_line(err_remember, 2, strerror(errno));
end(1);
}
void err_perror_str(t_const_string s, t_const_string msg)
{
print_line(err_remember, 2, "Error");
print_str(err_remember, 2, s);
print_str(err_remember, 2, ": ");
print_line(err_remember, 2, msg);
end(1);
}

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/26 15:28:34 by mcolonna #+# #+# */
/* Updated: 2024/03/06 16:18:09 by mcolonna ### ########.fr */
/* Updated: 2024/03/11 15:50:28 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,7 +14,8 @@
#include "env.h"
#include "error.h"
#include "display.h"
#include "assets.h"
#include "data_assets.h"
#include "room.h"
int expose_hook(void)
{
@ -35,7 +36,6 @@ int loop_hook(void)
int main(void)
{
g_env.mc = NULL;
g_env.mc = mem_newclass(err);
g_env.mlx = mlx_init();
if (!g_env.mlx)
@ -45,6 +45,8 @@ int main(void)
WINDOW_TITLE);
if (!g_env.win)
err("mlx_new_window() failed");
g_env.room = room_fromfile("room/room.ber");
room_draw(g_env.room);
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);

79
src/room.c Normal file
View file

@ -0,0 +1,79 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* room.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/07 23:31:22 by mcolonna #+# #+# */
/* Updated: 2024/03/13 16:37:06 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include <fcntl.h>
#include "room.h"
#include "error.h"
#include "libtf.h"
#include "env.h"
#include "room_utils.h"
static bool room_fromfile2(
int fd, t_memclass mc, t_const_string path, t_sprite **surface)
{
t_const_string line;
line = read_line(err_remember, mc, fd);
if (err_get())
err_perror(path);
if (!line)
return (false);
while (*line && *line != '\n')
{
**surface = sprite_init(getroomcase(path, *line)->surface_spr);
line++;
(*surface)++;
}
return (true);
}
t_room room_fromfile(t_const_string path)
{
t_room r;
int fd;
t_sprite *surface;
const t_memclass mc = mem_subclass(err, g_env.mc);
room_getsize(mc, &r, path);
r.mc = mem_subclass(err, g_env.mc);
r.surfaces = mem_alloc(err, r.mc, r.width * r.height * sizeof(t_sprite));
fd = open(path, O_RDONLY);
if (fd == -1)
err_perror(path);
surface = r.surfaces;
while (surface - r.surfaces < r.width * r.height
&& room_fromfile2(fd, mc, path, &surface))
;
mem_freeall(mc);
return (r);
}
void room_draw(t_room room)
{
int x;
int y;
y = -1;
while (++y < room.height)
{
x = -1;
while (++x < room.width)
{
sprite_draw(x * 50, y * 50, room.surfaces + y * room.width + x);
}
}
}
void room_free(t_room room)
{
mem_freeall(room.mc);
}

73
src/room_utils.c Normal file
View file

@ -0,0 +1,73 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* room_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/13 16:26:28 by mcolonna #+# #+# */
/* Updated: 2024/03/13 16:55:56 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include <fcntl.h>
#include "room_utils.h"
#include "error.h"
static t_roomcase g_roomcases[] = {
{c: '0', surface_spr: CASE_FLOOR},
{c: '1', surface_spr: CASE_WALL},
{c: '\0'}
};
int getlinelen(t_const_string line)
{
int r;
r = str_len(line);
if (line[r - 1] == '\n')
r--;
return (r);
}
void room_getsize(t_memclass mc, t_room *r, t_const_string path)
{
int fd;
t_const_string line;
fd = open(path, O_RDONLY);
if (fd == -1)
err_perror(path);
line = read_line(err_remember, mc, fd);
if (err_get())
err_perror(path);
if (!line)
err_perror_str(path, "room is empty");
r->width = getlinelen(line);
r->height = 1;
while (true)
{
line = read_line(err_remember, mc, fd);
if (err_get())
err_perror(path);
if (!line)
break ;
if (getlinelen(line) != r->width)
err_perror_str(path, "the lines have different lengths");
r->height++;
}
}
t_roomcase *getroomcase(t_const_string path, char c)
{
int i;
i = -1;
while (g_roomcases[++i].c)
{
if (g_roomcases[i].c == c)
return (&g_roomcases[i]);
}
err_perror_str(path, "unknown char");
return (NULL);
}

33
src/sprite.c Normal file
View file

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sprite.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/07 22:28:52 by mcolonna #+# #+# */
/* Updated: 2024/03/11 16:36:45 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "sprite.h"
#include "display.h"
t_sprite sprite_init(t_spriteid id)
{
t_sprite r;
r.info = g_spritesmap + id;
r.index = 0;
return (r);
}
void sprite_draw(int x, int y, t_sprite *spr)
{
display_draw(
x - spr->info->origin_x, y - spr->info->origin_y,
spr->info->first_frame + spr->index);
spr->index++;
if (spr->index >= spr->info->nb_frames)
spr->index = 0;
}