polyadvent

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

game.c (2949B)


      1 
      2 #include "mat_util.h"
      3 #include "game.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 game *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 game *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("SDL2/OpenGL Demo", 0, 0, width, height,
     51                                           SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);
     52 
     53     SDL_GL_CreateContext(*window);
     54 }
     55 
     56 void quit_game(struct game *game)
     57 {
     58     game->quit = 1;
     59 }
     60 
     61 // TODO: cleanup
     62 void init_misc(struct game *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 game_init(struct game *game, int width, int height) {
     94 	game->width = width;
     95 	game->height = height;
     96     debug("init sdl...\n");
     97     init_sdl(&game->window, width, height);
     98     debug("init gl...\n");
     99     init_gl();
    100     debug("init entities...\n");
    101     init_entity_system();
    102     init_geometry_manager();
    103     init_model_manager();
    104     init_node_manager();
    105     init_user_settings(&game->user_settings);
    106 
    107     check_gl();
    108 
    109     game->wireframe = 0;
    110 
    111     init_input(&game->input);
    112     init_controller(&game->input);
    113     //init_controller(&game->input);
    114     debug("init misc...\n");
    115     init_misc(game);
    116 
    117     printf("gpu programs %d\n", game->gpu.num_programs);
    118 }