medfall

A super great game engine
Log | Files | Refs

main.cc (6332B)


      1 #include <stdio.h>
      2 #include <xmmintrin.h>
      3 #include <pmmintrin.h>
      4 
      5 #include "game.h"
      6 #include "log.h"
      7 #include "intrinsics.h"
      8 #include "gl.h"
      9 #include "keys.h"
     10 #include "profiler.h"
     11 #include "work_queue.h"
     12 #include "shaders.h"
     13 #include "text_renderer.h"
     14 #include "autogdb.h"
     15 
     16 #include "rng/csprng.h"
     17 
     18 #define GLFW_INCLUDE_NONE
     19 #include "libs/glfw/include/GLFW/glfw3.h"
     20 
     21 GameInit game_init;
     22 GameFrame game_frame;
     23 
     24 int main( int argc, char ** argv ) {
     25 	install_debug_signal_handlers();
     26 
     27 	size_t persistent_size = megabytes( 256 );
     28 	u8 * persistent_memory = ( u8 * ) malloc( persistent_size );
     29 	if( persistent_memory == NULL ) {
     30 		FATAL( "couldn't allocate persistent memory" );
     31 	}
     32 
     33 	GameMemory mem = { };
     34 	memarena_init( &mem.persistent_arena, persistent_memory, persistent_size );
     35 
     36 	static GameState state;
     37 	mem.state = &state;
     38 
     39 	logger_init();
     40 	logger_thread_name( "main" );
     41 	profiler_init();
     42 
     43 	csprng_init();
     44 
     45 	GLFWwindow * window = gl_init( WINDOW_GAME );
     46 	renderer_init();
     47 	shaders_init();
     48 	text_renderer_init( &mem.persistent_arena );
     49 	workqueue_init( &state.background_tasks, &mem.persistent_arena, 4 );
     50 	game_init( &state, &mem );
     51 
     52 #if RELEASE_BUILD
     53 	glfwSetInputMode( window, GLFW_CURSOR, GLFW_CURSOR_DISABLED );
     54 #endif
     55 
     56 	const double program_start_time = glfwGetTime();
     57 	double last_frame_time = program_start_time;
     58 	GameInput last_input = { };
     59 	u64 total_frames = 0;
     60 
     61 	_MM_SET_DENORMALS_ZERO_MODE( _MM_DENORMALS_ZERO_ON );
     62 	_MM_SET_FLUSH_ZERO_MODE( _MM_FLUSH_ZERO_ON );
     63 
     64 	double last_xpos, last_ypos;
     65 	glfwGetCursorPos( window, &last_xpos, &last_ypos );
     66 
     67 	v2u32 last_window_size = v2u32( 0, 0 );
     68 
     69 	double game_time = 0;
     70 
     71 	while( !glfwWindowShouldClose( window ) ) {
     72 		const double current_frame_time = glfwGetTime();
     73 		const float dt = min( 1.0f / 20.0f, float( current_frame_time - last_frame_time ) );
     74 		game_time += dt;
     75 
     76 		if( glfwGetKey( window, GLFW_KEY_Q ) == GLFW_PRESS || glfwGetKey( window, GLFW_KEY_ESCAPE ) == GLFW_PRESS ) {
     77 			break;
     78 		}
     79 
     80 		// TODO: do this properly
     81 		GameInput input = { };
     82 		input.keys[ KEY_A ] = glfwGetKey( window, GLFW_KEY_A ) == GLFW_PRESS;
     83 		input.keys[ KEY_C ] = glfwGetKey( window, GLFW_KEY_C ) == GLFW_PRESS;
     84 		input.keys[ KEY_D ] = glfwGetKey( window, GLFW_KEY_D ) == GLFW_PRESS;
     85 		input.keys[ KEY_F ] = glfwGetKey( window, GLFW_KEY_F ) == GLFW_PRESS;
     86 		input.keys[ KEY_G ] = glfwGetKey( window, GLFW_KEY_G ) == GLFW_PRESS;
     87 		input.keys[ KEY_I ] = glfwGetKey( window, GLFW_KEY_I ) == GLFW_PRESS;
     88 		input.keys[ KEY_J ] = glfwGetKey( window, GLFW_KEY_J ) == GLFW_PRESS;
     89 		input.keys[ KEY_K ] = glfwGetKey( window, GLFW_KEY_K ) == GLFW_PRESS;
     90 		input.keys[ KEY_L ] = glfwGetKey( window, GLFW_KEY_L ) == GLFW_PRESS;
     91 		input.keys[ KEY_M ] = glfwGetKey( window, GLFW_KEY_M ) == GLFW_PRESS;
     92 		input.keys[ KEY_N ] = glfwGetKey( window, GLFW_KEY_N ) == GLFW_PRESS;
     93 		input.keys[ KEY_S ] = glfwGetKey( window, GLFW_KEY_S ) == GLFW_PRESS;
     94 		input.keys[ KEY_T ] = glfwGetKey( window, GLFW_KEY_T ) == GLFW_PRESS;
     95 		input.keys[ KEY_W ] = glfwGetKey( window, GLFW_KEY_W ) == GLFW_PRESS;
     96 		input.keys[ KEY_SPACE ] = glfwGetKey( window, GLFW_KEY_SPACE ) == GLFW_PRESS;
     97 		input.keys[ KEY_LEFTSHIFT ] = glfwGetKey( window, GLFW_KEY_LEFT_SHIFT ) == GLFW_PRESS;
     98 		input.keys[ KEY_LEFTCTRL ] = glfwGetKey( window, GLFW_KEY_LEFT_CONTROL ) == GLFW_PRESS;
     99 		input.keys[ KEY_LEFTALT ] = glfwGetKey( window, GLFW_KEY_LEFT_ALT ) == GLFW_PRESS;
    100 		input.keys[ KEY_UPARROW ] = glfwGetKey( window, GLFW_KEY_UP ) == GLFW_PRESS;
    101 		input.keys[ KEY_DOWNARROW ] = glfwGetKey( window, GLFW_KEY_DOWN ) == GLFW_PRESS;
    102 		input.keys[ KEY_LEFTARROW ] = glfwGetKey( window, GLFW_KEY_LEFT ) == GLFW_PRESS;
    103 		input.keys[ KEY_RIGHTARROW ] = glfwGetKey( window, GLFW_KEY_RIGHT ) == GLFW_PRESS;
    104 		input.keys[ KEY_LEFTARROW ] = glfwGetKey( window, GLFW_KEY_LEFT ) == GLFW_PRESS;
    105 		input.keys[ KEY_RIGHTARROW ] = glfwGetKey( window, GLFW_KEY_RIGHT ) == GLFW_PRESS;
    106 		input.keys[ KEY_MINUS ] = glfwGetKey( window, GLFW_KEY_MINUS ) == GLFW_PRESS;
    107 		input.keys[ KEY_EQUALS ] = glfwGetKey( window, GLFW_KEY_EQUAL ) == GLFW_PRESS;
    108 
    109 		input.keys[ KEY_F1 ] = glfwGetKey( window, GLFW_KEY_F1 ) == GLFW_PRESS;
    110 		input.keys[ KEY_F2 ] = glfwGetKey( window, GLFW_KEY_F2 ) == GLFW_PRESS;
    111 		input.keys[ KEY_F3 ] = glfwGetKey( window, GLFW_KEY_F3 ) == GLFW_PRESS;
    112 		input.keys[ KEY_F4 ] = glfwGetKey( window, GLFW_KEY_F4 ) == GLFW_PRESS;
    113 		input.keys[ KEY_F5 ] = glfwGetKey( window, GLFW_KEY_F5 ) == GLFW_PRESS;
    114 		input.keys[ KEY_F6 ] = glfwGetKey( window, GLFW_KEY_F6 ) == GLFW_PRESS;
    115 		input.keys[ KEY_F7 ] = glfwGetKey( window, GLFW_KEY_F7 ) == GLFW_PRESS;
    116 		input.keys[ KEY_F8 ] = glfwGetKey( window, GLFW_KEY_F8 ) == GLFW_PRESS;
    117 		input.keys[ KEY_F9 ] = glfwGetKey( window, GLFW_KEY_F9 ) == GLFW_PRESS;
    118 		input.keys[ KEY_F10 ] = glfwGetKey( window, GLFW_KEY_F10 ) == GLFW_PRESS;
    119 		input.keys[ KEY_F11 ] = glfwGetKey( window, GLFW_KEY_F11 ) == GLFW_PRESS;
    120 		input.keys[ KEY_F12 ] = glfwGetKey( window, GLFW_KEY_F12 ) == GLFW_PRESS;
    121 
    122 		for( size_t i = 0; i < input.keys.size(); i++ ) {
    123 			input.key_edges[ i ] = input.keys[ i ] != last_input.keys[ i ];
    124 		}
    125 
    126 		break1 = input.keys[ KEY_F1 ];
    127 		break2 = input.keys[ KEY_F2 ];
    128 		break3 = input.keys[ KEY_F3 ];
    129 		break4 = input.keys[ KEY_F4 ];
    130 
    131 		double xpos, ypos;
    132 		glfwGetCursorPos( window, &xpos, &ypos );
    133 		input.mouse_dx = xpos - last_xpos;
    134 		input.mouse_dy = ypos - last_ypos;
    135 		last_xpos = xpos;
    136 		last_ypos = ypos;
    137 
    138 #if !RELEASE_BUILD
    139 		input.mouse_dx = 0;
    140 		input.mouse_dy = 0;
    141 #endif
    142 
    143 		v2u32 window_size = get_window_size();
    144 		input.resized = window_size.x != last_window_size.x || window_size.y != last_window_size.y;
    145 		last_window_size = window_size;
    146 
    147 		if( total_frames % 60 == 0 ) {
    148 			hotload_shaders();
    149 		}
    150 
    151 		game_frame( &state, &mem, &input, game_time, dt );
    152 
    153 		glfwSwapBuffers( window );
    154 		glfwPollEvents();
    155 
    156 		last_frame_time = current_frame_time;
    157 		last_input = input;
    158 		total_frames++;
    159 
    160 		unsigned int csr = _mm_getcsr();
    161 		if( ( csr & _MM_FLUSH_ZERO_ON ) == 0 || ( csr & _MM_DENORMALS_ZERO_ON ) == 0 ) {
    162 			printf( "daz/ftz got switched off\n" );
    163 		}
    164 	}
    165 
    166 	const double program_run_time = glfwGetTime() - program_start_time;
    167 
    168 	workqueue_term( &state.background_tasks );
    169 	text_renderer_term();
    170 	shaders_term();
    171 	renderer_term();
    172 	gl_term();
    173 
    174 	profiler_write_summary();
    175 
    176 	profiler_term();
    177 	logger_term();
    178 
    179 	printf( "FPS: %.1f\n", total_frames / program_run_time );
    180 
    181 	return 0;
    182 }