part: implement some functions (windows & display)

part:
- remove mlx3ds_3dskey.h, instead use hid.h
- add definitions of:
  - mlx_init()
  - mlx_new_window()
  - mlx_clear_window()
  - mlx_destroy_window()
  - mlx_pixel_put()
  - mlx_new_image()
  - mlx_get_data_addr()
  - mlx_put_image_to_window()
  - mlx_destroy_image()

dev:
- Makefile: add rule check_headers
- Makefile: add variable FLAGS
    (call 'make rule FLAGS=flags'
	to add flags to compilation)
- add /dev/norm.sh: nicer norminette
- add tester in main(), and related utilsconsole.*

docs:
- make clear mlx_get_data_addr() docs
This commit is contained in:
Zy 2024-10-06 16:24:31 +02:00
parent c7d0dba4fd
commit 13b93106ee
19 changed files with 766 additions and 141 deletions

View file

@ -33,7 +33,6 @@
#ifndef MLX_H
# define MLX_H
# include "mlx3ds_3dskey.h"
# include "mlx3ds_typealiases.h"
# include "mlx_init.h"
# include "mlx_window.h"

View file

@ -1,95 +0,0 @@
/**
* mlx3ds_3dskey.h
* for the project "MinilibX for 3DS"
* by Zy
* at https://github.com/frzysk/mlx3ds
*/
/**
* Enum of all the keys of the 3ds.
*
* These are the same constants defined in hid.h from devkitPro,
* but with documentation that basically repeat what the name
* of the constants already say.
*/
#ifndef MLX3DS_3DSKEY_H
# define MLX3DS_3DSKEY_H
# define BIT(n) 1U << n
/// @brief Represents a key of the 3DS.
typedef enum e_3dskey
{
/* BUTTONS */
/// @brief A button
KEY_A = BIT(0),
/// @brief B button
KEY_B = BIT(1),
/// @brief X button
KEY_X = BIT(10),
/// @brief Y button
KEY_Y = BIT(11),
/// @brief Select button
KEY_SELECT = BIT(2),
/// @brief Start button
KEY_START = BIT(3),
/// @brief R button
KEY_R = BIT(8),
/// @brief L button
KEY_L = BIT(9),
/* DIRECTIONS */
/// @brief D-Pad Right
KEY_DRIGHT = BIT(4),
/// @brief D-Pad Left
KEY_DLEFT = BIT(5),
/// @brief D-Pad Up
KEY_DUP = BIT(6),
/// @brief D-Pad Down
KEY_DDOWN = BIT(7),
/// @brief C-Pad Right
KEY_CPAD_RIGHT= BIT(28),
/// @brief C-Pad Left
KEY_CPAD_LEFT = BIT(29),
/// @brief C-Pad Up
KEY_CPAD_UP = BIT(30),
/// @brief C-Pad Down
KEY_CPAD_DOWN = BIT(31),
/* TOUCH */
// ??? unsure what KEY_TOUCH is... TODO
KEY_TOUCH = BIT(20),
/* New 3DS */
/// @brief C-Stick Right (only for New 3DS)
KEY_CSTICK_RIGHT = BIT(24),
/// @brief C-Stick Left (only for New 3DS)
KEY_CSTICK_LEFT = BIT(25),
/// @brief C-Stick Up (only for New 3DS)
KEY_CSTICK_UP = BIT(26),
/// @brief C-Stick Down (only for New 3DS)
KEY_CSTICK_DOWN = BIT(27),
/// @brief ZL button (only for New 3DS)
KEY_ZL = BIT(14),
/// @brief ZR button (only for New 3DS)
KEY_ZR = BIT(15),
// TODO implement catch-all directions?
/*
// ???
KEY_UP = KEY_DUP | KEY_CPAD_UP, ///< D-Pad Up or Circle Pad Up
// ???
KEY_DOWN = KEY_DDOWN | KEY_CPAD_DOWN, ///< D-Pad Down or Circle Pad Down
// ???
KEY_LEFT = KEY_DLEFT | KEY_CPAD_LEFT, ///< D-Pad Left or Circle Pad Left
// ???
KEY_RIGHT = KEY_DRIGHT | KEY_CPAD_RIGHT, ///< D-Pad Right or Circle PadRight
*/
} t_3dskey;
#endif

View file

@ -27,7 +27,6 @@
# define MLX_HOOK_H
# include "mlx3ds_typealiases.h"
# include "mlx3ds_3dskey.h"
/// @brief Doesn't do anything, the 3DS doesn't have a mouse.
///
@ -43,14 +42,14 @@ int _mlx_mouse_hook(t_win win_ptr, int (*funct_ptr)(), void *param);
/// @param win_ptr Window to affect.
/// @param funct_ptr Function to call when the event occurs. `keycode` is the
/// key pressed (note that they are obviously different than
/// the keyboard ones, see 'mlx3ds_3ds_key.h')).
/// the keyboard ones, use the KEY_* constants from hid.h)).
/// (TODO what was the name of the key constants use with mlx?)
/// `param` is set as the address given to mlx_key_hook().
/// The return value is unused.
/// @param param Address to pass to the function every time it is called.
/// @return Unused.
int _mlx_key_hook(t_win win_ptr,
int (*funct_ptr)(t_3dskey keycode, void *param), void *param);
int (*funct_ptr)(int keycode, void *param), void *param);
/// @brief Assign a function which will be called when the window should be
/// redrawn.
@ -90,4 +89,4 @@ int _mlx_loop_end(void *mlx_ptr);
int _mlx_hook(t_mlx win_ptr, int x_event, int x_mask,
int (*funct)(), void *param);
#endif
#endif

View file

@ -37,22 +37,23 @@
/// @param width Desired width of the image.
/// @param height Desired height of the image.
/// @return A reference to the image. NULL if failed.
t_image _mlx_new_image(void *mlx_ptr, int width, int height);
t_image mlx_new_image(void *mlx_ptr, int width, int height);
/// @brief Get an address containing all the pixel values of the image. The user
/// can then modify the image using this pointer directly.
///
/// @param img_ptr Image to use.
/// @param bits_per_pixel Will be filled with the number of bits used for each
/// pixel. Will always be 24.
/// pixel. Can be NULL, result will always be 24.
/// @param size_line Will be filled with the number of bytes used to store
/// a line of the image, aka 3 * width.
/// a line of the image, aka 3 * width. Can be NULL.
/// @param endian Will be filled with the endianness of all color values.
/// 1 for big endian, 0 for little endian. Will always be 1.
/// 1 for big endian, 0 for little endian. Can be NULL, result
/// will always be 1.
/// @return Address to the data of the image.
/// Use `data[y * size_line + x * 3]` to get the first color value (red)
/// of a specific pixel.
char *_mlx_get_data_addr(t_image img_ptr, int *bits_per_pixel,
char *mlx_get_data_addr(t_image img_ptr, int *bits_per_pixel,
int *size_line, int *endian);
/// @brief Draw an image on the window.
@ -63,7 +64,7 @@ char *_mlx_get_data_addr(t_image img_ptr, int *bits_per_pixel,
/// @param x x position of the image to draw.
/// @param y y position of the image to draw.
/// @return Unused.
int _mlx_put_image_to_window(t_mlx mlx_ptr, t_win win_ptr, t_image img_ptr,
int mlx_put_image_to_window(t_mlx mlx_ptr, t_win win_ptr, t_image img_ptr,
int x, int y);
// ???
@ -79,6 +80,6 @@ t_image _mlx_xpm_file_to_image(void *mlx_ptr, char *filename,
/// @param mlx_ptr mlx connection identifier returned by mlx_init().
/// @param img_ptr Image to destroy.
/// @return Unused.
int _mlx_destroy_image(t_mlx mlx_ptr, t_image img_ptr);
int mlx_destroy_image(t_mlx mlx_ptr, t_image img_ptr);
#endif
#endif

View file

@ -35,6 +35,8 @@
///
/// @return The mlx connection identifier to use for all other functions.
/// NULL if failed.
t_mlx _mlx_init(void); // TODO should call this before REALLY anything else?
t_mlx mlx_init(void); // TODO should call this before REALLY anything else?
#endif
// TODO a mlx_end() necessary?
#endif

View file

@ -41,7 +41,7 @@
/// @param y y position of the pixel to set.
/// @param color Color to use in 0x00RRGGBB format.
/// @return Unused.
int _mlx_pixel_put(t_mlx mlx_ptr, t_win win_ptr, int x, int y, int color);
int mlx_pixel_put(t_mlx mlx_ptr, t_win win_ptr, int x, int y, int color);
/// @brief Print text on the window.
///
@ -57,4 +57,4 @@ int _mlx_string_put(void *mlx_ptr, void *win_ptr, int x, int y, int color,
// ???
void _mlx_set_font(void *mlx_ptr, void *win_ptr, char *name);
#endif
#endif

View file

@ -41,20 +41,21 @@
/// Maximum 240 to be able to adapt to the top screen of the 3DS.
/// @param title The title of the window. Unused in the 3DS.
/// @return A reference to the window. NULL if failed.
t_win _mlx_new_window(t_mlx mlx_ptr, int size_x, int size_y, char *title);
t_win mlx_new_window(t_mlx mlx_ptr, int size_x, int size_y,
const char *title);
/// @brief Clear the window in black.
///
/// @param mlx_ptr mlx connection identifier returned by mlx_init().
/// @param win_ptr Window to affect.
/// @return Unused.
int _mlx_clear_window(t_mlx mlx_ptr, t_win win_ptr);
int mlx_clear_window(t_mlx mlx_ptr, t_win win_ptr);
/// @brief Destroy a window.
///
/// @param mlx_ptr mlx connection identifier returned by mlx_init().
/// @param win_ptr Window to destroy.
/// @return Unused.
int _mlx_destroy_window(t_mlx mlx_ptr, t_win win_ptr);
int mlx_destroy_window(t_mlx mlx_ptr, t_win win_ptr);
#endif
#endif

52
include/utilsconsole.hpp Normal file
View file

@ -0,0 +1,52 @@
/**
* utilsconsole.hpp
* by Zy
*/
/**
* utilsconsole.hpp (uc_*() functions)
* Utility functions for the 3DS console.
*/
#pragma once
#include <string>
#include <vector>
using namespace std;
/// @brief To use with uc_menu(): Represent an element of the menu.
typedef struct {
/// @brief Displayed name.
string name;
/// @brief true if this button can be used to change the value of a number.
bool is_number_prompt;
/// @brief Only if is_number_prompt is true: the number to change.
int *value;
/// @brief Only if is_number_prompt is true: the minimum value possible for
/// value.
int min;
/// @brief Only if is_number_prompt is true: the maximum value possible for
/// value.
int max;
} s_uc_menu_element;
/// @brief Display a menu with several items in the console.
/// @details
/// Non-blocking function, must be called in loop.
/// Only one menu can work at the same time. 'elements' must not change in
/// address.
/// @param elements A pointer to a vector of all the elements of the menu.
/// @return The index of the confirmed element. If none, return -1.
int uc_menu(vector<s_uc_menu_element> &elements);
/// @brief Display a menu quickly using only confirm buttons.
/// @details
/// Blocking function. Will return -1 if aptMainLoop() becomes false.
/// @param str Text representing each element. The last parameter must be NULL.
/// @return The index of the confirmed element.
int uc_menu_quick(const char *str, ...);
/// @brief Blocks the program until the user presses A.
/// @details Will also return if aptMainLoop() becomes false.
void uc_pause(void);