Jo Engine  2024.04.28
Jo Sega Saturn Engine
voxel.h
Go to the documentation of this file.
1 /*
2 ** Jo Sega Saturn Engine
3 ** Copyright (c) 2012-2024, Johannes Fetz (johannesfetz@gmail.com)
4 ** All rights reserved.
5 **
6 ** Redistribution and use in source and binary forms, with or without
7 ** modification, are permitted provided that the following conditions are met:
8 ** * Redistributions of source code must retain the above copyright
9 ** notice, this list of conditions and the following disclaimer.
10 ** * Redistributions in binary form must reproduce the above copyright
11 ** notice, this list of conditions and the following disclaimer in the
12 ** documentation and/or other materials provided with the distribution.
13 ** * Neither the name of the Johannes Fetz nor the
14 ** names of its contributors may be used to endorse or promote products
15 ** derived from this software without specific prior written permission.
16 **
17 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 ** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 ** DISCLAIMED. IN NO EVENT SHALL Johannes Fetz BE LIABLE FOR ANY
21 ** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 ** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 ** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
35 #ifndef __JO_VOXEL_H__
36 # define __JO_VOXEL_H__
37 
38 #ifdef JO_COMPILE_WITH_SOFTWARE_RENDERER_SUPPORT
39 
43 typedef struct
44 {
51 
54 typedef struct
55 {
64 
67 typedef struct
68 {
77 } jo_voxel;
78 
79 /*
80  ██████╗ ██████╗ ██████╗ ███████╗
81 ██╔════╝██╔═══██╗██╔══██╗██╔════╝
82 ██║ ██║ ██║██████╔╝█████╗
83 ██║ ██║ ██║██╔══██╗██╔══╝
84 ╚██████╗╚██████╔╝██║ ██║███████╗
85  ╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝
86 */
87 
92 void jo_voxel_do_angle_computation(jo_voxel * const voxel_data);
93 
98 void jo_voxel_redraw(jo_voxel * const voxel_data);
99 
104 void jo_voxel_run(jo_voxel * const voxel_data);
105 
110 static __jo_force_inline void jo_voxel_redraw_and_flush(jo_voxel * const voxel_data)
111 {
113  jo_voxel_redraw(voxel_data);
114  jo_software_renderer_flush(voxel_data->gfx);
115 }
116 
117 /*
118  ██████╗ █████╗ ███╗ ███╗███████╗██████╗ █████╗
119 ██╔════╝██╔══██╗████╗ ████║██╔════╝██╔══██╗██╔══██╗
120 ██║ ███████║██╔████╔██║█████╗ ██████╔╝███████║
121 ██║ ██╔══██║██║╚██╔╝██║██╔══╝ ██╔══██╗██╔══██║
122 ╚██████╗██║ ██║██║ ╚═╝ ██║███████╗██║ ██║██║ ██║
123  ╚═════╝╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝
124 */
125 
130 static __jo_force_inline void jo_voxel_increase_view_distance(jo_voxel * const voxel_data, const jo_fixed distance)
131 {
132  voxel_data->camera.zfar += distance;
133  jo_voxel_do_angle_computation(voxel_data);
134 }
135 
140 static __jo_force_inline void jo_voxel_decrease_view_distance(jo_voxel * const voxel_data, const jo_fixed distance)
141 {
142  voxel_data->camera.zfar -= distance;
143  jo_voxel_do_angle_computation(voxel_data);
144 }
145 
150 static __jo_force_inline void jo_voxel_horizon_up(jo_voxel * const voxel_data, const jo_fixed distance)
151 {
152  voxel_data->camera.horizon += distance;
153 }
154 
159 static __jo_force_inline void jo_voxel_horizon_down(jo_voxel *const voxel_data, const jo_fixed distance)
160 {
161  voxel_data->camera.horizon -= distance;
162 }
163 
168 static __jo_force_inline void jo_voxel_move_up(jo_voxel *voxel_data, const jo_fixed distance)
169 {
170  voxel_data->camera.height += distance;
171 }
172 
177 static __jo_force_inline void jo_voxel_move_down(jo_voxel * const voxel_data, const jo_fixed distance)
178 {
179  voxel_data->camera.height -= distance;
180 }
181 
186 static __jo_force_inline void jo_voxel_move_forward(jo_voxel * const voxel_data, const jo_fixed distance)
187 {
188  voxel_data->camera.x += jo_fixed_mult(jo_fixed_cos(voxel_data->camera.angle), distance);
189  voxel_data->camera.y += jo_fixed_mult(jo_fixed_sin(voxel_data->camera.angle), distance);
190 }
191 
196 static __jo_force_inline void jo_voxel_move_backward(jo_voxel * const voxel_data, const jo_fixed distance)
197 {
198  voxel_data->camera.x -= jo_fixed_mult(jo_fixed_cos(voxel_data->camera.angle), distance);
199  voxel_data->camera.y -= jo_fixed_mult(jo_fixed_sin(voxel_data->camera.angle), distance);
200 }
201 
206 static __jo_force_inline void jo_voxel_rotate_left(jo_voxel * const voxel_data, const jo_fixed angle_rad)
207 {
208  voxel_data->camera.angle -= angle_rad;
209  jo_voxel_do_angle_computation(voxel_data);
210 }
211 
216 static __jo_force_inline void jo_voxel_rotate_right(jo_voxel * const voxel_data, const jo_fixed angle_rad)
217 {
218  voxel_data->camera.angle += angle_rad;
219  jo_voxel_do_angle_computation(voxel_data);
220 }
221 
222 #endif // JO_COMPILE_WITH_SOFTWARE_RENDERER_SUPPORT
223 
224 #endif /* !__JO_VOXEL_H__ */
225 
226 /*
227 ** END OF FILE
228 */
__jo_voxel_computation_cache::ply
jo_fixed ply
Definition: voxel.h:46
jo_voxel_camera::height
jo_fixed height
Definition: voxel.h:59
jo_voxel_move_backward
static __jo_force_inline void jo_voxel_move_backward(jo_voxel *const voxel_data, const jo_fixed distance)
Move camera backward (camera.x & camera.y)
Definition: voxel.h:196
jo_software_renderer_clear
void jo_software_renderer_clear(const jo_software_renderer_gfx *const gfx, const jo_color color)
Clear the buffer with the given color.
__jo_voxel_computation_cache::map_width_minus_1
int map_width_minus_1
Definition: voxel.h:49
__jo_voxel_computation_cache::plx
jo_fixed plx
Definition: voxel.h:45
jo_voxel
Voxel main definition.
Definition: voxel.h:68
jo_voxel_rotate_left
static __jo_force_inline void jo_voxel_rotate_left(jo_voxel *const voxel_data, const jo_fixed angle_rad)
Rotate camera on the left (camera.angle)
Definition: voxel.h:206
jo_voxel::terrain_img
jo_img_8bits terrain_img
Definition: voxel.h:72
jo_voxel::height_pal
jo_palette height_pal
Definition: voxel.h:71
jo_voxel::use_zbuffer
bool use_zbuffer
Definition: voxel.h:75
jo_voxel_move_up
static __jo_force_inline void jo_voxel_move_up(jo_voxel *voxel_data, const jo_fixed distance)
Increase camera height (camera.height)
Definition: voxel.h:168
jo_voxel_redraw_and_flush
static __jo_force_inline void jo_voxel_redraw_and_flush(jo_voxel *const voxel_data)
Draw voxels and flush the buffer.
Definition: voxel.h:110
jo_voxel_camera::angle
jo_fixed angle
Definition: voxel.h:62
jo_voxel_camera::scale
jo_fixed scale
Definition: voxel.h:56
jo_palette
Palette contents struct.
Definition: types.h:250
jo_fixed_cos
jo_fixed jo_fixed_cos(jo_fixed rad)
Fast cosinus computation using fixed number.
jo_voxel::__cache
__jo_voxel_computation_cache __cache
Definition: voxel.h:76
jo_voxel::camera
jo_voxel_camera camera
Definition: voxel.h:74
jo_voxel_move_down
static __jo_force_inline void jo_voxel_move_down(jo_voxel *const voxel_data, const jo_fixed distance)
Decrease camera height (camera.height)
Definition: voxel.h:177
jo_voxel_camera::zfar
jo_fixed zfar
Definition: voxel.h:61
jo_img_8bits
8 bits image struct
Definition: types.h:234
jo_voxel_camera::y
jo_fixed y
Definition: voxel.h:58
jo_voxel_run
void jo_voxel_run(jo_voxel *const voxel_data)
Draw voxels to gfx.
__jo_force_inline
#define __jo_force_inline
force inline attribute (and prevent Doxygen prototype parsing bug)
Definition: types.h:39
jo_fixed
int jo_fixed
Fixed point Q16.16 number.
Definition: types.h:49
jo_voxel::gfx
jo_software_renderer_gfx * gfx
Definition: voxel.h:69
jo_software_renderer_gfx
This is the main struct that handle everything for drawing.
Definition: software_renderer.h:93
jo_voxel_move_forward
static __jo_force_inline void jo_voxel_move_forward(jo_voxel *const voxel_data, const jo_fixed distance)
Move camera forward (camera.x & camera.y)
Definition: voxel.h:186
jo_voxel::terrain_pal
jo_palette terrain_pal
Definition: voxel.h:70
__jo_voxel_computation_cache::px
jo_fixed px
Definition: voxel.h:47
jo_voxel_camera::x
jo_fixed x
Definition: voxel.h:57
jo_voxel_decrease_view_distance
static __jo_force_inline void jo_voxel_decrease_view_distance(jo_voxel *const voxel_data, const jo_fixed distance)
Decrease view distance (camera.zfar)
Definition: voxel.h:140
JO_COLOR_Black
#define JO_COLOR_Black
Definition: colors.h:57
jo_voxel_camera::horizon
jo_fixed horizon
Definition: voxel.h:60
jo_voxel::height_img
jo_img_8bits height_img
Definition: voxel.h:73
__jo_voxel_computation_cache
Voxel computation cache (internal engine usage)
Definition: voxel.h:44
__jo_voxel_computation_cache::py
jo_fixed py
Definition: voxel.h:48
jo_voxel_increase_view_distance
static __jo_force_inline void jo_voxel_increase_view_distance(jo_voxel *const voxel_data, const jo_fixed distance)
Increase view distance (camera.zfar)
Definition: voxel.h:130
jo_voxel_horizon_up
static __jo_force_inline void jo_voxel_horizon_up(jo_voxel *const voxel_data, const jo_fixed distance)
Increase horizon height (camera.horizon)
Definition: voxel.h:150
jo_fixed_mult
jo_fixed jo_fixed_mult(jo_fixed x, jo_fixed y)
Multiply to fixed number.
jo_voxel_do_angle_computation
void jo_voxel_do_angle_computation(jo_voxel *const voxel_data)
Do computation linked to camera angle.
jo_fixed_sin
jo_fixed jo_fixed_sin(jo_fixed rad)
Fast sinus computation using fixed number.
jo_voxel_rotate_right
static __jo_force_inline void jo_voxel_rotate_right(jo_voxel *const voxel_data, const jo_fixed angle_rad)
Rotate camera on the right (camera.angle)
Definition: voxel.h:216
jo_voxel_horizon_down
static __jo_force_inline void jo_voxel_horizon_down(jo_voxel *const voxel_data, const jo_fixed distance)
Decrease horizon height (camera.horizon)
Definition: voxel.h:159
jo_voxel_redraw
void jo_voxel_redraw(jo_voxel *const voxel_data)
Draw voxels to gfx.
jo_voxel_camera
Voxel camera definition.
Definition: voxel.h:55
jo_software_renderer_flush
void jo_software_renderer_flush(jo_software_renderer_gfx *const gfx)
Copy the buffer into VDP1/2 VRAM.