polyadvent

A game engine from scratch in C
git clone git://jb55.com/polyadvent
Log | Files | Refs | README

engine.c (2854B)


      1 
      2 #include "mat_util.h"
      3 #include "engine.h"
      4 #include "ply.h"
      5 #include "model.h"
      6 #include "terrain.h"
      7 #include "render.h"
      8 #include "util.h"
      9 #include "update.h"
     10 #include "entity.h"
     11 #include "texture.h"
     12 #include "stb_image.h"
     13 #include "skybox.h"
     14 #include "quickhull.h"
     15 #include "poisson.h"
     16 #include "procmesh.h"
     17 #include "util.h"
     18 
     19 #include <assert.h>
     20 
     21 mat4 *cam_init = (float[16]){
     22   0.955761, 0.228018, -0.185425, 0.000000,
     23   -0.293528, 0.779583, -0.552437, 0.000000,
     24   0.018780, 0.580299, 0.802257, 0.000000,
     25   -71.766136, -47.881512, -44.216671, 1.000000
     26 };
     27 
     28 bool was_key_pressed_this_frame(struct engine *game, int scancode)
     29 {
     30     return is_key_down_on_frame(&game->input, scancode, game->frame);
     31 }
     32 
     33 bool was_button_pressed_this_frame(struct engine *game, SDL_GameControllerButton button)
     34 {
     35     return is_button_down_on_frame(&game->input, button, game->frame);
     36 }
     37 
     38 static void init_user_settings(struct user_settings *settings) {
     39     SDL_SetRelativeMouseMode(SDL_TRUE);
     40     settings->mouse_sens = 0.1;
     41 }
     42 
     43 
     44 static void init_sdl(SDL_Window **window, int width, int height)
     45 {
     46     SDL_Init( SDL_INIT_JOYSTICK );
     47 
     48     /* SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); */
     49 
     50     *window = SDL_CreateWindow("polyrogue", 0, 0, width, height,
     51                                           SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);
     52 
     53     SDL_GL_CreateContext(*window);
     54 }
     55 
     56 void quit_game(struct engine *game)
     57 {
     58     game->quit = 1;
     59 }
     60 
     61 // TODO: cleanup
     62 void init_misc(struct engine *game) {
     63     game->quit = 0;
     64     game->frame = 0;
     65     game->gpu.num_programs = 0;
     66 
     67 //debug("creating ui...\n");
     68     //check_gl();
     69 }
     70 
     71 void init_controller(struct input *input) {
     72     SDL_GameControllerAddMappingsFromFile("data/gamecontrollerdb.txt");
     73 
     74     int joysticks = SDL_NumJoysticks();
     75     SDL_GameController *controller = NULL;
     76 
     77     printf("Found %d joysticks\n", joysticks);
     78 
     79     for (int i = 0; i < joysticks; i++) {
     80         if (SDL_IsGameController(i)) {
     81             controller = SDL_GameControllerOpen(i);
     82             if (controller) {
     83                 printf("Found a game controller\n");
     84                 input->controller = controller;
     85                 break;
     86             }
     87         } else {
     88             printf("Could not open game controller %d: %s\n", i, SDL_GetError());
     89         }
     90     }
     91 }
     92 
     93 void init_engine(struct engine *game, int width, int height)
     94 {
     95 	game->width = width;
     96 	game->height = height;
     97 	debug("init sdl...\n");
     98 	init_sdl(&game->window, width, height);
     99 	debug("init gl...\n");
    100 	init_gl();
    101 	debug("init entities...\n");
    102 	init_entity_system();
    103 	init_geometry_manager();
    104 	init_model_manager();
    105 	init_node_manager();
    106 	init_user_settings(&game->user_settings);
    107 	
    108 	check_gl();
    109 	
    110 	game->gpu.wireframe = 0;
    111 	
    112 	init_input(&game->input);
    113 	init_controller(&game->input);
    114 	//init_controller(&game->input);
    115 	debug("init misc...\n");
    116 	init_misc(game);
    117 }