@@ -14,8 +14,6 @@ static const int PLAYER_ID = 0;
1414static const float ATLAS_WIDTH = 512.0f ;
1515static const int ATLAS_MIP_LEVELS = 4 ;
1616static const float BLOCK_WIDTH = 16.0f ;
17- static const float PLAYER_SENSITIVITY = 0.1f ;
18- static const float PLAYER_REACH = 10.0f ;
1917static const int SHADOW_RESOLUTION = 4096.0f ;
2018static const float SHADOW_Y = 30.0f ;
2119static const float SHADOW_ORTHO = 300.0f ;
@@ -52,7 +50,6 @@ static SDL_GPUSampler* linear_sampler;
5250static SDL_GPUSampler * nearest_sampler ;
5351static camera_t shadow_camera ;
5452static player_t player ;
55- static world_query_t player_query ;
5653static Uint64 ticks1 ;
5754static Uint64 ticks2 ;
5855
@@ -69,44 +66,6 @@ static void update_shadow_camera()
6966 camera_update (& shadow_camera );
7067}
7168
72- static void save_or_load_player (bool save )
73- {
74- struct
75- {
76- float x ;
77- float y ;
78- float z ;
79- float pitch ;
80- float yaw ;
81- block_t block ;
82- }
83- data ;
84- if (save )
85- {
86- data .x = player .camera .x ;
87- data .y = player .camera .y ;
88- data .z = player .camera .z ;
89- data .pitch = player .camera .pitch ;
90- data .yaw = player .camera .yaw ;
91- data .block = player .block ;
92- save_set_player (PLAYER_ID , & data , sizeof (data ));
93- }
94- else
95- {
96- player_init (& player );
97- if (save_get_player (PLAYER_ID , & data , sizeof (data )))
98- {
99- player .block = data .block ;
100- player .camera .x = data .x ;
101- player .camera .y = data .y ;
102- player .camera .z = data .z ;
103- player .camera .pitch = data .pitch ;
104- player .camera .yaw = data .yaw ;
105- }
106- player_update_grounded (& player );
107- }
108- }
109-
11069static bool create_atlas ()
11170{
11271 char path [512 ] = {0 };
@@ -583,9 +542,8 @@ SDL_AppResult SDLCALL SDL_AppInit(void** appstate, int argc, char** argv)
583542 set_window_icon (BLOCK_GRASS );
584543 save_init (SAVE_PATH );
585544 world_init (device );
586- save_or_load_player ( false);
545+ player_save_or_load ( & player , PLAYER_ID , false);
587546 world_update (& player .camera );
588- player_query = world_raycast (& player .camera , PLAYER_REACH );
589547 update_shadow_camera ();
590548 ticks2 = SDL_GetTicks ();
591549 ticks1 = 0 ;
@@ -596,7 +554,7 @@ void SDLCALL SDL_AppQuit(void* appstate, SDL_AppResult result)
596554{
597555 SDL_HideWindow (window );
598556 world_free ();
599- save_or_load_player ( true);
557+ player_save_or_load ( & player , PLAYER_ID , true);
600558 save_free ();
601559 SDL_ReleaseGPUSampler (device , linear_sampler );
602560 SDL_ReleaseGPUSampler (device , nearest_sampler );
@@ -916,14 +874,14 @@ static void render_transparent(SDL_GPUCommandBuffer* cbuf, SDL_GPURenderPass* pa
916874
917875static void render_raycast (SDL_GPUCommandBuffer * cbuf , SDL_GPURenderPass * pass )
918876{
919- if (player_query .block == BLOCK_EMPTY )
877+ if (player . query .block == BLOCK_EMPTY )
920878 {
921879 return ;
922880 }
923881 SDL_PushGPUDebugGroup (cbuf , "raycast" );
924882 SDL_BindGPUGraphicsPipeline (pass , raycast_pipeline );
925883 SDL_PushGPUVertexUniformData (cbuf , 0 , player .camera .matrix , 64 );
926- SDL_PushGPUVertexUniformData (cbuf , 1 , player_query .current , 12 );
884+ SDL_PushGPUVertexUniformData (cbuf , 1 , player . query .current , 12 );
927885 SDL_DrawGPUPrimitives (pass , 36 , 1 , 0 , 0 );
928886 SDL_PopGPUDebugGroup (cbuf );
929887}
@@ -1036,54 +994,15 @@ SDL_AppResult SDLCALL SDL_AppIterate(void* appstate)
1036994 ticks1 = ticks2 ;
1037995 if (SDL_GetWindowRelativeMouseMode (window ))
1038996 {
1039- player_move (& player , dt , SDL_GetKeyboardState (NULL ));
1040- player_query = world_raycast (& player .camera , PLAYER_REACH );
1041- save_or_load_player (true);
997+ player_move (& player , dt );
998+ player_save_or_load (& player , PLAYER_ID , true);
1042999 }
10431000 update_shadow_camera ();
10441001 world_update (& player .camera );
10451002 render ();
10461003 return SDL_APP_CONTINUE ;
10471004}
10481005
1049- static void rotate_player (float pitch , float yaw )
1050- {
1051- player_rotate (& player , pitch , yaw , PLAYER_SENSITIVITY );
1052- player_query = world_raycast (& player .camera , PLAYER_REACH );
1053- }
1054-
1055- static void break_block ()
1056- {
1057- if (player_query .block != BLOCK_EMPTY )
1058- {
1059- world_set_block (player_query .current , BLOCK_EMPTY );
1060- }
1061- }
1062-
1063- static void select_block ()
1064- {
1065- if (player_query .block != BLOCK_EMPTY )
1066- {
1067- player .block = player_query .block ;
1068- }
1069- }
1070-
1071- static void place_block ()
1072- {
1073- if (player_query .block != BLOCK_EMPTY && !player_overlaps_block (& player , player_query .previous ))
1074- {
1075- world_set_block (player_query .previous , player .block );
1076- }
1077- }
1078-
1079- static void change_block (int dy )
1080- {
1081- static const int COUNT = BLOCK_COUNT - BLOCK_EMPTY - 1 ;
1082- int block = player .block - (BLOCK_EMPTY + 1 ) + dy ;
1083- block = (block + COUNT ) % COUNT ;
1084- player .block = block + BLOCK_EMPTY + 1 ;
1085- }
1086-
10871006SDL_AppResult SDLCALL SDL_AppEvent (void * appstate , SDL_Event * event )
10881007{
10891008 switch (event -> type )
@@ -1093,7 +1012,7 @@ SDL_AppResult SDLCALL SDL_AppEvent(void* appstate, SDL_Event* event)
10931012 case SDL_EVENT_MOUSE_MOTION :
10941013 if (SDL_GetWindowRelativeMouseMode (window ))
10951014 {
1096- rotate_player ( event -> motion .yrel , event -> motion .xrel );
1015+ player_rotate ( & player , event -> motion .yrel , event -> motion .xrel );
10971016 }
10981017 break ;
10991018 case SDL_EVENT_KEY_DOWN :
@@ -1105,7 +1024,6 @@ SDL_AppResult SDLCALL SDL_AppEvent(void* appstate, SDL_Event* event)
11051024 else if (event -> key .scancode == SDL_SCANCODE_F5 )
11061025 {
11071026 player_toggle_controller (& player );
1108- SDL_Log ("Controller mode: %s" , player_controller_name (player .controller ));
11091027 }
11101028 else if (event -> key .scancode == SDL_SCANCODE_F11 )
11111029 {
@@ -1130,20 +1048,20 @@ SDL_AppResult SDLCALL SDL_AppEvent(void* appstate, SDL_Event* event)
11301048 {
11311049 if (event -> button .button == SDL_BUTTON_LEFT )
11321050 {
1133- break_block ( );
1051+ player_break_block ( & player );
11341052 }
11351053 else if (event -> button .button == SDL_BUTTON_MIDDLE )
11361054 {
1137- select_block ( );
1055+ player_select_block ( & player );
11381056 }
11391057 else if (event -> button .button == SDL_BUTTON_RIGHT )
11401058 {
1141- place_block ( );
1059+ player_place_block ( & player );
11421060 }
11431061 }
11441062 break ;
11451063 case SDL_EVENT_MOUSE_WHEEL :
1146- change_block ( event -> wheel .y );
1064+ player_change_block ( & player , event -> wheel .y );
11471065 break ;
11481066 }
11491067 return SDL_APP_CONTINUE ;
0 commit comments